rename cursor setting functions to make modal set/restore more clearly related functions.
[blender-staging.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 reset_default_button_poll(bContext *C)
438 {
439         PointerRNA ptr;
440         PropertyRNA *prop;
441         int index;
442
443         uiContextActiveProperty(C, &ptr, &prop, &index);
444         
445         return (ptr.data && prop && RNA_property_editable(&ptr, prop));
446 }
447
448 static int reset_default_button_exec(bContext *C, wmOperator *op)
449 {
450         PointerRNA ptr;
451         PropertyRNA *prop;
452         int success = 0;
453         int index, all = RNA_boolean_get(op->ptr, "all");
454
455         /* try to reset the nominated setting to its default value */
456         uiContextActiveProperty(C, &ptr, &prop, &index);
457         
458         /* if there is a valid property that is editable... */
459         if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
460                 if (RNA_property_reset(&ptr, prop, (all) ? -1 : index)) {
461                         /* perform updates required for this property */
462                         RNA_property_update(C, &ptr, prop);
463
464                         /* as if we pressed the button */
465                         uiContextActivePropertyHandle(C);
466
467                         success = 1;
468                 }
469         }
470
471         /* Since we don't want to undo _all_ edits to settings, eg window
472          * edits on the screen or on operator settings.
473          * it might be better to move undo's inline - campbell */
474         if (success) {
475                 ID *id = ptr.id.data;
476                 if (id && ID_CHECK_UNDO(id)) {
477                         /* do nothing, go ahead with undo */
478                 }
479                 else {
480                         return OPERATOR_CANCELLED;
481                 }
482         }
483         /* end hack */
484
485         return (success) ? OPERATOR_FINISHED : 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 /* Copy To Selected Operator ------------------------ */
507
508 static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb, int *use_path)
509 {
510         *use_path = FALSE;
511
512         if (RNA_struct_is_a(ptr->type, &RNA_EditBone))
513                 *lb = CTX_data_collection_get(C, "selected_editable_bones");
514         else if (RNA_struct_is_a(ptr->type, &RNA_PoseBone))
515                 *lb = CTX_data_collection_get(C, "selected_pose_bones");
516         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence))
517                 *lb = CTX_data_collection_get(C, "selected_editable_sequences");
518         else {
519                 ID *id = ptr->id.data;
520
521                 if (id && GS(id->name) == ID_OB) {
522                         *lb = CTX_data_collection_get(C, "selected_editable_objects");
523                         *use_path = TRUE;
524                 }
525                 else
526                         return 0;
527         }
528         
529         return 1;
530 }
531
532 static int copy_to_selected_button_poll(bContext *C)
533 {
534         PointerRNA ptr, lptr, idptr;
535         PropertyRNA *prop, *lprop;
536         int index, success = 0;
537
538         uiContextActiveProperty(C, &ptr, &prop, &index);
539
540         if (ptr.data && prop) {
541                 char *path = NULL;
542                 int use_path;
543                 CollectionPointerLink *link;
544                 ListBase lb;
545
546                 if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
547                         return success;
548
549                 if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
550                         for (link = lb.first; link; link = link->next) {
551                                 if (link->ptr.data != ptr.data) {
552                                         if (use_path) {
553                                                 lprop = NULL;
554                                                 RNA_id_pointer_create(link->ptr.id.data, &idptr);
555                                                 RNA_path_resolve_property(&idptr, path, &lptr, &lprop);
556                                         }
557                                         else {
558                                                 lptr = link->ptr;
559                                                 lprop = prop;
560                                         }
561
562                                         if (lprop == prop) {
563                                                 if (RNA_property_editable(&lptr, prop))
564                                                         success = 1;
565                                         }
566                                 }
567                         }
568
569                         if (path)
570                                 MEM_freeN(path);
571                 }
572
573                 BLI_freelistN(&lb);
574         }
575
576         return success;
577 }
578
579 static int copy_to_selected_button_exec(bContext *C, wmOperator *op)
580 {
581         PointerRNA ptr, lptr, idptr;
582         PropertyRNA *prop, *lprop;
583         int success = 0;
584         int index, all = RNA_boolean_get(op->ptr, "all");
585
586         /* try to reset the nominated setting to its default value */
587         uiContextActiveProperty(C, &ptr, &prop, &index);
588         
589         /* if there is a valid property that is editable... */
590         if (ptr.data && prop) {
591                 char *path = NULL;
592                 int use_path;
593                 CollectionPointerLink *link;
594                 ListBase lb;
595
596                 if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
597                         return success;
598
599                 if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
600                         for (link = lb.first; link; link = link->next) {
601                                 if (link->ptr.data != ptr.data) {
602                                         if (use_path) {
603                                                 lprop = NULL;
604                                                 RNA_id_pointer_create(link->ptr.id.data, &idptr);
605                                                 RNA_path_resolve_property(&idptr, path, &lptr, &lprop);
606                                         }
607                                         else {
608                                                 lptr = link->ptr;
609                                                 lprop = prop;
610                                         }
611
612                                         if (lprop == prop) {
613                                                 if (RNA_property_editable(&lptr, lprop)) {
614                                                         if (RNA_property_copy(&lptr, &ptr, prop, (all) ? -1 : index)) {
615                                                                 RNA_property_update(C, &lptr, prop);
616                                                                 success = 1;
617                                                         }
618                                                 }
619                                         }
620                                 }
621                         }
622
623                         if (path)
624                                 MEM_freeN(path);
625                 }
626
627                 BLI_freelistN(&lb);
628         }
629         
630         return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
631 }
632
633 static void UI_OT_copy_to_selected_button(wmOperatorType *ot)
634 {
635         /* identifiers */
636         ot->name = "Copy To Selected";
637         ot->idname = "UI_OT_copy_to_selected_button";
638         ot->description = "Copy property from this object to selected objects or bones";
639
640         /* callbacks */
641         ot->poll = copy_to_selected_button_poll;
642         ot->exec = copy_to_selected_button_exec;
643
644         /* flags */
645         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
646
647         /* properties */
648         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
649 }
650
651 /* Reports to Textblock Operator ------------------------ */
652
653 /* FIXME: this is just a temporary operator so that we can see all the reports somewhere 
654  * when there are too many to display...
655  */
656
657 static int reports_to_text_poll(bContext *C)
658 {
659         return CTX_wm_reports(C) != NULL;
660 }
661
662 static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
663 {
664         ReportList *reports = CTX_wm_reports(C);
665         Main *bmain = CTX_data_main(C);
666         Text *txt;
667         char *str;
668         
669         /* create new text-block to write to */
670         txt = BKE_text_add(bmain, "Recent Reports");
671         
672         /* convert entire list to a display string, and add this to the text-block
673          *      - if commandline debug option enabled, show debug reports too
674          *      - otherwise, up to info (which is what users normally see)
675          */
676         str = BKE_reports_string(reports, (G.debug & G_DEBUG) ? RPT_DEBUG : RPT_INFO);
677
678         if (str) {
679                 BKE_text_write(txt, str);
680                 MEM_freeN(str);
681
682                 return OPERATOR_FINISHED;
683         }
684         else {
685                 return OPERATOR_CANCELLED;
686         }
687 }
688
689 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
690 {
691         /* identifiers */
692         ot->name = "Reports to Text Block";
693         ot->idname = "UI_OT_reports_to_textblock";
694         ot->description = "Write the reports ";
695         
696         /* callbacks */
697         ot->poll = reports_to_text_poll;
698         ot->exec = reports_to_text_exec;
699 }
700
701 #ifdef WITH_PYTHON
702
703 /* ------------------------------------------------------------------------- */
704 /* EditSource Utility funcs and operator,
705  * note, this includes utility functions and button matching checks */
706
707 typedef struct uiEditSourceStore {
708         uiBut but_orig;
709         GHash *hash;
710 } uiEditSourceStore;
711
712 typedef struct uiEditSourceButStore {
713         char py_dbg_fn[FILE_MAX];
714         int py_dbg_ln;
715 } uiEditSourceButStore;
716
717 /* should only ever be set while the edit source operator is running */
718 static struct uiEditSourceStore *ui_editsource_info = NULL;
719
720 bool UI_editsource_enable_check(void)
721 {
722         return (ui_editsource_info != NULL);
723 }
724
725 static void ui_editsource_active_but_set(uiBut *but)
726 {
727         BLI_assert(ui_editsource_info == NULL);
728
729         ui_editsource_info = MEM_callocN(sizeof(uiEditSourceStore), __func__);
730         memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
731
732         ui_editsource_info->hash = BLI_ghash_ptr_new(__func__);
733 }
734
735 static void ui_editsource_active_but_clear(void)
736 {
737         BLI_ghash_free(ui_editsource_info->hash, NULL, MEM_freeN);
738         MEM_freeN(ui_editsource_info);
739         ui_editsource_info = NULL;
740 }
741
742 static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
743 {
744 #if 0
745         printf("matching buttons: '%s' == '%s'\n",
746                but_a->drawstr, but_b->drawstr);
747 #endif
748
749         /* this just needs to be a 'good-enough' comparison so we can know beyond
750          * reasonable doubt that these buttons are the same between redraws.
751          * if this fails it only means edit-source fails - campbell */
752         if (BLI_rctf_compare(&but_a->rect, &but_b->rect, FLT_EPSILON) &&
753             (but_a->type == but_b->type) &&
754             (but_a->rnaprop == but_b->rnaprop) &&
755             (but_a->optype == but_b->optype) &&
756             (but_a->unit_type == but_b->unit_type) &&
757             (strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0))
758         {
759                 return TRUE;
760         }
761         else {
762                 return FALSE;
763         }
764 }
765
766 void UI_editsource_active_but_test(uiBut *but)
767 {
768         extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
769
770         struct uiEditSourceButStore *but_store = MEM_callocN(sizeof(uiEditSourceButStore), __func__);
771
772         const char *fn;
773         int lineno = -1;
774
775 #if 0
776         printf("comparing buttons: '%s' == '%s'\n",
777                but->drawstr, ui_editsource_info->but_orig.drawstr);
778 #endif
779
780         PyC_FileAndNum_Safe(&fn, &lineno);
781
782         if (lineno != -1) {
783                 BLI_strncpy(but_store->py_dbg_fn, fn,
784                             sizeof(but_store->py_dbg_fn));
785                 but_store->py_dbg_ln = lineno;
786         }
787         else {
788                 but_store->py_dbg_fn[0] = '\0';
789                 but_store->py_dbg_ln = -1;
790         }
791
792         BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
793 }
794
795 static int editsource_text_edit(bContext *C, wmOperator *op,
796                                 char filepath[FILE_MAX], int line)
797 {
798         struct Main *bmain = CTX_data_main(C);
799         Text *text;
800
801         for (text = bmain->text.first; text; text = text->id.next) {
802                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
803                         break;
804                 }
805         }
806
807         if (text == NULL) {
808                 text = BKE_text_load(bmain, filepath, bmain->name);
809         }
810
811         if (text == NULL) {
812                 BKE_reportf(op->reports, RPT_WARNING, "File '%s' cannot be opened", filepath);
813                 return OPERATOR_CANCELLED;
814         }
815         else {
816                 /* naughty!, find text area to set, not good behavior
817                  * but since this is a dev tool lets allow it - campbell */
818                 ScrArea *sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
819                 if (sa) {
820                         SpaceText *st = sa->spacedata.first;
821                         st->text = text;
822                 }
823                 else {
824                         BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2);
825                 }
826
827                 txt_move_toline(text, line - 1, FALSE);
828                 WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text);
829         }
830
831         return OPERATOR_FINISHED;
832 }
833
834 static int editsource_exec(bContext *C, wmOperator *op)
835 {
836         uiBut *but = uiContextActiveButton(C);
837
838         if (but) {
839                 GHashIterator ghi;
840                 struct uiEditSourceButStore *but_store = NULL;
841
842                 ARegion *ar = CTX_wm_region(C);
843                 int ret;
844
845                 /* needed else the active button does not get tested */
846                 uiFreeActiveButtons(C, CTX_wm_screen(C));
847
848                 // printf("%s: begin\n", __func__);
849
850                 /* take care not to return before calling ui_editsource_active_but_clear */
851                 ui_editsource_active_but_set(but);
852
853                 /* redraw and get active button python info */
854                 ED_region_do_draw(C, ar);
855
856                 for (BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
857                      BLI_ghashIterator_done(&ghi) == false;
858                      BLI_ghashIterator_step(&ghi))
859                 {
860                         uiBut *but_key = BLI_ghashIterator_getKey(&ghi);
861                         if (but_key && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but_key)) {
862                                 but_store = BLI_ghashIterator_getValue(&ghi);
863                                 break;
864                         }
865
866                 }
867
868                 if (but_store) {
869                         if (but_store->py_dbg_ln != -1) {
870                                 ret = editsource_text_edit(C, op,
871                                                            but_store->py_dbg_fn,
872                                                            but_store->py_dbg_ln);
873                         }
874                         else {
875                                 BKE_report(op->reports, RPT_ERROR, "Active button is not from a script, cannot edit source");
876                                 ret = OPERATOR_CANCELLED;
877                         }
878                 }
879                 else {
880                         BKE_report(op->reports, RPT_ERROR, "Active button match cannot be found");
881                         ret = OPERATOR_CANCELLED;
882                 }
883
884
885                 ui_editsource_active_but_clear();
886
887                 // printf("%s: end\n", __func__);
888
889                 return ret;
890         }
891         else {
892                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
893                 return OPERATOR_CANCELLED;
894         }
895 }
896
897 static void UI_OT_editsource(wmOperatorType *ot)
898 {
899         /* identifiers */
900         ot->name = "Edit Source";
901         ot->idname = "UI_OT_editsource";
902         ot->description = "Edit UI source code of the active button";
903
904         /* callbacks */
905         ot->exec = editsource_exec;
906 }
907
908 /* ------------------------------------------------------------------------- */
909 /* EditTranslation utility funcs and operator,
910  * Note: this includes utility functions and button matching checks.
911  *       this only works in conjunction with a py operator! */
912
913 static void edittranslation_find_po_file(const char *root, const char *uilng, char *path, const size_t maxlen)
914 {
915         char tstr[32]; /* Should be more than enough! */
916
917         /* First, full lang code. */
918         BLI_snprintf(tstr, sizeof(tstr), "%s.po", uilng);
919         BLI_join_dirfile(path, maxlen, root, uilng);
920         BLI_path_append(path, maxlen, tstr);
921         if (BLI_is_file(path))
922                 return;
923
924         /* Now try without the second iso code part (_ES in es_ES). */
925         {
926                 char *tc = NULL;
927                 size_t szt = 0;
928                 tstr[0] = '\0';
929
930                 tc = strchr(uilng, '_');
931                 if (tc) {
932                         szt = tc - uilng;
933                         if (szt < sizeof(tstr)) /* Paranoid, should always be true! */
934                                 BLI_strncpy(tstr, uilng, szt + 1); /* +1 for '\0' char! */
935                 }
936                 if (tstr[0]) {
937                         /* Because of some codes like sr_SR@latin... */
938                         tc = strchr(uilng, '@');
939                         if (tc)
940                                 BLI_strncpy(tstr + szt, tc, sizeof(tstr) - szt);
941
942                         BLI_join_dirfile(path, maxlen, root, tstr);
943                         strcat(tstr, ".po");
944                         BLI_path_append(path, maxlen, tstr);
945                         if (BLI_is_file(path))
946                                 return;
947                 }
948         }
949
950         /* Else no po file! */
951         path[0] = '\0';
952 }
953
954 static int edittranslation_exec(bContext *C, wmOperator *op)
955 {
956         uiBut *but = uiContextActiveButton(C);
957         int ret = OPERATOR_CANCELLED;
958
959         if (but) {
960                 PointerRNA ptr;
961                 char popath[FILE_MAX];
962                 const char *root = U.i18ndir;
963                 const char *uilng = BLF_lang_get();
964
965                 uiStringInfo but_label = {BUT_GET_LABEL, NULL};
966                 uiStringInfo rna_label = {BUT_GET_RNA_LABEL, NULL};
967                 uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
968                 uiStringInfo but_tip = {BUT_GET_TIP, NULL};
969                 uiStringInfo rna_tip = {BUT_GET_RNA_TIP, NULL};
970                 uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
971                 uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
972                 uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
973                 uiStringInfo rna_enum = {BUT_GET_RNAENUM_IDENTIFIER, NULL};
974                 uiStringInfo rna_ctxt = {BUT_GET_RNA_LABEL_CONTEXT, NULL};
975
976                 if (!BLI_is_dir(root)) {
977                         BKE_report(op->reports, RPT_ERROR, "Please set your User Preferences' 'Translation Branches "
978                                                            "Directory' path to a valid directory");
979                         return OPERATOR_CANCELLED;
980                 }
981                 if (!WM_operatortype_find(EDTSRC_I18N_OP_NAME, 0)) {
982                         BKE_reportf(op->reports, RPT_ERROR, "Could not find operator '%s'! Please enable ui_translate addon "
983                                                             "in the User Preferences", EDTSRC_I18N_OP_NAME);
984                         return OPERATOR_CANCELLED;
985                 }
986                 /* Try to find a valid po file for current language... */
987                 edittranslation_find_po_file(root, uilng, popath, FILE_MAX);
988 /*              printf("po path: %s\n", popath);*/
989                 if (popath[0] == '\0') {
990                         BKE_reportf(op->reports, RPT_ERROR, "No valid po found for language '%s' under %s", uilng, root);
991                         return OPERATOR_CANCELLED;
992                 }
993
994                 uiButGetStrInfo(C, but, &but_label, &rna_label, &enum_label, &but_tip, &rna_tip, &enum_tip,
995                                 &rna_struct, &rna_prop, &rna_enum, &rna_ctxt, NULL);
996
997                 WM_operator_properties_create(&ptr, EDTSRC_I18N_OP_NAME);
998                 RNA_string_set(&ptr, "lang", uilng);
999                 RNA_string_set(&ptr, "po_file", popath);
1000                 RNA_string_set(&ptr, "but_label", but_label.strinfo);
1001                 RNA_string_set(&ptr, "rna_label", rna_label.strinfo);
1002                 RNA_string_set(&ptr, "enum_label", enum_label.strinfo);
1003                 RNA_string_set(&ptr, "but_tip", but_tip.strinfo);
1004                 RNA_string_set(&ptr, "rna_tip", rna_tip.strinfo);
1005                 RNA_string_set(&ptr, "enum_tip", enum_tip.strinfo);
1006                 RNA_string_set(&ptr, "rna_struct", rna_struct.strinfo);
1007                 RNA_string_set(&ptr, "rna_prop", rna_prop.strinfo);
1008                 RNA_string_set(&ptr, "rna_enum", rna_enum.strinfo);
1009                 RNA_string_set(&ptr, "rna_ctxt", rna_ctxt.strinfo);
1010                 ret = WM_operator_name_call(C, EDTSRC_I18N_OP_NAME, WM_OP_INVOKE_DEFAULT, &ptr);
1011
1012                 /* Clean up */
1013                 if (but_label.strinfo)
1014                         MEM_freeN(but_label.strinfo);
1015                 if (rna_label.strinfo)
1016                         MEM_freeN(rna_label.strinfo);
1017                 if (enum_label.strinfo)
1018                         MEM_freeN(enum_label.strinfo);
1019                 if (but_tip.strinfo)
1020                         MEM_freeN(but_tip.strinfo);
1021                 if (rna_tip.strinfo)
1022                         MEM_freeN(rna_tip.strinfo);
1023                 if (enum_tip.strinfo)
1024                         MEM_freeN(enum_tip.strinfo);
1025                 if (rna_struct.strinfo)
1026                         MEM_freeN(rna_struct.strinfo);
1027                 if (rna_prop.strinfo)
1028                         MEM_freeN(rna_prop.strinfo);
1029                 if (rna_enum.strinfo)
1030                         MEM_freeN(rna_enum.strinfo);
1031                 if (rna_ctxt.strinfo)
1032                         MEM_freeN(rna_ctxt.strinfo);
1033
1034                 return ret;
1035         }
1036         else {
1037                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
1038                 return OPERATOR_CANCELLED;
1039         }
1040 }
1041
1042 static void UI_OT_edittranslation_init(wmOperatorType *ot)
1043 {
1044         /* identifiers */
1045         ot->name = "Edit Translation";
1046         ot->idname = "UI_OT_edittranslation_init";
1047         ot->description = "Edit i18n in current language for the active button";
1048
1049         /* callbacks */
1050         ot->exec = edittranslation_exec;
1051 }
1052
1053 #endif /* WITH_PYTHON */
1054
1055 static int reloadtranslation_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1056 {
1057         BLF_lang_init();
1058         BLF_cache_clear();
1059         BLF_lang_set(NULL);
1060         UI_reinit_font();
1061         return OPERATOR_FINISHED;
1062 }
1063
1064 static void UI_OT_reloadtranslation(wmOperatorType *ot)
1065 {
1066         /* identifiers */
1067         ot->name = "Reload Translation";
1068         ot->idname = "UI_OT_reloadtranslation";
1069         ot->description = "Force a full reload of UI translation";
1070
1071         /* callbacks */
1072         ot->exec = reloadtranslation_exec;
1073 }
1074
1075 /* ********************************************************* */
1076 /* Registration */
1077
1078 void UI_buttons_operatortypes(void)
1079 {
1080         WM_operatortype_append(UI_OT_eyedropper);
1081         WM_operatortype_append(UI_OT_reset_default_theme);
1082         WM_operatortype_append(UI_OT_copy_data_path_button);
1083         WM_operatortype_append(UI_OT_reset_default_button);
1084         WM_operatortype_append(UI_OT_copy_to_selected_button);
1085         WM_operatortype_append(UI_OT_reports_to_textblock);  /* XXX: temp? */
1086
1087 #ifdef WITH_PYTHON
1088         WM_operatortype_append(UI_OT_editsource);
1089         WM_operatortype_append(UI_OT_edittranslation_init);
1090 #endif
1091         WM_operatortype_append(UI_OT_reloadtranslation);
1092 }