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         if (next)
2278                 ak = (ActKeyColumn *)BLI_dlrbTree_search_next(&keys, compare_ak_cfraPtr, &cfra);
2279         else
2280                 ak = (ActKeyColumn *)BLI_dlrbTree_search_prev(&keys, compare_ak_cfraPtr, &cfra);
2281         
2282         while ((ak != NULL) && (done == false)) {
2283                 if (CFRA != (int)ak->cfra) {
2284                         /* this changes the frame, so set the frame and we're done */
2285                         CFRA = (int)ak->cfra;
2286                         done = true;
2287                 }
2288                 else {
2289                         /* take another step... */
2290                         if (next) {
2291                                 ak = ak->next;
2292                         }
2293                         else {
2294                                 ak = ak->prev;
2295                         }
2296                 }
2297         }
2298         
2299         /* free temp stuff */
2300         BLI_dlrbTree_free(&keys);
2301
2302         /* any success? */
2303         if (done == false) {
2304                 BKE_report(op->reports, RPT_INFO, "No more keyframes to jump to in this direction");
2305
2306                 return OPERATOR_CANCELLED;
2307         }
2308         else {
2309                 areas_do_frame_follow(C, true);
2310
2311                 BKE_sound_seek_scene(bmain, scene);
2312
2313                 WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2314
2315                 return OPERATOR_FINISHED;
2316         }
2317 }
2318
2319 static void SCREEN_OT_keyframe_jump(wmOperatorType *ot)
2320 {
2321         ot->name = "Jump to Keyframe";
2322         ot->description = "Jump to previous/next keyframe";
2323         ot->idname = "SCREEN_OT_keyframe_jump";
2324         
2325         ot->exec = keyframe_jump_exec;
2326         
2327         ot->poll = ED_operator_screenactive_norender;
2328         ot->flag = OPTYPE_UNDO_GROUPED;
2329         ot->undo_group = "FRAME_CHANGE";
2330         
2331         /* properties */
2332         RNA_def_boolean(ot->srna, "next", true, "Next Keyframe", "");
2333 }
2334
2335 /* ************** jump to marker operator ***************************** */
2336
2337 /* function to be called outside UI context, or for redo */
2338 static int marker_jump_exec(bContext *C, wmOperator *op)
2339 {
2340         Main *bmain = CTX_data_main(C);
2341         Scene *scene = CTX_data_scene(C);
2342         TimeMarker *marker;
2343         int closest = CFRA;
2344         const bool next = RNA_boolean_get(op->ptr, "next");
2345         bool found = false;
2346
2347         /* find matching marker in the right direction */
2348         for (marker = scene->markers.first; marker; marker = marker->next) {
2349                 if (next) {
2350                         if ((marker->frame > CFRA) && (!found || closest > marker->frame)) {
2351                                 closest = marker->frame;
2352                                 found = true;
2353                         }
2354                 }
2355                 else {
2356                         if ((marker->frame < CFRA) && (!found || closest < marker->frame)) {
2357                                 closest = marker->frame;
2358                                 found = true;
2359                         }
2360                 }
2361         }
2362
2363         /* any success? */
2364         if (!found) {
2365                 BKE_report(op->reports, RPT_INFO, "No more markers to jump to in this direction");
2366
2367                 return OPERATOR_CANCELLED;
2368         }
2369         else {
2370                 CFRA = closest;
2371
2372                 areas_do_frame_follow(C, true);
2373
2374                 BKE_sound_seek_scene(bmain, scene);
2375
2376                 WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2377
2378                 return OPERATOR_FINISHED;
2379         }
2380 }
2381
2382 static void SCREEN_OT_marker_jump(wmOperatorType *ot)
2383 {
2384         ot->name = "Jump to Marker";
2385         ot->description = "Jump to previous/next marker";
2386         ot->idname = "SCREEN_OT_marker_jump";
2387
2388         ot->exec = marker_jump_exec;
2389
2390         ot->poll = ED_operator_screenactive_norender;
2391         ot->flag = OPTYPE_UNDO_GROUPED;
2392         ot->undo_group = "FRAME_CHANGE";
2393
2394         /* properties */
2395         RNA_def_boolean(ot->srna, "next", true, "Next Marker", "");
2396 }
2397
2398 /* ************** switch screen operator ***************************** */
2399
2400 /* function to be called outside UI context, or for redo */
2401 static int screen_set_exec(bContext *C, wmOperator *op)
2402 {
2403         WorkSpace *workspace = CTX_wm_workspace(C);
2404         int delta = RNA_int_get(op->ptr, "delta");
2405
2406         if (ED_workspace_layout_cycle(workspace, delta, C)) {
2407                 return OPERATOR_FINISHED;
2408         }
2409
2410         return OPERATOR_CANCELLED;
2411 }
2412
2413 static void SCREEN_OT_screen_set(wmOperatorType *ot)
2414 {
2415         ot->name = "Set Screen";
2416         ot->description = "Cycle through available screens";
2417         ot->idname = "SCREEN_OT_screen_set";
2418         
2419         ot->exec = screen_set_exec;
2420         ot->poll = ED_operator_screenactive;
2421
2422         /* rna */
2423         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
2424 }
2425
2426 /* ************** screen full-area operator ***************************** */
2427
2428
2429 /* function to be called outside UI context, or for redo */
2430 static int screen_maximize_area_exec(bContext *C, wmOperator *op)
2431 {
2432         bScreen *screen = CTX_wm_screen(C);
2433         ScrArea *sa = NULL;
2434         const bool hide_panels = RNA_boolean_get(op->ptr, "use_hide_panels");
2435         
2436         /* search current screen for 'fullscreen' areas */
2437         /* prevents restoring info header, when mouse is over it */
2438         for (sa = screen->areabase.first; sa; sa = sa->next) {
2439                 if (sa->full) break;
2440         }
2441         
2442         if (sa == NULL) {
2443                 sa = CTX_wm_area(C);
2444         }
2445         
2446         if (hide_panels) {
2447                 if (!ELEM(screen->state, SCREENNORMAL, SCREENFULL)) {
2448                         return OPERATOR_CANCELLED;
2449                 }
2450                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENFULL);
2451         }
2452         else {
2453                 if (!ELEM(screen->state, SCREENNORMAL, SCREENMAXIMIZED)) {
2454                         return OPERATOR_CANCELLED;
2455                 }
2456                 ED_screen_state_toggle(C, CTX_wm_window(C), sa, SCREENMAXIMIZED);
2457         }
2458
2459         return OPERATOR_FINISHED;
2460 }
2461
2462 static void SCREEN_OT_screen_full_area(wmOperatorType *ot)
2463 {
2464         PropertyRNA *prop;
2465
2466         ot->name = "Toggle Maximize Area";
2467         ot->description = "Toggle display selected area as fullscreen/maximized";
2468         ot->idname = "SCREEN_OT_screen_full_area";
2469         
2470         ot->exec = screen_maximize_area_exec;
2471         ot->poll = ED_operator_areaactive;
2472         ot->flag = 0;
2473
2474         prop = RNA_def_boolean(ot->srna, "use_hide_panels", false, "Hide Panels", "Hide all the panels");
2475         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2476 }
2477
2478 /* ************** join area operator ********************************************** */
2479
2480 /* operator state vars used:  
2481  * x1, y1     mouse coord in first area, which will disappear
2482  * x2, y2     mouse coord in 2nd area, which will become joined
2483  * 
2484  * functions:
2485  * 
2486  * init()   find edge based on state vars 
2487  * test if the edge divides two areas, 
2488  * store active and nonactive area,
2489  * 
2490  * apply()  do the actual join
2491  * 
2492  * exit()       cleanup, send notifier
2493  * 
2494  * callbacks:
2495  * 
2496  * exec()       calls init, apply, exit 
2497  * 
2498  * invoke() sets mouse coords in x,y
2499  * call init()
2500  * add modal handler
2501  * 
2502  * modal()      accept modal events while doing it
2503  * call apply() with active window and nonactive window
2504  * call exit() and remove handler when LMB confirm
2505  */
2506
2507 typedef struct sAreaJoinData {
2508         ScrArea *sa1;   /* first area to be considered */
2509         ScrArea *sa2;   /* second area to be considered */
2510         ScrArea *scr;   /* designed for removal */
2511
2512 } sAreaJoinData;
2513
2514
2515 /* validate selection inside screen, set variables OK */
2516 /* return 0: init failed */
2517 /* XXX todo: find edge based on (x,y) and set other area? */
2518 static int area_join_init(bContext *C, wmOperator *op)
2519 {
2520         ScrArea *sa1, *sa2;
2521         sAreaJoinData *jd = NULL;
2522         int x1, y1;
2523         int x2, y2;
2524         int shared = 0;
2525         
2526         /* required properties, make negative to get return 0 if not set by caller */
2527         x1 = RNA_int_get(op->ptr, "min_x");
2528         y1 = RNA_int_get(op->ptr, "min_y");
2529         x2 = RNA_int_get(op->ptr, "max_x");
2530         y2 = RNA_int_get(op->ptr, "max_y");
2531         
2532         sa1 = BKE_screen_find_area_xy(CTX_wm_screen(C), SPACE_TYPE_ANY, x1, y1);
2533         sa2 = BKE_screen_find_area_xy(CTX_wm_screen(C), SPACE_TYPE_ANY, x2, y2);
2534         if (sa1 == NULL || sa2 == NULL || sa1 == sa2)
2535                 return 0;
2536         
2537         /* do areas share an edge? */
2538         if (sa1->v1 == sa2->v1 || sa1->v1 == sa2->v2 || sa1->v1 == sa2->v3 || sa1->v1 == sa2->v4) shared++;
2539         if (sa1->v2 == sa2->v1 || sa1->v2 == sa2->v2 || sa1->v2 == sa2->v3 || sa1->v2 == sa2->v4) shared++;
2540         if (sa1->v3 == sa2->v1 || sa1->v3 == sa2->v2 || sa1->v3 == sa2->v3 || sa1->v3 == sa2->v4) shared++;
2541         if (sa1->v4 == sa2->v1 || sa1->v4 == sa2->v2 || sa1->v4 == sa2->v3 || sa1->v4 == sa2->v4) shared++;
2542         if (shared != 2) {
2543                 printf("areas don't share edge\n");
2544                 return 0;
2545         }
2546         
2547         jd = (sAreaJoinData *)MEM_callocN(sizeof(sAreaJoinData), "op_area_join");
2548         
2549         jd->sa1 = sa1;
2550         jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2551         jd->sa2 = sa2;
2552         jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2553         
2554         op->customdata = jd;
2555         
2556         return 1;
2557 }
2558
2559 /* apply the join of the areas (space types) */
2560 static int area_join_apply(bContext *C, wmOperator *op)
2561 {
2562         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2563         if (!jd) return 0;
2564         
2565         if (!screen_area_join(C, CTX_wm_screen(C), jd->sa1, jd->sa2)) {
2566                 return 0;
2567         }
2568         if (CTX_wm_area(C) == jd->sa2) {
2569                 CTX_wm_area_set(C, NULL);
2570                 CTX_wm_region_set(C, NULL);
2571         }
2572         
2573         return 1;
2574 }
2575
2576 /* finish operation */
2577 static void area_join_exit(bContext *C, wmOperator *op)
2578 {
2579         if (op->customdata) {
2580                 MEM_freeN(op->customdata);
2581                 op->customdata = NULL;
2582         }
2583         
2584         /* this makes sure aligned edges will result in aligned grabbing */
2585         removedouble_scredges(CTX_wm_screen(C));
2586         removenotused_scredges(CTX_wm_screen(C));
2587         removenotused_scrverts(CTX_wm_screen(C));
2588 }
2589
2590 static int area_join_exec(bContext *C, wmOperator *op)
2591 {
2592         if (!area_join_init(C, op)) 
2593                 return OPERATOR_CANCELLED;
2594         
2595         area_join_apply(C, op);
2596         area_join_exit(C, op);
2597         
2598         return OPERATOR_FINISHED;
2599 }
2600
2601 /* interaction callback */
2602 static int area_join_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2603 {
2604         
2605         if (event->type == EVT_ACTIONZONE_AREA) {
2606                 sActionzoneData *sad = event->customdata;
2607                 
2608                 if (sad == NULL || sad->modifier > 0) {
2609                         return OPERATOR_PASS_THROUGH;
2610                 }
2611                 
2612                 /* verify *sad itself */
2613                 if (sad->sa1 == NULL || sad->sa2 == NULL)
2614                         return OPERATOR_PASS_THROUGH;
2615                 
2616                 /* is this our *sad? if areas equal it should be passed on */
2617                 if (sad->sa1 == sad->sa2)
2618                         return OPERATOR_PASS_THROUGH;
2619                 
2620                 /* prepare operator state vars */
2621                 RNA_int_set(op->ptr, "min_x", sad->x);
2622                 RNA_int_set(op->ptr, "min_y", sad->y);
2623                 RNA_int_set(op->ptr, "max_x", event->x);
2624                 RNA_int_set(op->ptr, "max_y", event->y);
2625         }
2626         
2627         
2628         if (!area_join_init(C, op)) 
2629                 return OPERATOR_PASS_THROUGH;
2630         
2631         /* add temp handler */
2632         WM_event_add_modal_handler(C, op);
2633         
2634         return OPERATOR_RUNNING_MODAL;
2635 }
2636
2637 static void area_join_cancel(bContext *C, wmOperator *op)
2638 {
2639         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2640         
2641         if (jd->sa1) {
2642                 jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2643                 jd->sa1->flag &= ~AREA_FLAG_DRAWJOINTO;
2644         }
2645         if (jd->sa2) {
2646                 jd->sa2->flag &= ~AREA_FLAG_DRAWJOINFROM;
2647                 jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2648         }
2649         
2650         WM_event_add_notifier(C, NC_WINDOW, NULL);
2651         
2652         area_join_exit(C, op);
2653 }
2654
2655 /* modal callback while selecting area (space) that will be removed */
2656 static int area_join_modal(bContext *C, wmOperator *op, const wmEvent *event)
2657 {
2658         bScreen *sc = CTX_wm_screen(C);
2659         sAreaJoinData *jd = (sAreaJoinData *)op->customdata;
2660         
2661         /* execute the events */
2662         switch (event->type) {
2663                         
2664                 case MOUSEMOVE: 
2665                 {
2666                         ScrArea *sa = BKE_screen_find_area_xy(sc, SPACE_TYPE_ANY, event->x, event->y);
2667                         int dir;
2668                         
2669                         if (sa) {
2670                                 if (jd->sa1 != sa) {
2671                                         dir = area_getorientation(jd->sa1, sa);
2672                                         if (dir != -1) {
2673                                                 if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2674                                                 jd->sa2 = sa;
2675                                                 jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2676                                         }
2677                                         else {
2678                                                 /* we are not bordering on the previously selected area 
2679                                                  * we check if area has common border with the one marked for removal
2680                                                  * in this case we can swap areas.
2681                                                  */
2682                                                 dir = area_getorientation(sa, jd->sa2);
2683                                                 if (dir != -1) {
2684                                                         if (jd->sa1) jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2685                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2686                                                         jd->sa1 = jd->sa2;
2687                                                         jd->sa2 = sa;
2688                                                         if (jd->sa1) jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2689                                                         if (jd->sa2) jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2690                                                 }
2691                                                 else {
2692                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2693                                                         jd->sa2 = NULL;
2694                                                 }
2695                                         }
2696                                         WM_event_add_notifier(C, NC_WINDOW, NULL);
2697                                 }
2698                                 else {
2699                                         /* we are back in the area previously selected for keeping 
2700                                          * we swap the areas if possible to allow user to choose */
2701                                         if (jd->sa2 != NULL) {
2702                                                 if (jd->sa1) jd->sa1->flag &= ~AREA_FLAG_DRAWJOINFROM;
2703                                                 if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2704                                                 jd->sa1 = jd->sa2;
2705                                                 jd->sa2 = sa;
2706                                                 if (jd->sa1) jd->sa1->flag |= AREA_FLAG_DRAWJOINFROM;
2707                                                 if (jd->sa2) jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2708                                                 dir = area_getorientation(jd->sa1, jd->sa2);
2709                                                 if (dir == -1) {
2710                                                         printf("oops, didn't expect that!\n");
2711                                                 }
2712                                         }
2713                                         else {
2714                                                 dir = area_getorientation(jd->sa1, sa);
2715                                                 if (dir != -1) {
2716                                                         if (jd->sa2) jd->sa2->flag &= ~AREA_FLAG_DRAWJOINTO;
2717                                                         jd->sa2 = sa;
2718                                                         jd->sa2->flag |= AREA_FLAG_DRAWJOINTO;
2719                                                 }
2720                                         }
2721                                         WM_event_add_notifier(C, NC_WINDOW, NULL);
2722                                 }
2723                         }
2724                         break;
2725                 }
2726                 case LEFTMOUSE:
2727                         if (event->val == KM_RELEASE) {
2728                                 ED_area_tag_redraw(jd->sa1);
2729                                 ED_area_tag_redraw(jd->sa2);
2730
2731                                 area_join_apply(C, op);
2732                                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
2733                                 area_join_exit(C, op);
2734                                 return OPERATOR_FINISHED;
2735                         }
2736                         break;
2737                         
2738                 case RIGHTMOUSE:
2739                 case ESCKEY:
2740                         area_join_cancel(C, op);
2741                         return OPERATOR_CANCELLED;
2742         }
2743         
2744         return OPERATOR_RUNNING_MODAL;
2745 }
2746
2747 /* Operator for joining two areas (space types) */
2748 static void SCREEN_OT_area_join(wmOperatorType *ot)
2749 {
2750         /* identifiers */
2751         ot->name = "Join Area";
2752         ot->description = "Join selected areas into new window";
2753         ot->idname = "SCREEN_OT_area_join";
2754         
2755         /* api callbacks */
2756         ot->exec = area_join_exec;
2757         ot->invoke = area_join_invoke;
2758         ot->modal = area_join_modal;
2759         ot->poll = screen_active_editable;
2760         ot->cancel = area_join_cancel;
2761         
2762         /* flags */
2763         ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL;
2764         
2765         /* rna */
2766         RNA_def_int(ot->srna, "min_x", -100, INT_MIN, INT_MAX, "X 1", "", INT_MIN, INT_MAX);
2767         RNA_def_int(ot->srna, "min_y", -100, INT_MIN, INT_MAX, "Y 1", "", INT_MIN, INT_MAX);
2768         RNA_def_int(ot->srna, "max_x", -100, INT_MIN, INT_MAX, "X 2", "", INT_MIN, INT_MAX);
2769         RNA_def_int(ot->srna, "max_y", -100, INT_MIN, INT_MAX, "Y 2", "", INT_MIN, INT_MAX);
2770 }
2771
2772 /* ******************************* */
2773
2774 static int screen_area_options_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2775 {
2776         wmWindow *win = CTX_wm_window(C);
2777         bScreen *sc = CTX_wm_screen(C);
2778         uiPopupMenu *pup;
2779         uiLayout *layout;
2780         PointerRNA ptr;
2781         ScrEdge *actedge;
2782         const int winsize_x = WM_window_pixels_x(win);
2783         const int winsize_y = WM_window_pixels_y(win);
2784
2785         actedge = screen_find_active_scredge(sc, winsize_x, winsize_y, event->x, event->y);
2786         
2787         if (actedge == NULL) return OPERATOR_CANCELLED;
2788         
2789         pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2790         layout = UI_popup_menu_layout(pup);
2791         
2792         ptr = uiItemFullO(layout, "SCREEN_OT_area_split", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
2793         /* store initial mouse cursor position */
2794         RNA_int_set(&ptr, "mouse_x", event->x);
2795         RNA_int_set(&ptr, "mouse_y", event->y);
2796
2797         ptr = uiItemFullO(layout, "SCREEN_OT_area_join", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
2798         /* mouse cursor on edge, '4' can fail on wide edges... */
2799         RNA_int_set(&ptr, "min_x", event->x + 4);
2800         RNA_int_set(&ptr, "min_y", event->y + 4);
2801         RNA_int_set(&ptr, "max_x", event->x - 4);
2802         RNA_int_set(&ptr, "max_y", event->y - 4);
2803         
2804         UI_popup_menu_end(C, pup);
2805         
2806         return OPERATOR_INTERFACE;
2807 }
2808
2809 static void SCREEN_OT_area_options(wmOperatorType *ot)
2810 {
2811         /* identifiers */
2812         ot->name = "Area Options";
2813         ot->description = "Operations for splitting and merging";
2814         ot->idname = "SCREEN_OT_area_options";
2815         
2816         /* api callbacks */
2817         ot->invoke = screen_area_options_invoke;
2818         
2819         ot->poll = ED_operator_screen_mainwinactive;
2820
2821         /* flags */
2822         ot->flag = OPTYPE_INTERNAL;
2823 }
2824
2825
2826 /* ******************************* */
2827
2828
2829 static int spacedata_cleanup_exec(bContext *C, wmOperator *op)
2830 {
2831         Main *bmain = CTX_data_main(C);
2832         bScreen *screen;
2833         ScrArea *sa;
2834         int tot = 0;
2835         
2836         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
2837                 for (sa = screen->areabase.first; sa; sa = sa->next) {
2838                         if (sa->spacedata.first != sa->spacedata.last) {
2839                                 SpaceLink *sl = sa->spacedata.first;
2840
2841                                 BLI_remlink(&sa->spacedata, sl);
2842                                 tot += BLI_listbase_count(&sa->spacedata);
2843                                 BKE_spacedata_freelist(&sa->spacedata);
2844                                 BLI_addtail(&sa->spacedata, sl);
2845                         }
2846                 }
2847         }
2848         BKE_reportf(op->reports, RPT_INFO, "Removed amount of editors: %d", tot);
2849         
2850         return OPERATOR_FINISHED;
2851 }
2852
2853 static void SCREEN_OT_spacedata_cleanup(wmOperatorType *ot)
2854 {
2855         /* identifiers */
2856         ot->name = "Clean-up Space-data";
2857         ot->description = "Remove unused settings for invisible editors";
2858         ot->idname = "SCREEN_OT_spacedata_cleanup";
2859         
2860         /* api callbacks */
2861         ot->exec = spacedata_cleanup_exec;
2862         ot->poll = WM_operator_winactive;
2863         
2864 }
2865
2866 /* ************** repeat last operator ***************************** */
2867
2868 static int repeat_last_exec(bContext *C, wmOperator *UNUSED(op))
2869 {
2870         wmWindowManager *wm = CTX_wm_manager(C);
2871         wmOperator *lastop = wm->operators.last;
2872
2873         /* Seek last registered operator */
2874         while (lastop) {
2875                 if (lastop->type->flag & OPTYPE_REGISTER) {
2876                         break;
2877                 }
2878                 else {
2879                         lastop = lastop->prev;
2880                 }
2881         }
2882
2883         if (lastop) {
2884                 WM_operator_free_all_after(wm, lastop);
2885                 WM_operator_repeat(C, lastop);
2886         }
2887         
2888         return OPERATOR_CANCELLED;
2889 }
2890
2891 static void SCREEN_OT_repeat_last(wmOperatorType *ot)
2892 {
2893         /* identifiers */
2894         ot->name = "Repeat Last";
2895         ot->description = "Repeat last action";
2896         ot->idname = "SCREEN_OT_repeat_last";
2897         
2898         /* api callbacks */
2899         ot->exec = repeat_last_exec;
2900         
2901         ot->poll = ED_operator_screenactive;
2902         
2903 }
2904
2905 static int repeat_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
2906 {
2907         wmWindowManager *wm = CTX_wm_manager(C);
2908         wmOperator *lastop;
2909         uiPopupMenu *pup;
2910         uiLayout *layout;
2911         int items, i;
2912         
2913         items = BLI_listbase_count(&wm->operators);
2914         if (items == 0)
2915                 return OPERATOR_CANCELLED;
2916         
2917         pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2918         layout = UI_popup_menu_layout(pup);
2919         
2920         for (i = items - 1, lastop = wm->operators.last; lastop; lastop = lastop->prev, i--)
2921                 if ((lastop->type->flag & OPTYPE_REGISTER) && WM_operator_repeat_check(C, lastop)) {
2922                         uiItemIntO(layout, RNA_struct_ui_name(lastop->type->srna), ICON_NONE, op->type->idname, "index", i);
2923                 }
2924         
2925         UI_popup_menu_end(C, pup);
2926         
2927         return OPERATOR_INTERFACE;
2928 }
2929
2930 static int repeat_history_exec(bContext *C, wmOperator *op)
2931 {
2932         wmWindowManager *wm = CTX_wm_manager(C);
2933         
2934         op = BLI_findlink(&wm->operators, RNA_int_get(op->ptr, "index"));
2935         if (op) {
2936                 /* let's put it as last operator in list */
2937                 BLI_remlink(&wm->operators, op);
2938                 BLI_addtail(&wm->operators, op);
2939                 
2940                 WM_operator_repeat(C, op);
2941         }
2942         
2943         return OPERATOR_FINISHED;
2944 }
2945
2946 static void SCREEN_OT_repeat_history(wmOperatorType *ot)
2947 {
2948         /* identifiers */
2949         ot->name = "Repeat History";
2950         ot->description = "Display menu for previous actions performed";
2951         ot->idname = "SCREEN_OT_repeat_history";
2952         
2953         /* api callbacks */
2954         ot->invoke = repeat_history_invoke;
2955         ot->exec = repeat_history_exec;
2956         
2957         ot->poll = ED_operator_screenactive;
2958         
2959         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, 1000);
2960 }
2961
2962 /* ********************** redo operator ***************************** */
2963
2964 static int redo_last_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
2965 {
2966         wmOperator *lastop = WM_operator_last_redo(C);
2967         
2968         if (lastop)
2969                 WM_operator_redo_popup(C, lastop);
2970         
2971         return OPERATOR_CANCELLED;
2972 }
2973
2974 static void SCREEN_OT_redo_last(wmOperatorType *ot)
2975 {
2976         /* identifiers */
2977         ot->name = "Redo Last";
2978         ot->description = "Display menu for last action performed";
2979         ot->idname = "SCREEN_OT_redo_last";
2980         
2981         /* api callbacks */
2982         ot->invoke = redo_last_invoke;
2983         
2984         ot->poll = ED_operator_screenactive;
2985 }
2986
2987 /* ************** region four-split operator ***************************** */
2988
2989 static void view3d_localview_update_rv3d(struct RegionView3D *rv3d)
2990 {
2991         if (rv3d->localvd) {
2992                 rv3d->localvd->view = rv3d->view;
2993                 rv3d->localvd->persp = rv3d->persp;
2994                 copy_qt_qt(rv3d->localvd->viewquat, rv3d->viewquat);
2995         }
2996 }
2997
2998 static void region_quadview_init_rv3d(ScrArea *sa, ARegion *ar,
2999                                       const char viewlock, const char view, const char persp)
3000 {
3001         RegionView3D *rv3d = ar->regiondata;
3002
3003         if (persp == RV3D_CAMOB) {
3004                 ED_view3d_lastview_store(rv3d);
3005         }
3006
3007         rv3d->viewlock = viewlock;
3008         rv3d->view = view;
3009         rv3d->persp = persp;
3010
3011         ED_view3d_lock(rv3d);
3012         view3d_localview_update_rv3d(rv3d);
3013         if ((viewlock & RV3D_BOXCLIP) && (persp == RV3D_ORTHO)) {
3014                 ED_view3d_quadview_update(sa, ar, true);
3015         }
3016 }
3017
3018 /* insert a region in the area region list */
3019 static int region_quadview_exec(bContext *C, wmOperator *op)
3020 {
3021         ARegion *ar = CTX_wm_region(C);
3022         
3023         /* some rules... */
3024         if (ar->regiontype != RGN_TYPE_WINDOW) {
3025                 BKE_report(op->reports, RPT_ERROR, "Only window region can be 4-splitted");
3026         }
3027         else if (ar->alignment == RGN_ALIGN_QSPLIT) {
3028                 /* Exit quad-view */
3029                 ScrArea *sa = CTX_wm_area(C);
3030                 ARegion *arn;
3031                 
3032                 /* keep current region */
3033                 ar->alignment = 0;
3034                 
3035                 if (sa->spacetype == SPACE_VIEW3D) {
3036                         ARegion *ar_iter;
3037                         RegionView3D *rv3d = ar->regiondata;
3038
3039                         /* if this is a locked view, use settings from 'User' view */
3040                         if (rv3d->viewlock) {
3041                                 View3D *v3d_user;
3042                                 ARegion *ar_user;
3043
3044                                 if (ED_view3d_context_user_region(C, &v3d_user, &ar_user)) {
3045                                         if (ar != ar_user) {
3046                                                 SWAP(void *, ar->regiondata, ar_user->regiondata);
3047                                                 rv3d = ar->regiondata;
3048                                         }
3049                                 }
3050                         }
3051
3052                         rv3d->viewlock_quad = RV3D_VIEWLOCK_INIT;
3053                         rv3d->viewlock = 0;
3054                         rv3d->rflag &= ~RV3D_CLIPPING;
3055
3056                         /* accumulate locks, incase they're mixed */
3057                         for (ar_iter = sa->regionbase.first; ar_iter; ar_iter = ar_iter->next) {
3058                                 if (ar_iter->regiontype == RGN_TYPE_WINDOW) {
3059                                         RegionView3D *rv3d_iter = ar_iter->regiondata;
3060                                         rv3d->viewlock_quad |= rv3d_iter->viewlock;
3061                                 }
3062                         }
3063                 }
3064                 
3065                 for (ar = sa->regionbase.first; ar; ar = arn) {
3066                         arn = ar->next;
3067                         if (ar->alignment == RGN_ALIGN_QSPLIT) {
3068                                 ED_region_exit(C, ar);
3069                                 BKE_area_region_free(sa->type, ar);
3070                                 BLI_remlink(&sa->regionbase, ar);
3071                                 MEM_freeN(ar);
3072                         }
3073                 }
3074                 ED_area_tag_redraw(sa);
3075                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
3076         }
3077         else if (ar->next) {
3078                 BKE_report(op->reports, RPT_ERROR, "Only last region can be 4-splitted");
3079         }
3080         else {
3081                 /* Enter quad-view */
3082                 ScrArea *sa = CTX_wm_area(C);
3083                 ARegion *newar;
3084                 int count;
3085                 
3086                 ar->alignment = RGN_ALIGN_QSPLIT;
3087                 
3088                 for (count = 0; count < 3; count++) {
3089                         newar = BKE_area_region_copy(sa->type, ar);
3090                         BLI_addtail(&sa->regionbase, newar);
3091                 }
3092                 
3093                 /* lock views and set them */
3094                 if (sa->spacetype == SPACE_VIEW3D) {
3095                         View3D *v3d = sa->spacedata.first;
3096                         int index_qsplit = 0;
3097
3098                         /* run ED_view3d_lock() so the correct 'rv3d->viewquat' is set,
3099                          * otherwise when restoring rv3d->localvd the 'viewquat' won't
3100                          * match the 'view', set on entering localview See: [#26315],
3101                          *
3102                          * We could avoid manipulating rv3d->localvd here if exiting
3103                          * localview with a 4-split would assign these view locks */
3104                         RegionView3D *rv3d = ar->regiondata;
3105                         const char viewlock = (rv3d->viewlock_quad & RV3D_VIEWLOCK_INIT) ?
3106                                               (rv3d->viewlock_quad & ~RV3D_VIEWLOCK_INIT) : RV3D_LOCKED;
3107
3108                         region_quadview_init_rv3d(sa, ar,              viewlock, ED_view3d_lock_view_from_index(index_qsplit++), RV3D_ORTHO);
3109                         region_quadview_init_rv3d(sa, (ar = ar->next), viewlock, ED_view3d_lock_view_from_index(index_qsplit++), RV3D_ORTHO);
3110                         region_quadview_init_rv3d(sa, (ar = ar->next), viewlock, ED_view3d_lock_view_from_index(index_qsplit++), RV3D_ORTHO);
3111                         /* forcing camera is distracting */
3112 #if 0
3113                         if (v3d->camera) region_quadview_init_rv3d(sa, (ar = ar->next), 0, RV3D_VIEW_CAMERA, RV3D_CAMOB);
3114                         else             region_quadview_init_rv3d(sa, (ar = ar->next), 0, RV3D_VIEW_USER,   RV3D_PERSP);
3115 #else
3116                         (void)v3d;
3117 #endif
3118                 }
3119                 ED_area_tag_redraw(sa);
3120                 WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
3121         }
3122         
3123         
3124         return OPERATOR_FINISHED;
3125 }
3126
3127 static void SCREEN_OT_region_quadview(wmOperatorType *ot)
3128 {
3129         /* identifiers */
3130         ot->name = "Toggle Quad View";
3131         ot->description = "Split selected area into camera, front, right & top views";
3132         ot->idname = "SCREEN_OT_region_quadview";
3133         
3134         /* api callbacks */
3135         ot->exec = region_quadview_exec;
3136         ot->poll = ED_operator_region_view3d_active;
3137         ot->flag = 0;
3138 }
3139
3140
3141 /* ************** region flip operator ***************************** */
3142
3143 /* flip a region alignment */
3144 static int region_flip_exec(bContext *C, wmOperator *UNUSED(op))
3145 {
3146         ARegion *ar = CTX_wm_region(C);
3147         
3148         if (!ar)
3149                 return OPERATOR_CANCELLED;
3150         
3151         if (ar->alignment == RGN_ALIGN_TOP)
3152                 ar->alignment = RGN_ALIGN_BOTTOM;
3153         else if (ar->alignment == RGN_ALIGN_BOTTOM)
3154                 ar->alignment = RGN_ALIGN_TOP;
3155         else if (ar->alignment == RGN_ALIGN_LEFT)
3156                 ar->alignment = RGN_ALIGN_RIGHT;
3157         else if (ar->alignment == RGN_ALIGN_RIGHT)
3158                 ar->alignment = RGN_ALIGN_LEFT;
3159
3160         ED_area_tag_redraw(CTX_wm_area(C));
3161         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
3162         
3163         return OPERATOR_FINISHED;
3164 }
3165
3166
3167 static void SCREEN_OT_region_flip(wmOperatorType *ot)
3168 {
3169         /* identifiers */
3170         ot->name = "Flip Region";
3171         ot->idname = "SCREEN_OT_region_flip";
3172         ot->description = "Toggle the region's alignment (left/right or top/bottom)";
3173         
3174         /* api callbacks */
3175         ot->exec = region_flip_exec;
3176         ot->poll = ED_operator_areaactive;
3177         ot->flag = 0;
3178 }
3179
3180 /* ************** header operator ***************************** */
3181 static int header_exec(bContext *C, wmOperator *UNUSED(op))
3182 {
3183         ARegion *ar = screen_find_region_type(C, RGN_TYPE_HEADER);
3184
3185         if (ar == NULL) {
3186                 return OPERATOR_CANCELLED;
3187         }
3188
3189         ar->flag ^= RGN_FLAG_HIDDEN;
3190
3191         ED_area_tag_redraw(CTX_wm_area(C));
3192
3193         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
3194
3195         return OPERATOR_FINISHED;
3196 }
3197
3198 static void SCREEN_OT_header(wmOperatorType *ot)
3199 {
3200         /* identifiers */
3201         ot->name = "Toggle Header";
3202         ot->description = "Toggle header display";
3203         ot->idname = "SCREEN_OT_header";
3204
3205         /* api callbacks */
3206         ot->exec = header_exec;
3207 }
3208
3209 /* ************** header flip operator ***************************** */
3210
3211 /* flip a header region alignment */
3212 static int header_flip_exec(bContext *C, wmOperator *UNUSED(op))
3213 {
3214         ARegion *ar = screen_find_region_type(C, RGN_TYPE_HEADER);
3215
3216         if (ar == NULL) {
3217                 return OPERATOR_CANCELLED;
3218         }
3219         
3220         /* copied from SCREEN_OT_region_flip */
3221         if (ar->alignment == RGN_ALIGN_TOP)
3222                 ar->alignment = RGN_ALIGN_BOTTOM;
3223         else if (ar->alignment == RGN_ALIGN_BOTTOM)
3224                 ar->alignment = RGN_ALIGN_TOP;
3225         else if (ar->alignment == RGN_ALIGN_LEFT)
3226                 ar->alignment = RGN_ALIGN_RIGHT;
3227         else if (ar->alignment == RGN_ALIGN_RIGHT)
3228                 ar->alignment = RGN_ALIGN_LEFT;
3229
3230         ED_area_tag_redraw(CTX_wm_area(C));
3231
3232         WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
3233         
3234         return OPERATOR_FINISHED;
3235 }
3236
3237
3238 static void SCREEN_OT_header_flip(wmOperatorType *ot)
3239 {
3240         /* identifiers */
3241         ot->name = "Flip Header Region";
3242         ot->idname = "SCREEN_OT_header_flip";
3243         ot->description = "Toggle the header over/below the main window area";
3244         
3245         /* api callba