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