Fix #35267: cmd+v, cmd+c on OS X for copy/paste worked in some editors like the 3D
[blender.git] / source / blender / editors / space_node / node_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) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation, Nathan Letwory
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_node/node_ops.c
28  *  \ingroup spnode
29  */
30
31
32 #include "DNA_node_types.h"
33
34 #include "BLI_utildefines.h"
35
36 #include "BKE_context.h"
37
38 #include "ED_node.h"  /* own include */
39 #include "ED_screen.h"
40 #include "ED_transform.h"
41
42 #include "RNA_access.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "node_intern.h"  /* own include */
48
49 void node_operatortypes(void)
50 {
51         WM_operatortype_append(NODE_OT_properties);
52         WM_operatortype_append(NODE_OT_toolbar);
53         
54         WM_operatortype_append(NODE_OT_select);
55         WM_operatortype_append(NODE_OT_select_all);
56         WM_operatortype_append(NODE_OT_select_linked_to);
57         WM_operatortype_append(NODE_OT_select_linked_from);
58         WM_operatortype_append(NODE_OT_select_border);
59         WM_operatortype_append(NODE_OT_select_lasso);
60         WM_operatortype_append(NODE_OT_select_same_type);
61         WM_operatortype_append(NODE_OT_select_same_type_step);
62         
63         WM_operatortype_append(NODE_OT_find_node);
64         
65         WM_operatortype_append(NODE_OT_view_all);
66         WM_operatortype_append(NODE_OT_view_selected);
67
68         WM_operatortype_append(NODE_OT_mute_toggle);
69         WM_operatortype_append(NODE_OT_hide_toggle);
70         WM_operatortype_append(NODE_OT_preview_toggle);
71         WM_operatortype_append(NODE_OT_options_toggle);
72         WM_operatortype_append(NODE_OT_hide_socket_toggle);
73         WM_operatortype_append(NODE_OT_show_cyclic_dependencies);
74         WM_operatortype_append(NODE_OT_node_copy_color);
75         
76         WM_operatortype_append(NODE_OT_duplicate);
77         WM_operatortype_append(NODE_OT_delete);
78         WM_operatortype_append(NODE_OT_delete_reconnect);
79         WM_operatortype_append(NODE_OT_resize);
80         
81         WM_operatortype_append(NODE_OT_link);
82         WM_operatortype_append(NODE_OT_link_make);
83         WM_operatortype_append(NODE_OT_links_cut);
84         WM_operatortype_append(NODE_OT_links_detach);
85         WM_operatortype_append(NODE_OT_add_reroute);
86
87         WM_operatortype_append(NODE_OT_group_make);
88         WM_operatortype_append(NODE_OT_group_insert);
89         WM_operatortype_append(NODE_OT_group_ungroup);
90         WM_operatortype_append(NODE_OT_group_separate);
91         WM_operatortype_append(NODE_OT_group_edit);
92         
93         WM_operatortype_append(NODE_OT_link_viewer);
94         
95         WM_operatortype_append(NODE_OT_read_renderlayers);
96         WM_operatortype_append(NODE_OT_read_fullsamplelayers);
97         WM_operatortype_append(NODE_OT_render_changed);
98         
99         WM_operatortype_append(NODE_OT_backimage_move);
100         WM_operatortype_append(NODE_OT_backimage_zoom);
101         WM_operatortype_append(NODE_OT_backimage_sample);
102         
103         WM_operatortype_append(NODE_OT_add_file);
104         
105         WM_operatortype_append(NODE_OT_new_node_tree);
106         
107         WM_operatortype_append(NODE_OT_output_file_add_socket);
108         WM_operatortype_append(NODE_OT_output_file_remove_active_socket);
109         WM_operatortype_append(NODE_OT_output_file_move_active_socket);
110         
111         WM_operatortype_append(NODE_OT_parent_set);
112         WM_operatortype_append(NODE_OT_parent_clear);
113         WM_operatortype_append(NODE_OT_join);
114         WM_operatortype_append(NODE_OT_attach);
115         WM_operatortype_append(NODE_OT_detach);
116         
117         WM_operatortype_append(NODE_OT_clipboard_copy);
118         WM_operatortype_append(NODE_OT_clipboard_paste);
119         
120         WM_operatortype_append(NODE_OT_shader_script_update);
121
122         WM_operatortype_append(NODE_OT_viewer_border);
123
124         WM_operatortype_append(NODE_OT_tree_socket_add);
125         WM_operatortype_append(NODE_OT_tree_socket_remove);
126         WM_operatortype_append(NODE_OT_tree_socket_move);
127 }
128
129 void ED_operatormacros_node(void)
130 {
131         wmOperatorType *ot;
132         wmOperatorTypeMacro *mot;
133         
134         ot = WM_operatortype_append_macro("NODE_OT_select_link_viewer", "Link Viewer",
135                                           "Select node and link it to a viewer node",
136                                           OPTYPE_UNDO);
137         WM_operatortype_macro_define(ot, "NODE_OT_select");
138         WM_operatortype_macro_define(ot, "NODE_OT_link_viewer");
139
140         ot = WM_operatortype_append_macro("NODE_OT_translate_attach", "Move and Attach",
141                                           "Move nodes and attach to frame",
142                                           OPTYPE_UNDO | OPTYPE_REGISTER);
143         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
144         RNA_boolean_set(mot->ptr, "release_confirm", TRUE);
145         WM_operatortype_macro_define(ot, "NODE_OT_attach");
146
147         ot = WM_operatortype_append_macro("NODE_OT_detach_translate_attach", "Detach and Move",
148                                           "Detach nodes, move and attach to frame",
149                                           OPTYPE_UNDO | OPTYPE_REGISTER);
150         WM_operatortype_macro_define(ot, "NODE_OT_detach");
151         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
152         RNA_boolean_set(mot->ptr, "release_confirm", TRUE);
153         WM_operatortype_macro_define(ot, "NODE_OT_attach");
154
155         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move", "Duplicate",
156                                           "Duplicate selected nodes and move them",
157                                           OPTYPE_UNDO | OPTYPE_REGISTER);
158         WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
159         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
160
161         /* modified operator call for duplicating with input links */
162         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move_keep_inputs", "Duplicate",
163                                           "Duplicate selected nodes keeping input links and move them",
164                                           OPTYPE_UNDO | OPTYPE_REGISTER);
165         mot = WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
166         RNA_boolean_set(mot->ptr, "keep_inputs", TRUE);
167         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
168
169         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links", "Detach", "Move a node to detach links",
170                                           OPTYPE_UNDO | OPTYPE_REGISTER);
171         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
172         WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
173
174         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links_release", "Detach", "Move a node to detach links",
175                                           OPTYPE_UNDO | OPTYPE_REGISTER);
176         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
177         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
178 }
179
180 /* helper function for repetitive select operator keymap */
181 static void node_select_keymap(wmKeyMap *keymap, int extend)
182 {
183         /* modifier combinations */
184         const int mod_single[] = { 0, KM_CTRL, KM_ALT, KM_CTRL | KM_ALT,
185                                    -1 /* terminator */
186         };
187         const int mod_extend[] = { KM_SHIFT, KM_SHIFT | KM_CTRL,
188                                    KM_SHIFT | KM_ALT, KM_SHIFT | KM_CTRL | KM_ALT,
189                                    -1 /* terminator */
190         };
191         const int *mod = (extend ? mod_extend : mod_single);
192         wmKeyMapItem *kmi;
193         int i;
194         
195         for (i = 0; mod[i] >= 0; ++i) {
196                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", ACTIONMOUSE, KM_PRESS, mod[i], 0);
197                 RNA_boolean_set(kmi->ptr, "extend", extend);
198                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", SELECTMOUSE, KM_PRESS, mod[i], 0);
199                 RNA_boolean_set(kmi->ptr, "extend", extend);
200         }
201 }
202
203 void node_keymap(struct wmKeyConfig *keyconf)
204 {
205         wmKeyMap *keymap;
206         wmKeyMapItem *kmi;
207         
208         /* Entire Editor only ----------------- */
209         keymap = WM_keymap_find(keyconf, "Node Generic", SPACE_NODE, 0);
210         
211         WM_keymap_add_item(keymap, "NODE_OT_properties", NKEY, KM_PRESS, 0, 0);
212         WM_keymap_add_item(keymap, "NODE_OT_toolbar", TKEY, KM_PRESS, 0, 0);
213         
214         /* Main Area only ----------------- */
215         keymap = WM_keymap_find(keyconf, "Node Editor", SPACE_NODE, 0);
216         
217         /* mouse select in nodes used to be both keys, but perhaps this should be reduced? 
218          * NOTE: mouse-clicks on left-mouse will fall through to allow transform-tweak, but also link/resize
219          * NOTE 2: socket select is part of the node select operator, to handle overlapping cases
220          * NOTE 3: select op is registered for various combinations of modifier key, so the specialized
221          *         grab operators (unlink, attach, etc.) can work easily on single nodes.
222          */
223         node_select_keymap(keymap, FALSE);
224         node_select_keymap(keymap, TRUE);
225         
226         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", EVT_TWEAK_S, KM_ANY, 0, 0);
227         RNA_boolean_set(kmi->ptr, "tweak", TRUE);
228         
229         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
230         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
231         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
232         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
233
234         /* each of these falls through if not handled... */
235         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, 0, 0);
236         RNA_boolean_set(kmi->ptr, "detach", FALSE);
237         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
238         RNA_boolean_set(kmi->ptr, "detach", TRUE);
239         
240         WM_keymap_add_item(keymap, "NODE_OT_resize", LEFTMOUSE, KM_PRESS, 0, 0);
241         
242         WM_keymap_add_item(keymap, "NODE_OT_add_reroute", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
243         WM_keymap_add_item(keymap, "NODE_OT_links_cut", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
244         WM_keymap_add_item(keymap, "NODE_OT_select_link_viewer", LEFTMOUSE, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
245         
246         WM_keymap_add_item(keymap, "NODE_OT_backimage_move", MIDDLEMOUSE, KM_PRESS, KM_ALT, 0);
247         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, 0, 0);
248         RNA_float_set(kmi->ptr, "factor", 0.83333f);
249         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, KM_ALT, 0);
250         RNA_float_set(kmi->ptr, "factor", 1.2f);
251         WM_keymap_add_item(keymap, "NODE_OT_backimage_sample", ACTIONMOUSE, KM_PRESS, KM_ALT, 0);
252
253         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, 0, 0);
254         RNA_boolean_set(kmi->ptr, "replace", FALSE);
255         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, KM_SHIFT, 0);
256         RNA_boolean_set(kmi->ptr, "replace", TRUE);
257
258         WM_keymap_add_menu(keymap, "NODE_MT_add", AKEY, KM_PRESS, KM_SHIFT, 0);
259         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
260         /* modified operator call for duplicating with input links */
261         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move_keep_inputs", DKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
262         
263         WM_keymap_add_item(keymap, "NODE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
264         WM_keymap_add_item(keymap, "NODE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
265         WM_keymap_add_item(keymap, "NODE_OT_join", JKEY, KM_PRESS, KM_CTRL, 0);
266         
267         WM_keymap_add_item(keymap, "NODE_OT_hide_toggle", HKEY, KM_PRESS, 0, 0);
268         WM_keymap_add_item(keymap, "NODE_OT_mute_toggle", MKEY, KM_PRESS, 0, 0);
269         WM_keymap_add_item(keymap, "NODE_OT_preview_toggle", HKEY, KM_PRESS, KM_SHIFT, 0);
270         WM_keymap_add_item(keymap, "NODE_OT_hide_socket_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
271         
272         WM_keymap_add_item(keymap, "NODE_OT_show_cyclic_dependencies", CKEY, KM_PRESS, 0, 0);
273         
274         WM_keymap_add_item(keymap, "NODE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
275         WM_keymap_add_item(keymap, "NODE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
276
277         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", BKEY, KM_PRESS, 0, 0);
278         RNA_boolean_set(kmi->ptr, "tweak", FALSE);
279
280         WM_keymap_add_item(keymap, "NODE_OT_delete", XKEY, KM_PRESS, 0, 0);
281         WM_keymap_add_item(keymap, "NODE_OT_delete", DELKEY, KM_PRESS, 0, 0);
282         WM_keymap_add_item(keymap, "NODE_OT_delete_reconnect", XKEY, KM_PRESS, KM_CTRL, 0);
283
284         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", AKEY, KM_PRESS, 0, 0);
285         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
286         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
287         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
288
289         WM_keymap_add_item(keymap, "NODE_OT_select_linked_to", LKEY, KM_PRESS, KM_SHIFT, 0);
290         WM_keymap_add_item(keymap, "NODE_OT_select_linked_from", LKEY, KM_PRESS, 0, 0);
291         WM_keymap_add_item(keymap, "NODE_OT_select_same_type", GKEY, KM_PRESS, KM_SHIFT, 0);
292
293         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_same_type_step", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
294         RNA_boolean_set(kmi->ptr, "prev", FALSE);
295         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_same_type_step", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
296         RNA_boolean_set(kmi->ptr, "prev", TRUE);
297         
298         WM_keymap_add_item(keymap, "NODE_OT_find_node", FKEY, KM_PRESS, KM_CTRL, 0);
299         
300         /* node group operators */
301         WM_keymap_add_item(keymap, "NODE_OT_group_make", GKEY, KM_PRESS, KM_CTRL, 0);
302         WM_keymap_add_item(keymap, "NODE_OT_group_ungroup", GKEY, KM_PRESS, KM_ALT, 0);
303         WM_keymap_add_item(keymap, "NODE_OT_group_separate", PKEY, KM_PRESS, 0, 0);
304         kmi = WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, 0, 0);
305         RNA_boolean_set(kmi->ptr, "exit", FALSE);
306         kmi = WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, KM_SHIFT, 0);
307         RNA_boolean_set(kmi->ptr, "exit", TRUE);
308
309         WM_keymap_add_item(keymap, "NODE_OT_read_renderlayers", RKEY, KM_PRESS, KM_CTRL, 0);
310         WM_keymap_add_item(keymap, "NODE_OT_read_fullsamplelayers", RKEY, KM_PRESS, KM_SHIFT, 0);
311         WM_keymap_add_item(keymap, "NODE_OT_render_changed", ZKEY, KM_PRESS, 0, 0);
312         
313         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_CTRL, 0);
314         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
315         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_CTRL, 0);
316         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
317         
318         WM_keymap_add_item(keymap, "NODE_OT_viewer_border", BKEY, KM_PRESS, KM_CTRL, 0);
319
320         transform_keymap_for_space(keyconf, keymap, SPACE_NODE);
321 }