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