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