Rev 6526 | Rev 6559 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6524 | siemargl | 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 |
||
17 | #include |
||
18 | #include |
||
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]; |
||
6535 | siemargl | 25 | char** sys_path = (char**)0x20; // hack - get path from KOS header. analog argv[0] |
6524 | siemargl | 26 | |
6526 | siemargl | 27 | int main(int argc, char **argv) |
6524 | siemargl | 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), |
||
6526 | siemargl | 48 | *image_data, *pc; |
6524 | siemargl | 49 | // make full path + argv |
6526 | siemargl | 50 | strcpy(temp_path, *sys_path); |
6535 | siemargl | 51 | pc = strrchr(temp_path, '/'); // this fails if has params with '/' within. use argv[0] instead |
6526 | siemargl | 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"); |
||
6524 | siemargl | 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)); |
||
6526 | siemargl | 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)); |
||
6524 | siemargl | 83 | |
84 | statictext labels[3]; // tips |
||
85 | gui_add_statictext(main_window, kolibri_statictext_def(&labels[0], X_Y(5, 28), "Open")); |
||
6526 | siemargl | 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")); |
||
6524 | siemargl | 88 | |
89 | open_dialog *dlg_opensave = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320); |
||
90 | (*OpenDialog_init)(dlg_opensave); |
||
91 | |||
92 | pathview pview; |
||
6526 | siemargl | 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 |
6524 | siemargl | 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); |
||
6526 | siemargl | 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 | { |
||
6524 | siemargl | 130 | (*path_show_prepare)(&pview); |
6526 | siemargl | 131 | (*path_show_draw)(&pview); |
132 | } |
||
6524 | siemargl | 133 | } |
134 | if(tbar[1].click) // save |
||
135 | { |
||
136 | tbar[1].click = 0; |
||
137 | dlg_opensave->mode = SAVE; |
||
138 | (*OpenDialog_start)(dlg_opensave); |
||
6526 | siemargl | 139 | if (dlg_opensave->status != 2 && dlg_opensave->status != 0) // fail or cancel |
6524 | siemargl | 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); |
||
6526 | siemargl | 148 | if (dlg_opensave->status != 2 && dlg_opensave->status != 0) // fail or cancel |
6524 | siemargl | 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 |