* add exit app with CTRLKEY+QKEY.
[blender-staging.git] / source / blender / windowmanager / intern / wm_window.c
1 /**
2  * $Id: wm_window.c
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
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.
23  *
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "DNA_listBase.h"       
33 #include "DNA_screen_types.h"
34 #include "DNA_windowmanager_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "GHOST_C-api.h"
39
40 #include "BLI_blenlib.h"
41
42 #include "BKE_blender.h"
43 #include "BKE_global.h"
44 #include "BKE_utildefines.h"
45
46 #include "BIF_gl.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50 #include "wm.h"
51 #include "wm_window.h"
52 #include "wm_subwindow.h"
53 #include "wm_event_system.h"
54
55 #include "ED_screen.h"
56
57 /* the global to talk to ghost */
58 GHOST_SystemHandle g_system= NULL;
59
60 /* set by commandline */
61 static int prefsizx= 0, prefsizy= 0, prefstax= 0, prefstay= 0;
62
63
64 /* ******** win open & close ************ */
65
66
67 static void wm_get_screensize(int *width_r, int *height_r) 
68 {
69         unsigned int uiwidth;
70         unsigned int uiheight;
71         
72         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
73         *width_r= uiwidth;
74         *height_r= uiheight;
75 }
76
77 static void wm_ghostwindow_destroy(wmWindow *win) 
78 {
79         
80         if (win->timer) {
81                 GHOST_RemoveTimer(g_system, (GHOST_TimerTaskHandle)win->timer);
82                 win->timer= NULL;
83         }
84         
85         if(win->ghostwin) {
86                 GHOST_DisposeWindow(g_system, win->ghostwin);
87                 win->ghostwin= NULL;
88         }
89 }
90
91 /* including window itself */
92 void wm_window_free(bContext *C, wmWindow *win)
93 {
94         
95         /* update context */
96         if(C) {
97                 if(C->wm->windrawable==win)
98                         C->wm->windrawable= NULL;
99                 if(C->wm->winactive==win)
100                         C->wm->winactive= NULL;
101                 if(C->window==win)
102                         C->window= NULL;
103                 if(C->screen==win->screen)
104                         C->screen= NULL;
105         }       
106         /* XXX free screens */
107         
108         if(win->eventstate) MEM_freeN(win->eventstate);
109         
110         wm_event_free_handlers(&win->handlers);
111         wm_event_free_all(win);
112         wm_subwindows_free(win);
113         
114         wm_ghostwindow_destroy(win);
115         
116         MEM_freeN(win);
117 }
118
119 static int find_free_winid(wmWindowManager *wm)
120 {
121         wmWindow *win;
122         int id= 0;
123         
124         for(win= wm->windows.first; win; win= win->next)
125                 if(id <= win->winid)
126                         id= win->winid+1;
127         
128         return id;
129 }
130
131 /* dont change context itself */
132 wmWindow *wm_window_new(bContext *C, bScreen *screen)
133 {
134         wmWindow *win= MEM_callocN(sizeof(wmWindow), "window");
135         
136         BLI_addtail(&C->wm->windows, win);
137         win->winid= find_free_winid(C->wm);
138
139         win->screen= screen;
140         return win;
141 }
142
143 /* part of wm_window.c api */
144 wmWindow *wm_window_copy(bContext *C, wmWindow *winorig)
145 {
146         wmWindow *win= wm_window_new(C, winorig->screen);
147         
148         win->posx= winorig->posx+10;
149         win->posy= winorig->posy;
150         win->sizex= winorig->sizex;
151         win->sizey= winorig->sizey;
152         
153         win->screen= ED_screen_duplicate(win, win->screen);
154         win->screen->do_refresh= 1;
155         win->screen->do_draw= 1;
156         
157         return win;
158 }
159
160 /* operator callback */
161 int wm_window_duplicate_op(bContext *C, wmOperator *op)
162 {
163         
164         wm_window_copy(C, C->window);
165         wm_check(C);
166         
167         return 1;
168 }
169
170 /* fullscreen operator callback */
171 int wm_window_fullscreen_toggle_op(bContext *C, wmOperator *op)
172 {
173         GHOST_TWindowState state = GHOST_GetWindowState(C->window->ghostwin);
174         if(state!=GHOST_kWindowStateFullScreen)
175                 GHOST_SetWindowState(C->window->ghostwin, GHOST_kWindowStateFullScreen);
176         else
177                 GHOST_SetWindowState(C->window->ghostwin, GHOST_kWindowStateNormal);
178
179         return 1;
180         
181 }
182
183 /* this is event from ghost */
184 static void wm_window_close(bContext *C, wmWindow *win)
185 {
186         BLI_remlink(&C->wm->windows, win);
187         wm_window_free(C, win);
188         
189         if(C->wm->windows.first==NULL)
190                 WM_exit(C);
191 }
192         
193 /* exit blender */
194 int wm_exit_blender_op(bContext *C, wmOperator *op)
195 {
196         wmWindow *win= C->wm->windows.first;
197         while(win) {
198                 wm_window_close(C, win);
199                 win= win->next;
200         }
201
202         return 1;
203 }
204
205 static void wm_window_open(wmWindowManager *wm, char *title, wmWindow *win)
206 {
207         GHOST_WindowHandle ghostwin;
208         GHOST_TWindowState inital_state;
209         int scr_w, scr_h, posy;
210         
211         wm_get_screensize(&scr_w, &scr_h);
212         posy= (scr_h - win->posy - win->sizey);
213         
214 //              inital_state = GHOST_kWindowStateFullScreen;
215 //              inital_state = GHOST_kWindowStateMaximized;
216                 inital_state = GHOST_kWindowStateNormal;
217
218 #ifdef __APPLE__
219         {
220                 extern int macPrefState; /* creator.c */
221                 inital_state += macPrefState;
222         }
223 #endif
224         
225         ghostwin= GHOST_CreateWindow(g_system, title, 
226                                                                  win->posx, posy, win->sizex, win->sizey, 
227                                                                  inital_state, 
228                                                                  GHOST_kDrawingContextTypeOpenGL,
229                                                                  0 /* no stereo */);
230         
231         if (ghostwin) {
232
233                 win->ghostwin= ghostwin;
234                 GHOST_SetWindowUserData(ghostwin, win); /* pointer back */
235                 
236                 if(win->eventstate==NULL)
237                         win->eventstate= MEM_callocN(sizeof(wmEvent), "window event state");
238                 
239                 /* add keymap handlers (1 for all keys in map!) */
240                 WM_event_add_keymap_handler(&wm->windowkeymap, &win->handlers);
241                 WM_event_add_keymap_handler(&wm->screenkeymap, &win->handlers);
242                 
243                 /* until screens get drawn, make it nice grey */
244                 glClearColor(.55, .55, .55, 0.0);
245                 glClear(GL_COLOR_BUFFER_BIT);
246                 wm_window_swap_buffers(win);
247
248                 /* standard state vars for window */
249                 glEnable(GL_SCISSOR_TEST);
250         }
251 }
252
253 /* for wmWindows without ghostwin, open these and clear */
254 void wm_window_add_ghostwindows(wmWindowManager *wm)
255 {
256         wmWindow *win;
257         
258         /* no commandline prefsize? then we set this */
259         if (!prefsizx) {
260                 wm_get_screensize(&prefsizx, &prefsizy);
261                 
262 #ifdef __APPLE__
263                 {
264                         extern void wm_set_apple_prefsize(int, int);    /* wm_apple.c */
265                 
266                         wm_set_apple_prefsize(prefsizx, prefsizy);
267                 }
268 #else
269                 prefstax= 0;
270                 prefstay= 0;
271                 
272 #endif
273         }
274         
275         for(win= wm->windows.first; win; win= win->next) {
276                 if(win->ghostwin==NULL) {
277                         if(win->sizex==0) {
278                                 win->posx= prefstax;
279                                 win->posy= prefstay;
280                                 win->sizex= prefsizx;
281                                 win->sizey= prefsizy;
282                                 win->windowstate= 0;
283                         }
284                         wm_window_open(wm, "Blender", win);
285                 }
286         }
287 }
288
289 /* ************ events *************** */
290
291 static int query_qual(char qual) 
292 {
293         GHOST_TModifierKeyMask left, right;
294         int val= 0;
295         
296         if (qual=='s') {
297                 left= GHOST_kModifierKeyLeftShift;
298                 right= GHOST_kModifierKeyRightShift;
299         } else if (qual=='c') {
300                 left= GHOST_kModifierKeyLeftControl;
301                 right= GHOST_kModifierKeyRightControl;
302         } else if (qual=='C') {
303                 left= right= GHOST_kModifierKeyCommand;
304         } else {
305                 left= GHOST_kModifierKeyLeftAlt;
306                 right= GHOST_kModifierKeyRightAlt;
307         }
308         
309         GHOST_GetModifierKeyState(g_system, left, &val);
310         if (!val)
311                 GHOST_GetModifierKeyState(g_system, right, &val);
312         
313         return val;
314 }
315
316 void wm_window_make_drawable(bContext *C, wmWindow *win) 
317 {
318         if (win != C->window && win->ghostwin) {
319 //              win->lmbut= 0;  /* keeps hanging when mousepressed while other window opened */
320                 
321                 C->wm->windrawable= win;
322                 C->window= win;
323                 C->screen= win->screen;
324                 printf("set drawable %d\n", win->winid);
325                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
326         }
327 }
328
329 /* called by ghost, here we handle events for windows themselves or send to event system */
330 static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) 
331 {
332         bContext *C= private;
333         GHOST_TEventType type= GHOST_GetEventType(evt);
334         
335         if (type == GHOST_kEventQuit) {
336                 WM_exit(C);
337         } else {
338                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
339                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
340                 wmWindow *win;
341                 
342                 if (!ghostwin) {
343                         // XXX - should be checked, why are we getting an event here, and
344                         //      what is it?
345                         
346                         return 1;
347                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
348                         // XXX - should be checked, why are we getting an event here, and
349                         //      what is it?
350                         
351                         return 1;
352                 } else {
353                         win= GHOST_GetWindowUserData(ghostwin);
354                 }
355                 
356                 switch(type) {
357                         case GHOST_kEventWindowDeactivate:
358                                 win->active= 0; /* XXX */
359                                 break;
360                         case GHOST_kEventWindowActivate: 
361                         {
362                                 GHOST_TEventKeyData kdata;
363                                 int cx, cy, wx, wy;
364                                 
365                                 C->wm->winactive= win; /* no context change! c->window is drawable, or for area queues */
366                                 
367                                 win->active= 1;
368 //                              window_handle(win, INPUTCHANGE, win->active);
369                                 
370                                 /* bad ghost support for modifier keys... so on activate we set the modifiers again */
371                                 kdata.ascii= 0;
372                                 if (win->eventstate->shift && !query_qual('s')) {
373                                         kdata.key= GHOST_kKeyLeftShift;
374                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
375                                 }
376                                 if (win->eventstate->ctrl && !query_qual('c')) {
377                                         kdata.key= GHOST_kKeyLeftControl;
378                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
379                                 }
380                                 if (win->eventstate->alt && !query_qual('a')) {
381                                         kdata.key= GHOST_kKeyLeftAlt;
382                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
383                                 }
384                                 if (win->eventstate->oskey && !query_qual('C')) {
385                                         kdata.key= GHOST_kKeyCommand;
386                                         wm_event_add_ghostevent(win, GHOST_kEventKeyUp, &kdata);
387                                 }
388                                 
389                                 /* entering window, update mouse pos. but no event */
390                                 GHOST_GetCursorPosition(g_system, &wx, &wy);
391                                 
392                                 GHOST_ScreenToClient(win->ghostwin, wx, wy, &cx, &cy);
393                                 win->eventstate->x= cx;
394                                 win->eventstate->y= (win->sizey-1) - cy;
395                                 
396                                 ED_screen_set_subwinactive(win);        /* active subwindow in screen */
397                                 
398                                 wm_window_make_drawable(C, win);
399                                 break;
400                         }
401                         case GHOST_kEventWindowClose: {
402                                 wm_window_close(C, win);
403                                 break;
404                         }
405                         case GHOST_kEventWindowUpdate: {
406                                 printf("ghost redraw\n");
407                                 
408                                 wm_window_make_drawable(C, win);
409                                 WM_event_add_notifier(C->wm, win, 0, WM_NOTE_WINDOW_REDRAW, 0);
410
411                                 break;
412                         }
413                         case GHOST_kEventWindowSize:
414                         case GHOST_kEventWindowMove: {
415                                 GHOST_RectangleHandle client_rect;
416                                 int l, t, r, b, scr_w, scr_h;
417                                 
418                                 client_rect= GHOST_GetClientBounds(win->ghostwin);
419                                 GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
420                                 
421                                 GHOST_DisposeRectangle(client_rect);
422                                 
423                                 wm_get_screensize(&scr_w, &scr_h);
424                                 win->sizex= r-l;
425                                 win->sizey= b-t;
426                                 win->posx= l;
427                                 win->posy= scr_h - t - win->sizey;
428
429                                 /* debug prints */
430                                 if(0) {
431                                         GHOST_TWindowState state;
432                                         state = GHOST_GetWindowState(win->ghostwin);
433
434                                         if(state==GHOST_kWindowStateNormal)
435                                                 printf("window state: normal\n");
436                                         else if(state==GHOST_kWindowStateMinimized)
437                                                 printf("window state: minimized\n");
438                                         else if(state==GHOST_kWindowStateMaximized)
439                                                 printf("window state: maximized\n");
440                                         else if(state==GHOST_kWindowStateFullScreen)
441                                                 printf("window state: fullscreen\n");
442                                         
443                                         if(type!=GHOST_kEventWindowSize)
444                                                 printf("win move event pos %d %d size %d %d\n", win->posx, win->posy, win->sizex, win->sizey);
445                                         
446                                 }
447                                 
448                                 wm_window_make_drawable(C, win);
449                                 WM_event_add_notifier(C->wm, win, 0, WM_NOTE_SCREEN_CHANGED, 0);
450                                 
451                                 break;
452                         }
453                         default:
454                                 if(type==GHOST_kEventKeyDown) // XXX debug
455                                         WM_event_add_notifier(C->wm, win, 0, WM_NOTE_WINDOW_REDRAW, 0);
456                                 wm_event_add_ghostevent(win, type, data);
457                                 break;
458                 }
459
460         }
461         return 1;
462 }
463
464 void wm_window_process_events(int wait_for_event) 
465 {
466         GHOST_ProcessEvents(g_system, wait_for_event);
467         GHOST_DispatchEvents(g_system);
468 }
469
470 /* **************** init ********************** */
471
472 void wm_ghost_init(bContext *C)
473 {
474         if (!g_system) {
475                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(ghost_event_proc, C);
476                 
477                 g_system= GHOST_CreateSystem();
478                 GHOST_AddEventConsumer(g_system, consumer);
479         }       
480 }
481
482 /* **************** timer ********************** */
483
484 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
485 {
486         wmWindow *win= GHOST_GetTimerTaskUserData(timer);
487         
488         wm_event_add_ghostevent(win, win->timer_event, NULL);
489 }
490
491 void wm_window_set_timer(wmWindow *win, int delay_ms, int event)
492 {
493         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
494         
495         win->timer_event= event;
496         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
497 }
498
499 /* ************************************ */
500
501 void wm_window_set_title(wmWindow *win, char *title) 
502 {
503         GHOST_SetTitle(win->ghostwin, title);
504 }
505
506 void wm_window_get_position(wmWindow *win, int *posx_r, int *posy_r) 
507 {
508         *posx_r= win->posx;
509         *posy_r= win->posy;
510 }
511
512 void wm_window_get_size(wmWindow *win, int *width_r, int *height_r) 
513 {
514         *width_r= win->sizex;
515         *height_r= win->sizey;
516 }
517
518 void wm_window_set_size(wmWindow *win, int width, int height) 
519 {
520         GHOST_SetClientSize(win->ghostwin, width, height);
521 }
522
523 void wm_window_lower(wmWindow *win) 
524 {
525         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
526 }
527
528 void wm_window_raise(wmWindow *win) 
529 {
530         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
531 #ifdef _WIN32
532 //      markdirty_all(); /* to avoid redraw errors in fullscreen mode (aphex) */
533 #endif
534 }
535
536 void wm_window_swap_buffers(wmWindow *win)
537 {
538         GHOST_SwapWindowBuffers(win->ghostwin);
539 }
540
541 /* ******************* exported api ***************** */
542
543
544 /* called whem no ghost system was initialized */
545 void WM_setprefsize(int stax, int stay, int sizx, int sizy)
546 {
547         prefstax= stax;
548         prefstay= stay;
549         prefsizx= sizx;
550         prefsizy= sizy;
551 }
552