UI: report if WM_OT_call_menu uses an unknown menu
[blender.git] / source / blender / windowmanager / intern / wm_operators.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/windowmanager/intern/wm_operators.c
28  *  \ingroup wm
29  *
30  * Functions for dealing with wmOperator, adding, removing, calling
31  * as well as some generic operators and shared operator properties.
32  */
33
34
35 #include <float.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <stddef.h>
40 #include <assert.h>
41
42 #include "GHOST_C-api.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_ID.h"
47 #include "DNA_object_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51 #include "DNA_windowmanager_types.h"
52 #include "DNA_mesh_types.h" /* only for USE_BMESH_SAVE_AS_COMPAT */
53
54 #include "BLF_translation.h"
55
56 #include "PIL_time.h"
57
58 #include "BLI_blenlib.h"
59 #include "BLI_dynstr.h" /*for WM_operator_pystring */
60 #include "BLI_math.h"
61 #include "BLI_utildefines.h"
62 #include "BLI_ghash.h"
63
64 #include "BLO_readfile.h"
65
66 #include "BKE_autoexec.h"
67 #include "BKE_blender.h"
68 #include "BKE_brush.h"
69 #include "BKE_context.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_idprop.h"
72 #include "BKE_image.h"
73 #include "BKE_library.h"
74 #include "BKE_global.h"
75 #include "BKE_main.h"
76 #include "BKE_material.h"
77 #include "BKE_report.h"
78 #include "BKE_scene.h"
79 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
80 #include "BKE_utildefines.h"
81
82 #include "BKE_idcode.h"
83
84 #include "BIF_gl.h"
85 #include "BIF_glutil.h" /* for paint cursor */
86 #include "BLF_api.h"
87
88 #include "IMB_colormanagement.h"
89 #include "IMB_imbuf_types.h"
90 #include "IMB_imbuf.h"
91
92 #include "ED_screen.h"
93 #include "ED_util.h"
94 #include "ED_object.h"
95 #include "ED_view3d.h"
96
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99 #include "RNA_enum_types.h"
100
101 #include "UI_interface.h"
102 #include "UI_resources.h"
103
104 #include "WM_api.h"
105 #include "WM_types.h"
106
107 #include "wm.h"
108 #include "wm_draw.h"
109 #include "wm_event_system.h"
110 #include "wm_event_types.h"
111 #include "wm_files.h"
112 #include "wm_subwindow.h"
113 #include "wm_window.h"
114
115 static GHash *global_ops_hash = NULL;
116
117 #define UNDOCUMENTED_OPERATOR_TIP N_("(undocumented operator)")
118
119 /* ************ operator API, exported ********** */
120
121
122 wmOperatorType *WM_operatortype_find(const char *idname, bool quiet)
123 {
124         if (idname[0]) {
125                 wmOperatorType *ot;
126
127                 /* needed to support python style names without the _OT_ syntax */
128                 char idname_bl[OP_MAX_TYPENAME];
129                 WM_operator_bl_idname(idname_bl, idname);
130
131                 ot = BLI_ghash_lookup(global_ops_hash, idname_bl);
132                 if (ot) {
133                         return ot;
134                 }
135
136                 if (!quiet) {
137                         printf("search for unknown operator '%s', '%s'\n", idname_bl, idname);
138                 }
139         }
140         else {
141                 if (!quiet) {
142                         printf("search for empty operator\n");
143                 }
144         }
145
146         return NULL;
147 }
148
149 /* caller must free */
150 GHashIterator *WM_operatortype_iter(void)
151 {
152         return BLI_ghashIterator_new(global_ops_hash);
153 }
154
155 /* all ops in 1 list (for time being... needs evaluation later) */
156 void WM_operatortype_append(void (*opfunc)(wmOperatorType *))
157 {
158         wmOperatorType *ot;
159         
160         ot = MEM_callocN(sizeof(wmOperatorType), "operatortype");
161         ot->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_OperatorProperties);
162         /* Set the default i18n context now, so that opfunc can redefine it if needed! */
163         RNA_def_struct_translation_context(ot->srna, BLF_I18NCONTEXT_OPERATOR_DEFAULT);
164         opfunc(ot);
165
166         if (ot->name == NULL) {
167                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
168                 ot->name = N_("Dummy Name");
169         }
170
171         /* XXX All ops should have a description but for now allow them not to. */
172         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description : UNDOCUMENTED_OPERATOR_TIP);
173         RNA_def_struct_identifier(ot->srna, ot->idname);
174
175         BLI_ghash_insert(global_ops_hash, (void *)ot->idname, ot);
176 }
177
178 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType *, void *), void *userdata)
179 {
180         wmOperatorType *ot;
181
182         ot = MEM_callocN(sizeof(wmOperatorType), "operatortype");
183         ot->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_OperatorProperties);
184         /* Set the default i18n context now, so that opfunc can redefine it if needed! */
185         RNA_def_struct_translation_context(ot->srna, BLF_I18NCONTEXT_OPERATOR_DEFAULT);
186         opfunc(ot, userdata);
187         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description : UNDOCUMENTED_OPERATOR_TIP);
188         RNA_def_struct_identifier(ot->srna, ot->idname);
189
190         BLI_ghash_insert(global_ops_hash, (void *)ot->idname, ot);
191 }
192
193 /* ********************* macro operator ******************** */
194
195 typedef struct {
196         int retval;
197 } MacroData;
198
199 static void wm_macro_start(wmOperator *op)
200 {
201         if (op->customdata == NULL) {
202                 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
203         }
204 }
205
206 static int wm_macro_end(wmOperator *op, int retval)
207 {
208         if (retval & OPERATOR_CANCELLED) {
209                 MacroData *md = op->customdata;
210
211                 if (md->retval & OPERATOR_FINISHED) {
212                         retval |= OPERATOR_FINISHED;
213                         retval &= ~OPERATOR_CANCELLED;
214                 }
215         }
216
217         /* if modal is ending, free custom data */
218         if (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED)) {
219                 if (op->customdata) {
220                         MEM_freeN(op->customdata);
221                         op->customdata = NULL;
222                 }
223         }
224
225         return retval;
226 }
227
228 /* macro exec only runs exec calls */
229 static int wm_macro_exec(bContext *C, wmOperator *op)
230 {
231         wmOperator *opm;
232         int retval = OPERATOR_FINISHED;
233         
234         wm_macro_start(op);
235
236         for (opm = op->macro.first; opm; opm = opm->next) {
237                 
238                 if (opm->type->exec) {
239                         retval = opm->type->exec(C, opm);
240                         OPERATOR_RETVAL_CHECK(retval);
241                 
242                         if (retval & OPERATOR_FINISHED) {
243                                 MacroData *md = op->customdata;
244                                 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
245                         }
246                         else {
247                                 break; /* operator didn't finish, end macro */
248                         }
249                 }
250                 else {
251                         printf("%s: '%s' cant exec macro\n", __func__, opm->type->idname);
252                 }
253         }
254         
255         return wm_macro_end(op, retval);
256 }
257
258 static int wm_macro_invoke_internal(bContext *C, wmOperator *op, const wmEvent *event, wmOperator *opm)
259 {
260         int retval = OPERATOR_FINISHED;
261
262         /* start from operator received as argument */
263         for (; opm; opm = opm->next) {
264                 if (opm->type->invoke)
265                         retval = opm->type->invoke(C, opm, event);
266                 else if (opm->type->exec)
267                         retval = opm->type->exec(C, opm);
268
269                 OPERATOR_RETVAL_CHECK(retval);
270
271                 BLI_movelisttolist(&op->reports->list, &opm->reports->list);
272                 
273                 if (retval & OPERATOR_FINISHED) {
274                         MacroData *md = op->customdata;
275                         md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
276                 }
277                 else {
278                         break; /* operator didn't finish, end macro */
279                 }
280         }
281
282         return wm_macro_end(op, retval);
283 }
284
285 static int wm_macro_invoke(bContext *C, wmOperator *op, const wmEvent *event)
286 {
287         wm_macro_start(op);
288         return wm_macro_invoke_internal(C, op, event, op->macro.first);
289 }
290
291 static int wm_macro_modal(bContext *C, wmOperator *op, const wmEvent *event)
292 {
293         wmOperator *opm = op->opm;
294         int retval = OPERATOR_FINISHED;
295         
296         if (opm == NULL)
297                 printf("%s: macro error, calling NULL modal()\n", __func__);
298         else {
299                 retval = opm->type->modal(C, opm, event);
300                 OPERATOR_RETVAL_CHECK(retval);
301
302                 /* if we're halfway through using a tool and cancel it, clear the options [#37149] */
303                 if (retval & OPERATOR_CANCELLED) {
304                         WM_operator_properties_clear(opm->ptr);
305                 }
306
307                 /* if this one is done but it's not the last operator in the macro */
308                 if ((retval & OPERATOR_FINISHED) && opm->next) {
309                         MacroData *md = op->customdata;
310
311                         md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
312
313                         retval = wm_macro_invoke_internal(C, op, event, opm->next);
314
315                         /* if new operator is modal and also added its own handler */
316                         if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
317                                 wmWindow *win = CTX_wm_window(C);
318                                 wmEventHandler *handler;
319
320                                 handler = BLI_findptr(&win->modalhandlers, op, offsetof(wmEventHandler, op));
321                                 if (handler) {
322                                         BLI_remlink(&win->modalhandlers, handler);
323                                         wm_event_free_handler(handler);
324                                 }
325
326                                 /* if operator is blocking, grab cursor
327                                  * This may end up grabbing twice, but we don't care.
328                                  * */
329                                 if (op->opm->type->flag & OPTYPE_BLOCKING) {
330                                         int bounds[4] = {-1, -1, -1, -1};
331                                         int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
332
333                                         if (wrap) {
334                                                 ARegion *ar = CTX_wm_region(C);
335                                                 if (ar) {
336                                                         bounds[0] = ar->winrct.xmin;
337                                                         bounds[1] = ar->winrct.ymax;
338                                                         bounds[2] = ar->winrct.xmax;
339                                                         bounds[3] = ar->winrct.ymin;
340                                                 }
341                                         }
342
343                                         WM_cursor_grab_enable(CTX_wm_window(C), wrap, false, bounds);
344                                 }
345                         }
346                 }
347         }
348
349         return wm_macro_end(op, retval);
350 }
351
352 static void wm_macro_cancel(bContext *C, wmOperator *op)
353 {
354         /* call cancel on the current modal operator, if any */
355         if (op->opm && op->opm->type->cancel) {
356                 op->opm->type->cancel(C, op->opm);
357         }
358
359         wm_macro_end(op, OPERATOR_CANCELLED);
360 }
361
362 /* Names have to be static for now */
363 wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, const char *description, int flag)
364 {
365         wmOperatorType *ot;
366         
367         if (WM_operatortype_find(idname, TRUE)) {
368                 printf("%s: macro error: operator %s exists\n", __func__, idname);
369                 return NULL;
370         }
371         
372         ot = MEM_callocN(sizeof(wmOperatorType), "operatortype");
373         ot->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_OperatorProperties);
374         
375         ot->idname = idname;
376         ot->name = name;
377         ot->description = description;
378         ot->flag = OPTYPE_MACRO | flag;
379         
380         ot->exec = wm_macro_exec;
381         ot->invoke = wm_macro_invoke;
382         ot->modal = wm_macro_modal;
383         ot->cancel = wm_macro_cancel;
384         ot->poll = NULL;
385
386         if (!ot->description) /* XXX All ops should have a description but for now allow them not to. */
387                 ot->description = UNDOCUMENTED_OPERATOR_TIP;
388         
389         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
390         RNA_def_struct_identifier(ot->srna, ot->idname);
391         /* Use i18n context from ext.srna if possible (py operators). */
392         RNA_def_struct_translation_context(ot->srna, ot->ext.srna ? RNA_struct_translation_context(ot->ext.srna) :
393                                                                     BLF_I18NCONTEXT_OPERATOR_DEFAULT);
394
395         BLI_ghash_insert(global_ops_hash, (void *)ot->idname, ot);
396
397         return ot;
398 }
399
400 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType *, void *), void *userdata)
401 {
402         wmOperatorType *ot;
403
404         ot = MEM_callocN(sizeof(wmOperatorType), "operatortype");
405         ot->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_OperatorProperties);
406
407         ot->flag = OPTYPE_MACRO;
408         ot->exec = wm_macro_exec;
409         ot->invoke = wm_macro_invoke;
410         ot->modal = wm_macro_modal;
411         ot->cancel = wm_macro_cancel;
412         ot->poll = NULL;
413
414         if (!ot->description)
415                 ot->description = UNDOCUMENTED_OPERATOR_TIP;
416
417         /* Set the default i18n context now, so that opfunc can redefine it if needed! */
418         RNA_def_struct_translation_context(ot->srna, BLF_I18NCONTEXT_OPERATOR_DEFAULT);
419         opfunc(ot, userdata);
420
421         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
422         RNA_def_struct_identifier(ot->srna, ot->idname);
423
424         BLI_ghash_insert(global_ops_hash, (void *)ot->idname, ot);
425 }
426
427 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
428 {
429         wmOperatorTypeMacro *otmacro = MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
430
431         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
432
433         /* do this on first use, since operatordefinitions might have been not done yet */
434         WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
435         WM_operator_properties_sanitize(otmacro->ptr, 1);
436
437         BLI_addtail(&ot->macro, otmacro);
438
439         {
440                 /* operator should always be found but in the event its not. don't segfault */
441                 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
442                 if (otsub) {
443                         RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
444                                                 otsub->name, otsub->description);
445                 }
446         }
447
448         return otmacro;
449 }
450
451 static void wm_operatortype_free_macro(wmOperatorType *ot)
452 {
453         wmOperatorTypeMacro *otmacro;
454         
455         for (otmacro = ot->macro.first; otmacro; otmacro = otmacro->next) {
456                 if (otmacro->ptr) {
457                         WM_operator_properties_free(otmacro->ptr);
458                         MEM_freeN(otmacro->ptr);
459                 }
460         }
461         BLI_freelistN(&ot->macro);
462 }
463
464 void WM_operatortype_remove_ptr(wmOperatorType *ot)
465 {
466         BLI_assert(ot == WM_operatortype_find(ot->idname, false));
467
468         RNA_struct_free(&BLENDER_RNA, ot->srna);
469
470         if (ot->last_properties) {
471                 IDP_FreeProperty(ot->last_properties);
472                 MEM_freeN(ot->last_properties);
473         }
474
475         if (ot->macro.first)
476                 wm_operatortype_free_macro(ot);
477
478         BLI_ghash_remove(global_ops_hash, (void *)ot->idname, NULL, NULL);
479
480         WM_keyconfig_update_operatortype();
481
482         MEM_freeN(ot);
483 }
484
485 bool WM_operatortype_remove(const char *idname)
486 {
487         wmOperatorType *ot = WM_operatortype_find(idname, 0);
488
489         if (ot == NULL)
490                 return false;
491
492         WM_operatortype_remove_ptr(ot);
493
494         return true;
495 }
496
497 /* SOME_OT_op -> some.op */
498 void WM_operator_py_idname(char *to, const char *from)
499 {
500         char *sep = strstr(from, "_OT_");
501         if (sep) {
502                 int ofs = (sep - from);
503                 
504                 /* note, we use ascii tolower instead of system tolower, because the
505                  * latter depends on the locale, and can lead to idname mistmatch */
506                 memcpy(to, from, sizeof(char) * ofs);
507                 BLI_ascii_strtolower(to, ofs);
508
509                 to[ofs] = '.';
510                 BLI_strncpy(to + (ofs + 1), sep + 4, OP_MAX_TYPENAME - (ofs + 1));
511         }
512         else {
513                 /* should not happen but support just in case */
514                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
515         }
516 }
517
518 /* some.op -> SOME_OT_op */
519 void WM_operator_bl_idname(char *to, const char *from)
520 {
521         if (from) {
522                 char *sep = strchr(from, '.');
523
524                 if (sep) {
525                         int ofs = (sep - from);
526
527                         memcpy(to, from, sizeof(char) * ofs);
528                         BLI_ascii_strtoupper(to, ofs);
529                         strcpy(to + ofs, "_OT_");
530                         strcpy(to + (ofs + 4), sep + 1);
531                 }
532                 else {
533                         /* should not happen but support just in case */
534                         BLI_strncpy(to, from, OP_MAX_TYPENAME);
535                 }
536         }
537         else
538                 to[0] = 0;
539 }
540
541 /* Print a string representation of the operator, with the args that it runs so python can run it again.
542  *
543  * When calling from an existing wmOperator, better to use simple version:
544  *     WM_operator_pystring(C, op);
545  *
546  * Note: both op and opptr may be NULL (op is only used for macro operators).
547  */
548 char *WM_operator_pystring_ex(bContext *C, wmOperator *op, const bool all_args, const bool macro_args,
549                               wmOperatorType *ot, PointerRNA *opptr)
550 {
551         char idname_py[OP_MAX_TYPENAME];
552
553         /* for building the string */
554         DynStr *dynstr = BLI_dynstr_new();
555         char *cstring;
556         char *cstring_args;
557
558         /* arbitrary, but can get huge string with stroke painting otherwise */
559         int max_prop_length = 10;
560
561         WM_operator_py_idname(idname_py, ot->idname);
562         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
563
564         if (op && op->macro.first) {
565                 /* Special handling for macros, else we only get default values in this case... */
566                 wmOperator *opm;
567                 bool first_op = true;
568
569                 opm = macro_args ? op->macro.first : NULL;
570
571                 for (; opm; opm = opm->next) {
572                         PointerRNA *opmptr = opm->ptr;
573                         PointerRNA opmptr_default;
574                         if (opmptr == NULL) {
575                                 WM_operator_properties_create_ptr(&opmptr_default, opm->type);
576                                 opmptr = &opmptr_default;
577                         }
578
579                         cstring_args = RNA_pointer_as_string_id(C, opmptr);
580                         if (first_op) {
581                                 BLI_dynstr_appendf(dynstr, "%s=%s", opm->type->idname, cstring_args);
582                                 first_op = false;
583                         }
584                         else {
585                                 BLI_dynstr_appendf(dynstr, ", %s=%s", opm->type->idname, cstring_args);
586                         }
587                         MEM_freeN(cstring_args);
588
589                         if (opmptr == &opmptr_default) {
590                                 WM_operator_properties_free(&opmptr_default);
591                         }
592                 }
593         }
594         else {
595                 /* only to get the orginal props for comparisons */
596                 PointerRNA opptr_default;
597                 const bool macro_args_test = ot->macro.first ? macro_args : true;
598
599                 if (opptr == NULL) {
600                         WM_operator_properties_create_ptr(&opptr_default, ot);
601                         opptr = &opptr_default;
602                 }
603
604                 cstring_args = RNA_pointer_as_string_keywords(C, opptr, false, all_args, macro_args_test, max_prop_length);
605                 BLI_dynstr_append(dynstr, cstring_args);
606                 MEM_freeN(cstring_args);
607
608                 if (opptr == &opptr_default) {
609                         WM_operator_properties_free(&opptr_default);
610                 }
611         }
612
613         BLI_dynstr_append(dynstr, ")");
614
615         cstring = BLI_dynstr_get_cstring(dynstr);
616         BLI_dynstr_free(dynstr);
617         return cstring;
618 }
619
620 char *WM_operator_pystring(bContext *C, wmOperator *op,
621                            const bool all_args, const bool macro_args)
622 {
623         return WM_operator_pystring_ex(C, op, all_args, macro_args, op->type, op->ptr);
624 }
625
626
627 /**
628  * \return true if the string was shortened
629  */
630 bool WM_operator_pystring_abbreviate(char *str, int str_len_max)
631 {
632         const int str_len = strlen(str);
633         const char *parens_start = strchr(str, '(');
634
635         if (parens_start) {
636                 const int parens_start_pos = parens_start - str;
637                 const char *parens_end = strrchr(parens_start + 1, ')');
638
639                 if (parens_end) {
640                         const int parens_len = parens_end - parens_start;
641
642                         if (parens_len > str_len_max) {
643                                 const char *comma_first = strchr(parens_start, ',');
644
645                                 /* truncate after the first comma */
646                                 if (comma_first) {
647                                         const char end_str[] = " ... )";
648                                         const int end_str_len = sizeof(end_str) - 1;
649
650                                         /* leave a place for the first argument*/
651                                         const int new_str_len = (comma_first - parens_start) + 1;
652
653                                         if (str_len >= new_str_len + parens_start_pos + end_str_len + 1) {
654                                                 /* append " ... )" to the string after the comma */
655                                                 memcpy(str + new_str_len + parens_start_pos, end_str, end_str_len + 1);
656
657                                                 return true;
658                                         }
659                                 }
660                         }
661                 }
662         }
663
664         return false;
665 }
666
667 /* return NULL if no match is found */
668 #if 0
669 static char *wm_prop_pystring_from_context(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
670 {
671
672         /* loop over all context items and do 2 checks
673          *
674          * - see if the pointer is in the context.
675          * - see if the pointers ID is in the context.
676          */
677
678         /* don't get from the context store since this is normally set only for the UI and not usable elsewhere */
679         ListBase lb = CTX_data_dir_get_ex(C, FALSE, TRUE, TRUE);
680         LinkData *link;
681
682         const char *member_found = NULL;
683         const char *member_id = NULL;
684
685         char *prop_str = NULL;
686         char *ret = NULL;
687
688
689         for (link = lb.first; link; link = link->next) {
690                 const char *identifier = link->data;
691                 PointerRNA ctx_item_ptr = {{0}} // CTX_data_pointer_get(C, identifier); // XXX, this isnt working
692
693                 if (ctx_item_ptr.type == NULL) {
694                         continue;
695                 }
696
697                 if (ptr->id.data == ctx_item_ptr.id.data) {
698                         if ((ptr->data == ctx_item_ptr.data) &&
699                             (ptr->type == ctx_item_ptr.type))
700                         {
701                                 /* found! */
702                                 member_found = identifier;
703                                 break;
704                         }
705                         else if (RNA_struct_is_ID(ctx_item_ptr.type)) {
706                                 /* we found a reference to this ID,
707                                  * so fallback to it if there is no direct reference */
708                                 member_id = identifier;
709                         }
710                 }
711         }
712
713         if (member_found) {
714                 prop_str = RNA_path_property_py(ptr, prop, index);
715                 if (prop_str) {
716                         ret = BLI_sprintfN("bpy.context.%s.%s", member_found, prop_str);
717                         MEM_freeN(prop_str);
718                 }
719         }
720         else if (member_id) {
721                 prop_str = RNA_path_struct_property_py(ptr, prop, index);
722                 if (prop_str) {
723                         ret = BLI_sprintfN("bpy.context.%s.%s", member_id, prop_str);
724                         MEM_freeN(prop_str);
725                 }
726         }
727
728         BLI_freelistN(&lb);
729
730         return ret;
731 }
732 #else
733
734 /* use hard coded checks for now */
735 static char *wm_prop_pystring_from_context(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
736 {
737         const char *member_id = NULL;
738
739         char *prop_str = NULL;
740         char *ret = NULL;
741
742         if (ptr->id.data) {
743
744 #define CTX_TEST_PTR_ID(C, member, idptr) \
745                 { \
746                         const char *ctx_member = member; \
747                         PointerRNA ctx_item_ptr = CTX_data_pointer_get(C, ctx_member); \
748                         if (ctx_item_ptr.id.data == idptr) { \
749                                 member_id = ctx_member; \
750                                 break; \
751                         } \
752                 } (void)0
753
754 #define CTX_TEST_PTR_ID_CAST(C, member, member_full, cast, idptr) \
755                 { \
756                         const char *ctx_member = member; \
757                         const char *ctx_member_full = member_full; \
758                         PointerRNA ctx_item_ptr = CTX_data_pointer_get(C, ctx_member); \
759                         if (ctx_item_ptr.id.data && cast(ctx_item_ptr.id.data) == idptr) { \
760                                 member_id = ctx_member_full; \
761                                 break; \
762                         } \
763                 } (void)0
764
765 #define CTX_TEST_PTR_DATA_TYPE(C, member, rna_type, rna_ptr, dataptr_cmp) \
766                 { \
767                         const char *ctx_member = member; \
768                         if (RNA_struct_is_a((ptr)->type, &(rna_type)) && (ptr)->data == (dataptr_cmp)) { \
769                                 member_id = ctx_member; \
770                                 break; \
771                         } \
772                 } (void)0
773
774                 switch (GS(((ID *)ptr->id.data)->name)) {
775                         case ID_SCE:
776                         {
777                                 CTX_TEST_PTR_ID(C, "scene", ptr->id.data);
778                                 break;
779                         }
780                         case ID_OB:
781                         {
782                                 CTX_TEST_PTR_ID(C, "object", ptr->id.data);
783                                 break;
784                         }
785                         /* from rna_Main_objects_new */
786                         case OB_DATA_SUPPORT_ID_CASE:
787                         {
788 #define ID_CAST_OBDATA(id_pt) (((Object *)(id_pt))->data)
789                                 CTX_TEST_PTR_ID_CAST(C, "object", "object.data", ID_CAST_OBDATA, ptr->id.data);
790                                 break;
791 #undef ID_CAST_OBDATA
792                         }
793                         case ID_MA:
794                         {
795 #define ID_CAST_OBMATACT(id_pt) (give_current_material(((Object *)id_pt), ((Object *)id_pt)->actcol))
796                                 CTX_TEST_PTR_ID_CAST(C, "object", "object.active_material", ID_CAST_OBMATACT, ptr->id.data);
797                                 break;
798 #undef ID_CAST_OBMATACT
799                         }
800                         case ID_WO:
801                         {
802 #define ID_CAST_SCENEWORLD(id_pt) (((Scene *)(id_pt))->world)
803                                 CTX_TEST_PTR_ID_CAST(C, "scene", "scene.world", ID_CAST_SCENEWORLD, ptr->id.data);
804                                 break;
805 #undef ID_CAST_SCENEWORLD
806                         }
807                         case ID_SCR:
808                         {
809                                 CTX_TEST_PTR_ID(C, "screen", ptr->id.data);
810
811                                 CTX_TEST_PTR_DATA_TYPE(C, "space_data", RNA_Space, ptr, CTX_wm_space_data(C));
812                                 CTX_TEST_PTR_DATA_TYPE(C, "area", RNA_Area, ptr, CTX_wm_area(C));
813                                 CTX_TEST_PTR_DATA_TYPE(C, "region", RNA_Region, ptr, CTX_wm_region(C));
814
815                                 break;
816                         }
817                 }
818
819                 if (member_id) {
820                         prop_str = RNA_path_struct_property_py(ptr, prop, index);
821                         if (prop_str) {
822                                 ret = BLI_sprintfN("bpy.context.%s.%s", member_id, prop_str);
823                                 MEM_freeN(prop_str);
824                         }
825                 }
826 #undef CTX_TEST_PTR_ID
827 #undef CTX_TEST_PTR_ID_CAST
828         }
829
830         return ret;
831 }
832 #endif
833
834 char *WM_prop_pystring_assign(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
835 {
836         char *lhs, *rhs, *ret;
837
838         lhs = C ? wm_prop_pystring_from_context(C, ptr, prop, index) : NULL;
839
840         if (lhs == NULL) {
841                 /* fallback to bpy.data.foo[id] if we dont find in the context */
842                 lhs = RNA_path_full_property_py(ptr, prop, index);
843         }
844
845         if (!lhs) {
846                 return NULL;
847         }
848
849         rhs = RNA_property_as_string(C, ptr, prop, index, INT_MAX);
850         if (!rhs) {
851                 MEM_freeN(lhs);
852                 return NULL;
853         }
854
855         ret = BLI_sprintfN("%s = %s", lhs, rhs);
856         MEM_freeN(lhs);
857         MEM_freeN(rhs);
858         return ret;
859 }
860
861 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
862 {
863         RNA_pointer_create(NULL, ot->srna, NULL, ptr);
864 }
865
866 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
867 {
868         wmOperatorType *ot = WM_operatortype_find(opstring, 0);
869
870         if (ot)
871                 WM_operator_properties_create_ptr(ptr, ot);
872         else
873                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
874 }
875
876 /* similar to the function above except its uses ID properties
877  * used for keymaps and macros */
878 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
879 {
880         if (*properties == NULL) {
881                 IDPropertyTemplate val = {0};
882                 *properties = IDP_New(IDP_GROUP, &val, "wmOpItemProp");
883         }
884
885         if (*ptr == NULL) {
886                 *ptr = MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
887                 WM_operator_properties_create(*ptr, opstring);
888         }
889
890         (*ptr)->data = *properties;
891
892 }
893
894 void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
895 {
896         RNA_STRUCT_BEGIN (ptr, prop)
897         {
898                 switch (RNA_property_type(prop)) {
899                         case PROP_ENUM:
900                                 if (no_context)
901                                         RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
902                                 else
903                                         RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
904                                 break;
905                         case PROP_POINTER:
906                         {
907                                 StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
908
909                                 /* recurse into operator properties */
910                                 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
911                                         PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
912                                         WM_operator_properties_sanitize(&opptr, no_context);
913                                 }
914                                 break;
915                         }
916                         default:
917                                 break;
918                 }
919         }
920         RNA_STRUCT_END;
921 }
922
923
924 /** set all props to their default,
925  * \param do_update Only update un-initialized props.
926  *
927  * \note, theres nothing specific to operators here.
928  * this could be made a general function.
929  */
930 bool WM_operator_properties_default(PointerRNA *ptr, const bool do_update)
931 {
932         bool changed = false;
933         RNA_STRUCT_BEGIN (ptr, prop)
934         {
935                 switch (RNA_property_type(prop)) {
936                         case PROP_POINTER:
937                         {
938                                 StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
939                                 if (ptype != &RNA_Struct) {
940                                         PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
941                                         changed |= WM_operator_properties_default(&opptr, do_update);
942                                 }
943                                 break;
944                         }
945                         default:
946                                 if ((do_update == false) || (RNA_property_is_set(ptr, prop) == FALSE)) {
947                                         if (RNA_property_reset(ptr, prop, -1)) {
948                                                 changed = true;
949                                         }
950                                 }
951                                 break;
952                 }
953         }
954         RNA_STRUCT_END;
955
956         return changed;
957 }
958
959 /* remove all props without PROP_SKIP_SAVE */
960 void WM_operator_properties_reset(wmOperator *op)
961 {
962         if (op->ptr->data) {
963                 PropertyRNA *iterprop;
964                 iterprop = RNA_struct_iterator_property(op->type->srna);
965
966                 RNA_PROP_BEGIN (op->ptr, itemptr, iterprop)
967                 {
968                         PropertyRNA *prop = itemptr.data;
969
970                         if ((RNA_property_flag(prop) & PROP_SKIP_SAVE) == 0) {
971                                 const char *identifier = RNA_property_identifier(prop);
972                                 RNA_struct_idprops_unset(op->ptr, identifier);
973                         }
974                 }
975                 RNA_PROP_END;
976         }
977 }
978
979 void WM_operator_properties_clear(PointerRNA *ptr)
980 {
981         IDProperty *properties = ptr->data;
982
983         if (properties) {
984                 IDP_ClearProperty(properties);
985         }
986 }
987
988 void WM_operator_properties_free(PointerRNA *ptr)
989 {
990         IDProperty *properties = ptr->data;
991
992         if (properties) {
993                 IDP_FreeProperty(properties);
994                 MEM_freeN(properties);
995                 ptr->data = NULL; /* just in case */
996         }
997 }
998
999 /* ************ default op callbacks, exported *********** */
1000
1001 void WM_operator_view3d_unit_defaults(struct bContext *C, struct wmOperator *op)
1002 {
1003         if (op->flag & OP_IS_INVOKE) {
1004                 Scene *scene = CTX_data_scene(C);
1005                 View3D *v3d = CTX_wm_view3d(C);
1006
1007                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
1008
1009                 /* always run, so the values are initialized,
1010                  * otherwise we may get differ behavior when (dia != 1.0) */
1011                 RNA_STRUCT_BEGIN (op->ptr, prop)
1012                 {
1013                         if (RNA_property_type(prop) == PROP_FLOAT) {
1014                                 PropertySubType pstype = RNA_property_subtype(prop);
1015                                 if (pstype == PROP_DISTANCE) {
1016                                         /* we don't support arrays yet */
1017                                         BLI_assert(RNA_property_array_check(prop) == FALSE);
1018                                         /* initialize */
1019                                         if (!RNA_property_is_set_ex(op->ptr, prop, FALSE)) {
1020                                                 const float value = RNA_property_float_get_default(op->ptr, prop) * dia;
1021                                                 RNA_property_float_set(op->ptr, prop, value);
1022                                         }
1023                                 }
1024                         }
1025                 }
1026                 RNA_STRUCT_END;
1027         }
1028 }
1029
1030 int WM_operator_smooth_viewtx_get(const wmOperator *op)
1031 {
1032         return (op->flag & OP_IS_INVOKE) ? U.smooth_viewtx : 0;
1033 }
1034
1035 /* invoke callback, uses enum property named "type" */
1036 int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1037 {
1038         PropertyRNA *prop = op->type->prop;
1039         uiPopupMenu *pup;
1040         uiLayout *layout;
1041
1042         if (prop == NULL) {
1043                 printf("%s: %s has no enum property set\n", __func__, op->type->idname);
1044         }
1045         else if (RNA_property_type(prop) != PROP_ENUM) {
1046                 printf("%s: %s \"%s\" is not an enum property\n",
1047                        __func__, op->type->idname, RNA_property_identifier(prop));
1048         }
1049         else if (RNA_property_is_set(op->ptr, prop)) {
1050                 const int retval = op->type->exec(C, op);
1051                 OPERATOR_RETVAL_CHECK(retval);
1052                 return retval;
1053         }
1054         else {
1055                 pup = uiPupMenuBegin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
1056                 layout = uiPupMenuLayout(pup);
1057                 /* set this so the default execution context is the same as submenus */
1058                 uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_REGION_WIN);
1059                 uiItemsFullEnumO(layout, op->type->idname, RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
1060                 uiPupMenuEnd(C, pup);
1061         }
1062
1063         return OPERATOR_CANCELLED;
1064 }
1065
1066
1067 /* generic enum search invoke popup */
1068 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
1069 {
1070         static char search[256] = "";
1071         wmEvent event;
1072         wmWindow *win = CTX_wm_window(C);
1073         uiBlock *block;
1074         uiBut *but;
1075         wmOperator *op = (wmOperator *)arg_op;
1076
1077         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1078         uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_MOVEMOUSE_QUIT | UI_BLOCK_SEARCH_MENU);
1079
1080 #if 0 /* ok, this isn't so easy... */
1081         uiDefBut(block, LABEL, 0, RNA_struct_ui_name(op->type->srna), 10, 10, 180, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1082 #endif
1083         but = uiDefSearchButO_ptr(block, op->type, op->ptr->data, search, 0, ICON_VIEWZOOM, sizeof(search),
1084                                   10, 10, 9 * UI_UNIT_X, UI_UNIT_Y, 0, 0, "");
1085
1086         /* fake button, it holds space for search items */
1087         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxHeight(), uiSearchBoxWidth(), uiSearchBoxHeight(), NULL, 0, 0, 0, 0, NULL);
1088
1089         uiPopupBoundsBlock(block, 6, 0, -UI_UNIT_Y); /* move it downwards, mouse over button */
1090         uiEndBlock(C, block);
1091
1092         wm_event_init_from_window(win, &event);
1093         event.type = EVT_BUT_OPEN;
1094         event.val = KM_PRESS;
1095         event.customdata = but;
1096         event.customdatafree = FALSE;
1097         wm_event_add(win, &event);
1098
1099         return block;
1100 }
1101
1102
1103 int WM_enum_search_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1104 {
1105         uiPupBlock(C, wm_enum_search_menu, op);
1106         return OPERATOR_CANCELLED;
1107 }
1108
1109 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
1110 int WM_operator_confirm_message_ex(bContext *C, wmOperator *op,
1111                                    const char *title, const int icon,
1112                                    const char *message)
1113 {
1114         uiPopupMenu *pup;
1115         uiLayout *layout;
1116         IDProperty *properties = op->ptr->data;
1117
1118         if (properties && properties->len)
1119                 properties = IDP_CopyProperty(op->ptr->data);
1120         else
1121                 properties = NULL;
1122
1123         pup = uiPupMenuBegin(C, title, icon);
1124         layout = uiPupMenuLayout(pup);
1125         uiItemFullO_ptr(layout, op->type, message, ICON_NONE, properties, WM_OP_EXEC_REGION_WIN, 0);
1126         uiPupMenuEnd(C, pup);
1127         
1128         return OPERATOR_CANCELLED;
1129 }
1130
1131 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
1132 {
1133         return WM_operator_confirm_message_ex(C, op, IFACE_("OK?"), ICON_QUESTION, message);
1134 }
1135
1136 int WM_operator_confirm(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1137 {
1138         return WM_operator_confirm_message(C, op, NULL);
1139 }
1140
1141 /* op->invoke, opens fileselect if path property not set, otherwise executes */
1142 int WM_operator_filesel(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1143 {
1144         if (RNA_struct_property_is_set(op->ptr, "filepath")) {
1145                 return WM_operator_call_notest(C, op); /* call exec direct */
1146         }
1147         else {
1148                 WM_event_add_fileselect(C, op);
1149                 return OPERATOR_RUNNING_MODAL;
1150         }
1151 }
1152
1153 bool WM_operator_filesel_ensure_ext_imtype(wmOperator *op, const struct ImageFormatData *im_format)
1154 {
1155         PropertyRNA *prop;
1156         char filepath[FILE_MAX];
1157         /* dont NULL check prop, this can only run on ops with a 'filepath' */
1158         prop = RNA_struct_find_property(op->ptr, "filepath");
1159         RNA_property_string_get(op->ptr, prop, filepath);
1160         if (BKE_add_image_extension(filepath, im_format)) {
1161                 RNA_property_string_set(op->ptr, prop, filepath);
1162                 /* note, we could check for and update 'filename' here,
1163                  * but so far nothing needs this. */
1164                 return TRUE;
1165         }
1166         return FALSE;
1167 }
1168
1169 /* default properties for fileselect */
1170 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display)
1171 {
1172         PropertyRNA *prop;
1173
1174         static EnumPropertyItem file_display_items[] = {
1175                 {FILE_DEFAULTDISPLAY, "FILE_DEFAULTDISPLAY", 0, "Default", "Automatically determine display type for files"},
1176                 {FILE_SHORTDISPLAY, "FILE_SHORTDISPLAY", ICON_SHORTDISPLAY, "Short List", "Display files as short list"},
1177                 {FILE_LONGDISPLAY, "FILE_LONGDISPLAY", ICON_LONGDISPLAY, "Long List", "Display files as a detailed list"},
1178                 {FILE_IMGDISPLAY, "FILE_IMGDISPLAY", ICON_IMGDISPLAY, "Thumbnails", "Display files as thumbnails"},
1179                 {0, NULL, 0, NULL, NULL}
1180         };
1181
1182
1183         if (flag & WM_FILESEL_FILEPATH)
1184                 RNA_def_string_file_path(ot->srna, "filepath", NULL, FILE_MAX, "File Path", "Path to file");
1185
1186         if (flag & WM_FILESEL_DIRECTORY)
1187                 RNA_def_string_dir_path(ot->srna, "directory", NULL, FILE_MAX, "Directory", "Directory of the file");
1188
1189         if (flag & WM_FILESEL_FILENAME)
1190                 RNA_def_string_file_name(ot->srna, "filename", NULL, FILE_MAX, "File Name", "Name of the file");
1191
1192         if (flag & WM_FILESEL_FILES)
1193                 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1194
1195         if (action == FILE_SAVE) {
1196                 /* note, this is only used to check if we should highlight the filename area red when the
1197                  * filepath is an existing file. */
1198                 prop = RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
1199                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1200         }
1201         
1202         prop = RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
1203         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1204         prop = RNA_def_boolean(ot->srna, "filter_backup", (filter & BLENDERFILE_BACKUP), "Filter .blend files", "");
1205         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1206         prop = RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
1207         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1208         prop = RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
1209         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1210         prop = RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
1211         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1212         prop = RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
1213         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1214         prop = RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
1215         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1216         prop = RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
1217         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1218         prop = RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
1219         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1220         prop = RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
1221         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1222         prop = RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
1223         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1224
1225         prop = RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL,
1226                            "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
1227                            FILE_LOADLIB, FILE_SPECIAL);
1228         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1229
1230         if (flag & WM_FILESEL_RELPATH)
1231                 RNA_def_boolean(ot->srna, "relative_path", TRUE, "Relative Path", "Select the file relative to the blend file");
1232
1233         prop = RNA_def_enum(ot->srna, "display_type", file_display_items, display, "Display Type", "");
1234         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1235 }
1236
1237 static void wm_operator_properties_select_action_ex(wmOperatorType *ot, int default_action,
1238                                                     const EnumPropertyItem *select_actions)
1239 {
1240         RNA_def_enum(ot->srna, "action", select_actions, default_action, "Action", "Selection action to execute");
1241 }
1242
1243 void WM_operator_properties_select_action(wmOperatorType *ot, int default_action)
1244 {
1245         static EnumPropertyItem select_actions[] = {
1246                 {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
1247                 {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
1248                 {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
1249                 {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
1250                 {0, NULL, 0, NULL, NULL}
1251         };
1252
1253         wm_operator_properties_select_action_ex(ot, default_action, select_actions);
1254 }
1255
1256 /**
1257  * only SELECT/DESELECT
1258  */
1259 void WM_operator_properties_select_action_simple(wmOperatorType *ot, int default_action)
1260 {
1261         static EnumPropertyItem select_actions[] = {
1262                 {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
1263                 {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
1264                 {0, NULL, 0, NULL, NULL}
1265         };
1266
1267         wm_operator_properties_select_action_ex(ot, default_action, select_actions);
1268 }
1269
1270 void WM_operator_properties_select_all(wmOperatorType *ot)
1271 {
1272         WM_operator_properties_select_action(ot, SEL_TOGGLE);
1273 }
1274
1275 void WM_operator_properties_border(wmOperatorType *ot)
1276 {
1277         PropertyRNA *prop;
1278
1279         prop = RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
1280         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1281         prop = RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
1282         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1283         prop = RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
1284         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1285         prop = RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
1286         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1287 }
1288
1289 void WM_operator_properties_border_to_rcti(struct wmOperator *op, rcti *rect)
1290 {
1291         rect->xmin = RNA_int_get(op->ptr, "xmin");
1292         rect->ymin = RNA_int_get(op->ptr, "ymin");
1293         rect->xmax = RNA_int_get(op->ptr, "xmax");
1294         rect->ymax = RNA_int_get(op->ptr, "ymax");
1295 }
1296
1297 void WM_operator_properties_gesture_border(wmOperatorType *ot, bool extend)
1298 {
1299         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
1300
1301         WM_operator_properties_border(ot);
1302
1303         if (extend) {
1304                 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
1305         }
1306 }
1307
1308 void WM_operator_properties_mouse_select(wmOperatorType *ot)
1309 {
1310         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first");
1311         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Remove from selection");
1312         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle Selection", "Toggle the selection");
1313 }
1314
1315 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
1316 {
1317         PropertyRNA *prop;
1318
1319         prop = RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
1320         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1321         prop = RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
1322         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1323         prop = RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
1324         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1325         prop = RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
1326         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1327         
1328         if (cursor) {
1329                 prop = RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX,
1330                                    "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
1331                 RNA_def_property_flag(prop, PROP_HIDDEN);
1332         }
1333 }
1334
1335
1336 /* op->poll */
1337 int WM_operator_winactive(bContext *C)
1338 {
1339         if (CTX_wm_window(C) == NULL) return 0;
1340         return 1;
1341 }
1342
1343 /* return FALSE, if the UI should be disabled */
1344 bool WM_operator_check_ui_enabled(const bContext *C, const char *idname)
1345 {
1346         wmWindowManager *wm = CTX_wm_manager(C);
1347         Scene *scene = CTX_data_scene(C);
1348
1349         return !(ED_undo_valid(C, idname) == 0 || WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY));
1350 }
1351
1352 wmOperator *WM_operator_last_redo(const bContext *C)
1353 {
1354         wmWindowManager *wm = CTX_wm_manager(C);
1355         wmOperator *op;
1356
1357         /* only for operators that are registered and did an undo push */
1358         for (op = wm->operators.last; op; op = op->prev)
1359                 if ((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
1360                         break;
1361
1362         return op;
1363 }
1364
1365 static void wm_block_redo_cb(bContext *C, void *arg_op, int UNUSED(arg_event))
1366 {
1367         wmOperator *op = arg_op;
1368
1369         if (op == WM_operator_last_redo(C)) {
1370                 /* operator was already executed once? undo & repeat */
1371                 ED_undo_operator_repeat(C, op);
1372         }
1373         else {
1374                 /* operator not executed yet, call it */
1375                 ED_undo_push_op(C, op);
1376                 wm_operator_register(C, op);
1377
1378                 WM_operator_repeat(C, op);
1379         }
1380 }
1381
1382 static void wm_block_redo_cancel_cb(bContext *C, void *arg_op)
1383 {
1384         wmOperator *op = arg_op;
1385
1386         /* if operator never got executed, free it */
1387         if (op != WM_operator_last_redo(C))
1388                 WM_operator_free(op);
1389 }
1390
1391 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
1392 {
1393         wmOperator *op = arg_op;
1394         uiBlock *block;
1395         uiLayout *layout;
1396         uiStyle *style = UI_GetStyle();
1397         int width = 15 * UI_UNIT_X;
1398
1399         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1400         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1401         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN | UI_BLOCK_MOVEMOUSE_QUIT);
1402
1403         /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
1404          * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
1405         assert(op->type->flag & OPTYPE_REGISTER);
1406
1407         uiBlockSetHandleFunc(block, wm_block_redo_cb, arg_op);
1408         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, UI_UNIT_Y, 0, style);
1409
1410         if (op == WM_operator_last_redo(C))
1411                 if (!WM_operator_check_ui_enabled(C, op->type->name))
1412                         uiLayoutSetEnabled(layout, FALSE);
1413
1414         if (op->type->flag & OPTYPE_MACRO) {
1415                 for (op = op->macro.first; op; op = op->next) {
1416                         uiItemL(layout, RNA_struct_ui_name(op->type->srna), ICON_NONE);
1417                         uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
1418                 }
1419         }
1420         else {
1421                 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
1422         }
1423         
1424         uiPopupBoundsBlock(block, 4, 0, 0);
1425         uiEndBlock(C, block);
1426
1427         return block;
1428 }
1429
1430 typedef struct wmOpPopUp {
1431         wmOperator *op;
1432         int width;
1433         int height;
1434         int free_op;
1435 } wmOpPopUp;
1436
1437 /* Only invoked by OK button in popups created with wm_block_dialog_create() */
1438 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
1439 {
1440         wmOpPopUp *data = arg1;
1441         uiBlock *block = arg2;
1442
1443         WM_operator_call_ex(C, data->op, true);
1444
1445         /* let execute handle freeing it */
1446         //data->free_op = FALSE;
1447         //data->op = NULL;
1448
1449         /* in this case, wm_operator_ui_popup_cancel wont run */
1450         MEM_freeN(data);
1451
1452         uiPupBlockClose(C, block);
1453 }
1454
1455 static void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
1456 {
1457         wmOperator *op = op_ptr;
1458         if (op->type->check) {
1459                 if (op->type->check(C, op)) {
1460                         /* refresh */
1461                 }
1462         }
1463 }
1464
1465 /* Dialogs are popups that require user verification (click OK) before exec */
1466 static uiBlock *wm_block_dialog_create(bContext *C, ARegion *ar, void *userData)
1467 {
1468         wmOpPopUp *data = userData;
1469         wmOperator *op = data->op;
1470         uiBlock *block;
1471         uiLayout *layout;
1472         uiStyle *style = UI_GetStyle();
1473
1474         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1475         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1476
1477         /* intentionally don't use 'UI_BLOCK_MOVEMOUSE_QUIT', some dialogues have many items
1478          * where quitting by accident is very annoying */
1479         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1480
1481         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, 0, style);
1482         
1483         uiBlockSetFunc(block, dialog_check_cb, op, NULL);
1484
1485         uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
1486         
1487         /* clear so the OK button is left alone */
1488         uiBlockSetFunc(block, NULL, NULL, NULL);
1489
1490         /* new column so as not to interfere with custom layouts [#26436] */
1491         {
1492                 uiBlock *col_block;
1493                 uiLayout *col;
1494                 uiBut *btn;
1495
1496                 col = uiLayoutColumn(layout, FALSE);
1497                 col_block = uiLayoutGetBlock(col);
1498                 /* Create OK button, the callback of which will execute op */
1499                 btn = uiDefBut(col_block, BUT, 0, IFACE_("OK"), 0, -30, 0, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
1500                 uiButSetFunc(btn, dialog_exec_cb, data, col_block);
1501         }
1502
1503         /* center around the mouse */
1504         uiPopupBoundsBlock(block, 4, data->width / -2, data->height / 2);
1505         uiEndBlock(C, block);
1506
1507         return block;
1508 }
1509
1510 static uiBlock *wm_operator_ui_create(bContext *C, ARegion *ar, void *userData)
1511 {
1512         wmOpPopUp *data = userData;
1513         wmOperator *op = data->op;
1514         uiBlock *block;
1515         uiLayout *layout;
1516         uiStyle *style = UI_GetStyle();
1517
1518         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1519         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1520         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN | UI_BLOCK_MOVEMOUSE_QUIT);
1521
1522         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, 0, style);
1523
1524         /* since ui is defined the auto-layout args are not used */
1525         uiLayoutOperatorButs(C, layout, op, NULL, 'V', 0);
1526
1527         uiPopupBoundsBlock(block, 4, 0, 0);
1528         uiEndBlock(C, block);
1529
1530         return block;
1531 }
1532
1533 static void wm_operator_ui_popup_cancel(struct bContext *C, void *userData)
1534 {
1535         wmOpPopUp *data = userData;
1536         wmOperator *op = data->op;
1537
1538         if (op) {
1539                 if (op->type->cancel) {
1540                         op->type->cancel(C, op);
1541                 }
1542
1543                 if (data->free_op) {
1544                         WM_operator_free(op);
1545                 }
1546         }
1547
1548         MEM_freeN(data);
1549 }
1550
1551 static void wm_operator_ui_popup_ok(struct bContext *C, void *arg, int retval)
1552 {
1553         wmOpPopUp *data = arg;
1554         wmOperator *op = data->op;
1555
1556         if (op && retval > 0)
1557                 WM_operator_call_ex(C, op, true);
1558         
1559         MEM_freeN(data);
1560 }
1561
1562 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1563 {
1564         wmOpPopUp *data = MEM_callocN(sizeof(wmOpPopUp), "WM_operator_ui_popup");
1565         data->op = op;
1566         data->width = width;
1567         data->height = height;
1568         data->free_op = TRUE; /* if this runs and gets registered we may want not to free it */
1569         uiPupBlockEx(C, wm_operator_ui_create, NULL, wm_operator_ui_popup_cancel, data);
1570         return OPERATOR_RUNNING_MODAL;
1571 }
1572
1573 /**
1574  * For use by #WM_operator_props_popup_call, #WM_operator_props_popup only.
1575  *
1576  * \note operator menu needs undo flag enabled , for redo callback */
1577 static int wm_operator_props_popup_ex(bContext *C, wmOperator *op,
1578                                       const bool do_call, const bool do_redo)
1579 {
1580         if ((op->type->flag & OPTYPE_REGISTER) == 0) {
1581                 BKE_reportf(op->reports, RPT_ERROR,
1582                             "Operator '%s' does not have register enabled, incorrect invoke function", op->type->idname);
1583                 return OPERATOR_CANCELLED;
1584         }
1585
1586         /* if we don't have global undo, we can't do undo push for automatic redo,
1587          * so we require manual OK clicking in this popup */
1588         if (!do_redo || !(U.uiflag & USER_GLOBALUNDO))
1589                 return WM_operator_props_dialog_popup(C, op, 15 * UI_UNIT_X, UI_UNIT_Y);
1590
1591         uiPupBlockEx(C, wm_block_create_redo, NULL, wm_block_redo_cancel_cb, op);
1592
1593         if (do_call)
1594                 wm_block_redo_cb(C, op, 0);
1595
1596         return OPERATOR_RUNNING_MODAL;
1597 }
1598
1599 /* Same as WM_operator_props_popup but don't use operator redo.
1600  * just wraps WM_operator_props_dialog_popup.
1601  */
1602 int WM_operator_props_popup_confirm(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1603 {
1604         return wm_operator_props_popup_ex(C, op, false, false);
1605 }
1606
1607 /* Same as WM_operator_props_popup but call the operator first,
1608  * This way - the button values correspond to the result of the operator.
1609  * Without this, first access to a button will make the result jump,
1610  * see [#32452] */
1611 int WM_operator_props_popup_call(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1612 {
1613         return wm_operator_props_popup_ex(C, op, true, true);
1614 }
1615
1616 int WM_operator_props_popup(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1617 {
1618         return wm_operator_props_popup_ex(C, op, false, true);
1619 }
1620
1621 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1622 {
1623         wmOpPopUp *data = MEM_callocN(sizeof(wmOpPopUp), "WM_operator_props_dialog_popup");
1624         
1625         data->op = op;
1626         data->width = width;
1627         data->height = height;
1628         data->free_op = TRUE; /* if this runs and gets registered we may want not to free it */
1629
1630         /* op is not executed until popup OK but is clicked */
1631         uiPupBlockEx(C, wm_block_dialog_create, wm_operator_ui_popup_ok, wm_operator_ui_popup_cancel, data);
1632
1633         return OPERATOR_RUNNING_MODAL;
1634 }
1635
1636 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1637 {
1638         /* CTX_wm_reports(C) because operator is on stack, not active in event system */
1639         if ((op->type->flag & OPTYPE_REGISTER) == 0) {
1640                 BKE_reportf(CTX_wm_reports(C), RPT_ERROR,
1641                             "Operator redo '%s' does not have register enabled, incorrect invoke function", op->type->idname);
1642                 return OPERATOR_CANCELLED;
1643         }
1644         if (op->type->poll && op->type->poll(C) == 0) {
1645                 BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "Operator redo '%s': wrong context", op->type->idname);
1646                 return OPERATOR_CANCELLED;
1647         }
1648         
1649         uiPupBlock(C, wm_block_create_redo, op);
1650
1651         return OPERATOR_CANCELLED;
1652 }
1653
1654 /* ***************** Debug menu ************************* */
1655
1656 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1657 {
1658         G.debug_value = RNA_int_get(op->ptr, "debug_value");
1659         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1660         WM_event_add_notifier(C, NC_WINDOW, NULL);
1661
1662         return OPERATOR_FINISHED;
1663 }
1664
1665 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1666 {
1667         RNA_int_set(op->ptr, "debug_value", G.debug_value);
1668         return WM_operator_props_dialog_popup(C, op, 9 * UI_UNIT_X, UI_UNIT_Y);
1669 }
1670
1671 static void WM_OT_debug_menu(wmOperatorType *ot)
1672 {
1673         ot->name = "Debug Menu";
1674         ot->idname = "WM_OT_debug_menu";
1675         ot->description = "Open a popup to set the debug level";
1676         
1677         ot->invoke = wm_debug_menu_invoke;
1678         ot->exec = wm_debug_menu_exec;
1679         ot->poll = WM_operator_winactive;
1680         
1681         RNA_def_int(ot->srna, "debug_value", 0, SHRT_MIN, SHRT_MAX, "Debug Value", "", -10000, 10000);
1682 }
1683
1684 /* ***************** Operator defaults ************************* */
1685 static int wm_operator_defaults_exec(bContext *C, wmOperator *op)
1686 {
1687         PointerRNA ptr = CTX_data_pointer_get_type(C, "active_operator", &RNA_Operator);
1688
1689         if (!ptr.data) {
1690                 BKE_report(op->reports, RPT_ERROR, "No operator in context");
1691                 return OPERATOR_CANCELLED;
1692         }
1693
1694         WM_operator_properties_reset((wmOperator *)ptr.data);
1695         return OPERATOR_FINISHED;
1696 }
1697
1698 /* used by operator preset menu. pre-2.65 this was a 'Reset' button */
1699 static void WM_OT_operator_defaults(wmOperatorType *ot)
1700 {
1701         ot->name = "Restore Defaults";
1702         ot->idname = "WM_OT_operator_defaults";
1703         ot->description = "Set the active operator to its default values";
1704
1705         ot->exec = wm_operator_defaults_exec;
1706
1707         ot->flag = OPTYPE_INTERNAL;
1708 }
1709
1710 /* ***************** Splash Screen ************************* */
1711
1712 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1713 {
1714         uiPupBlockClose(C, arg_block);
1715 }
1716
1717 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1718
1719 /* XXX: hack to refresh splash screen with updated preset menu name,
1720  * since popup blocks don't get regenerated like panels do */
1721 static void wm_block_splash_refreshmenu(bContext *UNUSED(C), void *UNUSED(arg_block), void *UNUSED(arg))
1722 {
1723         /* ugh, causes crashes in other buttons, disabling for now until 
1724          * a better fix */
1725 #if 0
1726         uiPupBlockClose(C, arg_block);
1727         uiPupBlock(C, wm_block_create_splash, NULL);
1728 #endif
1729 }
1730
1731 static int wm_resource_check_prev(void)
1732 {
1733
1734         const char *res = BLI_get_folder_version(BLENDER_RESOURCE_PATH_USER, BLENDER_VERSION, true);
1735
1736         // if (res) printf("USER: %s\n", res);
1737
1738 #if 0 /* ignore the local folder */
1739         if (res == NULL) {
1740                 /* with a local dir, copying old files isn't useful since local dir get priority for config */
1741                 res = BLI_get_folder_version(BLENDER_RESOURCE_PATH_LOCAL, BLENDER_VERSION, true);
1742         }
1743 #endif
1744
1745         // if (res) printf("LOCAL: %s\n", res);
1746         if (res) {
1747                 return FALSE;
1748         }
1749         else {
1750                 return (BLI_get_folder_version(BLENDER_RESOURCE_PATH_USER, BLENDER_VERSION - 1, true) != NULL);
1751         }
1752 }
1753
1754 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1755 {
1756         uiBlock *block;
1757         uiBut *but;
1758         uiLayout *layout, *split, *col;
1759         uiStyle *style = UI_GetStyle();
1760         struct RecentFile *recent;
1761         int i;
1762         MenuType *mt = WM_menutype_find("USERPREF_MT_splash", TRUE);
1763         char url[96];
1764         char file[FILE_MAX];
1765
1766 #ifndef WITH_HEADLESS
1767         extern char datatoc_splash_png[];
1768         extern int datatoc_splash_png_size;
1769
1770         ImBuf *ibuf = IMB_ibImageFromMemory((unsigned char *)datatoc_splash_png,
1771                                             datatoc_splash_png_size, IB_rect, NULL, "<splash screen>");
1772 #else
1773         ImBuf *ibuf = NULL;
1774 #endif
1775
1776
1777 #ifdef WITH_BUILDINFO
1778         int label_delta = 0;
1779         int hash_width, date_width;
1780         char date_buf[128] = "\0";
1781         char hash_buf[128] = "\0";
1782         extern unsigned long build_commit_timestamp;
1783         extern char build_hash[], build_commit_date[], build_commit_time[], build_branch[];
1784
1785         /* Builds made from tag only shows tag sha */
1786         BLI_snprintf(hash_buf, sizeof(hash_buf), "Hash: %s", build_hash);
1787         BLI_snprintf(date_buf, sizeof(date_buf), "Date: %s %s", build_commit_date, build_commit_time);
1788         
1789         BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.pixelsize * U.dpi);
1790         hash_width = (int)BLF_width(style->widgetlabel.uifont_id, hash_buf, sizeof(hash_buf)) + U.widget_unit;
1791         date_width = (int)BLF_width(style->widgetlabel.uifont_id, date_buf, sizeof(date_buf)) + U.widget_unit;
1792 #endif  /* WITH_BUILDINFO */
1793
1794         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1795
1796         /* note on UI_BLOCK_NO_WIN_CLIP, the window size is not always synchronized
1797          * with the OS when the splash shows, window clipping in this case gives
1798          * ugly results and clipping the splash isn't useful anyway, just disable it [#32938] */
1799         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN | UI_BLOCK_NO_WIN_CLIP);
1800         
1801         /* XXX splash scales with pixelsize, should become widget-units */
1802         but = uiDefBut(block, BUT_IMAGE, 0, "", 0, 0.5f * U.widget_unit, U.pixelsize * 501, U.pixelsize * 282, ibuf, 0.0, 0.0, 0, 0, ""); /* button owns the imbuf now */
1803         uiButSetFunc(but, wm_block_splash_close, block, NULL);
1804         uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1805         
1806 #ifdef WITH_BUILDINFO
1807         if (build_commit_timestamp != 0) {
1808                 uiDefBut(block, LABEL, 0, date_buf, U.pixelsize * 494 - date_width, U.pixelsize * 270, date_width, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1809                 label_delta = 12;
1810         }
1811         uiDefBut(block, LABEL, 0, hash_buf, U.pixelsize * 494 - hash_width, U.pixelsize * (270 - label_delta), hash_width, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1812
1813         if (!STREQ(build_branch, "master")) {
1814                 char branch_buf[128] = "\0";
1815                 int branch_width;
1816                 BLI_snprintf(branch_buf, sizeof(branch_buf), "Branch: %s", build_branch);
1817                 branch_width = (int)BLF_width(style->widgetlabel.uifont_id, branch_buf, sizeof(branch_buf)) + U.widget_unit;
1818                 uiDefBut(block, LABEL, 0, branch_buf, U.pixelsize * 494 - branch_width, U.pixelsize * (258 - label_delta), branch_width, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
1819         }
1820 #endif  /* WITH_BUILDINFO */
1821         
1822         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, U.pixelsize * 480, U.pixelsize * 110, 0, style);
1823         
1824         uiBlockSetEmboss(block, UI_EMBOSS);
1825         /* show the splash menu (containing interaction presets), using python */
1826         if (mt) {
1827                 Menu menu = {NULL};
1828                 menu.layout = layout;
1829                 menu.type = mt;
1830                 mt->draw(C, &menu);
1831
1832 //              wmWindowManager *wm = CTX_wm_manager(C);
1833 //              uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, ICON_NONE);
1834         }
1835         
1836         uiBlockSetEmboss(block, UI_EMBOSSP);
1837         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1838         
1839         split = uiLayoutSplit(layout, 0.0f, FALSE);
1840         col = uiLayoutColumn(split, FALSE);
1841         uiItemL(col, IFACE_("Links"), ICON_NONE);
1842         uiItemStringO(col, IFACE_("Donations"), ICON_URL, "WM_OT_url_open", "url",
1843                       "http://www.blender.org/foundation/donation-payment/");
1844         uiItemStringO(col, IFACE_("Credits"), ICON_URL, "WM_OT_url_open", "url",
1845                       "http://www.blender.org/about/credits/");
1846         uiItemStringO(col, IFACE_("Release Log"), ICON_URL, "WM_OT_url_open", "url",
1847                       "http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.70");
1848         uiItemStringO(col, IFACE_("Manual"), ICON_URL, "WM_OT_url_open", "url",
1849                       "http://wiki.blender.org/index.php/Doc:2.6/Manual");
1850         uiItemStringO(col, IFACE_("Blender Website"), ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org");
1851         uiItemStringO(col, IFACE_("User Community"), ICON_URL, "WM_OT_url_open", "url",
1852                       "http://www.blender.org/community/user-community");
1853         if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "release")) {
1854                 BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d"
1855                                                STRINGIFY(BLENDER_VERSION_CHAR) "_release",
1856                              BLENDER_VERSION / 100, BLENDER_VERSION % 100);
1857         }
1858         else {
1859                 BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d",
1860                              BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION);
1861         }
1862         uiItemStringO(col, IFACE_("Python API Reference"), ICON_URL, "WM_OT_url_open", "url", url);
1863         uiItemL(col, "", ICON_NONE);
1864
1865         col = uiLayoutColumn(split, FALSE);
1866
1867         if (wm_resource_check_prev()) {
1868                 uiItemO(col, NULL, ICON_NEW, "WM_OT_copy_prev_settings");
1869                 uiItemS(col);
1870         }
1871
1872         uiItemL(col, IFACE_("Recent"), ICON_NONE);
1873         for (recent = G.recent_files.first, i = 0; (i < 5) && (recent); recent = recent->next, i++) {
1874                 BLI_split_file_part(recent->filepath, file, sizeof(file));
1875                 if (BLO_has_bfile_extension(file))
1876                         uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1877                 else
1878                         uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BACKUP, "WM_OT_open_mainfile", "filepath", recent->filepath);
1879         }
1880
1881         uiItemS(col);
1882         uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1883         uiItemL(col, "", ICON_NONE);
1884         
1885         uiCenteredBoundsBlock(block, 0);
1886         uiEndBlock(C, block);
1887         
1888         return block;
1889 }
1890
1891 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
1892 {
1893         uiPupBlock(C, wm_block_create_splash, NULL);
1894         
1895         return OPERATOR_FINISHED;
1896 }
1897
1898 static void WM_OT_splash(wmOperatorType *ot)
1899 {
1900         ot->name = "Splash Screen";
1901         ot->idname = "WM_OT_splash";
1902         ot->description = "Open the splash screen with release info";
1903         
1904         ot->invoke = wm_splash_invoke;
1905         ot->poll = WM_operator_winactive;
1906 }
1907
1908
1909 /* ***************** Search menu ************************* */
1910 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1911 {
1912         static char search[256] = "";
1913         wmEvent event;
1914         wmWindow *win = CTX_wm_window(C);
1915         uiBlock *block;
1916         uiBut *but;
1917         
1918         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1919         uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_MOVEMOUSE_QUIT | UI_BLOCK_SEARCH_MENU);
1920         
1921         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 10, 9 * UI_UNIT_X, UI_UNIT_Y, 0, 0, "");
1922         uiOperatorSearch_But(but);
1923         
1924         /* fake button, it holds space for search items */
1925         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxHeight(), uiSearchBoxWidth(), uiSearchBoxHeight(), NULL, 0, 0, 0, 0, NULL);
1926         
1927         uiPopupBoundsBlock(block, 6, 0, -UI_UNIT_Y); /* move it downwards, mouse over button */
1928         uiEndBlock(C, block);
1929         
1930         wm_event_init_from_window(win, &event);
1931         event.type = EVT_BUT_OPEN;
1932         event.val = KM_PRESS;
1933         event.customdata = but;
1934         event.customdatafree = FALSE;
1935         wm_event_add(win, &event);
1936         
1937         return block;
1938 }
1939
1940 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1941 {
1942         return OPERATOR_FINISHED;
1943 }
1944
1945 static int wm_search_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1946 {
1947         uiPupBlock(C, wm_block_search_menu, op);
1948         
1949         return OPERATOR_CANCELLED;
1950 }
1951
1952 /* op->poll */
1953 static int wm_search_menu_poll(bContext *C)
1954 {
1955         if (CTX_wm_window(C) == NULL) {
1956                 return 0;
1957         }
1958         else {
1959                 ScrArea *sa = CTX_wm_area(C);
1960                 if (sa) {
1961                         if (sa->spacetype == SPACE_CONSOLE) return 0;  /* XXX - so we can use the shortcut in the console */
1962                         if (sa->spacetype == SPACE_TEXT) return 0;     /* XXX - so we can use the spacebar in the text editor */
1963                 }
1964                 else {
1965                         Object *editob = CTX_data_edit_object(C);
1966                         if (editob && editob->type == OB_FONT) return 0;  /* XXX - so we can use the spacebar for entering text */
1967                 }
1968         }
1969         return 1;
1970 }
1971
1972 static void WM_OT_search_menu(wmOperatorType *ot)
1973 {
1974         ot->name = "Search Menu";
1975         ot->idname = "WM_OT_search_menu";
1976         ot->description = "Pop-up a search menu over all available operators in current context";
1977         
1978         ot->invoke = wm_search_menu_invoke;
1979         ot->exec = wm_search_menu_exec;
1980         ot->poll = wm_search_menu_poll;
1981 }
1982
1983 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1984 {
1985         char idname[BKE_ST_MAXNAME];
1986         RNA_string_get(op->ptr, "name", idname);
1987
1988         uiPupMenuInvoke(C, idname, op->reports);
1989
1990         return OPERATOR_CANCELLED;
1991 }
1992
1993 static void WM_OT_call_menu(wmOperatorType *ot)
1994 {
1995         ot->name = "Call Menu";
1996         ot->idname = "WM_OT_call_menu";
1997         ot->description = "Call (draw) a pre-defined menu";
1998
1999         ot->exec = wm_call_menu_exec;
2000         ot->poll = WM_operator_winactive;
2001
2002         ot->flag = OPTYPE_INTERNAL;
2003
2004         RNA_def_string(ot->srna, "name", NULL, BKE_ST_MAXNAME, "Name", "Name of the menu");
2005 }
2006
2007 /* ************ window / screen operator definitions ************** */
2008
2009 /* this poll functions is needed in place of WM_operator_winactive
2010  * while it crashes on full screen */
2011 static int wm_operator_winactive_normal(bContext *C)
2012 {
2013         wmWindow *win = CTX_wm_window(C);
2014
2015         if (win == NULL || win->screen == NULL || win->screen->full != SCREENNORMAL)
2016                 return 0;
2017
2018         return 1;
2019 }
2020
2021 static void WM_OT_window_duplicate(wmOperatorType *ot)
2022 {
2023         ot->name = "Duplicate Window";
2024         ot->idname = "WM_OT_window_duplicate";
2025         ot->description = "Duplicate the current Blender window";
2026                 
2027         ot->exec = wm_window_duplicate_exec;
2028         ot->poll = wm_operator_winactive_normal;
2029 }
2030
2031 static void WM_OT_save_homefile(wmOperatorType *ot)
2032 {
2033         ot->name = "Save Startup File";
2034         ot->idname = "WM_OT_save_homefile";
2035         ot->description = "Make the current file the default .blend file, includes preferences";
2036                 
2037         ot->invoke = WM_operator_confirm;
2038         ot->exec = wm_homefile_write_exec;
2039 }
2040
2041 static int wm_userpref_autoexec_add_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
2042 {
2043         bPathCompare *path_cmp = MEM_callocN(sizeof(bPathCompare), "bPathCompare");
2044         BLI_addtail(&U.autoexec_paths, path_cmp);
2045         return OPERATOR_FINISHED;
2046 }
2047
2048 static void WM_OT_userpref_autoexec_path_add(wmOperatorType *ot)
2049 {
2050         ot->name = "Add Autoexec Path";
2051         ot->idname = "WM_OT_userpref_autoexec_path_add";
2052         ot->description = "Add path to exclude from autoexecution";
2053
2054         ot->exec = wm_userpref_autoexec_add_exec;
2055
2056         ot->flag = OPTYPE_INTERNAL;
2057 }
2058
2059 static int wm_userpref_autoexec_remove_exec(bContext *UNUSED(C), wmOperator *op)
2060 {
2061         const int index = RNA_int_get(op->ptr, "index");
2062         bPathCompare *path_cmp = BLI_findlink(&U.autoexec_paths, index);
2063         if (path_cmp) {
2064                 BLI_freelinkN(&U.autoexec_paths, path_cmp);
2065         }
2066         return OPERATOR_FINISHED;
2067 }
2068
2069 static void WM_OT_userpref_autoexec_path_remove(wmOperatorType *ot)
2070 {
2071         ot->name = "Remove Autoexec Path";
2072         ot->idname = "WM_OT_userpref_autoexec_path_remove";
2073         ot->description = "Remove path to exclude from autoexecution";
2074
2075         ot->exec = wm_userpref_autoexec_remove_exec;
2076
2077         ot->flag = OPTYPE_INTERNAL;
2078
2079         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, 1000);
2080 }
2081
2082 static void WM_OT_save_userpref(wmOperatorType *ot)
2083 {
2084         ot->name = "Save User Settings";
2085         ot->idname = "WM_OT_save_userpref";
2086         ot->description = "Save user preferences separately, overrides startup file preferences";
2087         
2088         ot->invoke = WM_operator_confirm;
2089         ot->exec = wm_userpref_write_exec;
2090 }
2091
2092 static void WM_OT_read_history(wmOperatorType *ot)
2093 {
2094         ot->name = "Reload History File";
2095         ot->idname = "WM_OT_read_history";
2096         ot->description = "Reloads history and bookmarks";
2097
2098         ot->invoke = WM_operator_confirm;
2099         ot->exec = wm_history_read_exec;
2100
2101         /* this operator is only used for loading settings from a previous blender install */
2102         ot->flag = OPTYPE_INTERNAL;
2103 }
2104
2105 static void WM_OT_read_homefile(wmOperatorType *ot)
2106 {
2107         PropertyRNA *prop;
2108         ot->name = "Reload Start-Up File";
2109         ot->idname = "WM_OT_read_homefile";
2110         ot->description = "Open the default file (doesn't save the current file)";
2111         
2112         ot->invoke = WM_operator_confirm;
2113         ot->exec = wm_homefile_read_exec;
2114
2115         prop = RNA_def_string_file_path(ot->srna, "filepath", NULL,
2116                                         FILE_MAX, "File Path", 
2117                                         "Path to an alternative start-up file");
2118         RNA_def_property_flag(prop, PROP_HIDDEN);
2119
2120         /* ommit poll to run in background mode */
2121 }
2122
2123 static void WM_OT_read_factory_settings(wmOperatorType *ot)
2124 {
2125         ot->name = "Load Factory Settings";
2126         ot->idname = "WM_OT_read_factory_settings";
2127         ot->description = "Load default file and user preferences";
2128         
2129         ot->invoke = WM_operator_confirm;
2130         ot->exec = wm_homefile_read_exec;
2131         /* ommit poll to run in background mode */
2132 }
2133
2134 /* *************** open file **************** */
2135
2136 /**
2137  * Wrap #WM_file_read, shared by file reading operators.
2138  */
2139 static bool wm_file_read_opwrap(bContext *C, const char *filepath, ReportList *reports,
2140                                 const bool autoexec_init)
2141 {
2142         bool success;
2143
2144         /* XXX wm in context is not set correctly after WM_file_read -> crash */
2145         /* do it before for now, but is this correct with multiple windows? */
2146         WM_event_add_notifier(C, NC_WINDOW, NULL);
2147
2148         if (autoexec_init) {
2149                 WM_file_autoexec_init(filepath);
2150         }
2151
2152         success = WM_file_read(C, filepath, reports);
2153
2154         return success;
2155 }
2156
2157 /* currently fits in a pointer */
2158 struct FileRuntime {
2159         bool is_untrusted;
2160 };
2161
2162
2163 static void open_set_load_ui(wmOperator *op, bool use_prefs)
2164 {
2165         PropertyRNA *prop = RNA_struct_find_property(op->ptr, "load_ui");
2166         if (!RNA_property_is_set(op->ptr, prop)) {
2167                 RNA_property_boolean_set(op->ptr, prop, use_prefs ?
2168                                          (U.flag & USER_FILENOUI) == 0 :
2169                                          (G.fileflags & G_FILE_NO_UI) == 0);
2170         }
2171 }
2172
2173 static void open_set_use_scripts(wmOperator *op, bool use_prefs)
2174 {
2175         PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_scripts");
2176         if (!RNA_property_is_set(op->ptr, prop)) {
2177                 /* use G_SCRIPT_AUTOEXEC rather than the userpref because this means if
2178                  * the flag has been disabled from the command line, then opening
2179                  * from the menu wont enable this setting. */
2180                 RNA_property_boolean_set(op->ptr, prop, use_prefs ?
2181                                          (U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0 :
2182                                          (G.f & G_SCRIPT_AUTOEXEC) != 0);
2183         }
2184 }
2185
2186 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
2187 {
2188         const char *openname = G.main->name;
2189
2190         if (CTX_wm_window(C) == NULL) {
2191                 /* in rare cases this could happen, when trying to invoke in background
2192                  * mode on load for example. Don't use poll for this because exec()
2193                  * can still run without a window */
2194                 BKE_report(op->reports, RPT_ERROR, "Context window not set");
2195                 return OPERATOR_CANCELLED;
2196         }
2197
2198         /* if possible, get the name of the most recently used .blend file */
2199         if (G.recent_files.first) {
2200                 struct RecentFile *recent = G.recent_files.first;
2201                 openname = recent->filepath;
2202         }
2203
2204         RNA_string_set(op->ptr, "filepath", openname);
2205         open_set_load_ui(op, true);
2206         open_set_use_scripts(op, true);
2207         op->customdata = NULL;
2208
2209         WM_event_add_fileselect(C, op);
2210
2211         return OPERATOR_RUNNING_MODAL;
2212 }
2213
2214 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
2215 {
2216         char filepath[FILE_MAX];
2217         bool success;
2218
2219         RNA_string_get(op->ptr, "filepath", filepath);
2220
2221         /* re-use last loaded setting so we can reload a file without changing */
2222         open_set_load_ui(op, false);
2223         open_set_use_scripts(op, false);
2224
2225         if (RNA_boolean_get(op->ptr, "load_ui"))
2226                 G.fileflags &= ~G_FILE_NO_UI;
2227         else
2228                 G.fileflags |= G_FILE_NO_UI;
2229                 
2230         if (RNA_boolean_get(op->ptr, "use_scripts"))
2231                 G.f |= G_SCRIPT_AUTOEXEC;
2232         else
2233                 G.f &= ~G_SCRIPT_AUTOEXEC;
2234         
2235         success = wm_file_read_opwrap(C, filepath, op->reports, !(G.f & G_SCRIPT_AUTOEXEC));
2236
2237         /* for file open also popup for warnings, not only errors */
2238         BKE_report_print_level_set(op->reports, RPT_WARNING);
2239
2240         if (success) {
2241                 return OPERATOR_FINISHED;
2242         }
2243         else {
2244                 return OPERATOR_CANCELLED;
2245         }
2246 }
2247
2248 static bool wm_open_mainfile_check(bContext *UNUSED(C), wmOperator *op)
2249 {
2250         struct FileRuntime *file_info = (struct FileRuntime *)&op->customdata;
2251         PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_scripts");
2252         bool is_untrusted = false;
2253         char path[FILE_MAX];
2254         char *lslash;
2255
2256         RNA_string_get(op->ptr, "filepath", path);
2257
2258         /* get the dir */
2259         lslash = (char *)BLI_last_slash(path);
2260         if (lslash) *(lslash + 1) = '\0';
2261
2262         if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) {
2263                 if (BKE_autoexec_match(path) == true) {
2264                         RNA_property_boolean_set(op->ptr, prop, false);
2265                         is_untrusted = true;
2266                 }
2267         }
2268
2269         if (file_info) {
2270                 file_info->is_untrusted = is_untrusted;
2271         }
2272
2273         return is_untrusted;
2274 }
2275
2276 static void wm_open_mainfile_ui(bContext *UNUSED(C), wmOperator *op)
2277 {
2278         struct FileRuntime *file_info = (struct FileRuntime *)&op->customdata;
2279         uiLayout *layout = op->layout;
2280         uiLayout *col = op->layout;
2281         const char *autoexec_text = NULL;
2282
2283         uiItemR(layout, op->ptr, "load_ui", 0, NULL, ICON_NONE);
2284
2285         col = uiLayoutColumn(layout, false);
2286         if (file_info->is_untrusted) {
2287                 autoexec_text = "Trusted Source [Untrusted Path]";
2288                 uiLayoutSetActive(col, false);
2289                 uiLayoutSetEnabled(col, false);
2290         }
2291         else {
2292                 autoexec_text = "Trusted Source";
2293         }
2294
2295         uiItemR(col, op->ptr, "use_scripts", 0, autoexec_text, ICON_NONE);
2296 }
2297
2298 static void WM_OT_open_mainfile(wmOperatorType *ot)
2299 {
2300         ot->name = "Open Blender File";
2301         ot->idname = "WM_OT_open_mainfile";
2302         ot->description = "Open a Blender file";
2303
2304         ot->invoke = wm_open_mainfile_invoke;
2305         ot->exec = wm_open_mainfile_exec;
2306         ot->check = wm_open_mainfile_check;
2307         ot->ui = wm_open_mainfile_ui;
2308         /* ommit window poll so this can work in background mode */
2309
2310         WM_operator_properties_filesel(ot, FOLDERFILE | BLENDERFILE, FILE_BLENDER, FILE_OPENFILE,
2311                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
2312
2313         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
2314         RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source",
2315                         "Allow .blend file to execute scripts automatically, default available from system preferences");
2316 }
2317
2318
2319 /* *************** revert file **************** */
2320
2321 static int wm_revert_mainfile_exec(bContext *C, wmOperator *op)
2322 {
2323         bool success;
2324
2325         success = wm_file_read_opwrap(C, G.main->name, op->reports, true);
2326
2327         if (success) {
2328                 return OPERATOR_FINISHED;
2329         }
2330         else {
2331                 return OPERATOR_CANCELLED;
2332         }
2333 }
2334
2335 static int wm_revert_mainfile_poll(bContext *UNUSED(C))
2336 {
2337         return G.relbase_valid;
2338 }
2339
2340 static void WM_OT_revert_mainfile(wmOperatorType *ot)
2341 {
2342         ot->name = "Revert";
2343         ot->idname = "WM_OT_revert_mainfile";
2344         ot->description = "Reload the saved file";
2345
2346         ot->exec = wm_revert_mainfile_exec;
2347         ot->poll = wm_revert_mainfile_poll;
2348 }
2349
2350 /* **************** link/append *************** */
2351
2352 static int wm_link_append_poll(bContext *C)
2353 {
2354         if (WM_operator_winactive(C)) {
2355                 /* linking changes active object which is pretty useful in general,
2356                  * but which totally confuses edit mode (i.e. it becoming not so obvious
2357                  * to leave from edit mode and invalid tools in toolbar might be displayed)
2358                  * so disable link/append when in edit mode (sergey) */
2359                 if (CTX_data_edit_object(C))
2360                         return 0;
2361
2362                 return 1;
2363         }
2364
2365         return 0;
2366 }
2367
2368 static int wm_link_append_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
2369 {
2370         if (RNA_struct_property_is_set(op->ptr, "filepath")) {
2371                 return WM_operator_call_notest(C, op);
2372         }
2373         else {
2374                 /* XXX TODO solve where to get last linked library from */
2375                 if (G.lib[0] != '\0') {
2376                         RNA_string_set(op->ptr, "filepath", G.lib);
2377                 }
2378                 else if (G.relbase_valid) {
2379                         char path[FILE_MAX];
2380                         BLI_strncpy(path, G.main->name, sizeof(G.main->name));
2381                         BLI_parent_dir(path);
2382                         RNA_string_set(op->ptr, "filepath", path);
2383                 }
2384                 WM_event_add_fileselect(C, op);
2385                 return OPERATOR_RUNNING_MODAL;
2386         }
2387 }
2388
2389 static short wm_link_append_flag(wmOperator *op)
2390 {
2391         short flag = 0;
2392
2393         if (RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
2394         if (RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
2395         if (RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
2396         if (RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
2397         if (RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
2398
2399         return flag;
2400 }
2401
2402 static int wm_link_append_exec(bContext *C, wmOperator *op)
2403 {
2404         Main *bmain = CTX_data_main(C);
2405         Scene *scene = CTX_data_scene(C);
2406         Main *mainl = NULL;
2407         BlendHandle *bh;
2408         PropertyRNA *prop;
2409         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[BLO_GROUP_MAX];
2410         int idcode, totfiles = 0;
2411         short flag;
2412
2413         RNA_string_get(op->ptr, "filename", name);
2414         RNA_string_get(op->ptr, "directory", dir);
2415
2416         /* test if we have a valid data */
2417         if (BLO_is_a_library(dir, libname, group) == 0) {
2418                 BKE_report(op->reports, RPT_ERROR, "Not a library");
2419                 return OPERATOR_CANCELLED;
2420         }
2421         else if (group[0] == 0) {
2422                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
2423                 return OPERATOR_CANCELLED;
2424         }
2425         else if (BLI_path_cmp(bmain->name, libname) == 0) {
2426                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
2427                 return OPERATOR_CANCELLED;
2428         }
2429
2430         /* check if something is indicated for append/link */
2431         prop = RNA_struct_find_property(op->ptr, "files");
2432         if (prop) {
2433                 totfiles = RNA_property_collection_length(op->ptr, prop);
2434                 if (totfiles == 0) {
2435                         if (name[0] == '\0') {
2436                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
2437                                 return OPERATOR_CANCELLED;
2438                         }
2439                 }
2440         }
2441         else if (name[0] == '\0') {
2442                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
2443                 return OPERATOR_CANCELLED;
2444         }
2445
2446         bh = BLO_blendhandle_from_file(libname, op->reports);
2447
2448         if (bh == NULL) {
2449                 /* unlikely since we just browsed it, but possible
2450                  * error reports will have been made by BLO_blendhandle_from_file() */
2451                 return OPERATOR_CANCELLED;
2452         }
2453
2454
2455         /* from here down, no error returns */
2456
2457         idcode = BKE_idcode_from_name(group);
2458
2459         /* now we have or selected, or an indicated file */
2460         if (RNA_boolean_get(op->ptr, "autoselect"))
2461                 BKE_scene_base_deselect_all(scene);
2462
2463         
2464         flag = wm_link_append_flag(op);
2465
2466         /* sanity checks for flag */
2467         if (scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
2468                 /* TODO, user never gets this message */
2469                 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name + 2);
2470                 flag &= ~FILE_GROUP_INSTANCE;
2471         }
2472
2473
2474         /* tag everything, all untagged data can be made local
2475          * its also generally useful to know what is new
2476          *
2477          * take extra care BKE_main_id_flag_all(LIB_LINK_TAG, false) is called after! */
2478         BKE_main_id_flag_all(bmain, LIB_PRE_EXISTING, 1);
2479
2480         /* here appending/linking starts */
2481         mainl = BLO_library_append_begin(bmain, &bh, libname);
2482         if (totfiles == 0) {
2483                 BLO_library_append_named_part_ex(C, mainl, &bh, name, idcode, flag);
2484         }
2485         else {
2486                 RNA_BEGIN (op->ptr, itemptr, "files")
2487                 {
2488                         RNA_string_get(&itemptr, "name", name);
2489                         BLO_library_append_named_part_ex(C, mainl, &bh, name, idcode, flag);
2490                 }
2491                 RNA_END;
2492         }
2493         BLO_library_append_end(C, mainl, &bh, idcode, flag);
2494         
2495         /* mark all library linked objects to be updated */
2496         BKE_main_lib_objects_recalc_all(bmain);
2497         IMB_colormanagement_check_file_config(bmain);
2498
2499         /* append, rather than linking */
2500         if ((flag & FILE_LINK) == 0) {
2501                 Library *lib = BLI_findstring(&bmain->library, libname, offsetof(Library, filepath));
2502                 if (lib) BKE_library_make_local(bmain, lib, true);
2503                 else BLI_assert(!"cant find name of just added library!");
2504         }
2505
2506         /* important we unset, otherwise these object wont
2507          * link into other scenes from this blend file */
2508         BKE_main_id_flag_all(bmain, LIB_PRE_EXISTING, false);
2509
2510         /* recreate dependency graph to include new objects */
2511         DAG_scene_relations_rebuild(bmain, scene);
2512
2513         BLO_blendhandle_close(bh);
2514
2515         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
2516         BLI_strncpy(G.lib, dir, FILE_MAX);
2517
2518         WM_event_add_notifier(C, NC_WINDOW, NULL);
2519
2520         return OPERATOR_FINISHED;
2521 }
2522
2523 static void WM_OT_link_append(wmOperatorType *ot)
2524 {
2525         PropertyRNA *prop;
2526
2527         ot->name = "Link/Append from Library";
2528         ot->idname = "WM_OT_link_append";
2529         ot->description = "Link or Append from a Library .blend file";
2530         
2531         ot->invoke = wm_link_append_invoke;
2532         ot->exec = wm_link_append_exec;
2533         ot->poll = wm_link_append_poll;
2534         
2535         ot->flag |= OPTYPE_UNDO;
2536
2537         WM_operator_properties_filesel(
2538                 ot, FOLDERFILE | BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE,
2539                 WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME | WM_FILESEL_RELPATH | WM_FILESEL_FILES,
2540                 FILE_DEFAULTDISPLAY);
2541         
2542         /* better not save _any_ settings for this operator */
2543         /* properties */
2544         prop = RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
2545         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2546         prop = RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
2547         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2548         prop = RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
2549         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2550         prop = RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
2551         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2552 }
2553
2554 /* *************** recover last session **************** */
2555
2556 void WM_recover_last_session(bContext *C, ReportList *reports)
2557 {
2558         char filepath[FILE_MAX];
2559         
2560         BLI_make_file_string("/", filepath, BLI_temporary_dir(), BLENDER_QUIT_FILE);
2561         /* if reports==NULL, it's called directly without operator, we add a quick check here */
2562         if (reports || BLI_exists(filepath)) {
2563                 G.fileflags |= G_FILE_RECOVER;
2564                 
2565                 wm_file_read_opwrap(C, filepath, reports, true);
2566         
2567                 G.fileflags &= ~G_FILE_RECOVER;
2568                 
2569                 /* XXX bad global... fixme */
2570                 if (G.main->name[0])
2571                         G.file_loaded = 1;      /* prevents splash to show */
2572                 else {
2573                         G.relbase_valid = 0;
2574                         G.save_over = 0;    /* start with save preference untitled.blend */
2575                 }
2576
2577         }
2578 }
2579
2580 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
2581 {
2582         WM_recover_last_session(C, op->reports);
2583         return OPERATOR_FINISHED;
2584 }
2585
2586 static void WM_OT_recover_last_session(wmOperatorType *ot)
2587 {
2588         ot->name = "Recover Last Session";
2589         ot->idname = "WM_OT_recover_last_session";
2590         ot->description = "Open the last closed file (\"" BLENDER_QUIT_FILE "\")";
2591         
2592         ot->exec = wm_recover_last_session_exec;
2593 }
2594
2595 /* *************** recover auto save **************** */
2596
2597 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
2598 {
2599         char filepath[FILE_MAX];
2600         bool success;
2601
2602         RNA_string_get(op->ptr, "filepath", filepath);
2603
2604         G.fileflags |= G_FILE_RECOVER;
2605
2606         success = wm_file_read_opwrap(C, filepath, op->reports, true);
2607
2608         G.fileflags &= ~G_FILE_RECOVER;
2609         
2610         if (success) {
2611                 return OPERATOR_FINISHED;
2612         }
2613         else {
2614                 return OPERATOR_CANCELLED;
2615         }
2616 }
2617
2618 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
2619 {
2620         char filename[FILE_MAX];
2621
2622         wm_autosave_location(filename);
2623         RNA_string_set(op->ptr, "filepath", filename);
2624         WM_event_add_fileselect(C, op);
2625
2626         return OPERATOR_RUNNING_MODAL;
2627 }
2628
2629 static void WM_OT_recover_auto_save(wmOperatorType *ot)
2630 {
2631         ot->name = "Recover Auto Save";
2632         ot->idname = "WM_OT_recover_auto_save";
2633         ot->description = "Open an automatically saved file to recover it";
2634         
2635         ot->exec = wm_recover_auto_save_exec;
2636         ot->invoke = wm_recover_auto_save_invoke;
2637
2638         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE,
2639                                        WM_FILESEL_FILEPATH, FILE_LONGDISPLAY);
2640 }
2641
2642 /* *************** save file as **************** */
2643
2644 static void wm_filepath_default(char *filepath)
2645 {
2646         if (G.save_over == false) {
2647                 BLI_ensure_filename(filepath, FILE_MAX, "untitled.blend");
2648         }
2649 }
2650
2651 static void save_set_compress(wmOperator *op)
2652 {
2653         if (!RNA_struct_property_is_set(op->ptr, "compress")) {
2654                 if (G.save_over) /* keep flag for existing file */
2655                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
2656                 else /* use userdef for new file */
2657                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
2658         }
2659 }
2660
2661 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
2662 {
2663         char name[FILE_MAX];
2664
2665         save_set_compress(op);
2666         
2667         /* if not saved before, get the name of the most recently used .blend file */
2668         if (G.main->name[0] == 0 && G.recent_files.first) {
2669                 struct RecentFile *recent = G.recent_files.first;
2670                 BLI_strncpy(name, recent->filepath, FILE_MAX);
2671         }
2672         else
2673                 BLI_strncpy(name, G.main->name, FILE_MAX);
2674         
2675         wm_filepath_default(name);
2676         RNA_string_set(op->ptr, "filepath", name);
2677         
2678         WM_event_add_fileselect(C, op);
2679
2680         return OPERATOR_RUNNING_MODAL;
2681 }
2682
2683 /* function used for WM_OT_save_mainfile too */
2684 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
2685 {
2686         char path[FILE_MAX];
2687         int fileflags;
2688
2689         save_set_compress(op);
2690         
2691         if (RNA_struct_property_is_set(op->ptr, "filepath")) {
2692                 RNA_string_get(op->ptr, "filepath", path);
2693         }
2694         else {
2695                 BLI_strncpy(path, G.main->name, FILE_MAX);
2696                 wm_filepath_default(path);
2697         }
2698         
2699         fileflags = G.fileflags & ~G_FILE_USERPREFS;
2700
2701         /* set compression flag */
2702         BKE_BIT_TEST_SET(fileflags, RNA_boolean_get(op->ptr, "compress"),
2703                          G_FILE_COMPRESS);
2704         BKE_BIT_TEST_SET(fileflags, RNA_boolean_get(op->ptr, "relative_remap"),
2705                          G_FILE_RELATIVE_REMAP);
2706         BKE_BIT_TEST_SET(fileflags,
2707                          (RNA_struct_property_is_set(op->ptr, "copy") &&
2708                           RNA_boolean_get(op->ptr, "copy")),
2709                          G_FILE_SAVE_COPY);
2710
2711 #ifdef USE_BMESH_SAVE_AS_COMPAT
2712         BKE_BIT_TEST_SET(fileflags,
2713                          (RNA_struct_find_property(op->ptr, "use_mesh_compat") &&
2714                           RNA_boolean_get(op->ptr, "use_mesh_compat")),
2715                          G_FILE_MESH_COMPAT);
2716 #endif
2717
2718         if (wm_file_write(C, path, fileflags, op->reports) != 0)
2719                 return OPERATOR_CANCELLED;
2720
2721         WM_event_add_notifier(C, NC_WM | ND_FILESAVE, NULL);
2722
2723         return OPERATOR_FINISHED;
2724 }
2725
2726 /* function used for WM_OT_save_mainfile too */
2727 static bool blend_save_check(bContext *UNUSED(C), wmOperator *op)
2728 {
2729         char filepath[FILE_MAX];
2730         RNA_string_get(op->ptr, "filepath", filepath);
2731         if (!BLO_has_bfile_extension(filepath)) {
2732                 /* some users would prefer BLI_replace_extension(),
2733                  * we keep getting nitpicking bug reports about this - campbell */
2734                 BLI_ensure_extension(filepath, FILE_MAX, ".blend");
2735                 RNA_string_set(op->ptr, "filepath", filepath);
2736                 return TRUE;
2737         }
2738         return FALSE;
2739 }
2740
2741 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
2742 {
2743         PropertyRNA *prop;
2744
2745         ot->name = "Save As Blender File";
2746         ot->idname = "WM_OT_save_as_mainfile";
2747         ot->description = "Save the current file in the desired location";
2748         
2749         ot->invoke = wm_save_as_mainfile_invoke;
2750         ot->exec = wm_save_as_mainfile_exec;
2751         ot->check = blend_save_check;
2752         /* ommit window poll so this can work in background mode */
2753
2754         WM_operator_properties_filesel(ot, FOLDERFILE | BLENDERFILE, FILE_BLENDER, FILE_SAVE,
2755                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
2756         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
2757         RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative",
2758                         "Remap relative paths when saving in a different directory");
2759         prop = RNA_def_boolean(ot->srna, "copy", 0, "Save Copy",
2760                         "Save a copy of the actual working state but does not make saved file active");
2761         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2762 #ifdef USE_BMESH_SAVE_AS_COMPAT
2763         RNA_def_boolean(ot->srna, "use_mesh_compat", 0, "Legacy Mesh Format",
2764                         "Save using legacy mesh format (no ngons) - WARNING: only saves tris and quads, other ngons will "
2765                         "be lost (no implicit triangulation)");
2766 #endif
2767 }
2768
2769 /* *************** save file directly ******** */
2770
2771 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
2772 {
2773         char name[FILE_MAX];
2774         int ret;
2775         
2776         /* cancel if no active window */
2777         if (CTX_wm_window(C) == NULL)
2778                 return OPERATOR_CANCELLED;
2779
2780         save_set_compress(op);
2781
2782         /* if not saved before, get the name of the most recently used .blend file */
2783         if (G.main->name[0] == 0 && G.recent_files.first) {
2784                 struct RecentFile *recent = G.recent_files.first;
2785                 BLI_strncpy(name, recent->filepath, FILE_MAX);
2786         }
2787         else
2788                 BLI_strncpy(name, G.main->name, FILE_MAX);
2789
2790         wm_filepath_default(name);
2791         
2792         RNA_string_set(op->ptr, "filepath", name);
2793
2794         /* if we're saving for the first time and prefer relative paths - any existing paths will be absolute,
2795          * enable the option to remap paths to avoid confusion [#37240] */
2796         if ((G.relbase_valid == false) && (U.flag & USER_RELPATHS)) {
2797                 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "relative_remap");
2798                 if (!RNA_property_is_set(op->ptr, prop)) {
2799                         RNA_property_boolean_set(op->ptr, prop, true);
2800                 }
2801         }
2802
2803         if (G.save_over) {
2804                 if (BLI_exists(name)) {
2805                         ret = WM_operator_confirm_message_ex(C, op, IFACE_("Save Over?"), ICON_QUESTION, name);
2806                 }
2807                 else {
2808                         ret = wm_save_as_mainfile_exec(C, op);
2809                 }
2810         }
2811         else {
2812                 WM_event_add_fileselect(C, op);
2813                 ret = OPERATOR_RUNNING_MODAL;
2814         }
2815         
2816         return ret;
2817 }
2818
2819 static void WM_OT_save_mainfile(wmOperatorType *ot)
2820 {
2821         ot->name = "Save Blender File";
2822         ot->idname = "WM_OT_save_mainfile";
2823         ot->description = "Save the current Blender file";
2824         
2825         ot->invoke = wm_save_mainfile_invoke;
2826         ot->exec = wm_save_as_mainfile_exec;
2827         ot->check = blend_save_check;
2828         /* ommit window poll so this can work in background mode */
2829         
2830         WM_operator_properties_filesel(ot, FOLDERFILE | BLENDERFILE, FILE_BLENDER, FILE_SAVE,
2831                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
2832         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
2833         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
2834 }
2835
2836 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
2837 {
2838         ot->name = "Toggle Fullscreen";
2839         ot->idname = "WM_OT_window_fullscreen_toggle";
2840         ot->description = "Toggle the current window fullscreen";
2841
2842         ot->exec = wm_window_fullscreen_toggle_exec;
2843         ot->poll = WM_operator_winactive;
2844 }
2845
2846 static int wm_exit_blender_exec(bContext *C, wmOperator *op)
2847 {
2848         WM_operator_free(op);
2849         
2850         WM_exit(C);
2851         
2852         return OPERATOR_FINISHED;
2853 }
2854
2855 static void WM_OT_quit_blender(wmOperatorType *ot)
2856 {
2857         ot->name = "Quit Blender";
2858         ot->idname = "WM_OT_quit_blender";
2859         ot->description = "Quit Blender";
2860
2861         ot->invoke = WM_operator_confirm;
2862         ot->exec = wm_exit_blender_exec;
2863 }
2864
2865 /* *********************** */
2866
2867 #if defined(WIN32)
2868
2869 static int wm_console_toggle_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
2870 {
2871         GHOST_toggleConsole(2);
2872         return OPERATOR_FINISHED;
2873 }
2874
2875 static void WM_OT_console_toggle(wmOperatorType *ot)
2876 {
2877         /* XXX Have to mark these for xgettext, as under linux they do not exists... */
2878         ot->name = CTX_N_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Toggle System Console");
2879         ot->idname = "WM_OT_console_toggle";
2880         ot->description = N_("Toggle System Console");
2881         
2882         ot->exec = wm_console_toggle_exec;
2883         ot->poll = WM_operator_winactive;
2884 }
2885
2886 #endif
2887
2888 /* ************ default paint cursors, draw always around cursor *********** */
2889 /*
2890  * - returns handler to free
2891  * - poll(bContext): returns 1 if draw should happen
2892  * - draw(bContext): drawing callback for paint cursor
2893  */
2894
2895 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2896                                wmPaintCursorDraw draw, void *customdata)
2897 {
2898         wmPaintCursor *pc = MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2899         
2900         BLI_addtail(&wm->paintcursors, pc);
2901         
2902         pc->customdata = customdata;
2903         pc->poll = poll;
2904         pc->draw = draw;
2905         
2906         return pc;
2907 }
2908
2909 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2910 {
2911         wmPaintCursor *pc;
2912         
2913         for (pc = wm->paintcursors.first; pc; pc = pc->next) {
2914                 if (pc == (wmPaintCursor *)handle) {
2915                         BLI_remlink(&wm->paintcursors, pc);
2916                         MEM_freeN(pc);
2917                         return;
2918                 }
2919         }
2920 }
2921
2922 /* ************ window gesture operator-callback definitions ************** */
2923 /*
2924  * These are default callbacks for use in operators requiring gesture input
2925  */
2926
2927 /* **************** Border gesture *************** */
2928
2929 /* Border gesture has two types:
2930  * 1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
2931  * 2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2932  *
2933  * It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2934  */
2935
2936 static int border_apply_rect(wmOperator *op)
2937 {
2938         wmGesture *gesture = op->customdata;
2939         rcti *rect = gesture->customdata;
2940         
2941         if (rect->xmin == rect->xmax || rect->ymin == rect->ymax)
2942                 return 0;
2943
2944         
2945         /* operator arguments and storage. */
2946         RNA_int_set(op->ptr, "xmin", min_ii(rect->xmin, rect->xmax));
2947         RNA_int_set(op->ptr, "ymin", min_ii(rect->ymin, rect->ymax));
2948         RNA_int_set(op->ptr, "xmax", max_ii(rect->xmin, rect->xmax));
2949         RNA_int_set(op->ptr, "ymax", max_ii(rect->ymin, rect->ymax));
2950
2951         return 1;
2952 }
2953
2954 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2955 {
2956         int retval;
2957
2958         if (!border_apply_rect(op))
2959                 return 0;
2960         
2961         /* XXX weak; border should be configured for this without reading event types */
2962         if (RNA_struct_find_property(op->ptr, "gesture_mode")) {
2963                 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2964         }
2965
2966         retval = op->type->exec(C, op);
2967         OPERATOR_RETVAL_CHECK(retval);
2968
2969         return 1;
2970 }
2971
2972 static void wm_gesture_end(bContext *C, wmOperator *op)
2973 {
2974         wmGesture *gesture = op->customdata;
2975         
2976         WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2977         op->customdata = NULL;
2978
2979         ED_area_tag_redraw(CTX_wm_area(C));
2980         
2981         if (RNA_struct_find_property(op->ptr, "cursor")) {
2982                 WM_cursor_modal_restore(CTX_wm_window(C));
2983         }
2984 }
2985
2986 int WM_border_select_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2987 {
2988         if (ISTWEAK(event->type))
2989                 op->customdata = WM_gesture_new(C, event, WM_GESTURE_RECT);
2990         else
2991                 op->customdata = WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2992
2993         /* add modal handler */
2994         WM_event_add_modal_handler(C, op);
2995         
2996         wm_gesture_tag_redraw(C);
2997
2998         return OPERATOR_RUNNING_MODAL;
2999 }
3000
3001 int WM_border_select_modal(bContext *C, wmOperator *op, const wmEvent *event)
3002 {
3003         wmGesture *gesture = op->customdata;
3004         rcti *rect = gesture->customdata;
3005         int sx, sy;
3006         
3007         if (event->type == MOUSEMOVE) {
3008                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
3009
3010                 if (gesture->type == WM_GESTURE_CROSS_RECT && gesture->mode == 0) {
3011                         rect->xmin = rect->xmax = event->x - sx;
3012                         rect->ymin = rect->ymax = event->y - sy;
3013                 }
3014                 else {
3015                         rect->xmax = event->x - sx;
3016                         rect->ymax = event->y - sy;
3017                 }
3018                 border_apply_rect(op);
3019
3020                 wm_gesture_tag_redraw(C);
3021         }
3022         else if (event->type == EVT_MODAL_MAP) {
3023                 switch (event->val) {
3024                         case GESTURE_MODAL_BEGIN:
3025                                 if (gesture->type == WM_GESTURE_CROSS_RECT && gesture->mode == 0) {
3026                                         gesture->mode = 1;
3027                                         wm_gesture_tag_redraw(C);
3028                                 }
3029                                 break;
3030                         case GESTURE_MODAL_SELECT:
3031                         case GESTURE_MODAL_DESELECT:
3032                         case GESTURE_MODAL_IN:
3033                         case GESTURE_MODAL_OUT:
3034                                 if (border_apply(C, op, event->val)) {
3035                                         wm_gesture_end(C, op);
3036                                         return OPERATOR_FINISHED;
3037                                 }
3038                                 wm_gesture_end(C, op);
3039                                 return OPERATOR_CANCELLED;
3040                                 break;
3041
3042                         case GESTURE_MODAL_CANCEL:
3043                                 wm_gesture_end(C, op);
3044                                 return OPERATOR_CANCELLED;
3045                 }
3046
3047         }
3048 //      /* Allow view navigation??? */
3049 //      else {
3050 //              return OPERATOR_PASS_THROUGH;
3051 //      }
3052
3053         return OPERATOR_RUNNING_MODAL;
3054 }
3055
3056 void WM_border_select_cancel(bContext *C, wmOperator *op)
3057 {
3058         wm_gesture_end(C, op);
3059 }
3060
3061 /* **************** circle gesture *************** */
3062 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
3063
3064 #ifdef GESTURE_MEMORY
3065 int circle_select_size = 25; /* XXX - need some operator memory thing! */
3066 #endif
3067
3068 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, const wmEvent *event)
3069 {
3070         op->customdata = WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
3071         
3072         /* add modal handler */
3073         WM_event_add_modal_handler(C, op);
3074         
3075         wm_gesture_tag_redraw(C);
3076         
3077         return OPERATOR_RUNNING_MODAL;
3078 }
3079
3080 static void gesture_circle_apply(bContext *C, wmOperator *op)
3081 {
3082         wmGesture *gesture = op->customdata;
3083         rcti *rect = gesture->customdata;
3084         
3085         if (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_NOP)
3086                 return;
3087
3088         /* operator arguments and storage. */
3089         RNA_int_set(op->ptr, "x", rect->xmin);
3090         RNA_int_set(op->ptr, "y", rect->ymin);
3091         RNA_int_set(op->ptr, "radius", rect->xmax);
3092         
3093         if (op->type->exec) {
3094                 int retval;
3095                 retval = op->type->exec(C, op);
3096                 OPERATOR_RETVAL_CHECK(retval);
3097         }
3098 #ifdef GESTURE_MEMORY
3099         circle_select_size = rect->xmax;
3100 #endif
3101 }
3102
3103 int WM_gesture_circle_modal(bContext *C, wmOperator *op, const wmEvent *event)
3104 {
3105         wmGesture *gesture = op->customdata;
3106         rcti *rect = gesture->customdata;
3107         int sx, sy;
3108
3109         if (event->type == MOUSEMOVE) {
3110                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
3111
3112                 rect->xmin = event->x - sx;
3113                 rect->ymin = event->y - sy;
3114
3115                 wm_gesture_tag_redraw(C);
3116
3117                 if (gesture->mode)
3118                         gesture_circle_apply(C, op);
3119         }
3120         else if (event->type == EVT_MODAL_MAP) {
3121                 float fac;
3122                 
3123                 switch (event->val) {
3124                         case GESTURE_MODAL_CIRCLE_SIZE:
3125                                 fac = 0.3f * (event->y - event->prevy);
3126                                 if (fac > 0)
3127                                         rect->xmax += ceil(fac);
3128                                 else
3129                                         rect->xmax += floor(fac);
3130                                 if (rect->xmax < 1) rect->xmax = 1;
3131                                 wm_gesture_tag_redraw(C);
3132                                 break;
3133                         case GESTURE_MODAL_CIRCLE_ADD:
3134                                 rect->xmax += 2 + rect->xmax / 10;
3135                                 wm_gesture_tag_redraw(C);
3136                                 break;
3137                         case GESTURE_MODAL_CIRCLE_SUB:
3138                                 rect->xmax -= 2 + rect->xmax / 10;
3139                                 if (rect->xmax < 1) rect->xmax = 1;
3140                                 wm_gesture_tag_redraw(C);
3141                                 break;
3142                         case GESTURE_MODAL_SELECT:
3143                         case GESTURE_MODAL_DESELECT:
3144                         case GESTURE_MODAL_NOP:
3145                                 if (RNA_struct_find_property(op->ptr, "gesture_mode"))
3146                                         RNA_int_set(op->ptr, "gesture_mode", event->val);
3147
3148                                 if (event->val != GESTURE_MODAL_NOP) {
3149                                         /* apply first click */
3150                                         gesture_circle_apply(C, op);
3151                                         gesture->mode = 1;
3152                                         wm_gesture_tag_redraw(C);
3153                                 }
3154                                 break;
3155
3156                         case GESTURE_MODAL_CANCEL:
3157                         case GESTURE_MODAL_CONFIRM:
3158                                 wm_gesture_end(C, op);
3159                                 return OPERATOR_FINISHED; /* use finish or we don't get an undo */
3160                 }
3161         }
3162         /* Allow view navigation??? */
3163         /* note, this gives issues: 1) other modal ops run on top (border select), 2) middlemouse is used now 3) tablet/trackpad? */
3164 //      else {
3165 //              return OPERATOR_PASS_THROUGH;
3166 //      }
3167
3168         return OPERATOR_RUNNING_MODAL;
3169 }
3170
3171 void WM_gesture_circle_cancel(bContext *C, wmOperator *op)
3172 {
3173         wm_gesture_end(C, op);
3174 }
3175
3176 #if 0
3177 /* template to copy from */
3178 void WM_OT_circle_gesture(wmOperatorType *ot)
3179 {
3180         ot->name = "Circle Gesture";
3181         ot->idname = "WM_OT_circle_gesture";
3182         ot->description = "Enter rotate mode with a circular gesture";
3183         
3184         ot->invoke = WM_gesture_circle_invoke;
3185         ot->modal = WM_gesture_circle_modal;
3186         
3187         ot->poll = WM_operator_winactive;
3188         
3189         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
3190         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
3191         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
3192
3193 }
3194 #endif
3195
3196 /* **************** Tweak gesture *************** */
3197
3198 static void tweak_gesture_modal(bContext *C, const wmEvent *event)
3199 {
3200         wmWindow *window = CTX_wm_window(C);
3201         wmGesture *gesture = window->tweak;
3202         rcti *rect = gesture->customdata;
3203         int sx, sy, val;
3204         
3205         switch (event->type) {
3206                 case MOUSEMOVE:
3207                 case INBETWEEN_MOUSEMOVE:
3208                         
3209                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
3210                         
3211                         rect->xmax = event->x - sx;
3212                         rect->ymax = event->y - sy;
3213                         
3214                         if ((val = wm_gesture_evaluate(gesture))) {
3215                                 wmEvent tevent;
3216
3217                                 wm_event_init_from_window(window, &tevent);
3218                                 if (gesture->event_type == LEFTMOUSE)
3219                                         tevent.type = EVT_TWEAK_L;
3220                                 else if (gesture->event_type == RIGHTMOUSE)
3221                                         tevent.type = EVT_TWEAK_R;
3222                                 else
3223                                         tevent.type = EVT_TWEAK_M;
3224                                 tevent.val = val;
3225                                 /* mouse coords! */
3226                                 wm_event_add(window, &tevent);
3227                                 
3228                                 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
3229                         }
3230                         
3231                         break;
3232                         
3233                 case LEFTMOUSE:
3234                 case RIGHTMOUSE:
3235                 case MIDDLEMOUSE:
3236                         if (gesture->event_type == event->type) {
3237                                 WM_gesture_end(C, gesture);
3238
3239                                 /* when tweak fails we should give the other keymap entries a chance */
3240
3241                                 /* XXX, assigning to readonly, BAD JUJU! */
3242                                 ((wmEvent *)event)->val = KM_RELEASE;
3243                         }
3244                         break;
3245                 default:
3246                         if (!ISTIMER(event->type) && event->type != EVENT_NONE) {
3247                                 WM_gesture_end(C, gesture);
3248                         }
3249                         break;
3250         }
3251 }
3252
3253 /* standard tweak, called after window handlers passed on event */
3254 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)