4 #include <stdio.h> // TODO
8 #include "mouse_cursor.h"
14 #define DCLICK_INTERVAL 400
18 static void display(Window *win);
19 static void mouse(Window *win, int bn, bool pressed, int x, int y);
20 static void motion(Window *win, int x, int y);
22 bool init_window_manager()
24 if(!(wm = new WindowManager)) {
31 void destroy_window_manager()
36 void WindowManager::create_frame(Window *win)
38 Window *frame = new Window;
39 Window *parent = win->get_parent();
41 frame->set_display_callback(display);
42 frame->set_mouse_button_callback(mouse);
43 frame->set_mouse_motion_callback(motion);
44 frame->set_focusable(false);
45 frame->add_child(win);
47 windows.push_back(frame);
49 Rect win_rect = win->get_rect();
50 frame->move(win_rect.x - frame_thickness,
51 win_rect.y - frame_thickness - titlebar_thickness);
52 frame->resize(win_rect.width + frame_thickness * 2,
53 win_rect.height + frame_thickness * 2 + titlebar_thickness);
55 win->move(frame_thickness, frame_thickness + titlebar_thickness);
56 parent->add_child(frame);
59 void WindowManager::destroy_frame(Window *win)
61 Window *frame = win->parent;
70 std::list<Window*>::iterator it;
71 it = std::find(windows.begin(), windows.end(), frame);
72 if(it != windows.end()) {
73 root_win->add_child(win);
79 WindowManager::WindowManager()
84 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
87 root_win = new Window;
88 root_win->resize(get_screen_size().width, get_screen_size().height);
90 root_win->set_managed(false);
100 titlebar_thickness = 16;
102 set_focused_frame_color(0, 0, 0);
103 set_unfocused_frame_color(200, 200, 200);
105 mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
106 unsigned char *pixels = mouse_cursor.get_image();
108 for(int i=0; i<mouse_cursor_height; i++) {
109 for(int j=0; j<mouse_cursor_width; j++) {
110 int val = mouse_cursor_bw[i * mouse_cursor_width + j];
119 WindowManager::~WindowManager()
124 void WindowManager::invalidate_region(const Rect &rect)
126 dirty_rects.push_back(rect);
129 void WindowManager::process_windows()
131 if(dirty_rects.empty()) {
135 std::list<Rect>::iterator drit = dirty_rects.begin();
137 while(drit != dirty_rects.end()) {
138 uni = rect_union(uni, *drit++);
144 fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
146 root_win->draw_children(uni);
149 int mouse_x, mouse_y;
150 get_pointer_pos(&mouse_x, &mouse_y);
152 blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
153 get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
156 Rect mouse_rect = {mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height()};
157 invalidate_region(mouse_rect);
162 void WindowManager::add_window(Window *win)
164 if(!win || win == root_win) {
168 root_win->add_child(win);
170 if(windows.empty()) {
174 if(win->get_managed()) {
178 windows.push_back(win);
181 void WindowManager::remove_window(Window *win)
183 std::list<Window*>::iterator it;
184 it = std::find(windows.begin(), windows.end(), win);
186 if(it != windows.end()) {
191 void WindowManager::set_focused_window(Window *win)
193 if(win && win == focused_win) {
199 // invalidate the frame (if any)
200 parent = focused_win->get_parent();
201 if(parent && parent != root_win) {
202 parent->invalidate();
203 fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
212 if(win->get_focusable()) {
214 parent = focused_win->get_parent();
215 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
219 Window **children = win->get_children();
220 for(int i=0; i<win->get_children_count(); i++) {
221 if(children[0]->get_focusable()) {
222 set_focused_window(children[0]);
223 fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
231 const Window *WindowManager::get_focused_window() const
236 Window *WindowManager::get_focused_window()
241 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
243 Window *root_win = wm->get_root_window();
244 Window **children = root_win->get_children();
245 for(int i=root_win->get_children_count() - 1; i>=0; i--) {
246 if(children[i]->contains_point(pointer_x, pointer_y)) {
254 Window *WindowManager::get_root_window() const
259 void WindowManager::set_focused_frame_color(int r, int g, int b)
266 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
268 *r = frame_fcolor[0];
269 *g = frame_fcolor[1];
270 *b = frame_fcolor[2];
273 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
280 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
282 *r = frame_ucolor[0];
283 *g = frame_ucolor[1];
284 *b = frame_ucolor[2];
287 Window *WindowManager::get_grab_window() const
292 void WindowManager::grab_mouse(Window *win)
297 void WindowManager::release_mouse()
302 void WindowManager::raise_window(Window *win)
308 Window *parent = win->get_parent();
309 if(parent != root_win) {
310 if(parent->get_parent() == root_win) {
318 root_win->remove_child(win);
319 root_win->add_child(win);
322 void WindowManager::sink_window(Window *win)
328 std::list<Window*>::iterator it;
329 it = std::find(windows.begin(), windows.end(), win);
330 if(it != windows.end()) {
332 windows.push_front(win);
336 void WindowManager::maximize_window(Window *win)
338 win->normal_rect = win->rect;
340 Rect rect = get_screen_size();
343 if((frame = win->get_parent())) {
344 frame->normal_rect = frame->rect;
345 frame->resize(rect.width, rect.height);
346 frame->move(rect.x, rect.y);
348 rect.width -= frame_thickness * 2;
349 rect.height -= frame_thickness * 2 + titlebar_thickness;
354 win->move(rect.x, rect.y);
357 win->resize(rect.width, rect.height);
358 win->set_state(Window::STATE_MAXIMIZED);
361 void WindowManager::unmaximize_window(Window *win)
363 win->resize(win->normal_rect.width, win->normal_rect.height);
364 win->move(win->normal_rect.x, win->normal_rect.y);
367 if((frame = win->get_parent())) {
368 frame->resize(frame->normal_rect.width, frame->normal_rect.height);
369 frame->move(frame->normal_rect.x, frame->normal_rect.y);
372 win->set_state(Window::STATE_NORMAL);
375 static void display(Window *win)
378 Window *child = win->get_children()[0];
380 Rect abs_rect = win->get_absolute_rect();
382 //TODO 5 not hardcoded
383 set_text_position(abs_rect.x + 5, abs_rect.y + 15);
384 set_text_color(255, 255, 255);
386 if(child == wm->get_focused_window()) {
387 wm->get_focused_frame_color(&r, &g, &b);
388 fill_rect(abs_rect, r, g, b);
391 wm->get_unfocused_frame_color(&r, &g, &b);
392 fill_rect(win->get_absolute_rect(), r, g, b);
395 draw_text(child->get_title());
398 static int prev_x, prev_y;
400 static void mouse(Window *win, int bn, bool pressed, int x, int y)
402 static long last_click = 0;
406 long time = winnie_get_time();
407 if((time - last_click) < DCLICK_INTERVAL) {
408 Window *child = win->get_children()[0];
409 Window::State state = child->get_state();
410 if(state == Window::STATE_MAXIMIZED) {
411 child->set_state(Window::STATE_NORMAL);
412 wm->unmaximize_window(child);
414 else if(state == Window::STATE_NORMAL) {
415 wm->maximize_window(child);
420 wm->raise_window(win);
432 static void motion(Window *win, int x, int y)
434 int left_bn = get_button(0);
442 Rect rect = win->get_rect();
443 win->move(rect.x + dx, rect.y + dy);