4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2007 Blender Foundation but based
21 * on ghostwinlay.c (C) 2001-2002 by NaN Holding BV
22 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
32 #include "DNA_listBase.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_windowmanager_types.h"
36 #include "MEM_guardedalloc.h"
38 #include "GHOST_C-api.h"
40 #include "BLI_blenlib.h"
42 #include "BKE_blender.h"
43 #include "BKE_global.h"
44 #include "BKE_utildefines.h"
51 #include "wm_window.h"
52 #include "wm_subwindow.h"
53 #include "wm_event_system.h"
55 #include "ED_screen.h"
57 /* the global to talk to ghost */
58 GHOST_SystemHandle g_system= NULL;
60 /* set by commandline */
61 static int prefsizx= 0, prefsizy= 0, prefstax= 0, prefstay= 0;
64 /* ******** win open & close ************ */
67 static void wm_get_screensize(int *width_r, int *height_r)
70 unsigned int uiheight;
72 GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
77 static void wm_ghostwindow_destroy(wmWindow *win)
81 GHOST_RemoveTimer(g_system, (GHOST_TimerTaskHandle)win->timer);
86 GHOST_DisposeWindow(g_system, win->ghostwin);
91 /* including window itself */
92 void wm_window_free(bContext *C, wmWindow *win)
97 if(C->wm->windrawable==win)
98 C->wm->windrawable= NULL;
99 if(C->wm->winactive==win)
100 C->wm->winactive= NULL;
103 if(C->screen==win->screen)
106 /* XXX free screens */
108 if(win->eventstate) MEM_freeN(win->eventstate);
110 wm_event_free_handlers(&win->handlers);
111 wm_event_free_all(win);
112 wm_subwindows_free(win);
114 wm_ghostwindow_destroy(win);
119 static int find_free_winid(wmWindowManager *wm)
124 for(win= wm->windows.first; win; win= win->next)
131 /* dont change context itself */
132 wmWindow *wm_window_new(bContext *C, bScreen *screen)
134 wmWindow *win= MEM_callocN(sizeof(wmWindow), "window");
136 BLI_addtail(&C->wm->windows, win);
137 win->winid= find_free_winid(C->wm);
143 /* part of wm_window.c api */
144 wmWindow *wm_window_copy(bContext *C, wmWindow *winorig)
146 wmWindow *win= wm_window_new(C, winorig->screen);
148 win->posx= winorig->posx+10;
149 win->posy= winorig->posy;
150 win->sizex= winorig->sizex;
151 win->sizey= winorig->sizey;
153 win->screen= ED_screen_duplicate(win, win->screen);
154 win->screen->do_refresh= 1;
155 win->screen->do_draw= 1;
160 /* operator callback */
161 int wm_window_duplicate_op(bContext *C, wmOperator *op)
164 wm_window_copy(C, C->window);
170 /* fullscreen operator callback */
171 int wm_window_fullscreen_toggle_op(bContext *C, wmOperator *op)
173 GHOST_TWindowState state = GHOST_GetWindowState(C->window->ghostwin);
174 if(state!=GHOST_kWindowStateFullScreen)
175 GHOST_SetWindowState(C->window->ghostwin, GHOST_kWindowStateFullScreen);
177 GHOST_SetWindowState(C->window->ghostwin, GHOST_kWindowStateNormal);
183 /* this is event from ghost */
184 static void wm_window_close(bContext *C, wmWindow *win)
186 BLI_remlink(&C->wm->windows, win);
187 wm_window_free(C, win);
189 if(C->wm->windows.first==NULL)
193 static void wm_window_open(wmWindowManager *wm, char *title, wmWindow *win)
195 GHOST_WindowHandle ghostwin;
196 GHOST_TWindowState inital_state;
197 int scr_w, scr_h, posy;
199 wm_get_screensize(&scr_w, &scr_h);
200 posy= (scr_h - win->posy - win->sizey);
202 // inital_state = GHOST_kWindowStateFullScreen;
203 // inital_state = GHOST_kWindowStateMaximized;
204 inital_state = GHOST_kWindowStateNormal;
208 extern int macPrefState; /* creator.c */
209 inital_state += macPrefState;
213 ghostwin= GHOST_CreateWindow(g_system, title,
214 win->posx, posy, win->sizex, win->sizey,
216 GHOST_kDrawingContextTypeOpenGL,
221 win->ghostwin= ghostwin;
222 GHOST_SetWindowUserData(ghostwin, win); /* pointer back */
224 if(win->eventstate==NULL)
225 win->eventstate= MEM_callocN(sizeof(wmEvent), "window event state");
227 /* add keymap handlers (1 for all keys in map!) */
228 WM_event_add_keymap_handler(&wm->windowkeymap, &win->handlers);
229 WM_event_add_keymap_handler(&wm->screenkeymap, &win->handlers);
231 /* until screens get drawn, make it nice grey */
232 glClearColor(.55, .55, .55, 0.0);
233 glClear(GL_COLOR_BUFFER_BIT);
234 wm_window_swap_buffers(win);
236 /* standard state vars for window */
237 glEnable(GL_SCISSOR_TEST);
241 /* for wmWindows without ghostwin, open these and clear */
242 void wm_window_add_ghostwindows(wmWindowManager *wm)
246 /* no commandline prefsize? then we set this */
248 wm_get_screensize(&prefsizx, &prefsizy);
252 extern void wm_set_apple_prefsize(int, int); /* wm_apple.c */
254 wm_set_apple_prefsize(prefsizx, prefsizy);
263 for(win= wm->windows.first; win; win= win->next) {
264 if(win->ghostwin==NULL) {
268 win->sizex= prefsizx;
269 win->sizey= prefsizy;
272 wm_window_open(wm, "Blender", win);
277 /* ************ events *************** */
279 static int query_qual(char qual)
281 GHOST_TModifierKeyMask left, right;
285 left= GHOST_kModifierKeyLeftShift;
286 right= GHOST_kModifierKeyRightShift;
287 } else if (qual=='c') {
288 left= GHOST_kModifierKeyLeftControl;
289 right= GHOST_kModifierKeyRightControl;
290 } else if (qual=='C') {
291 left= right= GHOST_kModifierKeyCommand;
293 left= GHOST_kModifierKeyLeftAlt;
294 right= GHOST_kModifierKeyRightAlt;
297 GHOST_GetModifierKeyState(g_system, left, &val);
299 GHOST_GetModifierKeyState(g_system, right, &val);
304 void wm_window_make_drawable(bContext *C, wmWindow *win)
306 if (win != C->window && win->ghostwin) {
307 // win->lmbut= 0; /* keeps hanging when mousepressed while other window opened */
309 C->wm->windrawable= win;
311 C->screen= win->screen;
312 printf("set drawable %d\n", win->winid);
313 GHOST_ActivateWindowDrawingContext(win->ghostwin);
317 /* called by ghost, here we handle events for windows themselves or send to event system */
318 static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private)
320 bContext *C= private;
321 GHOST_TEventType type= GHOST_GetEventType(evt);
323 if (type == GHOST_kEventQuit) {
326 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
327 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
331 // XXX - should be checked, why are we getting an event here, and
335 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
336 // XXX - should be checked, why are we getting an event here, and
341 win= GHOST_GetWindowUserData(ghostwin);
345 case GHOST_kEventWindowDeactivate:
346 win->active= 0; /* XXX */
348 case GHOST_kEventWindowActivate:
350 GHOST_TEventKeyData kdata;
353 C->wm->winactive= win; /* no context change! c->window is drawable, or for area queues */
356 // window_handle(win, INPUTCHANGE, win->active);
358 /* bad ghost support for modifier keys... so on activate we set the modifiers again */
360 if (win->eventstate->shift && !query_qual('s')) {
361 kdata.key= GHOST_kKeyLeftShift;
362 wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
364 if (win->eventstate->ctrl && !query_qual('c')) {
365 kdata.key= GHOST_kKeyLeftControl;
366 wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
368 if (win->eventstate->alt && !query_qual('a')) {
369 kdata.key= GHOST_kKeyLeftAlt;
370 wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
372 if (win->eventstate->oskey && !query_qual('C')) {
373 kdata.key= GHOST_kKeyCommand;
374 wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
377 /* entering window, update mouse pos. but no event */
378 GHOST_GetCursorPosition(g_system, &wx, &wy);
380 GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
381 win->eventstate->x= cx;
382 win->eventstate->y= (win->sizey-1) - cy;
384 ED_screen_set_subwinactive(win); /* active subwindow in screen */
386 wm_window_make_drawable(C, win);
389 case GHOST_kEventWindowClose: {
390 wm_window_close(C, win);
393 case GHOST_kEventWindowUpdate: {
394 printf("ghost redraw\n");
396 wm_window_make_drawable(C, win);
397 WM_event_add_notifier(C->wm, win, 0, WM_NOTE_WINDOW_REDRAW, 0);
401 case GHOST_kEventWindowSize:
402 case GHOST_kEventWindowMove: {
403 GHOST_RectangleHandle client_rect;
404 int l, t, r, b, scr_w, scr_h;
406 client_rect= GHOST_GetClientBounds(win->ghostwin);
407 GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
409 GHOST_DisposeRectangle(client_rect);
411 wm_get_screensize(&scr_w, &scr_h);
415 win->posy= scr_h - t - win->sizey;
419 GHOST_TWindowState state;
420 state = GHOST_GetWindowState(win->ghostwin);
422 if(state==GHOST_kWindowStateNormal)
423 printf("window state: normal\n");
424 else if(state==GHOST_kWindowStateMinimized)
425 printf("window state: minimized\n");
426 else if(state==GHOST_kWindowStateMaximized)
427 printf("window state: maximized\n");
428 else if(state==GHOST_kWindowStateFullScreen)
429 printf("window state: fullscreen\n");
431 if(type!=GHOST_kEventWindowSize)
432 printf("win move event pos %d %d size %d %d\n", win->posx, win->posy, win->sizex, win->sizey);
436 wm_window_make_drawable(C, win);
437 WM_event_add_notifier(C->wm, win, 0, WM_NOTE_SCREEN_CHANGED, 0);
442 if(type==GHOST_kEventKeyDown) // XXX debug
443 WM_event_add_notifier(C->wm, win, 0, WM_NOTE_WINDOW_REDRAW, 0);
444 wm_event_add_ghostevent(win, type, data);
452 void wm_window_process_events(int wait_for_event)
454 GHOST_ProcessEvents(g_system, wait_for_event);
455 GHOST_DispatchEvents(g_system);
458 /* **************** init ********************** */
460 void wm_ghost_init(bContext *C)
463 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(ghost_event_proc, C);
465 g_system= GHOST_CreateSystem();
466 GHOST_AddEventConsumer(g_system, consumer);
470 /* **************** timer ********************** */
472 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
474 wmWindow *win= GHOST_GetTimerTaskUserData(timer);
476 wm_event_add_ghostevent(win, win->timer_event, NULL);
479 void wm_window_set_timer(wmWindow *win, int delay_ms, int event)
481 if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
483 win->timer_event= event;
484 win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
487 /* ************************************ */
489 void wm_window_set_title(wmWindow *win, char *title)
491 GHOST_SetTitle(win->ghostwin, title);
494 void wm_window_get_position(wmWindow *win, int *posx_r, int *posy_r)
500 void wm_window_get_size(wmWindow *win, int *width_r, int *height_r)
502 *width_r= win->sizex;
503 *height_r= win->sizey;
506 void wm_window_set_size(wmWindow *win, int width, int height)
508 GHOST_SetClientSize(win->ghostwin, width, height);
511 void wm_window_lower(wmWindow *win)
513 GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
516 void wm_window_raise(wmWindow *win)
518 GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
520 // markdirty_all(); /* to avoid redraw errors in fullscreen mode (aphex) */
524 void wm_window_swap_buffers(wmWindow *win)
526 GHOST_SwapWindowBuffers(win->ghostwin);
529 /* ******************* exported api ***************** */
532 /* called whem no ghost system was initialized */
533 void WM_setprefsize(int stax, int stay, int sizx, int sizy)