fix [#30063] Weight Paint + Pose Mode: [m] key does not toggle Face Selection Masking
[blender.git] / source / blender / editors / screen / screen_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  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/screen/screen_ops.c
26  *  \ingroup edscr
27  */
28
29
30 #include <math.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_math.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dlrbTree.h"
38 #include "BLI_utildefines.h"
39
40 #include "BLF_translation.h"
41
42 #include "DNA_armature_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_mask_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_context.h"
53 #include "BKE_customdata.h"
54 #include "BKE_global.h"
55 #include "BKE_main.h"
56 #include "BKE_mesh.h"
57 #include "BKE_object.h"
58 #include "BKE_report.h"
59 #include "BKE_scene.h"
60 #include "BKE_screen.h"
61 #include "BKE_tessmesh.h"
62 #include "BKE_sound.h"
63 #include "BKE_mask.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "ED_armature.h"
69 #include "ED_clip.h"
70 #include "ED_image.h"
71 #include "ED_keyframes_draw.h"
72 #include "ED_object.h"
73 #include "ED_screen.h"
74 #include "ED_screen_types.h"
75 #include "ED_sequencer.h"
76 #include "ED_util.h"
77 #include "ED_view3d.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81
82 #include "UI_interface.h"
83 #include "UI_resources.h"
84
85 #include "wm_window.h"
86
87 #include "screen_intern.h"  /* own module include */
88
89 #define KM_MODAL_CANCEL     1
90 #define KM_MODAL_APPLY      2
91 #define KM_MODAL_STEP10     3
92 #define KM_MODAL_STEP10_OFF 4
93
94 /* ************** Exported Poll tests ********************** */
95
96 int ED_operator_regionactive(bContext *C)
97 {
98         if (CTX_wm_window(C) == NULL) return 0;
99         if (CTX_wm_screen(C) == NULL) return 0;
100         if (CTX_wm_region(C) == NULL) return 0;
101         return 1;
102 }
103
104 int ED_operator_areaactive(bContext *C)
105 {
106         if (CTX_wm_window(C) == NULL) return 0;
107         if (CTX_wm_screen(C) == NULL) return 0;
108         if (CTX_wm_area(C) == NULL) return 0;
109         return 1;
110 }
111
112 int ED_operator_screenactive(bContext *C)
113 {
114         if (CTX_wm_window(C) == NULL) return 0;
115         if (CTX_wm_screen(C) == NULL) return 0;
116         return 1;
117 }
118
119 /* XXX added this to prevent anim state to change during renders */
120 static int ED_operator_screenactive_norender(bContext *C)
121 {
122         if (G.is_rendering) return 0;
123         if (CTX_wm_window(C) == NULL) return 0;
124         if (CTX_wm_screen(C) == NULL) return 0;
125         return 1;
126 }
127
128
129 static int screen_active_editable(bContext *C)
130 {
131         if (ED_operator_screenactive(C)) {
132                 /* no full window splitting allowed */
133                 if (CTX_wm_screen(C)->full != SCREENNORMAL)
134                         return 0;
135                 return 1;
136         }
137         return 0;
138 }
139
140 /* when mouse is over area-edge */
141 int ED_operator_screen_mainwinactive(bContext *C)
142 {
143         if (CTX_wm_window(C) == NULL) return 0;
144         if (CTX_wm_screen(C) == NULL) return 0;
145         if (CTX_wm_screen(C)->subwinactive != CTX_wm_screen(C)->mainwin) return 0;
146         return 1;
147 }
148
149 int ED_operator_scene_editable(bContext *C)
150 {
151         Scene *scene = CTX_data_scene(C);
152         if (scene && scene->id.lib == NULL)
153                 return 1;
154         return 0;
155 }
156
157 int ED_operator_objectmode(bContext *C)
158 {
159         Scene *scene = CTX_data_scene(C);
160         Object *obact = CTX_data_active_object(C);
161
162         if (scene == NULL || scene->id.lib)
163                 return 0;
164         if (CTX_data_edit_object(C) )
165                 return 0;
166         
167         /* add a check for ob->mode too? */
168         if (obact && obact->mode)
169                 return 0;
170         
171         return 1;
172 }
173
174
175 static int ed_spacetype_test(bContext *C, int type)
176 {
177         if (ED_operator_areaactive(C)) {
178                 SpaceLink *sl = (SpaceLink *)CTX_wm_space_data(C);
179                 return sl && (sl->spacetype == type);
180         }
181         return 0;
182 }
183
184 int ED_operator_view3d_active(bContext *C)
185 {
186         return ed_spacetype_test(C, SPACE_VIEW3D);
187 }
188
189 int ED_operator_region_view3d_active(bContext *C)
190 {
191         if (CTX_wm_region_view3d(C))
192                 return TRUE;
193
194         CTX_wm_operator_poll_msg_set(C, "expected a view3d region");
195         return FALSE;   
196 }
197
198 /* generic for any view2d which uses anim_ops */
199 int ED_operator_animview_active(bContext *C)
200 {
201         if (ED_operator_areaactive(C)) {
202                 SpaceLink *sl = (SpaceLink *)CTX_wm_space_data(C);
203                 if (sl && (ELEM5(sl->spacetype, SPACE_SEQ, SPACE_ACTION, SPACE_NLA, SPACE_IPO, SPACE_TIME)))
204                         return TRUE;
205         }
206
207         CTX_wm_operator_poll_msg_set(C, "expected an timeline/animation area to be active");
208         return 0;
209 }
210
211 int ED_operator_timeline_active(bContext *C)
212 {
213         return ed_spacetype_test(C, SPACE_TIME);
214 }
215
216 int ED_operator_outliner_active(bContext *C)
217 {
218         return ed_spacetype_test(C, SPACE_OUTLINER);
219 }
220
221 int ED_operator_outliner_active_no_editobject(bContext *C)
222 {
223         if (ed_spacetype_test(C, SPACE_OUTLINER)) {
224                 Object *ob = ED_object_active_context(C);
225                 Object *obedit = CTX_data_edit_object(C);
226                 if (ob && ob == obedit)
227                         return 0;
228                 else
229                         return 1;
230         }
231         return 0;
232 }
233
234 int ED_operator_file_active(bContext *C)
235 {
236         return ed_spacetype_test(C, SPACE_FILE);
237 }
238
239 int ED_operator_action_active(bContext *C)
240 {
241         return ed_spacetype_test(C, SPACE_ACTION);
242 }
243
244 int ED_operator_buttons_active(bContext *C)
245 {
246         return ed_spacetype_test(C, SPACE_BUTS);
247 }
248
249 int ED_operator_node_active(bContext *C)
250 {
251         SpaceNode *snode = CTX_wm_space_node(C);
252         
253         if (snode && snode->edittree)
254                 return 1;
255         
256         return 0;
257 }
258
259 // XXX rename
260 int ED_operator_graphedit_active(bContext *C)
261 {
262         return ed_spacetype_test(C, SPACE_IPO);
263 }
264
265 int ED_operator_sequencer_active(bContext *C)
266 {
267         return ed_spacetype_test(C, SPACE_SEQ);
268 }
269
270 int ED_operator_image_active(bContext *C)
271 {
272         return ed_spacetype_test(C, SPACE_IMAGE);
273 }
274
275 int ED_operator_nla_active(bContext *C)
276 {
277         return ed_spacetype_test(C, SPACE_NLA);
278 }
279
280 int ED_operator_logic_active(bContext *C)
281 {
282         return ed_spacetype_test(C, SPACE_LOGIC);
283 }
284
285 int ED_operator_info_active(bContext *C)
286 {
287         return ed_spacetype_test(C, SPACE_INFO);
288 }
289
290
291 int ED_operator_console_active(bContext *C)
292 {
293         return ed_spacetype_test(C, SPACE_CONSOLE);
294 }
295
296 int ED_operator_object_active(bContext *C)
297 {
298         Object *ob = ED_object_active_context(C);
299         return ((ob != NULL) && !(ob->restrictflag & OB_RESTRICT_VIEW));
300 }
301
302 int ED_operator_object_active_editable(bContext *C)
303 {
304         Object *ob = ED_object_active_context(C);
305         return ((ob != NULL) && !(ob->id.lib) && !(ob->restrictflag & OB_RESTRICT_VIEW));
306 }
307
308 int ED_operator_object_active_editable_mesh(bContext *C)
309 {
310         Object *ob = ED_object_active_context(C);
311         return ((ob != NULL) && !(ob->id.lib) && !(ob->restrictflag & OB_RESTRICT_VIEW) &&
312                 (ob->type == OB_MESH) && !(((ID *)ob->data)->lib));
313 }
314
315 int ED_operator_object_active_editable_font(bContext *C)
316 {
317         Object *ob = ED_object_active_context(C);
318         return ((ob != NULL) && !(ob->id.lib) && !(ob->restrictflag & OB_RESTRICT_VIEW) &&
319                 (ob->type == OB_FONT));
320 }
321
322 int ED_operator_editmesh(bContext *C)
323 {
324         Object *obedit = CTX_data_edit_object(C);
325         if (obedit && obedit->type == OB_MESH)
326                 return NULL != BMEdit_FromObject(obedit);
327         return 0;
328 }
329
330 int ED_operator_editmesh_view3d(bContext *C)
331 {
332         return ED_operator_editmesh(C) && ED_operator_view3d_active(C);
333 }
334
335 int ED_operator_editmesh_region_view3d(bContext *C)
336 {
337         if (ED_operator_editmesh(C) && CTX_wm_region_view3d(C))
338                 return 1;
339
340         CTX_wm_operator_poll_msg_set(C, "expected a view3d region & editmesh");
341         return 0;
342 }
343
344 int ED_operator_editarmature(bContext *C)
345 {
346         Object *obedit = CTX_data_edit_object(C);
347         if (obedit && obedit->type == OB_ARMATURE)
348                 return NULL != ((bArmature *)obedit->data)->edbo;
349         return 0;
350 }
351
352 /**
353  * \brief check for pose mode (no mixed modes)
354  *
355  * We wan't to enable most pose operations in weight paint mode,
356  * when it comes to transforming bones, but managing bomes layers/groups
357  * can be left for pose mode only. (not weight paint mode)
358  */
359 int ED_operator_posemode_exclusive(bContext *C)
360 {
361         Object *obact = CTX_data_active_object(C);
362
363         if (obact && !(obact->mode & OB_MODE_EDIT)) {
364                 Object *obpose;
365                 if ((obpose = BKE_object_pose_armature_get(obact))) {
366                         if (obact == obpose) {
367                                 return 1;
368                         }
369                 }
370         }
371
372         return 0;
373 }
374
375 int ED_operator_posemode(bContext *C)
376 {
377         Object *obact = CTX_data_active_object(C);
378
379         if (obact && !(obact->mode & OB_MODE_EDIT)) {
380                 Object *obpose;
381                 if ((obpose = BKE_object_pose_armature_get(obact))) {
382                         if ((obact == obpose) || (obact->mode & OB_MODE_WEIGHT_PAINT)) {
383                                 return 1;
384                         }
385                 }
386         }
387
388         return 0;
389 }
390
391 /* wrapper for ED_space_image_show_uvedit */
392 int ED_operator_uvedit(bContext *C)
393 {
394         SpaceImage *sima = CTX_wm_space_image(C);
395         Object *obedit = CTX_data_edit_object(C);
396         return ED_space_image_show_uvedit(sima, obedit);
397 }
398
399 int ED_operator_uvmap(bContext *C)
400 {
401         Object *obedit = CTX_data_edit_object(C);
402         BMEditMesh *em = NULL;
403         
404         if (obedit && obedit->type == OB_MESH) {
405                 em = BMEdit_FromObject(obedit);
406         }
407         
408         if (em && (em->bm->totface)) {
409                 return TRUE;
410         }
411         
412         return FALSE;
413 }
414
415 int ED_operator_editsurfcurve(bContext *C)
416 {
417         Object *obedit = CTX_data_edit_object(C);
418         if (obedit && ELEM(obedit->type, OB_CURVE, OB_SURF))
419                 return NULL != ((Curve *)obedit->data)->editnurb;
420         return 0;
421 }
422
423 int ED_operator_editsurfcurve_region_view3d(bContext *C)
424 {
425         if (ED_operator_editsurfcurve(C) && CTX_wm_region_view3d(C))
426                 return 1;
427
428         CTX_wm_operator_poll_msg_set(C, "expected a view3d region & editcurve");
429         return 0;
430 }
431
432 int ED_operator_editcurve(bContext *C)
433 {
434         Object *obedit = CTX_data_edit_object(C);
435         if (obedit && obedit->type == OB_CURVE)
436                 return NULL != ((Curve *)obedit->data)->editnurb;
437         return 0;
438 }
439
440 int ED_operator_editcurve_3d(bContext *C)
441 {
442         Object *obedit = CTX_data_edit_object(C);
443         if (obedit && obedit->type == OB_CURVE) {
444                 Curve *cu = (Curve *)obedit->data;
445
446                 return (cu->flag & CU_3D) && (NULL != cu->editnurb);
447         }
448         return 0;
449 }
450
451 int ED_operator_editsurf(bContext *C)
452 {
453         Object *obedit = CTX_data_edit_object(C);
454         if (obedit && obedit->type == OB_SURF)
455                 return NULL != ((Curve *)obedit->data)->editnurb;
456         return 0;
457 }
458
459 int ED_operator_editfont(bContext *C)
460 {
461         Object *obedit = CTX_data_edit_object(C);
462         if (obedit && obedit->type == OB_FONT)
463                 return NULL != ((Curve *)obedit->data)->editfont;
464         return 0;
465 }
466
467 int ED_operator_editlattice(bContext *C)
468 {
469         Object *obedit = CTX_data_edit_object(C);
470         if (obedit && obedit->type == OB_LATTICE)
471                 return NULL != ((Lattice *)obedit->data)->editlatt;
472         return 0;
473 }
474
475 int ED_operator_editmball(bContext *C)
476 {
477         Object *obedit = CTX_data_edit_object(C);
478         if (obedit && obedit->type == OB_MBALL)
479                 return NULL != ((MetaBall *)obedit->data)->editelems;
480         return 0;
481 }
482
483 int ED_operator_mask(bContext *C)
484 {
485         ScrArea *sa = CTX_wm_area(C);
486         if (sa && sa->spacedata.first) {
487                 switch (sa->spacetype) {
488                         case SPACE_CLIP:
489                         {
490                                 SpaceClip *sc = sa->spacedata.first;
491                                 return ED_space_clip_check_show_maskedit(sc);
492                         }
493                         case SPACE_SEQ:
494                         {
495                                 SpaceSeq *sseq = sa->spacedata.first;
496                                 Scene *scene = CTX_data_scene(C);
497                                 return ED_space_sequencer_check_show_maskedit(sseq, scene);
498                         }
499                         case SPACE_IMAGE:
500                         {
501                                 SpaceImage *sima = sa->spacedata.first;
502                                 Scene *scene = CTX_data_scene(C);
503                                 return ED_space_image_check_show_maskedit(scene, sima);
504                         }
505                 }
506         }
507
508         return FALSE;
509 }
510
511 /* *************************** action zone operator ************************** */
512
513 /* operator state vars used:  
514  * none
515  * 
516  * functions:
517  * 
518  * apply() set actionzone event
519  * 
520  * exit()       free customdata
521  * 
522  * callbacks:
523  * 
524  * exec()       never used
525  * 
526  * invoke() check if in zone  
527  * add customdata, put mouseco and area in it
528  * add modal handler
529  * 
530  * modal()      accept modal events while doing it
531  * call apply() with gesture info, active window, nonactive window
532  * call exit() and remove handler when LMB confirm
533  */
534
535 typedef struct sActionzoneData {
536         ScrArea *sa1, *sa2;
537         AZone *az;
538         int x, y, gesture_dir, modifier;
539 } sActionzoneData;
540
541 /* used by other operators too */
542 static ScrArea *screen_areahascursor(bScreen *scr, int x, int y)
543 {
544         ScrArea *sa = NULL;
545         sa = scr->areabase.first;
546         while (sa) {
547                 if (BLI_rcti_isect_pt(&sa->totrct, x, y)) break;
548                 sa = sa->next;
549         }
550         
551         return sa;
552 }
553
554 /* quick poll to save operators to be created and handled */
555 static int actionzone_area_poll(bContext *C)
556 {
557         wmWindow *win = CTX_wm_window(C);
558         ScrArea *sa = CTX_wm_area(C);
559         
560         if (sa && win) {
561                 AZone *az;
562                 int x = win->eventstate->x;
563                 int y = win->eventstate->y;
564                 
565                 for (az = sa->actionzones.first; az; az = az->next)
566                         if (BLI_rcti_isect_pt(&az->rect, x, y))
567                                 return 1;
568         }       
569         return 0;
570 }
571
572 AZone *is_in_area_actionzone(ScrArea *sa, const int xy[2])
573 {
574         AZone *az = NULL;
575         
576         for (az = sa->actionzones.first; az; az = az->next) {
577                 if (BLI_rcti_isect_pt_v(&az->rect, xy)) {
578                         if (az->type == AZONE_AREA) {
579                                 /* no triangle intersect but a hotspot circle based on corner */
580                                 int radius = (xy[0] - az->x1) * (xy[0] - az->x1) + (xy[1] - az->y1) * (xy[1] - az->y1);
581                                 
582                                 if (radius <= AZONESPOT * AZONESPOT)
583                                         break;
584                         }
585                         else if (az->type == AZONE_REGION) {
586                                 break;
587                         }
588                 }
589         }
590         
591         return az;
592 }
593
594
595 static void actionzone_exit(wmOperator *op)
596 {
597         if (op->customdata)
598                 MEM_freeN(op->customdata);
599         op->customdata = NULL;
600 }
601
602 /* send EVT_ACTIONZONE event */
603 static void actionzone_apply(bContext *C, wmOperator *op, int type)
604 {
605         wmEvent event;
606         wmWindow *win = CTX_wm_window(C);
607         sActionzoneData *sad = op->customdata;
608         
609         sad->modifier = RNA_int_get(op->ptr, "modifier");
610         
611         event = *(win->eventstate);  /* XXX huh huh? make api call */
612         if (type == AZONE_AREA)
613                 event.type = EVT_ACTIONZONE_AREA;
614         else
615                 event.type = EVT_ACTIONZONE_REGION;
616         event.customdata = op->customdata;
617         event.customdatafree = TRUE;
618         op->customdata = NULL;
619         
620         wm_event_add(win, &event);
621 }
622
623 static int actionzone_invoke(bContext *C, wmOperator *op, wmEvent *event)
624 {
625         AZone *az = is_in_area_actionzone(CTX_wm_area(C), &event->x);
626         sActionzoneData *sad;
627         
628         /* quick escape */
629         if (az == NULL)
630                 return OPERATOR_PASS_THROUGH;
631         
632         /* ok we do the actionzone */
633         sad = op->customdata = MEM_callocN(sizeof(sActionzoneData), "sActionzoneData");
634         sad->sa1 = CTX_wm_area(C);
635         sad->az = az;
636         sad->x = event->x; sad->y = event->y;
637         
638         /* region azone directly reacts on mouse clicks */
639         if (sad->az->type == AZONE_REGION) {
640                 actionzone_apply(C, op, AZONE_REGION);
641                 actionzone_exit(op);
642                 return OPERATOR_FINISHED;
643         }
644         else {
645                 /* add modal handler */
646                 WM_event_add_modal_handler(C, op);
647                 
648                 return OPERATOR_RUNNING_MODAL;
649         }
650 }
651
652
653 static int actionzone_modal(bContext *C, wmOperator *op, wmEvent *event)
654 {
655         sActionzoneData *sad = op->customdata;
656         int deltax, deltay;
657         int mindelta = sad->az->type == AZONE_REGION ? 1 : 12;
658         
659         switch (event->type) {
660                 case MOUSEMOVE:
661                         /* calculate gesture direction */
662                         deltax = (event->x - sad->x);
663                         deltay = (event->y - sad->y);
664                         
665                         if (deltay > ABS(deltax))
666                                 sad->gesture_dir = 'n';
667                         else if (deltax > ABS(deltay))
668                                 sad->gesture_dir = 'e';
669                         else if (deltay < -ABS(deltax))
670                                 sad->gesture_dir = 's';
671                         else
672                                 sad->gesture_dir = 'w';
673                         
674                         /* gesture is large enough? */
675                         if (ABS(deltax) > mindelta || ABS(deltay) > mindelta) {
676                                 
677                                 /* second area, for join */
678                                 sad->sa2 = screen_areahascursor(CTX_wm_screen(C), event->x, event->y);
679                                 /* apply sends event */
680                                 actionzone_apply(C, op, sad->az->type);
681                                 actionzone_exit(op);
682                                 
683                                 return OPERATOR_FINISHED;
684                         }
685                         break;
686                 case ESCKEY:
687                         actionzone_exit(op);
688                         return OPERATOR_CANCELLED;
689                 case LEFTMOUSE:                         
690                         actionzone_exit(op);
691                         return OPERATOR_CANCELLED;
692                         
693         }
694         
695         return OPERATOR_RUNNING_MODAL;
696 }
697
698 static int actionzone_cancel(bContext *UNUSED(C), wmOperator *op)
699 {
700         actionzone_exit(op);
701
702         return OPERATOR_CANCELLED;
703 }
704
705 static void SCREEN_OT_actionzone(wmOperatorType *ot)
706 {
707         /* identifiers */
708         ot->name = "Handle area action zones";
709         ot->description = "Handle area action zones for mouse actions/gestures";
710         ot->idname = "SCREEN_OT_actionzone";
711         
712         ot->invoke = actionzone_invoke;
713         ot->modal = actionzone_modal;
714         ot->poll = actionzone_area_poll;
715         ot->cancel = actionzone_cancel;
716         
717         ot->flag = OPTYPE_BLOCKING;
718         
719         RNA_def_int(ot->srna, "modifier", 0, 0, 2, "Modifier", "Modifier state", 0, 2);
720 }
721
722 /* ************** swap area operator *********************************** */
723
724 /* operator state vars used:  
725  * sa1          start area
726  * sa2          area to swap with
727  * 
728  * functions:
729  * 
730  * init()   set custom data for operator, based on actionzone event custom data
731  * 
732  * cancel()     cancel the operator
733  * 
734  * exit()       cleanup, send notifier
735  * 
736  * callbacks:
737  * 
738  * invoke() gets called on shift+lmb drag in actionzone
739  * call init(), add handler
740  * 
741  * modal()  accept modal events while doing it
742  */
743
744 typedef struct sAreaSwapData {
745         ScrArea *sa1, *sa2;
746 } sAreaSwapData;
747
748 static int area_swap_init(wmOperator *op, wmEvent *event)
749 {
750         sAreaSwapData *sd = NULL;
751         sActionzoneData *sad = event->customdata;
752         
753         if (sad == NULL || sad->sa1 == NULL)
754                 return 0;
755         
756         sd = MEM_callocN(sizeof(sAreaSwapData), "sAreaSwapData");
757         sd->sa1 = sad->sa1;
758         sd->sa2 = sad->sa2;
759         op->customdata = sd;
760         
761         return 1;
762 }
763
764
765 static void area_swap_exit(bContext *C, wmOperator *op)
766 {
767         WM_cursor_restore(CTX_wm_window(C));
768         if (op->customdata)
769                 MEM_freeN(op->customdata);
770         op->customdata = NULL;
771 }
772
773 static int area_swap_cancel(bContext *C, wmOperator *op)
774 {
775         area_swap_exit(C, op);
776         return OPERATOR_CANCELLED;
777 }
778
779 static int area_swap_invoke(bContext *C, wmOperator *op, wmEvent *event)
780 {
781         
782         if (!area_swap_init(op, event))
783                 return OPERATOR_PASS_THROUGH;
784         
785         /* add modal handler */
786         WM_cursor_modal(CTX_wm_window(C), BC_SWAPAREA_CURSOR);
787         WM_event_add_modal_handler(C, op);
788         
789         return OPERATOR_RUNNING_MODAL;
790         
791 }
792
793 static int area_swap_modal(bContext *C, wmOperator *op, wmEvent *event)
794 {
795         sActionzoneData *sad = op->customdata;
796         
797         switch (event->type) {
798                 case MOUSEMOVE:
799                         /* second area, for join */
800                         sad->sa2 = screen_areahascursor(CTX_wm_screen(C), event->x, event->y);
801                         break;
802                 case LEFTMOUSE: /* release LMB */
803                         if (event->val == KM_RELEASE) {
804                                 if (!sad->sa2 || sad->sa1 == sad->sa2) {
805                                         
806                                         return area_swap_cancel(C, op);
807                                 }
808
809                                 ED_area_tag_redraw(sad->sa1);
810                                 ED_area_tag_redraw(sad->sa2);
811
812                                 ED_area_swapspace(C, sad->sa1, sad->sa2);
813                                 
814                                 area_swap_exit(C, op);
815                                 
816                                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
817                                 
818                                 return OPERATOR_FINISHED;
819                         }
820                         break;
821                         
822                 case ESCKEY:
823                         return area_swap_cancel(C, op);
824         }
825         return OPERATOR_RUNNING_MODAL;
826 }
827
828 static void SCREEN_OT_area_swap(wmOperatorType *ot)
829 {
830         ot->name = "Swap areas";
831         ot->description = "Swap selected areas screen positions";
832         ot->idname = "SCREEN_OT_area_swap";
833         
834         ot->invoke = area_swap_invoke;
835         ot->modal = area_swap_modal;
836         ot->poll = ED_operator_areaactive;
837         ot->cancel = area_swap_cancel;
838         
839         ot->flag = OPTYPE_BLOCKING;
840 }
841
842 /* *********** Duplicate area as new window operator ****************** */
843
844 /* operator callback */
845 static int area_dupli_invoke(bContext *C, wmOperator *op, wmEvent *event)
846 {
847         wmWindow *newwin, *win;
848         bScreen *newsc, *sc;
849         ScrArea *sa;
850         rcti rect;
851         
852         win = CTX_wm_window(C);
853         sc = CTX_wm_screen(C);
854         sa = CTX_wm_area(C);
855         
856         /* XXX hrmf! */
857         if (event->type == EVT_ACTIONZONE_AREA) {
858                 sActionzoneData *sad = event->customdata;
859                 
860                 if (sad == NULL)
861                         return OPERATOR_PASS_THROUGH;
862                 
863                 sa = sad->sa1;
864         }
865         
866         /*  poll() checks area context, but we don't accept full-area windows */
867         if (sc->full != SCREENNORMAL) {
868                 if (event->type == EVT_ACTIONZONE_AREA)
869                         actionzone_exit(op);
870                 return OPERATOR_CANCELLED;
871         }
872         
873         /* adds window to WM */
874         rect = sa->totrct;
875         BLI_rcti_translate(&rect, win->posx, win->posy);
876         newwin = WM_window_open(C, &rect);
877         
878         /* allocs new screen and adds to newly created window, using window size */
879         newsc = ED_screen_add(newwin, CTX_data_scene(C), sc->id.name + 2);
880         newwin->screen = newsc;
881         
882         /* copy area to new screen */
883         area_copy_data((ScrArea *)newsc->areabase.first, sa, 0);
884
885         ED_area_tag_redraw((ScrArea *)newsc->areabase.first);
886
887         /* screen, areas init */
888         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
889         
890         if (event->type == EVT_ACTIONZONE_AREA)
891                 actionzone_exit(op);
892         
893         return OPERATOR_FINISHED;
894 }
895
896 static void SCREEN_OT_area_dupli(wmOperatorType *ot)
897 {
898         ot->name = "Duplicate Area into New Window";
899         ot->description = "Duplicate selected area into new window";
900         ot->idname = "SCREEN_OT_area_dupli";
901         
902         ot->invoke = area_dupli_invoke;
903         ot->poll = ED_operator_areaactive;
904 }
905
906
907 /* ************** move area edge operator *********************************** */
908
909 /* operator state vars used:  
910  * x, y             mouse coord near edge
911  * delta            movement of edge
912  * 
913  * functions:
914  * 
915  * init()   set default property values, find edge based on mouse coords, test
916  * if the edge can be moved, select edges, calculate min and max movement
917  * 
918  * apply()      apply delta on selection
919  * 
920  * exit()       cleanup, send notifier
921  * 
922  * cancel() cancel moving
923  * 
924  * callbacks:
925  * 
926  * exec()   execute without any user interaction, based on properties
927  * call init(), apply(), exit()
928  * 
929  * invoke() gets called on mouse click near edge
930  * call init(), add handler
931  * 
932  * modal()  accept modal events while doing it
933  * call apply() with delta motion
934  * call exit() and remove handler
935  */
936
937 typedef struct sAreaMoveData {
938         int bigger, smaller, origval, step;
939         char dir;
940 } sAreaMoveData;
941
942 /* helper call to move area-edge, sets limits */
943 static void area_move_set_limits(bScreen *sc, int dir, int *bigger, int *smaller)
944 {
945         ScrArea *sa;
946         int areaminy = ED_area_headersize() + 1;
947         
948         /* we check all areas and test for free space with MINSIZE */
949         *bigger = *smaller = 100000;
950         
951         for (sa = sc->areabase.first; sa; sa = sa->next) {
952                 if (dir == 'h') {
953                         int y1 = sa->v2->vec.y - sa->v1->vec.y - areaminy;
954                         
955                         /* if top or down edge selected, test height */
956                         if (sa->v1->flag && sa->v4->flag)
957                                 *bigger = MIN2(*bigger, y1);
958                         else if (sa->v2->flag && sa->v3->flag)
959                                 *smaller = MIN2(*smaller, y1);
960                 }
961                 else {
962                         int x1 = sa->v4->vec.x - sa->v1->vec.x - AREAMINX;
963                         
964                         /* if left or right edge selected, test width */
965                         if (sa->v1->flag && sa->v2->flag)
966                                 *bigger = MIN2(*bigger, x1);
967                         else if (sa->v3->flag && sa->v4->flag)
968                                 *smaller = MIN2(*smaller, x1);
969                 }
970         }
971 }
972
973 /* validate selection inside screen, set variables OK */
974 /* return 0: init failed */
975 static int area_move_init(bContext *C, wmOperator *op)
976 {
977         bScreen *sc = CTX_wm_screen(C);
978         ScrEdge *actedge;
979         sAreaMoveData *md;
980         int x, y;
981         
982         /* required properties */
983         x = RNA_int_get(op->ptr, "x");
984         y = RNA_int_get(op->ptr, "y");
985         
986         /* setup */
987         actedge = screen_find_active_scredge(sc, x, y);
988         if (actedge == NULL) return 0;
989         
990         md = MEM_callocN(sizeof(sAreaMoveData), "sAreaMoveData");
991         op->customdata = md;
992         
993         md->dir = scredge_is_horizontal(actedge) ? 'h' : 'v';
994         if (md->dir == 'h') md->origval = actedge->v1->vec.y;
995         else md->origval = actedge->v1->vec.x;
996         
997         select_connected_scredge(sc, actedge);
998         /* now all vertices with 'flag==1' are the ones that can be moved. */
999         
1000         area_move_set_limits(sc, md->dir, &md->bigger, &md->smaller);
1001         
1002         return 1;
1003 }
1004
1005 /* moves selected screen edge amount of delta, used by split & move */
1006 static void area_move_apply_do(bContext *C, int origval, int delta, int dir, int bigger, int smaller)
1007 {
1008         wmWindow *win = CTX_wm_window(C);
1009         bScreen *sc = CTX_wm_screen(C);
1010         ScrVert *v1;
1011         ScrArea *sa;
1012         int areaminy = ED_area_headersize() + 1;
1013         
1014         delta = CLAMPIS(delta, -smaller, bigger);
1015         
1016         for (v1 = sc->vertbase.first; v1; v1 = v1->next) {
1017                 if (v1->flag) {
1018                         /* that way a nice AREAGRID  */
1019                         if ((dir == 'v') && v1->vec.x > 0 && v1->vec.x < win->sizex - 1) {
1020                                 v1->vec.x = origval + delta;
1021                                 if (delta != bigger && delta != -smaller) v1->vec.x -= (v1->vec.x % AREAGRID);
1022                         }
1023                         if ((dir == 'h') && v1->vec.y > 0 && v1->vec.y < win->sizey - 1) {
1024                                 v1->vec.y = origval + delta;
1025                                 
1026                                 v1->vec.y += AREAGRID - 1;
1027                                 v1->vec.y -= (v1->vec.y % AREAGRID);
1028                                 
1029                                 /* prevent too small top header */
1030                                 if (v1->vec.y > win->sizey - areaminy)
1031                                         v1->vec.y = win->sizey - areaminy;
1032                         }
1033                 }
1034         }
1035
1036         for (sa = sc->areabase.first; sa; sa = sa->next) {
1037                 if (sa->v1->flag || sa->v2->flag || sa->v3->flag || sa->v4->flag)
1038                         ED_area_tag_redraw(sa);
1039         }
1040
1041         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL); /* redraw everything */
1042 }
1043
1044 static void area_move_apply(bContext *C, wmOperator *op)
1045 {
1046         sAreaMoveData *md = op->customdata;
1047         int delta;
1048         
1049         delta = RNA_int_get(op->ptr, "delta");
1050         area_move_apply_do(C, md->origval, delta, md->dir, md->bigger, md->smaller);
1051 }
1052
1053 static void area_move_exit(bContext *C, wmOperator *op)
1054 {
1055         if (op->customdata)
1056                 MEM_freeN(op->customdata);
1057         op->customdata = NULL;
1058         
1059         /* this makes sure aligned edges will result in aligned grabbing */
1060         removedouble_scrverts(CTX_wm_screen(C));
1061         removedouble_scredges(CTX_wm_screen(C));
1062 }
1063
1064 static int area_move_exec(bContext *C, wmOperator *op)
1065 {
1066         if (!area_move_init(C, op))
1067                 return OPERATOR_CANCELLED;
1068         
1069         area_move_apply(C, op);
1070         area_move_exit(C, op);
1071         
1072         return OPERATOR_FINISHED;
1073 }
1074
1075 /* interaction callback */
1076 static int area_move_invoke(bContext *C, wmOperator *op, wmEvent *event)
1077 {
1078         RNA_int_set(op->ptr, "x", event->x);
1079         RNA_int_set(op->ptr, "y", event->y);
1080         
1081         if (!area_move_init(C, op)) 
1082                 return OPERATOR_PASS_THROUGH;
1083         
1084         /* add temp handler */
1085         WM_event_add_modal_handler(C, op);
1086         
1087         return OPERATOR_RUNNING_MODAL;
1088 }
1089
1090 static int area_move_cancel(bContext *C, wmOperator *op)
1091 {
1092         
1093         RNA_int_set(op->ptr, "delta", 0);
1094         area_move_apply(C, op);
1095         area_move_exit(C, op);
1096         
1097         return OPERATOR_CANCELLED;
1098 }
1099
1100 /* modal callback for while moving edges */
1101 static int area_move_modal(bContext *C, wmOperator *op, wmEvent *event)
1102 {
1103         sAreaMoveData *md = op->customdata;
1104         int delta, x, y;
1105         
1106         /* execute the events */
1107         switch (event->type) {
1108                 case MOUSEMOVE:
1109                         
1110                         x = RNA_int_get(op->ptr, "x");
1111                         y = RNA_int_get(op->ptr, "y");
1112                         
1113                         delta = (md->dir == 'v') ? event->x - x : event->y - y;
1114                         if (md->step) delta = delta - (delta % md->step);
1115                         RNA_int_set(op->ptr, "delta", delta);
1116                         
1117                         area_move_apply(C, op);
1118                         break;
1119                         
1120                 case EVT_MODAL_MAP:
1121                         
1122                         switch (event->val) {
1123                                 case KM_MODAL_APPLY:
1124                                         area_move_exit(C, op);
1125                                         return OPERATOR_FINISHED;
1126                                         
1127                                 case KM_MODAL_CANCEL:
1128                                         return area_move_cancel(C, op);
1129                                         
1130                                 case KM_MODAL_STEP10:
1131                                         md->step = 10;
1132                                         break;
1133                                 case KM_MODAL_STEP10_OFF:
1134                                         md->step = 0;
1135                                         break;
1136                         }
1137         }
1138         
1139         return OPERATOR_RUNNING_MODAL;
1140 }
1141
1142 static void SCREEN_OT_area_move(wmOperatorType *ot)
1143 {
1144         /* identifiers */
1145         ot->name = "Move area edges";
1146         ot->description = "Move selected area edges";
1147         ot->idname = "SCREEN_OT_area_move";
1148         
1149         ot->exec = area_move_exec;
1150         ot->invoke = area_move_invoke;
1151         ot->cancel = area_move_cancel;
1152         ot->modal = area_move_modal;
1153         ot->poll = ED_operator_screen_mainwinactive; /* when mouse is over area-edge */
1154         
1155         ot->flag = OPTYPE_BLOCKING;
1156         
1157         /* rna */
1158         RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
1159         RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
1160         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1161 }
1162
1163 /* ************** split area operator *********************************** */
1164
1165 /* 
1166  * operator state vars:  
1167  * fac              spit point
1168  * dir              direction 'v' or 'h'
1169  * 
1170  * operator customdata:
1171  * area             pointer to (active) area
1172  * x, y                 last used mouse pos
1173  * (more, see below)
1174  * 
1175  * functions:
1176  * 
1177  * init()   set default property values, find area based on context
1178  * 
1179  * apply()      split area based on state vars
1180  * 
1181  * exit()       cleanup, send notifier
1182  * 
1183  * cancel() remove duplicated area
1184  * 
1185  * callbacks:
1186  * 
1187  * exec()   execute without any user interaction, based on state vars
1188  * call init(), apply(), exit()
1189  * 
1190  * invoke() gets called on mouse click in action-widget
1191  * call init(), add modal handler
1192  * call apply() with initial motion
1193  * 
1194  * modal()  accept modal events while doing it
1195  * call move-areas code with delta motion
1196  * call exit() or cancel() and remove handler
1197  */
1198
1199 #define SPLIT_STARTED   1
1200 #define SPLIT_PROGRESS  2
1201
1202 typedef struct sAreaSplitData {
1203         int x, y;   /* last used mouse position */
1204         
1205         int origval;            /* for move areas */
1206         int bigger, smaller;    /* constraints for moving new edge */
1207         int delta;              /* delta move edge */
1208         int origmin, origsize;  /* to calculate fac, for property storage */
1209         int previewmode;        /* draw previewline, then split */
1210
1211         ScrEdge *nedge;         /* new edge */
1212         ScrArea *sarea;         /* start area */
1213         ScrArea *narea;         /* new area */
1214         
1215 } sAreaSplitData;
1216
1217 /* generic init, menu case, doesn't need active area */
1218 static int area_split_menu_init(bContext *C, wmOperator *op)
1219 {
1220         sAreaSplitData *sd;
1221         
1222         /* custom data */
1223         sd = (sAreaSplitData *)MEM_callocN(sizeof(sAreaSplitData), "op_area_split");
1224         op->customdata = sd;
1225         
1226         sd->sarea = CTX_wm_area(C);
1227         
1228         if (sd->sarea) {
1229                 int dir = RNA_enum_get(op->ptr, "direction");
1230
1231                 if (dir == 'h')
1232                         sd->sarea->flag |= AREA_FLAG_DRAWSPLIT_H;
1233                 else
1234                         sd->sarea->flag |= AREA_FLAG_DRAWSPLIT_V;
1235         }
1236         return 1;
1237 }
1238
1239 /* generic init, no UI stuff here, assumes active area */
1240 static int area_split_init(bContext *C, wmOperator *op)
1241 {
1242         ScrArea *sa = CTX_wm_area(C);
1243         sAreaSplitData *sd;
1244         int areaminy = ED_area_headersize() + 1;
1245         int dir;
1246         
1247         /* required context */
1248         if (sa == NULL) return 0;
1249         
1250         /* required properties */
1251         dir = RNA_enum_get(op->ptr, "direction");
1252         
1253         /* minimal size */
1254         if (dir == 'v' && sa->winx < 2 * AREAMINX) return 0;
1255         if (dir == 'h' && sa->winy < 2 * areaminy) return 0;
1256         
1257         /* custom data */
1258         sd = (sAreaSplitData *)MEM_callocN(sizeof(sAreaSplitData), "op_area_split");
1259         op->customdata = sd;
1260         
1261         sd->sarea = sa;
1262         sd->origsize = dir == 'v' ? sa->winx : sa->winy;
1263         sd->origmin = dir == 'v' ? sa->totrct.xmin : sa->totrct.ymin;
1264         
1265         return 1;
1266 }
1267
1268 /* with sa as center, sb is located at: 0=W, 1=N, 2=E, 3=S */
1269 /* used with split operator */
1270 static ScrEdge *area_findsharededge(bScreen *screen, ScrArea *sa, ScrArea *sb)
1271 {
1272         ScrVert *sav1 = sa->v1;
1273         ScrVert *sav2 = sa->v2;
1274         ScrVert *sav3 = sa->v3;
1275         ScrVert *sav4 = sa->v4;
1276         ScrVert *sbv1 = sb->v1;
1277         ScrVert *sbv2 = sb->v2;
1278         ScrVert *sbv3 = sb->v3;
1279         ScrVert *sbv4 = sb->v4;
1280         
1281         if (sav1 == sbv4 && sav2 == sbv3) { /* sa to right of sb = W */
1282                 return screen_findedge(screen, sav1, sav2);
1283         }
1284         else if (sav2 == sbv1 && sav3 == sbv4) { /* sa to bottom of sb = N */
1285                 return screen_findedge(screen, sav2, sav3);
1286         }
1287         else if (sav3 == sbv2 && sav4 == sbv1) { /* sa to left of sb = E */
1288                 return screen_findedge(screen, sav3, sav4);
1289         }
1290         else if (sav1 == sbv2 && sav4 == sbv3) { /* sa on top of sb = S*/
1291                 return screen_findedge(screen, sav1, sav4);
1292         }
1293         
1294         return NULL;
1295 }
1296
1297
1298 /* do the split, return success */
1299 static int area_split_apply(bContext *C, wmOperator *op)
1300 {
1301         bScreen *sc = CTX_wm_screen(C);
1302         sAreaSplitData *sd = (sAreaSplitData *)op->customdata;
1303         float fac;
1304         int dir;
1305         
1306         fac = RNA_float_get(op->ptr, "factor");
1307         dir = RNA_enum_get(op->ptr, "direction");
1308         
1309         sd->narea = area_split(sc, sd->sarea, dir, fac, 0); /* 0 = no merge */
1310         
1311         if (sd->narea) {
1312                 ScrVert *sv;
1313                 
1314                 sd->nedge = area_findsharededge(sc, sd->sarea, sd->narea);
1315                 
1316                 /* select newly created edge, prepare for moving edge */
1317                 for (sv = sc->vertbase.first; sv; sv = sv->next)
1318                         sv->flag = 0;
1319                 
1320                 sd->nedge->v1->flag = 1;
1321                 sd->nedge->v2->flag = 1;
1322                 
1323                 if (dir == 'h') sd->origval = sd->nedge->v1->vec.y;
1324                 else sd->origval = sd->nedge->v1->vec.x;
1325
1326                 ED_area_tag_redraw(sd->sarea);
1327                 ED_area_tag_redraw(sd->narea);
1328
1329                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
1330                 
1331                 return 1;
1332         }               
1333         
1334         return 0;
1335 }
1336
1337 static void area_split_exit(bContext *C, wmOperator *op)
1338 {
1339         if (op->customdata) {
1340                 sAreaSplitData *sd = (sAreaSplitData *)op->customdata;
1341                 if (sd->sarea) ED_area_tag_redraw(sd->sarea);
1342                 if (sd->narea) ED_area_tag_redraw(sd->narea);
1343
1344                 if (sd->sarea)
1345                         sd->sarea->flag &= ~(AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V);
1346                 
1347                 MEM_freeN(op->customdata);
1348                 op->customdata = NULL;
1349         }
1350         
1351         WM_cursor_restore(CTX_wm_window(C));
1352         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
1353         
1354         /* this makes sure aligned edges will result in aligned grabbing */
1355         removedouble_scrverts(CTX_wm_screen(C));
1356         removedouble_scredges(CTX_wm_screen(C));
1357 }
1358
1359
1360 /* UI callback, adds new handler */
1361 static int area_split_invoke(bContext *C, wmOperator *op, wmEvent *event)
1362 {
1363         sAreaSplitData *sd;
1364         int dir;
1365         
1366         /* no full window splitting allowed */
1367         if (CTX_wm_screen(C)->full != SCREENNORMAL)
1368                 return OPERATOR_CANCELLED;
1369         
1370         if (event->type == EVT_ACTIONZONE_AREA) {
1371                 sActionzoneData *sad = event->customdata;
1372                 
1373                 if (sad->modifier > 0) {
1374                         return OPERATOR_PASS_THROUGH;
1375                 }
1376                 
1377                 /* verify *sad itself */
1378                 if (sad == NULL || sad->sa1 == NULL || sad->az == NULL)
1379                         return OPERATOR_PASS_THROUGH;
1380                 
1381                 /* is this our *sad? if areas not equal it should be passed on */
1382                 if (CTX_wm_area(C) != sad->sa1 || sad->sa1 != sad->sa2)
1383                         return OPERATOR_PASS_THROUGH;
1384                 
1385                 /* prepare operator state vars */
1386                 if (sad->gesture_dir == 'n' || sad->gesture_dir == 's') {
1387                         dir = 'h';
1388                         RNA_float_set(op->ptr, "factor", ((float)(event->x - sad->sa1->v1->vec.x)) / (float)sad->sa1->winx);
1389                 }
1390                 else {
1391                         dir = 'v';
1392                         RNA_float_set(op->ptr, "factor", ((float)(event->y - sad->sa1->v1->vec.y)) / (float)sad->sa1->winy);
1393                 }
1394                 RNA_enum_set(op->ptr, "direction", dir);
1395                 
1396                 /* general init, also non-UI case, adds customdata, sets area and defaults */
1397                 if (!area_split_init(C, op))
1398                         return OPERATOR_PASS_THROUGH;
1399                 
1400         }
1401         else {
1402                 ScrEdge *actedge;
1403                 int x, y;
1404                 
1405                 /* retrieve initial mouse coord, so we can find the active edge */
1406                 if (RNA_struct_property_is_set(op->ptr, "mouse_x"))
1407                         x = RNA_int_get(op->ptr, "mouse_x");
1408                 else
1409                         x = event->x;
1410                 
1411                 if (RNA_struct_property_is_set(op->ptr, "mouse_y"))
1412                         y = RNA_int_get(op->ptr, "mouse_y");
1413                 else
1414                         y = event->x;
1415                 
1416                 actedge = screen_find_active_scredge(CTX_wm_screen(C), x, y);
1417                 if (actedge == NULL)
1418                         return OPERATOR_CANCELLED;
1419                 
1420                 dir = scredge_is_horizontal(actedge) ? 'v' : 'h';
1421                 
1422                 RNA_enum_set(op->ptr, "direction", dir);
1423                 
1424                 /* special case, adds customdata, sets defaults */
1425                 if (!area_split_menu_init(C, op))
1426                         return OPERATOR_CANCELLED;
1427                 
1428         }
1429         
1430         sd = (sAreaSplitData *)op->customdata;
1431         
1432         sd->x = event->x;
1433         sd->y = event->y;
1434         
1435         if (event->type == EVT_ACTIONZONE_AREA) {
1436                 
1437                 /* do the split */
1438                 if (area_split_apply(C, op)) {
1439                         area_move_set_limits(CTX_wm_screen(C), dir, &sd->bigger, &sd->smaller);
1440                         
1441                         /* add temp handler for edge move or cancel */
1442                         WM_event_add_modal_handler(C, op);
1443                         
1444                         return OPERATOR_RUNNING_MODAL;
1445                 }
1446         }
1447         else {
1448                 sd->previewmode = 1;
1449                 /* add temp handler for edge move or cancel */
1450                 WM_event_add_modal_handler(C, op);
1451                 
1452                 return OPERATOR_RUNNING_MODAL;
1453                 
1454         }
1455         
1456         return OPERATOR_PASS_THROUGH;
1457 }
1458
1459 /* function to be called outside UI context, or for redo */
1460 static int area_split_exec(bContext *C, wmOperator *op)
1461 {
1462         
1463         if (!area_split_init(C, op))
1464                 return OPERATOR_CANCELLED;
1465         
1466         area_split_apply(C, op);
1467         area_split_exit(C, op);
1468         
1469         return OPERATOR_FINISHED;
1470 }
1471
1472
1473 static int area_split_cancel(bContext *C, wmOperator *op)
1474 {
1475         sAreaSplitData *sd = (sAreaSplitData *)op->customdata;
1476         
1477         if (sd->previewmode) {
1478         }
1479         else {
1480                 if (screen_area_join(C, CTX_wm_screen(C), sd->sarea, sd->narea)) {
1481                         if (CTX_wm_area(C) == sd->narea) {
1482                                 CTX_wm_area_set(C, NULL);
1483                                 CTX_wm_region_set(C, NULL);
1484                         }
1485                         sd->narea = NULL;
1486                 }
1487         }
1488         area_split_exit(C, op);
1489         
1490         return OPERATOR_CANCELLED;
1491 }
1492
1493 static int area_split_modal(bContext *C, wmOperator *op, wmEvent *event)
1494 {
1495         sAreaSplitData *sd = (sAreaSplitData *)op->customdata;
1496         float fac;
1497         int dir;
1498         
1499         /* execute the events */
1500         switch (event->type) {
1501                 case MOUSEMOVE:
1502                         dir = RNA_enum_get(op->ptr, "direction");
1503                         
1504                         sd->delta = (dir == 'v') ? event->x - sd->origval : event->y - sd->origval;
1505                         if (sd->previewmode == 0)
1506                                 area_move_apply_do(C, sd->origval, sd->delta, dir, sd->bigger, sd->smaller);
1507                         else {
1508                                 if (sd->sarea) {
1509                                         sd->sarea->flag &= ~(AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V);
1510                                         ED_area_tag_redraw(sd->sarea);
1511                                 }
1512                                 sd->sarea = screen_areahascursor(CTX_wm_screen(C), event->x, event->y);  /* area context not set */
1513                                 
1514                                 if (sd->sarea) {
1515                                         ED_area_tag_redraw(sd->sarea);
1516                                         if (dir == 'v') {
1517                                                 sd->origsize = sd->sarea->winx;
1518                                                 sd->origmin = sd->sarea->totrct.xmin;
1519                                                 sd->sarea->flag |= AREA_FLAG_DRAWSPLIT_V;
1520                                         }
1521                                         else {
1522                                                 sd->origsize = sd->sarea->winy;
1523                                                 sd->origmin = sd->sarea->totrct.ymin;
1524                                                 sd->sarea->flag |= AREA_FLAG_DRAWSPLIT_H;
1525                                         }
1526                                 }
1527                                 
1528                                 CTX_wm_window(C)->screen->do_draw = TRUE;
1529
1530                         }
1531                         
1532                         fac = (dir == 'v') ? event->x - sd->origmin : event->y - sd->origmin;
1533                         RNA_float_set(op->ptr, "factor", fac / (float)sd->origsize);
1534                         
1535                         break;
1536                         
1537                 case LEFTMOUSE:
1538                         if (sd->previewmode) {
1539                                 area_split_apply(C, op);
1540                                 area_split_exit(C, op);
1541                                 return OPERATOR_FINISHED;
1542                         }
1543                         else {
1544                                 if (event->val == KM_RELEASE) { /* mouse up */
1545                                         area_split_exit(C, op);
1546                                         return OPERATOR_FINISHED;
1547                                 }
1548                         }
1549                         break;
1550                         
1551                 case MIDDLEMOUSE:
1552                 case TABKEY:
1553                         if (sd->previewmode == 0) {
1554                         }
1555                         else {
1556                                 dir = RNA_enum_get(op->ptr, "direction");
1557                                 
1558                                 if (event->val == KM_PRESS) {
1559                                         if (sd->sarea) {
1560                                                 sd->sarea->flag &= ~(AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V);
1561                                                 ED_area_tag_redraw(sd->sarea);
1562                                                 
1563                                                 if (dir == 'v') {
1564                                                         RNA_enum_set(op->ptr, "direction", 'h');
1565                                                         sd->sarea->flag |= AREA_FLAG_DRAWSPLIT_H;
1566                                                         
1567                                                         WM_cursor_set(CTX_wm_window(C), CURSOR_X_MOVE);
1568                                                 }
1569                                                 else {
1570                                                         RNA_enum_set(op->ptr, "direction", 'v');
1571                                                         sd->sarea->flag |= AREA_FLAG_DRAWSPLIT_V;
1572                                                         
1573                                                         WM_cursor_set(CTX_wm_window(C), CURSOR_Y_MOVE);
1574                                                 }
1575                                         }
1576                                 }
1577                         }
1578                         
1579                         break;
1580                         
1581                 case RIGHTMOUSE: /* cancel operation */
1582                 case ESCKEY:
1583                         return area_split_cancel(C, op);
1584         }
1585         
1586         return OPERATOR_RUNNING_MODAL;
1587 }
1588
1589 static EnumPropertyItem prop_direction_items[] = {
1590         {'h', "HORIZONTAL", 0, "Horizontal", ""},
1591         {'v', "VERTICAL", 0, "Vertical", ""},
1592         {0, NULL, 0, NULL, NULL}
1593 };
1594
1595 static void SCREEN_OT_area_split(wmOperatorType *ot)
1596 {
1597         ot->name = "Split Area";
1598         ot->description = "Split selected area into new windows";
1599         ot->idname = "SCREEN_OT_area_split";
1600         
1601         ot->exec = area_split_exec;
1602         ot->invoke = area_split_invoke;
1603         ot->modal = area_split_modal;
1604         ot->cancel = area_split_cancel;
1605         
1606         ot->poll = screen_active_editable;
1607         ot->flag = OPTYPE_BLOCKING;
1608         
1609         /* rna */
1610         RNA_def_enum(ot->srna, "direction", prop_direction_items, 'h', "Direction", "");
1611         RNA_def_float(ot->srna, "factor", 0.5f, 0.0, 1.0, "Factor", "", 0.0, 1.0);
1612         RNA_def_int(ot->srna, "mouse_x", -100, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
1613         RNA_def_int(ot->srna, "mouse_y", -100, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
1614 }
1615
1616
1617
1618 /* ************** scale region edge operator *********************************** */
1619
1620 typedef struct RegionMoveData {
1621         AZone *az;
1622         ARegion *ar;
1623         ScrArea *sa;
1624         int bigger, smaller, origval;
1625         int origx, origy;
1626         int maxsize;
1627         AZEdge edge;
1628         
1629 } RegionMoveData;
1630
1631
1632 static int area_max_regionsize(ScrArea *sa, ARegion *scalear, AZEdge edge)
1633 {
1634         ARegion *ar;
1635         int dist;
1636         
1637         if (edge == AE_RIGHT_TO_TOPLEFT || edge == AE_LEFT_TO_TOPRIGHT) {
1638                 dist = BLI_RCT_SIZE_X(&sa->totrct);
1639         }
1640         else {  /* AE_BOTTOM_TO_TOPLEFT, AE_TOP_TO_BOTTOMRIGHT */
1641                 dist = BLI_RCT_SIZE_Y(&sa->totrct);
1642         }
1643         
1644         /* subtractwidth of regions on opposite side 
1645          * prevents dragging regions into other opposite regions */
1646         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1647                 if (ar == scalear)
1648                         continue;
1649                 
1650                 if (scalear->alignment == RGN_ALIGN_TOP && ar->alignment == RGN_ALIGN_BOTTOM)
1651                         dist -= ar->winy;
1652                 else if (scalear->alignment == RGN_ALIGN_BOTTOM && ar->alignment == RGN_ALIGN_TOP)
1653                         dist -= ar->winy;
1654                 else if (scalear->alignment == RGN_ALIGN_LEFT && ar->alignment == RGN_ALIGN_RIGHT)
1655                         dist -= ar->winx;
1656                 else if (scalear->alignment == RGN_ALIGN_RIGHT && ar->alignment == RGN_ALIGN_LEFT)
1657                         dist -= ar->winx;
1658                 
1659                 /* case of regions in regions, like operator properties panel */
1660                 /* these can sit on top of other regions such as headers, so account for this */
1661                 else if (edge == AE_BOTTOM_TO_TOPLEFT && scalear->alignment & RGN_ALIGN_TOP &&
1662                          ar->alignment == RGN_ALIGN_TOP && ar->regiontype == RGN_TYPE_HEADER)
1663                 {
1664                         dist -= ar->winy;
1665                 }
1666                 else if (edge == AE_TOP_TO_BOTTOMRIGHT && scalear->alignment & RGN_ALIGN_BOTTOM &&
1667                          ar->alignment == RGN_ALIGN_BOTTOM && ar->regiontype == RGN_TYPE_HEADER)
1668                 {
1669                         dist -= ar->winy;
1670                 }
1671         }
1672
1673         return dist;
1674 }
1675
1676 static int region_scale_invoke(bContext *C, wmOperator *op, wmEvent *event)
1677 {
1678         sActionzoneData *sad = event->customdata;
1679         AZone *az;
1680         
1681         if (event->type != EVT_ACTIONZONE_REGION) {
1682                 BKE_report(op->reports, RPT_ERROR, "Can only scale region size from an action zone");   
1683                 return OPERATOR_CANCELLED;
1684         }
1685         
1686         az = sad->az;
1687         
1688         if (az->ar) {
1689                 RegionMoveData *rmd = MEM_callocN(sizeof(RegionMoveData), "RegionMoveData");
1690                 int maxsize;
1691                 
1692                 op->customdata = rmd;
1693                 
1694                 rmd->az = az;
1695                 rmd->ar = az->ar;
1696                 rmd->sa = sad->sa1;
1697                 rmd->edge = az->edge;
1698                 rmd->origx = event->x;
1699                 rmd->origy = event->y;
1700                 rmd->maxsize = area_max_regionsize(rmd->sa, rmd->ar, rmd->edge);
1701                 
1702                 /* if not set we do now, otherwise it uses type */
1703                 if (rmd->ar->sizex == 0)
1704                         rmd->ar->sizex = rmd->ar->type->prefsizex;
1705                 if (rmd->ar->sizey == 0)
1706                         rmd->ar->sizey = rmd->ar->type->prefsizey;
1707                 
1708                 /* now copy to regionmovedata */
1709                 if (rmd->edge == AE_LEFT_TO_TOPRIGHT || rmd->edge == AE_RIGHT_TO_TOPLEFT) {
1710                         rmd->origval = rmd->ar->sizex;
1711                 }
1712                 else {
1713                         rmd->origval = rmd->ar->sizey;
1714                 }
1715                 
1716                 /* limit headers to standard height for now */
1717                 if (rmd->ar->regiontype == RGN_TYPE_HEADER)
1718                         maxsize = rmd->ar->type->prefsizey;
1719                 else
1720                         maxsize = 1000;
1721                 
1722                 CLAMP(rmd->maxsize, 0, maxsize);
1723                 
1724                 /* add temp handler */
1725                 WM_event_add_modal_handler(C, op);
1726                 
1727                 return OPERATOR_RUNNING_MODAL;
1728         }
1729         
1730         return OPERATOR_FINISHED;
1731 }
1732
1733 static int region_scale_get_maxsize(RegionMoveData *rmd)
1734 {
1735         int maxsize = 0;
1736
1737         if (rmd->edge == AE_LEFT_TO_TOPRIGHT || rmd->edge == AE_RIGHT_TO_TOPLEFT) {
1738                 return rmd->sa->winx - UI_UNIT_X;
1739         }
1740
1741         if (rmd->ar->regiontype == RGN_TYPE_TOOL_PROPS) {
1742                 /* this calculation seems overly verbose
1743                  * can someone explain why this method is necessary? - campbell */
1744                 maxsize = rmd->maxsize - ((rmd->sa->headertype == HEADERTOP) ? UI_UNIT_Y * 2 : UI_UNIT_Y) - (UI_UNIT_Y / 4);
1745         }
1746
1747         return maxsize;
1748 }
1749
1750 static void region_scale_validate_size(RegionMoveData *rmd)
1751 {
1752         if ((rmd->ar->flag & RGN_FLAG_HIDDEN) == 0) {
1753                 short *size, maxsize = -1;
1754
1755
1756                 if (rmd->edge == AE_LEFT_TO_TOPRIGHT || rmd->edge == AE_RIGHT_TO_TOPLEFT)
1757                         size = &rmd->ar->sizex;
1758                 else
1759                         size = &rmd->ar->sizey;
1760
1761                 maxsize = region_scale_get_maxsize(rmd);
1762
1763                 if (*size > maxsize && maxsize > 0)
1764                         *size = maxsize;
1765         }
1766 }
1767
1768 static void region_scale_toggle_hidden(bContext *C, RegionMoveData *rmd)
1769 {
1770         ED_region_toggle_hidden(C, rmd->ar);
1771         region_scale_validate_size(rmd);
1772 }
1773
1774 static int region_scale_modal(bContext *C, wmOperator *op, wmEvent *event)
1775 {
1776         RegionMoveData *rmd = op->customdata;
1777         int delta;
1778         
1779         /* execute the events */
1780         switch (event->type) {
1781                 case MOUSEMOVE:
1782                         
1783                         if (rmd->edge == AE_LEFT_TO_TOPRIGHT || rmd->edge == AE_RIGHT_TO_TOPLEFT) {
1784                                 delta = event->x - rmd->origx;
1785                                 if (rmd->edge == AE_LEFT_TO_TOPRIGHT) delta = -delta;
1786                                 
1787                                 rmd->ar->sizex = rmd->origval + delta;
1788                                 CLAMP(rmd->ar->sizex, 0, rmd->maxsize);
1789                                 
1790                                 if (rmd->ar->sizex < UI_UNIT_X) {
1791                                         rmd->ar->sizex = rmd->origval;
1792                                         if (!(rmd->ar->flag & RGN_FLAG_HIDDEN))
1793                                                 region_scale_toggle_hidden(C, rmd);
1794                                 }
1795                                 else if (rmd->ar->flag & RGN_FLAG_HIDDEN)
1796                                         region_scale_toggle_hidden(C, rmd);
1797                         }
1798                         else {
1799                                 int maxsize = region_scale_get_maxsize(rmd);
1800                                 delta = event->y - rmd->origy;
1801                                 if (rmd->edge == AE_BOTTOM_TO_TOPLEFT) delta = -delta;
1802                                 
1803                                 rmd->ar->sizey = rmd->origval + delta;
1804                                 CLAMP(rmd->ar->sizey, 0, rmd->maxsize);
1805
1806                                 /* note, 'UI_UNIT_Y/4' means you need to drag the header almost
1807                                  * all the way down for it to become hidden, this is done
1808                                  * otherwise its too easy to do this by accident */
1809                                 if (rmd->ar->sizey < UI_UNIT_Y / 4) {
1810                                         rmd->ar->sizey = rmd->origval;
1811                                         if (!(rmd->ar->flag & RGN_FLAG_HIDDEN))
1812                                                 region_scale_toggle_hidden(C, rmd);
1813                                 }
1814                                 else if (maxsize > 0 && (rmd->ar->sizey > maxsize)) 
1815                                         rmd->ar->sizey = maxsize;
1816                                 else if (rmd->ar->flag & RGN_FLAG_HIDDEN)
1817                                         region_scale_toggle_hidden(C, rmd);
1818                         }
1819                         ED_area_tag_redraw(rmd->sa);
1820                         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
1821                         
1822                         break;
1823                         
1824                 case LEFTMOUSE:
1825                         if (event->val == KM_RELEASE) {
1826                                 
1827                                 if (ABS(event->x - rmd->origx) < 2 && ABS(event->y - rmd->origy) < 2) {
1828                                         if (rmd->ar->flag & RGN_FLAG_HIDDEN) {
1829                                                 region_scale_toggle_hidden(C, rmd);
1830                                         }
1831                                         else if (rmd->ar->flag & RGN_FLAG_TOO_SMALL) {
1832                                                 region_scale_validate_size(rmd);
1833                                         }
1834
1835                                         ED_area_tag_redraw(rmd->sa);
1836                                         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
1837                                 }
1838                                 MEM_freeN(op->customdata);
1839                                 op->customdata = NULL;
1840                                 
1841                                 return OPERATOR_FINISHED;
1842                         }
1843                         break;
1844                         
1845                 case ESCKEY:
1846                         ;
1847         }
1848         
1849         return OPERATOR_RUNNING_MODAL;
1850 }
1851
1852 static int region_scale_cancel(bContext *UNUSED(C), wmOperator *op)
1853 {
1854         MEM_freeN(op->customdata);
1855         op->customdata = NULL;
1856
1857         return OPERATOR_CANCELLED;
1858 }
1859
1860 static void SCREEN_OT_region_scale(wmOperatorType *ot)
1861 {
1862         /* identifiers */
1863         ot->name = "Scale Region Size";
1864         ot->description = "Scale selected area";
1865         ot->idname = "SCREEN_OT_region_scale";
1866         
1867         ot->invoke = region_scale_invoke;
1868         ot->modal = region_scale_modal;
1869         ot->cancel = region_scale_cancel;
1870         
1871         ot->poll = ED_operator_areaactive;
1872         
1873         ot->flag = OPTYPE_BLOCKING;
1874 }
1875
1876
1877 /* ************** frame change operator ***************************** */
1878
1879 /* function to be called outside UI context, or for redo */
1880 static int frame_offset_exec(bContext *C, wmOperator *op)
1881 {
1882         Main *bmain = CTX_data_main(C);
1883         Scene *scene = CTX_data_scene(C);
1884         int delta;
1885         
1886         delta = RNA_int_get(op->ptr, "delta");
1887
1888         CFRA += delta;
1889         FRAMENUMBER_MIN_CLAMP(CFRA);
1890         SUBFRA = 0.f;
1891         
1892         sound_seek_scene(bmain, scene);
1893
1894         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, CTX_data_scene(C));
1895         
1896         return OPERATOR_FINISHED;
1897 }
1898
1899 static void SCREEN_OT_frame_offset(wmOperatorType *ot)
1900 {
1901         ot->name = "Frame Offset";
1902         ot->idname = "SCREEN_OT_frame_offset";
1903         ot->description = "Move current frame forward/backward by a given number";
1904         
1905         ot->exec = frame_offset_exec;
1906         
1907         ot->poll = ED_operator_screenactive_norender;
1908         ot->flag = 0;
1909         
1910         /* rna */
1911         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1912 }
1913
1914
1915 /* function to be called outside UI context, or for redo */
1916 static int frame_jump_exec(bContext *C, wmOperator *op)
1917 {
1918         Main *bmain = CTX_data_main(C);
1919         Scene *scene = CTX_data_scene(C);
1920         wmTimer *animtimer = CTX_wm_screen(C)->animtimer;
1921
1922         /* Don't change CFRA directly if animtimer is running as this can cause
1923          * first/last frame not to be actually shown (bad since for example physics
1924          * simulations aren't reset properly).
1925          */
1926         if (animtimer) {
1927                 ScreenAnimData *sad = animtimer->customdata;
1928                 
1929                 sad->flag |= ANIMPLAY_FLAG_USE_NEXT_FRAME;
1930                 
1931                 if (RNA_boolean_get(op->ptr, "end"))
1932                         sad->nextfra = PEFRA;
1933                 else
1934                         sad->nextfra = PSFRA;
1935         }
1936         else {
1937                 if (RNA_boolean_get(op->ptr, "end"))
1938                         CFRA = PEFRA;
1939                 else
1940                         CFRA = PSFRA;
1941                 
1942                 sound_seek_scene(bmain, scene);
1943
1944                 WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
1945         }
1946         
1947         return OPERATOR_FINISHED;
1948 }
1949
1950 static void SCREEN_OT_frame_jump(wmOperatorType *ot)
1951 {
1952         ot->name = "Jump to Endpoint";
1953         ot->description = "Jump to first/last frame in frame range";
1954         ot->idname = "SCREEN_OT_frame_jump";
1955         
1956         ot->exec = frame_jump_exec;
1957         
1958         ot->poll = ED_operator_screenactive_norender;
1959         ot->flag = OPTYPE_UNDO;
1960         
1961         /* rna */
1962         RNA_def_boolean(ot->srna, "end", 0, "Last Frame", "Jump to the last frame of the frame range");
1963 }
1964
1965
1966 /* ************** jump to keyframe operator ***************************** */
1967
1968 /* function to be called outside UI context, or for redo */
1969 static int keyframe_jump_exec(bContext *C, wmOperator *op)
1970 {
1971         Main *bmain = CTX_data_main(C);
1972         Scene *scene = CTX_data_scene(C);
1973         Object *ob = CTX_data_active_object(C);
1974         bDopeSheet ads = {NULL};
1975         DLRBT_Tree keys;
1976         ActKeyColumn *ak;
1977         float cfra;
1978         short next = RNA_boolean_get(op->ptr, "next");
1979         short done = FALSE;
1980         
1981         /* sanity checks */
1982         if (scene == NULL)
1983                 return OPERATOR_CANCELLED;
1984
1985         cfra = (float)(CFRA);
1986
1987         /* init binarytree-list for getting keyframes */
1988         BLI_dlrbTree_init(&keys);
1989         
1990         /* populate tree with keyframe nodes */
1991         scene_to_keylist(&ads, scene, &keys, NULL);
1992
1993         if (ob)
1994                 ob_to_keylist(&ads, ob, &keys, NULL);
1995
1996         {
1997                 Mask *mask = CTX_data_edit_mask(C);
1998                 if (mask) {
1999                         MaskLayer *masklay = BKE_mask_layer_active(mask);
2000                         mask_to_keylist(&ads, masklay, &keys);
2001                 }
2002         }
2003
2004         /* build linked-list for searching */
2005         BLI_dlrbTree_linkedlist_sync(&keys);
2006         
2007         /* find matching keyframe in the right direction */
2008         do {
2009                 if (next)
2010                         ak = (ActKeyColumn *)BLI_dlrbTree_search_next(&keys, compare_ak_cfraPtr, &cfra);
2011                 else
2012                         ak = (ActKeyColumn *)BLI_dlrbTree_search_prev(&keys, compare_ak_cfraPtr, &cfra);
2013                 
2014                 if (ak) {
2015                         if (CFRA != (int)ak->cfra) {
2016                                 /* this changes the frame, so set the frame and we're done */
2017                                 CFRA = (int)ak->cfra;
2018                                 done = TRUE;
2019                         }
2020                         else {
2021                                 /* make this the new starting point for the search */
2022                                 cfra = ak->cfra;
2023                         }
2024                 }
2025         } while ((ak != NULL) && (done == FALSE));
2026
2027         /* free temp stuff */
2028         BLI_dlrbTree_free(&keys);
2029
2030         /* any success? */
2031         if (done == FALSE) {
2032                 BKE_report(op->reports, RPT_INFO, "No more keyframes to jump to in this direction");
2033
2034                 return OPERATOR_CANCELLED;
2035         }
2036         else {
2037                 sound_seek_scene(bmain, scene);
2038
2039                 WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2040
2041                 return OPERATOR_FINISHED;
2042         }
2043 }
2044
2045 static void SCREEN_OT_keyframe_jump(wmOperatorType *ot)
2046 {
2047         ot->name = "Jump to Keyframe";
2048         ot->description = "Jump to previous/next keyframe";
2049         ot->idname = "SCREEN_OT_keyframe_jump";
2050         
2051         ot->exec = keyframe_jump_exec;
2052         
2053         ot->poll = ED_operator_screenactive_norender;
2054         ot->flag = OPTYPE_UNDO;
2055         
2056         /* properties */
2057         RNA_def_boolean(ot->srna, "next", TRUE, "Next Keyframe", "");
2058 }
2059
2060 /* ************** switch screen operator ***************************** */
2061
2062
2063 /* function to be called outside UI context, or for redo */
2064 static int screen_set_exec(bContext *C, wmOperator *op)
2065 {
2066         bScreen *screen = CTX_wm_screen(C);
2067         bScreen *screen_prev = screen;
2068         
2069         ScrArea *sa = CTX_wm_area(C);
2070         int tot = BLI_countlist(&CTX_data_main(C)->screen);
2071         int delta = RNA_int_get(op->ptr, "delta");
2072         
2073         /* temp screens are for userpref or render display */
2074         if (screen->temp)
2075                 return OPERATOR_CANCELLED;
2076         
2077         if (delta == 1) {
2078                 while (tot--) {
2079                         screen = screen->id.next;
2080                         if (screen == NULL) screen = CTX_data_main(C)->screen.first;
2081                         if (screen->winid == 0 && screen->full == 0 && screen != screen_prev)
2082                                 break;
2083                 }
2084         }
2085         else if (delta == -1) {
2086                 while (tot--) {
2087                         screen = screen->id.prev;
2088                         if (screen == NULL) screen = CTX_data_main(C)->screen.last;
2089                         if (screen->winid == 0 && screen->full == 0 && screen != screen_prev)
2090                                 break;
2091                 }
2092         }
2093         else {
2094                 screen = NULL;
2095         }
2096         
2097         if (screen && screen_prev != screen) {
2098                 /* return to previous state before switching screens */
2099                 if (sa && sa->full) {
2100                         ED_screen_full_restore(C, sa); /* may free 'screen_prev' */
2101                 }
2102                 
2103                 ED_screen_set(C, screen);
2104                 return OPERATOR_FINISHED;
2105         }
2106         return OPERATOR_CANCELLED;
2107 }
2108
2109 static void SCREEN_OT_screen_set(wmOperatorType *ot)
2110 {
2111         ot->name = "Set Screen";
2112         ot->description = "Cycle through available screens";
2113         ot->idname = "SCREEN_OT_screen_set";
2114         
2115         ot->exec = screen_set_exec;
2116         ot->poll = ED_operator_screenactive;
2117         
2118         /* rna */
2119         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
2120 }
2121
2122 /* ************** screen full-area operator ***************************** */
2123
2124
2125 /* function to be called outside UI context, or for redo */
2126 static int screen_full_area_exec(bContext *C, wmOperator *UNUSED(op))
2127 {
2128         bScreen *screen = CTX_wm_screen(C);
2129         ScrArea *sa = NULL;
2130         
2131         /* search current screen for 'fullscreen' areas */
2132         /* prevents restoring info header, when mouse is over it */
2133         for (sa = screen->areabase.first; sa; sa = sa->next) {
2134                 if (sa->full) break;
2135         }
2136         
2137         if (sa == NULL) sa = CTX_wm_area(C);
2138         
2139         ED_screen_full_toggle(C, CTX_wm_window(C), sa);
2140         return OPERATOR_FINISHED;
2141 }
2142
2143 static void SCREEN_OT_screen_full_area(wmOperatorType *ot)
2144 {
2145         ot->name = "Toggle Full Screen";
2146         ot->description = "Toggle display selected area as fullscreen";
2147         ot->idname = "SCREEN_OT_screen_full_area";
2148         
2149         ot->exec = screen_full_area_exec;
2150         ot->poll = ED_operator_areaactive;
2151         ot->flag = 0;
2152         
2153 }
2154
2155
2156
2157 /* ************** join area operator ********************************************** */
2158
2159 /* operator state vars used:  
2160  * x1, y1     mouse coord in first area, which will disappear
2161  * x2, y2     mouse coord in 2nd area, which will become joined
2162  * 
2163  * functions:
2164  * 
2165  * init()   find edge based on state vars 
2166  * test if the edge divides two areas, 
2167  * store active and nonactive area,
2168  * 
2169  * apply()  do the actual join
2170  * 
2171  * exit()       cleanup, send notifier
2172  * 
2173  * callbacks:
2174  * 
2175  * exec()       calls init, apply, exit 
2176  * 
2177  * invoke() sets mouse coords in x,y
2178  * call init()
2179  * add modal handler
2180  * 
2181  * modal()      accept modal events while doing it
2182  * call apply() with active window and nonactive window
2183  * call exit() and remove handler when LMB confirm
2184  */
2185
2186 typedef struct sAreaJoinData {
2187         ScrArea *sa1;   /* first area to be considered */
2188         ScrArea *sa2;   /* second area to be considered */
2189         ScrArea *scr;   /* designed for removal */
2190
2191 } sAreaJoinData;
2192
2193
2194 /* validate selection inside screen, set variables OK */
2195 /* return 0: init failed */
2196 /* XXX todo: find edge based on (x,y) and set other area? */
2197 static int area_join_init(bContext *C, wmOperator *op)
2198 {
2199         ScrArea *sa1, *sa2;
2200         sAreaJoinData *jd = NULL;
2201         int x1, y1;
2202         int x2, y2;
2203         int shared = 0;
2204         
2205         /* required properties, make negative to get return 0 if not set by caller */
2206         x1 = RNA_int_get(op->ptr, "min_x");
2207         y1 = RNA_int_get(op->ptr, "min_y");
2208         x2 = RNA_int_get(op->ptr, "max_x");
2209         y2 = RNA_int_get(op->ptr, "max_y");
2210         
2211         sa1 = screen_areahascursor(CTX_wm_screen(C), x1, y1);
2212         sa2 = screen_areahascursor(CTX_wm_screen(C), x2, y2);
2213         if (sa1 == NULL || sa2 == NULL || sa1 == sa2)
2214                 return 0;
2215         
2216         /* do areas share an edge? */
2217         if (sa1->v1 == sa2->v1 || sa1->v1 == sa2->v2 || sa1->v1 == sa2->v3 || sa1->v1 == sa2->v4) shared++;
2218         if (sa1->v2 == sa2->v1 || sa1->v2 == sa2->v2 || sa1->v2 == sa2->v3 || sa1->v2 == sa2->v4) shared++;
2219         if (sa1->v3 == sa2->v1 || sa1->v3 == sa2->v2 || sa1->v3 == sa2->v3 || sa1->v3 == sa2->v4) shared++;
2220         if (sa1->v4 == sa2->v1 || sa1->v4 == sa2->v2 || sa1->v4 == sa2->v3 || sa1->v4 == sa2->v4) shared++;
2221         if (shared != 2) {
2222                 printf("areas don't share edge\n");
2223                 return 0;
2224         }
2225         
2226         jd = (sAreaJoinData *)MEM_callocN(sizeof(sAreaJoinData), "op_area_join");
2227         
2228         jd->sa1 = sa1;
2229         jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2230         jd->sa2 = sa2;
2231         jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2232         
2233         op->customdata = jd;
2234         
2235         return 1;
2236 }
2237
2238 /* apply the join of the areas (space types) */
2239 static int area_join_apply(bContext *C, wmOperator *op)
2240 {
2241         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2242         if (!jd) return 0;
2243         
2244         if (!screen_area_join(C, CTX_wm_screen(C), jd->sa1, jd->sa2)) {
2245                 return 0;
2246         }
2247         if (CTX_wm_area(C) == jd->sa2) {
2248                 CTX_wm_area_set(C, NULL);
2249                 CTX_wm_region_set(C, NULL);
2250         }
2251         
2252         return 1;
2253 }
2254
2255 /* finish operation */
2256 static void area_join_exit(bContext *C, wmOperator *op)
2257 {
2258         if (op->customdata) {
2259                 MEM_freeN(op->customdata);
2260                 op->customdata = NULL;
2261         }
2262         
2263         /* this makes sure aligned edges will result in aligned grabbing */
2264         removedouble_scredges(CTX_wm_screen(C));
2265         removenotused_scredges(CTX_wm_screen(C));
2266         removenotused_scrverts(CTX_wm_screen(C));
2267 }
2268
2269 static int area_join_exec(bContext *C, wmOperator *op)
2270 {
2271         if (!area_join_init(C, op)) 
2272                 return OPERATOR_CANCELLED;
2273         
2274         area_join_apply(C, op);
2275         area_join_exit(C, op);
2276         
2277         return OPERATOR_FINISHED;
2278 }
2279
2280 /* interaction callback */
2281 static int area_join_invoke(bContext *C, wmOperator *op, wmEvent *event)
2282 {
2283         
2284         if (event->type == EVT_ACTIONZONE_AREA) {
2285                 sActionzoneData *sad = event->customdata;
2286                 
2287                 if (sad->modifier > 0) {
2288                         return OPERATOR_PASS_THROUGH;
2289                 }
2290                 
2291                 /* verify *sad itself */
2292                 if (sad == NULL || sad->sa1 == NULL || sad->sa2 == NULL)
2293                         return OPERATOR_PASS_THROUGH;
2294                 
2295                 /* is this our *sad? if areas equal it should be passed on */
2296                 if (sad->sa1 == sad->sa2)
2297                         return OPERATOR_PASS_THROUGH;
2298                 
2299                 /* prepare operator state vars */
2300                 RNA_int_set(op->ptr, "min_x", sad->x);
2301                 RNA_int_set(op->ptr, "min_y", sad->y);
2302                 RNA_int_set(op->ptr, "max_x", event->x);
2303                 RNA_int_set(op->ptr, "max_y", event->y);
2304         }
2305         
2306         
2307         if (!area_join_init(C, op)) 
2308                 return OPERATOR_PASS_THROUGH;
2309         
2310         /* add temp handler */
2311         WM_event_add_modal_handler(C, op);
2312         
2313         return OPERATOR_RUNNING_MODAL;
2314 }
2315
2316 static int area_join_cancel(bContext *C, wmOperator *op)
2317 {
2318         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2319         
2320         if (jd->sa1) {
2321                 jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2322                 jd->sa1->flag &= ~AREA_FLAG_DRAWJOINTO;
2323         }
2324         if (jd->sa2) {
2325                 jd->sa2->flag &= ~AREA_FLAG_DRAWJOINFROM;
2326                 jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2327         }
2328         
2329         WM_event_add_notifier(C, NC_WINDOW, NULL);
2330         
2331         area_join_exit(C, op);
2332         
2333         return OPERATOR_CANCELLED;
2334 }
2335
2336 /* modal callback while selecting area (space) that will be removed */
2337 static int area_join_modal(bContext *C, wmOperator *op, wmEvent *event)
2338 {
2339         bScreen *sc = CTX_wm_screen(C);
2340         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2341         
2342         /* execute the events */
2343         switch (event->type) {
2344                         
2345                 case MOUSEMOVE: 
2346                 {
2347                         ScrArea *sa = screen_areahascursor(sc, event->x, event->y);
2348                         int dir;
2349                         
2350                         if (sa) {                                       
2351                                 if (jd->sa1 != sa) {
2352                                         dir = area_getorientation(jd->sa1, sa);
2353                                         if (dir >= 0) {
2354                                                 if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2355                                                 jd->sa2 = sa;
2356                                                 jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2357                                         } 
2358                                         else {
2359                                                 /* we are not bordering on the previously selected area 
2360                                                  * we check if area has common border with the one marked for removal
2361                                                  * in this case we can swap areas.
2362                                                  */
2363                                                 dir = area_getorientation(sa, jd->sa2);
2364                                                 if (dir >= 0) {
2365                                                         if (jd->sa1) jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2366                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2367                                                         jd->sa1 = jd->sa2;
2368                                                         jd->sa2 = sa;
2369                                                         if (jd->sa1) jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2370                                                         if (jd->sa2) jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2371                                                 } 
2372                                                 else {
2373                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2374                                                         jd->sa2 = NULL;
2375                                                 }
2376                                         }
2377                                         WM_event_add_notifier(C, NC_WINDOW, NULL);
2378                                 } 
2379                                 else {
2380                                         /* we are back in the area previously selected for keeping 
2381                                          * we swap the areas if possible to allow user to choose */
2382                                         if (jd->sa2 != NULL) {
2383                                                 if (jd->sa1) jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2384                                                 if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2385                                                 jd->sa1 = jd->sa2;
2386                                                 jd->sa2 = sa;
2387                                                 if (jd->sa1) jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2388                                                 if (jd->sa2) jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2389                                                 dir = area_getorientation(jd->sa1, jd->sa2);
2390                                                 if (dir < 0) {
2391                                                         printf("oops, didn't expect that!\n");
2392                                                 }
2393                                         } 
2394                                         else {
2395                                                 dir = area_getorientation(jd->sa1, sa);
2396                                                 if (dir >= 0) {
2397                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2398                                                         jd->sa2 = sa;
2399                                                         jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2400                                                 }
2401                                         }
2402                                         WM_event_add_notifier(C, NC_WINDOW, NULL);
2403                                 }
2404                         }
2405                 }
2406                 break;
2407                 case LEFTMOUSE:
2408                         if (event->val == KM_RELEASE) {
2409                                 ED_area_tag_redraw(jd->sa1);
2410                                 ED_area_tag_redraw(jd->sa2);
2411
2412                                 area_join_apply(C, op);
2413                                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2414                                 area_join_exit(C, op);
2415                                 return OPERATOR_FINISHED;
2416                         }
2417                         break;
2418                         
2419                 case RIGHTMOUSE:
2420                 case ESCKEY:
2421                         return area_join_cancel(C, op);
2422         }
2423         
2424         return OPERATOR_RUNNING_MODAL;
2425 }
2426
2427 /* Operator for joining two areas (space types) */
2428 static void SCREEN_OT_area_join(wmOperatorType *ot)
2429 {
2430         /* identifiers */
2431         ot->name = "Join Area";
2432         ot->description = "Join selected areas into new window";
2433         ot->idname = "SCREEN_OT_area_join";
2434         
2435         /* api callbacks */
2436         ot->exec = area_join_exec;
2437         ot->invoke = area_join_invoke;
2438         ot->modal = area_join_modal;
2439         ot->poll = screen_active_editable;
2440         ot->cancel = area_join_cancel;
2441         
2442         ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL;
2443         
2444         /* rna */
2445         RNA_def_int(ot->srna, "min_x", -100, INT_MIN, INT_MAX, "X 1", "", INT_MIN, INT_MAX);
2446         RNA_def_int(ot->srna, "min_y", -100, INT_MIN, INT_MAX, "Y 1", "", INT_MIN, INT_MAX);
2447         RNA_def_int(ot->srna, "max_x", -100, INT_MIN, INT_MAX, "X 2", "", INT_MIN, INT_MAX);
2448         RNA_def_int(ot->srna, "max_y", -100, INT_MIN, INT_MAX, "Y 2", "", INT_MIN, INT_MAX);
2449 }
2450
2451 /* ******************************* */
2452
2453 static int screen_area_options_invoke(bContext *C, wmOperator *op, wmEvent *event)
2454 {
2455         uiPopupMenu *pup;
2456         uiLayout *layout;
2457         PointerRNA ptr1, ptr2;
2458         ScrEdge *actedge = screen_find_active_scredge(CTX_wm_screen(C), event->x, event->y);
2459         
2460         if (actedge == NULL) return OPERATOR_CANCELLED;
2461         
2462         pup = uiPupMenuBegin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2463         layout = uiPupMenuLayout(pup);
2464         
2465         WM_operator_properties_create(&ptr1, "SCREEN_OT_area_join");
2466         
2467         /* mouse cursor on edge, '4' can fail on wide edges... */
2468         RNA_int_set(&ptr1, "min_x", event->x + 4);
2469         RNA_int_set(&ptr1, "min_y", event->y + 4);
2470         RNA_int_set(&ptr1, "max_x", event->x - 4);
2471         RNA_int_set(&ptr1, "max_y", event->y - 4);
2472         
2473         WM_operator_properties_create(&ptr2, "SCREEN_OT_area_split");
2474         
2475         /* store initial mouse cursor position */
2476         RNA_int_set(&ptr2, "mouse_x", event->x);
2477         RNA_int_set(&ptr2, "mouse_y", event->y);
2478         
2479         uiItemFullO(layout, "SCREEN_OT_area_split", NULL, ICON_NONE, ptr2.data, WM_OP_INVOKE_DEFAULT, 0);
2480         uiItemFullO(layout, "SCREEN_OT_area_join", NULL, ICON_NONE, ptr1.data, WM_OP_INVOKE_DEFAULT, 0);
2481         
2482         uiPupMenuEnd(C, pup);
2483         
2484         return OPERATOR_CANCELLED;
2485 }
2486
2487 static void SCREEN_OT_area_options(wmOperatorType *ot)
2488 {
2489         /* identifiers */
2490         ot->name = "Area Options";
2491         ot->description = "Operations for splitting and merging";
2492         ot->idname = "SCREEN_OT_area_options";
2493         
2494         /* api callbacks */
2495         ot->invoke = screen_area_options_invoke;
2496         
2497         ot->poll = ED_operator_screen_mainwinactive;
2498 }
2499
2500
2501 /* ******************************* */
2502
2503
2504 static int spacedata_cleanup(bContext *C, wmOperator *op)
2505 {
2506         Main *bmain = CTX_data_main(C);
2507         bScreen *screen;
2508         ScrArea *sa;
2509         int tot = 0;
2510         
2511         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
2512                 for (sa = screen->areabase.first; sa; sa = sa->next) {
2513                         if (sa->spacedata.first != sa->spacedata.last) {
2514                                 SpaceLink *sl = sa->spacedata.first;
2515
2516                                 BLI_remlink(&sa->spacedata, sl);
2517                                 tot += BLI_countlist(&sa->spacedata);
2518                                 BKE_spacedata_freelist(&sa->spacedata);
2519                                 BLI_addtail(&sa->spacedata, sl);
2520                         }
2521                 }
2522         }
2523         BKE_reportf(op->reports, RPT_INFO, "Removed amount of editors: %d", tot);
2524         
2525         return OPERATOR_FINISHED;
2526 }
2527
2528 static void SCREEN_OT_spacedata_cleanup(wmOperatorType *ot)
2529 {
2530         /* identifiers */
2531         ot->name = "Clean-up space-data";
2532         ot->description = "Remove unused settings for invisible editors";
2533         ot->idname = "SCREEN_OT_spacedata_cleanup";
2534         
2535         /* api callbacks */
2536         ot->exec = spacedata_cleanup;
2537         ot->poll = WM_operator_winactive;
2538         
2539 }
2540
2541 /* ************** repeat last operator ***************************** */
2542
2543 static int repeat_last_exec(bContext *C, wmOperator *UNUSED(op))
2544 {
2545         wmOperator *lastop = CTX_wm_manager(C)->operators.last;
2546         
2547         if (lastop)
2548                 WM_operator_repeat(C, lastop);
2549         
2550         return OPERATOR_CANCELLED;
2551 }
2552
2553 static void SCREEN_OT_repeat_last(wmOperatorType *ot)
2554 {
2555         /* identifiers */
2556         ot->name = "Repeat Last";
2557         ot->description = "Repeat last action";
2558         ot->idname = "SCREEN_OT_repeat_last";
2559         
2560         /* api callbacks */
2561         ot->exec = repeat_last_exec;
2562         
2563         ot->poll = ED_operator_screenactive;
2564         
2565 }
2566
2567 static int repeat_history_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2568 {
2569         wmWindowManager *wm = CTX_wm_manager(C);
2570         wmOperator *lastop;
2571         uiPopupMenu *pup;
2572         uiLayout *layout;
2573         int items, i;
2574         
2575         items = BLI_countlist(&wm->operators);
2576         if (items == 0)
2577                 return OPERATOR_CANCELLED;
2578         
2579         pup = uiPupMenuBegin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2580         layout = uiPupMenuLayout(pup);
2581         
2582         for (i = items - 1, lastop = wm->operators.last; lastop; lastop = lastop->prev, i--)
2583                 uiItemIntO(layout, RNA_struct_ui_name(lastop->type->srna), ICON_NONE, op->type->idname, "index", i);
2584         
2585         uiPupMenuEnd(C, pup);
2586         
2587         return OPERATOR_CANCELLED;
2588 }
2589
2590 static int repeat_history_exec(bContext *C, wmOperator *op)
2591 {
2592         wmWindowManager *wm = CTX_wm_manager(C);
2593         
2594         op = BLI_findlink(&wm->operators, RNA_int_get(op->ptr, "index"));
2595         if (op) {
2596                 /* let's put it as last operator in list */
2597                 BLI_remlink(&wm->operators, op);
2598                 BLI_addtail(&wm->operators, op);
2599                 
2600                 WM_operator_repeat(C, op);
2601         }
2602         
2603         return OPERATOR_FINISHED;
2604 }
2605
2606 static void SCREEN_OT_repeat_history(wmOperatorType *ot)
2607 {
2608         /* identifiers */
2609         ot->name = "Repeat History";
2610         ot->description = "Display menu for previous actions performed";
2611         ot->idname = "SCREEN_OT_repeat_history";
2612         
2613         /* api callbacks */
2614         ot->invoke = repeat_history_invoke;
2615         ot->exec = repeat_history_exec;
2616         
2617         ot->poll = ED_operator_screenactive;
2618         
2619         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, 1000);
2620 }
2621
2622 /* ********************** redo operator ***************************** */
2623
2624 static int redo_last_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
2625 {
2626         wmOperator *lastop = WM_operator_last_redo(C);
2627         
2628         if (lastop)
2629                 WM_operator_redo_popup(C, lastop);
2630         
2631         return OPERATOR_CANCELLED;
2632 }
2633
2634 static void SCREEN_OT_redo_last(wmOperatorType *ot)
2635 {
2636         /* identifiers */
2637         ot->name = "Redo Last";
2638         ot->description = "Display menu for last action performed";
2639         ot->idname = "SCREEN_OT_redo_last";
2640         
2641         /* api callbacks */
2642         ot->invoke = redo_last_invoke;
2643         
2644         ot->poll = ED_operator_screenactive;
2645 }
2646
2647 /* ************** region four-split operator ***************************** */
2648
2649 static void view3d_localview_update_rv3d(struct RegionView3D *rv3d)
2650 {
2651         if (rv3d->localvd) {
2652                 rv3d->localvd->view = rv3d->view;
2653                 rv3d->localvd->persp = rv3d->persp;
2654                 copy_qt_qt(rv3d->localvd->viewquat, rv3d->viewquat);
2655         }
2656 }
2657
2658 /* insert a region in the area region list */
2659 static int region_quadview_exec(bContext *C, wmOperator *op)
2660 {
2661         ARegion *ar = CTX_wm_region(C);
2662         
2663         /* some rules... */
2664         if (ar->regiontype != RGN_TYPE_WINDOW)
2665                 BKE_report(op->reports, RPT_ERROR, "Only window region can be 4-splitted");
2666         else if (ar->alignment == RGN_ALIGN_QSPLIT) {
2667                 ScrArea *sa = CTX_wm_area(C);
2668                 ARegion *arn;
2669                 
2670                 /* keep current region */
2671                 ar->alignment = 0;
2672                 
2673                 if (sa->spacetype == SPACE_VIEW3D) {
2674                         RegionView3D *rv3d = ar->regiondata;
2675                         rv3d->viewlock = 0;
2676                         rv3d->rflag &= ~RV3D_CLIPPING;
2677                 }
2678                 
2679                 for (ar = sa->regionbase.first; ar; ar = arn) {
2680                         arn = ar->next;
2681                         if (ar->alignment == RGN_ALIGN_QSPLIT) {
2682                                 ED_region_exit(C, ar);
2683                                 BKE_area_region_free(sa->type, ar);
2684                                 BLI_remlink(&sa->regionbase, ar);
2685                                 MEM_freeN(ar);
2686                         }
2687                 }
2688                 ED_area_tag_redraw(sa);
2689                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2690         }
2691         else if (ar->next)
2692                 BKE_report(op->reports, RPT_ERROR, "Only last region can be 4-splitted");
2693         else {
2694                 ScrArea *sa = CTX_wm_area(C);
2695                 ARegion *newar;
2696                 int count;
2697                 
2698                 ar->alignment = RGN_ALIGN_QSPLIT;
2699                 
2700                 for (count = 0; count < 3; count++) {
2701                         newar = BKE_area_region_copy(sa->type, ar);
2702                         BLI_addtail(&sa->regionbase, newar);
2703                 }
2704                 
2705                 /* lock views and set them */
2706                 if (sa->spacetype == SPACE_VIEW3D) {
2707                         /* run ED_view3d_lock() so the correct 'rv3d->viewquat' is set,
2708                          * otherwise when restoring rv3d->localvd the 'viewquat' won't
2709                          * match the 'view', set on entering localview See: [#26315],
2710                          *
2711                          * We could avoid manipulating rv3d->localvd here if exiting
2712                          * localview with a 4-split would assign these view locks */
2713                         RegionView3D *rv3d;
2714
2715                         rv3d = ar->regiondata;
2716                         rv3d->viewlock = RV3D_LOCKED; rv3d->view = RV3D_VIEW_FRONT; rv3d->persp = RV3D_ORTHO;
2717                         ED_view3d_lock(rv3d);
2718                         view3d_localview_update_rv3d(rv3d);
2719                         
2720                         ar = ar->next;
2721                         rv3d = ar->regiondata;
2722                         rv3d->viewlock = RV3D_LOCKED; rv3d->view = RV3D_VIEW_TOP; rv3d->persp = RV3D_ORTHO;
2723                         ED_view3d_lock(rv3d);
2724                         view3d_localview_update_rv3d(rv3d);
2725                         
2726                         ar = ar->next;
2727                         rv3d = ar->regiondata;
2728                         rv3d->viewlock = RV3D_LOCKED; rv3d->view = RV3D_VIEW_RIGHT; rv3d->persp = RV3D_ORTHO;
2729                         ED_view3d_lock(rv3d);
2730                         view3d_localview_update_rv3d(rv3d);
2731                         
2732                         ar = ar->next;
2733                         rv3d = ar->regiondata;
2734                         rv3d->view = RV3D_VIEW_CAMERA; rv3d->persp = RV3D_CAMOB;
2735                         ED_view3d_lock(rv3d);
2736                         view3d_localview_update_rv3d(rv3d);
2737                 }
2738                 ED_area_tag_redraw(sa);
2739                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2740         }
2741         
2742         
2743         return OPERATOR_FINISHED;
2744 }
2745
2746 static void SCREEN_OT_region_quadview(wmOperatorType *ot)
2747 {
2748         /* identifiers */
2749         ot->name = "Toggle Quad View";
2750         ot->description = "Split selected area into camera, front, right & top views";
2751         ot->idname = "SCREEN_OT_region_quadview";
2752         
2753         /* api callbacks */
2754         //      ot->invoke = WM_operator_confirm;
2755         ot->exec = region_quadview_exec;
2756         ot->poll = ED_operator_region_view3d_active;
2757         ot->flag = 0;
2758 }
2759
2760
2761
2762 /* ************** region flip operator ***************************** */
2763
2764 /* flip a region alignment */
2765 static int region_flip_exec(bContext *C, wmOperator *UNUSED(op))
2766 {
2767         ARegion *ar = CTX_wm_region(C);
2768         
2769         if (!ar)
2770                 return OPERATOR_CANCELLED;
2771         
2772         if (ar->alignment == RGN_ALIGN_TOP)
2773                 ar->alignment = RGN_ALIGN_BOTTOM;
2774         else if (ar->alignment == RGN_ALIGN_BOTTOM)
2775                 ar->alignment = RGN_ALIGN_TOP;
2776         else if (ar->alignment == RGN_ALIGN_LEFT)
2777                 ar->alignment = RGN_ALIGN_RIGHT;
2778         else if (ar->alignment == RGN_ALIGN_RIGHT)
2779                 ar->alignment = RGN_ALIGN_LEFT;
2780
2781         ED_area_tag_redraw(CTX_wm_area(C));
2782         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2783         
2784         return OPERATOR_FINISHED;
2785 }
2786
2787
2788 static void SCREEN_OT_region_flip(wmOperatorType *ot)
2789 {
2790         /* identifiers */
2791         ot->name = "Flip Region";
2792         ot->idname = "SCREEN_OT_region_flip";
2793         ot->description = "Toggle the region's alignment (left/right or top/bottom)";
2794         
2795         /* api callbacks */
2796         ot->exec = region_flip_exec;
2797         ot->poll = ED_operator_areaactive;
2798         ot->flag = 0;
2799 }
2800
2801 /* ************** header flip operator ***************************** */
2802
2803 /* flip a header region alignment */
2804 static int header_flip_exec(bContext *C, wmOperator *UNUSED(op))
2805 {
2806         ARegion *ar = CTX_wm_region(C);
2807         
2808         /* find the header region 
2809          *      - try context first, but upon failing, search all regions in area...
2810          */
2811         if ((ar == NULL) || (ar->regiontype != RGN_TYPE_HEADER)) {
2812                 ScrArea *sa = CTX_wm_area(C);
2813                 ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
2814
2815                 /* don't do anything if no region */
2816                 if (ar == NULL)
2817                         return OPERATOR_CANCELLED;
2818         }       
2819         
2820         /* copied from SCREEN_OT_region_flip */
2821         if (ar->alignment == RGN_ALIGN_TOP)
2822                 ar->alignment = RGN_ALIGN_BOTTOM;
2823         else if (ar->alignment == RGN_ALIGN_BOTTOM)
2824                 ar->alignment = RGN_ALIGN_TOP;
2825         else if (ar->alignment == RGN_ALIGN_LEFT)
2826                 ar->alignment = RGN_ALIGN_RIGHT;
2827         else if (ar->alignment == RGN_ALIGN_RIGHT)
2828                 ar->alignment = RGN_ALIGN_LEFT;
2829
2830         ED_area_tag_redraw(CTX_wm_area(C));
2831
2832         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2833         
2834         return OPERATOR_FINISHED;
2835 }
2836
2837
2838 static void SCREEN_OT_header_flip(wmOperatorType *ot)
2839 {
2840         /* identifiers */
2841         ot->name = "Flip Header Region";
2842         ot->idname = "SCREEN_OT_header_flip";
2843         ot->description = "Toggle the header over/below the main window area";
2844         
2845         /* api callbacks */
2846         ot->exec = header_flip_exec;
2847         
2848         ot->poll = ED_operator_areaactive;
2849         ot->flag = 0;
2850 }
2851
2852 /* ************** header tools operator ***************************** */
2853
2854 static int header_toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
2855 {
2856         ScrArea *sa = CTX_wm_area(C);
2857         ARegion *ar = CTX_wm_region(C);
2858         uiPopupMenu *pup;
2859         uiLayout *layout;
2860         
2861         pup = uiPupMenuBegin(C, "Header", ICON_NONE);
2862         layout = uiPupMenuLayout(pup);
2863         
2864         // XXX SCREEN_OT_region_flip doesn't work - gets wrong context for active region, so added custom operator
2865         if (ar->alignment == RGN_ALIGN_TOP)
2866                 uiItemO(layout, "Flip to Bottom", ICON_NONE, "SCREEN_OT_header_flip");
2867         else
2868                 uiItemO(layout, "Flip to Top", ICON_NONE, "SCREEN_OT_header_flip");
2869         
2870         uiItemS(layout);
2871         
2872         /* file browser should be fullscreen all the time, but other regions can be maximized/restored... */
2873         if (sa->spacetype != SPACE_FILE) {
2874                 if (sa->full) 
2875                         uiItemO(layout, "Tile Area", ICON_NONE, "SCREEN_OT_screen_full_area");
2876                 else
2877                         uiItemO(layout, "Maximize Area", ICON_NONE, "SCREEN_OT_screen_full_area");
2878         }
2879         
2880         uiPupMenuEnd(C, pup);
2881         
2882         return OPERATOR_CANCELLED;
2883 }
2884
2885 static void SCREEN_OT_header_toolbox(wmOperatorType *ot)
2886 {
2887         /* identifiers */
2888         ot->name = "Header Toolbox";
2889         ot->description = "Display header region toolbox";
2890         ot->idname = "SCREEN_OT_header_toolbox";
2891         
2892         /* api callbacks */
2893         ot->invoke = header_toolbox_invoke;
2894 }
2895
2896 /* ****************** anim player, with timer ***************** */
2897
2898 static int match_area_with_refresh(int spacetype, int refresh)
2899 {
2900         switch (spacetype) {
2901                 case SPACE_TIME:
2902                         if (refresh & SPACE_TIME)
2903                                 return 1;
2904                         break;
2905         }
2906         
2907         return 0;
2908 }
2909
2910 static int match_region_with_redraws(int spacetype, int regiontype, int redraws)
2911 {
2912         if (regiontype == RGN_TYPE_WINDOW) {
2913                 
2914                 switch (spacetype) {
2915                         case SPACE_VIEW3D:
2916                                 if (redraws & TIME_ALL_3D_WIN)
2917                                         return 1;
2918                                 break;
2919                         case SPACE_IPO:
2920                         case SPACE_ACTION:
2921                         case SPACE_NLA:
2922                                 if (redraws & TIME_ALL_ANIM_WIN)
2923                                         return 1;
2924                                 break;
2925                         case SPACE_TIME:
2926                                 /* if only 1 window or 3d windows, we do timeline too */
2927                                 if (redraws & (TIME_ALL_ANIM_WIN | TIME_REGION | TIME_ALL_3D_WIN))
2928                                         return 1;
2929                                 break;
2930                         case SPACE_BUTS:
2931                                 if (redraws & TIME_ALL_BUTS_WIN)
2932                                         return 1;
2933                                 break;
2934                         case SPACE_SEQ:
2935                                 if (redraws & (TIME_SEQ | TIME_ALL_ANIM_WIN))
2936                                         return 1;
2937                                 break;
2938                         case SPACE_NODE:
2939                                 if (redraws & (TIME_NODES))
2940                                         return 1;
2941                                 break;
2942                         case SPACE_IMAGE:
2943                                 if (redraws & TIME_ALL_IMAGE_WIN)
2944                                         return 1;
2945                                 break;
2946                         case SPACE_CLIP:
2947                                 if (redraws & TIME_CLIPS)
2948                                         return 1;
2949                                 break;
2950                                 
2951                 }
2952         }
2953         else if (regiontype == RGN_TYPE_UI) {
2954                 if (spacetype == SPACE_CLIP) {
2955                         /* Track Preview button is on Properties Editor in SpaceClip,
2956                          * and it's very common case when users want it be refreshing
2957                          * during playback, so asking people to enable special option
2958                          * for this is a bit tricky, so add exception here for refreshing
2959                          * Properties Editor for SpaceClip always */
2960                         return 1;
2961                 }
2962
2963                 if (redraws & TIME_ALL_BUTS_WIN)
2964                         return 1;
2965         }
2966         else if (regiontype == RGN_TYPE_HEADER) {
2967                 if (spacetype == SPACE_TIME)
2968                         return 1;
2969         }
2970         else if (regiontype == RGN_TYPE_PREVIEW) {
2971                 switch (spacetype) {
2972                         case SPACE_SEQ:
2973                                 if (redraws & (TIME_SEQ | TIME_ALL_ANIM_WIN))
2974                                         return 1;
2975                                 break;
2976                         case SPACE_CLIP:
2977                                 return 1;
2978                 }
2979         }
2980         return 0;
2981 }
2982
2983 static int screen_animation_step(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
2984 {
2985         bScreen *screen = CTX_wm_screen(C);
2986
2987         if (screen->animtimer && screen->animtimer == event->customdata) {
2988                 Main *bmain = CTX_data_main(C);
2989                 Scene *scene = CTX_data_scene(C);
2990                 wmTimer *wt = screen->animtimer;
2991                 ScreenAnimData *sad = wt->customdata;
2992                 wmWindowManager *wm = CTX_wm_manager(C);
2993                 wmWindow *window;
2994                 ScrArea *sa;
2995                 int sync;
2996                 float time;
2997                 
2998                 /* sync, don't sync, or follow scene setting */
2999                 if (sad->flag & ANIMPLAY_FLAG_SYNC) sync = 1;
3000                 else if (sad->flag & ANIMPLAY_FLAG_NO_SYNC) sync = 0;
3001                 else sync = (scene->flag & SCE_FRAME_DROP);
3002                 
3003                 if ((scene->audio.flag & AUDIO_SYNC) &&
3004                     (sad->flag & ANIMPLAY_FLAG_REVERSE) == FALSE &&
3005                     finite(time = sound_sync_scene(scene)))
3006                 {
3007                         scene->r.cfra = (double)time * FPS + 0.5;
3008                 }
3009                 else {
3010                         if (sync) {
3011                                 int step = floor((wt->duration - sad->last_duration) * FPS);
3012                                 /* skip frames */
3013                                 if (sad->flag & ANIMPLAY_FLAG_REVERSE)
3014                                         scene->r.cfra -= step;
3015                                 else
3016                                         scene->r.cfra += step;
3017                         }
3018                         else {
3019                                 /* one frame +/- */
3020                                 if (sad->flag & ANIMPLAY_FLAG_REVERSE)
3021                                         scene->r.cfra--;
3022                                 else
3023                                         scene->r.cfra++;
3024                         }
3025                 }
3026                 
3027                 sad->last_duration = wt->duration;
3028
3029                 /* reset 'jumped' flag before checking if we need to jump... */
3030                 sad->flag &= ~ANIMPLAY_FLAG_JUMPED;
3031                 
3032                 if (sad->flag & ANIMPLAY_FLAG_REVERSE) {
3033                         /* jump back to end? */
3034                         if (PRVRANGEON) {
3035                                 if (scene->r.cfra < scene->r.psfra) {
3036                                         scene->r.cfra = scene->r.pefra;
3037                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3038                                 }
3039                         }
3040                         else {
3041                                 if (scene->r.cfra < scene->r.sfra) {
3042                                         scene->r.cfra = scene->r.efra;
3043                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3044                                 }
3045                         }
3046                 }
3047                 else {
3048                         /* jump back to start? */
3049                         if (PRVRANGEON) {
3050                                 if (scene->r.cfra > scene->r.pefra) {
3051                                         scene->r.cfra = scene->r.psfra;
3052                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3053                                 }
3054                         }
3055                         else {
3056                                 if (scene->r.cfra > scene->r.efra) {
3057                                         scene->r.cfra = scene->r.sfra;
3058                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3059                                 }
3060                         }
3061                 }
3062
3063                 /* next frame overridden by user action (pressed jump to first/last frame) */
3064                 if (sad->flag & ANIMPLAY_FLAG_USE_NEXT_FRAME) {
3065                         scene->r.cfra = sad->nextfra;
3066                         sad->flag &= ~ANIMPLAY_FLAG_USE_NEXT_FRAME;
3067                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3068                 }
3069                 
3070                 if (sad->flag & ANIMPLAY_FLAG_JUMPED)
3071                         sound_seek_scene(bmain, scene);
3072                 
3073                 /* since we follow drawflags, we can't send notifier but tag regions ourselves */
3074                 ED_update_for_newframe(CTX_data_main(C), scene, 1);
3075
3076                 for (window = wm->windows.first; window; window = window->next) {
3077                         for (sa = window->screen->areabase.first; sa; sa = sa->next) {
3078                                 ARegion *ar;
3079                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
3080                                         if (ar == sad->ar)
3081                                                 ED_region_tag_redraw(ar);
3082                                         else
3083                                         if (match_region_with_redraws(sa->spacetype, ar->regiontype, sad->redraws))
3084                                                 ED_region_tag_redraw(ar);
3085                                 }
3086                                 
3087                                 if (match_area_with_refresh(sa->spacetype, sad->refresh))
3088                                         ED_area_tag_refresh(sa);
3089                         }
3090                 }
3091                         
3092                 /* update frame rate info too 
3093                  * NOTE: this may not be accurate enough, since we might need this after modifiers/etc. 
3094                  * have been calculated instead of just before updates have been done?
3095                  */
3096                 ED_refresh_viewport_fps(C);
3097                 
3098                 /* recalculate the timestep for the timer now that we've finished calculating this,
3099                  * since the frames-per-second value may have been changed
3100                  */
3101                 // TODO: this may make evaluation a bit slower if the value doesn't change... any way to avoid this?
3102                 wt->timestep = (1.0 / FPS);
3103                 
3104                 return OPERATOR_FINISHED;
3105         }
3106         return OPERATOR_PASS_THROUGH;
3107 }
3108
3109 static void SCREEN_OT_animation_step(wmOperatorType *ot)
3110 {
3111         /* identifiers */
3112         ot->name = "Animation Step";
3113         ot->description = "Step through animation by position";
3114         ot->idname = "SCREEN_OT_animation_step";
3115         
3116         /* api callbacks */
3117         ot->invoke = screen_animation_step;
3118         
3119         ot->poll = ED_operator_screenactive_norender;
3120         
3121 }
3122
3123 /* ****************** anim player, starts or ends timer ***************** */
3124
3125 /* find window that owns the animation timer */
3126 bScreen *ED_screen_animation_playing(const wmWindowManager *wm)
3127 {
3128         wmWindow *window;
3129
3130         for (window = wm->windows.first; window; window = window->next)
3131                 if (window->screen->animtimer)
3132                         return window->screen;
3133         
3134         return NULL;
3135 }
3136
3137 /* toggle operator */
3138 int ED_screen_animation_play(bContext *C, int sync, int mode)
3139 {
3140         bScreen *screen = CTX_wm_screen(C);
3141         Scene *scene = CTX_data_scene(C);
3142
3143         if (ED_screen_animation_playing(CTX_wm_manager(C))) {
3144                 /* stop playback now */
3145                 ED_screen_animation_timer(C, 0, 0, 0, 0);
3146                 sound_stop_scene(scene);
3147         }
3148         else {
3149                 int refresh = SPACE_TIME; /* these settings are currently only available from a menu in the TimeLine */
3150                 
3151                 if (mode == 1) // XXX only play audio forwards!?
3152                         sound_play_scene(scene);
3153                 
3154                 ED_screen_animation_timer(C, screen->redraws_flag, refresh, sync, mode);
3155                 
3156                 if (screen->animtimer) {
3157                         wmTimer *wt = screen->animtimer;
3158                         ScreenAnimData *sad = wt->customdata;
3159                         
3160                         sad->ar = CTX_wm_region(C);
3161                 }
3162         }
3163
3164         return OPERATOR_FINISHED;
3165 }
3166
3167 static int screen_animation_play_exec(bContext *C, wmOperator *op)
3168 {
3169         int mode = (RNA_boolean_get(op->ptr, "reverse")) ? -1 : 1;
3170         int sync = -1;
3171         
3172         if (RNA_struct_property_is_set(op->ptr, "sync"))
3173                 sync = (RNA_boolean_get(op->ptr, "sync"));
3174         
3175         return ED_screen_animation_play(C, sync, mode);
3176 }
3177
3178 static void SCREEN_OT_animation_play(wmOperatorType *ot)
3179 {
3180         PropertyRNA *prop;
3181
3182         /* identifiers */
3183         ot->name = "Play Animation";
3184         ot->description = "Play animation";
3185         ot->idname = "SCREEN_OT_animation_play";
3186         
3187         /* api callbacks */
3188         ot->exec = screen_animation_play_exec;
3189         
3190         ot->poll = ED_operator_screenactive_norender;
3191         
3192         prop = RNA_def_boolean(ot->srna, "reverse", 0, "Play in Reverse", "Animation is played backwards");
3193         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3194         prop = RNA_def_boolean(ot->srna, "sync", 0, "Sync", "Drop frames to maintain framerate");
3195         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3196 }
3197
3198 static int screen_animation_cancel_exec(bContext *C, wmOperator *op)
3199 {
3200         bScreen *screen = ED_screen_animation_playing(CTX_wm_manager(C));
3201
3202         if (screen) {
3203                 if (RNA_boolean_get(op->ptr, "restore_frame")) {
3204                         ScreenAnimData *sad = screen->animtimer->customdata;
3205                         Scene *scene = CTX_data_scene(C);
3206
3207                         /* reset current frame before stopping, and just send a notifier to deal with the rest
3208                          * (since playback still needs to be stopped)
3209                          */
3210                         scene->r.cfra = sad->sfra;
3211
3212                         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
3213                 }
3214
3215                 /* call the other "toggling" operator to clean up now */
3216                 ED_screen_animation_play(C, 0, 0);
3217         }
3218
3219         return OPERATOR_PASS_THROUGH;
3220 }
3221
3222 static void SCREEN_OT_animation_cancel(wmOperatorType *ot)
3223 {
3224         /* identifiers */
3225         ot->name = "Cancel Animation";
3226         ot->description = "Cancel animation, returning to the original frame";
3227         ot->idname = "SCREEN_OT_animation_cancel";
3228         
3229         /* api callbacks */
3230         ot->exec = screen_animation_cancel_exec;
3231         
3232         ot->poll = ED_operator_screenactive;
3233
3234         RNA_def_boolean(ot->srna, "restore_frame", TRUE, "Restore Frame", "Restore the frame when animation was initialized");
3235 }
3236
3237 /* ************** border select operator (template) ***************************** */
3238
3239 /* operator state vars used: (added by default WM callbacks)   
3240  * xmin, ymin     
3241  * xmax, ymax     
3242  * 
3243  * customdata: the wmGesture pointer
3244  * 
3245  * callbacks: