utility function to find the biggest area: BKE_screen_find_big_area(...), was being...
[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
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                 const int color_manage = CTX_data_scene(C)->r.color_mgt_flag & R_COLOR_MANAGEMENT;
107                 float col[4];
108         
109                 RNA_property_float_get_array(&eye->ptr, eye->prop, col);
110                 
111                 glReadBuffer(GL_FRONT);
112                 glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, col);
113                 glReadBuffer(GL_BACK);
114         
115                 if (RNA_property_array_length(&eye->ptr, eye->prop) < 3) return;
116
117                 /* convert from screen (srgb) space to linear rgb space */
118                 if (color_manage && RNA_property_subtype(eye->prop) == PROP_COLOR)
119                         srgb_to_linearrgb_v3_v3(col, col);
120                 
121                 RNA_property_float_set_array(&eye->ptr, eye->prop, col);
122                 
123                 RNA_property_update(C, &eye->ptr, eye->prop);
124         }
125 }
126
127 /* main modal status check */
128 static int eyedropper_modal(bContext *C, wmOperator *op, wmEvent *event)
129 {
130         Eyedropper *eye = (Eyedropper *)op->customdata;
131         
132         switch(event->type) {
133                 case ESCKEY:
134                 case RIGHTMOUSE:
135                         return eyedropper_cancel(C, op);
136                 case LEFTMOUSE:
137                         if(event->val==KM_RELEASE) {
138                                 eyedropper_sample(C, eye, event->x, event->y);
139                                 eyedropper_exit(C, op);
140                                 return OPERATOR_FINISHED;
141                         }
142                         break;
143         }
144         
145         return OPERATOR_RUNNING_MODAL;
146 }
147
148 /* Modal Operator init */
149 static int eyedropper_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
150 {
151         /* init */
152         if (eyedropper_init(C, op)) {
153                 WM_cursor_modal(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
154
155                 /* add temp handler */
156                 WM_event_add_modal_handler(C, op);
157                 
158                 return OPERATOR_RUNNING_MODAL;
159         } else {
160                 eyedropper_exit(C, op);
161                 return OPERATOR_CANCELLED;
162         }
163 }
164
165 /* Repeat operator */
166 static int eyedropper_exec (bContext *C, wmOperator *op)
167 {
168         /* init */
169         if (eyedropper_init(C, op)) {
170                 
171                 /* do something */
172                 
173                 /* cleanup */
174                 eyedropper_exit(C, op);
175                 
176                 return OPERATOR_FINISHED;
177         } else {
178                 return OPERATOR_CANCELLED;
179         }
180 }
181
182 static int eyedropper_poll(bContext *C)
183 {
184         if (!CTX_wm_window(C)) return 0;
185         else return 1;
186 }
187
188 static void UI_OT_eyedropper(wmOperatorType *ot)
189 {
190         /* identifiers */
191         ot->name= "Eyedropper";
192         ot->idname= "UI_OT_eyedropper";
193         ot->description= "Sample a color from the Blender Window to store in a property";
194         
195         /* api callbacks */
196         ot->invoke= eyedropper_invoke;
197         ot->modal= eyedropper_modal;
198         ot->cancel= eyedropper_cancel;
199         ot->exec= eyedropper_exec;
200         ot->poll= eyedropper_poll;
201         
202         /* flags */
203         ot->flag= OPTYPE_BLOCKING;
204         
205         /* properties */
206 }
207
208 /* Reset Default Theme ------------------------ */
209
210 static int reset_default_theme_exec(bContext *C, wmOperator *UNUSED(op))
211 {
212         ui_theme_init_default();
213         WM_event_add_notifier(C, NC_WINDOW, NULL);
214         
215         return OPERATOR_FINISHED;
216 }
217
218 static void UI_OT_reset_default_theme(wmOperatorType *ot)
219 {
220         /* identifiers */
221         ot->name= "Reset to Default Theme";
222         ot->idname= "UI_OT_reset_default_theme";
223         ot->description= "Reset to the default theme colors";
224         
225         /* callbacks */
226         ot->exec= reset_default_theme_exec;
227         
228         /* flags */
229         ot->flag= OPTYPE_REGISTER;
230 }
231
232 /* Copy Data Path Operator ------------------------ */
233
234 static int copy_data_path_button_exec(bContext *C, wmOperator *UNUSED(op))
235 {
236         PointerRNA ptr;
237         PropertyRNA *prop;
238         char *path;
239         int success= 0;
240         int index;
241
242         /* try to create driver using property retrieved from UI */
243         uiContextActiveProperty(C, &ptr, &prop, &index);
244
245         if (ptr.id.data && ptr.data && prop) {
246                 path= RNA_path_from_ID_to_property(&ptr, prop);
247                 
248                 if (path) {
249                         WM_clipboard_text_set(path, FALSE);
250                         MEM_freeN(path);
251                 }
252         }
253
254         /* since we're just copying, we don't really need to do anything else...*/
255         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
256 }
257
258 static void UI_OT_copy_data_path_button(wmOperatorType *ot)
259 {
260         /* identifiers */
261         ot->name= "Copy Data Path";
262         ot->idname= "UI_OT_copy_data_path_button";
263         ot->description= "Copy the RNA data path for this property to the clipboard";
264
265         /* callbacks */
266         ot->exec= copy_data_path_button_exec;
267         //op->poll= ??? // TODO: need to have some valid property before this can be done
268
269         /* flags */
270         ot->flag= OPTYPE_REGISTER;
271 }
272
273 /* Reset to Default Values Button Operator ------------------------ */
274
275 static int reset_default_button_poll(bContext *C)
276 {
277         PointerRNA ptr;
278         PropertyRNA *prop;
279         int index;
280
281         uiContextActiveProperty(C, &ptr, &prop, &index);
282         
283         return (ptr.data && prop && RNA_property_editable(&ptr, prop));
284 }
285
286 static int reset_default_button_exec(bContext *C, wmOperator *op)
287 {
288         PointerRNA ptr;
289         PropertyRNA *prop;
290         int success= 0;
291         int index, all = RNA_boolean_get(op->ptr, "all");
292
293         /* try to reset the nominated setting to its default value */
294         uiContextActiveProperty(C, &ptr, &prop, &index);
295         
296         /* if there is a valid property that is editable... */
297         if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
298                 if(RNA_property_reset(&ptr, prop, (all)? -1: index)) {
299                         /* perform updates required for this property */
300                         RNA_property_update(C, &ptr, prop);
301
302                         /* as if we pressed the button */
303                         uiContextActivePropertyHandle(C);
304
305                         success= 1;
306                 }
307         }
308
309         /* Since we dont want to undo _all_ edits to settings, eg window
310          * edits on the screen or on operator settings.
311          * it might be better to move undo's inline - campbell */
312         if(success) {
313                 ID *id= ptr.id.data;
314                 if(id && ID_CHECK_UNDO(id)) {
315                         /* do nothing, go ahead with undo */
316                 }
317                 else {
318                         return OPERATOR_CANCELLED;
319                 }
320         }
321         /* end hack */
322
323         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
324 }
325
326 static void UI_OT_reset_default_button(wmOperatorType *ot)
327 {
328         /* identifiers */
329         ot->name= "Reset to Default Value";
330         ot->idname= "UI_OT_reset_default_button";
331         ot->description= "Reset this property's value to its default value";
332
333         /* callbacks */
334         ot->poll= reset_default_button_poll;
335         ot->exec= reset_default_button_exec;
336
337         /* flags */
338         ot->flag= OPTYPE_UNDO;
339         
340         /* properties */
341         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
342 }
343
344 /* Copy To Selected Operator ------------------------ */
345
346 static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb)
347 {
348         if(RNA_struct_is_a(ptr->type, &RNA_Object))
349                 *lb = CTX_data_collection_get(C, "selected_editable_objects");
350         else if(RNA_struct_is_a(ptr->type, &RNA_EditBone))
351                 *lb = CTX_data_collection_get(C, "selected_editable_bones");
352         else if(RNA_struct_is_a(ptr->type, &RNA_PoseBone))
353                 *lb = CTX_data_collection_get(C, "selected_pose_bones");
354         else if(RNA_struct_is_a(ptr->type, &RNA_Sequence))
355                 *lb = CTX_data_collection_get(C, "selected_editable_sequences");
356         else
357                 return 0;
358         
359         return 1;
360 }
361
362 static int copy_to_selected_button_poll(bContext *C)
363 {
364         PointerRNA ptr;
365         PropertyRNA *prop;
366         int index, success= 0;
367
368         uiContextActiveProperty(C, &ptr, &prop, &index);
369
370         if (ptr.data && prop) {
371                 CollectionPointerLink *link;
372                 ListBase lb;
373
374                 if(copy_to_selected_list(C, &ptr, &lb)) {
375                         for(link= lb.first; link; link=link->next)
376                                 if(link->ptr.data != ptr.data && RNA_property_editable(&link->ptr, prop))
377                                         success= 1;
378
379                         BLI_freelistN(&lb);
380                 }
381         }
382
383         return success;
384 }
385
386 static int copy_to_selected_button_exec(bContext *C, wmOperator *op)
387 {
388         PointerRNA ptr;
389         PropertyRNA *prop;
390         int success= 0;
391         int index, all = RNA_boolean_get(op->ptr, "all");
392
393         /* try to reset the nominated setting to its default value */
394         uiContextActiveProperty(C, &ptr, &prop, &index);
395         
396         /* if there is a valid property that is editable... */
397         if (ptr.data && prop) {
398                 CollectionPointerLink *link;
399                 ListBase lb;
400
401                 if(copy_to_selected_list(C, &ptr, &lb)) {
402                         for(link= lb.first; link; link=link->next) {
403                                 if(link->ptr.data != ptr.data && RNA_property_editable(&link->ptr, prop)) {
404                                         if(RNA_property_copy(&link->ptr, &ptr, prop, (all)? -1: index)) {
405                                                 RNA_property_update(C, &link->ptr, prop);
406                                                 success= 1;
407                                         }
408                                 }
409                         }
410
411                         BLI_freelistN(&lb);
412                 }
413         }
414         
415         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
416 }
417
418 static void UI_OT_copy_to_selected_button(wmOperatorType *ot)
419 {
420         /* identifiers */
421         ot->name= "Copy To Selected";
422         ot->idname= "UI_OT_copy_to_selected_button";
423         ot->description= "Copy property from this object to selected objects or bones";
424
425         /* callbacks */
426         ot->poll= copy_to_selected_button_poll;
427         ot->exec= copy_to_selected_button_exec;
428
429         /* flags */
430         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
431
432         /* properties */
433         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array");
434 }
435
436 /* Reports to Textblock Operator ------------------------ */
437
438 /* FIXME: this is just a temporary operator so that we can see all the reports somewhere 
439  * when there are too many to display...
440  */
441
442 static int reports_to_text_poll(bContext *C)
443 {
444         return CTX_wm_reports(C) != NULL;
445 }
446
447 static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
448 {
449         ReportList *reports = CTX_wm_reports(C);
450         Text *txt;
451         char *str;
452         
453         /* create new text-block to write to */
454         txt = add_empty_text("Recent Reports");
455         
456         /* convert entire list to a display string, and add this to the text-block
457          *      - if commandline debug option enabled, show debug reports too
458          *      - otherwise, up to info (which is what users normally see)
459          */
460         str = BKE_reports_string(reports, (G.f & G_DEBUG)? RPT_DEBUG : RPT_INFO);
461         
462         write_text(txt, str);
463         MEM_freeN(str);
464         
465         return OPERATOR_FINISHED;
466 }
467
468 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
469 {
470         /* identifiers */
471         ot->name= "Reports to Text Block";
472         ot->idname= "UI_OT_reports_to_textblock";
473         ot->description= "Write the reports ";
474         
475         /* callbacks */
476         ot->poll= reports_to_text_poll;
477         ot->exec= reports_to_text_exec;
478 }
479
480 #ifdef WITH_PYTHON
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 int editsource_text_edit(bContext *C, wmOperator *op,
582                                 char filepath[240], int line)
583 {
584         struct Main *bmain= CTX_data_main(C);
585         Text *text;
586
587         for (text=bmain->text.first; text; text=text->id.next) {
588                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
589                         break;
590                 }
591         }
592
593         if (text == NULL) {
594                 text= add_text(filepath, bmain->name);
595         }
596
597         if (text == NULL) {
598                 BKE_reportf(op->reports, RPT_WARNING,
599                             "file: '%s' can't be opened", filepath);
600                 return OPERATOR_CANCELLED;
601         }
602         else {
603                 /* naughty!, find text area to set, not good behavior
604                  * but since this is a dev tool lets allow it - campbell */
605                 ScrArea *sa= BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
606                 if(sa) {
607                         SpaceText *st= sa->spacedata.first;
608                         st->text= text;
609                 }
610                 else {
611                         BKE_reportf(op->reports, RPT_INFO,
612                                     "See '%s' in the text editor", text->id.name + 2);
613                 }
614
615                 txt_move_toline(text, line - 1, FALSE);
616                 WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text);
617         }
618
619         return OPERATOR_FINISHED;
620 }
621
622 static int editsource_exec(bContext *C, wmOperator *op)
623 {
624         uiBut *but= uiContextActiveButton(C);
625
626         if (but) {
627                 GHashIterator ghi;
628                 struct uiEditSourceButStore *but_store= NULL;
629
630                 ARegion *ar= CTX_wm_region(C);
631                 int ret;
632
633                 /* needed else the active button does not get tested */
634                 uiFreeActiveButtons(C, CTX_wm_screen(C));
635
636                 // printf("%s: begin\n", __func__);
637
638                 /* take care not to return before calling ui_editsource_active_but_clear */
639                 ui_editsource_active_but_set(but);
640
641                 /* redraw and get active button python info */
642                 ED_region_do_draw(C, ar);
643
644                 for(BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
645                     !BLI_ghashIterator_isDone(&ghi);
646                     BLI_ghashIterator_step(&ghi))
647                 {
648                         uiBut *but= BLI_ghashIterator_getKey(&ghi);
649                         if (but && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but)) {
650                                 but_store= BLI_ghashIterator_getValue(&ghi);
651                                 break;
652                         }
653
654                 }
655
656                 if (but_store) {
657                         if (but_store->py_dbg_ln != -1) {
658                                 ret= editsource_text_edit(C, op,
659                                                           but_store->py_dbg_fn,
660                                                           but_store->py_dbg_ln);
661                         }
662                         else {
663                                 BKE_report(op->reports, RPT_ERROR,
664                                                    "Active button isn't from a script, cant edit source.");
665                                 ret= OPERATOR_CANCELLED;
666                         }
667                 }
668                 else {
669                         BKE_report(op->reports, RPT_ERROR,
670                                            "Active button match can't be found.");
671                         ret= OPERATOR_CANCELLED;
672                 }
673
674
675                 ui_editsource_active_but_clear();
676
677                 // printf("%s: end\n", __func__);
678
679                 return ret;
680         }
681         else {
682                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
683                 return OPERATOR_CANCELLED;
684         }
685 }
686
687 static void UI_OT_editsource(wmOperatorType *ot)
688 {
689         /* identifiers */
690         ot->name= "Reports to Text Block";
691         ot->idname= "UI_OT_editsource";
692         ot->description= "Edit source code for a button";
693
694         /* callbacks */
695         ot->exec= editsource_exec;
696 }
697
698 #endif /* WITH_PYTHON */
699
700 /* ********************************************************* */
701 /* Registration */
702
703 void UI_buttons_operatortypes(void)
704 {
705         WM_operatortype_append(UI_OT_eyedropper);
706         WM_operatortype_append(UI_OT_reset_default_theme);
707         WM_operatortype_append(UI_OT_copy_data_path_button);
708         WM_operatortype_append(UI_OT_reset_default_button);
709         WM_operatortype_append(UI_OT_copy_to_selected_button);
710         WM_operatortype_append(UI_OT_reports_to_textblock); // XXX: temp?
711
712 #ifdef WITH_PYTHON
713         WM_operatortype_append(UI_OT_editsource);
714 #endif
715 }
716