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