Subversion Repositories Kolibri OS

Rev

Rev 6526 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     KolibriGUI demobox
  3.     -Picture Button
  4.     -StaticText
  5.     -File Open/Save Dialog
  6.     -Filebrowser (planned)
  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 "kos32sys.h"
  20. #include "kolibri_gui.h"
  21. #include "kolibri_opendialog.h"
  22. #include "kolibri_libimg.h"
  23.  
  24. char temp_path[4096];
  25.  
  26. int main()
  27. {
  28.     /* Load all libraries, initialize global tables like system color table and
  29.     operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
  30.     to do it's job. This is all you need to call and all libraries and GUI
  31.     elements can be used after a successful call to this function
  32.     */
  33.     kolibri_gui_init();
  34.     kolibri_proclib_init();  // opensave && color dialogs
  35.     kolibri_libimg_init();  // png handling
  36.  
  37.  
  38.     int gui_event = KOLIBRI_EVENT_REDRAW;
  39.     uint32_t pressed_button = 0;
  40. //    uint32_t mouse_button;
  41. //    pos_t   mouse_pos;
  42.     oskey_t keypress;
  43.  
  44.     // load image for buttons
  45.     const int icon_rgb_size = 16*16*3; // every icons 16x16 24bpp
  46.     char *image_data_rgb = malloc(icon_rgb_size * 3),
  47.          *image_data;
  48.     // make full path + argv
  49.     FILE *ficon = fopen("reload_16x16_8b.png", "rb");
  50.     if (!ficon)
  51.     {
  52.         debug_board_write_str("no icons file reload_16x16_8b.png ");
  53.         return 1;
  54.     }
  55.     int ficon_size = fread(image_data_rgb, 1, icon_rgb_size * 3, ficon);
  56.     if (ferror(ficon))
  57.     {
  58.         debug_board_write_str("error reading file reload_16x16_8b.png ");
  59.         return 1;
  60.     }
  61.     fclose(ficon);
  62.  
  63.     // îïðåäåëÿåì âèä èçîáðàæåíèÿ è ïåðåâîäèì åãî âî âðåìåííûé áóôåð image_data
  64.     image_data = (*img_decode)(image_data_rgb, ficon_size, 0);
  65.     // ïðåîáðàçóåì èçîáðàæåíèå ê ôîðìàòó rgb
  66.     (*img_to_rgb2)(image_data, image_data_rgb);
  67.     // óäàëÿåì âðåìåííûé áóôåð image_data
  68.     (*img_destroy)(image_data);
  69.  
  70.     // creating GUI using library functions
  71.     kolibri_window *main_window = kolibri_new_window(50, 40, 400, 160, "PictureButton and File dialog demo");
  72.  
  73.     pict_button tbar[3];
  74.     gui_add_pict_button(main_window, kolibri_pict_button(&tbar[0], X_Y(10, 16), X_Y(10, 16), image_data_rgb, image_data_rgb + icon_rgb_size, image_data_rgb + icon_rgb_size * 2, 24, NULL, 0));
  75.     gui_add_pict_button(main_window, kolibri_pict_button(&tbar[1], X_Y(20, 16), X_Y(10, 16), image_data_rgb, image_data_rgb + icon_rgb_size, image_data_rgb + icon_rgb_size * 2, 24, NULL, 0));
  76.     gui_add_pict_button(main_window, kolibri_pict_button(&tbar[2], X_Y(30, 16), X_Y(10, 16), image_data_rgb, image_data_rgb + icon_rgb_size, image_data_rgb + icon_rgb_size * 2, 24, NULL, 0));
  77.  
  78.     statictext labels[3];  //  tips
  79.     gui_add_statictext(main_window, kolibri_statictext_def(&labels[0], X_Y(5, 28), "Open"));
  80.     gui_add_statictext(main_window, kolibri_statictext_def(&labels[1], X_Y(20, 28), "Save"));
  81.     gui_add_statictext(main_window, kolibri_statictext_def(&labels[2], X_Y(30, 28), "Select Dir"));
  82.  
  83.     open_dialog *dlg_opensave = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320);
  84.     (*OpenDialog_init)(dlg_opensave);
  85.  
  86.     pathview pview;
  87.     gui_add_pathview(main_window, kolibri_pathview(&pview, X_Y(10, 40), 330, 0, 0, dlg_opensave->openfile_path, temp_path, 0, 0)); // black font, no background
  88.  
  89.     do  /* Start of main activity loop */
  90.     {
  91.         switch(gui_event)
  92.         {
  93.         case KOLIBRI_EVENT_REDRAW:
  94.             kolibri_handle_event_redraw(main_window);
  95.             break;
  96.         case KOLIBRI_EVENT_NONE:
  97.                         break;
  98.         case KOLIBRI_EVENT_KEY:
  99.             keypress = get_key();
  100.             kolibri_handle_event_key(main_window);
  101.                         break;
  102.         case KOLIBRI_EVENT_BUTTON:
  103.             pressed_button = get_os_button();
  104.             switch (pressed_button)
  105.             {
  106.               case BTN_QUIT:
  107.                 return 0;
  108.                 break;
  109.             }
  110.             break;
  111.         case KOLIBRI_EVENT_MOUSE:
  112. //            mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
  113. //            mouse_button = get_mouse_eventstate();
  114.             kolibri_handle_event_mouse(main_window);
  115.  
  116.             if(tbar[0].click)  // open
  117.             {
  118.                 tbar[0].click = 0;
  119.                 dlg_opensave->mode = OPEN;
  120.                 (*OpenDialog_start)(dlg_opensave);
  121.                 if (dlg_opensave->status != 2 && dlg_opensave->status != 1) // fail or cancel
  122.                     (*path_show_prepare)(&pview);
  123.                 kolibri_handle_event_redraw(main_window);
  124.             }
  125.             if(tbar[1].click)  // save
  126.             {
  127.                 tbar[1].click = 0;
  128.                 dlg_opensave->mode = SAVE;
  129.                 (*OpenDialog_start)(dlg_opensave);
  130.                 if (dlg_opensave->status != 2 && dlg_opensave->status != 1) // fail or cancel
  131.                     (*path_show_prepare)(&pview);
  132.                 kolibri_handle_event_redraw(main_window);
  133.             }
  134.             if(tbar[2].click)  // select
  135.             {
  136.                 tbar[2].click = 0;
  137.                 dlg_opensave->mode = SELECT;
  138.                 (*OpenDialog_start)(dlg_opensave);
  139.                 if (dlg_opensave->status != 2 && dlg_opensave->status != 1) // fail or cancel
  140.                     (*path_show_prepare)(&pview);
  141.                 kolibri_handle_event_redraw(main_window);
  142.             }
  143.  
  144.             break;
  145.         }
  146.  
  147.         gui_event = get_os_event();
  148.     } while(1) ; /* End of main activity loop */
  149.  
  150.   return 0;
  151. }
  152.  
  153.