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