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