36e965e13d2b44254c83a11d66d5d36051d21dfe
[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 <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_screen_types.h"
35 #include "DNA_text_types.h" /* for UI_OT_reports_to_text */
36
37 #include "BLI_blenlib.h"
38
39 #include "BLF_api.h"
40 #include "BLF_translation.h"
41
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44 #include "BKE_global.h"
45 #include "BKE_text.h" /* for UI_OT_reports_to_text */
46 #include "BKE_report.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "UI_interface.h"
52
53 #include "interface_intern.h"
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 /* only for UI_OT_editsource */
59 #include "ED_screen.h"
60 #include "BKE_main.h"
61 #include "BLI_ghash.h"
62
63 /* Reset Default Theme ------------------------ */
64
65 static int reset_default_theme_exec(bContext *C, wmOperator *UNUSED(op))
66 {
67         ui_theme_init_default();
68         ui_style_init_default();
69         WM_event_add_notifier(C, NC_WINDOW, NULL);
70         
71         return OPERATOR_FINISHED;
72 }
73
74 static void UI_OT_reset_default_theme(wmOperatorType *ot)
75 {
76         /* identifiers */
77         ot->name = "Reset to Default Theme";
78         ot->idname = "UI_OT_reset_default_theme";
79         ot->description = "Reset to the default theme colors";
80         
81         /* callbacks */
82         ot->exec = reset_default_theme_exec;
83         
84         /* flags */
85         ot->flag = OPTYPE_REGISTER;
86 }
87
88 /* Copy Data Path Operator ------------------------ */
89
90 static int copy_data_path_button_poll(bContext *C)
91 {
92         PointerRNA ptr;
93         PropertyRNA *prop;
94         char *path;
95         int index;
96
97         uiContextActiveProperty(C, &ptr, &prop, &index);
98
99         if (ptr.id.data && ptr.data && prop) {
100                 path = RNA_path_from_ID_to_property(&ptr, prop);
101                 
102                 if (path) {
103                         MEM_freeN(path);
104                         return 1;
105                 }
106         }
107
108         return 0;
109 }
110
111 static int copy_data_path_button_exec(bContext *C, wmOperator *UNUSED(op))
112 {
113         PointerRNA ptr;
114         PropertyRNA *prop;
115         char *path;
116         int index;
117
118         /* try to create driver using property retrieved from UI */
119         uiContextActiveProperty(C, &ptr, &prop, &index);
120
121         if (ptr.id.data && ptr.data && prop) {
122                 path = RNA_path_from_ID_to_property(&ptr, prop);
123                 
124                 if (path) {
125                         WM_clipboard_text_set(path, FALSE);
126                         MEM_freeN(path);
127                         return OPERATOR_FINISHED;
128                 }
129         }
130
131         return OPERATOR_CANCELLED;
132 }
133
134 static void UI_OT_copy_data_path_button(wmOperatorType *ot)
135 {
136         /* identifiers */
137         ot->name = "Copy Data Path";
138         ot->idname = "UI_OT_copy_data_path_button";
139         ot->description = "Copy the RNA data path for this property to the clipboard";
140
141         /* callbacks */
142         ot->exec = copy_data_path_button_exec;
143         ot->poll = copy_data_path_button_poll;
144
145         /* flags */
146         ot->flag = OPTYPE_REGISTER;
147 }
148
149 /* Reset to Default Values Button Operator ------------------------ */
150
151 static int operator_button_property_finish(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
152 {
153         ID *id = ptr->id.data;
154
155         /* perform updates required for this property */
156         RNA_property_update(C, ptr, prop);
157
158         /* as if we pressed the button */
159         uiContextActivePropertyHandle(C);
160
161         /* Since we don't want to undo _all_ edits to settings, eg window
162          * edits on the screen or on operator settings.
163          * it might be better to move undo's inline - campbell */
164         if (id && ID_CHECK_UNDO(id)) {
165                 /* do nothing, go ahead with undo */
166                 return OPERATOR_FINISHED;
167         }
168         else {
169                 return OPERATOR_CANCELLED;
170         }
171 }
172
173 static int reset_default_button_poll(bContext *C)
174 {
175         PointerRNA ptr;
176         PropertyRNA *prop;
177         int index;
178
179         uiContextActiveProperty(C, &ptr, &prop, &index);
180         
181         return (ptr.data && prop && RNA_property_editable(&ptr, prop));
182 }
183
184 static int reset_default_button_exec(bContext *C, wmOperator *op)
185 {
186         PointerRNA ptr;
187         PropertyRNA *prop;
188         int index, all = RNA_boolean_get(op->ptr, "all");
189
190         /* try to reset the nominated setting to its default value */
191         uiContextActiveProperty(C, &ptr, &prop, &index);
192         
193         /* if there is a valid property that is editable... */
194         if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
195                 if (RNA_property_reset(&ptr, prop, (all) ? -1 : index))
196                         return operator_button_property_finish(C, &ptr, prop);
197         }
198
199         return OPERATOR_CANCELLED;
200 }
201
202 static void UI_OT_reset_default_button(wmOperatorType *ot)
203 {
204         /* identifiers */
205         ot->name = "Reset to Default Value";
206         ot->idname = "UI_OT_reset_default_button";
207         ot->description = "Reset this property's value to its default value";
208
209         /* callbacks */
210         ot->poll = reset_default_button_poll;
211         ot->exec = reset_default_button_exec;
212
213         /* flags */
214         ot->flag = OPTYPE_UNDO;
215         
216         /* properties */
217         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
218 }
219
220 /* Unset Property Button Operator ------------------------ */
221
222 static int unset_property_button_exec(bContext *C, wmOperator *UNUSED(op))
223 {
224         PointerRNA ptr;
225         PropertyRNA *prop;
226         int index;
227
228         /* try to unset the nominated property */
229         uiContextActiveProperty(C, &ptr, &prop, &index);
230
231         /* if there is a valid property that is editable... */
232         if (ptr.data && prop && RNA_property_editable(&ptr, prop)
233             /*&& RNA_property_is_idprop(prop)*/ && RNA_property_is_set(&ptr, prop))
234         {
235                 RNA_property_unset(&ptr, prop);
236                 return operator_button_property_finish(C, &ptr, prop);
237         }
238
239         return OPERATOR_CANCELLED;
240 }
241
242 static void UI_OT_unset_property_button(wmOperatorType *ot)
243 {
244         /* identifiers */
245         ot->name = "Unset property";
246         ot->idname = "UI_OT_unset_property_button";
247         ot->description = "Clear the property and use default or generated value in operators";
248
249         /* callbacks */
250         ot->poll = ED_operator_regionactive;
251         ot->exec = unset_property_button_exec;
252
253         /* flags */
254         ot->flag = OPTYPE_UNDO;
255 }
256
257 /* Copy To Selected Operator ------------------------ */
258
259 static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb, int *use_path)
260 {
261         *use_path = FALSE;
262
263         if (RNA_struct_is_a(ptr->type, &RNA_EditBone))
264                 *lb = CTX_data_collection_get(C, "selected_editable_bones");
265         else if (RNA_struct_is_a(ptr->type, &RNA_PoseBone))
266                 *lb = CTX_data_collection_get(C, "selected_pose_bones");
267         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence))
268                 *lb = CTX_data_collection_get(C, "selected_editable_sequences");
269         else {
270                 ID *id = ptr->id.data;
271
272                 if (id && GS(id->name) == ID_OB) {
273                         *lb = CTX_data_collection_get(C, "selected_editable_objects");
274                         *use_path = TRUE;
275                 }
276                 else
277                         return 0;
278         }
279         
280         return 1;
281 }
282
283 static int copy_to_selected_button_poll(bContext *C)
284 {
285         PointerRNA ptr, lptr, idptr;
286         PropertyRNA *prop, *lprop;
287         int index, success = 0;
288
289         uiContextActiveProperty(C, &ptr, &prop, &index);
290
291         if (ptr.data && prop) {
292                 char *path = NULL;
293                 int use_path;
294                 CollectionPointerLink *link;
295                 ListBase lb;
296
297                 if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
298                         return success;
299
300                 if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
301                         for (link = lb.first; link; link = link->next) {
302                                 if (link->ptr.data != ptr.data) {
303                                         if (use_path) {
304                                                 lprop = NULL;
305                                                 RNA_id_pointer_create(link->ptr.id.data, &idptr);
306                                                 RNA_path_resolve_property(&idptr, path, &lptr, &lprop);
307                                         }
308                                         else {
309                                                 lptr = link->ptr;
310                                                 lprop = prop;
311                                         }
312
313                                         if (lprop == prop) {
314                                                 if (RNA_property_editable(&lptr, prop))
315                                                         success = 1;
316                                         }
317                                 }
318                         }
319
320                         if (path)
321                                 MEM_freeN(path);
322                 }
323
324                 BLI_freelistN(&lb);
325         }
326
327         return success;
328 }
329
330 static int copy_to_selected_button_exec(bContext *C, wmOperator *op)
331 {
332         PointerRNA ptr, lptr, idptr;
333         PropertyRNA *prop, *lprop;
334         int success = 0;
335         int index, all = RNA_boolean_get(op->ptr, "all");
336
337         /* try to reset the nominated setting to its default value */
338         uiContextActiveProperty(C, &ptr, &prop, &index);
339         
340         /* if there is a valid property that is editable... */
341         if (ptr.data && prop) {
342                 char *path = NULL;
343                 int use_path;
344                 CollectionPointerLink *link;
345                 ListBase lb;
346
347                 if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
348                         return success;
349
350                 if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
351                         for (link = lb.first; link; link = link->next) {
352                                 if (link->ptr.data != ptr.data) {
353                                         if (use_path) {
354                                                 lprop = NULL;
355                                                 RNA_id_pointer_create(link->ptr.id.data, &idptr);
356                                                 RNA_path_resolve_property(&idptr, path, &lptr, &lprop);
357                                         }
358                                         else {
359                                                 lptr = link->ptr;
360                                                 lprop = prop;
361                                         }
362
363                                         if (lprop == prop) {
364                                                 if (RNA_property_editable(&lptr, lprop)) {
365                                                         if (RNA_property_copy(&lptr, &ptr, prop, (all) ? -1 : index)) {
366                                                                 RNA_property_update(C, &lptr, prop);
367                                                                 success = 1;
368                                                         }
369                                                 }
370                                         }
371                                 }
372                         }
373
374                         if (path)
375                                 MEM_freeN(path);
376                 }
377
378                 BLI_freelistN(&lb);
379         }
380         
381         return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
382 }
383
384 static void UI_OT_copy_to_selected_button(wmOperatorType *ot)
385 {
386         /* identifiers */
387         ot->name = "Copy To Selected";
388         ot->idname = "UI_OT_copy_to_selected_button";
389         ot->description = "Copy property from this object to selected objects or bones";
390
391         /* callbacks */
392         ot->poll = copy_to_selected_button_poll;
393         ot->exec = copy_to_selected_button_exec;
394
395         /* flags */
396         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
397
398         /* properties */
399         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
400 }
401
402 /* Reports to Textblock Operator ------------------------ */
403
404 /* FIXME: this is just a temporary operator so that we can see all the reports somewhere 
405  * when there are too many to display...
406  */
407
408 static int reports_to_text_poll(bContext *C)
409 {
410         return CTX_wm_reports(C) != NULL;
411 }
412
413 static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
414 {
415         ReportList *reports = CTX_wm_reports(C);
416         Main *bmain = CTX_data_main(C);
417         Text *txt;
418         char *str;
419         
420         /* create new text-block to write to */
421         txt = BKE_text_add(bmain, "Recent Reports");
422         
423         /* convert entire list to a display string, and add this to the text-block
424          *      - if commandline debug option enabled, show debug reports too
425          *      - otherwise, up to info (which is what users normally see)
426          */
427         str = BKE_reports_string(reports, (G.debug & G_DEBUG) ? RPT_DEBUG : RPT_INFO);
428
429         if (str) {
430                 BKE_text_write(txt, str);
431                 MEM_freeN(str);
432
433                 return OPERATOR_FINISHED;
434         }
435         else {
436                 return OPERATOR_CANCELLED;
437         }
438 }
439
440 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
441 {
442         /* identifiers */
443         ot->name = "Reports to Text Block";
444         ot->idname = "UI_OT_reports_to_textblock";
445         ot->description = "Write the reports ";
446         
447         /* callbacks */
448         ot->poll = reports_to_text_poll;
449         ot->exec = reports_to_text_exec;
450 }
451
452 #ifdef WITH_PYTHON
453
454 /* ------------------------------------------------------------------------- */
455 /* EditSource Utility funcs and operator,
456  * note, this includes utility functions and button matching checks */
457
458 typedef struct uiEditSourceStore {
459         uiBut but_orig;
460         GHash *hash;
461 } uiEditSourceStore;
462
463 typedef struct uiEditSourceButStore {
464         char py_dbg_fn[FILE_MAX];
465         int py_dbg_ln;
466 } uiEditSourceButStore;
467
468 /* should only ever be set while the edit source operator is running */
469 static struct uiEditSourceStore *ui_editsource_info = NULL;
470
471 bool UI_editsource_enable_check(void)
472 {
473         return (ui_editsource_info != NULL);
474 }
475
476 static void ui_editsource_active_but_set(uiBut *but)
477 {
478         BLI_assert(ui_editsource_info == NULL);
479
480         ui_editsource_info = MEM_callocN(sizeof(uiEditSourceStore), __func__);
481         memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
482
483         ui_editsource_info->hash = BLI_ghash_ptr_new(__func__);
484 }
485
486 static void ui_editsource_active_but_clear(void)
487 {
488         BLI_ghash_free(ui_editsource_info->hash, NULL, MEM_freeN);
489         MEM_freeN(ui_editsource_info);
490         ui_editsource_info = NULL;
491 }
492
493 static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
494 {
495 #if 0
496         printf("matching buttons: '%s' == '%s'\n",
497                but_a->drawstr, but_b->drawstr);
498 #endif
499
500         /* this just needs to be a 'good-enough' comparison so we can know beyond
501          * reasonable doubt that these buttons are the same between redraws.
502          * if this fails it only means edit-source fails - campbell */
503         if (BLI_rctf_compare(&but_a->rect, &but_b->rect, FLT_EPSILON) &&
504             (but_a->type == but_b->type) &&
505             (but_a->rnaprop == but_b->rnaprop) &&
506             (but_a->optype == but_b->optype) &&
507             (but_a->unit_type == but_b->unit_type) &&
508             (strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0))
509         {
510                 return TRUE;
511         }
512         else {
513                 return FALSE;
514         }
515 }
516
517 void UI_editsource_active_but_test(uiBut *but)
518 {
519         extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
520
521         struct uiEditSourceButStore *but_store = MEM_callocN(sizeof(uiEditSourceButStore), __func__);
522
523         const char *fn;
524         int lineno = -1;
525
526 #if 0
527         printf("comparing buttons: '%s' == '%s'\n",
528                but->drawstr, ui_editsource_info->but_orig.drawstr);
529 #endif
530
531         PyC_FileAndNum_Safe(&fn, &lineno);
532
533         if (lineno != -1) {
534                 BLI_strncpy(but_store->py_dbg_fn, fn,
535                             sizeof(but_store->py_dbg_fn));
536                 but_store->py_dbg_ln = lineno;
537         }
538         else {
539                 but_store->py_dbg_fn[0] = '\0';
540                 but_store->py_dbg_ln = -1;
541         }
542
543         BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
544 }
545
546 static int editsource_text_edit(bContext *C, wmOperator *op,
547                                 char filepath[FILE_MAX], int line)
548 {
549         struct Main *bmain = CTX_data_main(C);
550         Text *text;
551
552         for (text = bmain->text.first; text; text = text->id.next) {
553                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
554                         break;
555                 }
556         }
557
558         if (text == NULL) {
559                 text = BKE_text_load(bmain, filepath, bmain->name);
560         }
561
562         if (text == NULL) {
563                 BKE_reportf(op->reports, RPT_WARNING, "File '%s' cannot be opened", filepath);
564                 return OPERATOR_CANCELLED;
565         }
566         else {
567                 /* naughty!, find text area to set, not good behavior
568                  * but since this is a dev tool lets allow it - campbell */
569                 ScrArea *sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
570                 if (sa) {
571                         SpaceText *st = sa->spacedata.first;
572                         st->text = text;
573                 }
574                 else {
575                         BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2);
576                 }
577
578                 txt_move_toline(text, line - 1, FALSE);
579                 WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text);
580         }
581
582         return OPERATOR_FINISHED;
583 }
584
585 static int editsource_exec(bContext *C, wmOperator *op)
586 {
587         uiBut *but = uiContextActiveButton(C);
588
589         if (but) {
590                 GHashIterator ghi;
591                 struct uiEditSourceButStore *but_store = NULL;
592
593                 ARegion *ar = CTX_wm_region(C);
594                 int ret;
595
596                 /* needed else the active button does not get tested */
597                 uiFreeActiveButtons(C, CTX_wm_screen(C));
598
599                 // printf("%s: begin\n", __func__);
600
601                 /* take care not to return before calling ui_editsource_active_but_clear */
602                 ui_editsource_active_but_set(but);
603
604                 /* redraw and get active button python info */
605                 ED_region_do_draw(C, ar);
606
607                 for (BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
608                      BLI_ghashIterator_done(&ghi) == false;
609                      BLI_ghashIterator_step(&ghi))
610                 {
611                         uiBut *but_key = BLI_ghashIterator_getKey(&ghi);
612                         if (but_key && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but_key)) {
613                                 but_store = BLI_ghashIterator_getValue(&ghi);
614                                 break;
615                         }
616
617                 }
618
619                 if (but_store) {
620                         if (but_store->py_dbg_ln != -1) {
621                                 ret = editsource_text_edit(C, op,
622                                                            but_store->py_dbg_fn,
623                                                            but_store->py_dbg_ln);
624                         }
625                         else {
626                                 BKE_report(op->reports, RPT_ERROR, "Active button is not from a script, cannot edit source");
627                                 ret = OPERATOR_CANCELLED;
628                         }
629                 }
630                 else {
631                         BKE_report(op->reports, RPT_ERROR, "Active button match cannot be found");
632                         ret = OPERATOR_CANCELLED;
633                 }
634
635
636                 ui_editsource_active_but_clear();
637
638                 // printf("%s: end\n", __func__);
639
640                 return ret;
641         }
642         else {
643                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
644                 return OPERATOR_CANCELLED;
645         }
646 }
647
648 static void UI_OT_editsource(wmOperatorType *ot)
649 {
650         /* identifiers */
651         ot->name = "Edit Source";
652         ot->idname = "UI_OT_editsource";
653         ot->description = "Edit UI source code of the active button";
654
655         /* callbacks */
656         ot->exec = editsource_exec;
657 }
658
659 /* ------------------------------------------------------------------------- */
660 /* EditTranslation utility funcs and operator,
661  * Note: this includes utility functions and button matching checks.
662  *       this only works in conjunction with a py operator! */
663
664 static void edittranslation_find_po_file(const char *root, const char *uilng, char *path, const size_t maxlen)
665 {
666         char tstr[32]; /* Should be more than enough! */
667
668         /* First, full lang code. */
669         BLI_snprintf(tstr, sizeof(tstr), "%s.po", uilng);
670         BLI_join_dirfile(path, maxlen, root, uilng);
671         BLI_path_append(path, maxlen, tstr);
672         if (BLI_is_file(path))
673                 return;
674
675         /* Now try without the second iso code part (_ES in es_ES). */
676         {
677                 char *tc = NULL;
678                 size_t szt = 0;
679                 tstr[0] = '\0';
680
681                 tc = strchr(uilng, '_');
682                 if (tc) {
683                         szt = tc - uilng;
684                         if (szt < sizeof(tstr)) /* Paranoid, should always be true! */
685                                 BLI_strncpy(tstr, uilng, szt + 1); /* +1 for '\0' char! */
686                 }
687                 if (tstr[0]) {
688                         /* Because of some codes like sr_SR@latin... */
689                         tc = strchr(uilng, '@');
690                         if (tc)
691                                 BLI_strncpy(tstr + szt, tc, sizeof(tstr) - szt);
692
693                         BLI_join_dirfile(path, maxlen, root, tstr);
694                         strcat(tstr, ".po");
695                         BLI_path_append(path, maxlen, tstr);
696                         if (BLI_is_file(path))
697                                 return;
698                 }
699         }
700
701         /* Else no po file! */
702         path[0] = '\0';
703 }
704
705 static int edittranslation_exec(bContext *C, wmOperator *op)
706 {
707         uiBut *but = uiContextActiveButton(C);
708         int ret = OPERATOR_CANCELLED;
709
710         if (but) {
711                 PointerRNA ptr;
712                 char popath[FILE_MAX];
713                 const char *root = U.i18ndir;
714                 const char *uilng = BLF_lang_get();
715
716                 uiStringInfo but_label = {BUT_GET_LABEL, NULL};
717                 uiStringInfo rna_label = {BUT_GET_RNA_LABEL, NULL};
718                 uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
719                 uiStringInfo but_tip = {BUT_GET_TIP, NULL};
720                 uiStringInfo rna_tip = {BUT_GET_RNA_TIP, NULL};
721                 uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
722                 uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
723                 uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
724                 uiStringInfo rna_enum = {BUT_GET_RNAENUM_IDENTIFIER, NULL};
725                 uiStringInfo rna_ctxt = {BUT_GET_RNA_LABEL_CONTEXT, NULL};
726
727                 if (!BLI_is_dir(root)) {
728                         BKE_report(op->reports, RPT_ERROR, "Please set your User Preferences' 'Translation Branches "
729                                                            "Directory' path to a valid directory");
730                         return OPERATOR_CANCELLED;
731                 }
732                 if (!WM_operatortype_find(EDTSRC_I18N_OP_NAME, 0)) {
733                         BKE_reportf(op->reports, RPT_ERROR, "Could not find operator '%s'! Please enable ui_translate addon "
734                                                             "in the User Preferences", EDTSRC_I18N_OP_NAME);
735                         return OPERATOR_CANCELLED;
736                 }
737                 /* Try to find a valid po file for current language... */
738                 edittranslation_find_po_file(root, uilng, popath, FILE_MAX);
739 /*              printf("po path: %s\n", popath);*/
740                 if (popath[0] == '\0') {
741                         BKE_reportf(op->reports, RPT_ERROR, "No valid po found for language '%s' under %s", uilng, root);
742                         return OPERATOR_CANCELLED;
743                 }
744
745                 uiButGetStrInfo(C, but, &but_label, &rna_label, &enum_label, &but_tip, &rna_tip, &enum_tip,
746                                 &rna_struct, &rna_prop, &rna_enum, &rna_ctxt, NULL);
747
748                 WM_operator_properties_create(&ptr, EDTSRC_I18N_OP_NAME);
749                 RNA_string_set(&ptr, "lang", uilng);
750                 RNA_string_set(&ptr, "po_file", popath);
751                 RNA_string_set(&ptr, "but_label", but_label.strinfo);
752                 RNA_string_set(&ptr, "rna_label", rna_label.strinfo);
753                 RNA_string_set(&ptr, "enum_label", enum_label.strinfo);
754                 RNA_string_set(&ptr, "but_tip", but_tip.strinfo);
755                 RNA_string_set(&ptr, "rna_tip", rna_tip.strinfo);
756                 RNA_string_set(&ptr, "enum_tip", enum_tip.strinfo);
757                 RNA_string_set(&ptr, "rna_struct", rna_struct.strinfo);
758                 RNA_string_set(&ptr, "rna_prop", rna_prop.strinfo);
759                 RNA_string_set(&ptr, "rna_enum", rna_enum.strinfo);
760                 RNA_string_set(&ptr, "rna_ctxt", rna_ctxt.strinfo);
761                 ret = WM_operator_name_call(C, EDTSRC_I18N_OP_NAME, WM_OP_INVOKE_DEFAULT, &ptr);
762
763                 /* Clean up */
764                 if (but_label.strinfo)
765                         MEM_freeN(but_label.strinfo);
766                 if (rna_label.strinfo)
767                         MEM_freeN(rna_label.strinfo);
768                 if (enum_label.strinfo)
769                         MEM_freeN(enum_label.strinfo);
770                 if (but_tip.strinfo)
771                         MEM_freeN(but_tip.strinfo);
772                 if (rna_tip.strinfo)
773                         MEM_freeN(rna_tip.strinfo);
774                 if (enum_tip.strinfo)
775                         MEM_freeN(enum_tip.strinfo);
776                 if (rna_struct.strinfo)
777                         MEM_freeN(rna_struct.strinfo);
778                 if (rna_prop.strinfo)
779                         MEM_freeN(rna_prop.strinfo);
780                 if (rna_enum.strinfo)
781                         MEM_freeN(rna_enum.strinfo);
782                 if (rna_ctxt.strinfo)
783                         MEM_freeN(rna_ctxt.strinfo);
784
785                 return ret;
786         }
787         else {
788                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
789                 return OPERATOR_CANCELLED;
790         }
791 }
792
793 static void UI_OT_edittranslation_init(wmOperatorType *ot)
794 {
795         /* identifiers */
796         ot->name = "Edit Translation";
797         ot->idname = "UI_OT_edittranslation_init";
798         ot->description = "Edit i18n in current language for the active button";
799
800         /* callbacks */
801         ot->exec = edittranslation_exec;
802 }
803
804 #endif /* WITH_PYTHON */
805
806 static int reloadtranslation_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
807 {
808         BLF_lang_init();
809         BLF_cache_clear();
810         BLF_lang_set(NULL);
811         UI_reinit_font();
812         return OPERATOR_FINISHED;
813 }
814
815 static void UI_OT_reloadtranslation(wmOperatorType *ot)
816 {
817         /* identifiers */
818         ot->name = "Reload Translation";
819         ot->idname = "UI_OT_reloadtranslation";
820         ot->description = "Force a full reload of UI translation";
821
822         /* callbacks */
823         ot->exec = reloadtranslation_exec;
824 }
825
826 /* ********************************************************* */
827 /* Registration */
828
829 void UI_buttons_operatortypes(void)
830 {
831         WM_operatortype_append(UI_OT_reset_default_theme);
832         WM_operatortype_append(UI_OT_copy_data_path_button);
833         WM_operatortype_append(UI_OT_reset_default_button);
834         WM_operatortype_append(UI_OT_unset_property_button);
835         WM_operatortype_append(UI_OT_copy_to_selected_button);
836         WM_operatortype_append(UI_OT_reports_to_textblock);  /* XXX: temp? */
837
838 #ifdef WITH_PYTHON
839         WM_operatortype_append(UI_OT_editsource);
840         WM_operatortype_append(UI_OT_edittranslation_init);
841 #endif
842         WM_operatortype_append(UI_OT_reloadtranslation);
843
844         /* external */
845         WM_operatortype_append(UI_OT_eyedropper_color);
846         WM_operatortype_append(UI_OT_eyedropper_id);
847 }