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