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