fixed mouse events the windows can grab the mouse
[winnie] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "winnie.h"
6
7 static void display(Window *win);
8 static void keyboard(Window *win, int key, bool pressed);
9 static void button(Window *win, int bn, bool pressed, int x, int y);
10 static void motion(Window *win, int x, int y);
11 static void cleanup();
12
13 int main()
14 {
15         if(!winnie_init()) {
16                 exit(1);
17         }
18
19         atexit(cleanup);
20
21         Window *win1 = new Window;
22         win1->set_title("red");
23         win1->move(5, 10);
24         win1->resize(200, 300);
25         win1->set_display_callback(display);
26         win1->set_keyboard_callback(keyboard);
27         win1->set_mouse_button_callback(button);
28         win1->set_mouse_motion_callback(motion);
29
30         Window *win2 = new Window;
31         win2->set_title("green");
32         win2->move(150, 10);
33         win2->resize(200, 300);
34         win2->set_display_callback(display);
35         win2->set_keyboard_callback(keyboard);
36         win2->set_mouse_button_callback(button);
37         win2->set_mouse_motion_callback(motion);
38
39         wm->add_window(win1);
40         wm->add_window(win2);
41
42         while(1) {
43                 process_events();
44         }
45 }
46
47 static void display(Window *win)
48 {
49         const char *win_title = win->get_title();
50         const char *t1 = "red";
51         const char *t2 = "green";
52
53         if(!strcmp(win_title, t1)) {
54                 fill_rect(win->get_absolute_rect(), 255, 0, 0);
55         }
56
57         if(!strcmp(win_title, t2)) {
58                 fill_rect(win->get_absolute_rect(), 0, 255, 0);
59         }
60 }
61
62 static void keyboard(Window *win, int key, bool pressed)
63 {
64         switch(key) {
65         case 'q':
66                 exit(0);
67         }
68 }
69
70 static void button(Window *win, int bn, bool pressed, int x, int y)
71 {
72         printf("WINDOW(%p) button %d %s\n", (void*)win, bn, pressed ? "press" : "release");
73 }
74
75 static void motion(Window *win, int x, int y)
76 {
77         printf("WINDOW(%p) motion %d %d\n", (void*)win, x, y);
78 }
79
80 static void cleanup()
81 {
82         winnie_shutdown();
83 }