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