Subversion Repositories Kolibri OS

Rev

Rev 6516 | Rev 6612 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6516 Rev 6559
1
/*
1
/*
2
    KolibriGUI demobox
2
    KolibriGUI demobox
3
    -OptionBox
3
    -OptionBox
4
    -MenuBar
4
    -MenuBar
5
    -Frame
5
    -Frame
6
 
6
 
7
    Free for all
7
    Free for all
8
 
8
 
9
    Initially written by Siemargl, 2016
9
    Initially written by Siemargl, 2016
10
 
10
 
11
 
11
 
12
    ToDo
12
    ToDo
13
*/
13
*/
14
 
14
 
15
#include 
15
#include 
16
#include 
16
#include 
17
#include 
17
#include 
18
#include "kos32sys.h"
18
#include "kos32sys.h"
19
#include "kolibri_gui.h"
19
#include "kolibri_gui.h"
20
 
20
 
21
#define SCAN_CODE_ALTM  50
21
#define SCAN_CODE_ALTM  50
22
#define SCAN_CODE_ALTE  18
22
#define SCAN_CODE_ALTE  18
23
inline
23
inline
24
uint32_t get_os_keyb_modifiers()
24
uint32_t get_os_keyb_modifiers()
25
{
25
{
26
    register uint32_t val;
26
    register uint32_t val;
27
    __asm__ __volatile__(
27
    __asm__ __volatile__(
28
    "int $0x40"
28
    "int $0x40"
29
    :"=a"(val)
29
    :"=a"(val)
30
    :"a"(66), "b"(3));
30
    :"a"(66), "b"(3));
31
    return val;
31
    return val;
32
};
32
};
33
 
33
 
34
#define KEY_LSHIFT     0x1
34
#define KEY_LSHIFT     0x1
35
#define KEY_RSHIFT     0x2
35
#define KEY_RSHIFT     0x2
36
#define KEY_LCTRL      0x4
36
#define KEY_LCTRL      0x4
37
#define KEY_RCTRL      0x8
37
#define KEY_RCTRL      0x8
38
#define KEY_LALT       0x10
38
#define KEY_LALT       0x10
39
#define KEY_RALT       0x20
39
#define KEY_RALT       0x20
40
#define KEY_CAPSLOCK   0x40
40
#define KEY_CAPSLOCK   0x40
41
#define KEY_NUMLOCK    0x80
41
#define KEY_NUMLOCK    0x80
42
#define KEY_SCROLLLOCK 0x100
42
#define KEY_SCROLLLOCK 0x100
43
#define KEY_LWIN       0x200
43
#define KEY_LWIN       0x200
44
#define KEY_RWIN       0x400
44
#define KEY_RWIN       0x400
45
 
45
 
46
inline
46
inline
47
void set_os_keyb_mode(int mode)
47
void set_os_keyb_mode(int mode)
48
// 0 - ASCII, 1 - SCAN
48
// 0 - ASCII, 1 - SCAN
49
{
49
{
50
    __asm__ __volatile__(
50
    __asm__ __volatile__(
51
    "int $0x40"
51
    "int $0x40"
52
    ::"a"(66), "b"(1), "c"(mode));
52
    ::"a"(66), "b"(1), "c"(mode));
53
};
53
};
54
 
54
 
55
int main()
55
int main()
56
{
56
{
57
    /* Load all libraries, initialize global tables like system color table and
57
    /* Load all libraries, initialize global tables like system color table and
58
    operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
58
    operations table. kolibri_gui_init() will EXIT with mcall -1 if it fails
59
    to do it's job. This is all you need to call and all libraries and GUI
59
    to do it's job. This is all you need to call and all libraries and GUI
60
    elements can be used after a successful call to this function
60
    elements can be used after a successful call to this function
61
    */
61
    */
62
    kolibri_gui_init();
62
    kolibri_gui_init();
63
    int gui_event = KOLIBRI_EVENT_REDRAW;
63
    int gui_event = KOLIBRI_EVENT_REDRAW;
64
    uint32_t pressed_button = 0;
64
    uint32_t pressed_button = 0;
65
//    uint32_t mouse_button;
65
//    uint32_t mouse_button;
66
//    pos_t   mouse_pos;
66
//    pos_t   mouse_pos;
67
    oskey_t keypress;
67
    oskey_t keypress;
68
 
68
 
69
 
69
 
70
    // creating GUI using library functions
70
    // creating GUI using library functions
71
    kolibri_window *main_window = kolibri_new_window(50, 40, 400, 160, "OptionBox and Menu demo");
71
    kolibri_window *main_window = kolibri_new_window(50, 40, 400, 160, "OptionBox and Menu demo");
72
    //check_box *checkbox = kolibri_new_check_box(20, 45, 12, 12, "Append BOARDMSG to entered message.");
72
    //check_box *checkbox = kolibri_new_check_box(20, 45, 12, 12, "Append BOARDMSG to entered message.");
73
 
73
 
74
    option_box opts1[3];
74
    option_box opts1[3];
75
    option_box *option1sel = opts1; // intially selected RED
75
    option_box *option1sel = opts1; // intially selected RED
76
 
76
 
77
    option_box *op1_1 = gui_optionbox(opts1, X_Y(20, 50), "G1 Item RED", &option1sel);
77
    option_box *op1_1 = gui_optionbox(opts1, X_Y(20, 50), "G1 Item RED", &option1sel);
78
    option_box *op1_2 = gui_optionbox(opts1+1, X_Y(20, 70), "G1 Item GREEN", &option1sel);
78
    option_box *op1_2 = gui_optionbox(opts1+1, X_Y(20, 70), "G1 Item GREEN", &option1sel);
79
    option_box *op1_3 = gui_optionbox(opts1+2, X_Y(20, 90), "G1 Item BLUE", &option1sel);
79
    option_box *op1_3 = gui_optionbox(opts1+2, X_Y(20, 90), "G1 Item BLUE", &option1sel);
80
    option_box* option_group1[] = {op1_1, op1_2, op1_3, NULL};
80
    option_box* option_group1[] = {op1_1, op1_2, op1_3, NULL};
81
 
81
 
82
    option_box opts2[3];
82
    option_box opts2[3];
83
    option_box *option2sel = opts2 + 1; // intially selected #2
83
    option_box *option2sel = opts2 + 1; // intially selected #2
84
    option_box *op2_1 = gui_optionbox(&opts2[0], X_Y(140, 50), "G2 Item 1st", &option2sel);
84
    option_box *op2_1 = gui_optionbox(&opts2[0], X_Y(140, 50), "G2 Item 1st", &option2sel);
85
    option_box *op2_2 = gui_optionbox(&opts2[1], X_Y(140, 70), "G2 Item 2nd", &option2sel);
85
    option_box *op2_2 = gui_optionbox(&opts2[1], X_Y(140, 70), "G2 Item 2nd", &option2sel);
86
    option_box *op2_3 = gui_optionbox(&opts2[2], X_Y(140, 90), "G2 Item 3rd", &option2sel);
86
    option_box *op2_3 = gui_optionbox(&opts2[2], X_Y(140, 90), "G2 Item 3rd", &option2sel);
87
    option_box* option_group2[] = {op2_1, op2_2, op2_3, NULL};
87
    option_box* option_group2[] = {op2_1, op2_2, op2_3, NULL};
88
 
88
 
89
    frame *fr1 = kolibri_new_frame_def(X_Y(12, 110), X_Y(40, 70), "Option 1");
89
    frame *fr1 = kolibri_new_frame_def(X_Y(12, 110), X_Y(40, 70), "Option 1");
90
    frame *fr2 = kolibri_new_frame_def(X_Y(132, 100), X_Y(40, 70), "Option 2");
90
    frame *fr2 = kolibri_new_frame_def(X_Y(132, 100), X_Y(40, 70), "Option 2");
91
 
91
 
92
    gui_add_optiongroup(main_window, option_group1);  // new syntax
92
    gui_add_optiongroup(main_window, option_group1);  // new syntax
93
    gui_add_optiongroup(main_window, option_group2);
93
    gui_add_optiongroup(main_window, option_group2);
94
    gui_add_frame(main_window, fr1);
94
    gui_add_frame(main_window, fr1);
95
    gui_add_frame(main_window, fr2);
95
    gui_add_frame(main_window, fr2);
96
 
96
 
97
    int option_index1 = 0;  // index of selected option
97
    int option_index1 = 0;  // index of selected option
98
    int option_index2 = 0;
98
    int option_index2 = 0;
99
 
99
 
100
    char *menu1stru[] = {"Menu1", "Set RED", "Set GREEN", "Set BLUE", NULL};
100
    char *menu1stru[] = {"Menu1", "Set RED", "Set GREEN", "Set BLUE", NULL};
101
    menubar* menu1 = kolibri_new_menubar_def(X_Y(10, 40), X_Y(5, 15), 80, 100, menu1stru);
101
    menubar* menu1 = kolibri_new_menubar_def(X_Y(10, 40), X_Y(5, 15), 80, 100, menu1stru);
102
    gui_add_menubar(main_window, menu1);
102
    gui_add_menubar(main_window, menu1);
103
 
103
 
104
    char *menu2stru[] = {"mEnu2", "Set Option 1", "Set Option 2", "Set Option 3", NULL};
104
    char *menu2stru[] = {"mEnu2", "Set Option 1", "Set Option 2", "Set Option 3", NULL};
105
    menubar* menu2 = kolibri_new_menubar_def(X_Y(50, 40), X_Y(5, 15), 80, 100, menu2stru);
105
    menubar* menu2 = kolibri_new_menubar_def(X_Y(50, 40), X_Y(5, 15), 80, 100, menu2stru);
106
    gui_add_menubar(main_window, menu2);
106
    gui_add_menubar(main_window, menu2);
107
 
107
 
108
    char *menu3stru[] = {"Quit", NULL};
108
    char *menu3stru[] = {"Quit", NULL};
109
    menubar* menu3 = kolibri_new_menubar_def(X_Y(90, 40), X_Y(5, 15), 0, 0, menu3stru);
109
    menubar* menu3 = kolibri_new_menubar_def(X_Y(90, 40), X_Y(5, 15), 0, 0, menu3stru);
110
    menu3->type = 1;  // no subitems
110
    menu3->type = 1;  // no subitems
111
    gui_add_menubar(main_window, menu3);
111
    gui_add_menubar(main_window, menu3);
112
 
112
 
113
 
113
 
114
 
114
 
115
    set_os_keyb_mode(1); // needed for keyboard use in menu
115
    set_os_keyb_mode(1); // needed for keyboard use in menu
116
 
116
 
117
 
117
 
118
    do  /* Start of main activity loop */
118
    do  /* Start of main activity loop */
119
    {
119
    {
120
        if(option_index1 != option1sel - opts1)
120
        if(option_index1 != option1sel - opts1)
121
            debug_board_printf("Option1 change to %d\n", option1sel - opts1);
121
            debug_board_printf("Option1 change to %d\n", option1sel - opts1);
122
        if(option_index2 != option2sel - opts2)
122
        if(option_index2 != option2sel - opts2)
123
            debug_board_printf("Option2 change to %d\n", option2sel - opts2);
123
            debug_board_printf("Option2 change to %d\n", option2sel - opts2);
124
        option_index1 = option1sel - opts1;
124
        option_index1 = option1sel - opts1;
125
        option_index2 = option2sel - opts2;
125
        option_index2 = option2sel - opts2;
126
 
126
 
127
        switch(gui_event)
127
        switch(gui_event)
128
        {
128
        {
129
        case KOLIBRI_EVENT_REDRAW:
129
        case KOLIBRI_EVENT_REDRAW:
130
            kolibri_handle_event_redraw(main_window);
130
            kolibri_handle_event_redraw(main_window);
131
            break;
131
            break;
132
        case KOLIBRI_EVENT_NONE:
132
        case KOLIBRI_EVENT_NONE:
133
			break;
133
			break;
134
        case KOLIBRI_EVENT_KEY:
134
        case KOLIBRI_EVENT_KEY:
135
            keypress = get_key();
135
            keypress = get_key();
136
            debug_board_printf("Key pressed state(%d) code(%d) ctrl_key(%d)  modifiers(%#x)\n", keypress.state, keypress.code, keypress.ctrl_key, get_os_keyb_modifiers());
136
            debug_board_printf("Key pressed state(%d) code(%d) ctrl_key(%d)  modifiers(%#x)\n", keypress.state, keypress.code, keypress.ctrl_key, get_os_keyb_modifiers());
137
            kolibri_handle_event_key(main_window); // ???????
137
            kolibri_handle_event_key(main_window); // ???????
138
 
138
 
139
            if(keypress.code == SCAN_CODE_ALTM && get_os_keyb_modifiers() & (KEY_LALT | KEY_RALT))
139
            if(keypress.code == SCAN_CODE_ALTM && get_os_keyb_modifiers() & (KEY_LALT | KEY_RALT))
140
                (*menu_bar_activate)(menu1); // wont work, immediately redraw command closes menu (
140
                (*menu_bar_activate)(menu1); // wont work, immediately redraw command closes menu (  . but Alt+F1 worked in opendial.asm:463
141
 
141
 
142
			break;
142
			break;
143
        case KOLIBRI_EVENT_BUTTON:
143
        case KOLIBRI_EVENT_BUTTON:
144
            pressed_button = get_os_button();
144
            pressed_button = get_os_button();
145
            switch (pressed_button)
145
            switch (pressed_button)
146
            {
146
            {
147
              case BTN_QUIT:
147
              case BTN_QUIT:
148
                return 0;
148
                return 0;
149
                break;
149
                break;
150
            }
150
            }
151
            break;
151
            break;
152
        case KOLIBRI_EVENT_MOUSE:
152
        case KOLIBRI_EVENT_MOUSE:
153
//            mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
153
//            mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
154
//            mouse_button = get_mouse_eventstate();
154
//            mouse_button = get_mouse_eventstate();
155
            kolibri_handle_event_mouse(main_window);
155
            kolibri_handle_event_mouse(main_window);
156
            if(menu1->click && menu1->cursor_out)
156
            if(menu1->click && menu1->cursor_out)
157
            {
157
            {
158
                option1sel = opts1 + menu1->cursor_out - 1; // check bounds ?
158
                option1sel = opts1 + menu1->cursor_out - 1; // check bounds ?
159
                (*option_box_draw)(option_group1);
159
                (*option_box_draw)(option_group1);
160
            }
160
            }
161
            if(menu2->click && menu2->cursor_out)
161
            if(menu2->click && menu2->cursor_out)
162
            {
162
            {
163
                option2sel = opts2 + menu2->cursor_out - 1; // check bounds ?
163
                option2sel = opts2 + menu2->cursor_out - 1; // check bounds ?
164
                (*option_box_draw)(option_group2);
164
                (*option_box_draw)(option_group2);
165
            }
165
            }
166
            if(menu3->click && menu3->cursor_out)
166
            if(menu3->click && menu3->cursor_out)
167
            {
167
            {
168
                return 0; // quit
168
                return 0; // quit
169
            }
169
            }
170
 
170
 
171
            break;
171
            break;
172
        }
172
        }
173
 
173
 
174
        gui_event = get_os_event();
174
        gui_event = get_os_event();
175
    } while(1) ; /* End of main activity loop */
175
    } while(1) ; /* End of main activity loop */
176
 
176
 
177
  return 0;
177
  return 0;
178
}
178
}