c64a4a37f3af7b25c04bc124cd59bdc22a029270
[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 = MIN2(*bigger, y1);
973                         else if (sa->v2->flag && sa->v3->flag)
974                                 *smaller = MIN2(*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 = MIN2(*bigger, x1);
982                         else if (sa->v3->flag && sa->v4->flag)
983                                 *smaller = MIN2(*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                         ;
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
2075 /* function to be called outside UI context, or for redo */
2076 static int screen_set_exec(bContext *C, wmOperator *op)
2077 {
2078         bScreen *screen = CTX_wm_screen(C);
2079         bScreen *screen_prev = screen;
2080         
2081         ScrArea *sa = CTX_wm_area(C);
2082         int tot = BLI_countlist(&CTX_data_main(C)->screen);
2083         int delta = RNA_int_get(op->ptr, "delta");
2084         
2085         /* temp screens are for userpref or render display */
2086         if (screen->temp)
2087                 return OPERATOR_CANCELLED;
2088         
2089         if (delta == 1) {
2090                 while (tot--) {
2091                         screen = screen->id.next;
2092                         if (screen == NULL) screen = CTX_data_main(C)->screen.first;
2093                         if (screen->winid == 0 && screen->full == 0 && screen != screen_prev)
2094                                 break;
2095                 }
2096         }
2097         else if (delta == -1) {
2098                 while (tot--) {
2099                         screen = screen->id.prev;
2100                         if (screen == NULL) screen = CTX_data_main(C)->screen.last;
2101                         if (screen->winid == 0 && screen->full == 0 && screen != screen_prev)
2102                                 break;
2103                 }
2104         }
2105         else {
2106                 screen = NULL;
2107         }
2108         
2109         if (screen && screen_prev != screen) {
2110                 /* return to previous state before switching screens */
2111                 if (sa && sa->full) {
2112                         ED_screen_full_restore(C, sa); /* may free 'screen_prev' */
2113                 }
2114                 
2115                 ED_screen_set(C, screen);
2116                 return OPERATOR_FINISHED;
2117         }
2118         return OPERATOR_CANCELLED;
2119 }
2120
2121 static void SCREEN_OT_screen_set(wmOperatorType *ot)
2122 {
2123         ot->name = "Set Screen";
2124         ot->description = "Cycle through available screens";
2125         ot->idname = "SCREEN_OT_screen_set";
2126         
2127         ot->exec = screen_set_exec;
2128         ot->poll = ED_operator_screenactive;
2129         
2130         /* rna */
2131         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
2132 }
2133
2134 /* ************** screen full-area operator ***************************** */
2135
2136
2137 /* function to be called outside UI context, or for redo */
2138 static int screen_full_area_exec(bContext *C, wmOperator *UNUSED(op))
2139 {
2140         bScreen *screen = CTX_wm_screen(C);
2141         ScrArea *sa = NULL;
2142         
2143         /* search current screen for 'fullscreen' areas */
2144         /* prevents restoring info header, when mouse is over it */
2145         for (sa = screen->areabase.first; sa; sa = sa->next) {
2146                 if (sa->full) break;
2147         }
2148         
2149         if (sa == NULL) sa = CTX_wm_area(C);
2150         
2151         ED_screen_full_toggle(C, CTX_wm_window(C), sa);
2152         return OPERATOR_FINISHED;
2153 }
2154
2155 static void SCREEN_OT_screen_full_area(wmOperatorType *ot)
2156 {
2157         ot->name = "Toggle Full Screen";
2158         ot->description = "Toggle display selected area as fullscreen";
2159         ot->idname = "SCREEN_OT_screen_full_area";
2160         
2161         ot->exec = screen_full_area_exec;
2162         ot->poll = ED_operator_areaactive;
2163         ot->flag = 0;
2164         
2165 }
2166
2167
2168
2169 /* ************** join area operator ********************************************** */
2170
2171 /* operator state vars used:  
2172  * x1, y1     mouse coord in first area, which will disappear
2173  * x2, y2     mouse coord in 2nd area, which will become joined
2174  * 
2175  * functions:
2176  * 
2177  * init()   find edge based on state vars 
2178  * test if the edge divides two areas, 
2179  * store active and nonactive area,
2180  * 
2181  * apply()  do the actual join
2182  * 
2183  * exit()       cleanup, send notifier
2184  * 
2185  * callbacks:
2186  * 
2187  * exec()       calls init, apply, exit 
2188  * 
2189  * invoke() sets mouse coords in x,y
2190  * call init()
2191  * add modal handler
2192  * 
2193  * modal()      accept modal events while doing it
2194  * call apply() with active window and nonactive window
2195  * call exit() and remove handler when LMB confirm
2196  */
2197
2198 typedef struct sAreaJoinData {
2199         ScrArea *sa1;   /* first area to be considered */
2200         ScrArea *sa2;   /* second area to be considered */
2201         ScrArea *scr;   /* designed for removal */
2202
2203 } sAreaJoinData;
2204
2205
2206 /* validate selection inside screen, set variables OK */
2207 /* return 0: init failed */
2208 /* XXX todo: find edge based on (x,y) and set other area? */
2209 static int area_join_init(bContext *C, wmOperator *op)
2210 {
2211         ScrArea *sa1, *sa2;
2212         sAreaJoinData *jd = NULL;
2213         int x1, y1;
2214         int x2, y2;
2215         int shared = 0;
2216         
2217         /* required properties, make negative to get return 0 if not set by caller */
2218         x1 = RNA_int_get(op->ptr, "min_x");
2219         y1 = RNA_int_get(op->ptr, "min_y");
2220         x2 = RNA_int_get(op->ptr, "max_x");
2221         y2 = RNA_int_get(op->ptr, "max_y");
2222         
2223         sa1 = screen_areahascursor(CTX_wm_screen(C), x1, y1);
2224         sa2 = screen_areahascursor(CTX_wm_screen(C), x2, y2);
2225         if (sa1 == NULL || sa2 == NULL || sa1 == sa2)
2226                 return 0;
2227         
2228         /* do areas share an edge? */
2229         if (sa1->v1 == sa2->v1 || sa1->v1 == sa2->v2 || sa1->v1 == sa2->v3 || sa1->v1 == sa2->v4) shared++;
2230         if (sa1->v2 == sa2->v1 || sa1->v2 == sa2->v2 || sa1->v2 == sa2->v3 || sa1->v2 == sa2->v4) shared++;
2231         if (sa1->v3 == sa2->v1 || sa1->v3 == sa2->v2 || sa1->v3 == sa2->v3 || sa1->v3 == sa2->v4) shared++;
2232         if (sa1->v4 == sa2->v1 || sa1->v4 == sa2->v2 || sa1->v4 == sa2->v3 || sa1->v4 == sa2->v4) shared++;
2233         if (shared != 2) {
2234                 printf("areas don't share edge\n");
2235                 return 0;
2236         }
2237         
2238         jd = (sAreaJoinData *)MEM_callocN(sizeof(sAreaJoinData), "op_area_join");
2239         
2240         jd->sa1 = sa1;
2241         jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2242         jd->sa2 = sa2;
2243         jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2244         
2245         op->customdata = jd;
2246         
2247         return 1;
2248 }
2249
2250 /* apply the join of the areas (space types) */
2251 static int area_join_apply(bContext *C, wmOperator *op)
2252 {
2253         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2254         if (!jd) return 0;
2255         
2256         if (!screen_area_join(C, CTX_wm_screen(C), jd->sa1, jd->sa2)) {
2257                 return 0;
2258         }
2259         if (CTX_wm_area(C) == jd->sa2) {
2260                 CTX_wm_area_set(C, NULL);
2261                 CTX_wm_region_set(C, NULL);
2262         }
2263         
2264         return 1;
2265 }
2266
2267 /* finish operation */
2268 static void area_join_exit(bContext *C, wmOperator *op)
2269 {
2270         if (op->customdata) {
2271                 MEM_freeN(op->customdata);
2272                 op->customdata = NULL;
2273         }
2274         
2275         /* this makes sure aligned edges will result in aligned grabbing */
2276         removedouble_scredges(CTX_wm_screen(C));
2277         removenotused_scredges(CTX_wm_screen(C));
2278         removenotused_scrverts(CTX_wm_screen(C));
2279 }
2280
2281 static int area_join_exec(bContext *C, wmOperator *op)
2282 {
2283         if (!area_join_init(C, op)) 
2284                 return OPERATOR_CANCELLED;
2285         
2286         area_join_apply(C, op);
2287         area_join_exit(C, op);
2288         
2289         return OPERATOR_FINISHED;
2290 }
2291
2292 /* interaction callback */
2293 static int area_join_invoke(bContext *C, wmOperator *op, wmEvent *event)
2294 {
2295         
2296         if (event->type == EVT_ACTIONZONE_AREA) {
2297                 sActionzoneData *sad = event->customdata;
2298                 
2299                 if (sad == NULL || sad->modifier > 0) {
2300                         return OPERATOR_PASS_THROUGH;
2301                 }
2302                 
2303                 /* verify *sad itself */
2304                 if (sad->sa1 == NULL || sad->sa2 == NULL)
2305                         return OPERATOR_PASS_THROUGH;
2306                 
2307                 /* is this our *sad? if areas equal it should be passed on */
2308                 if (sad->sa1 == sad->sa2)
2309                         return OPERATOR_PASS_THROUGH;
2310                 
2311                 /* prepare operator state vars */
2312                 RNA_int_set(op->ptr, "min_x", sad->x);
2313                 RNA_int_set(op->ptr, "min_y", sad->y);
2314                 RNA_int_set(op->ptr, "max_x", event->x);
2315                 RNA_int_set(op->ptr, "max_y", event->y);
2316         }
2317         
2318         
2319         if (!area_join_init(C, op)) 
2320                 return OPERATOR_PASS_THROUGH;
2321         
2322         /* add temp handler */
2323         WM_event_add_modal_handler(C, op);
2324         
2325         return OPERATOR_RUNNING_MODAL;
2326 }
2327
2328 static int area_join_cancel(bContext *C, wmOperator *op)
2329 {
2330         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2331         
2332         if (jd->sa1) {
2333                 jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2334                 jd->sa1->flag &= ~AREA_FLAG_DRAWJOINTO;
2335         }
2336         if (jd->sa2) {
2337                 jd->sa2->flag &= ~AREA_FLAG_DRAWJOINFROM;
2338                 jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2339         }
2340         
2341         WM_event_add_notifier(C, NC_WINDOW, NULL);
2342         
2343         area_join_exit(C, op);
2344         
2345         return OPERATOR_CANCELLED;
2346 }
2347
2348 /* modal callback while selecting area (space) that will be removed */
2349 static int area_join_modal(bContext *C, wmOperator *op, wmEvent *event)
2350 {
2351         bScreen *sc = CTX_wm_screen(C);
2352         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2353         
2354         /* execute the events */
2355         switch (event->type) {
2356                         
2357                 case MOUSEMOVE: 
2358                 {
2359                         ScrArea *sa = screen_areahascursor(sc, event->x, event->y);
2360                         int dir;
2361                         
2362                         if (sa) {                                       
2363                                 if (jd->sa1 != sa) {
2364                                         dir = area_getorientation(jd->sa1, sa);
2365                                         if (dir >= 0) {
2366                                                 if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2367                                                 jd->sa2 = sa;
2368                                                 jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2369                                         } 
2370                                         else {
2371                                                 /* we are not bordering on the previously selected area 
2372                                                  * we check if area has common border with the one marked for removal
2373                                                  * in this case we can swap areas.
2374                                                  */
2375                                                 dir = area_getorientation(sa, jd->sa2);
2376                                                 if (dir >= 0) {
2377                                                         if (jd->sa1) jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2378                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2379                                                         jd->sa1 = jd->sa2;
2380                                                         jd->sa2 = sa;
2381                                                         if (jd->sa1) jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2382                                                         if (jd->sa2) jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2383                                                 } 
2384                                                 else {
2385                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2386                                                         jd->sa2 = NULL;
2387                                                 }
2388                                         }
2389                                         WM_event_add_notifier(C, NC_WINDOW, NULL);
2390                                 } 
2391                                 else {
2392                                         /* we are back in the area previously selected for keeping 
2393                                          * we swap the areas if possible to allow user to choose */
2394                                         if (jd->sa2 != NULL) {
2395                                                 if (jd->sa1) jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2396                                                 if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2397                                                 jd->sa1 = jd->sa2;
2398                                                 jd->sa2 = sa;
2399                                                 if (jd->sa1) jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2400                                                 if (jd->sa2) jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2401                                                 dir = area_getorientation(jd->sa1, jd->sa2);
2402                                                 if (dir < 0) {
2403                                                         printf("oops, didn't expect that!\n");
2404                                                 }
2405                                         } 
2406                                         else {
2407                                                 dir = area_getorientation(jd->sa1, sa);
2408                                                 if (dir >= 0) {
2409                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2410                                                         jd->sa2 = sa;
2411                                                         jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2412                                                 }
2413                                         }
2414                                         WM_event_add_notifier(C, NC_WINDOW, NULL);
2415                                 }
2416                         }
2417                 }
2418                 break;
2419                 case LEFTMOUSE:
2420                         if (event->val == KM_RELEASE) {
2421                                 ED_area_tag_redraw(jd->sa1);
2422                                 ED_area_tag_redraw(jd->sa2);
2423
2424                                 area_join_apply(C, op);
2425                                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2426                                 area_join_exit(C, op);
2427                                 return OPERATOR_FINISHED;
2428                         }
2429                         break;
2430                         
2431                 case RIGHTMOUSE:
2432                 case ESCKEY:
2433                         return area_join_cancel(C, op);
2434         }
2435         
2436         return OPERATOR_RUNNING_MODAL;
2437 }
2438
2439 /* Operator for joining two areas (space types) */
2440 static void SCREEN_OT_area_join(wmOperatorType *ot)
2441 {
2442         /* identifiers */
2443         ot->name = "Join Area";
2444         ot->description = "Join selected areas into new window";
2445         ot->idname = "SCREEN_OT_area_join";
2446         
2447         /* api callbacks */
2448         ot->exec = area_join_exec;
2449         ot->invoke = area_join_invoke;
2450         ot->modal = area_join_modal;
2451         ot->poll = screen_active_editable;
2452         ot->cancel = area_join_cancel;
2453         
2454         ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL;
2455         
2456         /* rna */
2457         RNA_def_int(ot->srna, "min_x", -100, INT_MIN, INT_MAX, "X 1", "", INT_MIN, INT_MAX);
2458         RNA_def_int(ot->srna, "min_y", -100, INT_MIN, INT_MAX, "Y 1", "", INT_MIN, INT_MAX);
2459         RNA_def_int(ot->srna, "max_x", -100, INT_MIN, INT_MAX, "X 2", "", INT_MIN, INT_MAX);
2460         RNA_def_int(ot->srna, "max_y", -100, INT_MIN, INT_MAX, "Y 2", "", INT_MIN, INT_MAX);
2461 }
2462
2463 /* ******************************* */
2464
2465 static int screen_area_options_invoke(bContext *C, wmOperator *op, wmEvent *event)
2466 {
2467         uiPopupMenu *pup;
2468         uiLayout *layout;
2469         PointerRNA ptr1, ptr2;
2470         ScrEdge *actedge = screen_find_active_scredge(CTX_wm_screen(C), event->x, event->y);
2471         
2472         if (actedge == NULL) return OPERATOR_CANCELLED;
2473         
2474         pup = uiPupMenuBegin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2475         layout = uiPupMenuLayout(pup);
2476         
2477         WM_operator_properties_create(&ptr1, "SCREEN_OT_area_join");
2478         
2479         /* mouse cursor on edge, '4' can fail on wide edges... */
2480         RNA_int_set(&ptr1, "min_x", event->x + 4);
2481         RNA_int_set(&ptr1, "min_y", event->y + 4);
2482         RNA_int_set(&ptr1, "max_x", event->x - 4);
2483         RNA_int_set(&ptr1, "max_y", event->y - 4);
2484         
2485         WM_operator_properties_create(&ptr2, "SCREEN_OT_area_split");
2486         
2487         /* store initial mouse cursor position */
2488         RNA_int_set(&ptr2, "mouse_x", event->x);
2489         RNA_int_set(&ptr2, "mouse_y", event->y);
2490         
2491         uiItemFullO(layout, "SCREEN_OT_area_split", NULL, ICON_NONE, ptr2.data, WM_OP_INVOKE_DEFAULT, 0);
2492         uiItemFullO(layout, "SCREEN_OT_area_join", NULL, ICON_NONE, ptr1.data, WM_OP_INVOKE_DEFAULT, 0);
2493         
2494         uiPupMenuEnd(C, pup);
2495         
2496         return OPERATOR_CANCELLED;
2497 }
2498
2499 static void SCREEN_OT_area_options(wmOperatorType *ot)
2500 {
2501         /* identifiers */
2502         ot->name = "Area Options";
2503         ot->description = "Operations for splitting and merging";
2504         ot->idname = "SCREEN_OT_area_options";
2505         
2506         /* api callbacks */
2507         ot->invoke = screen_area_options_invoke;
2508         
2509         ot->poll = ED_operator_screen_mainwinactive;
2510 }
2511
2512
2513 /* ******************************* */
2514
2515
2516 static int spacedata_cleanup(bContext *C, wmOperator *op)
2517 {
2518         Main *bmain = CTX_data_main(C);
2519         bScreen *screen;
2520         ScrArea *sa;
2521         int tot = 0;
2522         
2523         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
2524                 for (sa = screen->areabase.first; sa; sa = sa->next) {
2525                         if (sa->spacedata.first != sa->spacedata.last) {
2526                                 SpaceLink *sl = sa->spacedata.first;
2527
2528                                 BLI_remlink(&sa->spacedata, sl);
2529                                 tot += BLI_countlist(&sa->spacedata);
2530                                 BKE_spacedata_freelist(&sa->spacedata);
2531                                 BLI_addtail(&sa->spacedata, sl);
2532                         }
2533                 }
2534         }
2535         BKE_reportf(op->reports, RPT_INFO, "Removed amount of editors: %d", tot);
2536         
2537         return OPERATOR_FINISHED;
2538 }
2539
2540 static void SCREEN_OT_spacedata_cleanup(wmOperatorType *ot)
2541 {
2542         /* identifiers */
2543         ot->name = "Clean-up space-data";
2544         ot->description = "Remove unused settings for invisible editors";
2545         ot->idname = "SCREEN_OT_spacedata_cleanup";
2546         
2547         /* api callbacks */
2548         ot->exec = spacedata_cleanup;
2549         ot->poll = WM_operator_winactive;
2550         
2551 }
2552
2553 /* ************** repeat last operator ***************************** */
2554
2555 static int repeat_last_exec(bContext *C, wmOperator *UNUSED(op))
2556 {
2557         wmOperator *lastop = CTX_wm_manager(C)->operators.last;
2558         
2559         if (lastop)
2560                 WM_operator_repeat(C, lastop);
2561         
2562         return OPERATOR_CANCELLED;
2563 }
2564
2565 static void SCREEN_OT_repeat_last(wmOperatorType *ot)
2566 {
2567         /* identifiers */
2568         ot->name = "Repeat Last";
2569         ot->description = "Repeat last action";
2570         ot->idname = "SCREEN_OT_repeat_last";
2571         
2572         /* api callbacks */
2573         ot->exec = repeat_last_exec;
2574         
2575         ot->poll = ED_operator_screenactive;
2576         
2577 }
2578
2579 static int repeat_history_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2580 {
2581         wmWindowManager *wm = CTX_wm_manager(C);
2582         wmOperator *lastop;
2583         uiPopupMenu *pup;
2584         uiLayout *layout;
2585         int items, i;
2586         
2587         items = BLI_countlist(&wm->operators);
2588         if (items == 0)
2589                 return OPERATOR_CANCELLED;
2590         
2591         pup = uiPupMenuBegin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2592         layout = uiPupMenuLayout(pup);
2593         
2594         for (i = items - 1, lastop = wm->operators.last; lastop; lastop = lastop->prev, i--)
2595                 uiItemIntO(layout, RNA_struct_ui_name(lastop->type->srna), ICON_NONE, op->type->idname, "index", i);
2596         
2597         uiPupMenuEnd(C, pup);
2598         
2599         return OPERATOR_CANCELLED;
2600 }
2601
2602 static int repeat_history_exec(bContext *C, wmOperator *op)
2603 {
2604         wmWindowManager *wm = CTX_wm_manager(C);
2605         
2606         op = BLI_findlink(&wm->operators, RNA_int_get(op->ptr, "index"));
2607         if (op) {
2608                 /* let's put it as last operator in list */
2609                 BLI_remlink(&wm->operators, op);
2610                 BLI_addtail(&wm->operators, op);
2611                 
2612                 WM_operator_repeat(C, op);
2613         }
2614         
2615         return OPERATOR_FINISHED;
2616 }
2617
2618 static void SCREEN_OT_repeat_history(wmOperatorType *ot)
2619 {
2620         /* identifiers */
2621         ot->name = "Repeat History";
2622         ot->description = "Display menu for previous actions performed";
2623         ot->idname = "SCREEN_OT_repeat_history";
2624         
2625         /* api callbacks */
2626         ot->invoke = repeat_history_invoke;
2627         ot->exec = repeat_history_exec;
2628         
2629         ot->poll = ED_operator_screenactive;
2630         
2631         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, 1000);
2632 }
2633
2634 /* ********************** redo operator ***************************** */
2635
2636 static int redo_last_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
2637 {
2638         wmOperator *lastop = WM_operator_last_redo(C);
2639         
2640         if (lastop)
2641                 WM_operator_redo_popup(C, lastop);
2642         
2643         return OPERATOR_CANCELLED;
2644 }
2645
2646 static void SCREEN_OT_redo_last(wmOperatorType *ot)
2647 {
2648         /* identifiers */
2649         ot->name = "Redo Last";
2650         ot->description = "Display menu for last action performed";
2651         ot->idname = "SCREEN_OT_redo_last";
2652         
2653         /* api callbacks */
2654         ot->invoke = redo_last_invoke;
2655         
2656         ot->poll = ED_operator_screenactive;
2657 }
2658
2659 /* ************** region four-split operator ***************************** */
2660
2661 static void view3d_localview_update_rv3d(struct RegionView3D *rv3d)
2662 {
2663         if (rv3d->localvd) {
2664                 rv3d->localvd->view = rv3d->view;
2665                 rv3d->localvd->persp = rv3d->persp;
2666                 copy_qt_qt(rv3d->localvd->viewquat, rv3d->viewquat);
2667         }
2668 }
2669
2670 /* insert a region in the area region list */
2671 static int region_quadview_exec(bContext *C, wmOperator *op)
2672 {
2673         ARegion *ar = CTX_wm_region(C);
2674         
2675         /* some rules... */
2676         if (ar->regiontype != RGN_TYPE_WINDOW)
2677                 BKE_report(op->reports, RPT_ERROR, "Only window region can be 4-splitted");
2678         else if (ar->alignment == RGN_ALIGN_QSPLIT) {
2679                 ScrArea *sa = CTX_wm_area(C);
2680                 ARegion *arn;
2681                 
2682                 /* keep current region */
2683                 ar->alignment = 0;
2684                 
2685                 if (sa->spacetype == SPACE_VIEW3D) {
2686                         RegionView3D *rv3d = ar->regiondata;
2687                         rv3d->viewlock = 0;
2688                         rv3d->rflag &= ~RV3D_CLIPPING;
2689                 }
2690                 
2691                 for (ar = sa->regionbase.first; ar; ar = arn) {
2692                         arn = ar->next;
2693                         if (ar->alignment == RGN_ALIGN_QSPLIT) {
2694                                 ED_region_exit(C, ar);
2695                                 BKE_area_region_free(sa->type, ar);
2696                                 BLI_remlink(&sa->regionbase, ar);
2697                                 MEM_freeN(ar);
2698                         }
2699                 }
2700                 ED_area_tag_redraw(sa);
2701                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2702         }
2703         else if (ar->next)
2704                 BKE_report(op->reports, RPT_ERROR, "Only last region can be 4-splitted");
2705         else {
2706                 ScrArea *sa = CTX_wm_area(C);
2707                 ARegion *newar;
2708                 int count;
2709                 
2710                 ar->alignment = RGN_ALIGN_QSPLIT;
2711                 
2712                 for (count = 0; count < 3; count++) {
2713                         newar = BKE_area_region_copy(sa->type, ar);
2714                         BLI_addtail(&sa->regionbase, newar);
2715                 }
2716                 
2717                 /* lock views and set them */
2718                 if (sa->spacetype == SPACE_VIEW3D) {
2719                         /* run ED_view3d_lock() so the correct 'rv3d->viewquat' is set,
2720                          * otherwise when restoring rv3d->localvd the 'viewquat' won't
2721                          * match the 'view', set on entering localview See: [#26315],
2722                          *
2723                          * We could avoid manipulating rv3d->localvd here if exiting
2724                          * localview with a 4-split would assign these view locks */
2725                         RegionView3D *rv3d;
2726
2727                         rv3d = ar->regiondata;
2728                         rv3d->viewlock = RV3D_LOCKED; rv3d->view = RV3D_VIEW_FRONT; rv3d->persp = RV3D_ORTHO;
2729                         ED_view3d_lock(rv3d);
2730                         view3d_localview_update_rv3d(rv3d);
2731                         
2732                         ar = ar->next;
2733                         rv3d = ar->regiondata;
2734                         rv3d->viewlock = RV3D_LOCKED; rv3d->view = RV3D_VIEW_TOP; rv3d->persp = RV3D_ORTHO;
2735                         ED_view3d_lock(rv3d);
2736                         view3d_localview_update_rv3d(rv3d);
2737                         
2738                         ar = ar->next;
2739                         rv3d = ar->regiondata;
2740                         rv3d->viewlock = RV3D_LOCKED; rv3d->view = RV3D_VIEW_RIGHT; 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->view = RV3D_VIEW_CAMERA; rv3d->persp = RV3D_CAMOB;
2747                         ED_view3d_lock(rv3d);
2748                         view3d_localview_update_rv3d(rv3d);
2749                 }
2750                 ED_area_tag_redraw(sa);
2751                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2752         }
2753         
2754         
2755         return OPERATOR_FINISHED;
2756 }
2757
2758 static void SCREEN_OT_region_quadview(wmOperatorType *ot)
2759 {
2760         /* identifiers */
2761         ot->name = "Toggle Quad View";
2762         ot->description = "Split selected area into camera, front, right & top views";
2763         ot->idname = "SCREEN_OT_region_quadview";
2764         
2765         /* api callbacks */
2766         //      ot->invoke = WM_operator_confirm;
2767         ot->exec = region_quadview_exec;
2768         ot->poll = ED_operator_region_view3d_active;
2769         ot->flag = 0;
2770 }
2771
2772
2773
2774 /* ************** region flip operator ***************************** */
2775
2776 /* flip a region alignment */
2777 static int region_flip_exec(bContext *C, wmOperator *UNUSED(op))
2778 {
2779         ARegion *ar = CTX_wm_region(C);
2780         
2781         if (!ar)
2782                 return OPERATOR_CANCELLED;
2783         
2784         if (ar->alignment == RGN_ALIGN_TOP)
2785                 ar->alignment = RGN_ALIGN_BOTTOM;
2786         else if (ar->alignment == RGN_ALIGN_BOTTOM)
2787                 ar->alignment = RGN_ALIGN_TOP;
2788         else if (ar->alignment == RGN_ALIGN_LEFT)
2789                 ar->alignment = RGN_ALIGN_RIGHT;
2790         else if (ar->alignment == RGN_ALIGN_RIGHT)
2791                 ar->alignment = RGN_ALIGN_LEFT;
2792
2793         ED_area_tag_redraw(CTX_wm_area(C));
2794         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2795         
2796         return OPERATOR_FINISHED;
2797 }
2798
2799
2800 static void SCREEN_OT_region_flip(wmOperatorType *ot)
2801 {
2802         /* identifiers */
2803         ot->name = "Flip Region";
2804         ot->idname = "SCREEN_OT_region_flip";
2805         ot->description = "Toggle the region's alignment (left/right or top/bottom)";
2806         
2807         /* api callbacks */
2808         ot->exec = region_flip_exec;
2809         ot->poll = ED_operator_areaactive;
2810         ot->flag = 0;
2811 }
2812
2813 /* ************** header flip operator ***************************** */
2814
2815 /* flip a header region alignment */
2816 static int header_flip_exec(bContext *C, wmOperator *UNUSED(op))
2817 {
2818         ARegion *ar = CTX_wm_region(C);
2819         
2820         /* find the header region 
2821          *      - try context first, but upon failing, search all regions in area...
2822          */
2823         if ((ar == NULL) || (ar->regiontype != RGN_TYPE_HEADER)) {
2824                 ScrArea *sa = CTX_wm_area(C);
2825                 ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
2826
2827                 /* don't do anything if no region */
2828                 if (ar == NULL)
2829                         return OPERATOR_CANCELLED;
2830         }       
2831         
2832         /* copied from SCREEN_OT_region_flip */
2833         if (ar->alignment == RGN_ALIGN_TOP)
2834                 ar->alignment = RGN_ALIGN_BOTTOM;
2835         else if (ar->alignment == RGN_ALIGN_BOTTOM)
2836                 ar->alignment = RGN_ALIGN_TOP;
2837         else if (ar->alignment == RGN_ALIGN_LEFT)
2838                 ar->alignment = RGN_ALIGN_RIGHT;
2839         else if (ar->alignment == RGN_ALIGN_RIGHT)
2840                 ar->alignment = RGN_ALIGN_LEFT;
2841
2842         ED_area_tag_redraw(CTX_wm_area(C));
2843
2844         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2845         
2846         return OPERATOR_FINISHED;
2847 }
2848
2849
2850 static void SCREEN_OT_header_flip(wmOperatorType *ot)
2851 {
2852         /* identifiers */
2853         ot->name = "Flip Header Region";
2854         ot->idname = "SCREEN_OT_header_flip";
2855         ot->description = "Toggle the header over/below the main window area";
2856         
2857         /* api callbacks */
2858         ot->exec = header_flip_exec;
2859         
2860         ot->poll = ED_operator_areaactive;
2861         ot->flag = 0;
2862 }
2863
2864 /* ************** header tools operator ***************************** */
2865 void ED_screens_header_tools_menu_create(bContext *C, uiLayout *layout, void *UNUSED(arg))
2866 {
2867         ScrArea *sa = CTX_wm_area(C);
2868         ARegion *ar = CTX_wm_region(C);
2869
2870         /* XXX SCREEN_OT_region_flip doesn't work - gets wrong context for active region, so added custom operator. */
2871         if (ar->alignment == RGN_ALIGN_TOP)
2872                 uiItemO(layout, IFACE_("Flip to Bottom"), ICON_NONE, "SCREEN_OT_header_flip");
2873         else
2874                 uiItemO(layout, IFACE_("Flip to Top"), ICON_NONE, "SCREEN_OT_header_flip");
2875
2876         uiItemS(layout);
2877
2878         /* file browser should be fullscreen all the time, but other regions can be maximized/restored... */
2879         if (sa->spacetype != SPACE_FILE) {
2880                 if (sa->full) 
2881                         uiItemO(layout, IFACE_("Tile Area"), ICON_NONE, "SCREEN_OT_screen_full_area");
2882                 else
2883                         uiItemO(layout, IFACE_("Maximize Area"), ICON_NONE, "SCREEN_OT_screen_full_area");
2884         }
2885 }
2886
2887 static int header_toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
2888 {
2889         uiPopupMenu *pup;
2890         uiLayout *layout;
2891
2892         pup = uiPupMenuBegin(C, N_("Header"), ICON_NONE);
2893         layout = uiPupMenuLayout(pup);
2894
2895         ED_screens_header_tools_menu_create(C, layout, NULL);
2896
2897         uiPupMenuEnd(C, pup);
2898
2899         return OPERATOR_CANCELLED;
2900 }
2901
2902 static void SCREEN_OT_header_toolbox(wmOperatorType *ot)
2903 {
2904         /* identifiers */
2905         ot->name = "Header Toolbox";
2906         ot->description = "Display header region toolbox";
2907         ot->idname = "SCREEN_OT_header_toolbox";
2908         
2909         /* api callbacks */
2910         ot->invoke = header_toolbox_invoke;
2911 }
2912
2913 /* ****************** anim player, with timer ***************** */
2914
2915 static int match_area_with_refresh(int spacetype, int refresh)
2916 {
2917         switch (spacetype) {
2918                 case SPACE_TIME:
2919                         if (refresh & SPACE_TIME)
2920                                 return 1;
2921                         break;
2922         }
2923         
2924         return 0;
2925 }
2926
2927 static int match_region_with_redraws(int spacetype, int regiontype, int redraws)
2928 {
2929         if (regiontype == RGN_TYPE_WINDOW) {
2930                 
2931                 switch (spacetype) {
2932                         case SPACE_VIEW3D:
2933                                 if (redraws & TIME_ALL_3D_WIN)
2934                                         return 1;
2935                                 break;
2936                         case SPACE_IPO:
2937                         case SPACE_ACTION:
2938                         case SPACE_NLA:
2939                                 if (redraws & TIME_ALL_ANIM_WIN)
2940                                         return 1;
2941                                 break;
2942                         case SPACE_TIME:
2943                                 /* if only 1 window or 3d windows, we do timeline too */
2944                                 if (redraws & (TIME_ALL_ANIM_WIN | TIME_REGION | TIME_ALL_3D_WIN))
2945                                         return 1;
2946                                 break;
2947                         case SPACE_BUTS:
2948                                 if (redraws & TIME_ALL_BUTS_WIN)
2949                                         return 1;
2950                                 break;
2951                         case SPACE_SEQ:
2952                                 if (redraws & (TIME_SEQ | TIME_ALL_ANIM_WIN))
2953                                         return 1;
2954                                 break;
2955                         case SPACE_NODE:
2956                                 if (redraws & (TIME_NODES))
2957                                         return 1;
2958                                 break;
2959                         case SPACE_IMAGE:
2960                                 if (redraws & TIME_ALL_IMAGE_WIN)
2961                                         return 1;
2962                                 break;
2963                         case SPACE_CLIP:
2964                                 if (redraws & TIME_CLIPS)
2965                                         return 1;
2966                                 break;
2967                                 
2968                 }
2969         }
2970         else if (regiontype == RGN_TYPE_UI) {
2971                 if (spacetype == SPACE_CLIP) {
2972                         /* Track Preview button is on Properties Editor in SpaceClip,
2973                          * and it's very common case when users want it be refreshing
2974                          * during playback, so asking people to enable special option
2975                          * for this is a bit tricky, so add exception here for refreshing
2976                          * Properties Editor for SpaceClip always */
2977                         return 1;
2978                 }
2979
2980                 if (redraws & TIME_ALL_BUTS_WIN)
2981                         return 1;
2982         }
2983         else if (regiontype == RGN_TYPE_HEADER) {
2984                 if (spacetype == SPACE_TIME)
2985                         return 1;
2986         }
2987         else if (regiontype == RGN_TYPE_PREVIEW) {
2988                 switch (spacetype) {
2989                         case SPACE_SEQ:
2990                                 if (redraws & (TIME_SEQ | TIME_ALL_ANIM_WIN))
2991                                         return 1;
2992                                 break;
2993                         case SPACE_CLIP:
2994                                 return 1;
2995                 }
2996         }
2997         return 0;
2998 }
2999
3000 static int screen_animation_step(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
3001 {
3002         bScreen *screen = CTX_wm_screen(C);
3003
3004         if (screen->animtimer && screen->animtimer == event->customdata) {
3005                 Main *bmain = CTX_data_main(C);
3006                 Scene *scene = CTX_data_scene(C);
3007                 wmTimer *wt = screen->animtimer;
3008                 ScreenAnimData *sad = wt->customdata;
3009                 wmWindowManager *wm = CTX_wm_manager(C);
3010                 wmWindow *window;
3011                 ScrArea *sa;
3012                 int sync;
3013                 float time;
3014                 
3015                 /* sync, don't sync, or follow scene setting */
3016                 if (sad->flag & ANIMPLAY_FLAG_SYNC) sync = 1;
3017                 else if (sad->flag & ANIMPLAY_FLAG_NO_SYNC) sync = 0;
3018                 else sync = (scene->flag & SCE_FRAME_DROP);
3019                 
3020                 if ((scene->audio.flag & AUDIO_SYNC) &&
3021                     (sad->flag & ANIMPLAY_FLAG_REVERSE) == FALSE &&
3022                     finite(time = sound_sync_scene(scene)))
3023                 {
3024                         scene->r.cfra = (double)time * FPS + 0.5;
3025                 }
3026                 else {
3027                         if (sync) {
3028                                 int step = floor((wt->duration - sad->last_duration) * FPS);
3029                                 /* skip frames */
3030                                 if (sad->flag & ANIMPLAY_FLAG_REVERSE)
3031                                         scene->r.cfra -= step;
3032                                 else
3033                                         scene->r.cfra += step;
3034                         }
3035                         else {
3036                                 /* one frame +/- */
3037                                 if (sad->flag & ANIMPLAY_FLAG_REVERSE)
3038                                         scene->r.cfra--;
3039                                 else
3040                                         scene->r.cfra++;
3041                         }
3042                 }
3043                 
3044                 sad->last_duration = wt->duration;
3045
3046                 /* reset 'jumped' flag before checking if we need to jump... */
3047                 sad->flag &= ~ANIMPLAY_FLAG_JUMPED;
3048                 
3049                 if (sad->flag & ANIMPLAY_FLAG_REVERSE) {
3050                         /* jump back to end? */
3051                         if (PRVRANGEON) {
3052                                 if (scene->r.cfra < scene->r.psfra) {
3053                                         scene->r.cfra = scene->r.pefra;
3054                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3055                                 }
3056                         }
3057                         else {
3058                                 if (scene->r.cfra < scene->r.sfra) {
3059                                         scene->r.cfra = scene->r.efra;
3060                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3061                                 }
3062                         }
3063                 }
3064                 else {
3065                         /* jump back to start? */
3066                         if (PRVRANGEON) {
3067                                 if (scene->r.cfra > scene->r.pefra) {
3068                                         scene->r.cfra = scene->r.psfra;
3069                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3070                                 }
3071                         }
3072                         else {
3073                                 if (scene->r.cfra > scene->r.efra) {
3074                                         scene->r.cfra = scene->r.sfra;
3075                                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3076                                 }
3077                         }
3078                 }
3079
3080                 /* next frame overridden by user action (pressed jump to first/last frame) */
3081                 if (sad->flag & ANIMPLAY_FLAG_USE_NEXT_FRAME) {
3082                         scene->r.cfra = sad->nextfra;
3083                         sad->flag &= ~ANIMPLAY_FLAG_USE_NEXT_FRAME;
3084                         sad->flag |= ANIMPLAY_FLAG_JUMPED;
3085                 }
3086                 
3087                 if (sad->flag & ANIMPLAY_FLAG_JUMPED)
3088                         sound_seek_scene(bmain, scene);
3089                 
3090                 /* since we follow drawflags, we can't send notifier but tag regions ourselves */
3091                 ED_update_for_newframe(CTX_data_main(C), scene, 1);
3092
3093                 for (window = wm->windows.first; window; window = window->next) {
3094                         for (sa = window->screen->areabase.first; sa; sa = sa->next) {
3095                                 ARegion *ar;
3096                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
3097                                         if (ar == sad->ar)
3098                                                 ED_region_tag_redraw(ar);
3099                                         else
3100                                         if (match_region_with_redraws(sa->spacetype, ar->regiontype, sad->redraws))
3101                                                 ED_region_tag_redraw(ar);
3102                                 }
3103                                 
3104                                 if (match_area_with_refresh(sa->spacetype, sad->refresh))
3105                                         ED_area_tag_refresh(sa);
3106                         }
3107                 }
3108                         
3109                 /* update frame rate info too 
3110                  * NOTE: this may not be accurate enough, since we might need this after modifiers/etc. 
3111                  * have been calculated instead of just before updates have been done?
3112                  */
3113                 ED_refresh_viewport_fps(C);
3114                 
3115                 /* recalculate the timestep for the timer now that we've finished calculating this,
3116                  * since the frames-per-second value may have been changed
3117                  */
3118                 // TODO: this may make evaluation a bit slower if the value doesn't change... any way to avoid this?
3119                 wt->timestep = (1.0 / FPS);
3120                 
3121                 return OPERATOR_FINISHED;
3122         }
3123         return OPERATOR_PASS_THROUGH;
3124 }
3125
3126 static void SCREEN_OT_animation_step(wmOperatorType *ot)
3127 {
3128         /* identifiers */
3129         ot->name = "Animation Step";
3130         ot->description = "Step through animation by position";
3131         ot->idname = "SCREEN_OT_animation_step";
3132         
3133         /* api callbacks */
3134         ot->invoke = screen_animation_step;
3135         
3136         ot->poll = ED_operator_screenactive_norender;
3137         
3138 }
3139
3140 /* ****************** anim player, starts or ends timer ***************** */
3141
3142 /* find window that owns the animation timer */
3143 bScreen *ED_screen_animation_playing(const wmWindowManager *wm)
3144 {
3145         wmWindow *window;
3146
3147         for (window = wm->windows.first; window; window = window->next)
3148                 if (window->screen->animtimer)
3149                         return window->screen;
3150         
3151         return NULL;
3152 }
3153
3154 /* toggle operator */
3155 int ED_screen_animation_play(bContext *C, int sync, int mode)
3156 {
3157         bScreen *screen = CTX_wm_screen(C);
3158         Scene *scene = CTX_data_scene(C);
3159
3160         if (ED_screen_animation_playing(CTX_wm_manager(C))) {
3161                 /* stop playback now */
3162                 ED_screen_animation_timer(C, 0, 0, 0, 0);
3163                 sound_stop_scene(scene);
3164         }
3165         else {
3166                 int refresh = SPACE_TIME; /* these settings are currently only available from a menu in the TimeLine */
3167                 
3168                 if (mode == 1) // XXX only play audio forwards!?
3169                         sound_play_scene(scene);
3170                 
3171                 ED_screen_animation_timer(C, screen->redraws_flag, refresh, sync, mode);
3172                 
3173                 if (screen->animtimer) {
3174                         wmTimer *wt = screen->animtimer;
3175                         ScreenAnimData *sad = wt->customdata;
3176                         
3177                         sad->ar = CTX_wm_region(C);
3178                 }
3179         }
3180
3181         return OPERATOR_FINISHED;
3182 }
3183
3184 static int screen_animation_play_exec(bContext *C, wmOperator *op)
3185 {
3186         int mode = (RNA_boolean_get(op->ptr, "reverse")) ? -1 : 1;
3187         int sync = -1;
3188         
3189         if (RNA_struct_property_is_set(op->ptr, "sync"))
3190                 sync = (RNA_boolean_get(op->ptr, "sync"));
3191         
3192         return ED_screen_animation_play(C, sync, mode);
3193 }
3194
3195 static void SCREEN_OT_animation_play(wmOperatorType *ot)
3196 {
3197         PropertyRNA *prop;
3198
3199         /* identifiers */
3200         ot->name = "Play Animation";
3201         ot->description = "Play animation";
3202         ot->idname = "SCREEN_OT_animation_play";
3203         
3204         /* api callbacks */
3205         ot->exec = screen_animation_play_exec;
3206         
3207         ot->poll = ED_operator_screenactive_norender;
3208         
3209         prop = RNA_def_boolean(ot->srna, "reverse", 0, "Play in Reverse", "Animation is played backwards");
3210         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3211         prop = RNA_def_boolean(ot->srna, "sync", 0, "Sync", "Drop frames to maintain framerate");
3212         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3213 }
3214
3215 static int screen_animation_cancel_exec(bContext *C, wmOperator *op)
3216 {
3217         bScreen *screen = ED_screen_animation_playing(CTX_wm_manager(C));
3218
3219         if (screen) {
3220                 if (RNA_boolean_get(op->ptr, "restore_frame")) {
3221                         ScreenAnimData *sad = screen->animtimer->customdata;
3222                         Scene *scene = CTX_data_scene(C);
3223
3224                         /* reset current frame before stopping, and just send a notifier to deal with the rest
3225                          * (since playback still needs to be stopped)
3226                          */
3227                         scene->r.cfra = sad->sfra;
3228
3229                         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
3230                 }
3231
3232                 /* call the other "toggling" operator to clean up now */
3233                 ED_screen_animation_play(C, 0, 0);
3234         }
3235
3236         return OPERATOR_PASS_THROUGH;
3237 }
3238
3239 static void SCREEN_OT_animation_cancel(wmOperatorType *ot)
3240 {