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