2.5
[blender-staging.git] / source / blender / windowmanager / intern / wm_event_system.c
1 /**
2  * $Id:
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.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "DNA_listBase.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_windowmanager_types.h"
36 #include "DNA_userdef_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "GHOST_C-api.h"
41
42 #include "BLI_blenlib.h"
43
44 #include "BKE_blender.h"
45 #include "BKE_context.h"
46 #include "BKE_idprop.h"
47 #include "BKE_global.h"
48 #include "BKE_object.h"
49 #include "BKE_report.h"
50 #include "BKE_scene.h"
51 #include "BKE_utildefines.h"
52 #include "BKE_pointcache.h"
53
54 #include "ED_fileselect.h"
55 #include "ED_screen.h"
56 #include "ED_space_api.h"
57 #include "ED_util.h"
58
59 #include "RNA_access.h"
60
61 #include "UI_interface.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65 #include "wm.h"
66 #include "wm_window.h"
67 #include "wm_event_system.h"
68 #include "wm_event_types.h"
69
70 /* ************ event management ************** */
71
72 void wm_event_add(wmWindow *win, wmEvent *event_to_add)
73 {
74         wmEvent *event= MEM_callocN(sizeof(wmEvent), "event");
75         
76         *event= *event_to_add;
77         BLI_addtail(&win->queue, event);
78 }
79
80 void wm_event_free(wmEvent *event)
81 {
82         if(event->customdata && event->customdatafree)
83                 MEM_freeN(event->customdata);
84         MEM_freeN(event);
85 }
86
87 void wm_event_free_all(wmWindow *win)
88 {
89         wmEvent *event;
90         
91         while((event= win->queue.first)) {
92                 BLI_remlink(&win->queue, event);
93                 wm_event_free(event);
94         }
95 }
96
97 /* ********************* notifiers, listeners *************** */
98
99 /* XXX: in future, which notifiers to send to other windows? */
100 void WM_event_add_notifier(const bContext *C, unsigned int type, void *reference)
101 {
102         wmNotifier *note= MEM_callocN(sizeof(wmNotifier), "notifier");
103         
104         note->wm= CTX_wm_manager(C);
105         BLI_addtail(&note->wm->queue, note);
106         
107         note->window= CTX_wm_window(C);
108         
109         if(CTX_wm_region(C))
110                 note->swinid= CTX_wm_region(C)->swinid;
111         
112         note->category= type & NOTE_CATEGORY;
113         note->data= type & NOTE_DATA;
114         note->subtype= type & NOTE_SUBTYPE;
115         note->action= type & NOTE_ACTION;
116         
117         note->reference= reference;
118 }
119
120 static wmNotifier *wm_notifier_next(wmWindowManager *wm)
121 {
122         wmNotifier *note= wm->queue.first;
123         
124         if(note) BLI_remlink(&wm->queue, note);
125         return note;
126 }
127
128 /* called in mainloop */
129 void wm_event_do_notifiers(bContext *C)
130 {
131         wmWindowManager *wm= CTX_wm_manager(C);
132         wmNotifier *note;
133         wmWindow *win;
134         
135         if(wm==NULL)
136                 return;
137         
138         /* cache & catch WM level notifiers, such as frame change, scene/screen set */
139         for(win= wm->windows.first; win; win= win->next) {
140                 int do_anim= 0;
141                 
142                 CTX_wm_window_set(C, win);
143                 
144                 for(note= wm->queue.first; note; note= note->next) {
145                         if(note->category==NC_WM) {
146                                 if( ELEM(note->data, ND_FILEREAD, ND_FILESAVE)) {
147                                         wm->file_saved= 1;
148                                         wm_window_title(wm, win);
149                                 }
150                                 else if(note->data==ND_DATACHANGED)
151                                         wm_window_title(wm, win);
152                         }
153                         if(note->window==win) {
154                                 if(note->category==NC_SCREEN) {
155                                         if(note->data==ND_SCREENBROWSE) {
156                                                 ED_screen_set(C, note->reference);      // XXX hrms, think this over!
157                                                 printf("screen set %p\n", note->reference);
158                                         }
159                                 }
160                                 else if(note->category==NC_SCENE) {
161                                         if(note->data==ND_SCENEBROWSE) {
162                                                 ED_screen_set_scene(C, note->reference);        // XXX hrms, think this over!
163                                                 printf("scene set %p\n", note->reference);
164                                         }
165                                         else if(note->data==ND_FRAME)
166                                                 do_anim= 1;
167                                 }
168                         }
169                 }
170                 if(do_anim) {
171                         /* depsgraph gets called, might send more notifiers */
172                         ED_update_for_newframe(C, 1);
173                 }
174         }
175         
176         /* the notifiers are sent without context, to keep it clean */
177         while( (note=wm_notifier_next(wm)) ) {
178                 wmWindow *win;
179                 
180                 for(win= wm->windows.first; win; win= win->next) {
181                         
182                         /* filter out notifiers */
183                         if(note->category==NC_SCREEN && note->reference && note->reference!=win->screen);
184                         else if(note->category==NC_SCENE && note->reference && note->reference!=win->screen->scene);
185                         else {
186                                 ScrArea *sa;
187                                 ARegion *ar;
188
189                                 /* XXX context in notifiers? */
190                                 CTX_wm_window_set(C, win);
191
192                                 /* printf("notifier win %d screen %s cat %x\n", win->winid, win->screen->id.name+2, note->category); */
193                                 ED_screen_do_listen(win, note);
194
195                                 for(ar=win->screen->regionbase.first; ar; ar= ar->next) {
196                                         ED_region_do_listen(ar, note);
197                                 }
198                                 
199                                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
200                                         ED_area_do_listen(sa, note);
201                                         for(ar=sa->regionbase.first; ar; ar= ar->next) {
202                                                 ED_region_do_listen(ar, note);
203                                         }
204                                 }
205                         }
206                 }
207                 
208                 MEM_freeN(note);
209         }
210         
211         /* cached: editor refresh callbacks now, they get context */
212         for(win= wm->windows.first; win; win= win->next) {
213                 Scene *sce, *scene= win->screen->scene;
214                 ScrArea *sa;
215                 Base *base;
216                 
217                 CTX_wm_window_set(C, win);
218                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
219                         if(sa->do_refresh) {
220                                 CTX_wm_area_set(C, sa);
221                                 ED_area_do_refresh(C, sa);
222                         }
223                 }
224                 
225                 if(G.rendering==0) { // XXX make lock in future, or separated derivedmesh users in scene
226                         
227                         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, 
228                                 no layer check here, gets correct flushed */
229                         /* sets first, we allow per definition current scene to have dependencies on sets */
230                         if(scene->set) {
231                                 for(SETLOOPER(scene->set, base))
232                                         object_handle_update(scene, base->object);
233                         }
234                         
235                         for(base= scene->base.first; base; base= base->next) {
236                                 object_handle_update(scene, base->object);
237                         }
238
239                         BKE_ptcache_quick_cache_all(scene);
240                 }               
241         }
242         CTX_wm_window_set(C, NULL);
243 }
244
245 /* ********************* operators ******************* */
246
247 /* if repeat is true, it doesn't register again, nor does it free */
248 static int wm_operator_exec(bContext *C, wmOperator *op, int repeat)
249 {
250         int retval= OPERATOR_CANCELLED;
251         
252         if(op==NULL || op->type==NULL)
253                 return retval;
254         
255         if(op->type->poll && op->type->poll(C)==0)
256                 return retval;
257         
258         if(op->type->exec)
259                 retval= op->type->exec(C, op);
260         
261         if(!(retval & OPERATOR_RUNNING_MODAL))
262                 if(op->reports->list.first)
263                         uiPupMenuReports(C, op->reports);
264         
265         if(retval & OPERATOR_FINISHED) {
266                 if(op->type->flag & OPTYPE_UNDO)
267                         ED_undo_push_op(C, op);
268                 
269                 if(repeat==0) {
270                         if((op->type->flag & OPTYPE_REGISTER) || (G.f & G_DEBUG))
271                                 wm_operator_register(C, op);
272                         else
273                                 WM_operator_free(op);
274                 }
275         }
276         else if(repeat==0)
277                 WM_operator_free(op);
278         
279         return retval;
280         
281 }
282
283 /* for running operators with frozen context (modal handlers, menus) */
284 int WM_operator_call(bContext *C, wmOperator *op)
285 {
286         return wm_operator_exec(C, op, 0);
287 }
288
289 /* do this operator again, put here so it can share above code */
290 int WM_operator_repeat(bContext *C, wmOperator *op)
291 {
292         return wm_operator_exec(C, op, 1);
293 }
294
295 static wmOperator *wm_operator_create(wmWindowManager *wm, wmOperatorType *ot, PointerRNA *properties, ReportList *reports)
296 {
297         wmOperator *op= MEM_callocN(sizeof(wmOperator), ot->idname);    /* XXX operatortype names are static still. for debug */
298         
299         /* XXX adding new operator could be function, only happens here now */
300         op->type= ot;
301         BLI_strncpy(op->idname, ot->idname, OP_MAX_TYPENAME);
302         
303         /* initialize properties, either copy or create */
304         op->ptr= MEM_callocN(sizeof(PointerRNA), "wmOperatorPtrRNA");
305         if(properties && properties->data) {
306                 op->properties= IDP_CopyProperty(properties->data);
307         }
308         else {
309                 IDPropertyTemplate val = {0};
310                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
311         }
312         RNA_pointer_create(&wm->id, ot->srna, op->properties, op->ptr);
313
314         /* initialize error reports */
315         if (reports) {
316                 op->reports= reports; /* must be initialized alredy */
317         }
318         else {
319                 op->reports= MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
320                 BKE_reports_init(op->reports, RPT_STORE);
321         }
322         
323         return op;
324 }
325
326 static void wm_operator_print(wmOperator *op)
327 {
328         char *buf = WM_operator_pystring(op);
329         printf("%s\n", buf);
330         MEM_freeN(buf);
331 }
332
333 static void wm_region_mouse_co(bContext *C, wmEvent *event)
334 {
335         ARegion *ar= CTX_wm_region(C);
336         if(ar) {
337                 /* compatibility convention */
338                 event->mval[0]= event->x - ar->winrct.xmin;
339                 event->mval[1]= event->y - ar->winrct.ymin;
340         }
341 }
342
343 static int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, PointerRNA *properties)
344 {
345         wmWindowManager *wm= CTX_wm_manager(C);
346         int retval= OPERATOR_PASS_THROUGH;
347
348         if(ot->poll==NULL || ot->poll(C)) {
349                 wmOperator *op= wm_operator_create(wm, ot, properties, NULL);
350                 
351                 if((G.f & G_DEBUG) && event && event->type!=MOUSEMOVE)
352                         printf("handle evt %d win %d op %s\n", event?event->type:0, CTX_wm_screen(C)->subwinactive, ot->idname); 
353                 
354                 if(op->type->invoke && event) {
355                         wm_region_mouse_co(C, event);
356                         retval= op->type->invoke(C, op, event);
357                 }
358                 else if(op->type->exec)
359                         retval= op->type->exec(C, op);
360                 else
361                         printf("invalid operator call %s\n", ot->idname); /* debug, important to leave a while, should never happen */
362
363                 if(!(retval & OPERATOR_RUNNING_MODAL)) {
364                         if(op->reports->list.first) /* only show the report if the report list was not given in the function */
365                                 uiPupMenuReports(C, op->reports);
366                 
367                 if (retval & OPERATOR_FINISHED) /* todo - this may conflict with the other wm_operator_print, if theres ever 2 prints for 1 action will may need to add modal check here */
368                         if(G.f & G_DEBUG)
369                                 wm_operator_print(op);
370                 }
371
372                 if(retval & OPERATOR_FINISHED) {
373                         if(ot->flag & OPTYPE_UNDO)
374                                 ED_undo_push_op(C, op);
375                         
376                         if((ot->flag & OPTYPE_REGISTER) || (G.f & G_DEBUG))
377                                 wm_operator_register(C, op);
378                         else
379                                 WM_operator_free(op);
380                 }
381                 else if(retval & OPERATOR_RUNNING_MODAL) {
382                         /* grab cursor during blocking modal ops (X11) */
383                         if(ot->flag & OPTYPE_BLOCKING)
384                                 WM_cursor_grab(CTX_wm_window(C), 1);
385                 }
386                 else
387                         WM_operator_free(op);
388         }
389
390         return retval;
391 }
392
393 /* invokes operator in context */
394 int WM_operator_name_call(bContext *C, const char *opstring, int context, PointerRNA *properties)
395 {
396         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
397         wmWindow *window= CTX_wm_window(C);
398         wmEvent *event;
399         
400         int retval;
401
402         /* dummie test */
403         if(ot && C && window) {
404                 event= window->eventstate;
405                 switch(context) {
406                         
407                         case WM_OP_EXEC_REGION_WIN:
408                                 event= NULL;    /* pass on without break */
409                         case WM_OP_INVOKE_REGION_WIN: 
410                         {
411                                 /* forces operator to go to the region window, for header menus */
412                                 ARegion *ar= CTX_wm_region(C);
413                                 ScrArea *area= CTX_wm_area(C);
414                                 
415                                 if(area) {
416                                         ARegion *ar1= area->regionbase.first;
417                                         for(; ar1; ar1= ar1->next)
418                                                 if(ar1->regiontype==RGN_TYPE_WINDOW)
419                                                         break;
420                                         if(ar1)
421                                                 CTX_wm_region_set(C, ar1);
422                                 }
423                                 
424                                 retval= wm_operator_invoke(C, ot, event, properties);
425                                 
426                                 /* set region back */
427                                 CTX_wm_region_set(C, ar);
428                                 
429                                 return retval;
430                         }
431                         case WM_OP_EXEC_AREA:
432                                 event= NULL;    /* pass on without break */
433                         case WM_OP_INVOKE_AREA:
434                         {
435                                         /* remove region from context */
436                                 ARegion *ar= CTX_wm_region(C);
437
438                                 CTX_wm_region_set(C, NULL);
439                                 retval= wm_operator_invoke(C, ot, event, properties);
440                                 CTX_wm_region_set(C, ar);
441
442                                 return retval;
443                         }
444                         case WM_OP_EXEC_SCREEN:
445                                 event= NULL;    /* pass on without break */
446                         case WM_OP_INVOKE_SCREEN:
447                         {
448                                 /* remove region + area from context */
449                                 ARegion *ar= CTX_wm_region(C);
450                                 ScrArea *area= CTX_wm_area(C);
451
452                                 CTX_wm_region_set(C, NULL);
453                                 CTX_wm_area_set(C, NULL);
454                                 retval= wm_operator_invoke(C, ot, event, properties);
455                                 CTX_wm_region_set(C, ar);
456                                 CTX_wm_area_set(C, area);
457
458                                 return retval;
459                         }
460                         case WM_OP_EXEC_DEFAULT:
461                                 event= NULL;    /* pass on without break */
462                         case WM_OP_INVOKE_DEFAULT:
463                                 return wm_operator_invoke(C, ot, event, properties);
464                 }
465         }
466         
467         return 0;
468 }
469
470 /* Similar to WM_operator_name_call called with WM_OP_EXEC_DEFAULT context.
471    - wmOperatorType is used instead of operator name since python alredy has the operator type
472    - poll() must be called by python before this runs.
473    - reports can be passed to this function (so python can report them as exceptions)
474 */
475 int WM_operator_call_py(bContext *C, wmOperatorType *ot, PointerRNA *properties, ReportList *reports)
476 {
477         wmWindowManager *wm=    CTX_wm_manager(C);
478         wmOperator *op=                 wm_operator_create(wm, ot, properties, reports);
479         int retval= OPERATOR_CANCELLED;
480         
481         if (op->type->exec)
482                 retval= op->type->exec(C, op);
483         else
484                 printf("error \"%s\" operator has no exec function, python cannot call it\n", op->type->name);
485         
486         if (reports)
487                 op->reports= NULL; /* dont let the operator free reports passed to this function */
488         WM_operator_free(op);
489         
490         return retval;
491 }
492
493
494 /* ********************* handlers *************** */
495
496 /* future extra customadata free? */
497 static void wm_event_free_handler(wmEventHandler *handler)
498 {
499         MEM_freeN(handler);
500 }
501
502 /* only set context when area/region is part of screen */
503 static void wm_handler_op_context(bContext *C, wmEventHandler *handler)
504 {
505         bScreen *screen= CTX_wm_screen(C);
506         
507         if(screen && handler->op) {
508                 if(handler->op_area==NULL)
509                         CTX_wm_area_set(C, NULL);
510                 else {
511                         ScrArea *sa;
512                         
513                         for(sa= screen->areabase.first; sa; sa= sa->next)
514                                 if(sa==handler->op_area)
515                                         break;
516                         if(sa==NULL) {
517                                 /* when changing screen layouts with running modal handlers (like render display), this
518                                    is not an error to print */
519                                 if(handler->op==NULL)
520                                         printf("internal error: handler (%s) has invalid area\n", handler->op->type->idname);
521                         }
522                         else {
523                                 ARegion *ar;
524                                 CTX_wm_area_set(C, sa);
525                                 for(ar= sa->regionbase.first; ar; ar= ar->next)
526                                         if(ar==handler->op_region)
527                                                 break;
528                                 /* XXX no warning print here, after full-area and back regions are remade */
529                                 if(ar)
530                                         CTX_wm_region_set(C, ar);
531                         }
532                 }
533         }
534 }
535
536 /* called on exit or remove area, only here call cancel callback */
537 void WM_event_remove_handlers(bContext *C, ListBase *handlers)
538 {
539         wmEventHandler *handler;
540         
541         /* C is zero on freeing database, modal handlers then already were freed */
542         while((handler=handlers->first)) {
543                 BLI_remlink(handlers, handler);
544                 
545                 if(handler->op) {
546                         if(handler->op->type->cancel) {
547                                 ScrArea *area= CTX_wm_area(C);
548                                 ARegion *region= CTX_wm_region(C);
549                                 
550                                 wm_handler_op_context(C, handler);
551
552                                 handler->op->type->cancel(C, handler->op);
553
554                                 CTX_wm_area_set(C, area);
555                                 CTX_wm_region_set(C, region);
556                         }
557
558                         WM_operator_free(handler->op);
559                         WM_cursor_grab(CTX_wm_window(C), 0);
560                 }
561                 else if(handler->ui_remove) {
562                         ScrArea *area= CTX_wm_area(C);
563                         ARegion *region= CTX_wm_region(C);
564                         ARegion *menu= CTX_wm_menu(C);
565                         
566                         if(handler->ui_area) CTX_wm_area_set(C, handler->ui_area);
567                         if(handler->ui_region) CTX_wm_region_set(C, handler->ui_region);
568                         if(handler->ui_menu) CTX_wm_menu_set(C, handler->ui_menu);
569
570                         handler->ui_remove(C, handler->ui_userdata);
571
572                         CTX_wm_area_set(C, area);
573                         CTX_wm_region_set(C, region);
574                         CTX_wm_menu_set(C, menu);
575                 }
576
577                 wm_event_free_handler(handler);
578         }
579 }
580
581 /* do userdef mappings */
582 static int wm_userdef_event_map(int kmitype)
583 {
584         switch(kmitype) {
585                 case SELECTMOUSE:
586                         if(U.flag & USER_LMOUSESELECT)
587                                 return LEFTMOUSE;
588                         else
589                                 return RIGHTMOUSE;
590                         
591                 case ACTIONMOUSE:
592                         if(U.flag & USER_LMOUSESELECT)
593                                 return RIGHTMOUSE;
594                         else
595                                 return LEFTMOUSE;
596                         
597                 case WHEELOUTMOUSE:
598                         if(U.uiflag & USER_WHEELZOOMDIR)
599                                 return WHEELUPMOUSE;
600                         else
601                                 return WHEELDOWNMOUSE;
602                         
603                 case WHEELINMOUSE:
604                         if(U.uiflag & USER_WHEELZOOMDIR)
605                                 return WHEELDOWNMOUSE;
606                         else
607                                 return WHEELUPMOUSE;
608                         
609                 case EVT_TWEAK_A:
610                         if(U.flag & USER_LMOUSESELECT)
611                                 return EVT_TWEAK_R;
612                         else
613                                 return EVT_TWEAK_L;
614                         
615                 case EVT_TWEAK_S:
616                         if(U.flag & USER_LMOUSESELECT)
617                                 return EVT_TWEAK_L;
618                         else
619                                 return EVT_TWEAK_R;
620         }
621         
622         return kmitype;
623 }
624
625 static int wm_eventmatch(wmEvent *winevent, wmKeymapItem *kmi)
626 {
627         int kmitype= wm_userdef_event_map(kmi->type);
628
629         if(kmi->inactive) return 0;
630         
631         /* the matching rules */
632         if(kmitype==KM_TEXTINPUT)
633                 if(ISKEYBOARD(winevent->type)) return 1;
634         if(kmitype!=KM_ANY)
635                 if(winevent->type!=kmitype) return 0;
636         
637         if(kmi->val!=KM_ANY)
638                 if(winevent->val!=kmi->val) return 0;
639         
640         /* modifiers also check bits, so it allows modifier order */
641         if(kmi->shift!=KM_ANY)
642                 if(winevent->shift != kmi->shift && !(winevent->shift & kmi->shift)) return 0;
643         if(kmi->ctrl!=KM_ANY)
644                 if(winevent->ctrl != kmi->ctrl && !(winevent->ctrl & kmi->ctrl)) return 0;
645         if(kmi->alt!=KM_ANY)
646                 if(winevent->alt != kmi->alt && !(winevent->alt & kmi->alt)) return 0;
647         if(kmi->oskey!=KM_ANY)
648                 if(winevent->oskey != kmi->oskey && !(winevent->oskey & kmi->oskey)) return 0;
649         if(kmi->keymodifier)
650                 if(winevent->keymodifier!=kmi->keymodifier) return 0;
651         
652         return 1;
653 }
654
655 static int wm_event_always_pass(wmEvent *event)
656 {
657         /* some events we always pass on, to ensure proper communication */
658         return ELEM5(event->type, TIMER, TIMER0, TIMER1, TIMER2, TIMERJOBS);
659 }
660
661 /* operator exists */
662 static void wm_event_modalkeymap(wmOperator *op, wmEvent *event)
663 {
664         if(op->type->modalkeymap) {
665                 wmKeymapItem *kmi;
666                 
667                 for(kmi= op->type->modalkeymap->keymap.first; kmi; kmi= kmi->next) {
668                         if(wm_eventmatch(event, kmi)) {
669                                         
670                                 event->type= EVT_MODAL_MAP;
671                                 event->val= kmi->propvalue;
672                         }
673                 }
674         }
675 }
676
677 /* Warning: this function removes a modal handler, when finished */
678 static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHandler *handler, wmEvent *event, PointerRNA *properties)
679 {
680         int retval= OPERATOR_PASS_THROUGH;
681         
682         /* derived, modal or blocking operator */
683         if(handler->op) {
684                 wmOperator *op= handler->op;
685                 wmOperatorType *ot= op->type;
686
687                 if(ot->modal) {
688                         /* we set context to where modal handler came from */
689                         ScrArea *area= CTX_wm_area(C);
690                         ARegion *region= CTX_wm_region(C);
691                         
692                         wm_handler_op_context(C, handler);
693                         wm_region_mouse_co(C, event);
694                         wm_event_modalkeymap(op, event);
695                         
696                         retval= ot->modal(C, op, event);
697
698                         /* putting back screen context, reval can pass trough after modal failures! */
699                         if((retval & OPERATOR_PASS_THROUGH) || wm_event_always_pass(event)) {
700                                 CTX_wm_area_set(C, area);
701                                 CTX_wm_region_set(C, region);
702                         }
703                         else {
704                                 /* this special cases is for areas and regions that get removed */
705                                 CTX_wm_area_set(C, NULL);
706                                 CTX_wm_region_set(C, NULL);
707                         }
708
709                         if(!(retval & OPERATOR_RUNNING_MODAL))
710                                 if(op->reports->list.first)
711                                         uiPupMenuReports(C, op->reports);
712
713                         if (retval & OPERATOR_FINISHED) {
714                                 if(G.f & G_DEBUG)
715                                         wm_operator_print(op); /* todo - this print may double up, might want to check more flags then the FINISHED */
716                         }                       
717
718                         if(retval & OPERATOR_FINISHED) {
719                                 if(ot->flag & OPTYPE_UNDO)
720                                         ED_undo_push_op(C, op);
721                                 
722                                 if((ot->flag & OPTYPE_REGISTER) || (G.f & G_DEBUG))
723                                         wm_operator_register(C, op);
724                                 else
725                                         WM_operator_free(op);
726                                 handler->op= NULL;
727                         }
728                         else if(retval & (OPERATOR_CANCELLED|OPERATOR_FINISHED)) {
729                                 WM_operator_free(op);
730                                 handler->op= NULL;
731                         }
732                         
733                         /* remove modal handler, operator itself should have been cancelled and freed */
734                         if(retval & (OPERATOR_CANCELLED|OPERATOR_FINISHED)) {
735                                 WM_cursor_grab(CTX_wm_window(C), 0);
736
737                                 BLI_remlink(handlers, handler);
738                                 wm_event_free_handler(handler);
739                                 
740                                 /* prevent silly errors from operator users */
741                                 //retval &= ~OPERATOR_PASS_THROUGH;
742                         }
743                         
744                 }
745                 else
746                         printf("wm_handler_operator_call error\n");
747         }
748         else {
749                 wmOperatorType *ot= WM_operatortype_find(event->keymap_idname, 0);
750
751                 if(ot)
752                         retval= wm_operator_invoke(C, ot, event, properties);
753         }
754
755         if(retval & OPERATOR_PASS_THROUGH)
756                 return WM_HANDLER_CONTINUE;
757
758         return WM_HANDLER_BREAK;
759 }
760
761 static int wm_handler_ui_call(bContext *C, wmEventHandler *handler, wmEvent *event)
762 {
763         ScrArea *area= CTX_wm_area(C);
764         ARegion *region= CTX_wm_region(C);
765         ARegion *menu= CTX_wm_menu(C);
766         int retval, always_pass;
767                         
768         /* we set context to where ui handler came from */
769         if(handler->ui_area) CTX_wm_area_set(C, handler->ui_area);
770         if(handler->ui_region) CTX_wm_region_set(C, handler->ui_region);
771         if(handler->ui_menu) CTX_wm_menu_set(C, handler->ui_menu);
772
773         /* in advance to avoid access to freed event on window close */
774         always_pass= wm_event_always_pass(event);
775
776         retval= handler->ui_handle(C, event, handler->ui_userdata);
777
778         /* putting back screen context */
779         if((retval != WM_UI_HANDLER_BREAK) || always_pass) {
780                 CTX_wm_area_set(C, area);
781                 CTX_wm_region_set(C, region);
782                 CTX_wm_menu_set(C, menu);
783         }
784         else {
785                 /* this special cases is for areas and regions that get removed */
786                 CTX_wm_area_set(C, NULL);
787                 CTX_wm_region_set(C, NULL);
788                 CTX_wm_menu_set(C, NULL);
789         }
790
791         if(retval == WM_UI_HANDLER_BREAK)
792                 return WM_HANDLER_BREAK;
793
794         return WM_HANDLER_CONTINUE;
795 }
796
797 /* fileselect handlers are only in the window queue, so it's save to switch screens or area types */
798 static int wm_handler_fileselect_call(bContext *C, ListBase *handlers, wmEventHandler *handler, wmEvent *event)
799 {
800         SpaceFile *sfile;
801         int action= WM_HANDLER_CONTINUE;
802         
803         if(event->type != EVT_FILESELECT)
804                 return action;
805         if(handler->op != (wmOperator *)event->customdata)
806                 return action;
807         
808         switch(event->val) {
809                 case EVT_FILESELECT_OPEN: 
810                 case EVT_FILESELECT_FULL_OPEN: 
811                         {
812                                 short flag =0; short display =FILE_SHORTDISPLAY; short filter =0; short sort =FILE_SORT_ALPHA;
813                                 char *dir= NULL; char *path= RNA_string_get_alloc(handler->op->ptr, "filename", NULL, 0);
814                                         
815                                 if(event->val==EVT_FILESELECT_OPEN)
816                                         ED_area_newspace(C, handler->op_area, SPACE_FILE);
817                                 else
818                                         ED_screen_full_newspace(C, handler->op_area, SPACE_FILE);
819                                 
820                                 /* settings for filebrowser, sfile is not operator owner but sends events */
821                                 sfile= (SpaceFile*)CTX_wm_space_data(C);
822                                 sfile->op= handler->op;
823                                 
824                                 /* XXX for now take the settings from the existing (previous) filebrowser 
825                                    should be stored in settings and passed via the operator */
826                                 if (sfile->params) {
827                                         flag = sfile->params->flag;
828                                         filter = sfile->params->filter;
829                                         display = sfile->params->display;
830                                         sort = sfile->params->sort;
831                                         dir = sfile->params->dir;
832                                 }
833
834                                 ED_fileselect_set_params(sfile, handler->op->type->name, dir, path, flag, display, filter, sort);
835                                 dir = NULL;
836                                 MEM_freeN(path);
837                                 
838                                 action= WM_HANDLER_BREAK;
839                         }
840                         break;
841                         
842                 case EVT_FILESELECT_EXEC:
843                 case EVT_FILESELECT_CANCEL:
844                         {
845                                 /* XXX validate area and region? */
846                                 bScreen *screen= CTX_wm_screen(C);
847                                 char *path= RNA_string_get_alloc(handler->op->ptr, "filename", NULL, 0);
848                                 
849                                 if(screen != handler->filescreen)
850                                         ED_screen_full_prevspace(C);
851                                 else
852                                         ED_area_prevspace(C);
853                                 
854                                 /* remlink now, for load file case */
855                                 BLI_remlink(handlers, handler);
856                                 
857                                 if(event->val==EVT_FILESELECT_EXEC) {
858                                         wm_handler_op_context(C, handler);
859                                 
860                                         /* a bit weak, might become arg for WM_event_fileselect? */
861                                         /* XXX also extension code in image-save doesnt work for this yet */
862                                         if(strncmp(handler->op->type->name, "Save", 4)==0) {
863                                                 /* this gives ownership to pupmenu */
864                                                 uiPupMenuSaveOver(C, handler->op, path);
865                                         }
866                                         else {
867                                                 int retval= handler->op->type->exec(C, handler->op);
868                                                 
869                                                 if (retval & OPERATOR_FINISHED)
870                                                         if(G.f & G_DEBUG)
871                                                                 wm_operator_print(handler->op);
872                                                 
873                                                 WM_operator_free(handler->op);
874                                         }
875                                         
876                                         CTX_wm_area_set(C, NULL);
877                                 }
878                                 else 
879                                         WM_operator_free(handler->op);
880                                 
881                                 wm_event_free_handler(handler);
882                                 MEM_freeN(path);
883                                 
884                                 action= WM_HANDLER_BREAK;
885                         }
886                         break;
887         }
888         
889         return action;
890 }
891
892 static int handler_boundbox_test(wmEventHandler *handler, wmEvent *event)
893 {
894         if(handler->bbwin) {
895                 if(handler->bblocal) {
896                         rcti rect= *handler->bblocal;
897                         BLI_translate_rcti(&rect, handler->bbwin->xmin, handler->bbwin->ymin);
898                         return BLI_in_rcti(&rect, event->x, event->y);
899                 }
900                 else 
901                         return BLI_in_rcti(handler->bbwin, event->x, event->y);
902         }
903         return 1;
904 }
905
906 static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers)
907 {
908         wmEventHandler *handler, *nexthandler;
909         int action= WM_HANDLER_CONTINUE;
910         int always_pass;
911
912         if(handlers==NULL) return action;
913         
914         /* modal handlers can get removed in this loop, we keep the loop this way */
915         for(handler= handlers->first; handler; handler= nexthandler) {
916                 nexthandler= handler->next;
917
918                 /* optional boundbox */
919                 if(handler_boundbox_test(handler, event)) {
920                         /* in advance to avoid access to freed event on window close */
921                         always_pass= wm_event_always_pass(event);
922                 
923                         /* modal+blocking handler */
924                         if(handler->flag & WM_HANDLER_BLOCKING)
925                                 action= WM_HANDLER_BREAK;
926
927                         if(handler->keymap) {
928                                 wmKeymapItem *kmi;
929                                 
930                                 for(kmi= handler->keymap->first; kmi; kmi= kmi->next) {
931                                         if(wm_eventmatch(event, kmi)) {
932                                                 
933                                                 event->keymap_idname= kmi->idname;      /* weak, but allows interactive callback to not use rawkey */
934                                                 
935                                                 action= wm_handler_operator_call(C, handlers, handler, event, kmi->ptr);
936                                                 if(action==WM_HANDLER_BREAK)  /* not wm_event_always_pass(event) here, it denotes removed handler */
937                                                         break;
938                                         }
939                                 }
940                         }
941                         else if(handler->ui_handle) {
942                                 action= wm_handler_ui_call(C, handler, event);
943                         }
944                         else if(handler->type==WM_HANDLER_FILESELECT) {
945                                 /* screen context changes here */
946                                 action= wm_handler_fileselect_call(C, handlers, handler, event);
947                         }
948                         else {
949                                 /* modal, swallows all */
950                                 action= wm_handler_operator_call(C, handlers, handler, event, NULL);
951                         }
952
953                         if(!always_pass && action==WM_HANDLER_BREAK)
954                                 break;
955                 }
956                 
957                 /* fileread case */
958                 if(CTX_wm_window(C)==NULL)
959                         break;
960         }
961         return action;
962 }
963
964 static int wm_event_inside_i(wmEvent *event, rcti *rect)
965 {
966         if(BLI_in_rcti(rect, event->x, event->y))
967            return 1;
968         if(event->type==MOUSEMOVE) {
969                 if( BLI_in_rcti(rect, event->prevx, event->prevy)) {
970                         return 1;
971                 }
972                 return 0;
973         }
974         return 0;
975 }
976
977 static ScrArea *area_event_inside(bContext *C, int x, int y)
978 {
979         bScreen *screen= CTX_wm_screen(C);
980         ScrArea *sa;
981         
982         if(screen)
983                 for(sa= screen->areabase.first; sa; sa= sa->next)
984                         if(BLI_in_rcti(&sa->totrct, x, y))
985                                 return sa;
986         return NULL;
987 }
988
989 static ARegion *region_event_inside(bContext *C, int x, int y)
990 {
991         bScreen *screen= CTX_wm_screen(C);
992         ScrArea *area= CTX_wm_area(C);
993         ARegion *ar;
994         
995         if(screen && area)
996                 for(ar= area->regionbase.first; ar; ar= ar->next)
997                         if(BLI_in_rcti(&ar->winrct, x, y))
998                                 return ar;
999         return NULL;
1000 }
1001
1002 static void wm_paintcursor_tag(bContext *C, wmPaintCursor *pc, ARegion *ar)
1003 {
1004         if(ar) {
1005                 for(; pc; pc= pc->next) {
1006                         if(pc->poll(C)) {
1007                                 wmWindow *win= CTX_wm_window(C);
1008                                 win->screen->do_draw_paintcursor= 1;
1009
1010                                 if(win->drawmethod != USER_DRAW_TRIPLE)
1011                                         ED_region_tag_redraw(ar);
1012                         }
1013                 }
1014         }
1015 }
1016
1017 /* called on mousemove, check updates for paintcursors */
1018 /* context was set on active area and region */
1019 static void wm_paintcursor_test(bContext *C, wmEvent *event)
1020 {
1021         wmWindowManager *wm= CTX_wm_manager(C);
1022         
1023         if(wm->paintcursors.first) {
1024                 ARegion *ar= CTX_wm_region(C);
1025                 if(ar)
1026                         wm_paintcursor_tag(C, wm->paintcursors.first, ar);
1027                 
1028                 /* if previous position was not in current region, we have to set a temp new context */
1029                 if(ar==NULL || !BLI_in_rcti(&ar->winrct, event->prevx, event->prevy)) {
1030                         ScrArea *sa= CTX_wm_area(C);
1031                         
1032                         CTX_wm_area_set(C, area_event_inside(C, event->prevx, event->prevy));
1033                         CTX_wm_region_set(C, region_event_inside(C, event->prevx, event->prevy));
1034
1035                         wm_paintcursor_tag(C, wm->paintcursors.first, CTX_wm_region(C));
1036                         
1037                         CTX_wm_area_set(C, sa);
1038                         CTX_wm_region_set(C, ar);
1039                 }
1040         }
1041 }
1042
1043 /* called in main loop */
1044 /* goes over entire hierarchy:  events -> window -> screen -> area -> region */
1045 void wm_event_do_handlers(bContext *C)
1046 {
1047         wmWindow *win;
1048
1049         for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
1050                 wmEvent *event;
1051                 
1052                 if( win->screen==NULL )
1053                         wm_event_free_all(win);
1054                 
1055                 while( (event= win->queue.first) ) {
1056                         int action;
1057                         
1058                         CTX_wm_window_set(C, win);
1059                         
1060                         /* we let modal handlers get active area/region, also wm_paintcursor_test needs it */
1061                         CTX_wm_area_set(C, area_event_inside(C, event->x, event->y));
1062                         CTX_wm_region_set(C, region_event_inside(C, event->x, event->y));
1063                         
1064                         /* MVC demands to not draw in event handlers... but we need to leave it for ogl selecting etc */
1065                         wm_window_make_drawable(C, win);
1066                         
1067                         action= wm_handlers_do(C, event, &win->handlers);
1068                         
1069                         /* fileread case */
1070                         if(CTX_wm_window(C)==NULL) {
1071                                 return;
1072                         }
1073                         
1074                         /* builtin tweak, if action is break it removes tweak */
1075                         if(!wm_event_always_pass(event))
1076                                 wm_tweakevent_test(C, event, action);
1077                         
1078                         if(wm_event_always_pass(event) || action==WM_HANDLER_CONTINUE) {
1079                                 ScrArea *sa;
1080                                 ARegion *ar;
1081                                 int doit= 0;
1082                                 
1083                                 /* XXX to solve, here screen handlers? */
1084                                 if(!wm_event_always_pass(event)) {
1085                                         if(event->type==MOUSEMOVE) {
1086                                                 /* state variables in screen, cursors */
1087                                                 ED_screen_set_subwinactive(win, event); 
1088                                                 /* for regions having custom cursors */
1089                                                 wm_paintcursor_test(C, event);
1090                                         }
1091                                 }
1092                                 
1093                                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
1094                                         if(wm_event_always_pass(event) || wm_event_inside_i(event, &sa->totrct)) {
1095                                                 
1096                                                 CTX_wm_area_set(C, sa);
1097                                                 CTX_wm_region_set(C, NULL);
1098                                                 action= wm_handlers_do(C, event, &sa->handlers);
1099
1100                                                 if(wm_event_always_pass(event) || action==WM_HANDLER_CONTINUE) {
1101                                                         for(ar=sa->regionbase.first; ar; ar= ar->next) {
1102                                                                 if(wm_event_always_pass(event) || wm_event_inside_i(event, &ar->winrct)) {
1103                                                                         CTX_wm_region_set(C, ar);
1104                                                                         action= wm_handlers_do(C, event, &ar->handlers);
1105
1106                                                                         doit |= (BLI_in_rcti(&ar->winrct, event->x, event->y));
1107                                                                         
1108                                                                         if(!wm_event_always_pass(event)) {
1109                                                                                 if(action==WM_HANDLER_BREAK)
1110                                                                                         break;
1111                                                                         }
1112                                                                 }
1113                                                         }
1114                                                 }
1115                                                 /* NOTE: do not escape on WM_HANDLER_BREAK, mousemove needs handled for previous area */
1116                                         }
1117                                 }
1118                                 
1119                                 /* XXX hrmf, this gives reliable previous mouse coord for area change, feels bad? 
1120                                    doing it on ghost queue gives errors when mousemoves go over area borders */
1121                                 if(doit && win->screen->subwinactive != win->screen->mainwin) {
1122                                         win->eventstate->prevx= event->x;
1123                                         win->eventstate->prevy= event->y;
1124                                 }
1125                         }
1126                         
1127                         /* unlink and free here, blender-quit then frees all */
1128                         BLI_remlink(&win->queue, event);
1129                         wm_event_free(event);
1130                         
1131                 }
1132                 
1133                 /* only add mousemove when queue was read entirely */
1134                 if(win->addmousemove) {
1135                         wmEvent event= *(win->eventstate);
1136                         event.type= MOUSEMOVE;
1137                         event.prevx= event.x;
1138                         event.prevy= event.y;
1139                         wm_event_add(win, &event);
1140                         win->addmousemove= 0;
1141                 }
1142                 
1143                 CTX_wm_window_set(C, NULL);
1144         }
1145 }
1146
1147 /* ********** filesector handling ************ */
1148
1149 void WM_event_fileselect_event(bContext *C, void *ophandle, int eventval)
1150 {
1151         /* add to all windows! */
1152         wmWindow *win;
1153         
1154         for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
1155                 wmEvent event= *win->eventstate;
1156                 
1157                 event.type= EVT_FILESELECT;
1158                 event.val= eventval;
1159                 event.customdata= ophandle;             // only as void pointer type check
1160
1161                 wm_event_add(win, &event);
1162         }
1163 }
1164
1165 /* operator is supposed to have a filled "filename" property */
1166 /* optional property: filetype (XXX enum?) */
1167
1168 /* Idea is to keep a handler alive on window queue, owning the operator.
1169    The filewindow can send event to make it execute, thus ensuring
1170    executing happens outside of lower level queues, with UI refreshed. 
1171    Should also allow multiwin solutions */
1172
1173 void WM_event_add_fileselect(bContext *C, wmOperator *op)
1174 {
1175         wmEventHandler *handler= MEM_callocN(sizeof(wmEventHandler), "fileselect handler");
1176         wmWindow *win= CTX_wm_window(C);
1177         int full= 1;    // XXX preset?
1178         
1179         handler->type= WM_HANDLER_FILESELECT;
1180         handler->op= op;
1181         handler->op_area= CTX_wm_area(C);
1182         handler->op_region= CTX_wm_region(C);
1183         handler->filescreen= CTX_wm_screen(C);
1184         
1185         BLI_addhead(&win->handlers, handler);
1186         
1187         WM_event_fileselect_event(C, op, full?EVT_FILESELECT_FULL_OPEN:EVT_FILESELECT_OPEN);
1188 }
1189
1190 /* lets not expose struct outside wm? */
1191 void WM_event_set_handler_flag(wmEventHandler *handler, int flag)
1192 {
1193         handler->flag= flag;
1194 }
1195
1196 wmEventHandler *WM_event_add_modal_handler(bContext *C, ListBase *handlers, wmOperator *op)
1197 {
1198         wmEventHandler *handler= MEM_callocN(sizeof(wmEventHandler), "event modal handler");
1199         handler->op= op;
1200         handler->op_area= CTX_wm_area(C);               /* means frozen screen context for modal handlers! */
1201         handler->op_region= CTX_wm_region(C);
1202         
1203         BLI_addhead(handlers, handler);
1204
1205         return handler;
1206 }
1207
1208 wmEventHandler *WM_event_add_keymap_handler(ListBase *handlers, ListBase *keymap)
1209 {
1210         wmEventHandler *handler;
1211
1212         /* only allow same keymap once */
1213         for(handler= handlers->first; handler; handler= handler->next)
1214                 if(handler->keymap==keymap)
1215                         return handler;
1216         
1217         handler= MEM_callocN(sizeof(wmEventHandler), "event keymap handler");
1218         BLI_addtail(handlers, handler);
1219         handler->keymap= keymap;
1220
1221         return handler;
1222 }
1223
1224 /* priorities not implemented yet, for time being just insert in begin of list */
1225 wmEventHandler *WM_event_add_keymap_handler_priority(ListBase *handlers, ListBase *keymap, int priority)
1226 {
1227         wmEventHandler *handler;
1228         
1229         WM_event_remove_keymap_handler(handlers, keymap);
1230         
1231         handler= MEM_callocN(sizeof(wmEventHandler), "event keymap handler");
1232         BLI_addhead(handlers, handler);
1233         handler->keymap= keymap;
1234         
1235         return handler;
1236 }
1237
1238 wmEventHandler *WM_event_add_keymap_handler_bb(ListBase *handlers, ListBase *keymap, rcti *bblocal, rcti *bbwin)
1239 {
1240         wmEventHandler *handler= WM_event_add_keymap_handler(handlers, keymap);
1241         
1242         if(handler) {
1243                 handler->bblocal= bblocal;
1244                 handler->bbwin= bbwin;
1245         }
1246         return handler;
1247 }
1248
1249 void WM_event_remove_keymap_handler(ListBase *handlers, ListBase *keymap)
1250 {
1251         wmEventHandler *handler;
1252         
1253         for(handler= handlers->first; handler; handler= handler->next) {
1254                 if(handler->keymap==keymap) {
1255                         BLI_remlink(handlers, handler);
1256                         wm_event_free_handler(handler);
1257                         break;
1258                 }
1259         }
1260 }
1261
1262 wmEventHandler *WM_event_add_ui_handler(const bContext *C, ListBase *handlers, wmUIHandlerFunc func, wmUIHandlerRemoveFunc remove, void *userdata)
1263 {
1264         wmEventHandler *handler= MEM_callocN(sizeof(wmEventHandler), "event ui handler");
1265         handler->ui_handle= func;
1266         handler->ui_remove= remove;
1267         handler->ui_userdata= userdata;
1268         handler->ui_area= (C)? CTX_wm_area(C): NULL;
1269         handler->ui_region= (C)? CTX_wm_region(C): NULL;
1270         handler->ui_menu= (C)? CTX_wm_menu(C): NULL;
1271         
1272         BLI_addhead(handlers, handler);
1273         
1274         return handler;
1275 }
1276
1277 void WM_event_remove_ui_handler(ListBase *handlers, wmUIHandlerFunc func, wmUIHandlerRemoveFunc remove, void *userdata)
1278 {
1279         wmEventHandler *handler;
1280         
1281         for(handler= handlers->first; handler; handler= handler->next) {
1282                 if(handler->ui_handle == func && handler->ui_remove == remove && handler->ui_userdata == userdata) {
1283                         BLI_remlink(handlers, handler);
1284                         wm_event_free_handler(handler);
1285                         break;
1286                 }
1287         }
1288 }
1289
1290 void WM_event_add_mousemove(bContext *C)
1291 {
1292         wmWindow *window= CTX_wm_window(C);
1293         
1294         window->addmousemove= 1;
1295 }
1296
1297 /* for modal callbacks, check configuration for how to interpret exit with tweaks  */
1298 int WM_modal_tweak_exit(wmEvent *evt, int tweak_event)
1299 {
1300         /* user preset or keymap? dunno... */
1301         int tweak_modal= (U.flag & USER_DRAGIMMEDIATE)==0;
1302         
1303         switch(tweak_event) {
1304                 case EVT_TWEAK_L:
1305                 case EVT_TWEAK_M:
1306                 case EVT_TWEAK_R:
1307                         if(evt->val==tweak_modal)
1308                                 return 1;
1309                 default:
1310                         /* this case is when modal callcback didnt get started with a tweak */
1311                         if(evt->val)
1312                                 return 1;
1313         }
1314         return 0;
1315 }
1316
1317
1318 /* ********************* ghost stuff *************** */
1319
1320 static int convert_key(GHOST_TKey key) 
1321 {
1322         if (key>=GHOST_kKeyA && key<=GHOST_kKeyZ) {
1323                 return (AKEY + ((int) key - GHOST_kKeyA));
1324         } else if (key>=GHOST_kKey0 && key<=GHOST_kKey9) {
1325                 return (ZEROKEY + ((int) key - GHOST_kKey0));
1326         } else if (key>=GHOST_kKeyNumpad0 && key<=GHOST_kKeyNumpad9) {
1327                 return (PAD0 + ((int) key - GHOST_kKeyNumpad0));
1328         } else if (key>=GHOST_kKeyF1 && key<=GHOST_kKeyF12) {
1329                 return (F1KEY + ((int) key - GHOST_kKeyF1));
1330         } else {
1331                 switch (key) {
1332                         case GHOST_kKeyBackSpace:               return BACKSPACEKEY;
1333                         case GHOST_kKeyTab:                             return TABKEY;
1334                         case GHOST_kKeyLinefeed:                return LINEFEEDKEY;
1335                         case GHOST_kKeyClear:                   return 0;
1336                         case GHOST_kKeyEnter:                   return RETKEY;
1337                                 
1338                         case GHOST_kKeyEsc:                             return ESCKEY;
1339                         case GHOST_kKeySpace:                   return SPACEKEY;
1340                         case GHOST_kKeyQuote:                   return QUOTEKEY;
1341                         case GHOST_kKeyComma:                   return COMMAKEY;
1342                         case GHOST_kKeyMinus:                   return MINUSKEY;
1343                         case GHOST_kKeyPeriod:                  return PERIODKEY;
1344                         case GHOST_kKeySlash:                   return SLASHKEY;
1345                                 
1346                         case GHOST_kKeySemicolon:               return SEMICOLONKEY;
1347                         case GHOST_kKeyEqual:                   return EQUALKEY;
1348                                 
1349                         case GHOST_kKeyLeftBracket:             return LEFTBRACKETKEY;
1350                         case GHOST_kKeyRightBracket:    return RIGHTBRACKETKEY;
1351                         case GHOST_kKeyBackslash:               return BACKSLASHKEY;
1352                         case GHOST_kKeyAccentGrave:             return ACCENTGRAVEKEY;
1353                                 
1354                         case GHOST_kKeyLeftShift:               return LEFTSHIFTKEY;
1355                         case GHOST_kKeyRightShift:              return RIGHTSHIFTKEY;
1356                         case GHOST_kKeyLeftControl:             return LEFTCTRLKEY;
1357                         case GHOST_kKeyRightControl:    return RIGHTCTRLKEY;
1358                         case GHOST_kKeyCommand:                 return COMMANDKEY;
1359                         case GHOST_kKeyLeftAlt:                 return LEFTALTKEY;
1360                         case GHOST_kKeyRightAlt:                return RIGHTALTKEY;
1361                                 
1362                         case GHOST_kKeyCapsLock:                return CAPSLOCKKEY;
1363                         case GHOST_kKeyNumLock:                 return 0;
1364                         case GHOST_kKeyScrollLock:              return 0;
1365                                 
1366                         case GHOST_kKeyLeftArrow:               return LEFTARROWKEY;
1367                         case GHOST_kKeyRightArrow:              return RIGHTARROWKEY;
1368                         case GHOST_kKeyUpArrow:                 return UPARROWKEY;
1369                         case GHOST_kKeyDownArrow:               return DOWNARROWKEY;
1370                                 
1371                         case GHOST_kKeyPrintScreen:             return 0;
1372                         case GHOST_kKeyPause:                   return PAUSEKEY;
1373                                 
1374                         case GHOST_kKeyInsert:                  return INSERTKEY;
1375                         case GHOST_kKeyDelete:                  return DELKEY;
1376                         case GHOST_kKeyHome:                    return HOMEKEY;
1377                         case GHOST_kKeyEnd:                             return ENDKEY;
1378                         case GHOST_kKeyUpPage:                  return PAGEUPKEY;
1379                         case GHOST_kKeyDownPage:                return PAGEDOWNKEY;
1380                                 
1381                         case GHOST_kKeyNumpadPeriod:    return PADPERIOD;
1382                         case GHOST_kKeyNumpadEnter:             return PADENTER;
1383                         case GHOST_kKeyNumpadPlus:              return PADPLUSKEY;
1384                         case GHOST_kKeyNumpadMinus:             return PADMINUS;
1385                         case GHOST_kKeyNumpadAsterisk:  return PADASTERKEY;
1386                         case GHOST_kKeyNumpadSlash:             return PADSLASHKEY;
1387                                 
1388                         case GHOST_kKeyGrLess:              return GRLESSKEY; 
1389                                 
1390                         default:
1391                                 return UNKNOWNKEY;      /* GHOST_kKeyUnknown */
1392                 }
1393         }
1394 }
1395
1396 /* adds customdata to event */
1397 static void update_tablet_data(wmWindow *win, wmEvent *event)
1398 {
1399         const GHOST_TabletData *td= GHOST_GetTabletData(win->ghostwin);
1400         
1401         /* if there's tablet data from an active tablet device then add it */
1402         if ((td != NULL) && td->Active) {
1403                 struct wmTabletData *wmtab= MEM_mallocN(sizeof(wmTabletData), "customdata tablet");
1404                 
1405                 wmtab->Active = td->Active;
1406                 wmtab->Pressure = td->Pressure;
1407                 wmtab->Xtilt = td->Xtilt;
1408                 wmtab->Ytilt = td->Ytilt;
1409                 
1410                 event->custom= EVT_DATA_TABLET;
1411                 event->customdata= wmtab;
1412                 event->customdatafree= 1;
1413         } 
1414 }
1415
1416
1417 /* windows store own event queues, no bContext here */
1418 void wm_event_add_ghostevent(wmWindow *win, int type, void *customdata)
1419 {
1420         wmEvent event, *evt= win->eventstate;
1421         
1422         /* initialize and copy state (only mouse x y and modifiers) */
1423         event= *evt;
1424         
1425         switch (type) {
1426                 /* mouse move */
1427                 case GHOST_kEventCursorMove: {
1428                         if(win->active) {
1429                                 GHOST_TEventCursorData *cd= customdata;
1430                                 int cx, cy;
1431
1432                                 GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
1433                                 
1434                                 event.type= MOUSEMOVE;
1435                                 event.x= evt->x= cx;
1436                                 event.y= evt->y= (win->sizey-1) - cy;
1437                                 
1438                                 update_tablet_data(win, &event);
1439                                 wm_event_add(win, &event);
1440
1441                                 cx = abs((win->downx - event.x));
1442                                 cy = abs((win->downy - event.y));
1443
1444                                 /* probably minimum drag size should be #defined instead of hardcoded 3 */
1445                                 if (win->downstate == LEFTMOUSE && (cx > 3 || cy > 3)) {
1446                                         wmEvent dragevt= *evt;
1447                                         dragevt.type= MOUSEDRAG;
1448                                         dragevt.customdata= NULL;
1449                                         dragevt.customdatafree= 0;
1450
1451                                         wm_event_add(win, &dragevt);
1452                                 }
1453                         }
1454                         break;
1455                 }
1456                 /* mouse button */
1457                 case GHOST_kEventButtonDown:
1458                 case GHOST_kEventButtonUp: {
1459                         GHOST_TEventButtonData *bd= customdata;
1460                         event.val= (type==GHOST_kEventButtonDown);
1461                         
1462                         if (bd->button == GHOST_kButtonMaskLeft)
1463                                 event.type= LEFTMOUSE;
1464                         else if (bd->button == GHOST_kButtonMaskRight)
1465                                 event.type= RIGHTMOUSE;
1466                         else
1467                                 event.type= MIDDLEMOUSE;
1468                         
1469                         if(event.val)
1470                                 event.keymodifier= evt->keymodifier= event.type;
1471                         else
1472                                 event.keymodifier= evt->keymodifier= 0;
1473                         
1474                         update_tablet_data(win, &event);
1475                         wm_event_add(win, &event);
1476
1477                         if (event.val) {
1478                                 win->downstate= event.type;
1479                                 win->downx= event.x;
1480                                 win->downy= event.y;
1481                         }
1482                         else if (win->downstate) {
1483                                 wmEvent dropevt= *evt;
1484                                 dropevt.type= MOUSEDROP;
1485                                 dropevt.customdata= NULL;
1486                                 dropevt.customdatafree= 0;
1487                                 win->downstate= 0;
1488                                 win->downx= 0;
1489                                 win->downy= 0;
1490
1491                                 wm_event_add(win, &dropevt);
1492                         }
1493                         
1494                         break;
1495                 }
1496                 /* keyboard */
1497                 case GHOST_kEventKeyDown:
1498                 case GHOST_kEventKeyUp: {
1499                         GHOST_TEventKeyData *kd= customdata;
1500                         event.type= convert_key(kd->key);
1501                         event.ascii= kd->ascii;
1502                         event.val= (type==GHOST_kEventKeyDown)?KM_PRESS:KM_RELEASE;
1503                         
1504                         /* exclude arrow keys, esc, etc from text input */
1505                         if(type==GHOST_kEventKeyUp || (event.ascii<32 && event.ascii>14))
1506                                 event.ascii= '\0';
1507                         
1508                         /* modifiers */
1509                         if (event.type==LEFTSHIFTKEY || event.type==RIGHTSHIFTKEY) {
1510                                 event.shift= evt->shift= (event.val==KM_PRESS);
1511                                 if(event.val==KM_PRESS && (evt->ctrl || evt->alt || evt->oskey))
1512                                    event.shift= evt->shift = 3;         // define?
1513                         } 
1514                         else if (event.type==LEFTCTRLKEY || event.type==RIGHTCTRLKEY) {
1515                                 event.ctrl= evt->ctrl= (event.val==KM_PRESS);
1516                                 if(event.val==KM_PRESS && (evt->shift || evt->alt || evt->oskey))
1517                                    event.ctrl= evt->ctrl = 3;           // define?
1518                         } 
1519                         else if (event.type==LEFTALTKEY || event.type==RIGHTALTKEY) {
1520                                 event.alt= evt->alt= (event.val==KM_PRESS);
1521                                 if(event.val==KM_PRESS && (evt->ctrl || evt->shift || evt->oskey))
1522                                    event.alt= evt->alt = 3;             // define?
1523                         } 
1524                         else if (event.type==COMMANDKEY) {
1525                                 event.oskey= evt->oskey= (event.val==KM_PRESS);
1526                                 if(event.val==KM_PRESS && (evt->ctrl || evt->alt || evt->shift))
1527                                    event.oskey= evt->oskey = 3;         // define?
1528                         }
1529                         
1530                         /* if test_break set, it catches this. XXX Keep global for now? */
1531                         if(event.type==ESCKEY)
1532                                 G.afbreek= 1;
1533
1534                         wm_event_add(win, &event);
1535                         
1536                         break;
1537                 }
1538                         
1539                 case GHOST_kEventWheel: {
1540                         GHOST_TEventWheelData* wheelData = customdata;
1541                         
1542                         if (wheelData->z > 0)
1543                                 event.type= WHEELUPMOUSE;
1544                         else
1545                                 event.type= WHEELDOWNMOUSE;
1546                         
1547                         event.val= KM_PRESS;
1548                         wm_event_add(win, &event);
1549                         
1550                         break;
1551                 }
1552                 case GHOST_kEventTimer: {
1553                         event.type= TIMER;
1554                         event.custom= EVT_DATA_TIMER;
1555                         event.customdata= customdata;
1556                         wm_event_add(win, &event);
1557
1558                         break;
1559                 }
1560
1561                 case GHOST_kEventUnknown:
1562                 case GHOST_kNumEventTypes:
1563                         break;
1564         }
1565 }