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