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