3 #include <stdio.h> // TODO
7 #include "mouse_cursor.h"
13 static WindowManager wminst;
15 static void display(Window *win);
16 static void mouse(Window *win, int bn, bool pressed, int x, int y);
17 static void motion(Window *win, int x, int y);
19 void WindowManager::create_frame(Window *win)
21 Window *frame = new Window;
22 Window *parent = win->get_parent();
24 frame->set_display_callback(display);
25 frame->set_mouse_button_callback(mouse);
26 frame->set_mouse_motion_callback(motion);
27 frame->set_focusable(false);
28 frame->add_child(win);
30 windows.push_back(frame);
32 Rect win_rect = win->get_rect();
33 frame->move(win_rect.x - frame_thickness,
34 win_rect.y - frame_thickness - titlebar_thickness);
35 frame->resize(win_rect.width + frame_thickness * 2,
36 win_rect.height + frame_thickness * 2 + titlebar_thickness);
38 win->move(frame_thickness, frame_thickness + titlebar_thickness);
39 parent->add_child(frame);
42 void WindowManager::destroy_frame(Window *win)
44 Window *frame = win->parent;
53 std::list<Window*>::iterator it;
54 it = std::find(windows.begin(), windows.end(), frame);
55 if(it != windows.end()) {
56 root_win->add_child(win);
62 WindowManager::WindowManager()
67 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
70 root_win = new Window;
71 root_win->resize(get_screen_size().width, get_screen_size().height);
73 root_win->set_managed(false);
82 titlebar_thickness = 16;
84 set_focused_frame_color(0, 0, 0);
85 set_unfocused_frame_color(200, 200, 200);
87 mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
88 unsigned char *pixels = mouse_cursor.get_image();
90 for(int i=0; i<mouse_cursor_height; i++) {
91 for(int j=0; j<mouse_cursor_width; j++) {
92 int val = mouse_cursor_bw[i * mouse_cursor_width + j];
101 WindowManager::~WindowManager()
106 void WindowManager::invalidate_region(const Rect &rect)
108 dirty_rects.push_back(rect);
111 void WindowManager::process_windows()
113 if(dirty_rects.empty()) {
117 std::list<Rect>::iterator drit = dirty_rects.begin();
119 while(drit != dirty_rects.end()) {
120 uni = rect_union(uni, *drit++);
126 fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
128 root_win->draw_children(uni);
131 int mouse_x, mouse_y;
132 get_pointer_pos(&mouse_x, &mouse_y);
134 blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
135 get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
138 Rect mouse_rect = {mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height()};
139 invalidate_region(mouse_rect);
144 void WindowManager::add_window(Window *win)
146 if(!win || win == root_win) {
150 root_win->add_child(win);
152 if(windows.empty()) {
156 if(win->get_managed()) {
160 windows.push_back(win);
163 void WindowManager::remove_window(Window *win)
165 std::list<Window*>::iterator it;
166 it = std::find(windows.begin(), windows.end(), win);
168 if(it != windows.end()) {
173 void WindowManager::set_focused_window(Window *win)
175 if(win && win == focused_win) {
181 // invalidate the frame (if any)
182 parent = focused_win->get_parent();
183 if(parent && parent != root_win) {
184 parent->invalidate();
185 fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
194 if(win->get_focusable()) {
196 parent = focused_win->get_parent();
197 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
201 Window **children = win->get_children();
202 for(int i=0; i<win->get_children_count(); i++) {
203 if(children[0]->get_focusable()) {
204 set_focused_window(children[0]);
205 fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
213 const Window *WindowManager::get_focused_window() const
218 Window *WindowManager::get_focused_window()
223 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
225 Window *root_win = wm->get_root_window();
226 Window **children = root_win->get_children();
227 for(int i=root_win->get_children_count() - 1; i>=0; i--) {
228 if(children[i]->contains_point(pointer_x, pointer_y)) {
236 Window *WindowManager::get_root_window() const
241 void WindowManager::set_focused_frame_color(int r, int g, int b)
248 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
250 *r = frame_fcolor[0];
251 *g = frame_fcolor[1];
252 *b = frame_fcolor[2];
255 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
262 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
264 *r = frame_ucolor[0];
265 *g = frame_ucolor[1];
266 *b = frame_ucolor[2];
269 Window *WindowManager::get_grab_window() const
274 void WindowManager::grab_mouse(Window *win)
279 void WindowManager::release_mouse()
284 void WindowManager::raise_window(Window *win)
290 Window *parent = win->get_parent();
291 if(parent != root_win) {
292 if(parent->get_parent() == root_win) {
300 root_win->remove_child(win);
301 root_win->add_child(win);
304 void WindowManager::sink_window(Window *win)
310 std::list<Window*>::iterator it;
311 it = std::find(windows.begin(), windows.end(), win);
312 if(it != windows.end()) {
314 windows.push_front(win);
318 static void display(Window *win)
321 Window *child = win->get_children()[0];
323 Rect abs_rect = win->get_absolute_rect();
325 //TODO 5 not hardcoded
326 set_text_position(abs_rect.x + 5, abs_rect.y + 15);
327 set_text_color(255, 255, 255);
329 if(child == wm->get_focused_window()) {
330 wm->get_focused_frame_color(&r, &g, &b);
331 fill_rect(abs_rect, r, g, b);
334 wm->get_unfocused_frame_color(&r, &g, &b);
335 fill_rect(win->get_absolute_rect(), r, g, b);
338 draw_text(child->get_title());
341 static int prev_x, prev_y;
343 static void mouse(Window *win, int bn, bool pressed, int x, int y)
348 wm->raise_window(win);
358 static void motion(Window *win, int x, int y)
360 int left_bn = get_button(0);
368 Rect rect = win->get_rect();
369 win->move(rect.x + dx, rect.y + dy);