Subversion Repositories Kolibri OS

Rev

Rev 6601 | Rev 6615 | 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 = "==========ADDED SAMPLE TEXT==========\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.     treelist_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.     treelist_cursor_next(tl);
  98.     treelist_node_add(tl, "node1.1", 1, 0, 1);
  99.     treelist_cursor_next(tl);
  100.     treelist_node_add(tl, "node1.1.1", 0, 0, 2);
  101.     treelist_cursor_next(tl);
  102.     treelist_node_add(tl, "node1.2", 1, 0, 1);
  103.     treelist_cursor_next(tl);
  104.  
  105.     treelist_node_add(tl, "node2", 1, 1, 0);  // closed node
  106.     treelist_cursor_next(tl);
  107.     treelist_node_add(tl, "node2.1", 1, 0, 1);
  108.     treelist_cursor_next(tl);
  109.  
  110.     treelist_node_add(tl, "node3", 1, 0, 0);
  111.     treelist_cursor_next(tl);
  112.  
  113.     treelist_cursor_begin(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.     treelist_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.     treelist_cursor_next(tl2);
  125.  
  126.     treelist_node_add(tl2, "list2", 0, 0, 0);
  127.     treelist_cursor_next(tl2);
  128.  
  129.     treelist_node_add(tl2, "list3", 0, 0, 0);
  130.     treelist_cursor_next(tl2);
  131.  
  132.     treelist_cursor_begin(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. trap(0x55); // for stop in debug
  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.     editor_delete(ed);
  185.     treelist_data_clear(tl);
  186.  
  187.   return 0;
  188. }
  189.  
  190.  
  191. char*   load_file_inmem(char* fname, int32_t* read_sz)
  192. {
  193.     FILE *f = fopen(fname, "rb");
  194.     if (!f) {
  195.         debug_board_printf("Can't open file: %s", fname);
  196.         exit(1);
  197.     }
  198.     if (fseek(f, 0, SEEK_END)) {
  199.         debug_board_printf("Can't SEEK_END file: %s", fname);
  200.         exit(1);
  201.     }
  202.     int filesize = ftell(f);
  203.     rewind(f);
  204.     char* fdata = malloc(filesize);
  205.     if(!fdata) {
  206.         debug_board_printf("No memory for file %s", fname);
  207.         exit(1);
  208.     }
  209.     *read_sz = fread(fdata, 1, filesize, f);
  210.     if (ferror(f)) {
  211.         debug_board_printf("Error reading file %s", fname);
  212.         exit(1);
  213.     }
  214.     fclose(f);
  215.  
  216.     return fdata;
  217. }
  218.  
  219. char*   load_image_file(char* fname)
  220. {
  221.     int32_t     read_bytes = -1, w, h;
  222.     char    *image_data = 0, *image_data_rgb = 0, *filedata = 0;
  223.  
  224.     filedata = load_file_inmem(fname, &read_bytes);
  225.     // îïðåäåëÿåì âèä èçîáðàæåíèÿ è ïåðåâîäèì åãî âî âðåìåííûé áóôåð image_data
  226.     image_data = (*img_decode)(filedata, read_bytes, 0);
  227.     w = *(int*)(image_data +4);
  228.     h = *(int*)(image_data +8);
  229.     image_data_rgb = malloc(w * h * 3);
  230.     // ïðåîáðàçóåì èçîáðàæåíèå ê ôîðìàòó rgb
  231.     (*img_to_rgb2)(image_data, image_data_rgb);
  232.     // óäàëÿåì âðåìåííûé áóôåð image_data
  233.     (*img_destroy)(image_data);
  234.     free(filedata);
  235.  
  236.     return image_data_rgb;
  237. }
  238.  
  239.