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