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