added iff, lbm and bmp filetypes to the fileselect. added loading of bmp images,
[blender.git] / source / blender / src / ghostwinlay.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef WIN32
40 #include "BLI_winstuff.h"
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46
47 #include "GHOST_C-api.h"
48
49 #include "BKE_utildefines.h"
50 #include "BKE_global.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_graphics.h"
54 #include "BIF_mywindow.h"
55 #include "BIF_screen.h"
56 #include "BIF_usiblender.h"
57
58 #include "mydevice.h"
59 #include "blendef.h"
60
61 #include "winlay.h"
62
63 static GHOST_SystemHandle g_system= 0;
64
65         /* Some simple ghost <-> blender conversions */
66         
67 static GHOST_TStandardCursor convert_cursor(int curs) {
68         switch(curs) {
69         default:
70         case CURSOR_STD:                return GHOST_kStandardCursorDefault;
71         case CURSOR_VPAINT:             return GHOST_kStandardCursorLeftArrow;
72         case CURSOR_FACESEL:    return GHOST_kStandardCursorRightArrow;
73         case CURSOR_WAIT:               return GHOST_kStandardCursorWait;
74         case CURSOR_EDIT:               return GHOST_kStandardCursorCrosshair;
75         case CURSOR_HELP:               return GHOST_kStandardCursorHelp;
76         case CURSOR_X_MOVE:             return GHOST_kStandardCursorLeftRight;
77         case CURSOR_Y_MOVE:             return GHOST_kStandardCursorUpDown;
78         }
79 }
80
81 static int convert_mbut(GHOST_TButtonMask but) {
82         if (but == GHOST_kButtonMaskLeft) {
83                 return LEFTMOUSE;
84         } else if (but == GHOST_kButtonMaskRight) {
85                 return RIGHTMOUSE;
86         } else {
87                 return MIDDLEMOUSE;
88         }
89 }
90
91 static int convert_key(GHOST_TKey key) {
92         if (key>=GHOST_kKeyA && key<=GHOST_kKeyZ) {
93                 return (AKEY + ((int) key - GHOST_kKeyA));
94         } else if (key>=GHOST_kKey0 && key<=GHOST_kKey9) {
95                 return (ZEROKEY + ((int) key - GHOST_kKey0));
96         } else if (key>=GHOST_kKeyNumpad0 && key<=GHOST_kKeyNumpad9) {
97                 return (PAD0 + ((int) key - GHOST_kKeyNumpad0));
98         } else if (key>=GHOST_kKeyF1 && key<=GHOST_kKeyF12) {
99                 return (F1KEY + ((int) key - GHOST_kKeyF1));
100         } else {
101                 switch (key) {
102                 case GHOST_kKeyBackSpace:               return BACKSPACEKEY;
103                 case GHOST_kKeyTab:                             return TABKEY;
104                 case GHOST_kKeyLinefeed:                return LINEFEEDKEY;
105                 case GHOST_kKeyClear:                   return 0;
106                 case GHOST_kKeyEnter:                   return RETKEY;
107         
108                 case GHOST_kKeyEsc:                             return ESCKEY;
109                 case GHOST_kKeySpace:                   return SPACEKEY;
110                 case GHOST_kKeyQuote:                   return QUOTEKEY;
111                 case GHOST_kKeyComma:                   return COMMAKEY;
112                 case GHOST_kKeyMinus:                   return MINUSKEY;
113                 case GHOST_kKeyPeriod:                  return PERIODKEY;
114                 case GHOST_kKeySlash:                   return SLASHKEY;
115
116                 case GHOST_kKeySemicolon:               return SEMICOLONKEY;
117                 case GHOST_kKeyEqual:                   return EQUALKEY;
118
119                 case GHOST_kKeyLeftBracket:             return LEFTBRACKETKEY;
120                 case GHOST_kKeyRightBracket:    return RIGHTBRACKETKEY;
121                 case GHOST_kKeyBackslash:               return BACKSLASHKEY;
122                 case GHOST_kKeyAccentGrave:             return ACCENTGRAVEKEY;
123
124                 case GHOST_kKeyLeftShift:               return LEFTSHIFTKEY;
125                 case GHOST_kKeyRightShift:              return RIGHTSHIFTKEY;
126                 case GHOST_kKeyLeftControl:             return LEFTCTRLKEY;
127                 case GHOST_kKeyRightControl:    return RIGHTCTRLKEY;
128                 case GHOST_kKeyLeftAlt:                 return LEFTALTKEY;
129                 case GHOST_kKeyRightAlt:                return RIGHTALTKEY;
130
131                 case GHOST_kKeyCapsLock:                return CAPSLOCKKEY;
132                 case GHOST_kKeyNumLock:                 return 0;
133                 case GHOST_kKeyScrollLock:              return 0;
134
135                 case GHOST_kKeyLeftArrow:               return LEFTARROWKEY;
136                 case GHOST_kKeyRightArrow:              return RIGHTARROWKEY;
137                 case GHOST_kKeyUpArrow:                 return UPARROWKEY;
138                 case GHOST_kKeyDownArrow:               return DOWNARROWKEY;
139
140                 case GHOST_kKeyPrintScreen:             return 0;
141                 case GHOST_kKeyPause:                   return PAUSEKEY;
142
143                 case GHOST_kKeyInsert:                  return INSERTKEY;
144                 case GHOST_kKeyDelete:                  return DELKEY;
145                 case GHOST_kKeyHome:                    return HOMEKEY;
146                 case GHOST_kKeyEnd:                             return ENDKEY;
147                 case GHOST_kKeyUpPage:                  return PAGEUPKEY;
148                 case GHOST_kKeyDownPage:                return PAGEDOWNKEY;
149
150                 case GHOST_kKeyNumpadPeriod:    return PADPERIOD;
151                 case GHOST_kKeyNumpadEnter:             return PADENTER;
152                 case GHOST_kKeyNumpadPlus:              return PADPLUSKEY;
153                 case GHOST_kKeyNumpadMinus:             return PADMINUS;
154                 case GHOST_kKeyNumpadAsterisk:  return PADASTERKEY;
155                 case GHOST_kKeyNumpadSlash:             return PADSLASHKEY;
156                 case GHOST_kKeyUnknown:                 return UNKNOWNKEY;
157
158                 default:
159                         return 0;
160                 }
161         }
162 }
163
164         /***/
165         
166 struct _Window {
167         GHOST_WindowHandle      ghostwin;
168         
169                 /* Handler and private data for handler */
170         WindowHandlerFP         handler;
171         void                            *user_data;
172         
173                 /* Window state */
174         int             size[2], position[2];
175         int             active, visible;
176         
177                 /* Last known mouse/button/qualifier state */
178         int             lmouse[2];
179         int             lqual;          /* (LR_SHFTKEY, LR_CTRLKEY, LR_ALTKEY) */
180         int             lmbut;          /* (L_MOUSE, M_MOUSE, R_MOUSE) */
181         int             commandqual;
182
183                 /* Tracks the faked mouse button, if non-zero it is
184                  * the event number of the last faked button.
185                  */
186         int             faked_mbut;
187
188         GHOST_TimerTaskHandle   timer;
189         int                                             timer_event;
190 };
191
192 static Window *window_new(GHOST_WindowHandle ghostwin)
193 {
194         Window *win= MEM_callocN(sizeof(*win), "Window");
195         win->ghostwin= ghostwin;
196         
197         return win;
198 }
199
200 static void window_handle(Window *win, short event, short val)
201 {
202         if (win->handler) {
203                 win->handler(win, win->user_data, event, val, 0);
204         }
205 }
206
207 static void window_handle_ext(Window *win, short event, short val, short extra)
208 {
209         if (win->handler) {
210                 win->handler(win, win->user_data, event, val, extra);
211         }
212 }
213
214 static void window_free(Window *win) 
215 {
216         MEM_freeN(win);
217 }
218
219         /***/
220
221 static Window *active_gl_window= NULL;
222
223 Window *window_open(char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
224 {
225         GHOST_WindowHandle ghostwin;
226         GHOST_TWindowState inital_state;
227         int scr_w, scr_h;
228
229         winlay_get_screensize(&scr_w, &scr_h);
230         posy= (scr_h-posy-sizey);
231         
232         /* create a fullscreen window on unix by default*/
233 #if !defined(WIN32) && !defined(__APPLE__)
234         inital_state= start_maximized?
235                 GHOST_kWindowStateFullScreen:GHOST_kWindowStateNormal;
236 #else
237         inital_state= start_maximized?
238                 GHOST_kWindowStateMaximized:GHOST_kWindowStateNormal;
239 #endif
240
241         ghostwin= GHOST_CreateWindow(g_system, 
242                                                                 title, 
243                                                                 posx, posy, sizex, sizey, 
244                                                                 inital_state, 
245                                                                 GHOST_kDrawingContextTypeOpenGL,
246                                                                 0 /* no stereo */);
247         
248         if (ghostwin) {
249                 Window *win= window_new(ghostwin);
250                 
251                 if (win) {
252                         GHOST_SetWindowUserData(ghostwin, win);
253                         
254                         win->position[0]= posx;
255                         win->position[1]= posy;
256                         win->size[0]= sizex;
257                         win->size[1]= sizey;
258                         
259                         win->lmouse[0]= win->size[0]/2;
260                         win->lmouse[1]= win->size[1]/2;
261                 } else {
262                         GHOST_DisposeWindow(g_system, ghostwin);
263                 }
264                 
265                 return win;
266         } else {
267                 return NULL;
268         }
269 }
270
271 void window_set_handler(Window *win, WindowHandlerFP handler, void *user_data)
272 {
273         win->handler= handler;
274         win->user_data= user_data;
275 }
276
277 static void window_timer_proc(GHOST_TimerTaskHandle timer, GHOST_TUns64 time)
278 {
279         Window *win= GHOST_GetTimerTaskUserData(timer);
280
281         win->handler(win, win->user_data, win->timer_event, 0, 0);
282 }
283
284 void window_set_timer(Window *win, int delay_ms, int event)
285 {
286         if (win->timer) GHOST_RemoveTimer(g_system, win->timer);
287
288         win->timer_event= event;
289         win->timer= GHOST_InstallTimer(g_system, delay_ms, delay_ms, window_timer_proc, win);
290 }
291
292 void window_destroy(Window *win) {
293         if (active_gl_window==win) {
294                 active_gl_window= NULL;
295         }
296         
297         if (win->timer) {
298                 GHOST_RemoveTimer(g_system, win->timer);
299                 win->timer= NULL;
300         }
301
302         GHOST_DisposeWindow(g_system, win->ghostwin);
303         window_free(win);
304 }
305
306 void window_set_cursor(Window *win, int curs) {
307         if (curs==CURSOR_NONE) {
308                 GHOST_SetCursorVisibility(win->ghostwin, 0);
309         } else {
310                 GHOST_SetCursorVisibility(win->ghostwin, 1);
311                 GHOST_SetCursorShape(win->ghostwin, convert_cursor(curs));
312         }
313 }
314
315 void window_set_custom_cursor(Window *win, unsigned char mask[16][2], unsigned char bitmap[16][2]) {
316         GHOST_SetCustomCursorShape(win->ghostwin, bitmap, mask, 7, 7);
317 }
318
319 void window_make_active(Window *win) {
320         if (win != active_gl_window) {
321                 active_gl_window= win;
322                 GHOST_ActivateWindowDrawingContext(win->ghostwin);
323         }
324 }
325
326 void window_swap_buffers(Window *win) {
327         GHOST_SwapWindowBuffers(win->ghostwin);
328 }
329
330 static int query_qual(char qual) {
331         GHOST_TModifierKeyMask left, right;
332         int val= 0;
333         
334         if (qual=='s') {
335                 left= GHOST_kModifierKeyLeftShift;
336                 right= GHOST_kModifierKeyRightShift;
337         } else if (qual=='c') {
338                 left= GHOST_kModifierKeyLeftControl;
339                 right= GHOST_kModifierKeyRightControl;
340         } else if (qual=='C') {
341                 left= right= GHOST_kModifierKeyCommand;
342         } else {
343                 left= GHOST_kModifierKeyLeftAlt;
344                 right= GHOST_kModifierKeyRightAlt;
345         }
346
347         GHOST_GetModifierKeyState(g_system, left, &val);
348         if (!val)
349                 GHOST_GetModifierKeyState(g_system, right, &val);
350         
351         return val;
352 }
353
354 static int change_bit(int val, int bit, int to_on) {
355         return to_on?(val|bit):(val&~bit);
356 }
357
358 static int event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr private) {
359         GHOST_TEventType type= GHOST_GetEventType(evt);
360
361         if (type == GHOST_kEventQuit) {
362                 exit_usiblender();
363         } else {
364                 GHOST_WindowHandle ghostwin= GHOST_GetEventWindow(evt);
365                 GHOST_TEventDataPtr data= GHOST_GetEventData(evt);
366                 Window *win;
367                 
368                 if (!ghostwin) {
369                         printf("GHOST event error - no window - type: %d\n", type);
370                         return 1;
371                 } else if (!GHOST_ValidWindow(g_system, ghostwin)) {
372                         printf("GHOST event error - invalid window - win: %p\n", ghostwin);
373                         return 1;
374                 } else {
375                         win= GHOST_GetWindowUserData(ghostwin);
376                 }
377                 
378                 switch (type) {
379                 case GHOST_kEventCursorMove: {
380                         GHOST_TEventCursorData *cd= data;
381                         int cx, cy;
382                         
383                         GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
384                         win->lmouse[0]= cx;
385                         win->lmouse[1]= (win->size[1]-1) - cy;
386
387                         window_handle(win, MOUSEX, win->lmouse[0]);
388                         window_handle(win, MOUSEY, win->lmouse[1]);
389                         
390                         break;
391                 }
392                 case GHOST_kEventButtonDown:
393                 case GHOST_kEventButtonUp: {
394                         GHOST_TEventButtonData *bd= data;
395                         int val= (type==GHOST_kEventButtonDown);
396                         int bbut= convert_mbut(bd->button);
397                 
398                         if (bbut==LEFTMOUSE) {
399                                 if (val) {
400                                         if (win->commandqual) {
401                                                 bbut= win->faked_mbut= RIGHTMOUSE;
402                                         } else if (win->lqual & LR_ALTKEY) {
403                                                 bbut= win->faked_mbut= MIDDLEMOUSE;
404                                         }
405                                 } else {
406                                         if (win->faked_mbut) {
407                                                 bbut= win->faked_mbut;
408                                                 win->faked_mbut= 0;
409                                         }
410                                 }
411                         }
412
413                         if (bbut==LEFTMOUSE) {
414                                 win->lmbut= change_bit(win->lmbut, L_MOUSE, val);
415                         } else if (bbut==MIDDLEMOUSE) {
416                                 win->lmbut= change_bit(win->lmbut, M_MOUSE, val);
417                         } else {
418                                 win->lmbut= change_bit(win->lmbut, R_MOUSE, val);
419                         }
420                         window_handle(win, bbut, val);
421                         
422                         break;
423                 }
424         
425                 case GHOST_kEventKeyDown:
426                 case GHOST_kEventKeyUp: {
427                         GHOST_TEventKeyData *kd= data;
428                         int val= (type==GHOST_kEventKeyDown);
429                         int bkey= convert_key(kd->key);
430
431                         if (kd->key == GHOST_kKeyCommand) {
432                                 win->commandqual= val;
433                         }
434
435                         if (bkey) {
436                                 if (bkey==LEFTSHIFTKEY || bkey==RIGHTSHIFTKEY) {
437                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, val);
438                                 } else if (bkey==LEFTCTRLKEY || bkey==RIGHTCTRLKEY) {
439                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, val);
440                                 } else if (bkey==LEFTALTKEY || bkey==RIGHTALTKEY) {
441                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, val);
442                                 }
443
444                                 window_handle_ext(win, bkey, val, kd->ascii);
445                         }
446                         
447                         break;
448                 }
449
450                 case GHOST_kEventWheel: {
451                         GHOST_TEventWheelData* wheelData = (GHOST_TEventWheelData*) data;
452                         if (wheelData->z > 0) {
453                                 window_handle(win, WHEELUPMOUSE, 1);
454                         } else {
455                                 window_handle(win, WHEELDOWNMOUSE, 1);
456                         }
457                         break;
458                 }
459
460                 case GHOST_kEventWindowDeactivate:
461                 case GHOST_kEventWindowActivate: {
462                         win->active= (type==GHOST_kEventWindowActivate);
463                         window_handle(win, INPUTCHANGE, win->active);
464                         
465                         if (win->active) {
466                                 if ((win->lqual & LR_SHIFTKEY) && !query_qual('s')) {
467                                         win->lqual= change_bit(win->lqual, LR_SHIFTKEY, 0);
468                                         window_handle(win, LEFTSHIFTKEY, 0);
469                                 }
470                                 if ((win->lqual & LR_CTRLKEY) && !query_qual('c')) {
471                                         win->lqual= change_bit(win->lqual, LR_CTRLKEY, 0);
472                                         window_handle(win, LEFTCTRLKEY, 0);
473                                 }
474                                 if ((win->lqual & LR_ALTKEY) && !query_qual('a')) {
475                                         win->lqual= change_bit(win->lqual, LR_ALTKEY, 0);
476                                         window_handle(win, LEFTALTKEY, 0);
477                                 }
478                                 win->commandqual= query_qual('C');
479
480                                 /* 
481                                  * XXX quick hack so OSX version works better
482                                  * when the window is clicked on (focused). 
483                                  */
484                                 window_handle(win, MOUSEX, win->lmouse[0]);
485                                 window_handle(win, MOUSEY, win->lmouse[1]);
486                         }
487                         
488                         break;
489                 }
490                 case GHOST_kEventWindowClose: {
491                         window_handle(win, WINCLOSE, 1);
492                         break;
493                 }
494                 case GHOST_kEventWindowUpdate: {
495                         window_handle(win, REDRAW, 1);
496                         break;
497                 }
498                 case GHOST_kEventWindowSize: {
499                         GHOST_RectangleHandle client_rect;
500                         int l, t, r, b, scr_w, scr_h;
501
502                         client_rect= GHOST_GetClientBounds(win->ghostwin);
503                         GHOST_GetRectangle(client_rect, &l, &t, &r, &b);
504                         
505                         GHOST_DisposeRectangle(client_rect);
506                         
507                         winlay_get_screensize(&scr_w, &scr_h);
508                         win->position[0]= l;
509                         win->position[1]= scr_h - b - 1;
510                         win->size[0]= r-l;
511                         win->size[1]= b-t;
512
513                         window_handle(win, RESHAPE, 1);
514                         break;
515                 }
516                 }
517         }
518
519         return 1;
520 }
521
522 char *window_get_title(Window *win) {
523         char *title= GHOST_GetTitle(win->ghostwin);
524         char *mem_title= BLI_strdup(title);
525         free(title);
526
527         return mem_title;
528 }
529
530 void window_set_title(Window *win, char *title) {
531         GHOST_SetTitle(win->ghostwin, title);
532 }
533
534 short window_get_qual(Window *win) {
535         return win->lqual;
536 }
537
538 short window_get_mbut(Window *win) {
539         return win->lmbut;
540 }
541
542 void window_get_mouse(Window *win, short *mval) {
543         mval[0]= win->lmouse[0];
544         mval[1]= win->lmouse[1];
545 }
546
547 void window_get_position(Window *win, int *posx_r, int *posy_r) {
548         *posx_r= win->position[0];
549         *posy_r= win->position[1];
550 }
551
552 void window_get_size(Window *win, int *width_r, int *height_r) {
553         *width_r= win->size[0];
554         *height_r= win->size[1];
555 }
556
557 void window_set_size(Window *win, int width, int height) {
558         GHOST_SetClientSize(win->ghostwin, width, height);
559 }
560
561 void window_lower(Window *win) {
562         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderBottom);
563 }
564
565 void window_raise(Window *win) {
566         GHOST_SetWindowOrder(win->ghostwin, GHOST_kWindowOrderTop);
567 }
568
569 void window_warp_pointer(Window *win, int x, int y) {
570         y= win->size[1] - y - 1;
571         GHOST_ClientToScreen(win->ghostwin, x, y, &x, &y);
572         GHOST_SetCursorPosition(g_system, x, y);
573 }
574
575 void window_queue_redraw(Window *win) {
576         GHOST_InvalidateWindow(win->ghostwin);
577 }
578
579 /***/
580
581 void winlay_process_events(int wait_for_event) {
582         GHOST_ProcessEvents(g_system, wait_for_event);
583         GHOST_DispatchEvents(g_system);
584 }
585
586 void winlay_get_screensize(int *width_r, int *height_r) {
587         unsigned int uiwidth;
588         unsigned int uiheight;
589         
590         if (!g_system) {
591                 GHOST_EventConsumerHandle consumer= GHOST_CreateEventConsumer(event_proc, NULL);
592         
593                 g_system= GHOST_CreateSystem();
594                 GHOST_AddEventConsumer(g_system, consumer);
595         }
596         
597         GHOST_GetMainDisplayDimensions(g_system, &uiwidth, &uiheight);
598         *width_r= uiwidth;
599         *height_r= uiheight;
600 }
601
602 Window *winlay_get_active_window(void) {
603         return active_gl_window;
604 }