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