Fix #36226, Select Linked works not in touch with Prefs.
[blender.git] / source / blender / editors / interface / interface_ops.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) 2009 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation, Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_ops.c
27  *  \ingroup edinterface
28  */
29
30 #include <stdio.h>
31 #include <math.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_text_types.h" /* for UI_OT_reports_to_text */
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math_color.h"
42 #include "BLI_math_vector.h"
43 #include "BLI_utildefines.h"
44
45 #include "BLF_api.h"
46 #include "BLF_translation.h"
47
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50 #include "BKE_global.h"
51 #include "BKE_text.h" /* for UI_OT_reports_to_text */
52 #include "BKE_report.h"
53
54 #include "RNA_access.h"
55 #include "RNA_define.h"
56
57 #include "BIF_gl.h"
58
59 #include "UI_interface.h"
60
61 #include "IMB_colormanagement.h"
62
63 #include "interface_intern.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 /* only for UI_OT_editsource */
69 #include "ED_screen.h"
70 #include "BKE_main.h"
71 #include "BLI_ghash.h"
72
73 #include "ED_image.h"  /* for HDR color sampling */
74 #include "ED_node.h"   /* for HDR color sampling */
75 #include "ED_clip.h"   /* for HDR color sampling */
76
77 /* ********************************************************** */
78
79 typedef struct Eyedropper {
80         struct ColorManagedDisplay *display;
81
82         PointerRNA ptr;
83         PropertyRNA *prop;
84         int index;
85
86         int   accum_start; /* has mouse been presed */
87         float accum_col[3];
88         int   accum_tot;
89 } Eyedropper;
90
91 static int eyedropper_init(bContext *C, wmOperator *op)
92 {
93         Scene *scene = CTX_data_scene(C);
94         Eyedropper *eye;
95         
96         op->customdata = eye = MEM_callocN(sizeof(Eyedropper), "Eyedropper");
97         
98         uiContextActiveProperty(C, &eye->ptr, &eye->prop, &eye->index);
99
100         if ((eye->ptr.data == NULL) ||
101             (eye->prop == NULL) ||
102             (RNA_property_editable(&eye->ptr, eye->prop) == FALSE) ||
103             (RNA_property_array_length(&eye->ptr, eye->prop) < 3) ||
104             (RNA_property_type(eye->prop) != PROP_FLOAT))
105         {
106                 return FALSE;
107         }
108
109         if (RNA_property_subtype(eye->prop) == PROP_COLOR) {
110                 const char *display_device;
111
112                 display_device = scene->display_settings.display_device;
113                 eye->display = IMB_colormanagement_display_get_named(display_device);
114         }
115
116         return TRUE;
117 }
118
119 static void eyedropper_exit(bContext *C, wmOperator *op)
120 {
121         WM_cursor_modal_restore(CTX_wm_window(C));
122         
123         if (op->customdata)
124                 MEM_freeN(op->customdata);
125         op->customdata = NULL;
126 }
127
128 static int eyedropper_cancel(bContext *C, wmOperator *op)
129 {
130         eyedropper_exit(C, op);
131         return OPERATOR_CANCELLED;
132 }
133
134 /* *** eyedropper_color_ helper functions *** */
135
136 /**
137  * \brief get the color from the screen.
138  *
139  * Special check for image or nodes where we MAY have HDR pixels which don't display.
140  */
141 static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int mx, int my, float r_col[3])
142 {
143
144         /* we could use some clever */
145         wmWindow *win = CTX_wm_window(C);
146         ScrArea *sa;
147         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
148                 if (BLI_rcti_isect_pt(&sa->totrct, mx, my)) {
149                         if (sa->spacetype == SPACE_IMAGE) {
150                                 ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
151                                 if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
152                                         SpaceImage *sima = sa->spacedata.first;
153                                         int mval[2] = {mx - ar->winrct.xmin,
154                                                        my - ar->winrct.ymin};
155
156                                         if (ED_space_image_color_sample(sima, ar, mval, r_col)) {
157                                                 return;
158                                         }
159                                 }
160                         }
161                         else if (sa->spacetype == SPACE_NODE) {
162                                 ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
163                                 if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
164                                         SpaceNode *snode = sa->spacedata.first;
165                                         int mval[2] = {mx - ar->winrct.xmin,
166                                                        my - ar->winrct.ymin};
167
168                                         if (ED_space_node_color_sample(snode, ar, mval, r_col)) {
169                                                 return;
170                                         }
171                                 }
172                         }
173                         else if (sa->spacetype == SPACE_CLIP) {
174                                 ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
175                                 if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
176                                         SpaceClip *sc = sa->spacedata.first;
177                                         int mval[2] = {mx - ar->winrct.xmin,
178                                                        my - ar->winrct.ymin};
179
180                                         if (ED_space_clip_color_sample(sc, ar, mval, r_col)) {
181                                                 return;
182                                         }
183                                 }
184                         }
185                 }
186         }
187
188         /* fallback to simple opengl picker */
189         glReadBuffer(GL_FRONT);
190         glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, r_col);
191         glReadBuffer(GL_BACK);
192 }
193
194 /* sets the sample color RGB, maintaining A */
195 static void eyedropper_color_set(bContext *C, Eyedropper *eye, const float col[3])
196 {
197         float col_conv[4];
198
199         /* to maintain alpha */
200         RNA_property_float_get_array(&eye->ptr, eye->prop, col_conv);
201
202         /* convert from display space to linear rgb space */
203         if (eye->display) {
204                 copy_v3_v3(col_conv, col);
205                 IMB_colormanagement_display_to_scene_linear_v3(col_conv, eye->display);
206         }
207         else {
208                 copy_v3_v3(col_conv, col);
209         }
210
211         RNA_property_float_set_array(&eye->ptr, eye->prop, col_conv);
212
213         RNA_property_update(C, &eye->ptr, eye->prop);
214 }
215
216 /* set sample from accumulated values */
217 static void eyedropper_color_set_accum(bContext *C, Eyedropper *eye)
218 {
219         float col[3];
220         mul_v3_v3fl(col, eye->accum_col, 1.0f / (float)eye->accum_tot);
221         eyedropper_color_set(C, eye, col);
222 }
223
224 /* single point sample & set */
225 static void eyedropper_color_sample(bContext *C, Eyedropper *eye, int mx, int my)
226 {
227         float col[3];
228         eyedropper_color_sample_fl(C, eye, mx, my, col);
229         eyedropper_color_set(C, eye, col);
230 }
231
232 static void eyedropper_color_sample_accum(bContext *C, Eyedropper *eye, int mx, int my)
233 {
234         float col[3];
235         eyedropper_color_sample_fl(C, eye, mx, my, col);
236         /* delay linear conversion */
237         add_v3_v3(eye->accum_col, col);
238         eye->accum_tot++;
239 }
240
241 /* main modal status check */
242 static int eyedropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
243 {
244         Eyedropper *eye = (Eyedropper *)op->customdata;
245         
246         switch (event->type) {
247                 case ESCKEY:
248                 case RIGHTMOUSE:
249                         return eyedropper_cancel(C, op);
250                 case LEFTMOUSE:
251                         if (event->val == KM_RELEASE) {
252                                 if (eye->accum_tot == 0) {
253                                         eyedropper_color_sample(C, eye, event->x, event->y);
254                                 }
255                                 else {
256                                         eyedropper_color_set_accum(C, eye);
257                                 }
258                                 eyedropper_exit(C, op);
259                                 return OPERATOR_FINISHED;
260                         }
261                         else if (event->val == KM_PRESS) {
262                                 /* enable accum and make first sample */
263                                 eye->accum_start = TRUE;
264                                 eyedropper_color_sample_accum(C, eye, event->x, event->y);
265                         }
266                         break;
267                 case MOUSEMOVE:
268                         if (eye->accum_start) {
269                                 /* button is pressed so keep sampling */
270                                 eyedropper_color_sample_accum(C, eye, event->x, event->y);
271                                 eyedropper_color_set_accum(C, eye);
272                         }
273                         break;
274                 case SPACEKEY:
275                         if (event->val == KM_RELEASE) {
276                                 eye->accum_tot = 0;
277                                 zero_v3(eye->accum_col);
278                                 eyedropper_color_sample_accum(C, eye, event->x, event->y);
279                                 eyedropper_color_set_accum(C, eye);
280                         }
281                         break;
282         }
283         
284         return OPERATOR_RUNNING_MODAL;
285 }
286
287 /* Modal Operator init */
288 static int eyedropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
289 {
290         /* init */
291         if (eyedropper_init(C, op)) {
292                 WM_cursor_modal_set(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
293
294                 /* add temp handler */
295                 WM_event_add_modal_handler(C, op);
296                 
297                 return OPERATOR_RUNNING_MODAL;
298         }
299         else {
300                 eyedropper_exit(C, op);
301                 return OPERATOR_CANCELLED;
302         }
303 }
304
305 /* Repeat operator */
306 static int eyedropper_exec(bContext *C, wmOperator *op)
307 {
308         /* init */
309         if (eyedropper_init(C, op)) {
310                 
311                 /* do something */
312                 
313                 /* cleanup */
314                 eyedropper_exit(C, op);
315                 
316                 return OPERATOR_FINISHED;
317         }
318         else {
319                 return OPERATOR_CANCELLED;
320         }
321 }
322
323 static int eyedropper_poll(bContext *C)
324 {
325         if (!CTX_wm_window(C)) return 0;
326         else return 1;
327 }
328
329 static void UI_OT_eyedropper(wmOperatorType *ot)
330 {
331         /* identifiers */
332         ot->name = "Eyedropper";
333         ot->idname = "UI_OT_eyedropper";
334         ot->description = "Sample a color from the Blender Window to store in a property";
335         
336         /* api callbacks */
337         ot->invoke = eyedropper_invoke;
338         ot->modal = eyedropper_modal;
339         ot->cancel = eyedropper_cancel;
340         ot->exec = eyedropper_exec;
341         ot->poll = eyedropper_poll;
342         
343         /* flags */
344         ot->flag = OPTYPE_BLOCKING;
345         
346         /* properties */
347 }
348
349 /* Reset Default Theme ------------------------ */
350
351 static int reset_default_theme_exec(bContext *C, wmOperator *UNUSED(op))
352 {
353         ui_theme_init_default();
354         ui_style_init_default();
355         WM_event_add_notifier(C, NC_WINDOW, NULL);
356         
357         return OPERATOR_FINISHED;
358 }
359
360 static void UI_OT_reset_default_theme(wmOperatorType *ot)
361 {
362         /* identifiers */
363         ot->name = "Reset to Default Theme";
364         ot->idname = "UI_OT_reset_default_theme";
365         ot->description = "Reset to the default theme colors";
366         
367         /* callbacks */
368         ot->exec = reset_default_theme_exec;
369         
370         /* flags */
371         ot->flag = OPTYPE_REGISTER;
372 }
373
374 /* Copy Data Path Operator ------------------------ */
375
376 static int copy_data_path_button_poll(bContext *C)
377 {
378         PointerRNA ptr;
379         PropertyRNA *prop;
380         char *path;
381         int index;
382
383         uiContextActiveProperty(C, &ptr, &prop, &index);
384
385         if (ptr.id.data && ptr.data && prop) {
386                 path = RNA_path_from_ID_to_property(&ptr, prop);
387                 
388                 if (path) {
389                         MEM_freeN(path);
390                         return 1;
391                 }
392         }
393
394         return 0;
395 }
396
397 static int copy_data_path_button_exec(bContext *C, wmOperator *UNUSED(op))
398 {
399         PointerRNA ptr;
400         PropertyRNA *prop;
401         char *path;
402         int index;
403
404         /* try to create driver using property retrieved from UI */
405         uiContextActiveProperty(C, &ptr, &prop, &index);
406
407         if (ptr.id.data && ptr.data && prop) {
408                 path = RNA_path_from_ID_to_property(&ptr, prop);
409                 
410                 if (path) {
411                         WM_clipboard_text_set(path, FALSE);
412                         MEM_freeN(path);
413                         return OPERATOR_FINISHED;
414                 }
415         }
416
417         return OPERATOR_CANCELLED;
418 }
419
420 static void UI_OT_copy_data_path_button(wmOperatorType *ot)
421 {
422         /* identifiers */
423         ot->name = "Copy Data Path";
424         ot->idname = "UI_OT_copy_data_path_button";
425         ot->description = "Copy the RNA data path for this property to the clipboard";
426
427         /* callbacks */
428         ot->exec = copy_data_path_button_exec;
429         ot->poll = copy_data_path_button_poll;
430
431         /* flags */
432         ot->flag = OPTYPE_REGISTER;
433 }
434
435 /* Reset to Default Values Button Operator ------------------------ */
436
437 static int operator_button_property_finish(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
438 {
439         ID *id = ptr->id.data;
440
441         /* perform updates required for this property */
442         RNA_property_update(C, ptr, prop);
443
444         /* as if we pressed the button */
445         uiContextActivePropertyHandle(C);
446
447         /* Since we don't want to undo _all_ edits to settings, eg window
448          * edits on the screen or on operator settings.
449          * it might be better to move undo's inline - campbell */
450         if (id && ID_CHECK_UNDO(id)) {
451                 /* do nothing, go ahead with undo */
452                 return OPERATOR_FINISHED;
453         }
454         else {
455                 return OPERATOR_CANCELLED;
456         }
457 }
458
459 static int reset_default_button_poll(bContext *C)
460 {
461         PointerRNA ptr;
462         PropertyRNA *prop;
463         int index;
464
465         uiContextActiveProperty(C, &ptr, &prop, &index);
466         
467         return (ptr.data && prop && RNA_property_editable(&ptr, prop));
468 }
469
470 static int reset_default_button_exec(bContext *C, wmOperator *op)
471 {
472         PointerRNA ptr;
473         PropertyRNA *prop;
474         int index, all = RNA_boolean_get(op->ptr, "all");
475
476         /* try to reset the nominated setting to its default value */
477         uiContextActiveProperty(C, &ptr, &prop, &index);
478         
479         /* if there is a valid property that is editable... */
480         if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
481                 if (RNA_property_reset(&ptr, prop, (all) ? -1 : index))
482                         return operator_button_property_finish(C, &ptr, prop);
483         }
484
485         return OPERATOR_CANCELLED;
486 }
487
488 static void UI_OT_reset_default_button(wmOperatorType *ot)
489 {
490         /* identifiers */
491         ot->name = "Reset to Default Value";
492         ot->idname = "UI_OT_reset_default_button";
493         ot->description = "Reset this property's value to its default value";
494
495         /* callbacks */
496         ot->poll = reset_default_button_poll;
497         ot->exec = reset_default_button_exec;
498
499         /* flags */
500         ot->flag = OPTYPE_UNDO;
501         
502         /* properties */
503         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
504 }
505
506 /* Unset Property Button Operator ------------------------ */
507
508 static int unset_property_button_exec(bContext *C, wmOperator *UNUSED(op))
509 {
510         PointerRNA ptr;
511         PropertyRNA *prop;
512         int index;
513
514         /* try to unset the nominated property */
515         uiContextActiveProperty(C, &ptr, &prop, &index);
516
517         /* if there is a valid property that is editable... */
518         if (ptr.data && prop && RNA_property_editable(&ptr, prop)
519             /*&& RNA_property_is_idprop(prop)*/ && RNA_property_is_set(&ptr, prop))
520         {
521                 RNA_property_unset(&ptr, prop);
522                 return operator_button_property_finish(C, &ptr, prop);
523         }
524
525         return OPERATOR_CANCELLED;
526 }
527
528 static void UI_OT_unset_property_button(wmOperatorType *ot)
529 {
530         /* identifiers */
531         ot->name = "Unset property";
532         ot->idname = "UI_OT_unset_property_button";
533         ot->description = "Clear the property and use default or generated value in operators";
534
535         /* callbacks */
536         ot->poll = ED_operator_regionactive;
537         ot->exec = unset_property_button_exec;
538
539         /* flags */
540         ot->flag = OPTYPE_UNDO;
541 }
542
543 /* Copy To Selected Operator ------------------------ */
544
545 static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb, int *use_path)
546 {
547         *use_path = FALSE;
548
549         if (RNA_struct_is_a(ptr->type, &RNA_EditBone))
550                 *lb = CTX_data_collection_get(C, "selected_editable_bones");
551         else if (RNA_struct_is_a(ptr->type, &RNA_PoseBone))
552                 *lb = CTX_data_collection_get(C, "selected_pose_bones");
553         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence))
554                 *lb = CTX_data_collection_get(C, "selected_editable_sequences");
555         else {
556                 ID *id = ptr->id.data;
557
558                 if (id && GS(id->name) == ID_OB) {
559                         *lb = CTX_data_collection_get(C, "selected_editable_objects");
560                         *use_path = TRUE;
561                 }
562                 else
563                         return 0;
564         }
565         
566         return 1;
567 }
568
569 static int copy_to_selected_button_poll(bContext *C)
570 {
571         PointerRNA ptr, lptr, idptr;
572         PropertyRNA *prop, *lprop;
573         int index, success = 0;
574
575         uiContextActiveProperty(C, &ptr, &prop, &index);
576
577         if (ptr.data && prop) {
578                 char *path = NULL;
579                 int use_path;
580                 CollectionPointerLink *link;
581                 ListBase lb;
582
583                 if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
584                         return success;
585
586                 if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
587                         for (link = lb.first; link; link = link->next) {
588                                 if (link->ptr.data != ptr.data) {
589                                         if (use_path) {
590                                                 lprop = NULL;
591                                                 RNA_id_pointer_create(link->ptr.id.data, &idptr);
592                                                 RNA_path_resolve_property(&idptr, path, &lptr, &lprop);
593                                         }
594                                         else {
595                                                 lptr = link->ptr;
596                                                 lprop = prop;
597                                         }
598
599                                         if (lprop == prop) {
600                                                 if (RNA_property_editable(&lptr, prop))
601                                                         success = 1;
602                                         }
603                                 }
604                         }
605
606                         if (path)
607                                 MEM_freeN(path);
608                 }
609
610                 BLI_freelistN(&lb);
611         }
612
613         return success;
614 }
615
616 static int copy_to_selected_button_exec(bContext *C, wmOperator *op)
617 {
618         PointerRNA ptr, lptr, idptr;
619         PropertyRNA *prop, *lprop;
620         int success = 0;
621         int index, all = RNA_boolean_get(op->ptr, "all");
622
623         /* try to reset the nominated setting to its default value */
624         uiContextActiveProperty(C, &ptr, &prop, &index);
625         
626         /* if there is a valid property that is editable... */
627         if (ptr.data && prop) {
628                 char *path = NULL;
629                 int use_path;
630                 CollectionPointerLink *link;
631                 ListBase lb;
632
633                 if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
634                         return success;
635
636                 if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
637                         for (link = lb.first; link; link = link->next) {
638                                 if (link->ptr.data != ptr.data) {
639                                         if (use_path) {
640                                                 lprop = NULL;
641                                                 RNA_id_pointer_create(link->ptr.id.data, &idptr);
642                                                 RNA_path_resolve_property(&idptr, path, &lptr, &lprop);
643                                         }
644                                         else {
645                                                 lptr = link->ptr;
646                                                 lprop = prop;
647                                         }
648
649                                         if (lprop == prop) {
650                                                 if (RNA_property_editable(&lptr, lprop)) {
651                                                         if (RNA_property_copy(&lptr, &ptr, prop, (all) ? -1 : index)) {
652                                                                 RNA_property_update(C, &lptr, prop);
653                                                                 success = 1;
654                                                         }
655                                                 }
656                                         }
657                                 }
658                         }
659
660                         if (path)
661                                 MEM_freeN(path);
662                 }
663
664                 BLI_freelistN(&lb);
665         }
666         
667         return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
668 }
669
670 static void UI_OT_copy_to_selected_button(wmOperatorType *ot)
671 {
672         /* identifiers */
673         ot->name = "Copy To Selected";
674         ot->idname = "UI_OT_copy_to_selected_button";
675         ot->description = "Copy property from this object to selected objects or bones";
676
677         /* callbacks */
678         ot->poll = copy_to_selected_button_poll;
679         ot->exec = copy_to_selected_button_exec;
680
681         /* flags */
682         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
683
684         /* properties */
685         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
686 }
687
688 /* Reports to Textblock Operator ------------------------ */
689
690 /* FIXME: this is just a temporary operator so that we can see all the reports somewhere 
691  * when there are too many to display...
692  */
693
694 static int reports_to_text_poll(bContext *C)
695 {
696         return CTX_wm_reports(C) != NULL;
697 }
698
699 static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
700 {
701         ReportList *reports = CTX_wm_reports(C);
702         Main *bmain = CTX_data_main(C);
703         Text *txt;
704         char *str;
705         
706         /* create new text-block to write to */
707         txt = BKE_text_add(bmain, "Recent Reports");
708         
709         /* convert entire list to a display string, and add this to the text-block
710          *      - if commandline debug option enabled, show debug reports too
711          *      - otherwise, up to info (which is what users normally see)
712          */
713         str = BKE_reports_string(reports, (G.debug & G_DEBUG) ? RPT_DEBUG : RPT_INFO);
714
715         if (str) {
716                 BKE_text_write(txt, str);
717                 MEM_freeN(str);
718
719                 return OPERATOR_FINISHED;
720         }
721         else {
722                 return OPERATOR_CANCELLED;
723         }
724 }
725
726 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
727 {
728         /* identifiers */
729         ot->name = "Reports to Text Block";
730         ot->idname = "UI_OT_reports_to_textblock";
731         ot->description = "Write the reports ";
732         
733         /* callbacks */
734         ot->poll = reports_to_text_poll;
735         ot->exec = reports_to_text_exec;
736 }
737
738 #ifdef WITH_PYTHON
739
740 /* ------------------------------------------------------------------------- */
741 /* EditSource Utility funcs and operator,
742  * note, this includes utility functions and button matching checks */
743
744 typedef struct uiEditSourceStore {
745         uiBut but_orig;
746         GHash *hash;
747 } uiEditSourceStore;
748
749 typedef struct uiEditSourceButStore {
750         char py_dbg_fn[FILE_MAX];
751         int py_dbg_ln;
752 } uiEditSourceButStore;
753
754 /* should only ever be set while the edit source operator is running */
755 static struct uiEditSourceStore *ui_editsource_info = NULL;
756
757 bool UI_editsource_enable_check(void)
758 {
759         return (ui_editsource_info != NULL);
760 }
761
762 static void ui_editsource_active_but_set(uiBut *but)
763 {
764         BLI_assert(ui_editsource_info == NULL);
765
766         ui_editsource_info = MEM_callocN(sizeof(uiEditSourceStore), __func__);
767         memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
768
769         ui_editsource_info->hash = BLI_ghash_ptr_new(__func__);
770 }
771
772 static void ui_editsource_active_but_clear(void)
773 {
774         BLI_ghash_free(ui_editsource_info->hash, NULL, MEM_freeN);
775         MEM_freeN(ui_editsource_info);
776         ui_editsource_info = NULL;
777 }
778
779 static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
780 {
781 #if 0
782         printf("matching buttons: '%s' == '%s'\n",
783                but_a->drawstr, but_b->drawstr);
784 #endif
785
786         /* this just needs to be a 'good-enough' comparison so we can know beyond
787          * reasonable doubt that these buttons are the same between redraws.
788          * if this fails it only means edit-source fails - campbell */
789         if (BLI_rctf_compare(&but_a->rect, &but_b->rect, FLT_EPSILON) &&
790             (but_a->type == but_b->type) &&
791             (but_a->rnaprop == but_b->rnaprop) &&
792             (but_a->optype == but_b->optype) &&
793             (but_a->unit_type == but_b->unit_type) &&
794             (strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0))
795         {
796                 return TRUE;
797         }
798         else {
799                 return FALSE;
800         }
801 }
802
803 void UI_editsource_active_but_test(uiBut *but)
804 {
805         extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
806
807         struct uiEditSourceButStore *but_store = MEM_callocN(sizeof(uiEditSourceButStore), __func__);
808
809         const char *fn;
810         int lineno = -1;
811
812 #if 0
813         printf("comparing buttons: '%s' == '%s'\n",
814                but->drawstr, ui_editsource_info->but_orig.drawstr);
815 #endif
816
817         PyC_FileAndNum_Safe(&fn, &lineno);
818
819         if (lineno != -1) {
820                 BLI_strncpy(but_store->py_dbg_fn, fn,
821                             sizeof(but_store->py_dbg_fn));
822                 but_store->py_dbg_ln = lineno;
823         }
824         else {
825                 but_store->py_dbg_fn[0] = '\0';
826                 but_store->py_dbg_ln = -1;
827         }
828
829         BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
830 }
831
832 static int editsource_text_edit(bContext *C, wmOperator *op,
833                                 char filepath[FILE_MAX], int line)
834 {
835         struct Main *bmain = CTX_data_main(C);
836         Text *text;
837
838         for (text = bmain->text.first; text; text = text->id.next) {
839                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
840                         break;
841                 }
842         }
843
844         if (text == NULL) {
845                 text = BKE_text_load(bmain, filepath, bmain->name);
846         }
847
848         if (text == NULL) {
849                 BKE_reportf(op->reports, RPT_WARNING, "File '%s' cannot be opened", filepath);
850                 return OPERATOR_CANCELLED;
851         }
852         else {
853                 /* naughty!, find text area to set, not good behavior
854                  * but since this is a dev tool lets allow it - campbell */
855                 ScrArea *sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
856                 if (sa) {
857                         SpaceText *st = sa->spacedata.first;
858                         st->text = text;
859                 }
860                 else {
861                         BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2);
862                 }
863
864                 txt_move_toline(text, line - 1, FALSE);
865                 WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text);
866         }
867
868         return OPERATOR_FINISHED;
869 }
870
871 static int editsource_exec(bContext *C, wmOperator *op)
872 {
873         uiBut *but = uiContextActiveButton(C);
874
875         if (but) {
876                 GHashIterator ghi;
877                 struct uiEditSourceButStore *but_store = NULL;
878
879                 ARegion *ar = CTX_wm_region(C);
880                 int ret;
881
882                 /* needed else the active button does not get tested */
883                 uiFreeActiveButtons(C, CTX_wm_screen(C));
884
885                 // printf("%s: begin\n", __func__);
886
887                 /* take care not to return before calling ui_editsource_active_but_clear */
888                 ui_editsource_active_but_set(but);
889
890                 /* redraw and get active button python info */
891                 ED_region_do_draw(C, ar);
892
893                 for (BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
894                      BLI_ghashIterator_done(&ghi) == false;
895                      BLI_ghashIterator_step(&ghi))
896                 {
897                         uiBut *but_key = BLI_ghashIterator_getKey(&ghi);
898                         if (but_key && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but_key)) {
899                                 but_store = BLI_ghashIterator_getValue(&ghi);
900                                 break;
901                         }
902
903                 }
904
905                 if (but_store) {
906                         if (but_store->py_dbg_ln != -1) {
907                                 ret = editsource_text_edit(C, op,
908                                                            but_store->py_dbg_fn,
909                                                            but_store->py_dbg_ln);
910                         }
911                         else {
912                                 BKE_report(op->reports, RPT_ERROR, "Active button is not from a script, cannot edit source");
913                                 ret = OPERATOR_CANCELLED;
914                         }
915                 }
916                 else {
917                         BKE_report(op->reports, RPT_ERROR, "Active button match cannot be found");
918                         ret = OPERATOR_CANCELLED;
919                 }
920
921
922                 ui_editsource_active_but_clear();
923
924                 // printf("%s: end\n", __func__);
925
926                 return ret;
927         }
928         else {
929                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
930                 return OPERATOR_CANCELLED;
931         }
932 }
933
934 static void UI_OT_editsource(wmOperatorType *ot)
935 {
936         /* identifiers */
937         ot->name = "Edit Source";
938         ot->idname = "UI_OT_editsource";
939         ot->description = "Edit UI source code of the active button";
940
941         /* callbacks */
942         ot->exec = editsource_exec;
943 }
944
945 /* ------------------------------------------------------------------------- */
946 /* EditTranslation utility funcs and operator,
947  * Note: this includes utility functions and button matching checks.
948  *       this only works in conjunction with a py operator! */
949
950 static void edittranslation_find_po_file(const char *root, const char *uilng, char *path, const size_t maxlen)
951 {
952         char tstr[32]; /* Should be more than enough! */
953
954         /* First, full lang code. */
955         BLI_snprintf(tstr, sizeof(tstr), "%s.po", uilng);
956         BLI_join_dirfile(path, maxlen, root, uilng);
957         BLI_path_append(path, maxlen, tstr);
958         if (BLI_is_file(path))
959                 return;
960
961         /* Now try without the second iso code part (_ES in es_ES). */
962         {
963                 char *tc = NULL;
964                 size_t szt = 0;
965                 tstr[0] = '\0';
966
967                 tc = strchr(uilng, '_');
968                 if (tc) {
969                         szt = tc - uilng;
970                         if (szt < sizeof(tstr)) /* Paranoid, should always be true! */
971                                 BLI_strncpy(tstr, uilng, szt + 1); /* +1 for '\0' char! */
972                 }
973                 if (tstr[0]) {
974                         /* Because of some codes like sr_SR@latin... */
975                         tc = strchr(uilng, '@');
976                         if (tc)
977                                 BLI_strncpy(tstr + szt, tc, sizeof(tstr) - szt);
978
979                         BLI_join_dirfile(path, maxlen, root, tstr);
980                         strcat(tstr, ".po");
981                         BLI_path_append(path, maxlen, tstr);
982                         if (BLI_is_file(path))
983                                 return;
984                 }
985         }
986
987         /* Else no po file! */
988         path[0] = '\0';
989 }
990
991 static int edittranslation_exec(bContext *C, wmOperator *op)
992 {
993         uiBut *but = uiContextActiveButton(C);
994         int ret = OPERATOR_CANCELLED;
995
996         if (but) {
997                 PointerRNA ptr;
998                 char popath[FILE_MAX];
999                 const char *root = U.i18ndir;
1000                 const char *uilng = BLF_lang_get();
1001
1002                 uiStringInfo but_label = {BUT_GET_LABEL, NULL};
1003                 uiStringInfo rna_label = {BUT_GET_RNA_LABEL, NULL};
1004                 uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
1005                 uiStringInfo but_tip = {BUT_GET_TIP, NULL};
1006                 uiStringInfo rna_tip = {BUT_GET_RNA_TIP, NULL};
1007                 uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
1008                 uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
1009                 uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
1010                 uiStringInfo rna_enum = {BUT_GET_RNAENUM_IDENTIFIER, NULL};
1011                 uiStringInfo rna_ctxt = {BUT_GET_RNA_LABEL_CONTEXT, NULL};
1012
1013                 if (!BLI_is_dir(root)) {
1014                         BKE_report(op->reports, RPT_ERROR, "Please set your User Preferences' 'Translation Branches "
1015                                                            "Directory' path to a valid directory");
1016                         return OPERATOR_CANCELLED;
1017                 }
1018                 if (!WM_operatortype_find(EDTSRC_I18N_OP_NAME, 0)) {
1019                         BKE_reportf(op->reports, RPT_ERROR, "Could not find operator '%s'! Please enable ui_translate addon "
1020                                                             "in the User Preferences", EDTSRC_I18N_OP_NAME);
1021                         return OPERATOR_CANCELLED;
1022                 }
1023                 /* Try to find a valid po file for current language... */
1024                 edittranslation_find_po_file(root, uilng, popath, FILE_MAX);
1025 /*              printf("po path: %s\n", popath);*/
1026                 if (popath[0] == '\0') {
1027                         BKE_reportf(op->reports, RPT_ERROR, "No valid po found for language '%s' under %s", uilng, root);
1028                         return OPERATOR_CANCELLED;
1029                 }
1030
1031                 uiButGetStrInfo(C, but, &but_label, &rna_label, &enum_label, &but_tip, &rna_tip, &enum_tip,
1032                                 &rna_struct, &rna_prop, &rna_enum, &rna_ctxt, NULL);
1033
1034                 WM_operator_properties_create(&ptr, EDTSRC_I18N_OP_NAME);
1035                 RNA_string_set(&ptr, "lang", uilng);
1036                 RNA_string_set(&ptr, "po_file", popath);
1037                 RNA_string_set(&ptr, "but_label", but_label.strinfo);
1038                 RNA_string_set(&ptr, "rna_label", rna_label.strinfo);
1039                 RNA_string_set(&ptr, "enum_label", enum_label.strinfo);
1040                 RNA_string_set(&ptr, "but_tip", but_tip.strinfo);
1041                 RNA_string_set(&ptr, "rna_tip", rna_tip.strinfo);
1042                 RNA_string_set(&ptr, "enum_tip", enum_tip.strinfo);
1043                 RNA_string_set(&ptr, "rna_struct", rna_struct.strinfo);
1044                 RNA_string_set(&ptr, "rna_prop", rna_prop.strinfo);
1045                 RNA_string_set(&ptr, "rna_enum", rna_enum.strinfo);
1046                 RNA_string_set(&ptr, "rna_ctxt", rna_ctxt.strinfo);
1047                 ret = WM_operator_name_call(C, EDTSRC_I18N_OP_NAME, WM_OP_INVOKE_DEFAULT, &ptr);
1048
1049                 /* Clean up */
1050                 if (but_label.strinfo)
1051                         MEM_freeN(but_label.strinfo);
1052                 if (rna_label.strinfo)
1053                         MEM_freeN(rna_label.strinfo);
1054                 if (enum_label.strinfo)
1055                         MEM_freeN(enum_label.strinfo);
1056                 if (but_tip.strinfo)
1057                         MEM_freeN(but_tip.strinfo);
1058                 if (rna_tip.strinfo)
1059                         MEM_freeN(rna_tip.strinfo);
1060                 if (enum_tip.strinfo)
1061                         MEM_freeN(enum_tip.strinfo);
1062                 if (rna_struct.strinfo)
1063                         MEM_freeN(rna_struct.strinfo);
1064                 if (rna_prop.strinfo)
1065                         MEM_freeN(rna_prop.strinfo);
1066                 if (rna_enum.strinfo)
1067                         MEM_freeN(rna_enum.strinfo);
1068                 if (rna_ctxt.strinfo)
1069                         MEM_freeN(rna_ctxt.strinfo);
1070
1071                 return ret;
1072         }
1073         else {
1074                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
1075                 return OPERATOR_CANCELLED;
1076         }
1077 }
1078
1079 static void UI_OT_edittranslation_init(wmOperatorType *ot)
1080 {
1081         /* identifiers */
1082         ot->name = "Edit Translation";
1083         ot->idname = "UI_OT_edittranslation_init";
1084         ot->description = "Edit i18n in current language for the active button";
1085
1086         /* callbacks */
1087         ot->exec = edittranslation_exec;
1088 }
1089
1090 #endif /* WITH_PYTHON */
1091
1092 static int reloadtranslation_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1093 {
1094         BLF_lang_init();
1095         BLF_cache_clear();
1096         BLF_lang_set(NULL);
1097         UI_reinit_font();
1098         return OPERATOR_FINISHED;
1099 }
1100
1101 static void UI_OT_reloadtranslation(wmOperatorType *ot)
1102 {
1103         /* identifiers */
1104         ot->name = "Reload Translation";
1105         ot->idname = "UI_OT_reloadtranslation";
1106         ot->description = "Force a full reload of UI translation";
1107
1108         /* callbacks */
1109         ot->exec = reloadtranslation_exec;
1110 }
1111
1112 /* ********************************************************* */
1113 /* Registration */
1114
1115 void UI_buttons_operatortypes(void)
1116 {
1117         WM_operatortype_append(UI_OT_eyedropper);
1118         WM_operatortype_append(UI_OT_reset_default_theme);
1119         WM_operatortype_append(UI_OT_copy_data_path_button);
1120         WM_operatortype_append(UI_OT_reset_default_button);
1121         WM_operatortype_append(UI_OT_unset_property_button);
1122         WM_operatortype_append(UI_OT_copy_to_selected_button);
1123         WM_operatortype_append(UI_OT_reports_to_textblock);  /* XXX: temp? */
1124
1125 #ifdef WITH_PYTHON
1126         WM_operatortype_append(UI_OT_editsource);
1127         WM_operatortype_append(UI_OT_edittranslation_init);
1128 #endif
1129         WM_operatortype_append(UI_OT_reloadtranslation);
1130 }