/*********************************************************************
*
*          MainTask
*/
void MainTask(void) {
  int Index;

  GUI_Init();
  //
  // Check if recommended memory for the sample is available
  //
  if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
    GUI_ErrorOut("Not enough memory available."); 
    return;
  }
  #if GUI_SUPPORT_MEMDEV
    WM_SetCreateFlags(WM_CF_MEMDEV);
  #endif
  WM_SetDesktopColor(GUI_BLACK);
  //
  // Initialize the temperature arrays
  //
  _InitRandomData(_aTemp1, GUI_COUNTOF(_aTemp1));
  _InitRandomData(_aTemp2, GUI_COUNTOF(_aTemp2));
  //
  // Execute the intro dialog
  //
  GUI_ExecDialogBox(_aDialogIntro, GUI_COUNTOF(_aDialogIntro), _cbDialogIntro, WM_HBKWIN, 0, 0);
  //
  // Execute the color and the temperature dialog
  //
  _hDialogColor = GUI_CreateDialogBox(_aDialogColor, GUI_COUNTOF(_aDialogColor), _cbDialogColor, WM_HBKWIN, 0, 0);
  _hDialogMain  = GUI_CreateDialogBox(_aDialogMain,  GUI_COUNTOF(_aDialogMain),  _cbDialogMain,  WM_HBKWIN, 0, 0);
  //
  // Add new temperatures...
  //
  Index = GUI_COUNTOF(_aTemp1) - 1;
  while (1) {
    WM_HWIN hItem;
    GUI_Delay(100); // Wait a while
    //
    // Shift the color arrays
    //
    memmove(_aTemp1, _aTemp1 + 1, sizeof(_aTemp1) - 2);
    memmove(_aTemp2, _aTemp2 + 1, sizeof(_aTemp2) - 2);
    //
    // Add new values
    //
    _aTemp1[Index] = _GetRandomValue(_aTemp1[Index - 1]);
    _aTemp2[Index] = _GetRandomValue(_aTemp2[Index - 1]);
    //
    // Update windows
    //
    hItem = WM_GetClientWindow(WM_GetDialogItem(_hDialogMain, ID_TEMPERATURE));
    WM_InvalidateWindow(hItem);
    _SetProgbarValue(GUI_ID_PROGBAR0, _aTemp1[Index]);
    _SetProgbarValue(GUI_ID_PROGBAR1, _aTemp2[Index]);
  }
}
/*********************************************************************
*
*       _cbDialogMain
*
* Function description
*   Callback routine of DialogMain
*/
static void _cbDialogMain(WM_MESSAGE * pMsg) {
  WM_HWIN hDlg;
  WM_HWIN hItem;
  int     Id;

  hDlg = pMsg->hWin;
  switch (pMsg->MsgId) {
  case WM_PAINT:
    break;
  case WM_INIT_DIALOG:
    //
    // Init progress bars
    //
    hItem = WM_GetDialogItem(hDlg, GUI_ID_PROGBAR0);
    WIDGET_SetEffect(hItem, &WIDGET_Effect_3D);
    _SetProgbarValue(GUI_ID_PROGBAR0, _aTemp1[GUI_COUNTOF(_aTemp1) - 1]);
    hItem = WM_GetDialogItem(hDlg, GUI_ID_PROGBAR1);
    WIDGET_SetEffect(hItem, &WIDGET_Effect_3D);
    _SetProgbarValue(GUI_ID_PROGBAR1, _aTemp2[GUI_COUNTOF(_aTemp2) - 1]);
    //
    // Init edit widgets
    //
    hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT0);
    EDIT_SetDecMode(hItem, _TempMin, 0, 999, 0, 0);
    WM_DisableWindow(hItem);
    hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1);
    EDIT_SetDecMode(hItem, _TempMax, 0, 999, 0, 0);
    WM_DisableWindow(hItem);
    //
    // Init temperature window *
    //
    hItem = WM_GetClientWindow(WM_GetDialogItem(hDlg, ID_TEMPERATURE));
    _pcbCallbackTemperature = WM_SetCallback(hItem, _cbTemperature);
    _UpdateTextColors(hDlg);
    break;
  case WM_NOTIFY_PARENT:
    if (pMsg->Data.v == WM_NOTIFICATION_RELEASED) {
      Id = WM_GetId(pMsg->hWinSrc);      // Id of widget
      switch (Id) {
      case GUI_ID_BUTTON0:
        WM_SetFocus(_hDialogColor);
        GUI_SetOrg(0, 240);
        break;
      }
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}
/*********************************************************************
*
*          MainTask
*/
void MainTask(void) {
  int Index;

  GUI_Init();
  #if GUI_SUPPORT_MEMDEV
    WM_SetCreateFlags(WM_CF_MEMDEV);
  #endif
  WM_SetDesktopColor(GUI_BLACK);
  //
  // Initialize the temperature arrays
  //
  _InitRandomData(_aTemp1, GUI_COUNTOF(_aTemp1));
  _InitRandomData(_aTemp2, GUI_COUNTOF(_aTemp2));
  //
  // Execute the intro dialog
  //
  GUI_ExecDialogBox(_aDialogIntro, GUI_COUNTOF(_aDialogIntro), _cbDialogIntro, WM_HBKWIN, 0, 0);
  //
  // Execute the color and the temperature dialog
  //
  _hDialogColor = GUI_CreateDialogBox(_aDialogColor, GUI_COUNTOF(_aDialogColor), _cbDialogColor, WM_HBKWIN, 0, 0);
  _hDialogMain  = GUI_CreateDialogBox(_aDialogMain,  GUI_COUNTOF(_aDialogMain),  _cbDialogMain,  WM_HBKWIN, 0, 0);
  //
  // Add new temperatures...
  //
  Index = GUI_COUNTOF(_aTemp1) - 1;
  while (1) {
    WM_HWIN hItem;
    GUI_Delay(100); // Wait a while
    //
    // Shift the color arrays
    //
    memmove(_aTemp1, _aTemp1 + 1, sizeof(_aTemp1) - 2);
    memmove(_aTemp2, _aTemp2 + 1, sizeof(_aTemp2) - 2);
    //
    // Add new values
    //
    _aTemp1[Index] = _GetRandomValue(_aTemp1[Index - 1]);
    _aTemp2[Index] = _GetRandomValue(_aTemp2[Index - 1]);
    //
    // Update windows
    //
    hItem = WM_GetClientWindow(WM_GetDialogItem(_hDialogMain, ID_TEMPERATURE));
    WM_InvalidateWindow(hItem);
    _SetProgbarValue(GUI_ID_PROGBAR0, _aTemp1[Index]);
    _SetProgbarValue(GUI_ID_PROGBAR1, _aTemp2[Index]);
  }
}