Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / editors / interface / interface_eyedropper_datablock.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup edinterface
21  *
22  * Eyedropper (ID data-blocks)
23  *
24  * Defines:
25  * - #UI_OT_eyedropper_id
26  */
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_space_types.h"
31 #include "DNA_screen_types.h"
32 #include "DNA_object_types.h"
33
34 #include "BLI_string.h"
35
36 #include "BLT_translation.h"
37
38 #include "BKE_context.h"
39 #include "BKE_screen.h"
40 #include "BKE_report.h"
41 #include "BKE_idcode.h"
42
43 #include "RNA_access.h"
44
45 #include "UI_interface.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_space_api.h"
51 #include "ED_screen.h"
52 #include "ED_view3d.h"
53
54 #include "interface_intern.h"
55 #include "interface_eyedropper_intern.h"
56
57 /**
58  * \note #DataDropper is only internal name to avoid confusion with other kinds of eye-droppers.
59  */
60 typedef struct DataDropper {
61         PointerRNA ptr;
62         PropertyRNA *prop;
63         short idcode;
64         const char *idcode_name;
65         bool is_undo;
66
67         ID *init_id; /* for resetting on cancel */
68
69         ARegionType *art;
70         void *draw_handle_pixel;
71         char name[200];
72 } DataDropper;
73
74
75 static void datadropper_draw_cb(const struct bContext *C, ARegion *ar, void *arg)
76 {
77         DataDropper *ddr = arg;
78         eyedropper_draw_cursor_text(C, ar, ddr->name);
79 }
80
81
82 static int datadropper_init(bContext *C, wmOperator *op)
83 {
84         int index_dummy;
85         StructRNA *type;
86
87         SpaceType *st;
88         ARegionType *art;
89
90         st = BKE_spacetype_from_id(SPACE_VIEW3D);
91         art = BKE_regiontype_from_id(st, RGN_TYPE_WINDOW);
92
93         DataDropper *ddr = MEM_callocN(sizeof(DataDropper), __func__);
94
95         uiBut *but = UI_context_active_but_prop_get(C, &ddr->ptr, &ddr->prop, &index_dummy);
96
97         if ((ddr->ptr.data == NULL) ||
98             (ddr->prop == NULL) ||
99             (RNA_property_editable(&ddr->ptr, ddr->prop) == false) ||
100             (RNA_property_type(ddr->prop) != PROP_POINTER))
101         {
102                 MEM_freeN(ddr);
103                 return false;
104         }
105         op->customdata = ddr;
106
107         ddr->is_undo = UI_but_flag_is_set(but, UI_BUT_UNDO);
108
109         ddr->art = art;
110         ddr->draw_handle_pixel = ED_region_draw_cb_activate(art, datadropper_draw_cb, ddr, REGION_DRAW_POST_PIXEL);
111
112         type = RNA_property_pointer_type(&ddr->ptr, ddr->prop);
113         ddr->idcode = RNA_type_to_ID_code(type);
114         BLI_assert(ddr->idcode != 0);
115         /* Note we can translate here (instead of on draw time),
116          * because this struct has very short lifetime. */
117         ddr->idcode_name = TIP_(BKE_idcode_to_name(ddr->idcode));
118
119         PointerRNA ptr = RNA_property_pointer_get(&ddr->ptr, ddr->prop);
120         ddr->init_id = ptr.id.data;
121
122         return true;
123 }
124
125 static void datadropper_exit(bContext *C, wmOperator *op)
126 {
127         WM_cursor_modal_restore(CTX_wm_window(C));
128
129         if (op->customdata) {
130                 DataDropper *ddr = (DataDropper *)op->customdata;
131
132                 if (ddr->art) {
133                         ED_region_draw_cb_exit(ddr->art, ddr->draw_handle_pixel);
134                 }
135
136                 MEM_freeN(op->customdata);
137
138                 op->customdata = NULL;
139         }
140
141         WM_event_add_mousemove(C);
142 }
143
144 /* *** datadropper id helper functions *** */
145 /**
146  * \brief get the ID from the screen.
147  */
148 static void datadropper_id_sample_pt(bContext *C, DataDropper *ddr, int mx, int my, ID **r_id)
149 {
150         /* we could use some clever */
151         bScreen *screen = CTX_wm_screen(C);
152         ScrArea *sa = BKE_screen_find_area_xy(screen, -1, mx, my);
153
154         ScrArea *area_prev = CTX_wm_area(C);
155         ARegion *ar_prev = CTX_wm_region(C);
156
157         ddr->name[0] = '\0';
158
159         if (sa) {
160                 if (sa->spacetype == SPACE_VIEW3D) {
161                         ARegion *ar = BKE_area_find_region_xy(sa, RGN_TYPE_WINDOW, mx, my);
162                         if (ar) {
163                                 const int mval[2] = {
164                                     mx - ar->winrct.xmin,
165                                     my - ar->winrct.ymin};
166                                 Base *base;
167
168                                 CTX_wm_area_set(C, sa);
169                                 CTX_wm_region_set(C, ar);
170
171                                 /* grr, always draw else we leave stale text */
172                                 ED_region_tag_redraw(ar);
173
174                                 base = ED_view3d_give_base_under_cursor(C, mval);
175                                 if (base) {
176                                         Object *ob = base->object;
177                                         ID *id = NULL;
178                                         if (ddr->idcode == ID_OB) {
179                                                 id = (ID *)ob;
180                                         }
181                                         else if (ob->data) {
182                                                 if (GS(((ID *)ob->data)->name) == ddr->idcode) {
183                                                         id = (ID *)ob->data;
184                                                 }
185                                                 else {
186                                                         BLI_snprintf(ddr->name, sizeof(ddr->name), "Incompatible, expected a %s",
187                                                                      ddr->idcode_name);
188                                                 }
189                                         }
190
191                                         PointerRNA idptr;
192                                         RNA_id_pointer_create(id, &idptr);
193
194                                         if (id && RNA_property_pointer_poll(&ddr->ptr, ddr->prop, &idptr)) {
195                                                 BLI_snprintf(ddr->name, sizeof(ddr->name), "%s: %s",
196                                                              ddr->idcode_name, id->name + 2);
197                                                 *r_id = id;
198                                         }
199                                 }
200                         }
201                 }
202         }
203
204         CTX_wm_area_set(C, area_prev);
205         CTX_wm_region_set(C, ar_prev);
206 }
207
208 /* sets the ID, returns success */
209 static bool datadropper_id_set(bContext *C, DataDropper *ddr, ID *id)
210 {
211         PointerRNA ptr_value;
212
213         RNA_id_pointer_create(id, &ptr_value);
214
215         RNA_property_pointer_set(&ddr->ptr, ddr->prop, ptr_value);
216
217         RNA_property_update(C, &ddr->ptr, ddr->prop);
218
219         ptr_value = RNA_property_pointer_get(&ddr->ptr, ddr->prop);
220
221         return (ptr_value.id.data == id);
222 }
223
224 /* single point sample & set */
225 static bool datadropper_id_sample(bContext *C, DataDropper *ddr, int mx, int my)
226 {
227         ID *id = NULL;
228
229         datadropper_id_sample_pt(C, ddr, mx, my, &id);
230         return datadropper_id_set(C, ddr, id);
231 }
232
233 static void datadropper_cancel(bContext *C, wmOperator *op)
234 {
235         DataDropper *ddr = op->customdata;
236         datadropper_id_set(C, ddr, ddr->init_id);
237         datadropper_exit(C, op);
238 }
239
240 /* main modal status check */
241 static int datadropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
242 {
243         DataDropper *ddr = (DataDropper *)op->customdata;
244
245         /* handle modal keymap */
246         if (event->type == EVT_MODAL_MAP) {
247                 switch (event->val) {
248                         case EYE_MODAL_CANCEL:
249                                 datadropper_cancel(C, op);
250                                 return OPERATOR_CANCELLED;
251                         case EYE_MODAL_SAMPLE_CONFIRM:
252                         {
253                                 const bool is_undo = ddr->is_undo;
254                                 const bool success = datadropper_id_sample(C, ddr, event->x, event->y);
255                                 datadropper_exit(C, op);
256                                 if (success) {
257                                         /* Could support finished & undo-skip. */
258                                         return is_undo ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
259                                 }
260                                 else {
261                                         BKE_report(op->reports, RPT_WARNING, "Failed to set value");
262                                         return OPERATOR_CANCELLED;
263                                 }
264                         }
265                 }
266         }
267         else if (event->type == MOUSEMOVE) {
268                 ID *id = NULL;
269                 datadropper_id_sample_pt(C, ddr, event->x, event->y, &id);
270         }
271
272         return OPERATOR_RUNNING_MODAL;
273 }
274
275 /* Modal Operator init */
276 static int datadropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
277 {
278         /* init */
279         if (datadropper_init(C, op)) {
280                 WM_cursor_modal_set(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
281
282                 /* add temp handler */
283                 WM_event_add_modal_handler(C, op);
284
285                 return OPERATOR_RUNNING_MODAL;
286         }
287         else {
288                 return OPERATOR_CANCELLED;
289         }
290 }
291
292 /* Repeat operator */
293 static int datadropper_exec(bContext *C, wmOperator *op)
294 {
295         /* init */
296         if (datadropper_init(C, op)) {
297                 /* cleanup */
298                 datadropper_exit(C, op);
299
300                 return OPERATOR_FINISHED;
301         }
302         else {
303                 return OPERATOR_CANCELLED;
304         }
305 }
306
307 static bool datadropper_poll(bContext *C)
308 {
309         PointerRNA ptr;
310         PropertyRNA *prop;
311         int index_dummy;
312         uiBut *but;
313
314         /* data dropper only supports object data */
315         if ((CTX_wm_window(C) != NULL) &&
316             (but = UI_context_active_but_prop_get(C, &ptr, &prop, &index_dummy)) &&
317             (but->type == UI_BTYPE_SEARCH_MENU) &&
318             (but->flag & UI_BUT_VALUE_CLEAR))
319         {
320                 if (prop && RNA_property_type(prop) == PROP_POINTER) {
321                         StructRNA *type = RNA_property_pointer_type(&ptr, prop);
322                         const short idcode = RNA_type_to_ID_code(type);
323                         if ((idcode == ID_OB) || OB_DATA_SUPPORT_ID(idcode)) {
324                                 return 1;
325                         }
326                 }
327         }
328
329         return 0;
330 }
331
332 void UI_OT_eyedropper_id(wmOperatorType *ot)
333 {
334         /* identifiers */
335         ot->name = "Eyedropper Data-Block";
336         ot->idname = "UI_OT_eyedropper_id";
337         ot->description = "Sample a data-block from the 3D View to store in a property";
338
339         /* api callbacks */
340         ot->invoke = datadropper_invoke;
341         ot->modal = datadropper_modal;
342         ot->cancel = datadropper_cancel;
343         ot->exec = datadropper_exec;
344         ot->poll = datadropper_poll;
345
346         /* flags */
347         ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING | OPTYPE_INTERNAL;
348
349         /* properties */
350 }