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