Subversion Repositories Kolibri OS

Rev

Rev 6479 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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