FRAMEWIN - Usage (Sample)

From SEGGER Wiki
Jump to: navigation, search
FRAMEWIN_Usage.c
FRAMEWIN Usage.gif
File(s) required
  • FRAMEWIN_Usage.c
Runs in simulation Yes
Runs on target Yes
Download FRAMEWIN_Usage.c

This sample demonstrates the usage of a FRAMEWIN widget.

Demo

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin 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  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : FRAMEWIN_Usage.c
Purpose     : Sample that demonstrates the usage of FRAMEWIN widgets
              in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdlib.h>

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_FRAMEWIN 0
#define NUM_COLORS  GUI_COUNTOF(_aRandomColors)
#define TEXT        "This is a resizeable framewin... Click the button above to set a random background color."

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       _aRandomColors: Colors that will be randomly picked.
*/
static const GUI_COLOR _aRandomColors[] = {
  GUI_BLUE,
  GUI_GREEN,
  GUI_RED,
  GUI_CYAN,
  GUI_MAGENTA,
  GUI_YELLOW,
  GUI_LIGHTBLUE,
  GUI_LIGHTGREEN,
  GUI_LIGHTRED,
  GUI_LIGHTCYAN,
  GUI_LIGHTMAGENTA,
  GUI_LIGHTYELLOW,
  GUI_DARKBLUE,
  GUI_DARKGREEN,
  GUI_DARKRED,
  GUI_DARKCYAN,
  GUI_DARKMAGENTA,
  GUI_DARKYELLOW,
  GUI_WHITE,
  GUI_LIGHTGRAY,
  GUI_GRAY,
  GUI_DARKGRAY,
  GUI_BLACK,
  GUI_BROWN,
  GUI_ORANGE
};

static GUI_COLOR _Color;

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbButton
*
*  Function description:
*     Callback routine for the button created by the user.
*/
static void _cbButton(WM_MESSAGE * pMsg) {
  switch (pMsg->MsgId) {
  case WM_PAINT:
    BUTTON_Callback(pMsg);
    if (_Color) {
      GUI_SetColor(_Color);
    } else {
      GUI_SetColor(GUI_WHITE);
    }
    GUI_AA_FillCircle(WM_GetXSize(pMsg->hWin) / 2, WM_GetYSize(pMsg->hWin) / 2, WM_GetYSize(pMsg->hWin) / 4);
    break;
  default:
    BUTTON_Callback(pMsg);
    break;
  }
}

/*********************************************************************
*
*       _cbFramewinUserDraw
*
*  Function description:
*     User callback routine for framewin widget.
*     This callback can be used to draw the client window or to handle
*     any notifications, like button input etc...
*/
static void _cbFramewinUserDraw(WM_MESSAGE * pMsg) {
  GUI_RECT Rect;

  switch (pMsg->MsgId) {
  case WM_PAINT:
    if (_Color) {
      GUI_SetBkColor(_Color);
    } else {
      GUI_SetBkColor(GUI_WHITE);
    }
    GUI_Clear();
    WM_GetClientRect(&Rect);
    GUI_SetColor(GUI_BLACK);
    GUI_SetFont(&GUI_Font16_1);
    GUI_DispStringInRectWrap(TEXT, &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
    break;
  default:
    FRAMEWIN_Callback(pMsg);
    break;
  }
}

/*********************************************************************
*
*       _cbFrame
*
*  Function description:
*    Second callback routine for the framewin.
*    Necessary to handle button input or any notification messages sent
*    to the parent.
*/
static void _cbFrame(WM_MESSAGE * pMsg) {
  int Id;
  int NCode;

  switch (pMsg->MsgId) {
  case WM_NOTIFY_PARENT: // Handle input of custom buttons we added
    Id = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch (Id) {
    case GUI_ID_BUTTON0:
      switch (NCode) {
      case WM_NOTIFICATION_RELEASED:
        srand(GUI_GetTime());
        _Color = _aRandomColors[rand() % NUM_COLORS];
        WM_InvalidateWindow(pMsg->hWin);
        break;
      }
      break;
    default: // any other buttons than our custom created one are handled normally
      FRAMEWIN_Callback(pMsg);
      break;
    }
    break;
  default:
    FRAMEWIN_Callback(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  FRAMEWIN_Handle hFrame;
  WM_HWIN         hButton;

  //
  // Init GUI
  //
  GUI_Init();
  //
  // Enable multi-buffering to avoid flickering during movement of a framewin
  //
  WM_MULTIBUF_Enable(1);
  //
  // Set desktop color so that the background window will be cleared.
  //
  WM_SetDesktopColor(GUI_BLACK);
  //
  // Create FRAMEWIN with user callback
  //
  hFrame = FRAMEWIN_CreateEx(10, 10, 130, 100, WM_HBKWIN, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE, ID_FRAMEWIN, "Sample Framewin", _cbFramewinUserDraw);
  WM_SetCallback(hFrame, _cbFrame);
  //
  // Add some default buttons to the framewin
  //
  FRAMEWIN_AddCloseButton(hFrame, FRAMEWIN_BUTTON_RIGHT, 0);
  FRAMEWIN_AddMaxButton(hFrame, FRAMEWIN_BUTTON_RIGHT, 0);
  FRAMEWIN_AddMinButton(hFrame, FRAMEWIN_BUTTON_RIGHT, 0);
  //
  // Make framewin resizeable
  //
  FRAMEWIN_SetResizeable(hFrame, 1);
  //
  // Add a custom button
  //
  hButton = FRAMEWIN_AddButton(hFrame, FRAMEWIN_BUTTON_RIGHT, 10, GUI_ID_BUTTON0);
  WM_SetCallback(hButton, _cbButton);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/