def9d7351acda14581348a3f9c088d3b091ea159
[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_windowmanager_types.h"
35 #include "DNA_userdef_types.h"  /* U.flag & TWOBUTTONMOUSE */
36
37 #include "MEM_guardedalloc.h"
38
39 #include "GHOST_C-api.h"
40
41 #include "BLI_blenlib.h"
42
43 #include "BKE_blender.h"
44 #include "BKE_context.h"
45 #include "BKE_idprop.h"
46 #include "BKE_global.h"
47 #include "BKE_report.h"
48 #include "BKE_utildefines.h"
49
50 #include "ED_screen.h"
51 #include "ED_space_api.h"
52 #include "ED_util.h"
53
54 #include "RNA_access.h"
55
56 #include "UI_interface.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60 #include "wm.h"
61 #include "wm_window.h"
62 #include "wm_event_system.h"
63 #include "wm_event_types.h"
64
65 /* ************ event management ************** */
66
67 void wm_event_add(wmWindow *win, wmEvent *event_to_add)
68 {
69         wmEvent *event= MEM_callocN(sizeof(wmEvent), "event");
70         
71         *event= *event_to_add;
72         BLI_addtail(&win->queue, event);
73 }
74
75 static void wm_event_free(wmEvent *event)
76 {
77         if(event->customdata && event->customdatafree)
78                 MEM_freeN(event->customdata);
79         MEM_freeN(event);
80 }
81
82 void wm_event_free_all(wmWindow *win)
83 {
84         wmEvent *event;
85         
86         while((event= win->queue.first)) {
87                 BLI_remlink(&win->queue, event);
88                 wm_event_free(event);
89         }
90 }
91
92 /* ********************* notifiers, listeners *************** */
93
94 /* XXX: in future, which notifiers to send to other windows? */
95 void WM_event_add_notifier(bContext *C, unsigned int type, void *reference)
96 {
97         wmNotifier *note= MEM_callocN(sizeof(wmNotifier), "notifier");
98         
99         note->wm= CTX_wm_manager(C);
100         BLI_addtail(&note->wm->queue, note);
101         
102         note->window= CTX_wm_window(C);
103         
104         if(CTX_wm_region(C))
105                 note->swinid= CTX_wm_region(C)->swinid;
106         
107         note->category= type & NOTE_CATEGORY;
108         note->data= type & NOTE_DATA;
109         note->subtype= type & NOTE_SUBTYPE;
110         note->action= type & NOTE_ACTION;
111         
112         note->reference= reference;
113 }
114
115 static wmNotifier *wm_notifier_next(wmWindowManager *wm)
116 {
117         wmNotifier *note= wm->queue.first;
118         
119         if(note) BLI_remlink(&wm->queue, note);
120         return note;
121 }
122
123 /* called in mainloop */
124 void wm_event_do_notifiers(bContext *C)
125 {
126         wmWindowManager *wm= CTX_wm_manager(C);
127         wmNotifier *note;
128         wmWindow *win;
129         
130         if(wm==NULL)
131                 return;
132         
133         /* cache & catch WM level notifiers, such as frame change, scene/screen set */
134         /* XXX todo, multiwindow scenes */
135         for(win= wm->windows.first; win; win= win->next) {
136                 int do_anim= 0;
137                 
138                 CTX_wm_window_set(C, win);
139                 
140                 for(note= wm->queue.first; note; note= note->next) {
141                         if(note->window==win) {
142                                 if(note->category==NC_SCREEN) {
143                                         if(note->data==ND_SCREENBROWSE)
144                                                 ED_screen_set(C, note->reference);      // XXX hrms, think this over!
145                                 }
146                                 else if(note->category==NC_SCENE) {
147                                         if(note->data==ND_FRAME)
148                                                 do_anim= 1;
149                                 }
150                         }
151                 }
152                 if(do_anim) {
153                         /* depsgraph gets called, might send more notifiers */
154                         ED_update_for_newframe(C, 1);
155                 }
156         }
157         
158         /* the notifiers are sent without context, to keep it clean */
159         while( (note=wm_notifier_next(wm)) ) {
160                 wmWindow *win;
161                 
162                 for(win= wm->windows.first; win; win= win->next) {
163                         ScrArea *sa;
164                         ARegion *ar;
165
166                         /* XXX context in notifiers? */
167                         CTX_wm_window_set(C, win);
168
169                         /* printf("notifier win %d screen %s\n", win->winid, win->screen->id.name+2); */
170                         ED_screen_do_listen(win, note);
171
172                         for(ar=win->screen->regionbase.first; ar; ar= ar->next) {
173                                 ED_region_do_listen(ar, note);
174                         }
175                         
176                         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
177                                 ED_area_do_listen(sa, note);
178                                 for(ar=sa->regionbase.first; ar; ar= ar->next) {
179                                         ED_region_do_listen(ar, note);
180                                 }
181                         }
182                 }
183                 
184                 CTX_wm_window_set(C, NULL);
185                 
186                 MEM_freeN(note);
187         }
188         
189         /* cached: editor refresh callbacks now, they get context */
190         for(win= wm->windows.first; win; win= win->next) {
191                 ScrArea *sa;
192                 CTX_wm_window_set(C, win);
193                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
194                         if(sa->do_refresh) {
195                                 CTX_wm_area_set(C, sa);
196                                 ED_area_do_refresh(C, sa);
197                         }
198                 }
199         }
200         CTX_wm_window_set(C, NULL);
201 }
202
203 /* ********************* operators ******************* */
204
205 /* if repeat is true, it doesn't register again, nor does it free */
206 static int wm_operator_exec(bContext *C, wmOperator *op, int repeat)
207 {
208         int retval= OPERATOR_CANCELLED;
209         
210         if(op==NULL || op->type==NULL)
211                 return retval;
212         
213         if(op->type->poll && op->type->poll(C)==0)
214                 return retval;
215         
216         if(op->type->exec)
217                 retval= op->type->exec(C, op);
218         
219         if(!(retval & OPERATOR_RUNNING_MODAL))
220                 if(op->reports->list.first)
221                         uiPupMenuReports(C, op->reports);
222         
223         if(retval & OPERATOR_FINISHED) {
224                 if(op->type->flag & OPTYPE_UNDO)
225                         ED_undo_push_op(C, op);
226                 
227                 if(repeat==0) {
228                         if(op->type->flag & OPTYPE_REGISTER)
229                                 wm_operator_register(CTX_wm_manager(C), op);
230                         else
231                                 WM_operator_free(op);
232                 }
233         }
234         else if(repeat==0)
235                 WM_operator_free(op);
236         
237         return retval;
238         
239 }
240
241 /* for running operators with frozen context (modal handlers, menus) */
242 int WM_operator_call(bContext *C, wmOperator *op)
243 {
244         return wm_operator_exec(C, op, 0);
245 }
246
247 /* do this operator again, put here so it can share above code */
248 int WM_operator_repeat(bContext *C, wmOperator *op)
249 {
250         return wm_operator_exec(C, op, 1);
251 }
252
253 static wmOperator *wm_operator_create(wmWindowManager *wm, wmOperatorType *ot, PointerRNA *properties, ReportList *reports)
254 {
255         wmOperator *op= MEM_callocN(sizeof(wmOperator), ot->idname);    /* XXX operatortype names are static still. for debug */
256         
257         /* XXX adding new operator could be function, only happens here now */
258         op->type= ot;
259         BLI_strncpy(op->idname, ot->idname, OP_MAX_TYPENAME);
260         
261         /* initialize properties, either copy or create */
262         op->ptr= MEM_callocN(sizeof(PointerRNA), "wmOperatorPtrRNA");
263         if(properties && properties->data) {
264                 op->properties= IDP_CopyProperty(properties->data);
265         }
266         else {
267                 IDPropertyTemplate val = {0};
268                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
269         }
270         RNA_pointer_create(&wm->id, ot->srna, op->properties, op->ptr);
271
272         /* initialize error reports */
273         if (reports) {
274                 op->reports= reports; /* must be initialized alredy */
275         }
276         else {
277                 op->reports= MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
278                 BKE_reports_init(op->reports, RPT_STORE);
279         }
280         
281         return op;
282 }
283
284 static void wm_operator_print(wmOperator *op)
285 {
286         char *buf = WM_operator_pystring(op);
287         printf("%s\n", buf);
288         MEM_freeN(buf);
289 }
290
291 static int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, PointerRNA *properties)
292 {
293         wmWindowManager *wm= CTX_wm_manager(C);
294         int retval= OPERATOR_PASS_THROUGH;
295
296         if(ot->poll==NULL || ot->poll(C)) {
297                 wmOperator *op= wm_operator_create(wm, ot, properties, NULL);
298                 
299                 if((G.f & G_DEBUG) && event && event->type!=MOUSEMOVE)
300                         printf("handle evt %d win %d op %s\n", event?event->type:0, CTX_wm_screen(C)->subwinactive, ot->idname); 
301                 
302                 if(op->type->invoke && event)
303                         retval= (*op->type->invoke)(C, op, event);
304                 else if(op->type->exec)
305                         retval= op->type->exec(C, op);
306                 else
307                         printf("invalid operator call %s\n", ot->idname); /* debug, important to leave a while, should never happen */
308
309                 if(!(retval & OPERATOR_RUNNING_MODAL)) {
310                         if(op->reports->list.first) /* only show the report if the report list was not given in the function */
311                                 uiPupMenuReports(C, op->reports);
312                 
313                 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 */
314                         if(G.f & G_DEBUG)
315                                 wm_operator_print(op);
316                 }
317
318                 if(retval & OPERATOR_FINISHED) {
319                         if(ot->flag & OPTYPE_UNDO)
320                                 ED_undo_push_op(C, op);
321                         
322                         if(ot->flag & OPTYPE_REGISTER)
323                                 wm_operator_register(wm, op);
324                         else
325                                 WM_operator_free(op);
326                 }
327                 else if(!(retval & OPERATOR_RUNNING_MODAL)) {
328                         WM_operator_free(op);
329                 }
330         }
331
332         return retval;
333 }
334
335 /* invokes operator in context */
336 int WM_operator_name_call(bContext *C, const char *opstring, int context, PointerRNA *properties)
337 {
338         wmOperatorType *ot= WM_operatortype_find(opstring);
339         wmWindow *window= CTX_wm_window(C);
340         wmEvent *event;
341         
342         int retval;
343
344         /* dummie test */
345         if(ot && C && window) {
346                 event= window->eventstate;
347                 switch(context) {
348                         
349                         case WM_OP_EXEC_REGION_WIN:
350                                 event= NULL;    /* pass on without break */
351                         case WM_OP_INVOKE_REGION_WIN: 
352                         {
353                                 /* forces operator to go to the region window, for header menus */
354                                 ARegion *ar= CTX_wm_region(C);
355                                 ScrArea *area= CTX_wm_area(C);
356                                 
357                                 if(area) {
358                                         ARegion *ar1= area->regionbase.first;
359                                         for(; ar1; ar1= ar1->next)
360                                                 if(ar1->regiontype==RGN_TYPE_WINDOW)
361                                                         break;
362                                         if(ar1)
363                                                 CTX_wm_region_set(C, ar1);
364                                 }
365                                 
366                                 retval= wm_operator_invoke(C, ot, event, properties);
367                                 
368                                 /* set region back */
369                                 CTX_wm_region_set(C, ar);
370                                 
371                                 return retval;
372                         }
373                         case WM_OP_EXEC_AREA:
374                                 event= NULL;    /* pass on without break */
375                         case WM_OP_INVOKE_AREA:
376                         {
377                                         /* remove region from context */
378                                 ARegion *ar= CTX_wm_region(C);
379
380                                 CTX_wm_region_set(C, NULL);
381                                 retval= wm_operator_invoke(C, ot, event, properties);
382                                 CTX_wm_region_set(C, ar);
383
384                                 return retval;
385                         }
386                         case WM_OP_EXEC_SCREEN:
387                                 event= NULL;    /* pass on without break */
388                         case WM_OP_INVOKE_SCREEN:
389                         {
390                                 /* remove region + area from context */
391                                 ARegion *ar= CTX_wm_region(C);
392                                 ScrArea *area= CTX_wm_area(C);
393
394                                 CTX_wm_region_set(C, NULL);
395                                 CTX_wm_area_set(C, NULL);
396                                 retval= wm_operator_invoke(C, ot, window->eventstate, properties);
397                                 CTX_wm_region_set(C, ar);
398                                 CTX_wm_area_set(C, area);
399
400                                 return retval;
401                         }
402                         case WM_OP_EXEC_DEFAULT:
403                                 event= NULL;    /* pass on without break */
404                         case WM_OP_INVOKE_DEFAULT:
405                                 return wm_operator_invoke(C, ot, event, properties);
406                 }
407         }
408         
409         return 0;
410 }
411
412 /* Similar to WM_operator_name_call called with WM_OP_EXEC_DEFAULT context.
413    - wmOperatorType is used instead of operator name since python alredy has the operator type
414    - poll() must be called by python before this runs.
415    - reports can be passed to this function (so python can report them as exceptions)
416 */
417 int WM_operator_call_py(bContext *C, wmOperatorType *ot, PointerRNA *properties, ReportList *reports)
418 {
419         wmWindowManager *wm=    CTX_wm_manager(C);
420         wmOperator *op=                 wm_operator_create(wm, ot, properties, reports);
421         int retval=                             op->type->exec(C, op);
422         
423         if (reports)
424                 op->reports= NULL; /* dont let the operator free reports passed to this function */
425         WM_operator_free(op);
426         
427         return retval;
428 }
429
430
431 /* ********************* handlers *************** */
432
433 /* not handler itself, is called by UI to move handlers to other queues, so don't close modal ones */
434 static void wm_event_free_handler(wmEventHandler *handler)
435 {
436         
437 }
438
439 /* called on exit or remove area, only here call cancel callback */
440 void WM_event_remove_handlers(bContext *C, ListBase *handlers)
441 {
442         wmEventHandler *handler;
443         
444         /* C is zero on freeing database, modal handlers then already were freed */
445         while((handler=handlers->first)) {
446                 BLI_remlink(handlers, handler);
447                 
448                 if(handler->op) {
449                         if(handler->op->type->cancel) {
450                                 ScrArea *area= CTX_wm_area(C);
451                                 ARegion *region= CTX_wm_region(C);
452                                 
453                                 CTX_wm_area_set(C, handler->op_area);
454                                 CTX_wm_region_set(C, handler->op_region);
455
456                                 handler->op->type->cancel(C, handler->op);
457
458                                 CTX_wm_area_set(C, area);
459                                 CTX_wm_region_set(C, region);
460                         }
461
462                         WM_operator_free(handler->op);
463                 }
464                 else if(handler->ui_remove) {
465                         ScrArea *area= CTX_wm_area(C);
466                         ARegion *region= CTX_wm_region(C);
467                         
468                         if(handler->ui_area) CTX_wm_area_set(C, handler->ui_area);
469                         if(handler->ui_region) CTX_wm_region_set(C, handler->ui_region);
470
471                         handler->ui_remove(C, handler->ui_userdata);
472
473                         CTX_wm_area_set(C, area);
474                         CTX_wm_region_set(C, region);
475                 }
476
477                 wm_event_free_handler(handler);
478                 MEM_freeN(handler);
479         }
480 }
481
482 /* do userdef mappings */
483 static int wm_userdef_event_map(int kmitype)
484 {
485         switch(kmitype) {
486                 case SELECTMOUSE:
487                         if(U.flag & USER_LMOUSESELECT)
488                                 return LEFTMOUSE;
489                         else
490                                 return RIGHTMOUSE;
491                         
492                 case ACTIONMOUSE:
493                         if(U.flag & USER_LMOUSESELECT)
494                                 return RIGHTMOUSE;
495                         else
496                                 return LEFTMOUSE;
497                         
498                 case WHEELOUTMOUSE:
499                         if(U.uiflag & USER_WHEELZOOMDIR)
500                                 return WHEELUPMOUSE;
501                         else
502                                 return WHEELDOWNMOUSE;
503                         
504                 case WHEELINMOUSE:
505                         if(U.uiflag & USER_WHEELZOOMDIR)
506                                 return WHEELDOWNMOUSE;
507                         else
508                                 return WHEELUPMOUSE;
509                         
510                 case EVT_TWEAK_A:
511                         if(U.flag & USER_LMOUSESELECT)
512                                 return EVT_TWEAK_R;
513                         else
514                                 return EVT_TWEAK_L;
515                         
516                 case EVT_TWEAK_S:
517                         if(U.flag & USER_LMOUSESELECT)
518                                 return EVT_TWEAK_L;
519                         else
520                                 return EVT_TWEAK_R;
521         }
522         
523         return kmitype;
524 }
525
526 static int wm_eventmatch(wmEvent *winevent, wmKeymapItem *kmi)
527 {
528         int kmitype= wm_userdef_event_map(kmi->type);
529
530         /* the matching rules */
531         if(kmitype==KM_TEXTINPUT)
532                 if(ISKEYBOARD(winevent->type)) return 1;
533         if(kmitype!=KM_ANY)
534                 if(winevent->type!=kmitype) return 0;
535         
536         if(kmi->val!=KM_ANY)
537                 if(winevent->val!=kmi->val) return 0;
538         
539         /* modifiers also check bits, so it allows modifier order */
540         if(kmi->shift!=KM_ANY)
541                 if(winevent->shift != kmi->shift && !(winevent->shift & kmi->shift)) return 0;
542         if(kmi->ctrl!=KM_ANY)
543                 if(winevent->ctrl != kmi->ctrl && !(winevent->ctrl & kmi->ctrl)) return 0;
544         if(kmi->alt!=KM_ANY)
545                 if(winevent->alt != kmi->alt && !(winevent->alt & kmi->alt)) return 0;
546         if(kmi->oskey!=KM_ANY)
547                 if(winevent->oskey != kmi->oskey && !(winevent->oskey & kmi->oskey)) return 0;
548         if(kmi->keymodifier)
549                 if(winevent->keymodifier!=kmi->keymodifier) return 0;
550         
551         return 1;
552 }
553
554 static int wm_event_always_pass(wmEvent *event)
555 {
556         /* some events we always pass on, to ensure proper communication */
557         return ELEM5(event->type, TIMER, TIMER0, TIMER1, TIMER2, TIMERJOBS);
558 }
559
560 /* Warning: this function removes a modal handler, when finished */
561 static int wm_handler_operator_call(bContext *C, ListBase *handlers, wmEventHandler *handler, wmEvent *event, PointerRNA *properties)
562 {
563         int retval= OPERATOR_PASS_THROUGH;
564         
565         /* derived, modal or blocking operator */
566         if(handler->op) {
567                 wmOperator *op= handler->op;
568                 wmOperatorType *ot= op->type;
569
570                 if(ot->modal) {
571                         /* we set context to where modal handler came from */
572                         ScrArea *area= CTX_wm_area(C);
573                         ARegion *region= CTX_wm_region(C);
574                         
575                         CTX_wm_area_set(C, handler->op_area);
576                         CTX_wm_region_set(C, handler->op_region);
577                         
578                         retval= ot->modal(C, op, event);
579
580                         /* putting back screen context, reval can pass trough after modal failures! */
581                         if((retval & OPERATOR_PASS_THROUGH) || wm_event_always_pass(event)) {
582                                 CTX_wm_area_set(C, area);
583                                 CTX_wm_region_set(C, region);
584                         }
585                         else {
586                                 /* this special cases is for areas and regions that get removed */
587                                 CTX_wm_area_set(C, NULL);
588                                 CTX_wm_region_set(C, NULL);
589                         }
590
591                         if(!(retval & OPERATOR_RUNNING_MODAL))
592                                 if(op->reports->list.first)
593                                         uiPupMenuReports(C, op->reports);
594
595                         if (retval & OPERATOR_FINISHED) {
596                                 if(G.f & G_DEBUG)
597                                         wm_operator_print(op); /* todo - this print may double up, might want to check more flags then the FINISHED */
598                         }                       
599
600                         if(retval & OPERATOR_FINISHED) {
601                                 if(ot->flag & OPTYPE_UNDO)
602                                         ED_undo_push_op(C, op);
603                                 
604                                 if(ot->flag & OPTYPE_REGISTER)
605                                         wm_operator_register(CTX_wm_manager(C), op);
606                                 else
607                                         WM_operator_free(op);
608                                 handler->op= NULL;
609                         }
610                         else if(retval & (OPERATOR_CANCELLED|OPERATOR_FINISHED)) {
611                                 WM_operator_free(op);
612                                 handler->op= NULL;
613                         }
614                         
615                         /* remove modal handler, operator itself should have been cancelled and freed */
616                         if(retval & (OPERATOR_CANCELLED|OPERATOR_FINISHED)) {
617                                 BLI_remlink(handlers, handler);
618                                 wm_event_free_handler(handler);
619                                 MEM_freeN(handler);
620                                 
621                                 /* prevent silly errors from operator users */
622                                 //retval &= ~OPERATOR_PASS_THROUGH;
623                         }
624                         
625                 }
626                 else
627                         printf("wm_handler_operator_call error\n");
628         }
629         else {
630                 wmOperatorType *ot= WM_operatortype_find(event->keymap_idname);
631
632                 if(ot)
633                         retval= wm_operator_invoke(C, ot, event, properties);
634         }
635
636         if(retval & OPERATOR_PASS_THROUGH)
637                 return WM_HANDLER_CONTINUE;
638
639         return WM_HANDLER_BREAK;
640 }
641
642 static int wm_handler_ui_call(bContext *C, wmEventHandler *handler, wmEvent *event)
643 {
644         ScrArea *area= CTX_wm_area(C);
645         ARegion *region= CTX_wm_region(C);
646         int retval;
647                         
648         /* we set context to where ui handler came from */
649         if(handler->ui_area) CTX_wm_area_set(C, handler->ui_area);
650         if(handler->ui_region) CTX_wm_region_set(C, handler->ui_region);
651
652         retval= handler->ui_handle(C, event, handler->ui_userdata);
653
654         /* putting back screen context */
655         if((retval != WM_UI_HANDLER_BREAK) || wm_event_always_pass(event)) {
656                 CTX_wm_area_set(C, area);
657                 CTX_wm_region_set(C, region);
658         }
659         else {
660                 /* this special cases is for areas and regions that get removed */
661                 CTX_wm_area_set(C, NULL);
662                 CTX_wm_region_set(C, NULL);
663         }
664
665         if(retval == WM_UI_HANDLER_BREAK)
666                 return WM_HANDLER_BREAK;
667
668         return WM_HANDLER_CONTINUE;
669 }
670
671 static int handler_boundbox_test(wmEventHandler *handler, wmEvent *event)
672 {
673         if(handler->bbwin) {
674                 if(handler->bblocal) {
675                         rcti rect= *handler->bblocal;
676                         BLI_translate_rcti(&rect, handler->bbwin->xmin, handler->bbwin->ymin);
677                         return BLI_in_rcti(&rect, event->x, event->y);
678                 }
679                 else 
680                         return BLI_in_rcti(handler->bbwin, event->x, event->y);
681         }
682         return 1;
683 }
684
685 static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers)
686 {
687         wmEventHandler *handler, *nexthandler;
688         int action= WM_HANDLER_CONTINUE;
689
690         if(handlers==NULL) return action;
691         
692         /* modal handlers can get removed in this loop, we keep the loop this way */
693         for(handler= handlers->first; handler; handler= nexthandler) {
694                 nexthandler= handler->next;
695
696                 /* optional boundbox */
697                 if(handler_boundbox_test(handler, event)) {
698                 
699                         /* modal+blocking handler */
700                         if(handler->flag & WM_HANDLER_BLOCKING)
701                                 action= WM_HANDLER_BREAK;
702
703                         if(handler->keymap) {
704                                 wmKeymapItem *kmi;
705                                 
706                                 for(kmi= handler->keymap->first; kmi; kmi= kmi->next) {
707                                         if(wm_eventmatch(event, kmi)) {
708                                                 
709                                                 event->keymap_idname= kmi->idname;      /* weak, but allows interactive callback to not use rawkey */
710                                                 
711                                                 action= wm_handler_operator_call(C, handlers, handler, event, kmi->ptr);
712                                                 if(action==WM_HANDLER_BREAK)  /* not wm_event_always_pass(event) here, it denotes removed handler */
713                                                         break;
714                                         }
715                                 }
716                         }
717                         else if(handler->ui_handle) {
718                                 action= wm_handler_ui_call(C, handler, event);
719                         }
720                         else {
721                                 /* modal, swallows all */
722                                 action= wm_handler_operator_call(C, handlers, handler, event, NULL);
723                         }
724
725                         if(!wm_event_always_pass(event) && action==WM_HANDLER_BREAK)
726                                 break;
727                 }
728                 
729                 /* fileread case */
730                 if(CTX_wm_window(C)==NULL)
731                         break;
732         }
733         return action;
734 }
735
736 static int wm_event_inside_i(wmEvent *event, rcti *rect)
737 {
738         if(BLI_in_rcti(rect, event->x, event->y))
739            return 1;
740         if(event->type==MOUSEMOVE) {
741                 if( BLI_in_rcti(rect, event->prevx, event->prevy)) {
742                         return 1;
743                 }
744                 return 0;
745         }
746         return 0;
747 }
748
749 static ScrArea *area_event_inside(bContext *C, int x, int y)
750 {
751         bScreen *screen= CTX_wm_screen(C);
752         ScrArea *sa;
753         
754         if(screen)
755                 for(sa= screen->areabase.first; sa; sa= sa->next)
756                         if(BLI_in_rcti(&sa->totrct, x, y))
757                                 return sa;
758         return NULL;
759 }
760
761 static ARegion *region_event_inside(bContext *C, int x, int y)
762 {
763         bScreen *screen= CTX_wm_screen(C);
764         ScrArea *area= CTX_wm_area(C);
765         ARegion *ar;
766         
767         if(screen && area)
768                 for(ar= area->regionbase.first; ar; ar= ar->next)
769                         if(BLI_in_rcti(&ar->winrct, x, y))
770                                 return ar;
771         return NULL;
772 }
773
774 static void wm_paintcursor_tag(bContext *C, wmPaintCursor *pc, ARegion *ar)
775 {
776         if(ar) {
777                 for(; pc; pc= pc->next) {
778                         if(pc->poll(C)) {
779                                 wmWindow *win= CTX_wm_window(C);
780                                 win->screen->do_draw_paintcursor= 1;
781
782                                 if(win->drawmethod != USER_DRAW_TRIPLE)
783                                         ED_region_tag_redraw(ar);
784                         }
785                 }
786         }
787 }
788
789 /* called on mousemove, check updates for paintcursors */
790 /* context was set on active area and region */
791 static void wm_paintcursor_test(bContext *C, wmEvent *event)
792 {
793         wmWindowManager *wm= CTX_wm_manager(C);
794         
795         if(wm->paintcursors.first) {
796                 ARegion *ar= CTX_wm_region(C);
797                 if(ar)
798                         wm_paintcursor_tag(C, wm->paintcursors.first, ar);
799                 
800                 /* if previous position was not in current region, we have to set a temp new context */
801                 if(ar==NULL || !BLI_in_rcti(&ar->winrct, event->prevx, event->prevy)) {
802                         ScrArea *sa= CTX_wm_area(C);
803                         
804                         CTX_wm_area_set(C, area_event_inside(C, event->prevx, event->prevy));
805                         CTX_wm_region_set(C, region_event_inside(C, event->prevx, event->prevy));
806
807                         wm_paintcursor_tag(C, wm->paintcursors.first, CTX_wm_region(C));
808                         
809                         CTX_wm_area_set(C, sa);
810                         CTX_wm_region_set(C, ar);
811                 }
812         }
813 }
814
815 /* called in main loop */
816 /* goes over entire hierarchy:  events -> window -> screen -> area -> region */
817 void wm_event_do_handlers(bContext *C)
818 {
819         wmWindow *win;
820
821         for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
822                 wmEvent *event;
823                 
824                 if( win->screen==NULL )
825                         wm_event_free_all(win);
826                 
827                 while( (event= win->queue.first) ) {
828                         int action;
829                         
830                         CTX_wm_window_set(C, win);
831                         
832                         /* we let modal handlers get active area/region, also wm_paintcursor_test needs it */
833                         CTX_wm_area_set(C, area_event_inside(C, event->x, event->y));
834                         CTX_wm_region_set(C, region_event_inside(C, event->x, event->y));
835                         
836                         /* MVC demands to not draw in event handlers... but we need to leave it for ogl selecting etc */
837                         wm_window_make_drawable(C, win);
838                         
839                         action= wm_handlers_do(C, event, &win->handlers);
840                         
841                         /* fileread case */
842                         if(CTX_wm_window(C)==NULL) {
843                                 return;
844                         }
845                         
846                         /* builtin tweak, if action is break it removes tweak */
847                         if(!wm_event_always_pass(event))
848                                 wm_tweakevent_test(C, event, action);
849                         
850                         if(wm_event_always_pass(event) || action==WM_HANDLER_CONTINUE) {
851                                 ScrArea *sa;
852                                 ARegion *ar;
853                                 int doit= 0;
854                                 
855                                 /* XXX to solve, here screen handlers? */
856                                 if(!wm_event_always_pass(event)) {
857                                         if(event->type==MOUSEMOVE) {
858                                                 /* state variables in screen, cursors */
859                                                 ED_screen_set_subwinactive(win, event); 
860                                                 /* for regions having custom cursors */
861                                                 wm_paintcursor_test(C, event);
862                                         }
863                                 }
864                                 
865                                 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
866                                         if(wm_event_always_pass(event) || wm_event_inside_i(event, &sa->totrct)) {
867                                                 
868                                                 CTX_wm_area_set(C, sa);
869                                                 CTX_wm_region_set(C, NULL);
870                                                 action= wm_handlers_do(C, event, &sa->handlers);
871
872                                                 if(wm_event_always_pass(event) || action==WM_HANDLER_CONTINUE) {
873                                                         for(ar=sa->regionbase.first; ar; ar= ar->next) {
874                                                                 if(wm_event_always_pass(event) || wm_event_inside_i(event, &ar->winrct)) {
875                                                                         CTX_wm_region_set(C, ar);
876                                                                         action= wm_handlers_do(C, event, &ar->handlers);
877
878                                                                         doit |= (BLI_in_rcti(&ar->winrct, event->x, event->y));
879                                                                         
880                                                                         if(!wm_event_always_pass(event)) {
881                                                                                 if(action==WM_HANDLER_BREAK)
882                                                                                         break;
883                                                                         }
884                                                                 }
885                                                         }
886                                                 }
887                                                 /* NOTE: do not escape on WM_HANDLER_BREAK, mousemove needs handled for previous area */
888                                         }
889                                 }
890                                 
891                                 /* XXX hrmf, this gives reliable previous mouse coord for area change, feels bad? 
892                                    doing it on ghost queue gives errors when mousemoves go over area borders */
893                                 if(doit && win->screen->subwinactive != win->screen->mainwin) {
894                                         win->eventstate->prevx= event->x;
895                                         win->eventstate->prevy= event->y;
896                                 }
897                         }
898                         
899                         /* unlink and free here, blender-quit then frees all */
900                         BLI_remlink(&win->queue, event);
901                         wm_event_free(event);
902                         
903                 }
904                 CTX_wm_window_set(C, NULL);
905         }
906 }
907
908 /* lets not expose struct outside wm? */
909 void WM_event_set_handler_flag(wmEventHandler *handler, int flag)
910 {
911         handler->flag= flag;
912 }
913
914 wmEventHandler *WM_event_add_modal_handler(bContext *C, ListBase *handlers, wmOperator *op)
915 {
916         wmEventHandler *handler= MEM_callocN(sizeof(wmEventHandler), "event modal handler");
917         handler->op= op;
918         handler->op_area= CTX_wm_area(C);               /* means frozen screen context for modal handlers! */
919         handler->op_region= CTX_wm_region(C);
920         
921         BLI_addhead(handlers, handler);
922
923         return handler;
924 }
925
926 wmEventHandler *WM_event_add_keymap_handler(ListBase *handlers, ListBase *keymap)
927 {
928         wmEventHandler *handler;
929
930         /* only allow same keymap once */
931         for(handler= handlers->first; handler; handler= handler->next)
932                 if(handler->keymap==keymap)
933                         return handler;
934         
935         handler= MEM_callocN(sizeof(wmEventHandler), "event keymap handler");
936         BLI_addtail(handlers, handler);
937         handler->keymap= keymap;
938
939         return handler;
940 }
941
942 /* priorities not implemented yet, for time being just insert in begin of list */
943 wmEventHandler *WM_event_add_keymap_handler_priority(ListBase *handlers, ListBase *keymap, int priority)
944 {
945         wmEventHandler *handler;
946         
947         WM_event_remove_keymap_handler(handlers, keymap);
948         
949         handler= MEM_callocN(sizeof(wmEventHandler), "event keymap handler");
950         BLI_addhead(handlers, handler);
951         handler->keymap= keymap;
952         
953         return handler;
954 }
955
956 wmEventHandler *WM_event_add_keymap_handler_bb(ListBase *handlers, ListBase *keymap, rcti *bblocal, rcti *bbwin)
957 {
958         wmEventHandler *handler= WM_event_add_keymap_handler(handlers, keymap);
959         
960         if(handler) {
961                 handler->bblocal= bblocal;
962                 handler->bbwin= bbwin;
963         }
964         return handler;
965 }
966
967 void WM_event_remove_keymap_handler(ListBase *handlers, ListBase *keymap)
968 {
969         wmEventHandler *handler;
970         
971         for(handler= handlers->first; handler; handler= handler->next) {
972                 if(handler->keymap==keymap) {
973                         BLI_remlink(handlers, handler);
974                         wm_event_free_handler(handler);
975                         MEM_freeN(handler);
976                         break;
977                 }
978         }
979 }
980
981 wmEventHandler *WM_event_add_ui_handler(bContext *C, ListBase *handlers, wmUIHandlerFunc func, wmUIHandlerRemoveFunc remove, void *userdata)
982 {
983         wmEventHandler *handler= MEM_callocN(sizeof(wmEventHandler), "event ui handler");
984         handler->ui_handle= func;
985         handler->ui_remove= remove;
986         handler->ui_userdata= userdata;
987         handler->ui_area= (C)? CTX_wm_area(C): NULL;
988         handler->ui_region= (C)? CTX_wm_region(C): NULL;
989         
990         BLI_addhead(handlers, handler);
991         
992         return handler;
993 }
994
995 void WM_event_remove_ui_handler(ListBase *handlers, wmUIHandlerFunc func, wmUIHandlerRemoveFunc remove, void *userdata)
996 {
997         wmEventHandler *handler;
998         
999         for(handler= handlers->first; handler; handler= handler->next) {
1000                 if(handler->ui_handle == func && handler->ui_remove == remove && handler->ui_userdata == userdata) {
1001                         BLI_remlink(handlers, handler);
1002                         wm_event_free_handler(handler);
1003                         MEM_freeN(handler);
1004                         break;
1005                 }
1006         }
1007 }
1008
1009 void WM_event_add_mousemove(bContext *C)
1010 {
1011         wmWindow *window= CTX_wm_window(C);
1012         wmEvent event= *(window->eventstate);
1013         event.type= MOUSEMOVE;
1014         event.prevx= event.x;
1015         event.prevy= event.y;
1016         wm_event_add(window, &event);
1017 }
1018
1019 /* for modal callbacks, check configuration for how to interpret exit with tweaks  */
1020 int WM_modal_tweak_exit(wmEvent *evt, int tweak_event)
1021 {
1022         /* user preset or keymap? dunno... */
1023         int tweak_modal= (U.flag & USER_DRAGIMMEDIATE)==0;
1024         
1025         switch(tweak_event) {
1026                 case EVT_TWEAK_L:
1027                 case EVT_TWEAK_M:
1028                 case EVT_TWEAK_R:
1029                         if(evt->val==tweak_modal)
1030                                 return 1;
1031                 default:
1032                         /* this case is when modal callcback didnt get started with a tweak */
1033                         if(evt->val)
1034                                 return 1;
1035         }
1036         return 0;
1037 }
1038
1039
1040 /* ********************* ghost stuff *************** */
1041
1042 static int convert_key(GHOST_TKey key) 
1043 {
1044         if (key>=GHOST_kKeyA && key<=GHOST_kKeyZ) {
1045                 return (AKEY + ((int) key - GHOST_kKeyA));
1046         } else if (key>=GHOST_kKey0 && key<=GHOST_kKey9) {
1047                 return (ZEROKEY + ((int) key - GHOST_kKey0));
1048         } else if (key>=GHOST_kKeyNumpad0 && key<=GHOST_kKeyNumpad9) {
1049                 return (PAD0 + ((int) key - GHOST_kKeyNumpad0));
1050         } else if (key>=GHOST_kKeyF1 && key<=GHOST_kKeyF12) {
1051                 return (F1KEY + ((int) key - GHOST_kKeyF1));
1052         } else {
1053                 switch (key) {
1054                         case GHOST_kKeyBackSpace:               return BACKSPACEKEY;
1055                         case GHOST_kKeyTab:                             return TABKEY;
1056                         case GHOST_kKeyLinefeed:                return LINEFEEDKEY;
1057                         case GHOST_kKeyClear:                   return 0;
1058                         case GHOST_kKeyEnter:                   return RETKEY;
1059                                 
1060                         case GHOST_kKeyEsc:                             return ESCKEY;
1061                         case GHOST_kKeySpace:                   return SPACEKEY;
1062                         case GHOST_kKeyQuote:                   return QUOTEKEY;
1063                         case GHOST_kKeyComma:                   return COMMAKEY;
1064                         case GHOST_kKeyMinus:                   return MINUSKEY;
1065                         case GHOST_kKeyPeriod:                  return PERIODKEY;
1066                         case GHOST_kKeySlash:                   return SLASHKEY;
1067                                 
1068                         case GHOST_kKeySemicolon:               return SEMICOLONKEY;
1069                         case GHOST_kKeyEqual:                   return EQUALKEY;
1070                                 
1071                         case GHOST_kKeyLeftBracket:             return LEFTBRACKETKEY;
1072                         case GHOST_kKeyRightBracket:    return RIGHTBRACKETKEY;
1073                         case GHOST_kKeyBackslash:               return BACKSLASHKEY;
1074                         case GHOST_kKeyAccentGrave:             return ACCENTGRAVEKEY;
1075                                 
1076                         case GHOST_kKeyLeftShift:               return LEFTSHIFTKEY;
1077                         case GHOST_kKeyRightShift:              return RIGHTSHIFTKEY;
1078                         case GHOST_kKeyLeftControl:             return LEFTCTRLKEY;
1079                         case GHOST_kKeyRightControl:    return RIGHTCTRLKEY;
1080                         case GHOST_kKeyCommand:                 return COMMANDKEY;
1081                         case GHOST_kKeyLeftAlt:                 return LEFTALTKEY;
1082                         case GHOST_kKeyRightAlt:                return RIGHTALTKEY;
1083                                 
1084                         case GHOST_kKeyCapsLock:                return CAPSLOCKKEY;
1085                         case GHOST_kKeyNumLock:                 return 0;
1086                         case GHOST_kKeyScrollLock:              return 0;
1087                                 
1088                         case GHOST_kKeyLeftArrow:               return LEFTARROWKEY;
1089                         case GHOST_kKeyRightArrow:              return RIGHTARROWKEY;
1090                         case GHOST_kKeyUpArrow:                 return UPARROWKEY;
1091                         case GHOST_kKeyDownArrow:               return DOWNARROWKEY;
1092                                 
1093                         case GHOST_kKeyPrintScreen:             return 0;
1094                         case GHOST_kKeyPause:                   return PAUSEKEY;
1095                                 
1096                         case GHOST_kKeyInsert:                  return INSERTKEY;
1097                         case GHOST_kKeyDelete:                  return DELKEY;
1098                         case GHOST_kKeyHome:                    return HOMEKEY;
1099                         case GHOST_kKeyEnd:                             return ENDKEY;
1100                         case GHOST_kKeyUpPage:                  return PAGEUPKEY;
1101                         case GHOST_kKeyDownPage:                return PAGEDOWNKEY;
1102                                 
1103                         case GHOST_kKeyNumpadPeriod:    return PADPERIOD;
1104                         case GHOST_kKeyNumpadEnter:             return PADENTER;
1105                         case GHOST_kKeyNumpadPlus:              return PADPLUSKEY;
1106                         case GHOST_kKeyNumpadMinus:             return PADMINUS;
1107                         case GHOST_kKeyNumpadAsterisk:  return PADASTERKEY;
1108                         case GHOST_kKeyNumpadSlash:             return PADSLASHKEY;
1109                                 
1110                         case GHOST_kKeyGrLess:              return GRLESSKEY; 
1111                                 
1112                         default:
1113                                 return UNKNOWNKEY;      /* GHOST_kKeyUnknown */
1114                 }
1115         }
1116 }
1117
1118 /* adds customdata to event */
1119 static void update_tablet_data(wmWindow *win, wmEvent *event)
1120 {
1121         const GHOST_TabletData *td= GHOST_GetTabletData(win->ghostwin);
1122         
1123         /* if there's tablet data from an active tablet device then add it */
1124         if ((td != NULL) && td->Active) {
1125                 struct wmTabletData *wmtab= MEM_mallocN(sizeof(wmTabletData), "customdata tablet");
1126                 
1127                 wmtab->Active = td->Active;
1128                 wmtab->Pressure = td->Pressure;
1129                 wmtab->Xtilt = td->Xtilt;
1130                 wmtab->Ytilt = td->Ytilt;
1131                 
1132                 event->custom= EVT_DATA_TABLET;
1133                 event->customdata= wmtab;
1134                 event->customdatafree= 1;
1135         } 
1136 }
1137
1138
1139 /* windows store own event queues, no bContext here */
1140 void wm_event_add_ghostevent(wmWindow *win, int type, void *customdata)
1141 {
1142         wmEvent event, *evt= win->eventstate;
1143         
1144         /* initialize and copy state (only mouse x y and modifiers) */
1145         event= *evt;
1146         
1147         switch (type) {
1148                 /* mouse move */
1149                 case GHOST_kEventCursorMove: {
1150                         if(win->active) {
1151                                 GHOST_TEventCursorData *cd= customdata;
1152                                 int cx, cy;
1153
1154                                 GHOST_ScreenToClient(win->ghostwin, cd->x, cd->y, &cx, &cy);
1155                                 
1156                                 event.type= MOUSEMOVE;
1157                                 event.x= evt->x= cx;
1158                                 event.y= evt->y= (win->sizey-1) - cy;
1159                                 
1160                                 update_tablet_data(win, &event);
1161                                 wm_event_add(win, &event);
1162                         }
1163                         break;
1164                 }
1165                 /* mouse button */
1166                 case GHOST_kEventButtonDown:
1167                 case GHOST_kEventButtonUp: {
1168                         GHOST_TEventButtonData *bd= customdata;
1169                         event.val= (type==GHOST_kEventButtonDown);
1170                         
1171                         if (bd->button == GHOST_kButtonMaskLeft)
1172                                 event.type= LEFTMOUSE;
1173                         else if (bd->button == GHOST_kButtonMaskRight)
1174                                 event.type= RIGHTMOUSE;
1175                         else
1176                                 event.type= MIDDLEMOUSE;
1177                         
1178                         if(event.val)
1179                                 event.keymodifier= evt->keymodifier= event.type;
1180                         else
1181                                 event.keymodifier= evt->keymodifier= 0;
1182                         
1183                         update_tablet_data(win, &event);
1184                         wm_event_add(win, &event);
1185                         
1186                         break;
1187                 }
1188                 /* keyboard */
1189                 case GHOST_kEventKeyDown:
1190                 case GHOST_kEventKeyUp: {
1191                         GHOST_TEventKeyData *kd= customdata;
1192                         event.type= convert_key(kd->key);
1193                         event.ascii= kd->ascii;
1194                         event.val= (type==GHOST_kEventKeyDown); /* XXX eventmatch uses defines, bad code... */
1195                         
1196                         /* exclude arrow keys, esc, etc from text input */
1197                         if(type==GHOST_kEventKeyUp || (event.ascii<32 && event.ascii>14))
1198                                 event.ascii= '\0';
1199                         
1200                         /* modifiers */
1201                         if (event.type==LEFTSHIFTKEY || event.type==RIGHTSHIFTKEY) {
1202                                 event.shift= evt->shift= event.val;
1203                                 if(event.val && (evt->ctrl || evt->alt || evt->oskey))
1204                                    event.shift= evt->shift = 3;         // define?
1205                         } 
1206                         else if (event.type==LEFTCTRLKEY || event.type==RIGHTCTRLKEY) {
1207                                 event.ctrl= evt->ctrl= event.val;
1208                                 if(event.val && (evt->shift || evt->alt || evt->oskey))
1209                                    event.ctrl= evt->ctrl = 3;           // define?
1210                         } 
1211                         else if (event.type==LEFTALTKEY || event.type==RIGHTALTKEY) {
1212                                 event.alt= evt->alt= event.val;
1213                                 if(event.val && (evt->ctrl || evt->shift || evt->oskey))
1214                                    event.alt= evt->alt = 3;             // define?
1215                         } 
1216                         else if (event.type==COMMANDKEY) {
1217                                 event.oskey= evt->oskey= event.val;
1218                                 if(event.val && (evt->ctrl || evt->alt || evt->shift))
1219                                    event.oskey= evt->oskey = 3;         // define?
1220                         }
1221                         
1222                         /* if test_break set, it catches this. Keep global for now? */
1223                         if(event.type==ESCKEY)
1224                                 G.afbreek= 1;
1225                         
1226                         wm_event_add(win, &event);
1227                         
1228                         break;
1229                 }
1230                         
1231                 case GHOST_kEventWheel: {
1232                         GHOST_TEventWheelData* wheelData = customdata;
1233                         
1234                         if (wheelData->z > 0)
1235                                 event.type= WHEELUPMOUSE;
1236                         else
1237                                 event.type= WHEELDOWNMOUSE;
1238                         
1239                         event.val= KM_PRESS;
1240                         wm_event_add(win, &event);
1241                         
1242                         break;
1243                 }
1244                 case GHOST_kEventTimer: {
1245                         event.type= TIMER;
1246                         event.custom= EVT_DATA_TIMER;
1247                         event.customdata= customdata;
1248                         wm_event_add(win, &event);
1249
1250                         break;
1251                 }
1252
1253                 case GHOST_kEventUnknown:
1254                 case GHOST_kNumEventTypes:
1255                         break;
1256         }
1257 }
1258