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