messagebox.c
上传用户:pch188
上传日期:2013-04-20
资源大小:6697k
文件大小:7k
源码类别:

微处理器开发

开发平台:

C/C++

  1. /* ********************************************************************************************************* *                                                uC/GUI *                        Universal graphic software for embedded applications * *                       (c) Copyright 2002, Micrium Inc., Weston, FL *                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH * *              礐/GUI is protected by international copyright laws. Knowledge of the *              source code may not be used to write a similar product. This file may *              only be used in accordance with a license and should not be redistributed *              in any way. We appreciate your understanding and fairness. * ----------------------------------------------------------------------
  2. File        : ProgBar.c
  3. Purpose     : Progress bar for emWin GSC
  4. ---------------------------END-OF-HEADER------------------------------
  5. */
  6. #include <string.h>             /* for memset */
  7. #include "GUI.h"
  8. #include "BUTTON.h"
  9. #include "FRAMEWIN.h"
  10. #include "TEXT.h"
  11. #include "DIALOG.h"
  12. #if GUI_WINSUPPORT
  13. /*********************************************************************
  14. *
  15. *       Defaults
  16. *
  17. **********************************************************************
  18. */
  19. #ifndef MESSAGEBOX_BORDER
  20.   #define MESSAGEBOX_BORDER 4
  21. #endif
  22. #ifndef MESSAGEBOX_XSIZEOK
  23.   #define MESSAGEBOX_XSIZEOK 50
  24. #endif
  25. #ifndef MESSAGEBOX_YSIZEOK
  26.   #define MESSAGEBOX_YSIZEOK 20
  27. #endif
  28. #ifndef MESSAGEBOX_BKCOLOR
  29.   #define MESSAGEBOX_BKCOLOR GUI_WHITE
  30. #endif
  31. #define ID_FRAME 100
  32. /*********************************************************************
  33. *
  34. *       Static functions
  35. *
  36. **********************************************************************
  37. */
  38. static WM_RESULT _MESSAGEBOX_cbCallback(WM_MESSAGE * pMsg) {
  39.   WM_HWIN hWin = pMsg->hWin;
  40.   switch (pMsg->MsgId) {
  41.     case WM_INIT_DIALOG:
  42.       FRAMEWIN_SetClientColor(hWin, MESSAGEBOX_BKCOLOR);
  43.       break;
  44.     case WM_KEY:
  45.       {
  46.         int Key = ((WM_KEY_INFO*)(pMsg->Data.p))->Key;
  47.         switch (Key) {
  48.         case GUI_KEY_ESCAPE:
  49.           GUI_EndDialog(hWin, 1);             /* End dialog with return value 1 if <ESC> is pressed */
  50.           break;
  51.         case GUI_KEY_ENTER:
  52.           GUI_EndDialog(hWin, 0);             /* End dialog with return value 0 if <ENTER> is pressed */
  53.           break;
  54.         }
  55.       }
  56.       break;
  57.     case WM_NOTIFY_PARENT:
  58.       {
  59.         int NCode = pMsg->Data.v;             /* Get notification code */
  60.         int Id    = WM_GetId(pMsg->hWinSrc);  /* Get control ID */
  61.         switch (NCode) {
  62.           case WM_NOTIFICATION_RELEASED:      /* React only if released */
  63.             if (Id == GUI_ID_OK) {
  64.               GUI_EndDialog(hWin, 0);         /* End dialog with return value 0 if OK */
  65.             }
  66.             break;
  67.         }
  68.       }
  69.       break;
  70.     default:
  71.       WM_DefaultProc(pMsg);
  72.   }
  73. }
  74. /*********************************************************************
  75. *
  76. *       Exported routines
  77. *
  78. **********************************************************************
  79. */
  80. int GUI_MessageBox(const char * sMessage, const char * sCaption, int Flags) {
  81.   GUI_WIDGET_CREATE_INFO _aDialogCreate[3];                                     /* 0: FrameWin, 1: Text, 2: Button */
  82.   int BorderSize = FRAMEWIN_GetDefaultBorderSize();                             /* Default border size of frame window */
  83.   int xSizeFrame = MESSAGEBOX_XSIZEOK + 2 * BorderSize + MESSAGEBOX_BORDER * 2; /* XSize of frame window */
  84.   int ySizeFrame;                                                               /* YSize of frame window */
  85.   int x0, y0;                                                                   /* Position of frame window */
  86.   int xSizeMessage;                                                             /* Length in pixels of message */
  87.   int xSizeCaption;                                                             /* Length in pixels of caption */
  88.   int ySizeCaption;                                                             /* YSize of caption */
  89.   int ySizeMessage;                                                             /* YSize of message */
  90.   GUI_RECT Rect;
  91.   const GUI_FONT * pOldFont;
  92.   /* Zeroinit variables */
  93.   memset(_aDialogCreate, 0, sizeof(_aDialogCreate));
  94.   /* Get dimension of message */
  95.   pOldFont = GUI_SetFont(TEXT_GetDefaultFont());
  96.   GUI_GetTextExtend(&Rect, sMessage, 255);
  97.   xSizeMessage = Rect.x1 - Rect.x0 + MESSAGEBOX_BORDER * 2;
  98.   ySizeMessage = Rect.y1 - Rect.y0 + 1;
  99.   if (xSizeFrame < (xSizeMessage + 4 + MESSAGEBOX_BORDER * 2))
  100.     xSizeFrame = xSizeMessage + 4 + MESSAGEBOX_BORDER * 2;
  101.   ySizeCaption = GUI_GetYSizeOfFont(FRAMEWIN_GetDefaultFont());
  102.   if (ySizeCaption < FRAMEWIN_GetDefaultCaptionSize())
  103.     ySizeCaption = FRAMEWIN_GetDefaultCaptionSize();
  104.   ySizeFrame = ySizeMessage +            /* size of message */
  105.                MESSAGEBOX_YSIZEOK +      /* size of button */
  106.                ySizeCaption +            /* caption size */
  107.                MESSAGEBOX_BORDER * 3 +   /* inner border - text, text - button, button - bottom */
  108.                BorderSize * 2 +          /* top & bottom border */
  109.                1;                        /* inner border */
  110.   /* Get xsize of caption */
  111.   xSizeCaption = GUI_GetStringDistX(sCaption);
  112.   if (xSizeFrame < xSizeCaption + BorderSize * 2)
  113.     xSizeFrame = xSizeCaption + BorderSize * 2;
  114.   /* Check maximum */
  115.   if (xSizeFrame > LCD_GET_XSIZE())
  116.     xSizeFrame = LCD_GET_XSIZE();
  117.   if (ySizeFrame > LCD_GET_YSIZE())
  118.     ySizeFrame = LCD_GET_YSIZE();
  119.   /* Calculate position of framewin */
  120.   x0 = (LCD_GET_XSIZE() - xSizeFrame) / 2;
  121.   y0 = (LCD_GET_YSIZE() - ySizeFrame) / 2;
  122.   /* Fill frame win ressource */
  123.   _aDialogCreate[0].pfCreateIndirect = FRAMEWIN_CreateIndirect;
  124.   _aDialogCreate[0].pName            = sCaption;
  125.   _aDialogCreate[0].x0               = x0;
  126.   _aDialogCreate[0].y0               = y0;
  127.   _aDialogCreate[0].xSize            = xSizeFrame;
  128.   _aDialogCreate[0].ySize            = ySizeFrame;
  129.   if (Flags & GUI_MESSAGEBOX_CF_MOVEABLE) {
  130.     _aDialogCreate[0].Flags          = FRAMEWIN_CF_MOVEABLE;
  131.   }
  132.   /* Fill text ressource */
  133.   _aDialogCreate[1].pfCreateIndirect = TEXT_CreateIndirect;
  134.   _aDialogCreate[1].pName            = sMessage;
  135.   _aDialogCreate[1].x0               = (xSizeFrame - xSizeMessage - BorderSize * 2) / 2;
  136.   _aDialogCreate[1].y0               = MESSAGEBOX_BORDER;
  137.   _aDialogCreate[1].xSize            = xSizeMessage;
  138.   _aDialogCreate[1].ySize            = ySizeMessage;
  139.   _aDialogCreate[1].Para             = GUI_TA_TOP | GUI_TA_HCENTER;
  140.   /* Fill button ressource */
  141.   _aDialogCreate[2].pfCreateIndirect = BUTTON_CreateIndirect;
  142.   _aDialogCreate[2].pName            = "OK";
  143.   _aDialogCreate[2].Id               = GUI_ID_OK;
  144.   _aDialogCreate[2].x0               = (xSizeFrame - MESSAGEBOX_XSIZEOK - BorderSize * 2) / 2;
  145.   _aDialogCreate[2].y0               = MESSAGEBOX_BORDER * 2 + ySizeMessage;
  146.   _aDialogCreate[2].xSize            = MESSAGEBOX_XSIZEOK;
  147.   _aDialogCreate[2].ySize            = MESSAGEBOX_YSIZEOK;
  148.   /* Exec dialog */
  149.   GUI_ExecDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _MESSAGEBOX_cbCallback, 0, 0, 0);
  150.   GUI_SetFont(pOldFont);
  151.   return 0;
  152. }
  153. #else
  154. void GUI_MessageBox_C(void) {} /* avoid empty object files */
  155. #endif /* GUI_WINSUPPORT */