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