Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7540 leency 1
/*
2
	newlib-style window example
3
*/
4
 
5
 
6
#include 
7
#include 
8
#include 
9
#include 
10
#include "kos32sys1.h"
11
 
12
struct kolibri_system_colors sys_color_table;
13
 
14
char statusbar[255];
15
char proc_info[1024];
16
char text_line[255];
17
 
18
enum BUTTONS
19
{
20
    BTN_QUIT = 1,
21
    BTN_POP = 10,
22
    BTN_UNLOCK = 11
23
};
24
 
25
#define FONT_W 8
26
#define FONT_H 14
27
#define LINES 10
28
 
29
void draw_window()
30
{
31
    int win_hight, win_width, i, pos_y = get_skin_height() + 36;  // 60 == 24+36
32
 
33
    // start redraw
34
    begin_draw();
35
	// define&draw window
36
	sys_create_window(10, 40, 600, 400, "My window", /*sys_color_table.work_area*/0xFFFFFF, 0x13);
37
 
38
    get_proc_info(proc_info);
39
    win_width = *(int*)(proc_info + 0x3E); // client, 2A windows
40
    win_hight = *(int*)(proc_info + 0x42); // client, 2E windows
41
 
42
	define_button((10 << 16) + 80, (30 << 16) + 20, BTN_POP, sys_color_table.work_button);
43
    draw_text_sys("BUTTON1", 15, 34, 0, 0x90000000 | sys_color_table.work_button_text);  //0x80000000 asciiz
44
 
45
	define_button((100 << 16) + 100, (30 << 16) + 20, BTN_UNLOCK, sys_color_table.work_button);
46
    draw_text_sys("BUTTTON2", 110, 34, 0, 0x90000000 | sys_color_table.work_button_text);
47
 
48
    // display statusbar
49
    draw_bar(6, win_hight - 17, win_width - 11, 12, 0x80000000 | sys_color_table.work_area);  //0x80000000 gradient
50
    draw_text_sys(statusbar, 10, win_hight - 15, 0, 0x80000000 | sys_color_table.work_text);
51
 
52
    // display strings
53
    for (i = LINES; i > 0; i--)
54
    {
55
        tiny_snprintf (text_line, sizeof text_line, "Line[%d]<>", i);
56
 
57
        text_line[(win_width - 10 - 5) / FONT_W + 1] = '\0'; // clip text size, seems to big lines crashing OS, and form len by window size
58
//        draw_number_sys(nbytes, 5, pos_y, 6, 0x10000000); 8x12 font
59
        draw_text_sys(text_line, 5, pos_y, 0, 0x90000000 /*| sys_color_table.work_text*/);
60
        pos_y += FONT_H;
61
 
62
        if(pos_y + 29 > win_hight) break; // 12 font + 12 statusbar + 5 border
63
    }
64
 
65
    // end redraw
66
    end_draw();
67
}
68
 
69
int main()
70
{
71
    int gui_event;
72
    uint32_t pressed_button = 0, mouse_button;
73
    pos_t   mouse_pos;
74
    strcpy(statusbar, "Program running...Double click on TEXT for details");
75
 
76
    get_system_colors(&sys_color_table);
77
    set_event_mask(0xC0000027); // mouse events only when focused window and mouse inside
78
 
79
    do  /* Start of main activity loop */
80
    {
81
//        gui_event = wait_for_event(10); // 100 = 1 sec, case you have background work
82
        gui_event = get_os_event();
83
        switch(gui_event)
84
        {
85
        case KOLIBRI_EVENT_NONE:
86
            // background work
87
			break;
88
        case KOLIBRI_EVENT_REDRAW:
89
            draw_window();
90
			break;
91
        case KOLIBRI_EVENT_KEY:
92
            // scroll
93
			break;
94
        case KOLIBRI_EVENT_BUTTON:
95
            pressed_button = get_os_button();
96
            switch (pressed_button)
97
            {
98
              case BTN_POP:
99
                strcpy(statusbar, "POP pressed....");
100
                draw_window();
101
                break;
102
              case BTN_UNLOCK:
103
                strcpy(statusbar, "UNLOCK pressed....");
104
                draw_window();
105
                break;
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
            debug_board_printf("mouse ev (%d,%d)%x\n", mouse_pos.x, mouse_pos.y, mouse_button);
115
            if (mouse_button & (1<<24)) // double click
116
            {
117
                int n = (mouse_pos.y - 60) / FONT_H;
118
                if (n < 0 || n >= LINES) break;
119
                debug_board_printf("click on str(%d), clip slot(%d)\n", n, LINES - n - 1);
120
                tiny_sprintf(statusbar, "click on str(%d), clip slot(%d)\n", n, LINES - n - 1);
121
		draw_window();
122
            }
123
            // ignore
124
            break;
125
        }
126
    } while(1) ; /* End of main activity loop */
127
 
128
  return 0;
129
}
130
 
131
 
132
 
133
void __attribute__ ((noinline)) debug_board_write_str(const char* str){
134
  while(*str)
135
    debug_board_write_byte(*str++);
136
}
137
 
138
void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
139
{
140
        va_list ap;
141
        char log_board[300];
142
 
143
        va_start (ap, format);
144
        tiny_vsnprintf(log_board, sizeof log_board, format, ap);
145
        va_end(ap);
146
        debug_board_write_str(log_board);
147
 
148
}