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