Context menu 'Edit Source' operator no longer needs to be enabled as a build option...
[blender.git] / source / blender / editors / interface / interface_ops.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  * 
23  * Contributor(s): Blender Foundation, Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/interface/interface_ops.c
29  *  \ingroup edinterface
30  */
31
32
33 #include <stdio.h>
34 #include <math.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_text_types.h" /* for UI_OT_reports_to_text */
42
43 #include "BLI_blenlib.h"
44 #include "BLI_math_color.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_text.h" /* for UI_OT_reports_to_text */
50 #include "BKE_report.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54
55 #include "BIF_gl.h"
56
57 #include "UI_interface.h"
58
59 #include "interface_intern.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 /* only for UI_OT_editsource */
65 #include "ED_screen.h"
66 #include "BKE_main.h"
67 #include "BLI_ghash.h"
68
69
70 /* ********************************************************** */
71
72 typedef struct Eyedropper {
73         PointerRNA ptr;
74         PropertyRNA *prop;
75         int index;
76 } Eyedropper;
77
78 static int eyedropper_init(bContext *C, wmOperator *op)
79 {
80         Eyedropper *eye;
81         
82         op->customdata= eye= MEM_callocN(sizeof(Eyedropper), "Eyedropper");
83         
84         uiContextActiveProperty(C, &eye->ptr, &eye->prop, &eye->index);
85         
86         return (eye->ptr.data && eye->prop && RNA_property_editable(&eye->ptr, eye->prop));
87 }
88  
89 static void eyedropper_exit(bContext *C, wmOperator *op)
90 {
91         WM_cursor_restore(CTX_wm_window(C));
92         
93         if(op->customdata)
94                 MEM_freeN(op->customdata);
95         op->customdata= NULL;
96 }
97
98 static int eyedropper_cancel(bContext *C, wmOperator *op)
99 {
100         eyedropper_exit(C, op);
101         return OPERATOR_CANCELLED;
102 }
103
104 static void eyedropper_sample(bContext *C, Eyedropper *eye, int mx, int my)
105 {
106         if(RNA_property_type(eye->prop) == PROP_FLOAT) {
107                 const int color_manage = CTX_data_scene(C)->r.color_mgt_flag & R_COLOR_MANAGEMENT;
108                 float col[4];
109         
110                 RNA_property_float_get_array(&eye->ptr, eye->prop, col);
111                 
112                 glReadBuffer(GL_FRONT);
113                 glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, col);
114                 glReadBuffer(GL_BACK);
115         
116                 if (RNA_property_array_length(&eye->ptr, eye->prop) < 3) return;
117
118                 /* convert from screen (srgb) space to linear rgb space */
119                 if (color_manage && RNA_property_subtype(eye->prop) == PROP_COLOR)
120                         srgb_to_linearrgb_v3_v3(col, col);
121                 
122                 RNA_property_float_set_array(&eye->ptr, eye->prop, col);
123                 
124                 RNA_property_update(C, &eye->ptr, eye->prop);
125         }
126 }
127
128 /* main modal status check */
129 static int eyedropper_modal(bContext *C, wmOperator *op, wmEvent *event)
130 {
131         Eyedropper *eye = (Eyedropper *)op->customdata;
132         
133         switch(event->type) {
134                 case ESCKEY:
135                 case RIGHTMOUSE:
136                         return eyedropper_cancel(C, op);
137                 case LEFTMOUSE:
138                         if(event->val==KM_RELEASE) {
139                                 eyedropper_sample(C, eye, event->x, event->y);
140                                 eyedropper_exit(C, op);
141                                 return OPERATOR_FINISHED;
142                         }
143                         break;
144         }
145         
146         return OPERATOR_RUNNING_MODAL;
147 }
148
149 /* Modal Operator init */
150 static int eyedropper_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
151 {
152         /* init */
153         if (eyedropper_init(C, op)) {
154                 WM_cursor_modal(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
155
156                 /* add temp handler */
157                 WM_event_add_modal_handler(C, op);
158                 
159                 return OPERATOR_RUNNING_MODAL;
160         } else {
161                 eyedropper_exit(C, op);
162                 return OPERATOR_CANCELLED;
163         }
164 }
165
166 /* Repeat operator */
167 static int eyedropper_exec (bContext *C, wmOperator *op)
168 {
169         /* init */
170         if (eyedropper_init(C, op)) {
171                 
172                 /* do something */
173                 
174                 /* cleanup */
175                 eyedropper_exit(C, op);
176                 
177                 return OPERATOR_FINISHED;
178         } else {
179                 return OPERATOR_CANCELLED;
180         }
181 }
182
183 static int eyedropper_poll(bContext *C)
184 {
185         if (!CTX_wm_window(C)) return 0;
186         else return 1;
187 }
188
189 static void UI_OT_eyedropper(wmOperatorType *ot)
190 {
191         /* identifiers */
192         ot->name= "Eyedropper";
193         ot->idname= "UI_OT_eyedropper";
194         ot->description= "Sample a color from the Blender Window to store in a property";
195         
196         /* api callbacks */
197         ot->invoke= eyedropper_invoke;
198         ot->modal= eyedropper_modal;
199         ot->cancel= eyedropper_cancel;
200         ot->exec= eyedropper_exec;
201         ot->poll= eyedropper_poll;
202         
203         /* flags */
204         ot->flag= OPTYPE_BLOCKING;
205         
206         /* properties */
207 }
208
209 /* Reset Default Theme ------------------------ */
210
211 static int reset_default_theme_exec(bContext *C, wmOperator *UNUSED(op))
212 {
213         ui_theme_init_default();
214         WM_event_add_notifier(C, NC_WINDOW, NULL);
215         
216         return OPERATOR_FINISHED;
217 }
218
219 static void UI_OT_reset_default_theme(wmOperatorType *ot)
220 {
221         /* identifiers */
222         ot->name= "Reset to Default Theme";
223         ot->idname= "UI_OT_reset_default_theme";
224         ot->description= "Reset to the default theme colors";
225         
226         /* callbacks */
227         ot->exec= reset_default_theme_exec;
228         
229         /* flags */
230         ot->flag= OPTYPE_REGISTER;
231 }
232
233 /* Copy Data Path Operator ------------------------ */
234
235 static int copy_data_path_button_exec(bContext *C, wmOperator *UNUSED(op))
236 {
237         PointerRNA ptr;
238         PropertyRNA *prop;
239         char *path;
240         int success= 0;
241         int index;
242
243         /* try to create driver using property retrieved from UI */
244         uiContextActiveProperty(C, &ptr, &prop, &index);
245
246         if (ptr.id.data && ptr.data && prop) {
247                 path= RNA_path_from_ID_to_property(&ptr, prop);
248                 
249                 if (path) {
250                         WM_clipboard_text_set(path, FALSE);
251                         MEM_freeN(path);
252                 }
253         }
254
255         /* since we're just copying, we don't really need to do anything else...*/
256         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
257 }
258
259 static void UI_OT_copy_data_path_button(wmOperatorType *ot)
260 {
261         /* identifiers */
262         ot->name= "Copy Data Path";
263         ot->idname= "UI_OT_copy_data_path_button";
264         ot->description= "Copy the RNA data path for this property to the clipboard";
265
266         /* callbacks */
267         ot->exec= copy_data_path_button_exec;
268         //op->poll= ??? // TODO: need to have some valid property before this can be done
269
270         /* flags */
271         ot->flag= OPTYPE_REGISTER;
272 }
273
274 /* Reset to Default Values Button Operator ------------------------ */
275
276 static int reset_default_button_poll(bContext *C)
277 {
278         PointerRNA ptr;
279         PropertyRNA *prop;
280         int index;
281
282         uiContextActiveProperty(C, &ptr, &prop, &index);
283         
284         return (ptr.data && prop && RNA_property_editable(&ptr, prop));
285 }
286
287 static int reset_default_button_exec(bContext *C, wmOperator *op)
288 {
289         PointerRNA ptr;
290         PropertyRNA *prop;
291         int success= 0;
292         int index, all = RNA_boolean_get(op->ptr, "all");
293
294         /* try to reset the nominated setting to its default value */
295         uiContextActiveProperty(C, &ptr, &prop, &index);
296         
297         /* if there is a valid property that is editable... */
298         if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
299                 if(RNA_property_reset(&ptr, prop, (all)? -1: index)) {
300                         /* perform updates required for this property */
301                         RNA_property_update(C, &ptr, prop);
302
303                         /* as if we pressed the button */
304                         uiContextActivePropertyHandle(C);
305
306                         success= 1;
307                 }
308         }
309
310         /* Since we dont want to undo _all_ edits to settings, eg window
311          * edits on the screen or on operator settings.
312          * it might be better to move undo's inline - campbell */
313         if(success) {
314                 ID *id= ptr.id.data;
315                 if(id && ID_CHECK_UNDO(id)) {
316                         /* do nothing, go ahead with undo */
317                 }
318                 else {
319                         return OPERATOR_CANCELLED;
320                 }
321         }
322         /* end hack */
323
324         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
325 }
326
327 static void UI_OT_reset_default_button(wmOperatorType *ot)
328 {
329         /* identifiers */
330         ot->name= "Reset to Default Value";
331         ot->idname= "UI_OT_reset_default_button";
332         ot->description= "Reset this property's value to its default value";
333
334         /* callbacks */
335         ot->poll= reset_default_button_poll;
336         ot->exec= reset_default_button_exec;
337
338         /* flags */
339         ot->flag= OPTYPE_UNDO;
340         
341         /* properties */
342         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
343 }
344
345 /* Copy To Selected Operator ------------------------ */
346
347 static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb)
348 {
349         if(RNA_struct_is_a(ptr->type, &RNA_Object))
350                 *lb = CTX_data_collection_get(C, "selected_editable_objects");
351         else if(RNA_struct_is_a(ptr->type, &RNA_EditBone))
352                 *lb = CTX_data_collection_get(C, "selected_editable_bones");
353         else if(RNA_struct_is_a(ptr->type, &RNA_PoseBone))
354                 *lb = CTX_data_collection_get(C, "selected_pose_bones");
355         else if(RNA_struct_is_a(ptr->type, &RNA_Sequence))
356                 *lb = CTX_data_collection_get(C, "selected_editable_sequences");
357         else
358                 return 0;
359         
360         return 1;
361 }
362
363 static int copy_to_selected_button_poll(bContext *C)
364 {
365         PointerRNA ptr;
366         PropertyRNA *prop;
367         int index, success= 0;
368
369         uiContextActiveProperty(C, &ptr, &prop, &index);
370
371         if (ptr.data && prop) {
372                 CollectionPointerLink *link;
373                 ListBase lb;
374
375                 if(copy_to_selected_list(C, &ptr, &lb)) {
376                         for(link= lb.first; link; link=link->next)
377                                 if(link->ptr.data != ptr.data && RNA_property_editable(&link->ptr, prop))
378                                         success= 1;
379
380                         BLI_freelistN(&lb);
381                 }
382         }
383
384         return success;
385 }
386
387 static int copy_to_selected_button_exec(bContext *C, wmOperator *op)
388 {
389         PointerRNA ptr;
390         PropertyRNA *prop;
391         int success= 0;
392         int index, all = RNA_boolean_get(op->ptr, "all");
393
394         /* try to reset the nominated setting to its default value */
395         uiContextActiveProperty(C, &ptr, &prop, &index);
396         
397         /* if there is a valid property that is editable... */
398         if (ptr.data && prop) {
399                 CollectionPointerLink *link;
400                 ListBase lb;
401
402                 if(copy_to_selected_list(C, &ptr, &lb)) {
403                         for(link= lb.first; link; link=link->next) {
404                                 if(link->ptr.data != ptr.data && RNA_property_editable(&link->ptr, prop)) {
405                                         if(RNA_property_copy(&link->ptr, &ptr, prop, (all)? -1: index)) {
406                                                 RNA_property_update(C, &link->ptr, prop);
407                                                 success= 1;
408                                         }
409                                 }
410                         }
411
412                         BLI_freelistN(&lb);
413                 }
414         }
415         
416         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
417 }
418
419 static void UI_OT_copy_to_selected_button(wmOperatorType *ot)
420 {
421         /* identifiers */
422         ot->name= "Copy To Selected";
423         ot->idname= "UI_OT_copy_to_selected_button";
424         ot->description= "Copy property from this object to selected objects or bones";
425
426         /* callbacks */
427         ot->poll= copy_to_selected_button_poll;
428         ot->exec= copy_to_selected_button_exec;
429
430         /* flags */
431         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
432
433         /* properties */
434         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
435 }
436
437 /* Reports to Textblock Operator ------------------------ */
438
439 /* FIXME: this is just a temporary operator so that we can see all the reports somewhere 
440  * when there are too many to display...
441  */
442
443 static int reports_to_text_poll(bContext *C)
444 {
445         return CTX_wm_reports(C) != NULL;
446 }
447
448 static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
449 {
450         ReportList *reports = CTX_wm_reports(C);
451         Text *txt;
452         char *str;
453         
454         /* create new text-block to write to */
455         txt = add_empty_text("Recent Reports");
456         
457         /* convert entire list to a display string, and add this to the text-block
458          *      - if commandline debug option enabled, show debug reports too
459          *      - otherwise, up to info (which is what users normally see)
460          */
461         str = BKE_reports_string(reports, (G.f & G_DEBUG)? RPT_DEBUG : RPT_INFO);
462         
463         write_text(txt, str);
464         MEM_freeN(str);
465         
466         return OPERATOR_FINISHED;
467 }
468
469 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
470 {
471         /* identifiers */
472         ot->name= "Reports to Text Block";
473         ot->idname= "UI_OT_reports_to_textblock";
474         ot->description= "Write the reports ";
475         
476         /* callbacks */
477         ot->poll= reports_to_text_poll;
478         ot->exec= reports_to_text_exec;
479 }
480
481
482 /* ------------------------------------------------------------------------- */
483 /* EditSource Utility funcs and operator,
484  * note, this includes itility functions and button matching checks */
485
486 struct uiEditSourceStore {
487         uiBut but_orig;
488         GHash *hash;
489 } uiEditSourceStore;
490
491 struct uiEditSourceButStore {
492         char py_dbg_fn[240];
493         int py_dbg_ln;
494 } uiEditSourceButStore;
495
496 /* should only ever be set while the edit source operator is running */
497 struct uiEditSourceStore *ui_editsource_info= NULL;
498
499 int  UI_editsource_enable_check(void)
500 {
501         return (ui_editsource_info != NULL);
502 }
503
504 static void ui_editsource_active_but_set(uiBut *but)
505 {
506         BLI_assert(ui_editsource_info == NULL);
507
508         ui_editsource_info= MEM_callocN(sizeof(uiEditSourceStore), __func__);
509         memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
510
511         ui_editsource_info->hash = BLI_ghash_new(BLI_ghashutil_ptrhash,
512                                                  BLI_ghashutil_ptrcmp,
513                                                  __func__);
514 }
515
516 static void ui_editsource_active_but_clear(void)
517 {
518         BLI_ghash_free(ui_editsource_info->hash, NULL, (GHashValFreeFP)MEM_freeN);
519         MEM_freeN(ui_editsource_info);
520         ui_editsource_info= NULL;
521 }
522
523 static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
524 {
525 #if 0
526         printf("matching buttons: '%s' == '%s'\n",
527                but_a->drawstr, but_b->drawstr);
528 #endif
529
530         /* this just needs to be a 'good-enough' comparison so we can know beyond
531          * reasonable doubt that these buttons are the same between redraws.
532          * if this fails it only means edit-source fails - campbell */
533         if(     (but_a->x1 == but_b->x1) &&
534                 (but_a->x2 == but_b->x2) &&
535                 (but_a->y1 == but_b->y1) &&
536                 (but_a->y2 == but_b->y2) &&
537                 (but_a->type == but_b->type) &&
538                 (but_a->rnaprop == but_b->rnaprop) &&
539                 (but_a->optype == but_b->optype) &&
540                 (but_a->unit_type == but_b->unit_type) &&
541                 strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0
542         ) {
543                 return TRUE;
544         }
545         else {
546                 return FALSE;
547         }
548 }
549
550 void UI_editsource_active_but_test(uiBut *but)
551 {
552         extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
553
554         struct uiEditSourceButStore *but_store= MEM_callocN(sizeof(uiEditSourceButStore), __func__);
555
556         const char *fn;
557         int lineno= -1;
558
559 #if 0
560         printf("comparing buttons: '%s' == '%s'\n",
561                but->drawstr, ui_editsource_info->but_orig.drawstr);
562 #endif
563
564         PyC_FileAndNum_Safe(&fn, &lineno);
565
566         if (lineno != -1) {
567                 BLI_strncpy(but_store->py_dbg_fn, fn,
568                                         sizeof(but_store->py_dbg_fn));
569                 but_store->py_dbg_ln= lineno;
570         }
571         else {
572                 but_store->py_dbg_fn[0]= '\0';
573                 but_store->py_dbg_ln= -1;
574         }
575
576         BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
577 }
578
579 /* editsource operator component */
580
581 static ScrArea *biggest_text_view(bContext *C)
582 {
583         bScreen *sc= CTX_wm_screen(C);
584         ScrArea *sa, *big= NULL;
585         int size, maxsize= 0;
586
587         for(sa= sc->areabase.first; sa; sa= sa->next) {
588                 if(sa->spacetype==SPACE_TEXT) {
589                         size= sa->winx * sa->winy;
590                         if(size > maxsize) {
591                                 maxsize= size;
592                                 big= sa;
593                         }
594                 }
595         }
596         return big;
597 }
598
599 static int editsource_text_edit(bContext *C, wmOperator *op,
600                                 char filepath[240], int line)
601 {
602         struct Main *bmain= CTX_data_main(C);
603         Text *text;
604
605         for (text=bmain->text.first; text; text=text->id.next) {
606                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
607                         break;
608                 }
609         }
610
611         if (text == NULL) {
612                 text= add_text(filepath, bmain->name);
613         }
614
615         if (text == NULL) {
616                 BKE_reportf(op->reports, RPT_WARNING,
617                             "file: '%s' can't be opened", filepath);
618                 return OPERATOR_CANCELLED;
619         }
620         else {
621                 /* naughty!, find text area to set, not good behavior
622                  * but since this is a dev tool lets allow it - campbell */
623                 ScrArea *sa= biggest_text_view(C);
624                 if(sa) {
625                         SpaceText *st= sa->spacedata.first;
626                         st->text= text;
627                 }
628                 else {
629                         BKE_reportf(op->reports, RPT_INFO,
630                                     "See '%s' in the text editor", text->id.name + 2);
631                 }
632
633                 txt_move_toline(text, line - 1, FALSE);
634                 WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text);
635         }
636
637         return OPERATOR_FINISHED;
638 }
639
640 static int editsource_exec(bContext *C, wmOperator *op)
641 {
642         uiBut *but= uiContextActiveButton(C);
643
644         if (but) {
645                 GHashIterator ghi;
646                 struct uiEditSourceButStore *but_store= NULL;
647
648                 ARegion *ar= CTX_wm_region(C);
649                 int ret;
650
651                 uiFreeActiveButtons(C, CTX_wm_screen(C));
652
653                 // printf("%s: begin\n", __func__);
654
655                 ui_editsource_active_but_set(but);
656
657                 /* redraw and get active button python info */
658                 ED_region_do_draw(C, ar);
659
660                 for(BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
661                     !BLI_ghashIterator_isDone(&ghi);
662                     BLI_ghashIterator_step(&ghi))
663                 {
664                         uiBut *but= BLI_ghashIterator_getKey(&ghi);
665                         if (but && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but)) {
666                                 but_store= BLI_ghashIterator_getValue(&ghi);
667                                 break;
668                         }
669
670                 }
671
672                 if (but_store) {
673                         if (but_store->py_dbg_ln != -1) {
674                                 ret= editsource_text_edit(C, op,
675                                                           but_store->py_dbg_fn,
676                                                           but_store->py_dbg_ln);
677                         }
678                         else {
679                                 BKE_report(op->reports, RPT_ERROR,
680                                                    "Active button isn't from a script, cant edit source.");
681                                 ret= OPERATOR_CANCELLED;
682                         }
683                 }
684                 else {
685                         BKE_report(op->reports, RPT_ERROR,
686                                            "Active button match can't be found.");
687                         ret= OPERATOR_CANCELLED;
688                 }
689
690
691                 ui_editsource_active_but_clear();
692
693                 // printf("%s: end\n", __func__);
694
695                 return ret;
696         }
697         else {
698                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
699                 return OPERATOR_CANCELLED;
700         }
701 }
702
703 static void UI_OT_editsource(wmOperatorType *ot)
704 {
705         /* identifiers */
706         ot->name= "Reports to Text Block";
707         ot->idname= "UI_OT_editsource";
708         ot->description= "Edit source code for a button";
709
710         /* callbacks */
711         ot->exec= editsource_exec;
712 }
713
714
715 /* ********************************************************* */
716 /* Registration */
717
718 void UI_buttons_operatortypes(void)
719 {
720         WM_operatortype_append(UI_OT_eyedropper);
721         WM_operatortype_append(UI_OT_reset_default_theme);
722         WM_operatortype_append(UI_OT_copy_data_path_button);
723         WM_operatortype_append(UI_OT_reset_default_button);
724         WM_operatortype_append(UI_OT_copy_to_selected_button);
725         WM_operatortype_append(UI_OT_reports_to_textblock); // XXX: temp?
726         WM_operatortype_append(UI_OT_editsource);
727 }
728