Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. /*
  3.     KolibriGUI demobox
  4.     -Editor (multiline edit)
  5.     -TreeView
  6.     -MsgBox Dialog
  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 <assert.h>
  20. #include "kos32sys.h"
  21. #include "kolibri_gui.h"
  22. #include "kolibri_libimg.h"
  23. #include "kolibri_msgbox.h"
  24.  
  25. /*extern inline scrollbar* kolibri_scrollbar(scrollbar* sb, uint32_t x_w, uint32_t y_h, uint32_t btn_height, uint32_t max_area,
  26.         uint32_t cur_area, uint32_t position, uint32_t back_color, uint32_t front_color, uint32_t line_color);
  27.  
  28. extern inline scrollbar* kolibri_new_scrollbar(uint32_t x_w, uint32_t y_h, uint32_t btn_height, uint32_t max_area,
  29.         uint32_t cur_area, uint32_t position, uint32_t back_color, uint32_t front_color, uint32_t line_color);
  30.  
  31. extern inline scrollbar* kolibri_new_scrollbar_def(uint32_t x_w, uint32_t y_h, uint32_t max_area, uint32_t cur_area, uint32_t position);
  32. */
  33. char run_path[4096];
  34. char fname[4096];
  35.  
  36. char*   load_file_inmem(char* fname, int32_t* read_sz); // see below
  37. char*   load_image_file(char* fname); // see below
  38.  
  39. void set_os_keyb_mode(int mode)
  40. // 0 - ASCII, 1 - SCAN
  41. {
  42.     __asm__ __volatile__(
  43.     "int $0x40"
  44.     ::"a"(66), "b"(1), "c"(mode));
  45. };
  46.  
  47.  
  48. int main(int argc, char **argv)
  49. {
  50.     /* Load all libraries, initialize global tables like system color table and
  51.     operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
  52.     to do it's job. This is all you need to call and all libraries and GUI
  53.     elements can be used after a successful call to this function
  54.     */
  55.     kolibri_gui_init();
  56.     set_os_keyb_mode(1); // scan code mode needed for editor
  57. //    kolibri_proclib_init();  // opensave && color dialogs
  58.     kolibri_libimg_init();  // png handling
  59.  
  60.     int gui_event = KOLIBRI_EVENT_REDRAW;
  61.     uint32_t pressed_button = 0;
  62. //    uint32_t mouse_button;
  63. //    pos_t   mouse_pos;
  64. //    oskey_t keypress;
  65.  
  66.     // make full path + argv
  67.     strcpy(run_path, argv[0]);
  68.     char *pc = strrchr(run_path, '/');  // this fails if has params with '/' within. use argv[0] instead
  69.     if (pc) pc[1] = 0;
  70. //    debug_board_write_str(temp_path);
  71.  
  72.     // creating GUI using library functions
  73.     kolibri_window *main_window = kolibri_new_window(50, 40, 490, 500, "Editor, TreeView and MsgBox demo");
  74.  
  75.     editor *ed;
  76.     void *ed_lock;
  77.  
  78.     gui_add_editor(main_window, ed = kolibri_new_editor(X_Y(0, 440), X_Y(20, 150), 0x11, 2048, &ed_lock));  // 0x11 font 8x16 sized x2
  79.     ed_lock = ed;
  80.  
  81.     // load sample file
  82.     //int res, len;
  83.     //res = editor_openfile(ed, "/sys/boardlog.txt", &len);
  84.     //debug_board_printf("loaded sample file err=%d, len=%d\n", res, len);
  85.  
  86.     //adding sample text @cursor
  87.     char *sampletext = "*123*=========ADDED SAMPLE TEXT=========*789*\n";
  88.     (*ted_text_add)(ed, sampletext, strlen(sampletext), 0);
  89.  
  90.     // treelist as tree
  91.     treelist *tl = kolibri_new_treelist(X_Y(0, 200), X_Y(200, 200), 16, X_Y(16, 16), 100, 50, 0, 0, /*TL_KEY_NO_EDIT |*/ TL_DRAW_PAR_LINE, &ed_lock, 0x8080ff, 0x0000ff, 0xffffff);
  92.     (*tl_data_init)(tl);
  93.  
  94.     // ������ ���� � ��������� � �������
  95.     strcpy(fname, run_path);
  96.     strcat(fname, "tl_sys_16.png");
  97.     tl->data_img_sys = load_image_file(fname);
  98.  
  99.     // ������ ���� � �������� �����
  100.     strcpy(fname, run_path);
  101.     strcat(fname, "tl_nod_16.png");
  102.     tl->data_img = load_image_file(fname);
  103.  
  104.     treelist_node_add(tl, "node1", 1, 0, 0); // ��� 1 ����� ������ � ������
  105.     (*tl_cur_next)(tl);
  106.     treelist_node_add(tl, "node1.1", 1, 0, 1);
  107.     (*tl_cur_next)(tl);
  108.     treelist_node_add(tl, "node1.1.1", 0, 0, 2);
  109.     (*tl_cur_next)(tl);
  110.     treelist_node_add(tl, "node1.2", 1, 0, 1);
  111.     (*tl_cur_next)(tl);
  112.  
  113.     treelist_node_add(tl, "node2", 1, 1, 0);  // closed node
  114.     (*tl_cur_next)(tl);
  115.     treelist_node_add(tl, "node2.1", 1, 0, 1);
  116.     (*tl_cur_next)(tl);
  117.  
  118.     treelist_node_add(tl, "node3", 1, 0, 0);
  119.     (*tl_cur_next)(tl);
  120.  
  121.     (*tl_cur_beg)(tl); //;������ ������ �� ������ ������
  122.     gui_add_treelist(main_window, tl);
  123.  
  124.     // treelist as listbox, no caption, no icons
  125.     treelist *tl2 = kolibri_new_treelist(X_Y(220, 200), X_Y(200, 200), 0, X_Y(16, 16), 100, 50, 0, 0, TL_LISTBOX_MODE, &ed_lock, 0x8080ff, 0x0000ff, 0xffffff);
  126.     (*tl_data_init)(tl2);
  127.     // tl->col_zag |= 0x10000000; // 0x10 in txt color must give font 9x16, but this not work, only 6x8 font (
  128.  
  129.     tl2->data_img_sys = tl->data_img_sys;
  130.     //tl2->data_img = tl->data_img; - no icons will be drawed
  131.  
  132.     treelist_node_add(tl2, "list1", 0, 0, 0); // ��� 1 ����� ������ � ������
  133.     (*tl_cur_next)(tl2);
  134.  
  135.     treelist_node_add(tl2, "list2", 0, 0, 0);
  136.     (*tl_cur_next)(tl2);
  137.  
  138.     treelist_node_add(tl2, "list3", 0, 0, 0);
  139.     (*tl_cur_next)(tl2);
  140.  
  141.     (*tl_cur_beg)(tl2); //;������ ������ �� ������ ������
  142.     gui_add_treelist(main_window, tl2);
  143.  
  144.     msgbox* box = kolibri_new_msgbox("Exit", "Are\rYOU\rSure?", 3, "YES", "Absolute", "Not Yet", NULL);  // default NOT
  145.  
  146.     oskey_t key;
  147.     do  /* Start of main activity loop */
  148.     {
  149.         switch(gui_event)
  150.         {
  151.         case KOLIBRI_EVENT_REDRAW:
  152.             if (box->retval == 1 || box->retval == 2) goto clearing;  // msgbox closes
  153.  
  154.             kolibri_handle_event_redraw(main_window);
  155.             break;
  156.         case KOLIBRI_EVENT_NONE:
  157.                         break;
  158.         case KOLIBRI_EVENT_KEY:
  159.             key = get_key();
  160.             if (ed_lock == ed)
  161.                 editor_key(ed, key);
  162.             else if(ed_lock == tl)
  163.             {
  164.                 treelist_key(tl, key);
  165.             }
  166.             else if(ed_lock == tl2)
  167.             {
  168.                 treelist_key(tl2, key);
  169.             }
  170.             else
  171.                 kolibri_handle_event_key(main_window, key);
  172.                         break;
  173.         case KOLIBRI_EVENT_BUTTON:
  174.             pressed_button = get_os_button();
  175.             switch (pressed_button)
  176.             {
  177.               case BTN_QUIT:
  178.                   if (box->retval == 3 || box->retval == 0) // not started or cancelled, analyze when redraw after closing msgbox
  179.                     kolibri_start_msgbox(box, NULL);
  180.                 break;
  181.             }
  182.             break;
  183.         case KOLIBRI_EVENT_MOUSE:
  184.             kolibri_handle_event_mouse(main_window);
  185.             break;
  186.         }
  187.  
  188.         gui_event = get_os_event();
  189.     } while(1) ; /* End of main activity loop */
  190.  
  191. clearing:
  192. trap(0x55); // for stop in debug
  193.     //res = editor_savefile(ed, "/tmp0/1/editorlog.txt");
  194.     pc = editor_get_text(ed);
  195.     debug_board_printf("saved text \"%s\"\n", pc);
  196.     free(pc);
  197.  
  198.  
  199.  
  200.     editor_delete(ed);
  201.     treelist_data_clear(tl);
  202.  
  203.   return 0;
  204. }
  205.  
  206.  
  207. char*   load_file_inmem(char* fname, int32_t* read_sz)
  208. {
  209.     FILE *f = fopen(fname, "rb");
  210.     if (!f) {
  211.         debug_board_printf("Can't open file: %s", fname);
  212.         exit(1);
  213.     }
  214.     if (fseek(f, 0, SEEK_END)) {
  215.         debug_board_printf("Can't SEEK_END file: %s", fname);
  216.         exit(1);
  217.     }
  218.     int filesize = ftell(f);
  219.     rewind(f);
  220.     char* fdata = malloc(filesize);
  221.     if(!fdata) {
  222.         debug_board_printf("No memory for file %s", fname);
  223.         exit(1);
  224.     }
  225.     *read_sz = fread(fdata, 1, filesize, f);
  226.     if (ferror(f)) {
  227.         debug_board_printf("Error reading file %s", fname);
  228.         exit(1);
  229.     }
  230.     fclose(f);
  231.  
  232.     return fdata;
  233. }
  234.  
  235. char*   load_image_file(char* fname)
  236. {
  237.     int32_t     read_bytes = -1, w, h;
  238.     char    *image_data = 0, *image_data_rgb = 0, *filedata = 0;
  239.  
  240.     filedata = load_file_inmem(fname, &read_bytes);
  241.     // ���������� ��� ����������� � ��������� ��� �� ��������� ����� image_data
  242.     image_data = (*img_decode)(filedata, read_bytes, 0);
  243.     w = *(int*)(image_data +4);
  244.     h = *(int*)(image_data +8);
  245.     image_data_rgb = malloc(w * h * 3);
  246.     // ����������� ����������� � ������� rgb
  247.     (*img_to_rgb2)(image_data, image_data_rgb);
  248.     // ������� ��������� ����� image_data
  249.     (*img_destroy)(image_data);
  250.     free(filedata);
  251.  
  252.     return image_data_rgb;
  253. }
  254.  
  255.