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