The official µGFX library repository.

main.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2012, 2013, Joel Bodenmann aka Tectu <joel@unormal.org>
  3. * Copyright (c) 2012, 2013, Andrew Hannam aka inmarket
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of the <organization> nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "gfx.h"
  30. static GListener gl;
  31. static GHandle ghSlider1, ghSlider2, ghConsole;
  32. static void createWidgets(void) {
  33. GWidgetInit wi;
  34. // Apply some default values for GWIN
  35. gwinWidgetClearInit(&wi);
  36. wi.g.show = TRUE;
  37. // create Slider1
  38. wi.g.y = 10; wi.g.x = 10; wi.g.width = gdispGetWidth()-20; wi.g.height = 20; wi.text = "S1";
  39. ghSlider1 = gwinSliderCreate(0, &wi);
  40. // create Slider2
  41. wi.g.y = 40; wi.g.x = 10; wi.g.width = 20; wi.g.height = gdispGetHeight() - 50; wi.text = "S2";
  42. ghSlider2 = gwinSliderCreate(0, &wi);
  43. // Set slider 2 to return extended events
  44. gwinSliderSendExtendedEvents(ghSlider2, TRUE);
  45. // Console to display slider events
  46. wi.g.y = 40; wi.g.x = 40; wi.g.width = gdispGetWidth()-50; wi.g.height = gdispGetHeight()-50;
  47. ghConsole = gwinConsoleCreate(0, &wi.g);
  48. }
  49. int main(void) {
  50. GEventGWinSlider * pe;
  51. const char * sAction;
  52. // Initialize the display
  53. gfxInit();
  54. // Set the widget defaults
  55. gwinSetDefaultFont(gdispOpenFont("UI2"));
  56. gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE);
  57. gdispClear(White);
  58. // create the widget
  59. createWidgets();
  60. gwinSetColor(ghConsole, Green);
  61. gwinSetBgColor(ghConsole, White);
  62. gwinClear(ghConsole);
  63. // We want to listen for widget events
  64. geventListenerInit(&gl);
  65. gwinAttachListener(&gl);
  66. while(1) {
  67. // Get an Event (assume it is a slider event)
  68. pe = (GEventGWinSlider *)geventEventWait(&gl, TIME_INFINITE);
  69. switch(pe->type) {
  70. case GEVENT_GWIN_SLIDER:
  71. switch(pe->action) {
  72. case GSLIDER_EVENT_SET: sAction = "SET"; break;
  73. case GSLIDER_EVENT_CANCEL: sAction = "CANCEL"; break;
  74. case GSLIDER_EVENT_MOVE: sAction = "MOVE"; break;
  75. case GSLIDER_EVENT_START: sAction = "START"; break;
  76. default: sAction = "????"; break;
  77. }
  78. gwinPrintf(ghConsole, "Slider %s = %d%% %s\n", gwinGetText(pe->gwin), pe->position, sAction);
  79. break;
  80. default:
  81. // Oops - not a slider event.
  82. break;
  83. }
  84. }
  85. return 0;
  86. }