uv sculpting now functional on bmesh
[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         if (str) {
463                 write_text(txt, str);
464                 MEM_freeN(str);
465
466                 return OPERATOR_FINISHED;
467         }
468         else {
469                 return OPERATOR_CANCELLED;
470         }
471 }
472
473 static void UI_OT_reports_to_textblock(wmOperatorType *ot)
474 {
475         /* identifiers */
476         ot->name= "Reports to Text Block";
477         ot->idname= "UI_OT_reports_to_textblock";
478         ot->description= "Write the reports ";
479         
480         /* callbacks */
481         ot->poll= reports_to_text_poll;
482         ot->exec= reports_to_text_exec;
483 }
484
485 #ifdef WITH_PYTHON
486
487 /* ------------------------------------------------------------------------- */
488 /* EditSource Utility funcs and operator,
489  * note, this includes itility functions and button matching checks */
490
491 struct uiEditSourceStore {
492         uiBut but_orig;
493         GHash *hash;
494 } uiEditSourceStore;
495
496 struct uiEditSourceButStore {
497         char py_dbg_fn[FILE_MAX];
498         int py_dbg_ln;
499 } uiEditSourceButStore;
500
501 /* should only ever be set while the edit source operator is running */
502 static struct uiEditSourceStore *ui_editsource_info= NULL;
503
504 int  UI_editsource_enable_check(void)
505 {
506         return (ui_editsource_info != NULL);
507 }
508
509 static void ui_editsource_active_but_set(uiBut *but)
510 {
511         BLI_assert(ui_editsource_info == NULL);
512
513         ui_editsource_info= MEM_callocN(sizeof(uiEditSourceStore), __func__);
514         memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
515
516         ui_editsource_info->hash = BLI_ghash_new(BLI_ghashutil_ptrhash,
517                                                  BLI_ghashutil_ptrcmp,
518                                                  __func__);
519 }
520
521 static void ui_editsource_active_but_clear(void)
522 {
523         BLI_ghash_free(ui_editsource_info->hash, NULL, (GHashValFreeFP)MEM_freeN);
524         MEM_freeN(ui_editsource_info);
525         ui_editsource_info= NULL;
526 }
527
528 static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
529 {
530 #if 0
531         printf("matching buttons: '%s' == '%s'\n",
532                but_a->drawstr, but_b->drawstr);
533 #endif
534
535         /* this just needs to be a 'good-enough' comparison so we can know beyond
536          * reasonable doubt that these buttons are the same between redraws.
537          * if this fails it only means edit-source fails - campbell */
538         if(     (but_a->x1 == but_b->x1) &&
539                 (but_a->x2 == but_b->x2) &&
540                 (but_a->y1 == but_b->y1) &&
541                 (but_a->y2 == but_b->y2) &&
542                 (but_a->type == but_b->type) &&
543                 (but_a->rnaprop == but_b->rnaprop) &&
544                 (but_a->optype == but_b->optype) &&
545                 (but_a->unit_type == but_b->unit_type) &&
546                 strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0
547         ) {
548                 return TRUE;
549         }
550         else {
551                 return FALSE;
552         }
553 }
554
555 void UI_editsource_active_but_test(uiBut *but)
556 {
557         extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
558
559         struct uiEditSourceButStore *but_store= MEM_callocN(sizeof(uiEditSourceButStore), __func__);
560
561         const char *fn;
562         int lineno= -1;
563
564 #if 0
565         printf("comparing buttons: '%s' == '%s'\n",
566                but->drawstr, ui_editsource_info->but_orig.drawstr);
567 #endif
568
569         PyC_FileAndNum_Safe(&fn, &lineno);
570
571         if (lineno != -1) {
572                 BLI_strncpy(but_store->py_dbg_fn, fn,
573                                         sizeof(but_store->py_dbg_fn));
574                 but_store->py_dbg_ln= lineno;
575         }
576         else {
577                 but_store->py_dbg_fn[0]= '\0';
578                 but_store->py_dbg_ln= -1;
579         }
580
581         BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
582 }
583
584 /* editsource operator component */
585
586 static int editsource_text_edit(bContext *C, wmOperator *op,
587                                 char filepath[FILE_MAX], int line)
588 {
589         struct Main *bmain= CTX_data_main(C);
590         Text *text;
591
592         for (text=bmain->text.first; text; text=text->id.next) {
593                 if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
594                         break;
595                 }
596         }
597
598         if (text == NULL) {
599                 text= add_text(filepath, bmain->name);
600         }
601
602         if (text == NULL) {
603                 BKE_reportf(op->reports, RPT_WARNING,
604                             "file: '%s' can't be opened", filepath);
605                 return OPERATOR_CANCELLED;
606         }
607         else {
608                 /* naughty!, find text area to set, not good behavior
609                  * but since this is a dev tool lets allow it - campbell */
610                 ScrArea *sa= BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
611                 if(sa) {
612                         SpaceText *st= sa->spacedata.first;
613                         st->text= text;
614                 }
615                 else {
616                         BKE_reportf(op->reports, RPT_INFO,
617                                     "See '%s' in the text editor", text->id.name + 2);
618                 }
619
620                 txt_move_toline(text, line - 1, FALSE);
621                 WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text);
622         }
623
624         return OPERATOR_FINISHED;
625 }
626
627 static int editsource_exec(bContext *C, wmOperator *op)
628 {
629         uiBut *but= uiContextActiveButton(C);
630
631         if (but) {
632                 GHashIterator ghi;
633                 struct uiEditSourceButStore *but_store= NULL;
634
635                 ARegion *ar= CTX_wm_region(C);
636                 int ret;
637
638                 /* needed else the active button does not get tested */
639                 uiFreeActiveButtons(C, CTX_wm_screen(C));
640
641                 // printf("%s: begin\n", __func__);
642
643                 /* take care not to return before calling ui_editsource_active_but_clear */
644                 ui_editsource_active_but_set(but);
645
646                 /* redraw and get active button python info */
647                 ED_region_do_draw(C, ar);
648
649                 for(BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
650                     !BLI_ghashIterator_isDone(&ghi);
651                     BLI_ghashIterator_step(&ghi))
652                 {
653                         uiBut *but= BLI_ghashIterator_getKey(&ghi);
654                         if (but && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but)) {
655                                 but_store= BLI_ghashIterator_getValue(&ghi);
656                                 break;
657                         }
658
659                 }
660
661                 if (but_store) {
662                         if (but_store->py_dbg_ln != -1) {
663                                 ret= editsource_text_edit(C, op,
664                                                           but_store->py_dbg_fn,
665                                                           but_store->py_dbg_ln);
666                         }
667                         else {
668                                 BKE_report(op->reports, RPT_ERROR,
669                                                    "Active button isn't from a script, cant edit source.");
670                                 ret= OPERATOR_CANCELLED;
671                         }
672                 }
673                 else {
674                         BKE_report(op->reports, RPT_ERROR,
675                                            "Active button match can't be found.");
676                         ret= OPERATOR_CANCELLED;
677                 }
678
679
680                 ui_editsource_active_but_clear();
681
682                 // printf("%s: end\n", __func__);
683
684                 return ret;
685         }
686         else {
687                 BKE_report(op->reports, RPT_ERROR, "Active button not found");
688                 return OPERATOR_CANCELLED;
689         }
690 }
691
692 static void UI_OT_editsource(wmOperatorType *ot)
693 {
694         /* identifiers */
695         ot->name= "Reports to Text Block";
696         ot->idname= "UI_OT_editsource";
697         ot->description= "Edit source code for a button";
698
699         /* callbacks */
700         ot->exec= editsource_exec;
701 }
702
703 #endif /* WITH_PYTHON */
704
705 /* ********************************************************* */
706 /* Registration */
707
708 void UI_buttons_operatortypes(void)
709 {
710         WM_operatortype_append(UI_OT_eyedropper);
711         WM_operatortype_append(UI_OT_reset_default_theme);
712         WM_operatortype_append(UI_OT_copy_data_path_button);
713         WM_operatortype_append(UI_OT_reset_default_button);
714         WM_operatortype_append(UI_OT_copy_to_selected_button);
715         WM_operatortype_append(UI_OT_reports_to_textblock); // XXX: temp?
716
717 #ifdef WITH_PYTHON
718         WM_operatortype_append(UI_OT_editsource);
719 #endif
720 }
721