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