Subversion Repositories Kolibri OS

Rev

Rev 6589 | Rev 6612 | 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. int main(int argc, char **argv)
  32. {
  33.     /* Load all libraries, initialize global tables like system color table and
  34.     operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
  35.     to do it's job. This is all you need to call and all libraries and GUI
  36.     elements can be used after a successful call to this function
  37.     */
  38.     kolibri_gui_init();
  39. //    kolibri_proclib_init();  // opensave && color dialogs
  40.     kolibri_libimg_init();  // png handling
  41.  
  42.     int gui_event = KOLIBRI_EVENT_REDRAW;
  43.     uint32_t pressed_button = 0;
  44. //    uint32_t mouse_button;
  45. //    pos_t   mouse_pos;
  46.     oskey_t keypress;
  47.  
  48.     // make full path + argv
  49.     strcpy(run_path, argv[0]);
  50.     char *pc = strrchr(run_path, '/');  // this fails if has params with '/' within. use argv[0] instead
  51.     if (pc) pc[1] = 0;
  52. //    debug_board_write_str(temp_path);
  53.  
  54.     // creating GUI using library functions
  55.     kolibri_window *main_window = kolibri_new_window(50, 40, 490, 500, "Editor, TreeView and MsgBox demo");
  56.  
  57.     editor *ed;
  58.     editor *ed_lock;
  59.  
  60.     gui_add_editor(main_window, ed = kolibri_new_editor(X_Y(0, 440), X_Y(20, 150), 0x1001, 2048, &ed_lock));
  61.     ed_lock = ed;
  62.  
  63.     treelist *tl = kolibri_new_treelist(X_Y(0, 440), 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);
  64.     treelist_data_init(tl);
  65.  
  66.     // ÷èòàåì ôàéë ñ êóðñîðàìè è ëèíèÿìè
  67.     strcpy(fname, run_path);
  68.     strcat(fname, "tl_sys_16.png");
  69.     tl->data_img_sys = load_image_file(fname);
  70.  
  71.     // ÷èòàåì ôàéë ñ èêîíêàìè óçëîâ
  72.     strcpy(fname, run_path);
  73.     strcat(fname, "tl_nod_16.png");
  74.     tl->data_img = load_image_file(fname);
  75.  
  76.     treelist_node_add(tl, "node1", 1, 0, 0); // ãäå 1 íîìåð èêîíêè ñ êíèãîé
  77.     treelist_cursor_next(tl);
  78.  
  79.     treelist_node_add(tl, "node2", 1, 0, 0);
  80.     treelist_cursor_next(tl);
  81.  
  82.     treelist_node_add(tl, "node3", 1, 0, 0);
  83.     treelist_cursor_next(tl);
  84.  
  85.     treelist_cursor_begin(tl); //;ñòàâèì êóðñîð íà íà÷àëî ñïèñêà
  86.     gui_add_treelist(main_window, tl);
  87.  
  88.     msgbox* box = kolibri_new_msgbox("Exit", "Are\rYOU\rSure?", 3, "YES", "Absolute", "Not Yet", NULL);  // default NOT
  89.  
  90.     do  /* Start of main activity loop */
  91.     {
  92.         switch(gui_event)
  93.         {
  94.         case KOLIBRI_EVENT_REDRAW:
  95.             if (box->retval == 1 || box->retval == 2) goto clearing;  // msgbox closes
  96.  
  97.             kolibri_handle_event_redraw(main_window);
  98.             break;
  99.         case KOLIBRI_EVENT_NONE:
  100.                         break;
  101.         case KOLIBRI_EVENT_KEY:
  102.             if (ed_lock == ed)
  103.                 editor_key(ed);
  104.             else
  105.                 kolibri_handle_event_key(main_window);
  106.                         break;
  107.         case KOLIBRI_EVENT_BUTTON:
  108.             pressed_button = get_os_button();
  109.             switch (pressed_button)
  110.             {
  111.               case BTN_QUIT:
  112.                   if (box->retval == 3 || box->retval == 0) // not started or cancelled, analyze when redraw after closing msgbox
  113.                     kolibri_start_msgbox(box, NULL);
  114.                 break;
  115.             }
  116.             break;
  117.         case KOLIBRI_EVENT_MOUSE:
  118.             kolibri_handle_event_mouse(main_window);
  119.             break;
  120.         }
  121.  
  122.         gui_event = get_os_event();
  123.     } while(1) ; /* End of main activity loop */
  124.  
  125. clearing:
  126.     editor_delete(ed);
  127.     treelist_data_clear(tl);
  128.  
  129.   return 0;
  130. }
  131.  
  132.  
  133. char*   load_file_inmem(char* fname, int32_t* read_sz)
  134. {
  135.     FILE *f = fopen(fname, "rb");
  136.     if (!f) {
  137.         debug_board_printf("Can't open file: %s", fname);
  138.         exit(1);
  139.     }
  140.     if (fseek(f, 0, SEEK_END)) {
  141.         debug_board_printf("Can't SEEK_END file: %s", fname);
  142.         exit(1);
  143.     }
  144.     int filesize = ftell(f);
  145.     rewind(f);
  146.     char* fdata = malloc(filesize);
  147.     if(!fdata) {
  148.         debug_board_printf("No memory for file %s", fname);
  149.         exit(1);
  150.     }
  151.     *read_sz = fread(fdata, 1, filesize, f);
  152.     if (ferror(f)) {
  153.         debug_board_printf("Error reading file %s", fname);
  154.         exit(1);
  155.     }
  156.     fclose(f);
  157.  
  158.     return fdata;
  159. }
  160.  
  161. char*   load_image_file(char* fname)
  162. {
  163.     int32_t     read_bytes = -1, w, h;
  164.     char    *image_data = 0, *image_data_rgb = 0, *filedata = 0;
  165.  
  166.     filedata = load_file_inmem(fname, &read_bytes);
  167.     // îïðåäåëÿåì âèä èçîáðàæåíèÿ è ïåðåâîäèì åãî âî âðåìåííûé áóôåð image_data
  168.     image_data = (*img_decode)(filedata, read_bytes, 0);
  169.     w = *(int*)(image_data +4);
  170.     h = *(int*)(image_data +8);
  171.     image_data_rgb = malloc(w * h * 3);
  172.     // ïðåîáðàçóåì èçîáðàæåíèå ê ôîðìàòó rgb
  173.     (*img_to_rgb2)(image_data, image_data_rgb);
  174.     // óäàëÿåì âðåìåííûé áóôåð image_data
  175.     (*img_destroy)(image_data);
  176.     free(filedata);
  177.  
  178.     return image_data_rgb;
  179. }
  180.  
  181.