2.5: merge with trunk, previous merge was only up to yesterday.
[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_windowmanager_types.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37
38 #include "BKE_blender.h"
39 #include "BKE_global.h"
40 #include "BKE_library.h"
41 #include "BKE_main.h"
42 #include "BKE_idprop.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46 #include "wm_window.h"
47 #include "wm_event_system.h"
48
49 static ListBase global_ops= {NULL, NULL};
50
51 /* ************ operator API, exported ********** */
52
53 wmOperatorType *WM_operatortype_find(const char *idname)
54 {
55         wmOperatorType *ot;
56         
57         for(ot= global_ops.first; ot; ot= ot->next) {
58                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
59                    return ot;
60         }
61         return NULL;
62 }
63
64 /* all ops in 1 list (for time being... needs evaluation later) */
65 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
66 {
67         wmOperatorType *ot;
68         
69         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
70         opfunc(ot);
71         BLI_addtail(&global_ops, ot);
72 }
73
74 /* ************ default ops, exported *********** */
75
76 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
77 {
78 //      if(okee(op->type->name)) {
79 //              return op->type->exec(C, op);
80 //      }
81         return 0;
82 }
83 int WM_operator_winactive(bContext *C)
84 {
85         if(C->window==NULL) return 0;
86         return 1;
87 }
88
89 /* ************ window / screen operator definitions ************** */
90
91 static void WM_OT_window_duplicate(wmOperatorType *ot)
92 {
93         ot->name= "Duplicate Window";
94         ot->idname= "WM_OT_window_duplicate";
95         
96         ot->invoke= NULL; //WM_operator_confirm;
97         ot->exec= wm_window_duplicate_op;
98         ot->poll= WM_operator_winactive;
99 }
100
101 static void WM_OT_window_rip(wmOperatorType *ot)
102 {
103         ot->name= "Rip Area into New Window";
104         ot->idname= "WM_OT_window_rip";
105         
106         ot->invoke= wm_window_rip_op; //WM_operator_confirm;
107         ot->exec= NULL;
108         ot->poll= WM_operator_winactive;
109 }
110
111 static void WM_OT_save_homefile(wmOperatorType *ot)
112 {
113         ot->name= "Save User Settings";
114         ot->idname= "WM_OT_save_homefile";
115         
116         ot->invoke= NULL; //WM_operator_confirm;
117         ot->exec= WM_write_homefile;
118         ot->poll= WM_operator_winactive;
119         
120         ot->flag= OPTYPE_REGISTER;
121 }
122
123 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
124 {
125     ot->name= "Toggle Fullscreen";
126     ot->idname= "WM_OT_window_fullscreen_toggle";
127
128     ot->invoke= NULL;
129     ot->exec= wm_window_fullscreen_toggle_op;
130     ot->poll= WM_operator_winactive;
131 }
132
133 static void WM_OT_exit_blender(wmOperatorType *ot)
134 {
135         ot->name= "Exit Blender";
136         ot->idname= "WM_OT_exit_blender";
137
138         ot->invoke= NULL; /* do confirm stuff */
139         ot->exec= wm_exit_blender_op;
140         ot->poll= WM_operator_winactive;
141 }
142
143 /* ************ window / screen border operator definitions ************** */
144 /*
145  * This is and example of global operator working with
146  * the gesture system.
147  */
148 static int border_select_init(bContext *C, wmOperator *op)
149 {
150         int x, y;
151
152         if(!(OP_get_int(op, "start_x", &x) && OP_get_int(op, "start_y", &y)))
153                 return 0;
154
155         WM_gesture_init(C, GESTURE_RECT);
156         return 1;
157 }
158
159 static int border_select_apply(bContext *C, wmOperator *op)
160 {
161         wmGestureRect rect;
162         int x, y, endx, endy;
163
164         OP_get_int(op, "start_x", &x);
165         OP_get_int(op, "start_y", &y);
166         OP_get_int(op, "end_x", &endx);
167         OP_get_int(op, "end_y", &endy);
168
169         rect.gesture.next= rect.gesture.prev= NULL;
170         rect.gesture.type= GESTURE_RECT;
171         rect.x1= x;
172         rect.y1= y;
173         rect.x2= endx;
174         rect.y2= endy;
175         WM_gesture_update(C, (wmGesture *) &rect);
176         WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_GESTURE_CHANGED, GESTURE_RECT, NULL);
177
178         return 1;
179 }
180
181 static int border_select_exit(bContext *C, wmOperator *op)
182 {
183         WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0, NULL);
184         OP_free_property(op);
185         return 1;
186 }
187
188 static int border_select_exec(bContext *C, wmOperator *op)
189 {
190         if(!border_select_init(C, op))
191                 return OPERATOR_CANCELLED;
192         
193         border_select_apply(C, op);
194         border_select_exit(C, op);
195
196         return OPERATOR_FINISHED;
197 }
198
199 static int border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
200 {
201         /* operator arguments and storage. */
202         OP_verify_int(op, "start_x", event->x, NULL);
203         OP_verify_int(op, "start_y", event->y, NULL);
204
205         if(!border_select_init(C, op))
206                 return OPERATOR_CANCELLED;
207
208         /* add temp handler */
209         WM_event_add_modal_handler(&C->window->handlers, op);
210         return OPERATOR_RUNNING_MODAL;
211 }
212
213 static int border_select_cancel(bContext *C, wmOperator *op)
214 {
215         WM_event_remove_modal_handler(&C->window->handlers, op);
216         border_select_exit(C, op);
217         return OPERATOR_CANCELLED;
218 }
219
220 static int border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
221 {
222         switch(event->type) {
223                 case MOUSEMOVE:
224                         OP_set_int(op, "end_x", event->x);
225                         OP_set_int(op, "end_y", event->y);
226                         border_select_apply(C, op);
227                         WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_GESTURE_CHANGED, GESTURE_RECT, NULL);
228                         WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_SCREEN_CHANGED, 0, NULL);
229                         break;
230                 case LEFTMOUSE:
231                         if(event->val==0) {
232                                 border_select_apply(C, op);
233                                 WM_gesture_end(C, GESTURE_RECT);
234                                 border_select_exit(C, op);
235                                 WM_event_remove_modal_handler(&C->window->handlers, op);
236                                 return OPERATOR_FINISHED;
237                         }
238                         break;
239                 case ESCKEY:
240                         return border_select_cancel(C, op);
241         }
242         return OPERATOR_RUNNING_MODAL;
243 }
244
245 void WM_OT_border_select(wmOperatorType *ot)
246 {
247         /* identifiers */
248         ot->name= "Border select";
249         ot->idname= "WM_OT_border_select";
250
251         ot->exec= border_select_exec;
252         ot->invoke= border_select_invoke;
253         ot->cancel= border_select_cancel;
254         ot->modal= border_select_modal;
255
256         ot->poll= WM_operator_winactive;
257 }
258  
259 /* called on initialize WM_exit() */
260 void wm_operatortype_free(void)
261 {
262         BLI_freelistN(&global_ops);
263 }
264
265 /* called on initialize WM_init() */
266 void wm_operatortype_init(void)
267 {
268         WM_operatortype_append(WM_OT_window_duplicate);
269         WM_operatortype_append(WM_OT_window_rip);
270         WM_operatortype_append(WM_OT_save_homefile);
271         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
272         WM_operatortype_append(WM_OT_exit_blender);
273         WM_operatortype_append(WM_OT_border_select);
274 }
275
276 /* wrapped to get property from a operator. */
277 IDProperty *op_get_property(wmOperator *op, char *name)
278 {
279         IDProperty *prop;
280         
281         if(!op->properties)
282                 return NULL;
283
284         prop= IDP_GetPropertyFromGroup(op->properties, name);
285         return prop;
286 }
287
288 /*
289  * We need create a "group" to store the operator properties.
290  * We don't have a WM_operator_new or some thing like that,
291  * so this function is called by all the OP_set_* function
292  * in case that op->properties is equal to NULL.
293  */
294 void op_init_property(wmOperator *op)
295 {
296         IDPropertyTemplate val;
297         val.i = 0; /* silence MSVC warning about uninitialized var when debugging */
298         op->properties= IDP_New(IDP_GROUP, val, "property");
299 }
300
301 /* ***** Property API, exported ***** */
302 void OP_free_property(wmOperator *op)
303 {
304         if(op->properties) {
305         IDP_FreeProperty(op->properties);
306         /*
307          * This need change, when the idprop code only
308          * need call IDP_FreeProperty. (check BKE_idprop.h)
309          */
310         MEM_freeN(op->properties);
311         op->properties= NULL;
312         }
313 }
314
315 void OP_set_int(wmOperator *op, char *name, int value)
316 {
317         IDPropertyTemplate val;
318         IDProperty *prop;
319
320         if(!op->properties)
321                 op_init_property(op);
322
323         val.i= value;
324         prop= IDP_New(IDP_INT, val, name);
325         IDP_ReplaceInGroup(op->properties, prop);
326 }
327
328 void OP_set_float(wmOperator *op, char *name, float value)
329 {
330         IDPropertyTemplate val;
331         IDProperty *prop;
332
333         if(!op->properties)
334                 op_init_property(op);
335
336         val.f= value;
337         prop= IDP_New(IDP_FLOAT, val, name);
338         IDP_ReplaceInGroup(op->properties, prop);
339 }
340
341 void OP_set_int_array(wmOperator *op, char *name, int *array, short len)
342 {
343         IDPropertyTemplate val;
344         IDProperty *prop;
345         short i;
346         int *pointer;
347
348         if(!op->properties)
349                 op_init_property(op);
350
351         val.array.len= len;
352         val.array.type= IDP_INT;
353         prop= IDP_New(IDP_ARRAY, val, name);
354
355         pointer= (int *)prop->data.pointer;
356         for(i= 0; i < len; i++)
357                 pointer[i]= array[i];
358         IDP_ReplaceInGroup(op->properties, prop);
359 }
360
361 void OP_set_float_array(wmOperator *op, char *name, float *array, short len)
362 {
363         IDPropertyTemplate val;
364         IDProperty *prop;
365         short i;
366         float *pointer;
367
368         if(!op->properties)
369                 op_init_property(op);
370
371         val.array.len= len;
372         val.array.type= IDP_FLOAT;
373         prop= IDP_New(IDP_ARRAY, val, name);
374
375         pointer= (float *) prop->data.pointer;
376         for(i= 0; i < len; i++)
377                 pointer[i]= array[i];
378         IDP_ReplaceInGroup(op->properties, prop);
379 }
380
381 void OP_set_string(wmOperator *op, char *name, char *str)
382 {
383         IDPropertyTemplate val;
384         IDProperty *prop;
385
386         if(!op->properties)
387                 op_init_property(op);
388
389         val.str= str;
390         prop= IDP_New(IDP_STRING, val, name);
391         IDP_ReplaceInGroup(op->properties, prop);
392 }
393
394 int OP_get_int(wmOperator *op, char *name, int *value)
395 {
396         IDProperty *prop= op_get_property(op, name);
397         int status= 0;
398
399         if ((prop) && (prop->type == IDP_INT)) {
400                 (*value)= prop->data.val;
401                 status= 1;
402         }
403         return (status);
404 }
405
406 int OP_get_float(wmOperator *op, char *name, float *value)
407 {
408         IDProperty *prop= op_get_property(op, name);
409         int status= 0;
410
411         if ((prop) && (prop->type == IDP_FLOAT)) {
412                 (*value)= *(float*)&prop->data.val;
413                 status= 1;
414         }
415         return (status);
416 }
417
418 int OP_get_int_array(wmOperator *op, char *name, int *array, short *len)
419 {
420         IDProperty *prop= op_get_property(op, name);
421         short i;
422         int status= 0;
423         int *pointer;
424
425         if ((prop) && (prop->type == IDP_ARRAY)) {
426                 pointer= (int *) prop->data.pointer;
427
428                 for(i= 0; (i < prop->len) && (i < *len); i++)
429                         array[i]= pointer[i];
430
431                 (*len)= i;
432                 status= 1;
433         }
434         return (status);
435 }
436
437 int OP_get_float_array(wmOperator *op, char *name, float *array, short *len)
438 {
439         IDProperty *prop= op_get_property(op, name);
440         short i;
441         float *pointer;
442         int status= 0;
443
444         if ((prop) && (prop->type == IDP_ARRAY)) {
445                 pointer= (float *) prop->data.pointer;
446
447                 for(i= 0; (i < prop->len) && (i < *len); i++)
448                         array[i]= pointer[i];
449
450                 (*len)= i;
451                 status= 1;
452         }
453         return (status);
454 }
455
456 char *OP_get_string(wmOperator *op, char *name)
457 {
458         IDProperty *prop= op_get_property(op, name);
459         if ((prop) && (prop->type == IDP_STRING))
460                 return ((char *) prop->data.pointer);
461         return (NULL);
462 }
463
464 void OP_verify_int(wmOperator *op, char *name, int value, int *result)
465 {
466         int rvalue;
467
468         if(OP_get_int(op, name, &rvalue))
469                 value= rvalue;
470         else
471                 OP_set_int(op, name, value);
472
473         if(result)
474                 *result= value;
475 }
476
477 void OP_verify_float(wmOperator *op, char *name, float value, int *result)
478 {
479         float rvalue;
480
481         if(OP_get_float(op, name, &rvalue))
482                 value= rvalue;
483         else
484                 OP_set_float(op, name, value);
485         
486         if(result)
487                 *result= value;
488 }
489
490 char *OP_verify_string(wmOperator *op, char *name, char *str)
491 {
492         char *result= OP_get_string(op, name);
493
494         if(!result) {
495                 OP_set_string(op, name, str);
496                 result= OP_get_string(op, name);
497         }
498
499         return result;
500 }
501
502 void OP_verify_int_array(wmOperator *op, char *name, int *array, short len, int *resultarray, short *resultlen)
503 {
504         int rarray[1];
505         short rlen= 1;
506
507         if(resultarray && resultlen) {
508                 if(!OP_get_int_array(op, name, resultarray, &rlen)) {
509                         OP_set_int_array(op, name, array, len);
510                         OP_get_int_array(op, name, resultarray, resultlen);
511                 }
512         }
513         else {
514                 if(!OP_get_int_array(op, name, rarray, &rlen))
515                         OP_set_int_array(op, name, array, len);
516         }
517 }
518
519 void OP_verify_float_array(wmOperator *op, char *name, float *array, short len, float *resultarray, short *resultlen)
520 {
521         float rarray[1];
522         short rlen= 1;
523
524         if(resultarray && resultlen) {
525                 if(!OP_get_float_array(op, name, resultarray, &rlen)) {
526                         OP_set_float_array(op, name, array, len);
527                         OP_get_float_array(op, name, resultarray, resultlen);
528                 }
529         }
530         else {
531                 if(!OP_get_float_array(op, name, rarray, &rlen))
532                         OP_set_float_array(op, name, array, len);
533         }
534 }
535