wip operator py-api
[blender.git] / source / blender / windowmanager / intern / wm_operators.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30
31 #include "DNA_ID.h"
32 #include "DNA_screen_types.h"
33 #include "DNA_userdef_types.h"
34 #include "DNA_windowmanager_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "BKE_blender.h"
41 #include "BKE_context.h"
42 #include "BKE_idprop.h"
43 #include "BKE_library.h"
44 #include "BKE_global.h"
45 #include "BKE_main.h"
46 #include "BKE_utildefines.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "UI_interface.h"
52 #include "UI_resources.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "wm.h"
58 #include "wm_window.h"
59 #include "wm_subwindow.h"
60 #include "wm_event_system.h"
61
62 #include "ED_screen.h"
63
64 static ListBase global_ops= {NULL, NULL};
65
66 /* ************ operator API, exported ********** */
67
68 wmOperatorType *WM_operatortype_find(const char *idname)
69 {
70         wmOperatorType *ot;
71         
72         for(ot= global_ops.first; ot; ot= ot->next) {
73                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
74                    return ot;
75         }
76         printf("search for unknown operator %s\n", idname);
77         return NULL;
78 }
79
80 wmOperatorType *WM_operatortype_first(void)
81 {
82         return global_ops.first;
83 }
84
85 /* all ops in 1 list (for time being... needs evaluation later) */
86 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
87 {
88         wmOperatorType *ot;
89         
90         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
91         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
92         opfunc(ot);
93         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
94         RNA_def_struct_identifier(ot->srna, ot->idname);
95         BLI_addtail(&global_ops, ot);
96 }
97
98 /* ************ default op callbacks, exported *********** */
99
100 static void operator_callback(bContext *C, void *arg, int retval)
101 {
102         wmOperator *op= arg;
103         
104         if(op && retval > 0)
105                 op->type->exec(C, op);
106 }
107
108 void WM_error(bContext *C, char *str)
109 {
110         char buf[148], testbuf[128];
111         
112         BLI_strncpy(testbuf, str, 128);
113         sprintf(buf, "Error %%i%d%%t|%s", ICON_ERROR, testbuf);
114         uiPupmenu(C, 0, operator_callback, NULL, buf);
115         
116 }
117
118 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
119 {
120         char buf[512];
121         
122         sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
123         uiPupmenu(C, 0, operator_callback, op, buf);
124         
125         return 1;
126 }
127
128 int WM_operator_winactive(bContext *C)
129 {
130         if(CTX_wm_window(C)==NULL) return 0;
131         return 1;
132 }
133
134 /* ************ window / screen operator definitions ************** */
135
136 static void WM_OT_window_duplicate(wmOperatorType *ot)
137 {
138         ot->name= "Duplicate Window";
139         ot->idname= "WM_OT_window_duplicate";
140         
141         ot->invoke= WM_operator_confirm;
142         ot->exec= wm_window_duplicate_op;
143         ot->poll= WM_operator_winactive;
144 }
145
146 static void WM_OT_save_homefile(wmOperatorType *ot)
147 {
148         ot->name= "Save User Settings";
149         ot->idname= "WM_OT_save_homefile";
150         
151         ot->invoke= WM_operator_confirm;
152         ot->exec= WM_write_homefile;
153         ot->poll= WM_operator_winactive;
154         
155         ot->flag= OPTYPE_REGISTER;
156 }
157
158 /* ********* recent file *********** */
159
160 static void recent_filelist(char *pup)
161 {
162         struct RecentFile *recent;
163         int i, ofs= 0;
164         char *p;
165         
166         p= pup + sprintf(pup, "Open Recent%%t");
167         
168         if (G.sce[0]) {
169                 p+= sprintf(p, "|%s %%x%d", G.sce, 1);
170                 ofs = 1;
171         }
172         
173         for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
174                 if (strcmp(recent->filename, G.sce)) {
175                         p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
176                 }
177         }
178 }
179
180 static void recentfile_callback(bContext *C, void *arg, int event)
181 {
182         
183         if(event>0) {
184                 if (G.sce[0] && (event==1))
185                         WM_read_file(C, G.sce);
186                 else {
187                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
188                         if(recent) {
189                                 WM_read_file(C, recent->filename);
190                         }
191                 }
192         }
193 }
194
195 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
196 {
197         char pup[2048];
198         
199         recent_filelist(pup);
200         uiPupmenu(C, 0, recentfile_callback, op, pup);
201         
202         return 1;
203 }
204
205 static void WM_OT_open_recentfile(wmOperatorType *ot)
206 {
207         ot->name= "Open Recent File";
208         ot->idname= "WM_OT_open_recentfile";
209         
210         ot->invoke= wm_recentfile_invoke;
211         ot->poll= WM_operator_winactive;
212         
213         ot->flag= OPTYPE_REGISTER;
214 }
215
216 /* *********************** */
217
218 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
219 {
220     ot->name= "Toggle Fullscreen";
221     ot->idname= "WM_OT_window_fullscreen_toggle";
222
223     ot->invoke= WM_operator_confirm;
224     ot->exec= wm_window_fullscreen_toggle_op;
225     ot->poll= WM_operator_winactive;
226 }
227
228 static void WM_OT_exit_blender(wmOperatorType *ot)
229 {
230         ot->name= "Exit Blender";
231         ot->idname= "WM_OT_exit_blender";
232
233         ot->invoke= WM_operator_confirm;
234         ot->exec= wm_exit_blender_op;
235         ot->poll= WM_operator_winactive;
236 }
237
238 /* ************ window gesture operator-callback definitions ************** */
239 /*
240  * These are default callbacks for use in operators requiring gesture input
241  */
242
243 /* **************** Border gesture *************** */
244
245 /* Border gesture has two types:
246    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
247    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
248
249    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
250 */
251
252 static void border_select_apply(bContext *C, wmOperator *op, int event_type)
253 {
254         wmGesture *gesture= op->customdata;
255         rcti *rect= gesture->customdata;
256         
257         if(rect->xmin > rect->xmax)
258                 SWAP(int, rect->xmin, rect->xmax);
259         if(rect->ymin > rect->ymax)
260                 SWAP(int, rect->ymin, rect->ymax);
261         
262         /* operator arguments and storage. */
263         RNA_int_set(op->ptr, "xmin", rect->xmin);
264         RNA_int_set(op->ptr, "ymin", rect->ymin);
265         RNA_int_set(op->ptr, "xmax", rect->xmax);
266         RNA_int_set(op->ptr, "ymax", rect->ymax);
267         
268         RNA_int_set(op->ptr, "event_type", event_type);
269         
270         op->type->exec(C, op);
271 }
272
273 static void border_select_end(bContext *C, wmOperator *op)
274 {
275         wmGesture *gesture= op->customdata;
276         
277         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
278         op->customdata= NULL;
279
280         ED_area_tag_redraw(CTX_wm_area(C));
281         
282 }
283
284 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
285 {
286         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
287
288         /* add modal handler */
289         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
290         
291         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
292
293         return OPERATOR_RUNNING_MODAL;
294 }
295
296 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
297 {
298         wmGesture *gesture= op->customdata;
299         rcti *rect= gesture->customdata;
300         int sx, sy;
301         
302         switch(event->type) {
303                 case MOUSEMOVE:
304                         
305                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
306                         
307                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
308                                 rect->xmin= rect->xmax= event->x - sx;
309                                 rect->ymin= rect->ymax= event->y - sy;
310                         }
311                         else {
312                                 rect->xmax= event->x - sx;
313                                 rect->ymax= event->y - sy;
314                         }
315                         
316                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
317
318                         break;
319                         
320                 case LEFTMOUSE:
321                 case MIDDLEMOUSE:
322                 case RIGHTMOUSE:
323                         if(event->val==1) {
324                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
325                                         gesture->mode= 1;
326                                         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
327                                 }
328                         }
329                         else {
330                                 border_select_apply(C, op, event->type);
331                                 border_select_end(C, op);
332                                 return OPERATOR_FINISHED;
333                         }
334                         break;
335                 case ESCKEY:
336                         border_select_end(C, op);
337                         return OPERATOR_CANCELLED;
338         }
339         return OPERATOR_RUNNING_MODAL;
340 }
341
342 /* **************** Tweak gesture *************** */
343
344 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
345 {
346         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
347         
348         /* add modal handler */
349         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
350         
351         WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
352         
353         return OPERATOR_RUNNING_MODAL;
354 }
355
356 static void tweak_gesture_end(bContext *C, wmOperator *op)
357 {
358         wmGesture *gesture= op->customdata;
359         
360         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
361         op->customdata= NULL;
362
363         ED_area_tag_redraw(CTX_wm_area(C));
364         
365 }
366
367 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
368 {
369         wmWindow *window= CTX_wm_window(C);
370         wmGesture *gesture= op->customdata;
371         rcti *rect= gesture->customdata;
372         int sx, sy, val;
373         
374         switch(event->type) {
375                 case MOUSEMOVE:
376                         
377                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
378                         
379                         rect->xmax= event->x - sx;
380                         rect->ymax= event->y - sy;
381                         
382                         if((val= wm_gesture_evaluate(C, gesture))) {
383                                 wmEvent event;
384                                         
385                                 event= *(window->eventstate);
386                                 if(gesture->event_type==LEFTMOUSE)
387                                         event.type= EVT_TWEAK_L;
388                                 else if(gesture->event_type==RIGHTMOUSE)
389                                         event.type= EVT_TWEAK_R;
390                                 else
391                                         event.type= EVT_TWEAK_M;
392                                 event.val= val;
393                                 /* mouse coords! */
394                                 wm_event_add(window, &event);
395                                 
396                                 tweak_gesture_end(C, op);
397                                 return OPERATOR_FINISHED;
398                         }
399                         else
400                                 WM_event_add_notifier(C, WM_NOTE_GESTURE_REDRAW, 0, NULL);
401                         
402                         break;
403                         
404                 case LEFTMOUSE:
405                 case RIGHTMOUSE:
406                 case MIDDLEMOUSE:
407                         if(gesture->event_type==event->type) {
408                                 wm_gesture_evaluate(C, gesture);
409                                 tweak_gesture_end(C, op);
410                                 return OPERATOR_FINISHED;
411                         }
412                         break;
413         }
414         return OPERATOR_RUNNING_MODAL;
415 }
416
417 void WM_OT_tweak_gesture(wmOperatorType *ot)
418 {
419         ot->name= "Tweak Gesture";
420         ot->idname= "WM_OT_tweak_gesture";
421         
422         ot->invoke= tweak_gesture_invoke;
423         ot->modal= tweak_gesture_modal;
424
425         ot->poll= WM_operator_winactive;
426 }
427
428
429 /* ******************************************************* */
430  
431 /* called on initialize WM_exit() */
432 void wm_operatortype_free(void)
433 {
434         BLI_freelistN(&global_ops);
435 }
436
437 /* called on initialize WM_init() */
438 void wm_operatortype_init(void)
439 {
440         WM_operatortype_append(WM_OT_window_duplicate);
441         WM_operatortype_append(WM_OT_save_homefile);
442         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
443         WM_operatortype_append(WM_OT_exit_blender);
444         WM_operatortype_append(WM_OT_tweak_gesture);
445         WM_operatortype_append(WM_OT_open_recentfile);
446 }
447
448 /* default keymap for windows and screens, only call once per WM */
449 void wm_window_keymap(wmWindowManager *wm)
450 {
451         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
452         
453         /* note, this doesn't replace existing keymap items */
454         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
455         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
456         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
457         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
458         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
459 }
460