304da25017b21892aaca54a930d38d211676232b
[blender-staging.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= strchr(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         uiItemL(layout, op->type->name, 0);
547
548         if(op->type->ui)
549                 op->type->ui((bContext*)C, &ptr, layout);
550         else
551                 uiDefAutoButsRNA(C, layout, &ptr, 2);
552
553         uiPopupBoundsBlock(block, 4.0f, 0, 0);
554         uiEndBlock(C, block);
555
556         return block;
557 }
558
559 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
560 {
561         int retval= OPERATOR_CANCELLED;
562         
563         if(op->type->exec)
564                 retval= op->type->exec(C, op);
565
566         if(retval != OPERATOR_CANCELLED)
567                 uiPupBlock(C, wm_block_create_redo, op);
568
569         return retval;
570 }
571
572 int WM_operator_redo_popup(bContext *C, wmOperator *op)
573 {
574         uiPupBlock(C, wm_block_create_redo, op);
575
576         return OPERATOR_CANCELLED;
577 }
578
579 /* ***************** Debug menu ************************* */
580
581 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
582 {
583         wmOperator *op= arg_op;
584         uiBlock *block;
585         uiLayout *layout;
586         uiStyle *style= U.uistyles.first;
587         
588         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
589         uiBlockClearFlag(block, UI_BLOCK_LOOP);
590         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
591         
592         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
593         uiItemL(layout, op->type->name, 0);
594
595         if(op->type->ui)
596                 op->type->ui(C, op->ptr, layout);
597         else
598                 uiDefAutoButsRNA(C, layout, op->ptr, 2);
599         
600         uiPopupBoundsBlock(block, 4.0f, 0, 0);
601         uiEndBlock(C, block);
602         
603         return block;
604 }
605
606 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
607 {
608         G.rt= RNA_int_get(op->ptr, "debugval");
609         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
610         WM_event_add_notifier(C, NC_WINDOW, NULL);
611         
612         return OPERATOR_FINISHED;       
613 }
614
615 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
616 {
617         
618         RNA_int_set(op->ptr, "debugval", G.rt);
619         
620         /* pass on operator, so return modal */
621         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
622         
623         return OPERATOR_RUNNING_MODAL;
624 }
625
626 static void WM_OT_debug_menu(wmOperatorType *ot)
627 {
628         ot->name= "Debug Menu";
629         ot->idname= "WM_OT_debug_menu";
630         
631         ot->invoke= wm_debug_menu_invoke;
632         ot->exec= wm_debug_menu_exec;
633         ot->poll= WM_operator_winactive;
634         
635         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
636 }
637
638 /* ***************** Search menu ************************* */
639 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
640 {
641         wmOperatorType *ot= arg2;
642         
643         if(ot)
644                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
645 }
646
647 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
648 {
649         wmOperatorType *ot = WM_operatortype_first();
650         
651         for(; ot; ot= ot->next) {
652                 
653                 if(BLI_strcasestr(ot->name, str)) {
654                         if(ot->poll==NULL || ot->poll((bContext *)C)) {
655                                 char name[256];
656                                 int len= strlen(ot->name);
657                                 
658                                 /* display name for menu, can hold hotkey */
659                                 BLI_strncpy(name, ot->name, 256);
660                                 
661                                 /* check for hotkey */
662                                 if(len < 256-6) {
663                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
664                                                 name[len]= '|';
665                                 }
666                                 
667                                 if(0==uiSearchItemAdd(items, name, ot, 0))
668                                         break;
669                         }
670                 }
671         }
672 }
673
674 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
675 {
676         static char search[256]= "";
677         wmEvent event;
678         wmWindow *win= CTX_wm_window(C);
679         uiBlock *block;
680         uiBut *but;
681         
682         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
683         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
684         
685         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
686         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
687         
688         /* fake button, it holds space for search items */
689         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
690         
691         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
692         uiEndBlock(C, block);
693         
694         event= *(win->eventstate);      /* XXX huh huh? make api call */
695         event.type= EVT_BUT_OPEN;
696         event.val= KM_PRESS;
697         event.customdata= but;
698         event.customdatafree= FALSE;
699         wm_event_add(win, &event);
700         
701         return block;
702 }
703
704 static int wm_search_menu_exec(bContext *C, wmOperator *op)
705 {
706         
707         return OPERATOR_FINISHED;       
708 }
709
710 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
711 {
712         
713         uiPupBlock(C, wm_block_search_menu, op);
714         
715         return OPERATOR_CANCELLED;
716 }
717
718 /* op->poll */
719 int wm_search_menu_poll(bContext *C)
720 {
721         if(CTX_wm_window(C)==NULL) return 0;
722         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
723         return 1;
724 }
725
726 static void WM_OT_search_menu(wmOperatorType *ot)
727 {
728         ot->name= "Search Menu";
729         ot->idname= "WM_OT_search_menu";
730         
731         ot->invoke= wm_search_menu_invoke;
732         ot->exec= wm_search_menu_exec;
733         ot->poll= wm_search_menu_poll;
734 }
735
736
737 /* ************ window / screen operator definitions ************** */
738
739 static void WM_OT_window_duplicate(wmOperatorType *ot)
740 {
741         ot->name= "Duplicate Window";
742         ot->idname= "WM_OT_window_duplicate";
743         
744         ot->invoke= WM_operator_confirm;
745         ot->exec= wm_window_duplicate_op;
746         ot->poll= WM_operator_winactive;
747 }
748
749 static void WM_OT_save_homefile(wmOperatorType *ot)
750 {
751         ot->name= "Save User Settings";
752         ot->idname= "WM_OT_save_homefile";
753         
754         ot->invoke= WM_operator_confirm;
755         ot->exec= WM_write_homefile;
756         ot->poll= WM_operator_winactive;
757 }
758
759 static void WM_OT_read_homefile(wmOperatorType *ot)
760 {
761         ot->name= "Reload Start-Up File";
762         ot->idname= "WM_OT_read_homefile";
763         
764         ot->invoke= WM_operator_confirm;
765         ot->exec= WM_read_homefile;
766         ot->poll= WM_operator_winactive;
767         
768         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
769 }
770
771
772 /* ********* recent file *********** */
773
774 static int recentfile_exec(bContext *C, wmOperator *op)
775 {
776         int event= RNA_enum_get(op->ptr, "file");
777
778         // XXX wm in context is not set correctly after WM_read_file -> crash
779         // do it before for now, but is this correct with multiple windows?
780
781         if(event>0) {
782                 if (G.sce[0] && (event==1)) {
783                         WM_event_add_notifier(C, NC_WINDOW, NULL);
784                         WM_read_file(C, G.sce, op->reports);
785                 }
786                 else {
787                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
788                         if(recent) {
789                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
790                                 WM_read_file(C, recent->filename, op->reports);
791                         }
792                 }
793         }
794         return 0;
795 }
796
797 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
798 {
799         uiPopupMenu *pup;
800         uiLayout *layout;
801
802         pup= uiPupMenuBegin(C, "Open Recent", 0);
803         layout= uiPupMenuLayout(pup);
804         uiItemsEnumO(layout, op->type->idname, "file");
805         uiPupMenuEnd(C, pup);
806         
807         return OPERATOR_CANCELLED;
808 }
809
810 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
811 {
812         EnumPropertyItem tmp = {0, "", 0, "", ""};
813         EnumPropertyItem *item= NULL;
814         struct RecentFile *recent;
815         int totitem= 0, i, ofs= 0;
816
817         if(G.sce[0]) {
818                 tmp.value= 1;
819                 tmp.identifier= G.sce;
820                 tmp.name= G.sce;
821                 RNA_enum_item_add(&item, &totitem, &tmp);
822                 ofs = 1;
823         }
824
825         /* dynamically construct enum */
826         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
827                 if(strcmp(recent->filename, G.sce)) {
828                         tmp.value= i+ofs+1;
829                         tmp.identifier= recent->filename;
830                         tmp.name= recent->filename;
831                         RNA_enum_item_add(&item, &totitem, &tmp);
832                 }
833         }
834
835         RNA_enum_item_end(&item, &totitem);
836         *free= 1;
837
838         return item;
839 }
840
841 static void WM_OT_open_recentfile(wmOperatorType *ot)
842 {
843         PropertyRNA *prop;
844         static EnumPropertyItem file_items[]= {
845                 {0, NULL, 0, NULL, NULL}};
846
847         ot->name= "Open Recent File";
848         ot->idname= "WM_OT_open_recentfile";
849         
850         ot->invoke= wm_recentfile_invoke;
851         ot->exec= recentfile_exec;
852         ot->poll= WM_operator_winactive;
853         
854         prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
855         RNA_def_enum_funcs(prop, open_recentfile_itemf);
856 }
857
858 /* ********* main file *********** */
859
860 static void untitled(char *name)
861 {
862         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
863                 char *c= BLI_last_slash(name);
864                 
865                 if (c)
866                         strcpy(&c[1], "untitled.blend");
867                 else
868                         strcpy(name, "untitled.blend");
869         }
870 }
871
872
873 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
874 {
875         RNA_string_set(op->ptr, "filename", G.sce);
876         WM_event_add_fileselect(C, op);
877
878         return OPERATOR_RUNNING_MODAL;
879 }
880
881 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
882 {
883         char filename[FILE_MAX];
884         RNA_string_get(op->ptr, "filename", filename);
885         
886         // XXX wm in context is not set correctly after WM_read_file -> crash
887         // do it before for now, but is this correct with multiple windows?
888         WM_event_add_notifier(C, NC_WINDOW, NULL);
889
890         WM_read_file(C, filename, op->reports);
891         
892         return 0;
893 }
894
895 static void WM_OT_open_mainfile(wmOperatorType *ot)
896 {
897         ot->name= "Open Blender File";
898         ot->idname= "WM_OT_open_mainfile";
899         
900         ot->invoke= wm_open_mainfile_invoke;
901         ot->exec= wm_open_mainfile_exec;
902         ot->poll= WM_operator_winactive;
903         
904         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
905 }
906
907 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
908 {
909         char scestr[FILE_MAX], filename[FILE_MAX];
910         int save_over;
911
912         /* back up some values */
913         BLI_strncpy(scestr, G.sce, sizeof(scestr));
914         save_over = G.save_over;
915
916         // XXX wm in context is not set correctly after WM_read_file -> crash
917         // do it before for now, but is this correct with multiple windows?
918         WM_event_add_notifier(C, NC_WINDOW, NULL);
919
920         /* load file */
921         BLI_make_file_string("/", filename, btempdir, "quit.blend");
922         WM_read_file(C, filename, op->reports);
923
924         /* restore */
925         G.save_over = save_over;
926         BLI_strncpy(G.sce, scestr, sizeof(G.sce));
927
928         return 0;
929 }
930
931 static void WM_OT_recover_last_session(wmOperatorType *ot)
932 {
933         ot->name= "Recover Last Session";
934         ot->idname= "WM_OT_recover_last_session";
935         
936         ot->exec= wm_recover_last_session_exec;
937         ot->poll= WM_operator_winactive;
938 }
939
940 static void save_set_compress(wmOperator *op)
941 {
942         if(!RNA_property_is_set(op->ptr, "compress")) {
943                 if(G.save_over) /* keep flag for existing file */
944                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
945                 else /* use userdef for new file */
946                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
947         }
948 }
949
950 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
951 {
952         char name[FILE_MAX];
953
954         save_set_compress(op);
955         
956         BLI_strncpy(name, G.sce, FILE_MAX);
957         untitled(name);
958         RNA_string_set(op->ptr, "filename", name);
959         
960         WM_event_add_fileselect(C, op);
961
962         return OPERATOR_RUNNING_MODAL;
963 }
964
965 /* function used for WM_OT_save_mainfile too */
966 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
967 {
968         char filename[FILE_MAX];
969         int compress;
970
971         save_set_compress(op);
972         compress= RNA_boolean_get(op->ptr, "compress");
973         
974         if(RNA_property_is_set(op->ptr, "filename"))
975                 RNA_string_get(op->ptr, "filename", filename);
976         else {
977                 BLI_strncpy(filename, G.sce, FILE_MAX);
978                 untitled(filename);
979         }
980
981         WM_write_file(C, filename, compress, op->reports);
982         
983         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
984
985         return 0;
986 }
987
988 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
989 {
990         ot->name= "Save As Blender File";
991         ot->idname= "WM_OT_save_as_mainfile";
992         
993         ot->invoke= wm_save_as_mainfile_invoke;
994         ot->exec= wm_save_as_mainfile_exec;
995         ot->poll= WM_operator_winactive;
996         
997         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
998         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
999 }
1000
1001 /* *************** Save file directly ******** */
1002
1003 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1004 {
1005         char name[FILE_MAX];
1006
1007         save_set_compress(op);
1008         
1009         BLI_strncpy(name, G.sce, FILE_MAX);
1010         untitled(name);
1011         RNA_string_set(op->ptr, "filename", name);
1012         uiPupMenuSaveOver(C, op, name);
1013
1014         return OPERATOR_RUNNING_MODAL;
1015 }
1016
1017 static void WM_OT_save_mainfile(wmOperatorType *ot)
1018 {
1019         ot->name= "Save Blender File";
1020         ot->idname= "WM_OT_save_mainfile";
1021         
1022         ot->invoke= wm_save_mainfile_invoke;
1023         ot->exec= wm_save_as_mainfile_exec;
1024         ot->poll= WM_operator_winactive;
1025         
1026         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
1027         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1028 }
1029
1030
1031 /* *********************** */
1032
1033 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1034 {
1035     ot->name= "Toggle Fullscreen";
1036     ot->idname= "WM_OT_window_fullscreen_toggle";
1037
1038     ot->invoke= WM_operator_confirm;
1039     ot->exec= wm_window_fullscreen_toggle_op;
1040     ot->poll= WM_operator_winactive;
1041 }
1042
1043 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1044 {
1045         WM_operator_free(op);
1046         
1047         WM_exit(C);     
1048         
1049         return OPERATOR_FINISHED;
1050 }
1051
1052 static void WM_OT_exit_blender(wmOperatorType *ot)
1053 {
1054         ot->name= "Exit Blender";
1055         ot->idname= "WM_OT_exit_blender";
1056
1057         ot->invoke= WM_operator_confirm;
1058         ot->exec= wm_exit_blender_op;
1059         ot->poll= WM_operator_winactive;
1060 }
1061
1062 /* ************ default paint cursors, draw always around cursor *********** */
1063 /*
1064  - returns handler to free 
1065  - poll(bContext): returns 1 if draw should happen
1066  - draw(bContext): drawing callback for paint cursor
1067 */
1068
1069 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1070                                wmPaintCursorDraw draw, void *customdata)
1071 {
1072         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1073         
1074         BLI_addtail(&wm->paintcursors, pc);
1075         
1076         pc->customdata = customdata;
1077         pc->poll= poll;
1078         pc->draw= draw;
1079         
1080         return pc;
1081 }
1082
1083 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1084 {
1085         wmPaintCursor *pc;
1086         
1087         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1088                 if(pc == (wmPaintCursor *)handle) {
1089                         BLI_remlink(&wm->paintcursors, pc);
1090                         MEM_freeN(pc);
1091                         return;
1092                 }
1093         }
1094 }
1095
1096 /* ************ window gesture operator-callback definitions ************** */
1097 /*
1098  * These are default callbacks for use in operators requiring gesture input
1099  */
1100
1101 /* **************** Border gesture *************** */
1102
1103 /* Border gesture has two types:
1104    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1105    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1106
1107    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1108 */
1109
1110 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1111 {
1112         wmGesture *gesture= op->customdata;
1113         rcti *rect= gesture->customdata;
1114         
1115         if(rect->xmin > rect->xmax)
1116                 SWAP(int, rect->xmin, rect->xmax);
1117         if(rect->ymin > rect->ymax)
1118                 SWAP(int, rect->ymin, rect->ymax);
1119         
1120         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1121                 return 0;
1122                 
1123         /* operator arguments and storage. */
1124         RNA_int_set(op->ptr, "xmin", rect->xmin);
1125         RNA_int_set(op->ptr, "ymin", rect->ymin);
1126         RNA_int_set(op->ptr, "xmax", rect->xmax);
1127         RNA_int_set(op->ptr, "ymax", rect->ymax);
1128         
1129         /* XXX weak; border should be configured for this without reading event types */
1130         if( RNA_struct_find_property(op->ptr, "event_type") ) {
1131                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1132                         event_type= LEFTMOUSE;
1133                 
1134                 RNA_int_set(op->ptr, "event_type", event_type);
1135         }
1136         op->type->exec(C, op);
1137         
1138         return 1;
1139 }
1140
1141 static void wm_gesture_end(bContext *C, wmOperator *op)
1142 {
1143         wmGesture *gesture= op->customdata;
1144         
1145         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1146         op->customdata= NULL;
1147
1148         ED_area_tag_redraw(CTX_wm_area(C));
1149         
1150         if( RNA_struct_find_property(op->ptr, "cursor") )
1151                 WM_cursor_restore(CTX_wm_window(C));
1152 }
1153
1154 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1155 {
1156         if(WM_key_event_is_tweak(event->type))
1157                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1158         else
1159                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1160
1161         /* add modal handler */
1162         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1163         
1164         wm_gesture_tag_redraw(C);
1165
1166         return OPERATOR_RUNNING_MODAL;
1167 }
1168
1169 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1170 {
1171         wmGesture *gesture= op->customdata;
1172         rcti *rect= gesture->customdata;
1173         int sx, sy;
1174         
1175         switch(event->type) {
1176                 case MOUSEMOVE:
1177                         
1178                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1179                         
1180                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1181                                 rect->xmin= rect->xmax= event->x - sx;
1182                                 rect->ymin= rect->ymax= event->y - sy;
1183                         }
1184                         else {
1185                                 rect->xmax= event->x - sx;
1186                                 rect->ymax= event->y - sy;
1187                         }
1188                         
1189                         wm_gesture_tag_redraw(C);
1190
1191                         break;
1192                         
1193                 case LEFTMOUSE:
1194                 case MIDDLEMOUSE:
1195                 case RIGHTMOUSE:
1196                         if(event->val==KM_PRESS) {
1197                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1198                                         gesture->mode= 1;
1199                                         wm_gesture_tag_redraw(C);
1200                                 }
1201                         }
1202                         else {
1203                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1204                                         wm_gesture_end(C, op);
1205                                         return OPERATOR_FINISHED;
1206                                 }
1207                                 wm_gesture_end(C, op);
1208                                 return OPERATOR_CANCELLED;
1209                         }
1210                         break;
1211                 case ESCKEY:
1212                         wm_gesture_end(C, op);
1213                         return OPERATOR_CANCELLED;
1214         }
1215         return OPERATOR_RUNNING_MODAL;
1216 }
1217
1218 /* **************** circle gesture *************** */
1219 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1220
1221 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1222 {
1223         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1224         
1225         /* add modal handler */
1226         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1227         
1228         wm_gesture_tag_redraw(C);
1229         
1230         return OPERATOR_RUNNING_MODAL;
1231 }
1232
1233 static void gesture_circle_apply(bContext *C, wmOperator *op)
1234 {
1235         wmGesture *gesture= op->customdata;
1236         rcti *rect= gesture->customdata;
1237         
1238         /* operator arguments and storage. */
1239         RNA_int_set(op->ptr, "x", rect->xmin);
1240         RNA_int_set(op->ptr, "y", rect->ymin);
1241         RNA_int_set(op->ptr, "radius", rect->xmax);
1242         
1243         if(op->type->exec)
1244                 op->type->exec(C, op);
1245 }
1246
1247 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1248 {
1249         wmGesture *gesture= op->customdata;
1250         rcti *rect= gesture->customdata;
1251         int sx, sy;
1252         
1253         switch(event->type) {
1254                 case MOUSEMOVE:
1255                         
1256                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1257                         
1258                         rect->xmin= event->x - sx;
1259                         rect->ymin= event->y - sy;
1260                         
1261                         wm_gesture_tag_redraw(C);
1262                         
1263                         if(gesture->mode)
1264                                 gesture_circle_apply(C, op);
1265
1266                         break;
1267                 case WHEELUPMOUSE:
1268                         rect->xmax += 2 + rect->xmax/10;
1269                         wm_gesture_tag_redraw(C);
1270                         break;
1271                 case WHEELDOWNMOUSE:
1272                         rect->xmax -= 2 + rect->xmax/10;
1273                         if(rect->xmax < 1) rect->xmax= 1;
1274                         wm_gesture_tag_redraw(C);
1275                         break;
1276                 case LEFTMOUSE:
1277                 case MIDDLEMOUSE:
1278                 case RIGHTMOUSE:
1279                         if(event->val==0) {     /* key release */
1280                                 wm_gesture_end(C, op);
1281                                 return OPERATOR_FINISHED;
1282                         }
1283                         else {
1284                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1285                                         RNA_int_set(op->ptr, "event_type", event->type);
1286                                 
1287                                 /* apply first click */
1288                                 gesture_circle_apply(C, op);
1289                                 gesture->mode= 1;
1290                         }
1291                         break;
1292                 case ESCKEY:
1293                         wm_gesture_end(C, op);
1294                         return OPERATOR_CANCELLED;
1295         }
1296         return OPERATOR_RUNNING_MODAL;
1297 }
1298
1299 #if 0
1300 /* template to copy from */
1301 void WM_OT_circle_gesture(wmOperatorType *ot)
1302 {
1303         ot->name= "Circle Gesture";
1304         ot->idname= "WM_OT_circle_gesture";
1305         
1306         ot->invoke= WM_gesture_circle_invoke;
1307         ot->modal= WM_gesture_circle_modal;
1308         
1309         ot->poll= WM_operator_winactive;
1310         
1311         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1312         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1313         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1314
1315 }
1316 #endif
1317
1318 /* **************** Tweak gesture *************** */
1319
1320 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1321 {
1322         wmWindow *window= CTX_wm_window(C);
1323         wmGesture *gesture= window->tweak;
1324         rcti *rect= gesture->customdata;
1325         int sx, sy, val;
1326         
1327         switch(event->type) {
1328                 case MOUSEMOVE:
1329                         
1330                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1331                         
1332                         rect->xmax= event->x - sx;
1333                         rect->ymax= event->y - sy;
1334                         
1335                         if((val= wm_gesture_evaluate(C, gesture))) {
1336                                 wmEvent event;
1337
1338                                 event= *(window->eventstate);
1339                                 if(gesture->event_type==LEFTMOUSE)
1340                                         event.type= EVT_TWEAK_L;
1341                                 else if(gesture->event_type==RIGHTMOUSE)
1342                                         event.type= EVT_TWEAK_R;
1343                                 else
1344                                         event.type= EVT_TWEAK_M;
1345                                 event.val= val;
1346                                 /* mouse coords! */
1347                                 wm_event_add(window, &event);
1348                                 
1349                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1350                                 window->tweak= NULL;
1351                         }
1352                         
1353                         break;
1354                         
1355                 case LEFTMOUSE:
1356                 case RIGHTMOUSE:
1357                 case MIDDLEMOUSE:
1358                         if(gesture->event_type==event->type) {
1359                                 WM_gesture_end(C, gesture);
1360                                 window->tweak= NULL;
1361                                 
1362                                 /* when tweak fails we should give the other keymap entries a chance */
1363                                 event->val= KM_RELEASE;
1364                         }
1365                         break;
1366                 default:
1367                         WM_gesture_end(C, gesture);
1368                         window->tweak= NULL;
1369         }
1370 }
1371
1372 /* standard tweak, called after window handlers passed on event */
1373 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1374 {
1375         wmWindow *win= CTX_wm_window(C);
1376         
1377         if(win->tweak==NULL) {
1378                 if(CTX_wm_region(C)) {
1379                         if(event->val) { // pressed
1380                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1381                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1382                         }
1383                 }
1384         }
1385         else {
1386                 if(action==WM_HANDLER_BREAK) {
1387                         WM_gesture_end(C, win->tweak);
1388                         win->tweak= NULL;
1389                 }
1390                 else
1391                         tweak_gesture_modal(C, event);
1392         }
1393 }
1394
1395 /* *********************** lasso gesture ****************** */
1396
1397 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1398 {
1399         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1400         
1401         /* add modal handler */
1402         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1403         
1404         wm_gesture_tag_redraw(C);
1405         
1406         if( RNA_struct_find_property(op->ptr, "cursor") )
1407                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1408         
1409         return OPERATOR_RUNNING_MODAL;
1410 }
1411
1412 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1413 {
1414         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1415         
1416         /* add modal handler */
1417         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1418         
1419         wm_gesture_tag_redraw(C);
1420         
1421         if( RNA_struct_find_property(op->ptr, "cursor") )
1422                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1423         
1424         return OPERATOR_RUNNING_MODAL;
1425 }
1426
1427
1428 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1429 {
1430         wmGesture *gesture= op->customdata;
1431         PointerRNA itemptr;
1432         float loc[2];
1433         int i;
1434         short *lasso= gesture->customdata;
1435         
1436         /* operator storage as path. */
1437
1438         for(i=0; i<gesture->points; i++, lasso+=2) {
1439                 loc[0]= lasso[0];
1440                 loc[1]= lasso[1];
1441                 RNA_collection_add(op->ptr, "path", &itemptr);
1442                 RNA_float_set_array(&itemptr, "loc", loc);
1443         }
1444         
1445         wm_gesture_end(C, op);
1446                 
1447         if(op->type->exec)
1448                 op->type->exec(C, op);
1449         
1450 }
1451
1452 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1453 {
1454         wmGesture *gesture= op->customdata;
1455         int sx, sy;
1456         
1457         switch(event->type) {
1458                 case MOUSEMOVE:
1459                         
1460                         wm_gesture_tag_redraw(C);
1461                         
1462                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1463                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1464                                 short *lasso= gesture->customdata;
1465                                 lasso += 2 * gesture->points;
1466                                 lasso[0] = event->x - sx;
1467                                 lasso[1] = event->y - sy;
1468                                 gesture->points++;
1469                         }
1470                         else {
1471                                 gesture_lasso_apply(C, op, event->type);
1472                                 return OPERATOR_FINISHED;
1473                         }
1474                         break;
1475                         
1476                 case LEFTMOUSE:
1477                 case MIDDLEMOUSE:
1478                 case RIGHTMOUSE:
1479                         if(event->val==0) {     /* key release */
1480                                 gesture_lasso_apply(C, op, event->type);
1481                                 return OPERATOR_FINISHED;
1482                         }
1483                         break;
1484                 case ESCKEY:
1485                         wm_gesture_end(C, op);
1486                         return OPERATOR_CANCELLED;
1487         }
1488         return OPERATOR_RUNNING_MODAL;
1489 }
1490
1491 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1492 {
1493         return WM_gesture_lasso_modal(C, op, event);
1494 }
1495
1496 #if 0
1497 /* template to copy from */
1498
1499 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1500 {
1501         RNA_BEGIN(op->ptr, itemptr, "path") {
1502                 float loc[2];
1503                 
1504                 RNA_float_get_array(&itemptr, "loc", loc);
1505                 printf("Location: %f %f\n", loc[0], loc[1]);
1506         }
1507         RNA_END;
1508         
1509         return OPERATOR_FINISHED;
1510 }
1511
1512 void WM_OT_lasso_gesture(wmOperatorType *ot)
1513 {
1514         PropertyRNA *prop;
1515         
1516         ot->name= "Lasso Gesture";
1517         ot->idname= "WM_OT_lasso_gesture";
1518         
1519         ot->invoke= WM_gesture_lasso_invoke;
1520         ot->modal= WM_gesture_lasso_modal;
1521         ot->exec= gesture_lasso_exec;
1522         
1523         ot->poll= WM_operator_winactive;
1524         
1525         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1526         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1527 }
1528 #endif
1529
1530 /* *********************** radial control ****************** */
1531
1532 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1533
1534 typedef struct wmRadialControl {
1535         int mode;
1536         float initial_value, value, max_value;
1537         int initial_mouse[2];
1538         void *cursor;
1539         GLuint tex;
1540 } wmRadialControl;
1541
1542 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1543 {
1544         wmRadialControl *rc = (wmRadialControl*)customdata;
1545         ARegion *ar = CTX_wm_region(C);
1546         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1547
1548         /* Keep cursor in the original place */
1549         x = rc->initial_mouse[0] - ar->winrct.xmin;
1550         y = rc->initial_mouse[1] - ar->winrct.ymin;
1551
1552         glPushMatrix();
1553         
1554         glTranslatef((float)x, (float)y, 0.0f);
1555
1556         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1557                 r1= rc->value;
1558                 r2= rc->initial_value;
1559                 r3= r1;
1560         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1561                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1562                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1563                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1564         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1565                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1566                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1567                 angle = rc->value;
1568         }
1569
1570         glColor4ub(255, 255, 255, 128);
1571         glEnable( GL_LINE_SMOOTH );
1572         glEnable(GL_BLEND);
1573
1574         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1575                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1576
1577         if(rc->tex) {
1578                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1579
1580                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1581                         glRotatef(angle, 0, 0, 1);
1582                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1583                 }
1584
1585                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1586
1587                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1588                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1589
1590                 glEnable(GL_TEXTURE_2D);
1591                 glBegin(GL_QUADS);
1592                 glColor4f(0,0,0, str);
1593                 glTexCoord2f(0,0);
1594                 glVertex2f(-r3, -r3);
1595                 glTexCoord2f(1,0);
1596                 glVertex2f(r3, -r3);
1597                 glTexCoord2f(1,1);
1598                 glVertex2f(r3, r3);
1599                 glTexCoord2f(0,1);
1600                 glVertex2f(-r3, r3);
1601                 glEnd();
1602                 glDisable(GL_TEXTURE_2D);
1603         }
1604
1605         glColor4ub(255, 255, 255, 128); 
1606         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1607         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1608         glDisable(GL_BLEND);
1609         glDisable( GL_LINE_SMOOTH );
1610         
1611         glPopMatrix();
1612 }
1613
1614 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1615 {
1616         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1617         int mode, initial_mouse[2], delta[2];
1618         float dist;
1619         double new_value = RNA_float_get(op->ptr, "new_value");
1620         int ret = OPERATOR_RUNNING_MODAL;
1621
1622         mode = RNA_int_get(op->ptr, "mode");
1623         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1624
1625         switch(event->type) {
1626         case MOUSEMOVE:
1627                 delta[0]= initial_mouse[0] - event->x;
1628                 delta[1]= initial_mouse[1] - event->y;
1629                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1630
1631                 if(mode == WM_RADIALCONTROL_SIZE)
1632                         new_value = dist;
1633                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1634                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1635                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1636                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1637                 
1638                 if(event->ctrl) {
1639                         if(mode == WM_RADIALCONTROL_STRENGTH)
1640                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1641                         else
1642                                 new_value = ((int)new_value + 5) / 10*10;
1643                 }
1644                 
1645                 break;
1646         case ESCKEY:
1647         case RIGHTMOUSE:
1648                 ret = OPERATOR_CANCELLED;
1649                 break;
1650         case LEFTMOUSE:
1651         case PADENTER:
1652                 op->type->exec(C, op);
1653                 ret = OPERATOR_FINISHED;
1654                 break;
1655         }
1656
1657         /* Clamp */
1658         if(new_value > rc->max_value)
1659                 new_value = rc->max_value;
1660         else if(new_value < 0)
1661                 new_value = 0;
1662
1663         /* Update paint data */
1664         rc->value = new_value;
1665
1666         RNA_float_set(op->ptr, "new_value", new_value);
1667
1668         if(ret != OPERATOR_RUNNING_MODAL) {
1669                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1670                 MEM_freeN(rc);
1671         }
1672         
1673         ED_region_tag_redraw(CTX_wm_region(C));
1674
1675         return ret;
1676 }
1677
1678 /* Expects the operator customdata to be an ImBuf (or NULL) */
1679 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1680 {
1681         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1682         int mode = RNA_int_get(op->ptr, "mode");
1683         float initial_value = RNA_float_get(op->ptr, "initial_value");
1684         int mouse[2] = {event->x, event->y};
1685
1686         if(mode == WM_RADIALCONTROL_SIZE) {
1687                 rc->max_value = 200;
1688                 mouse[0]-= initial_value;
1689         }
1690         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1691                 rc->max_value = 1;
1692                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1693         }
1694         else if(mode == WM_RADIALCONTROL_ANGLE) {
1695                 rc->max_value = 360;
1696                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1697                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1698                 initial_value *= 180.0f/M_PI;
1699         }
1700
1701         if(op->customdata) {
1702                 ImBuf *im = (ImBuf*)op->customdata;
1703                 /* Build GL texture */
1704                 glGenTextures(1, &rc->tex);
1705                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1706                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1707                 MEM_freeN(im->rect_float);
1708                 MEM_freeN(im);
1709         }
1710
1711         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1712         RNA_float_set(op->ptr, "new_value", initial_value);
1713                 
1714         op->customdata = rc;
1715         rc->mode = mode;
1716         rc->initial_value = initial_value;
1717         rc->initial_mouse[0] = mouse[0];
1718         rc->initial_mouse[1] = mouse[1];
1719         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1720                                               wm_radial_control_paint, op->customdata);
1721
1722         /* add modal handler */
1723         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1724         
1725         WM_radial_control_modal(C, op, event);
1726         
1727         return OPERATOR_RUNNING_MODAL;
1728 }
1729
1730 /* Gets a descriptive string of the operation */
1731 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1732 {
1733         int mode = RNA_int_get(op->ptr, "mode");
1734         float v = RNA_float_get(op->ptr, "new_value");
1735
1736         if(mode == WM_RADIALCONTROL_SIZE)
1737                 sprintf(str, "Size: %d", (int)v);
1738         else if(mode == WM_RADIALCONTROL_STRENGTH)
1739                 sprintf(str, "Strength: %d", (int)v);
1740         else if(mode == WM_RADIALCONTROL_ANGLE)
1741                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1742 }
1743
1744 /** Important: this doesn't define an actual operator, it
1745     just sets up the common parts of the radial control op. **/
1746 void WM_OT_radial_control_partial(wmOperatorType *ot)
1747 {
1748         static EnumPropertyItem prop_mode_items[] = {
1749                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
1750                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
1751                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
1752                 {0, NULL, 0, NULL, NULL}};
1753
1754         /* Should be set in custom invoke() */
1755         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1756
1757         /* Set internally, should be used in custom exec() to get final value */
1758         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1759
1760         /* Should be set before calling operator */
1761         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1762
1763         /* Internal */
1764         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1765 }
1766
1767 /* ************************** timer for testing ***************** */
1768
1769 /* uses no type defines, fully local testing function anyway... ;) */
1770
1771 static int ten_timer_exec(bContext *C, wmOperator *op)
1772 {
1773         ARegion *ar= CTX_wm_region(C);
1774         double stime= PIL_check_seconds_timer();
1775         int type = RNA_int_get(op->ptr, "type");
1776         int a, time;
1777         char tmpstr[128];
1778         
1779         WM_cursor_wait(1);
1780         
1781         for(a=0; a<10; a++) {
1782                 if (type==0) {
1783                         ED_region_do_draw(C, ar);
1784                 } 
1785                 else if (type==1) {
1786                         wmWindow *win= CTX_wm_window(C);
1787                         
1788                         ED_region_tag_redraw(ar);
1789                         wm_draw_update(C);
1790                         
1791                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1792                 }
1793                 else if (type==2) {
1794                         wmWindow *win= CTX_wm_window(C);
1795                         ScrArea *sa;
1796                         
1797                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1798                                 ED_area_tag_redraw(sa);
1799                         wm_draw_update(C);
1800                         
1801                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1802                 }
1803                 else if (type==3) {
1804                         Scene *scene= CTX_data_scene(C);
1805                         
1806                         if(a & 1) scene->r.cfra--;
1807                         else scene->r.cfra++;
1808                         scene_update_for_newframe(scene, scene->lay);
1809                 }
1810                 else {
1811                         ED_undo_pop(C);
1812                         ED_undo_redo(C);
1813                 }
1814         }
1815         
1816         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1817         
1818         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1819         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1820         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1821         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1822         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1823         
1824         WM_cursor_wait(0);
1825         
1826         uiPupMenuNotice(C, tmpstr);
1827         
1828         return OPERATOR_FINISHED;
1829 }
1830
1831 static void WM_OT_ten_timer(wmOperatorType *ot)
1832 {
1833         static EnumPropertyItem prop_type_items[] = {
1834         {0, "DRAW", 0, "Draw Region", ""},
1835         {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
1836         {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
1837         {3, "ANIMSTEP", 0, "Anim Step", ""},
1838         {4, "UNDO", 0, "Undo/Redo", ""},
1839         {0, NULL, 0, NULL, NULL}};
1840         
1841         ot->name= "Ten Timer";
1842         ot->idname= "WM_OT_ten_timer";
1843         
1844         ot->invoke= WM_menu_invoke;
1845         ot->exec= ten_timer_exec;
1846         ot->poll= WM_operator_winactive;
1847         
1848         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1849
1850 }
1851
1852
1853
1854 /* ******************************************************* */
1855  
1856 /* called on initialize WM_exit() */
1857 void wm_operatortype_free(void)
1858 {
1859         wmOperatorType *ot;
1860         
1861         for(ot= global_ops.first; ot; ot= ot->next)
1862                 if(ot->macro.first)
1863                         wm_operatortype_free_macro(ot);
1864         
1865         BLI_freelistN(&global_ops);
1866 }
1867
1868 /* called on initialize WM_init() */
1869 void wm_operatortype_init(void)
1870 {
1871         WM_operatortype_append(WM_OT_window_duplicate);
1872         WM_operatortype_append(WM_OT_read_homefile);
1873         WM_operatortype_append(WM_OT_save_homefile);
1874         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1875         WM_operatortype_append(WM_OT_exit_blender);
1876         WM_operatortype_append(WM_OT_open_recentfile);
1877         WM_operatortype_append(WM_OT_open_mainfile);
1878         WM_operatortype_append(WM_OT_recover_last_session);
1879         WM_operatortype_append(WM_OT_jobs_timer);
1880         WM_operatortype_append(WM_OT_save_as_mainfile);
1881         WM_operatortype_append(WM_OT_save_mainfile);
1882         WM_operatortype_append(WM_OT_ten_timer);
1883         WM_operatortype_append(WM_OT_debug_menu);
1884         WM_operatortype_append(WM_OT_search_menu);
1885 }
1886
1887 /* default keymap for windows and screens, only call once per WM */
1888 void wm_window_keymap(wmWindowManager *wm)
1889 {
1890         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1891         
1892         /* items to make WM work */
1893         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1894         
1895         /* note, this doesn't replace existing keymap items */
1896         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1897         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1898         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1899         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1900         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1901         WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1902         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1903         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_SHIFT, 0);
1904         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1905
1906         /* debug/testing */
1907         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1908         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1909         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, KM_CTRL, 0);
1910         
1911 }
1912