Subversion Repositories Kolibri OS

Rev

Rev 6589 | 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
  7.     -Controlling minimal window size
  8.  
  9.     Free for all
  10.  
  11.     Initially written by Siemargl, 2016
  12.  
  13.  
  14.     ToDo
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include "kos32sys.h"
  22. #include "kolibri_gui.h"
  23. #include "kolibri_opendialog.h"
  24. #include "kolibri_libimg.h"
  25.  
  26. char temp_path[4096];
  27. char** sys_path = (char**)0x20; // hack - get path from KOS header. analog argv[0]
  28.  
  29. char*   load_file_inmem(char* fname, int32_t* read_sz); // see below
  30. void* read_folderdata(char* name);
  31. void control_minimal_window_size(int wmin, int hmin);
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.     /* Load all libraries, initialize global tables like system color table and
  36.     operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
  37.     to do it's job. This is all you need to call and all libraries and GUI
  38.     elements can be used after a successful call to this function
  39.     */
  40.     kolibri_gui_init();
  41.     kolibri_proclib_init();  // opensave && color dialogs
  42.     kolibri_libimg_init();  // png handling
  43.  
  44.     int gui_event = KOLIBRI_EVENT_REDRAW;
  45.     uint32_t pressed_button = 0;
  46. //    uint32_t mouse_button;
  47. //    pos_t   mouse_pos;
  48.     oskey_t keypress;
  49.  
  50.     // load image for buttons
  51.     const int icon_rgb_size = 16*16*3; // every icons 16x16 24bpp
  52.     char *image_data_rgb,
  53.          *image_data,
  54.          *filedata;
  55.     // make full path + argv
  56.     strcpy(temp_path, *sys_path);
  57.     char *pc = strrchr(temp_path, '/');  // this fails if has params with '/' within. use argv[0] instead
  58.     if (pc) pc[1] = 0;
  59.     strcat(temp_path, "reload_16x16_8b.png");
  60. //    debug_board_write_str(temp_path);
  61.  
  62.     int32_t read_bytes;
  63.     filedata = load_file_inmem(temp_path, &read_bytes);
  64.     image_data_rgb = malloc(icon_rgb_size * 3);  // we know size
  65.     // юяЁхфхы хь тшф шчюсЁрцхэш  ш яхЁхтюфшь хую тю тЁхьхээ√щ сєЇхЁ image_data
  66.     image_data = (*img_decode)(filedata, read_bytes, 0);
  67.     // яЁхюсЁрчєхь шчюсЁрцхэшх ъ ЇюЁьрЄє rgb
  68.     (*img_to_rgb2)(image_data, image_data_rgb);
  69.     // єфры хь тЁхьхээ√щ сєЇхЁ image_data
  70.     (*img_destroy)(image_data);
  71.     free(filedata);
  72.  
  73.     // creating GUI using library functions
  74.     kolibri_window *main_window = kolibri_new_window(50, 40, 430, 500, "PictureButton and File dialog demo");
  75.  
  76.     pict_button tbar[3];
  77.     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));
  78.     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));
  79.     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));
  80.  
  81.     statictext labels[3];  //  tips
  82.     gui_add_statictext(main_window, kolibri_statictext_def(&labels[0], X_Y(5, 28), "Open"));
  83.     gui_add_statictext(main_window, kolibri_statictext_def(&labels[1], X_Y(35, 28), "Save"));
  84.     gui_add_statictext(main_window, kolibri_statictext_def(&labels[2], X_Y(65, 28), "Select Dir & browse"));
  85.  
  86.     open_dialog *dlg_opensave = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320);
  87.     (*OpenDialog_init)(dlg_opensave);
  88.  
  89.     pathview pview;
  90.     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
  91.  
  92.     filebrowser brows;
  93.     filedata = load_file_inmem("/rd/1/File managers/z_icons.png", &read_bytes);
  94.     image_data_rgb = malloc(icon_rgb_size * 20);  // we know size
  95.     // юяЁхфхы хь тшф шчюсЁрцхэш  ш яхЁхтюфшь хую тю тЁхьхээ√щ сєЇхЁ image_data
  96.     image_data = (*img_decode)(filedata, read_bytes, 0);
  97.     // яЁхюсЁрчєхь шчюсЁрцхэшх ъ ЇюЁьрЄє rgb
  98.     (*img_to_rgb2)(image_data, image_data_rgb);
  99.     // єфры хь тЁхьхээ√щ сєЇхЁ image_data
  100.     (*img_destroy)(image_data);
  101.     free(filedata);
  102.  
  103.     filedata = load_file_inmem("/rd/1/File managers/icons.ini", &read_bytes);
  104.     gui_add_filebrowser(main_window, kolibri_filebrowser(&brows, X_Y(10, 400), X_Y(80, 300), X_Y(6, 9), X_Y(16, 16), image_data_rgb, NULL, 24,
  105.                                          filedata, filedata + read_bytes,
  106.                                          0x00FF00, 0xbbddff, 0x000000, 0xFFFFFF, 0xFF0000));
  107.  
  108.     // try devices "/" - good
  109.     brows.folder_data = read_folderdata("/rd/1");
  110.     brows.select_panel_counter = 1;  // if want to show selection
  111.  
  112.     do  /* Start of main activity loop */
  113.     {
  114.         switch(gui_event)
  115.         {
  116.         case KOLIBRI_EVENT_REDRAW:
  117.             control_minimal_window_size(430, 500);
  118.             brows.all_redraw = 1;
  119.             kolibri_handle_event_redraw(main_window);
  120.             brows.all_redraw = 0;
  121.             break;
  122.         case KOLIBRI_EVENT_NONE:
  123.                         break;
  124.         case KOLIBRI_EVENT_KEY:
  125.             keypress = get_key();
  126.             filebrowser_key(&brows, keypress);
  127.             //kolibri_handle_event_key(main_window);
  128.                         break;
  129.         case KOLIBRI_EVENT_BUTTON:
  130.             pressed_button = get_os_button();
  131.             switch (pressed_button)
  132.             {
  133.               case BTN_QUIT:
  134.                 return 0;
  135.                 break;
  136.             }
  137.             break;
  138.         case KOLIBRI_EVENT_MOUSE:
  139. //            mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
  140. //            mouse_button = get_mouse_eventstate();
  141.             brows.select_flag = 0;
  142.             kolibri_handle_event_mouse(main_window);
  143.  
  144.  
  145.             if (brows.mouse_keys_delta == 3)  // double clicked in browser
  146.             {
  147.                 debug_board_printf("mouse_keys_delta == 3, name %s\n", brows.selected_BDVK_adress->fname);
  148.                 brows.mouse_keys_delta = 0;
  149.             }
  150.  
  151.             if(tbar[0].click)  // open
  152.             {
  153.                 tbar[0].click = 0;
  154.                 dlg_opensave->mode = OPEN;
  155.                 (*OpenDialog_start)(dlg_opensave);
  156.                 //debug_board_printf("status == %d, buf = %s\n", dlg_opensave->status, dlg_opensave->openfile_path);
  157.                 if (dlg_opensave->status != 2 && dlg_opensave->status != 0) // fail or cancel
  158.                 {
  159.                     (*path_show_prepare)(&pview);
  160.                     (*path_show_draw)(&pview);
  161.                 }
  162.             }
  163.             if(tbar[1].click)  // save
  164.             {
  165.                 tbar[1].click = 0;
  166.                 dlg_opensave->mode = SAVE;
  167.                 (*OpenDialog_start)(dlg_opensave);
  168.                 if (dlg_opensave->status != 2 && dlg_opensave->status != 0) // fail or cancel
  169.                     (*path_show_prepare)(&pview);
  170.  
  171.                 // just calling line below draws incomplete
  172.                 // kolibri_handle_event_redraw(main_window);
  173.             }
  174.             if(tbar[2].click)  // select
  175.             {
  176.                 tbar[2].click = 0;
  177.                 dlg_opensave->mode = SELECT;
  178.                 (*OpenDialog_start)(dlg_opensave);
  179.                 if (dlg_opensave->status != 2 && dlg_opensave->status != 0) // fail or cancel
  180.                 {
  181.                     (*path_show_prepare)(&pview);
  182.                     free(brows.folder_data);
  183.                     brows.folder_data = read_folderdata(dlg_opensave->openfile_path);
  184.                     brows.start_draw_line = brows.start_draw_cursor_line = 0;
  185.                 }
  186.                 // we may redraw here, or just wait next redraw event
  187.                 brows.all_redraw = 1;
  188.                 kolibri_handle_event_redraw(main_window);
  189.                 brows.all_redraw = 0;
  190.             }
  191.  
  192.             break;
  193.         }
  194.  
  195.         gui_event = get_os_event();
  196.     } while(1) ; /* End of main activity loop */
  197.  
  198.   return 0;
  199. }
  200.  
  201.  
  202. char*   load_file_inmem(char* fname, int32_t* read_sz)
  203. {
  204.     FILE *f = fopen(fname, "rb");
  205.     if (!f) {
  206.         debug_board_printf("Can't open file: %s", fname);
  207.         exit(1);
  208.     }
  209.     if (fseek(f, 0, SEEK_END)) {
  210.         debug_board_printf("Can't SEEK_END file: %s", fname);
  211.         exit(1);
  212.     }
  213.     int filesize = ftell(f);
  214.     rewind(f);
  215.     char* fdata = malloc(filesize);
  216.     if(!fdata) {
  217.         debug_board_printf("No memory for file %s", fname);
  218.         exit(1);
  219.     }
  220.     *read_sz = fread(fdata, 1, filesize, f);
  221.     if (ferror(f)) {
  222.         debug_board_printf("Error reading file %s", fname);
  223.         exit(1);
  224.     }
  225.     fclose(f);
  226.  
  227.     return fdata;
  228. }
  229.  
  230. void* read_folderdata(char* name)
  231. {
  232.     struct fs_dirinfo di;
  233.     struct fs_dirheader dhead;
  234.     assert(sizeof di == 25);
  235.  
  236.     memset(&di, 0, sizeof di);
  237.     di.ppath = name;
  238.     di.retval = (uint32_t)&dhead;
  239.     int rc = sf_file(1, &di);  // read dir size
  240.     if(rc) {
  241.         debug_board_printf("Error reading dir size %s", name);
  242.         exit(1);
  243.     }
  244.     di.size = dhead.totl_blocks;
  245.  
  246.     char *retdir = malloc(sizeof dhead + dhead.totl_blocks * sizeof(struct fsBDFE));
  247.     if(!retdir) {
  248.         debug_board_printf("No memory for dir %s", name);
  249.         exit(1);
  250.     }
  251.     di.retval = (uint32_t)retdir;
  252.     rc = sf_file(1, &di);  // read dir size
  253.     if(rc) {
  254.         debug_board_printf("Error 2 reading dir size %s", name);
  255.         exit(1);
  256.     }
  257.  
  258.     // manual clear mark flag (random junk in fname free space)
  259.     int i;
  260.     for (i = 0; i < dhead.totl_blocks; i++)
  261.         ((struct fsBDFE*)(retdir+32))[i].fname[259] = 0;
  262.  
  263.     debug_board_printf("Loaded dir [%s] etnries %d,\n first file [%s]\n", name, ((struct fs_dirheader*)(retdir))->curn_blocks, ((struct fsBDFE*)(retdir+32))->fname);
  264.  
  265.     return retdir;
  266. }
  267.  
  268.  
  269.  
  270. void control_minimal_window_size(int wmin, int hmin)
  271. {
  272.     char pinfo[1024];
  273.     get_proc_info(pinfo);
  274.  
  275.     int win_hight = *(int*)(pinfo + 46),
  276.         win_width = *(int*)(pinfo + 42);
  277.     char win_status = pinfo[70];
  278.  
  279.     if (win_status & 7) return;  // maximized, minimized or titlebar mode
  280.  
  281.     if (win_width < wmin)
  282.         __asm__ __volatile__("int $0x40" ::"a"(67), "b"(-1), "c"(-1), "d"(wmin), "S"(-1));  // SF_CHANGE_WINDOW x,y,w,h
  283.     if (win_hight < hmin)
  284.         __asm__ __volatile__("int $0x40" ::"a"(67), "b"(-1), "c"(-1), "d"(-1), "S"(hmin));  // x,y,w,h
  285.  
  286. }
  287.  
  288.  
  289.