Subversion Repositories Kolibri OS

Rev

Rev 6526 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     KolibriGUI demobox
  3.     -Scrollbar
  4.     -Progressbar
  5.     -StaticText
  6.     -StaticNum
  7.  
  8.     Free for all
  9.  
  10.     Initially written by Siemargl, 2016
  11.  
  12.  
  13.     ToDo
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "kos32sys.h"
  20. #include "kolibri_gui.h"
  21.  
  22. // codes copied from \programs\cmm\lib\keyboard.h, but they're decimal
  23. //ASCII KEYS
  24. #define ASCII_KEY_BS    8
  25. #define ASCII_KEY_TAB   9
  26. #define ASCII_KEY_ENTER 13
  27. #define ASCII_KEY_ESC   27
  28. #define ASCII_KEY_DEL   182
  29. #define ASCII_KEY_INS   185
  30. #define ASCII_KEY_SPACE 32
  31.  
  32. #define ASCII_KEY_LEFT  176
  33. #define ASCII_KEY_RIGHT 179
  34. #define ASCII_KEY_DOWN  177
  35. #define ASCII_KEY_UP    178
  36. #define ASCII_KEY_HOME  180
  37. #define ASCII_KEY_END   181
  38. #define ASCII_KEY_PGDN  183
  39. #define ASCII_KEY_PGUP  184
  40.  
  41. //SCAN CODE KEYS
  42. #define SCAN_CODE_BS    14
  43. #define SCAN_CODE_TAB   15
  44. #define SCAN_CODE_ENTER 28
  45. #define SCAN_CODE_ESC   1
  46. #define SCAN_CODE_DEL   83
  47. #define SCAN_CODE_INS   82
  48. #define SCAN_CODE_SPACE 57
  49.  
  50. #define SCAN_CODE_LEFT  75
  51. #define SCAN_CODE_RIGHT 77
  52. #define SCAN_CODE_DOWN  80
  53. #define SCAN_CODE_UP    72
  54. #define SCAN_CODE_HOME  71
  55. #define SCAN_CODE_END   79
  56. #define SCAN_CODE_PGDN  81
  57. #define SCAN_CODE_PGUP  73
  58.  
  59. #define KEY_LSHIFT     00000000001b
  60. #define KEY_RSHIFT     00000000010b
  61. #define KEY_LCTRL      00000000100b
  62. #define KEY_RCTRL      00000001000b
  63. #define KEY_LALT       00000010000b
  64. #define KEY_RALT       00000100000b
  65. #define KEY_CAPSLOCK   00001000000b
  66. #define KEY_NUMLOCK    00010000000b
  67. #define KEY_SCROLLLOCK 00100000000b
  68. #define KEY_LWIN       01000000000b
  69. #define KEY_RWIN       10000000000b
  70.  
  71.  
  72. int main()
  73. {
  74.     /* Load all libraries, initialize global tables like system color table and
  75.     operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
  76.     to do it's job. This is all you need to call and all libraries and GUI
  77.     elements can be used after a successful call to this function
  78.     */
  79.     kolibri_gui_init();
  80.     int gui_event = KOLIBRI_EVENT_REDRAW;
  81.     uint32_t pressed_button = 0;
  82. //    uint32_t mouse_button;
  83. //    pos_t   mouse_pos;
  84.     oskey_t keypress;
  85.  
  86.     int value = 40; // showed value
  87.     int valuechange = 0;
  88.  
  89.     // creating GUI using library functions
  90.     kolibri_window *main_window = kolibri_new_window(50, 40, 400, 400, "Scrollbar and progressbar showcase");
  91.     statictext *txt = kolibri_new_statictext_def(X_Y(10, 20), "StaticText default 6x9. Use Arrows or PgUp/PgDn");
  92.     statictext *txt2 = kolibri_new_statictext(X_Y(10, 30), "StaticText 8x16 x2:", CP866, 1, kolibri_color_table.color_work_text, 0);
  93.     staticnum *num = kolibri_new_staticnum_def(X_Y(10 + (strlen("StaticText 8x16 x2:") + 2) * 8 * 2, 30), 3, value);
  94.     scrollbar *sbh = kolibri_new_scrollbar(X_Y(30, 300), X_Y(350, 15), 15, 100, 10, value, kolibri_color_table.color_work_area, kolibri_color_table.color_work_button, 0);
  95.     scrollbar *sbv = kolibri_new_scrollbar(X_Y(370, 15), X_Y(40, 300), 15, 100, 10, value, kolibri_color_table.color_work_area, kolibri_color_table.color_work_button, 0);
  96.     progressbar *pg = kolibri_new_progressbar(0, 100, value, 10, 70, 200, 20);
  97.  
  98.     kolibri_window_add_element(main_window, KOLIBRI_STATICTEXT, txt);
  99.     kolibri_window_add_element(main_window, KOLIBRI_STATICTEXT, txt2);
  100.     kolibri_window_add_element(main_window, KOLIBRI_STATICNUM, num);
  101.     kolibri_window_add_element(main_window, KOLIBRI_SCROLL_BAR_H, sbh);
  102.     kolibri_window_add_element(main_window, KOLIBRI_SCROLL_BAR_V, sbv);
  103.     kolibri_window_add_element(main_window, KOLIBRI_PROGRESS_BAR, pg);
  104.  
  105.     do  /* Start of main activity loop */
  106.     {
  107.         switch(gui_event)
  108.         {
  109.         case KOLIBRI_EVENT_REDRAW:
  110.             sbh->all_redraw = sbv->all_redraw = 1; // resetted by sbar
  111.             kolibri_handle_event_redraw(main_window);
  112.             valuechange = 0;
  113.             break;
  114.         case KOLIBRI_EVENT_NONE:
  115.                         break;
  116.         case KOLIBRI_EVENT_KEY:
  117.             keypress = get_key();
  118.             // add logic to find focused active widget
  119.             // we have only one reaction
  120.             switch (keypress.ctrl_key)
  121.             {
  122.               case SCAN_CODE_UP:  case SCAN_CODE_RIGHT:
  123.                   if(value < 100)
  124.                     {
  125.                         value++; valuechange = 1;
  126.                         progressbar_progress(pg); // +1 and redraw self
  127.                     }
  128.                 break;
  129.               case SCAN_CODE_PGUP:
  130.                   if(value < 100)
  131.                     {
  132.                         value += 10; valuechange = 1;
  133.                         if (value > 100) value = 100;
  134.                     }
  135.                 break;
  136.               case SCAN_CODE_DOWN:  case SCAN_CODE_LEFT:
  137.                   if(value > 0)
  138.                     {
  139.                         value--; valuechange = 1;
  140.                     }
  141.                 break;
  142.               case SCAN_CODE_PGDN:
  143.                   if(value > 0)
  144.                     {
  145.                         value -= 10; valuechange = 1;
  146.                         if (value < 0) value = 0;
  147.                     }
  148.                 break;
  149.             }
  150.             kolibri_handle_event_key(main_window, keypress);
  151.                         break;
  152.         case KOLIBRI_EVENT_BUTTON:
  153.             pressed_button = get_os_button();
  154.             switch (pressed_button)
  155.             {
  156.               case BTN_QUIT:
  157.                 return 0;
  158.                 break;
  159.             }
  160.             break;
  161.         case KOLIBRI_EVENT_MOUSE:
  162. //            mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
  163. //            mouse_button = get_mouse_eventstate();
  164.             // add logic to find widget under mouse
  165.             kolibri_handle_event_mouse(main_window);
  166.             // we can optimize a lot, if sb->delta2 == 1 then call sb->mouse() and redraw only if sb->redraw with flag all_redraw = 0 (only runner). Then reset redraw. OMG (
  167.             if (sbh->position != value)  // scrollbars was changed
  168.             {
  169.                 value = sbh->position;
  170.                 valuechange = 1;
  171.             }else
  172.             if (sbv->position != value)  // scrollbars was changed
  173.             {
  174.                 value = sbv->position;
  175.                 valuechange = 1;
  176.             }
  177. /*            debug_board_printf("mouse ev (%d,%d)%x\n", mouse_pos.x, mouse_pos.y, mouse_button);
  178.             if (mouse_button & (1<<24)) // double click
  179.             {
  180.             }
  181.             // ignore
  182. */
  183.             break;
  184.         }
  185.         if(valuechange)
  186.         {
  187.             debug_board_printf("value change (%d)\n", value);
  188.             num->number = value;
  189.             sbh->position = value;
  190.             sbv->position = value;
  191.             pg->value = value;
  192.             gui_event = KOLIBRI_EVENT_REDRAW;
  193.             continue;
  194.         }
  195.  
  196.         gui_event = wait_for_event(10); // 100 = 1 sec
  197.     } while(1) ; /* End of main activity loop */
  198.  
  199.   return 0;
  200. }
  201. /*
  202. void __attribute__ ((noinline)) debug_board_write_str(const char* str){
  203.   while(*str)
  204.     debug_board_write_byte(*str++);
  205. }
  206.  
  207. void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
  208. {
  209.         va_list ap;
  210.         char log_board[300];
  211.  
  212.         va_start (ap, format);
  213.         tiny_vsnprintf(log_board, sizeof log_board, format, ap);
  214.         va_end(ap);
  215.         debug_board_write_str(log_board);
  216.  
  217. }
  218. */
  219.