Subversion Repositories Kolibri OS

Rev

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