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