SCROLLER - Usage CustomWidgets (Sample)

From SEGGER Wiki
Jump to: navigation, search
SCROLLER_Usage_CustomWidgets.c
SCROLLER Usage CustomWidgets.png
File(s) required
  • SCROLLER_Usage_CustomWidgets.c
Runs in simulation Yes
Runs on target Yes
Download SCROLLER_Usage_CustomWidgets.c

This sample demonstrates how the SCROLLER widget can be attached to custom widgets or windows. For more information, please have a look at the emWin documentation.

Demo

Code

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

** emWin V6.24 - 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        : SCROLLER_Usage_CustomWidgets.c
Purpose     : Sample that demonstrates how SCROLLERs can be attached
              to custom windows or custom widgets.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
Wiki link   : https://wiki.segger.com/SCROLLER_-_Usage_CustomWidgets_%28Sample%29
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define COLOR_1   GUI_MAKE_COLOR(0x00DFD9CF)
#define COLOR_2   GUI_MAKE_COLOR(0x00F0EBE2)
#define TEXT      "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod" \
                  " tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At" \
                  " vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergre" \
                  "n, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor si" \
                  "t amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut " \
                  "labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam" \
                  " et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata s" \
                  "anctus est Lorem ipsum dolor sit amet."

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
//
// Scroll state of the window.
//
static WM_SCROLL_STATE _ScrollStateV;

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _GetContentRect
*/
static void _GetContentRect(WM_HWIN hParent, GUI_RECT * pRect) {
  WM_GetClientRectEx(hParent, pRect);
  GUI_AddRect(pRect, pRect, -20);
}

/*********************************************************************
*
*       _GetTextRect
*/
static void _GetTextRect(WM_HWIN hParent, GUI_RECT * pRect) {
  if (hParent && pRect) {
    _GetContentRect(hParent, pRect);
    GUI_AddRect(pRect, pRect, -10);
  }
}

/*********************************************************************
*
*       _UpdateScrollPos
*/
static void _UpdateScrollPos(WM_HWIN hParent, SCROLLER_Handle hScroller) {
  int Id;

  Id = WM_GetId(hScroller);
  switch (Id) {
  case GUI_ID_VSCROLLER:
    //
    // Set state to SCROLLER.
    // 
    WM_SetScrollState(hScroller, &_ScrollStateV);
    break;
  }
}

/*********************************************************************
*
*       _OnScrollerAdded
*/
static void _OnScrollerAdded(WM_HWIN hParent, SCROLLER_Handle hScroller) {
  int Id;

  Id = WM_GetId(hScroller);
  switch (Id) {
  case GUI_ID_VSCROLLER:
    //
    // Show vertical SCROLLERs.
    //
    WM_ShowWin(hScroller);
    break;
  case GUI_ID_HSCROLLER:
    //
    // Hide horizontal SCROLLERs.
    //
    WM_HideWin(hScroller);
    break;
  }
}

/*********************************************************************
*
*       _OnValueChanged
*/
static void _OnValueChanged(WM_HWIN hParent, SCROLLER_Handle hScroller, WM_SCROLL_STATE * pNewState) {
  int Id;

  Id = WM_GetId(hScroller);
  switch (Id) {
  case GUI_ID_VSCROLLER:
    //
    // Set new state to parent scroll state.
    //
    _ScrollStateV = *pNewState;
    WM_InvalidateWindow(hParent);
    break;
  }
}

/*********************************************************************
*
*       _InterfaceAPI
*/
static const SCROLLER_INTERFACE_API _InterfaceAPI = {
  _UpdateScrollPos,  // pfUpdateScrollPos
  _OnScrollerAdded,  // pfScrollerAdded
  _OnValueChanged,   // pfValueChanged
  _GetContentRect,   // pfGetContentRect
};

/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  GUI_RECT         Rect;
  GUI_RECT         CRect;
  WM_MOTION_INFO * pInfo;
  int              MaxOffset;

  switch (pMsg->MsgId) {
  case WM_CREATE:
    //
    // Set initial scroll state
    //
    GUI_SetFont(&GUI_Font32_1);
    _GetTextRect(pMsg->hWin, &Rect);
    MaxOffset = (GUI_WrapGetNumLines(TEXT, Rect.x1 - Rect.x0, GUI_WRAPMODE_WORD) * GUI_GetFontSizeY()) - (Rect.y1 - Rect.y0 + 1);
    WM_GetContentRectEx(pMsg->hWin, &Rect);
    _ScrollStateV.PageSize = (Rect.y1 - Rect.y0 + 1);
    _ScrollStateV.NumItems = MaxOffset + _ScrollStateV.PageSize;
    _ScrollStateV.v        = 0;
    break;
  case WM_MOTION:
    pInfo = (WM_MOTION_INFO *)pMsg->Data.p;
    switch (pInfo->Cmd) {
    case WM_MOTION_INIT:
      //
      // Tell the motion module to move in y direction and that we manage it on our own
      //
      pInfo->Flags = WM_CF_MOTION_Y | WM_MOTION_MANAGE_BY_WINDOW;
      break;
    case WM_MOTION_MOVE:
      //
      // Move the text rectangle up or down
      //
      _ScrollStateV.v -= pInfo->dy;
      if (_ScrollStateV.v > (_ScrollStateV.NumItems - _ScrollStateV.PageSize)) {
        //
        // If reach the end, make sure we stop there
        //
        _ScrollStateV.v = (_ScrollStateV.NumItems - _ScrollStateV.PageSize);
        pInfo->StopMotion = 1;
      }
      else if (_ScrollStateV.v < 0) {
        //
        // If reach the top, make sure we stop there
        //
        _ScrollStateV.v = 0;
        pInfo->StopMotion = 1;
      }
      //
      // Tell the window to redraw
      //
      WM_InvalidateWindow(pMsg->hWin);
      break;
    case WM_MOTION_GETPOS:
      pInfo->yPos = -_ScrollStateV.v;
      break;
    }
    break;
  case WM_PAINT:
    //
    // Draw something
    //
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    //
    GUI_SetColor(GUI_BLACK);
    WM_GetContentRectEx(pMsg->hWin, &Rect);
    GUI_SetPenSize(2);
    GUI_DrawGradientRoundedV(Rect.x0, Rect.y0, Rect.x1, Rect.y1, 3, COLOR_1, COLOR_2);
    GUI_AA_DrawRoundedRectEx(&Rect, 3);
    //
    // Copy current rectangle into a clip rectangle
    //
    CRect = Rect;
    GUI_SetClipRect(&CRect);
    //
    // Prepare rect to display text
    //
    _GetTextRect(pMsg->hWin, &Rect);
    GUI_SetFont(&GUI_Font32_1);
    //
    // Calculate size of rectangle, so that the entire text will fit in
    //
    Rect.y1 += GUI_WrapGetNumLines(TEXT, Rect.x1 - Rect.x0, GUI_WRAPMODE_WORD) * GUI_GetFontSizeY();
    //
    // Add the current yOffset to the rectangle, so the text is displayed accordingly
    //
    Rect.y0 -= _ScrollStateV.v;
    Rect.y1 -= _ScrollStateV.v;
    GUI_SetTextMode(GUI_TM_TRANS);
    GUI_DispStringInRectWrap(TEXT, &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
    //
    // Clear the clipping rectangle
    //
    GUI_SetClipRect(NULL);
    break;
  }
  //
  // Send messages also to SCROLLER_ParentMsgHandler().
  // If the routine does not handle the message, WM_DefaultProc() will be called.
  //
  if (SCROLLER_ParentMsgHandler(pMsg)) {
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  WM_HWIN         hParent;
  SCROLLER_Handle hScroller;

  GUI_Init();
  //
  // Enable motion support
  //
  WM_MOTION_Enable(1);
  WM_MULTIBUF_Enable(1);
  //
  // Create a custom window.
  //
  hParent = WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW | WM_CF_HASTRANS, _cbWin, 0);
  //
  // Attach SCROLLER to window.
  //
  hScroller = SCROLLER_CreateAttached(hParent, SCROLLER_CF_VERTICAL | SCROLLER_CF_TOUCH | SCROLLER_CF_FADING);
  SCROLLER_SetAlignOffset(hScroller, -5);
  //
  // Set API to SCROLLER widget.
  //
  SCROLLER_SetInterfaceAPI(hScroller, &_InterfaceAPI);

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

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