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