2.5/RNA:
[blender.git] / source / blender / windowmanager / intern / wm_operators.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 <float.h>
30 #define _USE_MATH_DEFINES
31 #include <math.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35
36 #include "DNA_ID.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
40 #include "DNA_windowmanager_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "PIL_time.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_dynstr.h" /*for WM_operator_pystring */
48
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_idprop.h"
52 #include "BKE_library.h"
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55 #include "BKE_scene.h"
56 #include "BKE_utildefines.h"
57
58 #include "BIF_gl.h"
59 #include "BIF_glutil.h" /* for paint cursor */
60
61 #include "IMB_imbuf_types.h"
62
63 #include "ED_screen.h"
64 #include "ED_util.h"
65
66 #include "RNA_access.h"
67 #include "RNA_define.h"
68
69 #include "UI_interface.h"
70 #include "UI_resources.h"
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74
75 #include "wm.h"
76 #include "wm_draw.h"
77 #include "wm_event_system.h"
78 #include "wm_subwindow.h"
79 #include "wm_window.h"
80
81
82
83 static ListBase global_ops= {NULL, NULL};
84
85 /* ************ operator API, exported ********** */
86
87
88 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
89 {
90         wmOperatorType *ot;
91         
92         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
93         WM_operator_bl_idname(idname_bl, idname);
94
95         for(ot= global_ops.first; ot; ot= ot->next) {
96                 if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
97                    return ot;
98         }
99         
100         if(!quiet)
101                 printf("search for unknown operator %s, %s\n", idname_bl, idname);
102         
103         return NULL;
104 }
105
106 wmOperatorType *WM_operatortype_exists(const char *idname)
107 {
108         wmOperatorType *ot;
109         
110         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
111         WM_operator_bl_idname(idname_bl, idname);
112
113         for(ot= global_ops.first; ot; ot= ot->next) {
114                 if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
115                    return ot;
116         }
117         return NULL;
118 }
119
120 wmOperatorType *WM_operatortype_first(void)
121 {
122         return global_ops.first;
123 }
124
125 /* all ops in 1 list (for time being... needs evaluation later) */
126 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
127 {
128         wmOperatorType *ot;
129         
130         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
131         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
132         opfunc(ot);
133
134         if(ot->name==NULL) {
135                 static char dummy_name[] = "Dummy Name";
136                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
137                 ot->name= dummy_name;
138         }
139
140         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)"); // XXX All ops should have a description but for now allow them not to.
141         RNA_def_struct_identifier(ot->srna, ot->idname);
142         BLI_addtail(&global_ops, ot);
143 }
144
145 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
146 {
147         wmOperatorType *ot;
148
149         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
150         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
151         opfunc(ot, userdata);
152         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
153         RNA_def_struct_identifier(ot->srna, ot->idname);
154         BLI_addtail(&global_ops, ot);
155 }
156
157 /* ********************* macro operator ******************** */
158
159 /* macro exec only runs exec calls */
160 static int wm_macro_exec(bContext *C, wmOperator *op)
161 {
162         wmOperator *opm;
163         int retval= OPERATOR_FINISHED;
164         
165 //      printf("macro exec %s\n", op->type->idname);
166         
167         for(opm= op->macro.first; opm; opm= opm->next) {
168                 
169                 if(opm->type->exec) {
170 //                      printf("macro exec %s\n", opm->type->idname);
171                         retval= opm->type->exec(C, opm);
172                 
173                         if(!(retval & OPERATOR_FINISHED))
174                                 break;
175                 }
176         }
177 //      if(opm)
178 //              printf("macro ended not finished\n");
179 //      else
180 //              printf("macro end\n");
181         
182         return retval;
183 }
184
185 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
186 {
187         wmOperator *opm;
188         int retval= OPERATOR_FINISHED;
189         
190 //      printf("macro invoke %s\n", op->type->idname);
191         
192         for(opm= op->macro.first; opm; opm= opm->next) {
193                 
194                 if(opm->type->invoke)
195                         retval= opm->type->invoke(C, opm, event);
196                 else if(opm->type->exec)
197                         retval= opm->type->exec(C, opm);
198                 
199                 if(!(retval & OPERATOR_FINISHED))
200                         break;
201         }
202         
203 //      if(opm)
204 //              printf("macro ended not finished\n");
205 //      else
206 //              printf("macro end\n");
207         
208         
209         return retval;
210 }
211
212 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
213 {
214 //      printf("macro modal %s\n", op->type->idname);
215         
216         if(op->opm==NULL)
217                 printf("macro error, calling NULL modal()\n");
218         else {
219 //              printf("macro modal %s\n", op->opm->type->idname);
220                 return op->opm->type->modal(C, op->opm, event);
221         }       
222         
223         return OPERATOR_FINISHED;
224 }
225
226 /* Names have to be static for now */
227 wmOperatorType *WM_operatortype_append_macro(char *idname, char *name, int flag)
228 {
229         wmOperatorType *ot;
230         
231         if(WM_operatortype_exists(idname)) {
232                 printf("Macro error: operator %s exists\n", idname);
233                 return NULL;
234         }
235         
236         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
237         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
238         
239         ot->idname= idname;
240         ot->name= name;
241         ot->flag= OPTYPE_MACRO | flag;
242         
243         ot->exec= wm_macro_exec;
244         ot->invoke= wm_macro_invoke;
245         ot->modal= wm_macro_modal;
246         ot->poll= NULL;
247         
248         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)"); // XXX All ops should have a description but for now allow them not to.
249         RNA_def_struct_identifier(ot->srna, ot->idname);
250
251         BLI_addtail(&global_ops, ot);
252
253         return ot;
254 }
255
256 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
257 {
258         wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
259         
260         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
261
262         /* do this on first use, since operatordefinitions might have been not done yet */
263 //      otmacro->ptr= MEM_callocN(sizeof(PointerRNA), "optype macro ItemPtr");
264 //      WM_operator_properties_create(otmacro->ptr, idname);
265         
266         BLI_addtail(&ot->macro, otmacro);
267         
268         return otmacro;
269 }
270
271 static void wm_operatortype_free_macro(wmOperatorType *ot)
272 {
273         wmOperatorTypeMacro *otmacro;
274         
275         for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
276                 if(otmacro->ptr) {
277                         WM_operator_properties_free(otmacro->ptr);
278                         MEM_freeN(otmacro->ptr);
279                 }
280         }
281         BLI_freelistN(&ot->macro);
282 }
283
284
285 int WM_operatortype_remove(const char *idname)
286 {
287         wmOperatorType *ot = WM_operatortype_find(idname, 0);
288
289         if (ot==NULL)
290                 return 0;
291         
292         BLI_remlink(&global_ops, ot);
293         RNA_struct_free(&BLENDER_RNA, ot->srna);
294         
295         if(ot->macro.first)
296                 wm_operatortype_free_macro(ot);
297         
298         MEM_freeN(ot);
299
300         return 1;
301 }
302
303 /* SOME_OT_op -> some.op */
304 void WM_operator_py_idname(char *to, const char *from)
305 {
306         char *sep= strstr(from, "_OT_");
307         if(sep) {
308                 int i, ofs= (sep-from);
309
310                 for(i=0; i<ofs; i++)
311                         to[i]= tolower(from[i]);
312
313                 to[ofs] = '.';
314                 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
315         }
316         else {
317                 /* should not happen but support just incase */
318                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
319         }
320 }
321
322 /* some.op -> SOME_OT_op */
323 void WM_operator_bl_idname(char *to, const char *from)
324 {
325         char *sep= strstr(from, ".");
326
327         if(sep) {
328                 int i, ofs= (sep-from);
329
330                 for(i=0; i<ofs; i++)
331                         to[i]= toupper(from[i]);
332
333                 BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
334                 BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
335         }
336         else {
337                 /* should not happen but support just incase */
338                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
339         }
340 }
341
342 /* print a string representation of the operator, with the args that it runs 
343  * so python can run it again,
344  *
345  * When calling from an existing wmOperator do.
346  * WM_operator_pystring(op->type, op->ptr);
347  */
348 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
349 {
350         const char *arg_name= NULL;
351         char idname_py[OP_MAX_TYPENAME];
352
353         PropertyRNA *prop, *iterprop;
354
355         /* for building the string */
356         DynStr *dynstr= BLI_dynstr_new();
357         char *cstring, *buf;
358         int first_iter=1, ok= 1;
359
360
361         /* only to get the orginal props for comparisons */
362         PointerRNA opptr_default;
363         PropertyRNA *prop_default;
364         char *buf_default;
365         if(!all_args) {
366                 WM_operator_properties_create(&opptr_default, ot->idname);
367         }
368
369
370         WM_operator_py_idname(idname_py, ot->idname);
371         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
372
373         iterprop= RNA_struct_iterator_property(opptr->type);
374
375         RNA_PROP_BEGIN(opptr, propptr, iterprop) {
376                 prop= propptr.data;
377                 arg_name= RNA_property_identifier(prop);
378
379                 if (strcmp(arg_name, "rna_type")==0) continue;
380
381                 buf= RNA_property_as_string(C, opptr, prop);
382                 
383                 ok= 1;
384
385                 if(!all_args) {
386                         /* not verbose, so only add in attributes that use non-default values
387                          * slow but good for tooltips */
388                         prop_default= RNA_struct_find_property(&opptr_default, arg_name);
389
390                         if(prop_default) {
391                                 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
392
393                                 if(strcmp(buf, buf_default)==0)
394                                         ok= 0; /* values match, dont bother printing */
395
396                                 MEM_freeN(buf_default);
397                         }
398
399                 }
400                 if(ok) {
401                         BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
402                         first_iter = 0;
403                 }
404
405                 MEM_freeN(buf);
406
407         }
408         RNA_PROP_END;
409
410         if(all_args==0)
411                 WM_operator_properties_free(&opptr_default);
412
413         BLI_dynstr_append(dynstr, ")");
414
415         cstring = BLI_dynstr_get_cstring(dynstr);
416         BLI_dynstr_free(dynstr);
417         return cstring;
418 }
419
420 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
421 {
422         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
423
424         if(ot)
425                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
426         else
427                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
428 }
429
430 void WM_operator_properties_free(PointerRNA *ptr)
431 {
432         IDProperty *properties= ptr->data;
433
434         if(properties) {
435                 IDP_FreeProperty(properties);
436                 MEM_freeN(properties);
437         }
438 }
439
440 /* ************ default op callbacks, exported *********** */
441
442 /* invoke callback, uses enum property named "type" */
443 /* only weak thing is the fixed property name... */
444 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
445 {
446         PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
447         uiPopupMenu *pup;
448         uiLayout *layout;
449
450         if(prop==NULL) {
451                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
452         }
453         else if (RNA_property_type(prop) != PROP_ENUM) {
454                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
455         }
456         else {
457                 pup= uiPupMenuBegin(C, op->type->name, 0);
458                 layout= uiPupMenuLayout(pup);
459                 uiItemsEnumO(layout, op->type->idname, "type");
460                 uiPupMenuEnd(C, pup);
461         }
462
463         return OPERATOR_CANCELLED;
464 }
465
466 /* op->invoke */
467 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
468 {
469         uiPopupMenu *pup;
470         uiLayout *layout;
471
472         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
473         layout= uiPupMenuLayout(pup);
474         uiItemO(layout, NULL, 0, op->type->idname);
475         uiPupMenuEnd(C, pup);
476         
477         return OPERATOR_CANCELLED;
478 }
479
480 /* op->invoke, opens fileselect if filename property not set, otherwise executes */
481 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
482 {
483         if (RNA_property_is_set(op->ptr, "filename")) {
484                 return WM_operator_call(C, op);
485         } 
486         else {
487                 WM_event_add_fileselect(C, op);
488                 return OPERATOR_RUNNING_MODAL;
489         }
490 }
491
492 /* default properties for fileselect */
493 void WM_operator_properties_filesel(wmOperatorType *ot, int filter)
494 {
495         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "Path to file.");
496
497         RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
498         RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
499         RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
500         RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
501         RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
502         RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
503         RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
504         RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
505 }
506
507 /* op->poll */
508 int WM_operator_winactive(bContext *C)
509 {
510         if(CTX_wm_window(C)==NULL) return 0;
511         return 1;
512 }
513
514 /* op->invoke */
515 static void redo_cb(bContext *C, void *arg_op, void *arg2)
516 {
517         wmOperator *lastop= arg_op;
518         
519         if(lastop) {
520                 ED_undo_pop_op(C, lastop);
521                 WM_operator_repeat(C, lastop);
522         }
523 }
524
525 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
526 {
527         wmWindowManager *wm= CTX_wm_manager(C);
528         wmOperator *op= arg_op;
529         PointerRNA ptr;
530         uiBlock *block;
531         uiLayout *layout;
532         uiStyle *style= U.uistyles.first;
533         
534         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
535         uiBlockClearFlag(block, UI_BLOCK_LOOP);
536         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
537         uiBlockSetFunc(block, redo_cb, arg_op, NULL);
538
539         if(!op->properties) {
540                 IDPropertyTemplate val = {0};
541                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
542         }
543
544         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
545         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
546         uiDefAutoButsRNA(C, layout, &ptr, 2);
547
548         uiPopupBoundsBlock(block, 4.0f, 0, 0);
549         uiEndBlock(C, block);
550
551         return block;
552 }
553
554 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
555 {
556         int retval= OPERATOR_CANCELLED;
557         
558         if(op->type->exec)
559                 retval= op->type->exec(C, op);
560
561         if(retval != OPERATOR_CANCELLED)
562                 uiPupBlock(C, wm_block_create_redo, op);
563
564         return retval;
565 }
566
567 int WM_operator_redo_popup(bContext *C, wmOperator *op)
568 {
569         uiPupBlock(C, wm_block_create_redo, op);
570
571         return OPERATOR_CANCELLED;
572 }
573
574 /* ***************** Debug menu ************************* */
575
576 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
577 {
578         wmOperator *op= arg_op;
579         uiBlock *block;
580         uiLayout *layout;
581         uiStyle *style= U.uistyles.first;
582         
583         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
584         uiBlockClearFlag(block, UI_BLOCK_LOOP);
585         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
586         
587         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
588         uiDefAutoButsRNA(C, layout, op->ptr, 2);
589         
590         uiPopupBoundsBlock(block, 4.0f, 0, 0);
591         uiEndBlock(C, block);
592         
593         return block;
594 }
595
596 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
597 {
598         G.rt= RNA_int_get(op->ptr, "debugval");
599         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
600         WM_event_add_notifier(C, NC_WINDOW, NULL);
601         
602         return OPERATOR_FINISHED;       
603 }
604
605 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
606 {
607         
608         RNA_int_set(op->ptr, "debugval", G.rt);
609         
610         /* pass on operator, so return modal */
611         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
612         
613         return OPERATOR_RUNNING_MODAL;
614 }
615
616 static void WM_OT_debug_menu(wmOperatorType *ot)
617 {
618         ot->name= "Debug Menu";
619         ot->idname= "WM_OT_debug_menu";
620         
621         ot->invoke= wm_debug_menu_invoke;
622         ot->exec= wm_debug_menu_exec;
623         ot->poll= WM_operator_winactive;
624         
625         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
626 }
627
628 /* ***************** Search menu ************************* */
629 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
630 {
631         wmOperatorType *ot= arg2;
632         
633         if(ot)
634                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
635 }
636
637 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
638 {
639         wmOperatorType *ot = WM_operatortype_first();
640         
641         for(; ot; ot= ot->next) {
642                 
643                 if(BLI_strcasestr(ot->name, str)) {
644                         if(ot->poll==NULL || ot->poll((bContext *)C)) {
645                                 char name[256];
646                                 int len= strlen(ot->name);
647                                 
648                                 /* display name for menu, can hold hotkey */
649                                 BLI_strncpy(name, ot->name, 256);
650                                 
651                                 /* check for hotkey */
652                                 if(len < 256-6) {
653                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
654                                                 name[len]= '|';
655                                 }
656                                 
657                                 if(0==uiSearchItemAdd(items, name, ot, 0))
658                                         break;
659                         }
660                 }
661         }
662 }
663
664 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
665 {
666         static char search[256]= "";
667         wmEvent event;
668         wmWindow *win= CTX_wm_window(C);
669         uiBlock *block;
670         uiBut *but;
671         
672         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
673         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
674         
675         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
676         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
677         
678         /* fake button, it holds space for search items */
679         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
680         
681         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
682         uiEndBlock(C, block);
683         
684         event= *(win->eventstate);      /* XXX huh huh? make api call */
685         event.type= EVT_BUT_OPEN;
686         event.val= KM_PRESS;
687         event.customdata= but;
688         event.customdatafree= FALSE;
689         wm_event_add(win, &event);
690         
691         return block;
692 }
693
694 static int wm_search_menu_exec(bContext *C, wmOperator *op)
695 {
696         
697         return OPERATOR_FINISHED;       
698 }
699
700 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
701 {
702         
703         uiPupBlock(C, wm_block_search_menu, op);
704         
705         return OPERATOR_CANCELLED;
706 }
707
708 /* op->poll */
709 int wm_search_menu_poll(bContext *C)
710 {
711         if(CTX_wm_window(C)==NULL) return 0;
712         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
713         return 1;
714 }
715
716 static void WM_OT_search_menu(wmOperatorType *ot)
717 {
718         ot->name= "Search Menu";
719         ot->idname= "WM_OT_search_menu";
720         
721         ot->invoke= wm_search_menu_invoke;
722         ot->exec= wm_search_menu_exec;
723         ot->poll= wm_search_menu_poll;
724 }
725
726
727 /* ************ window / screen operator definitions ************** */
728
729 static void WM_OT_window_duplicate(wmOperatorType *ot)
730 {
731         ot->name= "Duplicate Window";
732         ot->idname= "WM_OT_window_duplicate";
733         
734         ot->invoke= WM_operator_confirm;
735         ot->exec= wm_window_duplicate_op;
736         ot->poll= WM_operator_winactive;
737 }
738
739 static void WM_OT_save_homefile(wmOperatorType *ot)
740 {
741         ot->name= "Save User Settings";
742         ot->idname= "WM_OT_save_homefile";
743         
744         ot->invoke= WM_operator_confirm;
745         ot->exec= WM_write_homefile;
746         ot->poll= WM_operator_winactive;
747 }
748
749 static void WM_OT_read_homefile(wmOperatorType *ot)
750 {
751         ot->name= "Reload Start-Up File";
752         ot->idname= "WM_OT_read_homefile";
753         
754         ot->invoke= WM_operator_confirm;
755         ot->exec= WM_read_homefile;
756         ot->poll= WM_operator_winactive;
757         
758         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
759 }
760
761
762 /* ********* recent file *********** */
763
764 static int recentfile_exec(bContext *C, wmOperator *op)
765 {
766         int event= RNA_enum_get(op->ptr, "file");
767
768         // XXX wm in context is not set correctly after WM_read_file -> crash
769         // do it before for now, but is this correct with multiple windows?
770
771         if(event>0) {
772                 if (G.sce[0] && (event==1)) {
773                         WM_event_add_notifier(C, NC_WINDOW, NULL);
774                         WM_read_file(C, G.sce, op->reports);
775                 }
776                 else {
777                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
778                         if(recent) {
779                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
780                                 WM_read_file(C, recent->filename, op->reports);
781                         }
782                 }
783         }
784         return 0;
785 }
786
787 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
788 {
789         uiPopupMenu *pup;
790         uiLayout *layout;
791
792         pup= uiPupMenuBegin(C, "Open Recent", 0);
793         layout= uiPupMenuLayout(pup);
794         uiItemsEnumO(layout, op->type->idname, "file");
795         uiPupMenuEnd(C, pup);
796         
797         return OPERATOR_CANCELLED;
798 }
799
800 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
801 {
802         EnumPropertyItem tmp = {0, "", 0, "", ""};
803         EnumPropertyItem *item= NULL;
804         struct RecentFile *recent;
805         int totitem= 0, i, ofs= 0;
806
807         if(G.sce[0]) {
808                 tmp.value= 1;
809                 tmp.identifier= G.sce;
810                 tmp.name= G.sce;
811                 RNA_enum_item_add(&item, &totitem, &tmp);
812                 ofs = 1;
813         }
814
815         /* dynamically construct enum */
816         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
817                 if(strcmp(recent->filename, G.sce)) {
818                         tmp.value= i+ofs+1;
819                         tmp.identifier= recent->filename;
820                         tmp.name= recent->filename;
821                         RNA_enum_item_add(&item, &totitem, &tmp);
822                 }
823         }
824
825         RNA_enum_item_end(&item, &totitem);
826         *free= 1;
827
828         return item;
829 }
830
831 static void WM_OT_open_recentfile(wmOperatorType *ot)
832 {
833         PropertyRNA *prop;
834         static EnumPropertyItem file_items[]= {
835                 {0, NULL, 0, NULL, NULL}};
836
837         ot->name= "Open Recent File";
838         ot->idname= "WM_OT_open_recentfile";
839         
840         ot->invoke= wm_recentfile_invoke;
841         ot->exec= recentfile_exec;
842         ot->poll= WM_operator_winactive;
843         
844         prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
845         RNA_def_enum_funcs(prop, open_recentfile_itemf);
846 }
847
848 /* ********* main file *********** */
849
850 static void untitled(char *name)
851 {
852         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
853                 char *c= BLI_last_slash(name);
854                 
855                 if (c)
856                         strcpy(&c[1], "untitled.blend");
857                 else
858                         strcpy(name, "untitled.blend");
859         }
860 }
861
862
863 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
864 {
865         RNA_string_set(op->ptr, "filename", G.sce);
866         WM_event_add_fileselect(C, op);
867
868         return OPERATOR_RUNNING_MODAL;
869 }
870
871 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
872 {
873         char filename[FILE_MAX];
874         RNA_string_get(op->ptr, "filename", filename);
875         
876         // XXX wm in context is not set correctly after WM_read_file -> crash
877         // do it before for now, but is this correct with multiple windows?
878         WM_event_add_notifier(C, NC_WINDOW, NULL);
879
880         WM_read_file(C, filename, op->reports);
881         
882         return 0;
883 }
884
885 static void WM_OT_open_mainfile(wmOperatorType *ot)
886 {
887         ot->name= "Open Blender File";
888         ot->idname= "WM_OT_open_mainfile";
889         
890         ot->invoke= wm_open_mainfile_invoke;
891         ot->exec= wm_open_mainfile_exec;
892         ot->poll= WM_operator_winactive;
893         
894         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
895 }
896
897 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
898 {
899         char scestr[FILE_MAX], filename[FILE_MAX];
900         int save_over;
901
902         /* back up some values */
903         BLI_strncpy(scestr, G.sce, sizeof(scestr));
904         save_over = G.save_over;
905
906         // XXX wm in context is not set correctly after WM_read_file -> crash
907         // do it before for now, but is this correct with multiple windows?
908         WM_event_add_notifier(C, NC_WINDOW, NULL);
909
910         /* load file */
911         BLI_make_file_string("/", filename, btempdir, "quit.blend");
912         WM_read_file(C, filename, op->reports);
913
914         /* restore */
915         G.save_over = save_over;
916         BLI_strncpy(G.sce, scestr, sizeof(G.sce));
917
918         return 0;
919 }
920
921 static void WM_OT_recover_last_session(wmOperatorType *ot)
922 {
923         ot->name= "Recover Last Session";
924         ot->idname= "WM_OT_recover_last_session";
925         
926         ot->exec= wm_recover_last_session_exec;
927         ot->poll= WM_operator_winactive;
928 }
929
930 static void save_set_compress(wmOperator *op)
931 {
932         if(!RNA_property_is_set(op->ptr, "compress")) {
933                 if(G.save_over) /* keep flag for existing file */
934                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
935                 else /* use userdef for new file */
936                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
937         }
938 }
939
940 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
941 {
942         char name[FILE_MAX];
943
944         save_set_compress(op);
945         
946         BLI_strncpy(name, G.sce, FILE_MAX);
947         untitled(name);
948         RNA_string_set(op->ptr, "filename", name);
949         
950         WM_event_add_fileselect(C, op);
951
952         return OPERATOR_RUNNING_MODAL;
953 }
954
955 /* function used for WM_OT_save_mainfile too */
956 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
957 {
958         char filename[FILE_MAX];
959         int compress;
960
961         save_set_compress(op);
962         compress= RNA_boolean_get(op->ptr, "compress");
963         
964         if(RNA_property_is_set(op->ptr, "filename"))
965                 RNA_string_get(op->ptr, "filename", filename);
966         else {
967                 BLI_strncpy(filename, G.sce, FILE_MAX);
968                 untitled(filename);
969         }
970
971         WM_write_file(C, filename, compress, op->reports);
972         
973         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
974
975         return 0;
976 }
977
978 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
979 {
980         ot->name= "Save As Blender File";
981         ot->idname= "WM_OT_save_as_mainfile";
982         
983         ot->invoke= wm_save_as_mainfile_invoke;
984         ot->exec= wm_save_as_mainfile_exec;
985         ot->poll= WM_operator_winactive;
986         
987         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
988         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
989 }
990
991 /* *************** Save file directly ******** */
992
993 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
994 {
995         char name[FILE_MAX];
996
997         save_set_compress(op);
998         
999         BLI_strncpy(name, G.sce, FILE_MAX);
1000         untitled(name);
1001         RNA_string_set(op->ptr, "filename", name);
1002         uiPupMenuSaveOver(C, op, name);
1003
1004         return OPERATOR_RUNNING_MODAL;
1005 }
1006
1007 static void WM_OT_save_mainfile(wmOperatorType *ot)
1008 {
1009         ot->name= "Save Blender File";
1010         ot->idname= "WM_OT_save_mainfile";
1011         
1012         ot->invoke= wm_save_mainfile_invoke;
1013         ot->exec= wm_save_as_mainfile_exec;
1014         ot->poll= WM_operator_winactive;
1015         
1016         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
1017         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1018 }
1019
1020
1021 /* *********************** */
1022
1023 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1024 {
1025     ot->name= "Toggle Fullscreen";
1026     ot->idname= "WM_OT_window_fullscreen_toggle";
1027
1028     ot->invoke= WM_operator_confirm;
1029     ot->exec= wm_window_fullscreen_toggle_op;
1030     ot->poll= WM_operator_winactive;
1031 }
1032
1033 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1034 {
1035         WM_operator_free(op);
1036         
1037         WM_exit(C);     
1038         
1039         return OPERATOR_FINISHED;
1040 }
1041
1042 static void WM_OT_exit_blender(wmOperatorType *ot)
1043 {
1044         ot->name= "Exit Blender";
1045         ot->idname= "WM_OT_exit_blender";
1046
1047         ot->invoke= WM_operator_confirm;
1048         ot->exec= wm_exit_blender_op;
1049         ot->poll= WM_operator_winactive;
1050 }
1051
1052 /* ************ default paint cursors, draw always around cursor *********** */
1053 /*
1054  - returns handler to free 
1055  - poll(bContext): returns 1 if draw should happen
1056  - draw(bContext): drawing callback for paint cursor
1057 */
1058
1059 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1060                                void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
1061 {
1062         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1063         
1064         BLI_addtail(&wm->paintcursors, pc);
1065         
1066         pc->customdata = customdata;
1067         pc->poll= poll;
1068         pc->draw= draw;
1069         
1070         return pc;
1071 }
1072
1073 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1074 {
1075         wmPaintCursor *pc;
1076         
1077         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1078                 if(pc == (wmPaintCursor *)handle) {
1079                         BLI_remlink(&wm->paintcursors, pc);
1080                         MEM_freeN(pc);
1081                         return;
1082                 }
1083         }
1084 }
1085
1086 /* ************ window gesture operator-callback definitions ************** */
1087 /*
1088  * These are default callbacks for use in operators requiring gesture input
1089  */
1090
1091 /* **************** Border gesture *************** */
1092
1093 /* Border gesture has two types:
1094    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1095    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1096
1097    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1098 */
1099
1100 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1101 {
1102         wmGesture *gesture= op->customdata;
1103         rcti *rect= gesture->customdata;
1104         
1105         if(rect->xmin > rect->xmax)
1106                 SWAP(int, rect->xmin, rect->xmax);
1107         if(rect->ymin > rect->ymax)
1108                 SWAP(int, rect->ymin, rect->ymax);
1109         
1110         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1111                 return 0;
1112                 
1113         /* operator arguments and storage. */
1114         RNA_int_set(op->ptr, "xmin", rect->xmin);
1115         RNA_int_set(op->ptr, "ymin", rect->ymin);
1116         RNA_int_set(op->ptr, "xmax", rect->xmax);
1117         RNA_int_set(op->ptr, "ymax", rect->ymax);
1118         
1119         /* XXX weak; border should be configured for this without reading event types */
1120         if( RNA_struct_find_property(op->ptr, "event_type") ) {
1121                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1122                         event_type= LEFTMOUSE;
1123                 
1124                 RNA_int_set(op->ptr, "event_type", event_type);
1125         }
1126         op->type->exec(C, op);
1127         
1128         return 1;
1129 }
1130
1131 static void wm_gesture_end(bContext *C, wmOperator *op)
1132 {
1133         wmGesture *gesture= op->customdata;
1134         
1135         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1136         op->customdata= NULL;
1137
1138         ED_area_tag_redraw(CTX_wm_area(C));
1139         
1140         if( RNA_struct_find_property(op->ptr, "cursor") )
1141                 WM_cursor_restore(CTX_wm_window(C));
1142 }
1143
1144 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1145 {
1146         if(WM_key_event_is_tweak(event->type))
1147                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1148         else
1149                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1150
1151         /* add modal handler */
1152         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1153         
1154         wm_gesture_tag_redraw(C);
1155
1156         return OPERATOR_RUNNING_MODAL;
1157 }
1158
1159 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1160 {
1161         wmGesture *gesture= op->customdata;
1162         rcti *rect= gesture->customdata;
1163         int sx, sy;
1164         
1165         switch(event->type) {
1166                 case MOUSEMOVE:
1167                         
1168                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1169                         
1170                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1171                                 rect->xmin= rect->xmax= event->x - sx;
1172                                 rect->ymin= rect->ymax= event->y - sy;
1173                         }
1174                         else {
1175                                 rect->xmax= event->x - sx;
1176                                 rect->ymax= event->y - sy;
1177                         }
1178                         
1179                         wm_gesture_tag_redraw(C);
1180
1181                         break;
1182                         
1183                 case LEFTMOUSE:
1184                 case MIDDLEMOUSE:
1185                 case RIGHTMOUSE:
1186                         if(event->val==KM_PRESS) {
1187                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1188                                         gesture->mode= 1;
1189                                         wm_gesture_tag_redraw(C);
1190                                 }
1191                         }
1192                         else {
1193                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1194                                         wm_gesture_end(C, op);
1195                                         return OPERATOR_FINISHED;
1196                                 }
1197                                 wm_gesture_end(C, op);
1198                                 return OPERATOR_CANCELLED;
1199                         }
1200                         break;
1201                 case ESCKEY:
1202                         wm_gesture_end(C, op);
1203                         return OPERATOR_CANCELLED;
1204         }
1205         return OPERATOR_RUNNING_MODAL;
1206 }
1207
1208 /* **************** circle gesture *************** */
1209 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1210
1211 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1212 {
1213         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1214         
1215         /* add modal handler */
1216         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1217         
1218         wm_gesture_tag_redraw(C);
1219         
1220         return OPERATOR_RUNNING_MODAL;
1221 }
1222
1223 static void gesture_circle_apply(bContext *C, wmOperator *op)
1224 {
1225         wmGesture *gesture= op->customdata;
1226         rcti *rect= gesture->customdata;
1227         
1228         /* operator arguments and storage. */
1229         RNA_int_set(op->ptr, "x", rect->xmin);
1230         RNA_int_set(op->ptr, "y", rect->ymin);
1231         RNA_int_set(op->ptr, "radius", rect->xmax);
1232         
1233         if(op->type->exec)
1234                 op->type->exec(C, op);
1235 }
1236
1237 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1238 {
1239         wmGesture *gesture= op->customdata;
1240         rcti *rect= gesture->customdata;
1241         int sx, sy;
1242         
1243         switch(event->type) {
1244                 case MOUSEMOVE:
1245                         
1246                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1247                         
1248                         rect->xmin= event->x - sx;
1249                         rect->ymin= event->y - sy;
1250                         
1251                         wm_gesture_tag_redraw(C);
1252                         
1253                         if(gesture->mode)
1254                                 gesture_circle_apply(C, op);
1255
1256                         break;
1257                 case WHEELUPMOUSE:
1258                         rect->xmax += 2 + rect->xmax/10;
1259                         wm_gesture_tag_redraw(C);
1260                         break;
1261                 case WHEELDOWNMOUSE:
1262                         rect->xmax -= 2 + rect->xmax/10;
1263                         if(rect->xmax < 1) rect->xmax= 1;
1264                         wm_gesture_tag_redraw(C);
1265                         break;
1266                 case LEFTMOUSE:
1267                 case MIDDLEMOUSE:
1268                 case RIGHTMOUSE:
1269                         if(event->val==0) {     /* key release */
1270                                 wm_gesture_end(C, op);
1271                                 return OPERATOR_FINISHED;
1272                         }
1273                         else {
1274                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1275                                         RNA_int_set(op->ptr, "event_type", event->type);
1276                                 
1277                                 /* apply first click */
1278                                 gesture_circle_apply(C, op);
1279                                 gesture->mode= 1;
1280                         }
1281                         break;
1282                 case ESCKEY:
1283                         wm_gesture_end(C, op);
1284                         return OPERATOR_CANCELLED;
1285         }
1286         return OPERATOR_RUNNING_MODAL;
1287 }
1288
1289 #if 0
1290 /* template to copy from */
1291 void WM_OT_circle_gesture(wmOperatorType *ot)
1292 {
1293         ot->name= "Circle Gesture";
1294         ot->idname= "WM_OT_circle_gesture";
1295         
1296         ot->invoke= WM_gesture_circle_invoke;
1297         ot->modal= WM_gesture_circle_modal;
1298         
1299         ot->poll= WM_operator_winactive;
1300         
1301         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1302         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1303         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1304
1305 }
1306 #endif
1307
1308 /* **************** Tweak gesture *************** */
1309
1310 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1311 {
1312         wmWindow *window= CTX_wm_window(C);
1313         wmGesture *gesture= window->tweak;
1314         rcti *rect= gesture->customdata;
1315         int sx, sy, val;
1316         
1317         switch(event->type) {
1318                 case MOUSEMOVE:
1319                         
1320                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1321                         
1322                         rect->xmax= event->x - sx;
1323                         rect->ymax= event->y - sy;
1324                         
1325                         if((val= wm_gesture_evaluate(C, gesture))) {
1326                                 wmEvent event;
1327
1328                                 event= *(window->eventstate);
1329                                 if(gesture->event_type==LEFTMOUSE)
1330                                         event.type= EVT_TWEAK_L;
1331                                 else if(gesture->event_type==RIGHTMOUSE)
1332                                         event.type= EVT_TWEAK_R;
1333                                 else
1334                                         event.type= EVT_TWEAK_M;
1335                                 event.val= val;
1336                                 /* mouse coords! */
1337                                 wm_event_add(window, &event);
1338                                 
1339                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1340                                 window->tweak= NULL;
1341                         }
1342                         
1343                         break;
1344                         
1345                 case LEFTMOUSE:
1346                 case RIGHTMOUSE:
1347                 case MIDDLEMOUSE:
1348                         if(gesture->event_type==event->type) {
1349                                 WM_gesture_end(C, gesture);
1350                                 window->tweak= NULL;
1351                                 
1352                                 /* when tweak fails we should give the other keymap entries a chance */
1353                                 event->val= KM_RELEASE;
1354                         }
1355                         break;
1356                 default:
1357                         WM_gesture_end(C, gesture);
1358                         window->tweak= NULL;
1359         }
1360 }
1361
1362 /* standard tweak, called after window handlers passed on event */
1363 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1364 {
1365         wmWindow *win= CTX_wm_window(C);
1366         
1367         if(win->tweak==NULL) {
1368                 if(CTX_wm_region(C)) {
1369                         if(event->val) { // pressed
1370                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1371                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1372                         }
1373                 }
1374         }
1375         else {
1376                 if(action==WM_HANDLER_BREAK) {
1377                         WM_gesture_end(C, win->tweak);
1378                         win->tweak= NULL;
1379                 }
1380                 else
1381                         tweak_gesture_modal(C, event);
1382         }
1383 }
1384
1385 /* *********************** lasso gesture ****************** */
1386
1387 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1388 {
1389         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1390         
1391         /* add modal handler */
1392         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1393         
1394         wm_gesture_tag_redraw(C);
1395         
1396         if( RNA_struct_find_property(op->ptr, "cursor") )
1397                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1398         
1399         return OPERATOR_RUNNING_MODAL;
1400 }
1401
1402 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1403 {
1404         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1405         
1406         /* add modal handler */
1407         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1408         
1409         wm_gesture_tag_redraw(C);
1410         
1411         if( RNA_struct_find_property(op->ptr, "cursor") )
1412                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1413         
1414         return OPERATOR_RUNNING_MODAL;
1415 }
1416
1417
1418 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1419 {
1420         wmGesture *gesture= op->customdata;
1421         PointerRNA itemptr;
1422         float loc[2];
1423         int i;
1424         short *lasso= gesture->customdata;
1425         
1426         /* operator storage as path. */
1427
1428         for(i=0; i<gesture->points; i++, lasso+=2) {
1429                 loc[0]= lasso[0];
1430                 loc[1]= lasso[1];
1431                 RNA_collection_add(op->ptr, "path", &itemptr);
1432                 RNA_float_set_array(&itemptr, "loc", loc);
1433         }
1434         
1435         wm_gesture_end(C, op);
1436                 
1437         if(op->type->exec)
1438                 op->type->exec(C, op);
1439         
1440 }
1441
1442 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1443 {
1444         wmGesture *gesture= op->customdata;
1445         int sx, sy;
1446         
1447         switch(event->type) {
1448                 case MOUSEMOVE:
1449                         
1450                         wm_gesture_tag_redraw(C);
1451                         
1452                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1453                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1454                                 short *lasso= gesture->customdata;
1455                                 lasso += 2 * gesture->points;
1456                                 lasso[0] = event->x - sx;
1457                                 lasso[1] = event->y - sy;
1458                                 gesture->points++;
1459                         }
1460                         else {
1461                                 gesture_lasso_apply(C, op, event->type);
1462                                 return OPERATOR_FINISHED;
1463                         }
1464                         break;
1465                         
1466                 case LEFTMOUSE:
1467                 case MIDDLEMOUSE:
1468                 case RIGHTMOUSE:
1469                         if(event->val==0) {     /* key release */
1470                                 gesture_lasso_apply(C, op, event->type);
1471                                 return OPERATOR_FINISHED;
1472                         }
1473                         break;
1474                 case ESCKEY:
1475                         wm_gesture_end(C, op);
1476                         return OPERATOR_CANCELLED;
1477         }
1478         return OPERATOR_RUNNING_MODAL;
1479 }
1480
1481 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1482 {
1483         return WM_gesture_lasso_modal(C, op, event);
1484 }
1485
1486 #if 0
1487 /* template to copy from */
1488
1489 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1490 {
1491         RNA_BEGIN(op->ptr, itemptr, "path") {
1492                 float loc[2];
1493                 
1494                 RNA_float_get_array(&itemptr, "loc", loc);
1495                 printf("Location: %f %f\n", loc[0], loc[1]);
1496         }
1497         RNA_END;
1498         
1499         return OPERATOR_FINISHED;
1500 }
1501
1502 void WM_OT_lasso_gesture(wmOperatorType *ot)
1503 {
1504         PropertyRNA *prop;
1505         
1506         ot->name= "Lasso Gesture";
1507         ot->idname= "WM_OT_lasso_gesture";
1508         
1509         ot->invoke= WM_gesture_lasso_invoke;
1510         ot->modal= WM_gesture_lasso_modal;
1511         ot->exec= gesture_lasso_exec;
1512         
1513         ot->poll= WM_operator_winactive;
1514         
1515         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1516         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1517 }
1518 #endif
1519
1520 /* *********************** radial control ****************** */
1521
1522 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1523
1524 typedef struct wmRadialControl {
1525         int mode;
1526         float initial_value, value, max_value;
1527         int initial_mouse[2];
1528         void *cursor;
1529         GLuint tex;
1530 } wmRadialControl;
1531
1532 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1533 {
1534         wmRadialControl *rc = (wmRadialControl*)customdata;
1535         ARegion *ar = CTX_wm_region(C);
1536         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1537
1538         /* Keep cursor in the original place */
1539         x = rc->initial_mouse[0] - ar->winrct.xmin;
1540         y = rc->initial_mouse[1] - ar->winrct.ymin;
1541
1542         glPushMatrix();
1543         
1544         glTranslatef((float)x, (float)y, 0.0f);
1545
1546         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1547                 r1= rc->value;
1548                 r2= rc->initial_value;
1549                 r3= r1;
1550         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1551                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1552                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1553                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1554         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1555                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1556                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1557                 angle = rc->value;
1558         }
1559
1560         glColor4ub(255, 255, 255, 128);
1561         glEnable( GL_LINE_SMOOTH );
1562         glEnable(GL_BLEND);
1563
1564         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1565                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1566
1567         if(rc->tex) {
1568                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1569
1570                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1571                         glRotatef(angle, 0, 0, 1);
1572                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1573                 }
1574
1575                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1576
1577                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1578                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1579
1580                 glEnable(GL_TEXTURE_2D);
1581                 glBegin(GL_QUADS);
1582                 glColor4f(0,0,0, str);
1583                 glTexCoord2f(0,0);
1584                 glVertex2f(-r3, -r3);
1585                 glTexCoord2f(1,0);
1586                 glVertex2f(r3, -r3);
1587                 glTexCoord2f(1,1);
1588                 glVertex2f(r3, r3);
1589                 glTexCoord2f(0,1);
1590                 glVertex2f(-r3, r3);
1591                 glEnd();
1592                 glDisable(GL_TEXTURE_2D);
1593         }
1594
1595         glColor4ub(255, 255, 255, 128); 
1596         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1597         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1598         glDisable(GL_BLEND);
1599         glDisable( GL_LINE_SMOOTH );
1600         
1601         glPopMatrix();
1602 }
1603
1604 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1605 {
1606         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1607         int mode, initial_mouse[2], delta[2];
1608         float dist;
1609         double new_value = RNA_float_get(op->ptr, "new_value");
1610         int ret = OPERATOR_RUNNING_MODAL;
1611
1612         mode = RNA_int_get(op->ptr, "mode");
1613         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1614
1615         switch(event->type) {
1616         case MOUSEMOVE:
1617                 delta[0]= initial_mouse[0] - event->x;
1618                 delta[1]= initial_mouse[1] - event->y;
1619                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1620
1621                 if(mode == WM_RADIALCONTROL_SIZE)
1622                         new_value = dist;
1623                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1624                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1625                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1626                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1627                 
1628                 if(event->ctrl) {
1629                         if(mode == WM_RADIALCONTROL_STRENGTH)
1630                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1631                         else
1632                                 new_value = ((int)new_value + 5) / 10*10;
1633                 }
1634                 
1635                 break;
1636         case ESCKEY:
1637         case RIGHTMOUSE:
1638                 ret = OPERATOR_CANCELLED;
1639                 break;
1640         case LEFTMOUSE:
1641         case PADENTER:
1642                 op->type->exec(C, op);
1643                 ret = OPERATOR_FINISHED;
1644                 break;
1645         }
1646
1647         /* Clamp */
1648         if(new_value > rc->max_value)
1649                 new_value = rc->max_value;
1650         else if(new_value < 0)
1651                 new_value = 0;
1652
1653         /* Update paint data */
1654         rc->value = new_value;
1655
1656         RNA_float_set(op->ptr, "new_value", new_value);
1657
1658         if(ret != OPERATOR_RUNNING_MODAL) {
1659                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1660                 MEM_freeN(rc);
1661         }
1662         
1663         ED_region_tag_redraw(CTX_wm_region(C));
1664
1665         return ret;
1666 }
1667
1668 /* Expects the operator customdata to be an ImBuf (or NULL) */
1669 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1670 {
1671         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1672         int mode = RNA_int_get(op->ptr, "mode");
1673         float initial_value = RNA_float_get(op->ptr, "initial_value");
1674         int mouse[2] = {event->x, event->y};
1675
1676         if(mode == WM_RADIALCONTROL_SIZE) {
1677                 rc->max_value = 200;
1678                 mouse[0]-= initial_value;
1679         }
1680         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1681                 rc->max_value = 1;
1682                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1683         }
1684         else if(mode == WM_RADIALCONTROL_ANGLE) {
1685                 rc->max_value = 360;
1686                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1687                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1688                 initial_value *= 180.0f/M_PI;
1689         }
1690
1691         if(op->customdata) {
1692                 ImBuf *im = (ImBuf*)op->customdata;
1693                 /* Build GL texture */
1694                 glGenTextures(1, &rc->tex);
1695                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1696                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1697                 MEM_freeN(im->rect_float);
1698                 MEM_freeN(im);
1699         }
1700
1701         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1702         RNA_float_set(op->ptr, "new_value", initial_value);
1703                 
1704         op->customdata = rc;
1705         rc->mode = mode;
1706         rc->initial_value = initial_value;
1707         rc->initial_mouse[0] = mouse[0];
1708         rc->initial_mouse[1] = mouse[1];
1709         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1710                                               wm_radial_control_paint, op->customdata);
1711
1712         /* add modal handler */
1713         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1714         
1715         WM_radial_control_modal(C, op, event);
1716         
1717         return OPERATOR_RUNNING_MODAL;
1718 }
1719
1720 /* Gets a descriptive string of the operation */
1721 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1722 {
1723         int mode = RNA_int_get(op->ptr, "mode");
1724         float v = RNA_float_get(op->ptr, "new_value");
1725
1726         if(mode == WM_RADIALCONTROL_SIZE)
1727                 sprintf(str, "Size: %d", (int)v);
1728         else if(mode == WM_RADIALCONTROL_STRENGTH)
1729                 sprintf(str, "Strength: %d", (int)v);
1730         else if(mode == WM_RADIALCONTROL_ANGLE)
1731                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1732 }
1733
1734 /** Important: this doesn't define an actual operator, it
1735     just sets up the common parts of the radial control op. **/
1736 void WM_OT_radial_control_partial(wmOperatorType *ot)
1737 {
1738         static EnumPropertyItem prop_mode_items[] = {
1739                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
1740                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
1741                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
1742                 {0, NULL, 0, NULL, NULL}};
1743
1744         /* Should be set in custom invoke() */
1745         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1746
1747         /* Set internally, should be used in custom exec() to get final value */
1748         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1749
1750         /* Should be set before calling operator */
1751         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1752
1753         /* Internal */
1754         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1755 }
1756
1757 /* ************************** timer for testing ***************** */
1758
1759 /* uses no type defines, fully local testing function anyway... ;) */
1760
1761 static int ten_timer_exec(bContext *C, wmOperator *op)
1762 {
1763         ARegion *ar= CTX_wm_region(C);
1764         double stime= PIL_check_seconds_timer();
1765         int type = RNA_int_get(op->ptr, "type");
1766         int a, time;
1767         char tmpstr[128];
1768         
1769         WM_cursor_wait(1);
1770         
1771         for(a=0; a<10; a++) {
1772                 if (type==0) {
1773                         ED_region_do_draw(C, ar);
1774                 } 
1775                 else if (type==1) {
1776                         wmWindow *win= CTX_wm_window(C);
1777                         
1778                         ED_region_tag_redraw(ar);
1779                         wm_draw_update(C);
1780                         
1781                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1782                 }
1783                 else if (type==2) {
1784                         wmWindow *win= CTX_wm_window(C);
1785                         ScrArea *sa;
1786                         
1787                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1788                                 ED_area_tag_redraw(sa);
1789                         wm_draw_update(C);
1790                         
1791                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1792                 }
1793                 else if (type==3) {
1794                         Scene *scene= CTX_data_scene(C);
1795                         
1796                         if(a & 1) scene->r.cfra--;
1797                         else scene->r.cfra++;
1798                         scene_update_for_newframe(scene, scene->lay);
1799                 }
1800                 else {
1801                         ED_undo_pop(C);
1802                         ED_undo_redo(C);
1803                 }
1804         }
1805         
1806         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1807         
1808         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1809         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1810         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1811         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1812         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1813         
1814         WM_cursor_wait(0);
1815         
1816         uiPupMenuNotice(C, tmpstr);
1817         
1818         return OPERATOR_FINISHED;
1819 }
1820
1821 static void WM_OT_ten_timer(wmOperatorType *ot)
1822 {
1823         static EnumPropertyItem prop_type_items[] = {
1824         {0, "DRAW", 0, "Draw Region", ""},
1825         {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
1826         {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
1827         {3, "ANIMSTEP", 0, "Anim Step", ""},
1828         {4, "UNDO", 0, "Undo/Redo", ""},
1829         {0, NULL, 0, NULL, NULL}};
1830         
1831         ot->name= "Ten Timer";
1832         ot->idname= "WM_OT_ten_timer";
1833         
1834         ot->invoke= WM_menu_invoke;
1835         ot->exec= ten_timer_exec;
1836         ot->poll= WM_operator_winactive;
1837         
1838         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1839
1840 }
1841
1842
1843
1844 /* ******************************************************* */
1845  
1846 /* called on initialize WM_exit() */
1847 void wm_operatortype_free(void)
1848 {
1849         wmOperatorType *ot;
1850         
1851         for(ot= global_ops.first; ot; ot= ot->next)
1852                 if(ot->macro.first)
1853                         wm_operatortype_free_macro(ot);
1854         
1855         BLI_freelistN(&global_ops);
1856 }
1857
1858 /* called on initialize WM_init() */
1859 void wm_operatortype_init(void)
1860 {
1861         WM_operatortype_append(WM_OT_window_duplicate);
1862         WM_operatortype_append(WM_OT_read_homefile);
1863         WM_operatortype_append(WM_OT_save_homefile);
1864         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1865         WM_operatortype_append(WM_OT_exit_blender);
1866         WM_operatortype_append(WM_OT_open_recentfile);
1867         WM_operatortype_append(WM_OT_open_mainfile);
1868         WM_operatortype_append(WM_OT_recover_last_session);
1869         WM_operatortype_append(WM_OT_jobs_timer);
1870         WM_operatortype_append(WM_OT_save_as_mainfile);
1871         WM_operatortype_append(WM_OT_save_mainfile);
1872         WM_operatortype_append(WM_OT_ten_timer);
1873         WM_operatortype_append(WM_OT_debug_menu);
1874         WM_operatortype_append(WM_OT_search_menu);
1875 }
1876
1877 /* default keymap for windows and screens, only call once per WM */
1878 void wm_window_keymap(wmWindowManager *wm)
1879 {
1880         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1881         
1882         /* items to make WM work */
1883         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1884         
1885         /* note, this doesn't replace existing keymap items */
1886         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1887         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1888         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1889         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1890         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1891         WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1892         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1893         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_SHIFT, 0);
1894         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1895
1896         /* debug/testing */
1897         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1898         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1899         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, KM_CTRL, 0);
1900         
1901 }
1902