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