Subversion Repositories Kolibri OS

Rev

Rev 6612 | Rev 6690 | 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
  117.     treelist *tl2 = kolibri_new_treelist(X_Y(220, 200), X_Y(200, 200), 16, X_Y(16, 16), 100, 50, 0, 0, TL_LISTBOX_MODE, &ed_lock, 0x8080ff, 0x0000ff, 0xffffff);
  118.     (*tl_data_init)(tl2);
  119.  
  120.     tl2->data_img_sys = tl->data_img_sys;
  121.     tl2->data_img = tl->data_img;
  122.  
  123.     treelist_node_add(tl2, "list1", 0, 0, 0); // ãäå 1 íîìåð èêîíêè ñ êíèãîé
  124.     (*tl_cur_next)(tl2);
  125.  
  126.     treelist_node_add(tl2, "list2", 0, 0, 0);
  127.     (*tl_cur_next)(tl2);
  128.  
  129.     treelist_node_add(tl2, "list3", 0, 0, 0);
  130.     (*tl_cur_next)(tl2);
  131.  
  132.     (*tl_cur_beg)(tl2); //;ñòàâèì êóðñîð íà íà÷àëî ñïèñêà
  133.     gui_add_treelist(main_window, tl2);
  134.  
  135.     msgbox* box = kolibri_new_msgbox("Exit", "Are\rYOU\rSure?", 3, "YES", "Absolute", "Not Yet", NULL);  // default NOT
  136.  
  137.     oskey_t key;
  138.     do  /* Start of main activity loop */
  139.     {
  140.         switch(gui_event)
  141.         {
  142.         case KOLIBRI_EVENT_REDRAW:
  143.             if (box->retval == 1 || box->retval == 2) goto clearing;  // msgbox closes
  144.  
  145.             kolibri_handle_event_redraw(main_window);
  146.             break;
  147.         case KOLIBRI_EVENT_NONE:
  148.                         break;
  149.         case KOLIBRI_EVENT_KEY:
  150.             key = get_key();
  151.             if (ed_lock == ed)
  152.                 editor_key(ed, key);
  153.             else if(ed_lock == tl)
  154.             {
  155.                 treelist_key(tl, key);
  156.             }
  157.             else if(ed_lock == tl2)
  158.             {
  159.                 treelist_key(tl2, key);
  160.             }
  161.             else
  162.                 kolibri_handle_event_key(main_window, key);
  163.                         break;
  164.         case KOLIBRI_EVENT_BUTTON:
  165.             pressed_button = get_os_button();
  166.             switch (pressed_button)
  167.             {
  168.               case BTN_QUIT:
  169.                   if (box->retval == 3 || box->retval == 0) // not started or cancelled, analyze when redraw after closing msgbox
  170.                     kolibri_start_msgbox(box, NULL);
  171.                 break;
  172.             }
  173.             break;
  174.         case KOLIBRI_EVENT_MOUSE:
  175.             kolibri_handle_event_mouse(main_window);
  176.             break;
  177.         }
  178.  
  179.         gui_event = get_os_event();
  180.     } while(1) ; /* End of main activity loop */
  181.  
  182. clearing:
  183. trap(0x55); // for stop in debug
  184.     //res = editor_savefile(ed, "/tmp0/1/editorlog.txt");
  185.     pc = editor_get_text(ed);
  186.     debug_board_printf("saved text \"%s\"\n", pc);
  187.     free(pc);
  188.  
  189.  
  190.  
  191.     editor_delete(ed);
  192.     treelist_data_clear(tl);
  193.  
  194.   return 0;
  195. }
  196.  
  197.  
  198. char*   load_file_inmem(char* fname, int32_t* read_sz)
  199. {
  200.     FILE *f = fopen(fname, "rb");
  201.     if (!f) {
  202.         debug_board_printf("Can't open file: %s", fname);
  203.         exit(1);
  204.     }
  205.     if (fseek(f, 0, SEEK_END)) {
  206.         debug_board_printf("Can't SEEK_END file: %s", fname);
  207.         exit(1);
  208.     }
  209.     int filesize = ftell(f);
  210.     rewind(f);
  211.     char* fdata = malloc(filesize);
  212.     if(!fdata) {
  213.         debug_board_printf("No memory for file %s", fname);
  214.         exit(1);
  215.     }
  216.     *read_sz = fread(fdata, 1, filesize, f);
  217.     if (ferror(f)) {
  218.         debug_board_printf("Error reading file %s", fname);
  219.         exit(1);
  220.     }
  221.     fclose(f);
  222.  
  223.     return fdata;
  224. }
  225.  
  226. char*   load_image_file(char* fname)
  227. {
  228.     int32_t     read_bytes = -1, w, h;
  229.     char    *image_data = 0, *image_data_rgb = 0, *filedata = 0;
  230.  
  231.     filedata = load_file_inmem(fname, &read_bytes);
  232.     // îïðåäåëÿåì âèä èçîáðàæåíèÿ è ïåðåâîäèì åãî âî âðåìåííûé áóôåð image_data
  233.     image_data = (*img_decode)(filedata, read_bytes, 0);
  234.     w = *(int*)(image_data +4);
  235.     h = *(int*)(image_data +8);
  236.     image_data_rgb = malloc(w * h * 3);
  237.     // ïðåîáðàçóåì èçîáðàæåíèå ê ôîðìàòó rgb
  238.     (*img_to_rgb2)(image_data, image_data_rgb);
  239.     // óäàëÿåì âðåìåííûé áóôåð image_data
  240.     (*img_destroy)(image_data);
  241.     free(filedata);
  242.  
  243.     return image_data_rgb;
  244. }
  245.  
  246.