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