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