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