Code Cleanup:
[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
31 #include <stdio.h>
32 #include <math.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_text_types.h" /* for UI_OT_reports_to_text */
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math_color.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47 #include "BKE_global.h"
48 #include "BKE_text.h" /* for UI_OT_reports_to_text */
49 #include "BKE_report.h"
50
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53
54 #include "BIF_gl.h"
55
56 #include "UI_interface.h"
57
58 #include "interface_intern.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 /* only for UI_OT_editsource */
64 #include "ED_screen.h"
65 #include "BKE_main.h"
66 #include "BLI_ghash.h"
67
68
69 /* ********************************************************** */
70
71 typedef struct Eyedropper {
72         PointerRNA ptr;
73         PropertyRNA *prop;
74         int index;
75 } Eyedropper;
76
77 static int eyedropper_init(bContext *C, wmOperator *op)
78 {
79         Eyedropper *eye;
80         
81         op->customdata= eye= MEM_callocN(sizeof(Eyedropper), "Eyedropper");
82         
83         uiContextActiveProperty(C, &eye->ptr, &eye->prop, &eye->index);
84         
85         return (eye->ptr.data && eye->prop && RNA_property_editable(&eye->ptr, eye->prop));
86 }
87  
88 static void eyedropper_exit(bContext *C, wmOperator *op)
89 {
90         WM_cursor_restore(CTX_wm_window(C));
91         
92         if(op->customdata)
93                 MEM_freeN(op->customdata);
94         op->customdata= NULL;
95 }
96
97 static int eyedropper_cancel(bContext *C, wmOperator *op)
98 {
99         eyedropper_exit(C, op);
100         return OPERATOR_CANCELLED;
101 }
102
103 static void eyedropper_sample(bContext *C, Eyedropper *eye, int mx, int my)
104 {
105         if(RNA_property_type(eye->prop) == PROP_FLOAT) {
106                 Scene *scene = CTX_data_scene(C);
107                 const int color_manage = scene->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         if (str) {
464                 write_text(txt, str);
465                 MEM_freeN(str);
466
467                 return OPERATOR_FINISHED;
468         }
469         else {
470                 return OPERATOR_CANCELLED;
471         }
472 }
473
474 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
475 {
476         /* identifiers */
477         ot->name= "Reports to Text Block";
478         ot->idname= "UI_OT_reports_to_textblock";
479         ot->description= "Write the reports ";
480         
481         /* callbacks */
482         ot->poll= reports_to_text_poll;
483         ot->exec= reports_to_text_exec;
484 }
485
486 #ifdef WITH_PYTHON
487
488 /* ------------------------------------------------------------------------- */
489 /* EditSource Utility funcs and operator,
490  * note, this includes itility functions and button matching checks */
491
492 struct uiEditSourceStore {
493         uiBut but_orig;
494         GHash *hash;
495 } uiEditSourceStore;
496
497 struct uiEditSourceButStore {
498         char py_dbg_fn[FILE_MAX];
499         int py_dbg_ln;
500 } uiEditSourceButStore;
501
502 /* should only ever be set while the edit source operator is running */
503 static struct uiEditSourceStore *ui_editsource_info= NULL;
504
505 int  UI_editsource_enable_check(void)
506 {
507         return (ui_editsource_info != NULL);
508 }
509
510 static void ui_editsource_active_but_set(uiBut *but)
511 {
512         BLI_assert(ui_editsource_info == NULL);
513
514         ui_editsource_info= MEM_callocN(sizeof(uiEditSourceStore), __func__);
515         memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
516
517         ui_editsource_info->hash = BLI_ghash_new(BLI_ghashutil_ptrhash,
518                                                  BLI_ghashutil_ptrcmp,
519                                                  __func__);
520 }
521
522 static void ui_editsource_active_but_clear(void)
523 {
524         BLI_ghash_free(ui_editsource_info->hash, NULL, (GHashValFreeFP)MEM_freeN);
525         MEM_freeN(ui_editsource_info);
526         ui_editsource_info= NULL;
527 }
528
529 static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
530 {
531 #if 0
532         printf("matching buttons: '%s' == '%s'\n",
533                but_a->drawstr, but_b->drawstr);
534 #endif
535
536         /* this just needs to be a 'good-enough' comparison so we can know beyond
537          * reasonable doubt that these buttons are the same between redraws.
538          * if this fails it only means edit-source fails - campbell */
539         if(     (but_a->x1 == but_b->x1) &&
540                 (but_a->x2 == but_b->x2) &&
541                 (but_a->y1 == but_b->y1) &&
542                 (but_a->y2 == but_b->y2) &&
543                 (but_a->type == but_b->type) &&
544                 (but_a->rnaprop == but_b->rnaprop) &&
545                 (but_a->optype == but_b->optype) &&
546                 (but_a->unit_type == but_b->unit_type) &&
547                 strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0
548         ) {
549                 return TRUE;
550         }
551         else {
552                 return FALSE;
553         }
554 }
555
556 void UI_editsource_active_but_test(uiBut *but)
557 {
558         extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
559
560         struct uiEditSourceButStore *but_store= MEM_callocN(sizeof(uiEditSourceButStore), __func__);
561
562         const char *fn;
563         int lineno= -1;
564
565 #if 0
566         printf("comparing buttons: '%s' == '%s'\n",
567                but->drawstr, ui_editsource_info->but_orig.drawstr);
568 #endif
569
570         PyC_FileAndNum_Safe(&fn, &lineno);
571
572         if (lineno != -1) {
573                 BLI_strncpy(but_store->py_dbg_fn, fn,
574                                         sizeof(but_store->py_dbg_fn));
575                 but_store->py_dbg_ln= lineno;
576         }
577         else {
578                 but_store->py_dbg_fn[0]= '\0';
579                 but_store->py_dbg_ln= -1;
580         }
581
582         BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
583 }
584
585 /* editsource operator component */
586
587 static int editsource_text_edit(bContext *C, wmOperator *op,
588                                 char filepath[FILE_MAX], int line)
589 {
590         struct Main *bmain= CTX_data_main(C);
591         Text *text;
592
593         for (text=bmain->text.first; text; text=text->id.next) {
594                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
595                         break;
596                 }
597         }
598
599         if (text == NULL) {
600                 text= add_text(filepath, bmain->name);
601         }
602
603         if (text == NULL) {
604                 BKE_reportf(op->reports, RPT_WARNING,
605                             "file: '%s' can't be opened", filepath);
606                 return OPERATOR_CANCELLED;
607         }
608         else {
609                 /* naughty!, find text area to set, not good behavior
610                  * but since this is a dev tool lets allow it - campbell */
611                 ScrArea *sa= BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
612                 if(sa) {
613                         SpaceText *st= sa->spacedata.first;
614                         st->text= text;
615                 }
616                 else {
617                         BKE_reportf(op->reports, RPT_INFO,
618                                     "See '%s' in the text editor", text->id.name + 2);
619                 }
620
621                 txt_move_toline(text, line - 1, FALSE);
622                 WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text);
623         }
624
625         return OPERATOR_FINISHED;
626 }
627
628 static int editsource_exec(bContext *C, wmOperator *op)
629 {
630         uiBut *but= uiContextActiveButton(C);
631
632         if (but) {
633                 GHashIterator ghi;
634                 struct uiEditSourceButStore *but_store= NULL;
635
636                 ARegion *ar= CTX_wm_region(C);
637                 int ret;
638
639                 /* needed else the active button does not get tested */
640                 uiFreeActiveButtons(C, CTX_wm_screen(C));
641
642                 // printf("%s: begin\n", __func__);
643
644                 /* take care not to return before calling ui_editsource_active_but_clear */
645                 ui_editsource_active_but_set(but);
646
647                 /* redraw and get active button python info */
648                 ED_region_do_draw(C, ar);
649
650                 for(BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
651                     !BLI_ghashIterator_isDone(&ghi);
652                     BLI_ghashIterator_step(&ghi))
653                 {
654                         uiBut *but= BLI_ghashIterator_getKey(&ghi);
655                         if (but && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but)) {
656                                 but_store= BLI_ghashIterator_getValue(&ghi);
657                                 break;
658                         }
659
660                 }
661
662                 if (but_store) {
663                         if (but_store->py_dbg_ln != -1) {
664                                 ret= editsource_text_edit(C, op,
665                                                           but_store->py_dbg_fn,
666                                                           but_store->py_dbg_ln);
667                         }
668                         else {
669                                 BKE_report(op->reports, RPT_ERROR,
670                                                    "Active button isn't from a script, cant edit source.");
671                                 ret= OPERATOR_CANCELLED;
672                         }
673                 }
674                 else {
675                         BKE_report(op->reports, RPT_ERROR,
676                                            "Active button match can't be found.");
677                         ret= OPERATOR_CANCELLED;
678                 }
679
680
681                 ui_editsource_active_but_clear();
682
683                 // printf("%s: end\n", __func__);
684
685                 return ret;
686         }
687         else {
688                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
689                 return OPERATOR_CANCELLED;
690         }
691 }
692
693 static void UI_OT_editsource(wmOperatorType *ot)
694 {
695         /* identifiers */
696         ot->name= "Reports to Text Block";
697         ot->idname= "UI_OT_editsource";
698         ot->description= "Edit source code for a button";
699
700         /* callbacks */
701         ot->exec= editsource_exec;
702 }
703
704 #endif /* WITH_PYTHON */
705
706 /* ********************************************************* */
707 /* Registration */
708
709 void UI_buttons_operatortypes(void)
710 {
711         WM_operatortype_append(UI_OT_eyedropper);
712         WM_operatortype_append(UI_OT_reset_default_theme);
713         WM_operatortype_append(UI_OT_copy_data_path_button);
714         WM_operatortype_append(UI_OT_reset_default_button);
715         WM_operatortype_append(UI_OT_copy_to_selected_button);
716         WM_operatortype_append(UI_OT_reports_to_textblock); // XXX: temp?
717
718 #ifdef WITH_PYTHON
719         WM_operatortype_append(UI_OT_editsource);
720 #endif
721 }
722