Subversion Repositories Kolibri OS

Rev

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