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