code cleanup: use const events for modal and invoke operators.
[blender.git] / source / blender / editors / space_clip / space_clip.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) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/space_clip.c
29  *  \ingroup spclip
30  */
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_scene_types.h"
36 #include "DNA_mask_types.h"
37 #include "DNA_movieclip_types.h"
38 #include "DNA_view3d_types.h"  /* for pivot point */
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_math.h"
45
46 #include "BKE_main.h"
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49 #include "BKE_movieclip.h"
50 #include "BKE_tracking.h"
51
52 #include "IMB_imbuf_types.h"
53
54 #include "ED_mask.h"
55 #include "ED_space_api.h"
56 #include "ED_screen.h"
57 #include "ED_clip.h"
58 #include "ED_transform.h"
59
60 #include "IMB_imbuf.h"
61
62 #include "BIF_gl.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69 #include "UI_view2d.h"
70
71 #include "RNA_access.h"
72
73
74 #include "clip_intern.h"  /* own include */
75
76 static void init_preview_region(const bContext *C, ARegion *ar)
77 {
78         Scene *scene = CTX_data_scene(C);
79         ScrArea *sa = CTX_wm_area(C);
80         SpaceClip *sc = CTX_wm_space_clip(C);
81
82         ar->regiontype = RGN_TYPE_PREVIEW;
83         ar->alignment = RGN_ALIGN_TOP;
84         ar->flag |= RGN_FLAG_HIDDEN;
85
86         if (sc->view == SC_VIEW_DOPESHEET) {
87                 ar->v2d.tot.xmin = -10.0f;
88                 ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
89                 ar->v2d.tot.xmax = (float)(sa->winx);
90                 ar->v2d.tot.ymax = 0.0f;
91
92                 ar->v2d.cur = ar->v2d.tot;
93
94                 ar->v2d.min[0] = 0.0f;
95                 ar->v2d.min[1] = 0.0f;
96
97                 ar->v2d.max[0] = MAXFRAMEF;
98                 ar->v2d.max[1] = FLT_MAX;
99
100                 ar->v2d.minzoom = 0.01f;
101                 ar->v2d.maxzoom = 50;
102                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
103                 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
104                 ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
105                 ar->v2d.keepofs = V2D_KEEPOFS_Y;
106                 ar->v2d.align = V2D_ALIGN_NO_POS_Y;
107                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
108         }
109         else {
110                 ar->v2d.tot.xmin = 0.0f;
111                 ar->v2d.tot.ymin = -10.0f;
112                 ar->v2d.tot.xmax = (float)scene->r.efra;
113                 ar->v2d.tot.ymax = 10.0f;
114
115                 ar->v2d.cur = ar->v2d.tot;
116
117                 ar->v2d.min[0] = FLT_MIN;
118                 ar->v2d.min[1] = FLT_MIN;
119
120                 ar->v2d.max[0] = MAXFRAMEF;
121                 ar->v2d.max[1] = FLT_MAX;
122
123                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
124                 ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
125
126                 ar->v2d.minzoom = 0.0f;
127                 ar->v2d.maxzoom = 0.0f;
128                 ar->v2d.keepzoom = 0;
129                 ar->v2d.keepofs = 0;
130                 ar->v2d.align = 0;
131                 ar->v2d.flag = 0;
132
133                 ar->v2d.keeptot = 0;
134         }
135 }
136
137 static void reinit_preview_region(const bContext *C, ARegion *ar)
138 {
139         SpaceClip *sc = CTX_wm_space_clip(C);
140
141         if (sc->view == SC_VIEW_DOPESHEET) {
142                 if ((ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) == 0)
143                         init_preview_region(C, ar);
144         }
145         else {
146                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL)
147                         init_preview_region(C, ar);
148         }
149 }
150
151 static ARegion *ED_clip_has_preview_region(const bContext *C, ScrArea *sa)
152 {
153         ARegion *ar, *arnew;
154
155         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
156         if (ar)
157                 return ar;
158
159         /* add subdiv level; after header */
160         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
161
162         /* is error! */
163         if (ar == NULL)
164                 return NULL;
165
166         arnew = MEM_callocN(sizeof(ARegion), "clip preview region");
167
168         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
169         init_preview_region(C, arnew);
170
171         return arnew;
172 }
173
174 static ARegion *ED_clip_has_channels_region(ScrArea *sa)
175 {
176         ARegion *ar, *arnew;
177
178         ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
179         if (ar)
180                 return ar;
181
182         /* add subdiv level; after header */
183         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
184
185         /* is error! */
186         if (ar == NULL)
187                 return NULL;
188
189         arnew = MEM_callocN(sizeof(ARegion), "clip channels region");
190
191         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
192         arnew->regiontype = RGN_TYPE_CHANNELS;
193         arnew->alignment = RGN_ALIGN_LEFT;
194
195         arnew->v2d.scroll = V2D_SCROLL_BOTTOM;
196         arnew->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
197
198         return arnew;
199 }
200
201 static void clip_scopes_tag_refresh(ScrArea *sa)
202 {
203         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
204         ARegion *ar;
205
206         if (sc->mode != SC_MODE_TRACKING)
207                 return;
208
209         /* only while proeprties are visible */
210         for (ar = sa->regionbase.first; ar; ar = ar->next) {
211                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
212                         return;
213         }
214
215         sc->scopes.ok = FALSE;
216 }
217
218 static void clip_scopes_check_gpencil_change(ScrArea *sa)
219 {
220         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
221
222         if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
223                 clip_scopes_tag_refresh(sa);
224         }
225 }
226
227 static void clip_stabilization_tag_refresh(ScrArea *sa)
228 {
229         SpaceClip *sc = (SpaceClip *) sa->spacedata.first;
230         MovieClip *clip = ED_space_clip_get_clip(sc);
231
232         if (clip) {
233                 MovieTrackingStabilization *stab = &clip->tracking.stabilization;
234
235                 stab->ok = FALSE;
236         }
237 }
238
239 /* ******************** default callbacks for clip space ***************** */
240
241 static SpaceLink *clip_new(const bContext *C)
242 {
243         ARegion *ar;
244         SpaceClip *sc;
245
246         sc = MEM_callocN(sizeof(SpaceClip), "initclip");
247         sc->spacetype = SPACE_CLIP;
248         sc->flag = SC_SHOW_MARKER_PATTERN | SC_SHOW_TRACK_PATH | SC_MANUAL_CALIBRATION |
249                    SC_SHOW_GRAPH_TRACKS | SC_SHOW_GRAPH_FRAMES | SC_SHOW_GPENCIL;
250         sc->zoom = 1.0f;
251         sc->path_length = 20;
252         sc->scopes.track_preview_height = 120;
253         sc->around = V3D_LOCAL;
254
255         /* header */
256         ar = MEM_callocN(sizeof(ARegion), "header for clip");
257
258         BLI_addtail(&sc->regionbase, ar);
259         ar->regiontype = RGN_TYPE_HEADER;
260         ar->alignment = RGN_ALIGN_BOTTOM;
261
262         /* tools view */
263         ar = MEM_callocN(sizeof(ARegion), "tools for clip");
264
265         BLI_addtail(&sc->regionbase, ar);
266         ar->regiontype = RGN_TYPE_TOOLS;
267         ar->alignment = RGN_ALIGN_LEFT;
268
269         /* tool properties */
270         ar = MEM_callocN(sizeof(ARegion), "tool properties for clip");
271
272         BLI_addtail(&sc->regionbase, ar);
273         ar->regiontype = RGN_TYPE_TOOL_PROPS;
274         ar->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
275
276         /* properties view */
277         ar = MEM_callocN(sizeof(ARegion), "properties for clip");
278
279         BLI_addtail(&sc->regionbase, ar);
280         ar->regiontype = RGN_TYPE_UI;
281         ar->alignment = RGN_ALIGN_RIGHT;
282
283         /* channels view */
284         ar = MEM_callocN(sizeof(ARegion), "channels for clip");
285
286         BLI_addtail(&sc->regionbase, ar);
287         ar->regiontype = RGN_TYPE_CHANNELS;
288         ar->alignment = RGN_ALIGN_LEFT;
289
290         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
291         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
292
293         /* preview view */
294         ar = MEM_callocN(sizeof(ARegion), "preview for clip");
295
296         BLI_addtail(&sc->regionbase, ar);
297         init_preview_region(C, ar);
298
299         /* main area */
300         ar = MEM_callocN(sizeof(ARegion), "main area for clip");
301
302         BLI_addtail(&sc->regionbase, ar);
303         ar->regiontype = RGN_TYPE_WINDOW;
304
305         return (SpaceLink *) sc;
306 }
307
308 /* not spacelink itself */
309 static void clip_free(SpaceLink *sl)
310 {
311         SpaceClip *sc = (SpaceClip *) sl;
312
313         sc->clip = NULL;
314
315         if (sc->scopes.track_preview)
316                 IMB_freeImBuf(sc->scopes.track_preview);
317
318         if (sc->scopes.track_search)
319                 IMB_freeImBuf(sc->scopes.track_search);
320
321         ED_space_clip_free_texture_buffer(sc);
322 }
323
324 /* spacetype; init callback */
325 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
326 {
327         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
328
329         /* add drop boxes */
330         WM_event_add_dropbox_handler(&sa->handlers, lb);
331 }
332
333 static SpaceLink *clip_duplicate(SpaceLink *sl)
334 {
335         SpaceClip *scn = MEM_dupallocN(sl);
336
337         /* clear or remove stuff from old */
338         scn->scopes.track_search = NULL;
339         scn->scopes.track_preview = NULL;
340         scn->scopes.ok = FALSE;
341         scn->draw_context = NULL;
342
343         return (SpaceLink *)scn;
344 }
345
346 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
347 {
348         /* context changes */
349         switch (wmn->category) {
350                 case NC_SCENE:
351                         switch (wmn->data) {
352                                 case ND_FRAME:
353                                         clip_scopes_tag_refresh(sa);
354                                         /* no break! */
355
356                                 case ND_FRAME_RANGE:
357                                         ED_area_tag_redraw(sa);
358                                         break;
359                         }
360                         break;
361                 case NC_MOVIECLIP:
362                         switch (wmn->data) {
363                                 case ND_DISPLAY:
364                                 case ND_SELECT:
365                                         clip_scopes_tag_refresh(sa);
366                                         ED_area_tag_redraw(sa);
367                                         break;
368                         }
369                         switch (wmn->action) {
370                                 case NA_REMOVED:
371                                 case NA_EDITED:
372                                 case NA_EVALUATED:
373                                         clip_stabilization_tag_refresh(sa);
374                                         /* no break! */
375
376                                 case NA_SELECTED:
377                                         clip_scopes_tag_refresh(sa);
378                                         ED_area_tag_redraw(sa);
379                                         break;
380                         }
381                         break;
382                 case NC_MASK:
383                         switch (wmn->data) {
384                                 case ND_SELECT:
385                                 case ND_DATA:
386                                 case ND_DRAW:
387                                         ED_area_tag_redraw(sa);
388                                         break;
389                         }
390                         switch (wmn->action) {
391                                 case NA_SELECTED:
392                                         ED_area_tag_redraw(sa);
393                                         break;
394                                 case NA_EDITED:
395                                         ED_area_tag_redraw(sa);
396                                         break;
397                         }
398                         break;
399                 case NC_GEOM:
400                         switch (wmn->data) {
401                                 case ND_SELECT:
402                                         clip_scopes_tag_refresh(sa);
403                                         ED_area_tag_redraw(sa);
404                                         break;
405                         }
406                         break;
407                 case NC_SCREEN:
408                         switch (wmn->data) {
409                                 case ND_ANIMPLAY:
410                                         ED_area_tag_redraw(sa);
411                                         break;
412                         }
413                         break;
414                 case NC_SPACE:
415                         if (wmn->data == ND_SPACE_CLIP) {
416                                 clip_scopes_tag_refresh(sa);
417                                 clip_stabilization_tag_refresh(sa);
418                                 ED_area_tag_redraw(sa);
419                         }
420                         break;
421                 case NC_GPENCIL:
422                         if (wmn->action == NA_EDITED) {
423                                 clip_scopes_check_gpencil_change(sa);
424                                 ED_area_tag_redraw(sa);
425                         }
426                         break;
427         }
428 }
429
430 static void clip_operatortypes(void)
431 {
432         /* ** clip_ops.c ** */
433         WM_operatortype_append(CLIP_OT_open);
434         WM_operatortype_append(CLIP_OT_reload);
435         WM_operatortype_append(CLIP_OT_view_pan);
436         WM_operatortype_append(CLIP_OT_view_zoom);
437         WM_operatortype_append(CLIP_OT_view_zoom_in);
438         WM_operatortype_append(CLIP_OT_view_zoom_out);
439         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
440         WM_operatortype_append(CLIP_OT_view_all);
441         WM_operatortype_append(CLIP_OT_view_selected);
442         WM_operatortype_append(CLIP_OT_change_frame);
443         WM_operatortype_append(CLIP_OT_rebuild_proxy);
444         WM_operatortype_append(CLIP_OT_mode_set);
445         WM_operatortype_append(CLIP_OT_view_ndof);
446
447         /* ** clip_toolbar.c ** */
448         WM_operatortype_append(CLIP_OT_tools);
449         WM_operatortype_append(CLIP_OT_properties);
450
451         /* ** tracking_ops.c ** */
452
453         /* navigation */
454         WM_operatortype_append(CLIP_OT_frame_jump);
455
456         /* foorage */
457         WM_operatortype_append(CLIP_OT_set_center_principal);
458
459         /* selection */
460         WM_operatortype_append(CLIP_OT_select);
461         WM_operatortype_append(CLIP_OT_select_all);
462         WM_operatortype_append(CLIP_OT_select_border);
463         WM_operatortype_append(CLIP_OT_select_lasso);
464         WM_operatortype_append(CLIP_OT_select_circle);
465         WM_operatortype_append(CLIP_OT_select_grouped);
466
467         /* markers */
468         WM_operatortype_append(CLIP_OT_add_marker);
469         WM_operatortype_append(CLIP_OT_slide_marker);
470         WM_operatortype_append(CLIP_OT_delete_track);
471         WM_operatortype_append(CLIP_OT_delete_marker);
472
473         /* track */
474         WM_operatortype_append(CLIP_OT_track_markers);
475
476         /* solving */
477         WM_operatortype_append(CLIP_OT_solve_camera);
478         WM_operatortype_append(CLIP_OT_clear_solution);
479
480         WM_operatortype_append(CLIP_OT_disable_markers);
481         WM_operatortype_append(CLIP_OT_hide_tracks);
482         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
483         WM_operatortype_append(CLIP_OT_lock_tracks);
484
485         WM_operatortype_append(CLIP_OT_set_solver_keyframe);
486
487         /* orientation */
488         WM_operatortype_append(CLIP_OT_set_origin);
489         WM_operatortype_append(CLIP_OT_set_plane);
490         WM_operatortype_append(CLIP_OT_set_axis);
491         WM_operatortype_append(CLIP_OT_set_scale);
492         WM_operatortype_append(CLIP_OT_set_solution_scale);
493
494         /* detect */
495         WM_operatortype_append(CLIP_OT_detect_features);
496
497         /* stabilization */
498         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
499         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
500         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
501         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
502
503         /* clean-up */
504         WM_operatortype_append(CLIP_OT_clear_track_path);
505         WM_operatortype_append(CLIP_OT_join_tracks);
506         WM_operatortype_append(CLIP_OT_track_copy_color);
507
508         WM_operatortype_append(CLIP_OT_clean_tracks);
509
510         /* object tracking */
511         WM_operatortype_append(CLIP_OT_tracking_object_new);
512         WM_operatortype_append(CLIP_OT_tracking_object_remove);
513
514         /* clipboard */
515         WM_operatortype_append(CLIP_OT_copy_tracks);
516         WM_operatortype_append(CLIP_OT_paste_tracks);
517
518         /* ** clip_graph_ops.c  ** */
519
520         /* graph editing */
521
522         /* selection */
523         WM_operatortype_append(CLIP_OT_graph_select);
524         WM_operatortype_append(CLIP_OT_graph_select_border);
525         WM_operatortype_append(CLIP_OT_graph_select_all_markers);
526
527         WM_operatortype_append(CLIP_OT_graph_delete_curve);
528         WM_operatortype_append(CLIP_OT_graph_delete_knot);
529         WM_operatortype_append(CLIP_OT_graph_view_all);
530         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
531
532         WM_operatortype_append(CLIP_OT_graph_disable_markers);
533
534         /* ** clip_dopesheet_ops.c  ** */
535
536         WM_operatortype_append(CLIP_OT_dopesheet_select_channel);
537         WM_operatortype_append(CLIP_OT_dopesheet_view_all);
538 }
539
540 static void clip_keymap(struct wmKeyConfig *keyconf)
541 {
542         wmKeyMap *keymap;
543         wmKeyMapItem *kmi;
544
545         /* ******** Global hotkeys avalaible for all regions ******** */
546
547         keymap = WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
548
549         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
550
551         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
552         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
553
554         /* 2d tracking */
555         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
556         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
557         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
558         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
559         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
560         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
561         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
562         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
563         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
564         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
565         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
566         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
567
568         /* mode */
569         WM_keymap_add_menu(keymap, "CLIP_MT_select_mode", TABKEY, KM_PRESS, 0, 0);
570
571         WM_keymap_add_item(keymap, "CLIP_OT_solve_camera", SKEY, KM_PRESS, KM_SHIFT, 0);
572
573         kmi = WM_keymap_add_item(keymap, "CLIP_OT_set_solver_keyframe", QKEY, KM_PRESS, 0, 0);
574         RNA_enum_set(kmi->ptr, "keyframe", 0);
575
576         kmi = WM_keymap_add_item(keymap, "CLIP_OT_set_solver_keyframe", EKEY, KM_PRESS, 0, 0);
577         RNA_enum_set(kmi->ptr, "keyframe", 1);
578
579         /* ******** Hotkeys avalaible for main region only ******** */
580
581         keymap = WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
582 //      keymap->poll = ED_space_clip_tracking_poll;
583         /* ** View/navigation ** */
584
585         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
586         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
587         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
588
589         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
590         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
591         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEPAN, 0, KM_CTRL, 0);
592         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
593         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
594         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
595         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
596
597         /* ctrl now works as well, shift + numpad works as arrow keys on Windows */
598         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 8.0f);
599         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 4.0f);
600         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 2.0f);
601         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
602         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
603         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
604
605         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
606         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
607         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
608         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
609
610         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
611
612         kmi = WM_keymap_add_item(keymap, "CLIP_OT_view_all", FKEY, KM_PRESS, 0, 0);
613         RNA_boolean_set(kmi->ptr, "fit_view", TRUE);
614
615         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
616
617         WM_keymap_add_item(keymap, "CLIP_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0);
618         WM_keymap_add_item(keymap, "CLIP_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
619
620         /* jump to special frame */
621         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
622         RNA_enum_set(kmi->ptr, "position", 0);
623
624         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
625         RNA_enum_set(kmi->ptr, "position", 1);
626
627         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
628         RNA_enum_set(kmi->ptr, "position", 2);
629
630         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
631         RNA_enum_set(kmi->ptr, "position", 3);
632
633         /* "timeline" */
634         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
635
636         /* selection */
637         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
638         RNA_boolean_set(kmi->ptr, "extend", FALSE);
639         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
640         RNA_boolean_set(kmi->ptr, "extend", TRUE);
641         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
642         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
643         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
644         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
645         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
646         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
647         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
648
649         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
650         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
651         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
652         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
653
654         /* marker */
655         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
656
657         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
658         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
659
660         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
661
662         kmi = WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
663         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
664
665         /* tracks */
666         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
667         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
668
669         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
670         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
671
672         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
673         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
674
675         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
676         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
677
678         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
679         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
680
681         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
682
683         /* clean-up */
684         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
685
686         /* menus */
687         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
688
689         /* display */
690         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
691         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
692
693         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", DKEY, KM_PRESS, KM_ALT, 0);
694         RNA_string_set(kmi->ptr, "data_path", "space_data.show_disabled");
695
696         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_ALT, 0);
697         RNA_string_set(kmi->ptr, "data_path", "space_data.show_marker_search");
698
699         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
700         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
701
702         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
703
704         /* clean-up */
705         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
706         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
707         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
708         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
709         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
710         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
711         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
712         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
713         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
714
715         /* ******** Hotkeys avalaible for preview region only ******** */
716
717         keymap = WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
718
719         /* "timeline" */
720         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
721
722         /* selection */
723         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
724         RNA_boolean_set(kmi->ptr, "extend", FALSE);
725         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
726         RNA_boolean_set(kmi->ptr, "extend", TRUE);
727
728         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", AKEY, KM_PRESS, 0, 0);
729         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
730         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", IKEY, KM_PRESS, KM_CTRL, 0);
731         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
732
733         WM_keymap_add_item(keymap, "CLIP_OT_graph_select_border", BKEY, KM_PRESS, 0, 0);
734
735         /* delete */
736         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
737         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
738
739         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
740         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
741
742         /* view */
743         WM_keymap_add_item(keymap, "CLIP_OT_graph_view_all", HOMEKEY, KM_PRESS, 0, 0);
744         WM_keymap_add_item(keymap, "CLIP_OT_graph_center_current_frame", PADPERIOD, KM_PRESS, 0, 0);
745
746         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
747         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_time_cursor");
748
749         /* clean-up */
750         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
751         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
752         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
753         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
754         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
755         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
756         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
757         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
758         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
759
760         /* tracks */
761         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
762         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
763
764         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
765
766         /* ******** Hotkeys avalaible for channels region only ******** */
767
768         keymap = WM_keymap_find(keyconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
769
770         kmi = WM_keymap_add_item(keymap, "CLIP_OT_dopesheet_select_channel", ACTIONMOUSE, KM_PRESS, 0, 0);
771         RNA_boolean_set(kmi->ptr, "extend", TRUE);  /* toggle */
772
773         WM_keymap_add_item(keymap, "CLIP_OT_dopesheet_view_all", HOMEKEY, KM_PRESS, 0, 0);
774 }
775
776 const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL};
777
778 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
779 {
780         SpaceClip *sc = CTX_wm_space_clip(C);
781
782         if (CTX_data_dir(member)) {
783                 CTX_data_dir_set(result, clip_context_dir);
784
785                 return TRUE;
786         }
787         else if (CTX_data_equals(member, "edit_movieclip")) {
788                 if (sc->clip)
789                         CTX_data_id_pointer_set(result, &sc->clip->id);
790                 return TRUE;
791         }
792         else if (CTX_data_equals(member, "edit_mask")) {
793                 if (sc->mask_info.mask)
794                         CTX_data_id_pointer_set(result, &sc->mask_info.mask->id);
795                 return TRUE;
796         }
797
798         return FALSE;
799 }
800
801 /* dropboxes */
802 static int clip_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
803 {
804         if (drag->type == WM_DRAG_PATH)
805                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
806                         return TRUE;
807
808         return FALSE;
809 }
810
811 static void clip_drop_copy(wmDrag *drag, wmDropBox *drop)
812 {
813         PointerRNA itemptr;
814         char dir[FILE_MAX], file[FILE_MAX];
815
816         BLI_split_dirfile(drag->path, dir, file, sizeof(dir), sizeof(file));
817
818         RNA_string_set(drop->ptr, "directory", dir);
819
820         RNA_collection_clear(drop->ptr, "files");
821         RNA_collection_add(drop->ptr, "files", &itemptr);
822         RNA_string_set(&itemptr, "name", file);
823 }
824
825 /* area+region dropbox definition */
826 static void clip_dropboxes(void)
827 {
828         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
829
830         WM_dropbox_add(lb, "CLIP_OT_open", clip_drop_poll, clip_drop_copy);
831 }
832
833 static void clip_refresh(const bContext *C, ScrArea *sa)
834 {
835         wmWindowManager *wm = CTX_wm_manager(C);
836         wmWindow *window = CTX_wm_window(C);
837         Scene *scene = CTX_data_scene(C);
838         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
839         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
840         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
841         ARegion *ar_tool_props = BKE_area_find_region_type(sa, RGN_TYPE_TOOL_PROPS);
842         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
843         ARegion *ar_properties = ED_clip_has_properties_region(sa);
844         ARegion *ar_channels = ED_clip_has_channels_region(sa);
845         int main_visible = FALSE, preview_visible = FALSE, tools_visible = FALSE;
846         int tool_props_visible = FALSE, properties_visible = FALSE, channels_visible = FALSE;
847         int view_changed = FALSE;
848
849         switch (sc->view) {
850                 case SC_VIEW_CLIP:
851                         main_visible = TRUE;
852                         preview_visible = FALSE;
853                         tools_visible = TRUE;
854                         tool_props_visible = TRUE;
855                         properties_visible = TRUE;
856                         channels_visible = FALSE;
857                         break;
858                 case SC_VIEW_GRAPH:
859                         main_visible = FALSE;
860                         preview_visible = TRUE;
861                         tools_visible = FALSE;
862                         tool_props_visible = FALSE;
863                         properties_visible = FALSE;
864                         channels_visible = FALSE;
865
866                         reinit_preview_region(C, ar_preview);
867                         break;
868                 case SC_VIEW_DOPESHEET:
869                         main_visible = FALSE;
870                         preview_visible = TRUE;
871                         tools_visible = FALSE;
872                         tool_props_visible = FALSE;
873                         properties_visible = FALSE;
874                         channels_visible = TRUE;
875
876                         reinit_preview_region(C, ar_preview);
877                         break;
878         }
879
880         if (main_visible) {
881                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
882                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
883                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
884                         view_changed = TRUE;
885                 }
886
887                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
888                         ar_main->alignment = RGN_ALIGN_NONE;
889                         view_changed = TRUE;
890                 }
891         }
892         else {
893                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
894                         ar_main->flag |= RGN_FLAG_HIDDEN;
895                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
896                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
897                         view_changed = TRUE;
898                 }
899                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
900                         ar_main->alignment = RGN_ALIGN_NONE;
901                         view_changed = TRUE;
902                 }
903         }
904
905         if (properties_visible) {
906                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
907                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
908                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
909                         view_changed = TRUE;
910                 }
911                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
912                         ar_properties->alignment = RGN_ALIGN_RIGHT;
913                         view_changed = TRUE;
914                 }
915         }
916         else {
917                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
918                         ar_properties->flag |= RGN_FLAG_HIDDEN;
919                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
920                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
921                         view_changed = TRUE;
922                 }
923                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
924                         ar_properties->alignment = RGN_ALIGN_NONE;
925                         view_changed = TRUE;
926                 }
927         }
928
929         if (tools_visible) {
930                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
931                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
932                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
933                         view_changed = TRUE;
934                 }
935                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
936                         ar_tools->alignment = RGN_ALIGN_LEFT;
937                         view_changed = TRUE;
938                 }
939         }
940         else {
941                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
942                         ar_tools->flag |= RGN_FLAG_HIDDEN;
943                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
944                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
945                         view_changed = TRUE;
946                 }
947                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
948                         ar_tools->alignment = RGN_ALIGN_NONE;
949                         view_changed = TRUE;
950                 }
951         }
952
953         if (tool_props_visible) {
954                 if (ar_tool_props && (ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
955                         ar_tool_props->flag &= ~RGN_FLAG_HIDDEN;
956                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
957                         view_changed = TRUE;
958                 }
959                 if (ar_tool_props && (ar_tool_props->alignment != (RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV))) {
960                         ar_tool_props->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
961                         view_changed = TRUE;
962                 }
963         }
964         else {
965                 if (ar_tool_props && !(ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
966                         ar_tool_props->flag |= RGN_FLAG_HIDDEN;
967                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
968                         WM_event_remove_handlers((bContext *)C, &ar_tool_props->handlers);
969                         view_changed = TRUE;
970                 }
971                 if (ar_tool_props && ar_tool_props->alignment != RGN_ALIGN_NONE) {
972                         ar_tool_props->alignment = RGN_ALIGN_NONE;
973                         view_changed = TRUE;
974                 }
975         }
976
977         if (preview_visible) {
978                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
979                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
980                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
981                         ar_preview->v2d.cur = ar_preview->v2d.tot;
982                         view_changed = TRUE;
983                 }
984                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
985                         ar_preview->alignment = RGN_ALIGN_NONE;
986                         view_changed = TRUE;
987                 }
988         }
989         else {
990                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
991                         ar_preview->flag |= RGN_FLAG_HIDDEN;
992                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
993                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
994                         view_changed = TRUE;
995                 }
996                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
997                         ar_preview->alignment = RGN_ALIGN_NONE;
998                         view_changed = TRUE;
999                 }
1000         }
1001
1002         if (channels_visible) {
1003                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
1004                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
1005                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
1006                         view_changed = TRUE;
1007                 }
1008                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
1009                         ar_channels->alignment = RGN_ALIGN_LEFT;
1010                         view_changed = TRUE;
1011                 }
1012         }
1013         else {
1014                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
1015                         ar_channels->flag |= RGN_FLAG_HIDDEN;
1016                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
1017                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
1018                         view_changed = TRUE;
1019                 }
1020                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
1021                         ar_channels->alignment = RGN_ALIGN_NONE;
1022                         view_changed = TRUE;
1023                 }
1024         }
1025
1026         if (view_changed) {
1027                 ED_area_initialize(wm, window, sa);
1028                 ED_area_tag_redraw(sa);
1029         }
1030
1031         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
1032 }
1033
1034 /********************* main region ********************/
1035
1036 /* sets up the fields of the View2D from zoom and offset */
1037 static void movieclip_main_area_set_view2d(const bContext *C, ARegion *ar)
1038 {
1039         SpaceClip *sc = CTX_wm_space_clip(C);
1040         float x1, y1, w, h, aspx, aspy;
1041         int width, height, winx, winy;
1042
1043         ED_space_clip_get_size(sc, &width, &height);
1044         ED_space_clip_get_aspect(sc, &aspx, &aspy);
1045
1046         w = width * aspx;
1047         h = height * aspy;
1048
1049         winx = BLI_rcti_size_x(&ar->winrct) + 1;
1050         winy = BLI_rcti_size_y(&ar->winrct) + 1;
1051
1052         ar->v2d.tot.xmin = 0;
1053         ar->v2d.tot.ymin = 0;
1054         ar->v2d.tot.xmax = w;
1055         ar->v2d.tot.ymax = h;
1056
1057         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
1058         ar->v2d.mask.xmax = winx;
1059         ar->v2d.mask.ymax = winy;
1060
1061         /* which part of the image space do we see? */
1062         x1 = ar->winrct.xmin + (winx - sc->zoom * w) / 2.0f;
1063         y1 = ar->winrct.ymin + (winy - sc->zoom * h) / 2.0f;
1064
1065         x1 -= sc->zoom * sc->xof;
1066         y1 -= sc->zoom * sc->yof;
1067
1068         /* relative display right */
1069         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
1070         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
1071
1072         /* relative display left */
1073         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
1074         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
1075
1076         /* normalize 0.0..1.0 */
1077         ar->v2d.cur.xmin /= w;
1078         ar->v2d.cur.xmax /= w;
1079         ar->v2d.cur.ymin /= h;
1080         ar->v2d.cur.ymax /= h;
1081 }
1082
1083 /* add handlers, stuff you only do once or on area/region changes */
1084 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
1085 {
1086         wmKeyMap *keymap;
1087
1088         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
1089
1090         /* mask polls mode */
1091         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
1092         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1093
1094         /* own keymap */
1095         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1096         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1097
1098         keymap = WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
1099         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1100 }
1101
1102 static void clip_main_area_draw(const bContext *C, ARegion *ar)
1103 {
1104         /* draw entirely, view changes should be handled here */
1105         SpaceClip *sc = CTX_wm_space_clip(C);
1106         MovieClip *clip = ED_space_clip_get_clip(sc);
1107
1108         /* if tracking is in progress, we should synchronize framenr from clipuser
1109          * so latest tracked frame would be shown */
1110         if (clip && clip->tracking_context)
1111                 BKE_tracking_context_sync_user(clip->tracking_context, &sc->user);
1112
1113         if (sc->flag & SC_LOCK_SELECTION) {
1114                 ImBuf *tmpibuf = NULL;
1115
1116                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1117                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
1118                 }
1119
1120                 if (ED_clip_view_selection(C, ar, 0)) {
1121                         sc->xof += sc->xlockof;
1122                         sc->yof += sc->ylockof;
1123                 }
1124
1125                 if (tmpibuf)
1126                         IMB_freeImBuf(tmpibuf);
1127         }
1128
1129         /* clear and setup matrix */
1130         UI_ThemeClearColor(TH_BACK);
1131         glClear(GL_COLOR_BUFFER_BIT);
1132
1133         /* data... */
1134         movieclip_main_area_set_view2d(C, ar);
1135
1136         clip_draw_main(C, sc, ar);
1137
1138         if (sc->mode == SC_MODE_MASKEDIT) {
1139
1140                 Mask *mask = CTX_data_edit_mask(C);
1141                 if (mask) {
1142                         ScrArea *sa = CTX_wm_area(C);
1143                         int width, height;
1144                         float aspx, aspy;
1145                         ED_mask_get_size(sa, &width, &height);
1146                         ED_space_clip_get_aspect(sc, &aspx, &aspy);
1147                         ED_mask_draw_region(mask, ar,
1148                                             sc->mask_info.draw_flag, sc->mask_info.draw_type,
1149                                             width, height,
1150                                             aspx, aspy,
1151                                             TRUE, TRUE,
1152                                             sc->stabmat, C);
1153                 }
1154
1155
1156         }
1157
1158         if (sc->flag & SC_SHOW_GPENCIL) {
1159                 /* Grease Pencil */
1160                 clip_draw_grease_pencil((bContext *)C, TRUE);
1161         }
1162
1163         /* reset view matrix */
1164         UI_view2d_view_restore(C);
1165
1166         if (sc->flag & SC_SHOW_GPENCIL) {
1167                 /* draw Grease Pencil - screen space only */
1168                 clip_draw_grease_pencil((bContext *)C, FALSE);
1169         }
1170 }
1171
1172 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
1173 {
1174         /* context changes */
1175         switch (wmn->category) {
1176                 case NC_GPENCIL:
1177                         if (wmn->action == NA_EDITED)
1178                                 ED_region_tag_redraw(ar);
1179                         break;
1180         }
1181 }
1182
1183 /****************** preview region ******************/
1184
1185 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
1186 {
1187         wmKeyMap *keymap;
1188
1189         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
1190
1191         /* own keymap */
1192         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1193         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1194
1195         keymap = WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
1196         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1197
1198         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1199         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1200 }
1201
1202 static void graph_area_draw(const bContext *C, ARegion *ar)
1203 {
1204         View2D *v2d = &ar->v2d;
1205         View2DScrollers *scrollers;
1206         SpaceClip *sc = CTX_wm_space_clip(C);
1207         Scene *scene = CTX_data_scene(C);
1208         short unitx, unity;
1209
1210         if (sc->flag & SC_LOCK_TIMECURSOR)
1211                 ED_clip_graph_center_current_frame(scene, ar);
1212
1213         /* clear and setup matrix */
1214         UI_ThemeClearColor(TH_BACK);
1215         glClear(GL_COLOR_BUFFER_BIT);
1216
1217         UI_view2d_view_ortho(v2d);
1218
1219         /* data... */
1220         clip_draw_graph(sc, ar, scene);
1221
1222         /* reset view matrix */
1223         UI_view2d_view_restore(C);
1224
1225         /* scrollers */
1226         unitx = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1227         unity = V2D_UNIT_VALUES;
1228         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1229         UI_view2d_scrollers_draw(C, v2d, scrollers);
1230         UI_view2d_scrollers_free(scrollers);
1231 }
1232
1233 static void dopesheet_area_draw(const bContext *C, ARegion *ar)
1234 {
1235         Scene *scene = CTX_data_scene(C);
1236         SpaceClip *sc = CTX_wm_space_clip(C);
1237         MovieClip *clip = ED_space_clip_get_clip(sc);
1238         View2D *v2d = &ar->v2d;
1239         View2DGrid *grid;
1240         View2DScrollers *scrollers;
1241         short unit = 0;
1242
1243         if (clip)
1244                 BKE_tracking_dopesheet_update(&clip->tracking);
1245
1246         /* clear and setup matrix */
1247         UI_ThemeClearColor(TH_BACK);
1248         glClear(GL_COLOR_BUFFER_BIT);
1249
1250         UI_view2d_view_ortho(v2d);
1251
1252         /* time grid */
1253         unit = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1254         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP,
1255                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1256         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1257         UI_view2d_grid_free(grid);
1258
1259         /* data... */
1260         clip_draw_dopesheet_main(sc, ar, scene);
1261
1262         /* reset view matrix */
1263         UI_view2d_view_restore(C);
1264
1265         /* scrollers */
1266         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1267         UI_view2d_scrollers_draw(C, v2d, scrollers);
1268         UI_view2d_scrollers_free(scrollers);
1269 }
1270
1271 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
1272 {
1273         SpaceClip *sc = CTX_wm_space_clip(C);
1274
1275         if (sc->view == SC_VIEW_GRAPH)
1276                 graph_area_draw(C, ar);
1277         else if (sc->view == SC_VIEW_DOPESHEET)
1278                 dopesheet_area_draw(C, ar);
1279 }
1280
1281 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1282 {
1283 }
1284
1285 /****************** channels region ******************/
1286
1287 static void clip_channels_area_init(wmWindowManager *wm, ARegion *ar)
1288 {
1289         wmKeyMap *keymap;
1290
1291         /* ensure the 2d view sync works - main region has bottom scroller */
1292         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
1293
1294         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1295
1296         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1297         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1298 }
1299
1300 static void clip_channels_area_draw(const bContext *C, ARegion *ar)
1301 {
1302         SpaceClip *sc = CTX_wm_space_clip(C);
1303         MovieClip *clip = ED_space_clip_get_clip(sc);
1304         View2D *v2d = &ar->v2d;
1305
1306         if (clip)
1307                 BKE_tracking_dopesheet_update(&clip->tracking);
1308
1309         /* clear and setup matrix */
1310         UI_ThemeClearColor(TH_BACK);
1311         glClear(GL_COLOR_BUFFER_BIT);
1312
1313         UI_view2d_view_ortho(v2d);
1314
1315         /* data... */
1316         clip_draw_dopesheet_channels(C, ar);
1317
1318         /* reset view matrix */
1319         UI_view2d_view_restore(C);
1320 }
1321
1322 static void clip_channels_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1323 {
1324 }
1325
1326 /****************** header region ******************/
1327
1328 /* add handlers, stuff you only do once or on area/region changes */
1329 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1330 {
1331         ED_region_header_init(ar);
1332 }
1333
1334 static void clip_header_area_draw(const bContext *C, ARegion *ar)
1335 {
1336         ED_region_header(C, ar);
1337 }
1338
1339 static void clip_header_area_listener(ARegion *ar, wmNotifier *wmn)
1340 {
1341         /* context changes */
1342         switch (wmn->category) {
1343                 case NC_SCENE:
1344                         switch (wmn->data) {
1345                                 /* for proportional editmode only */
1346                                 case ND_TOOLSETTINGS:
1347                                         /* TODO - should do this when in mask mode only but no datas available */
1348                                         // if (sc->mode == SC_MODE_MASKEDIT)
1349                                 {
1350                                         ED_region_tag_redraw(ar);
1351                                 }
1352                                 break;
1353                         }
1354                         break;
1355         }
1356 }
1357
1358
1359 /****************** tools region ******************/
1360
1361 /* add handlers, stuff you only do once or on area/region changes */
1362 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
1363 {
1364         ED_region_panels_init(wm, ar);
1365 }
1366
1367 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
1368 {
1369         ED_region_panels(C, ar, 1, NULL, -1);
1370 }
1371
1372 /****************** tool properties region ******************/
1373
1374 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
1375 {
1376         /* context changes */
1377         switch (wmn->category) {
1378                 case NC_WM:
1379                         if (wmn->data == ND_HISTORY)
1380                                 ED_region_tag_redraw(ar);
1381                         break;
1382                 case NC_SCENE:
1383                         if (wmn->data == ND_MODE)
1384                                 ED_region_tag_redraw(ar);
1385                         break;
1386                 case NC_SPACE:
1387                         if (wmn->data == ND_SPACE_CLIP)
1388                                 ED_region_tag_redraw(ar);
1389                         break;
1390                 case NC_GPENCIL:
1391                         if (wmn->action == NA_EDITED)
1392                                 ED_region_tag_redraw(ar);
1393                         break;
1394         }
1395 }
1396
1397 /****************** properties region ******************/
1398
1399 /* add handlers, stuff you only do once or on area/region changes */
1400 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
1401 {
1402         wmKeyMap *keymap;
1403
1404         ED_region_panels_init(wm, ar);
1405
1406         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1407         WM_event_add_keymap_handler(&ar->handlers, keymap);
1408 }
1409
1410 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
1411 {
1412         SpaceClip *sc = CTX_wm_space_clip(C);
1413
1414         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1415
1416         ED_region_panels(C, ar, 1, NULL, -1);
1417 }
1418
1419 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
1420 {
1421         /* context changes */
1422         switch (wmn->category) {
1423                 case NC_GPENCIL:
1424                         if (wmn->data == ND_DATA)
1425                                 ED_region_tag_redraw(ar);
1426                         break;
1427                 case NC_BRUSH:
1428                         if (wmn->action == NA_EDITED)
1429                                 ED_region_tag_redraw(ar);
1430                         break;
1431         }
1432 }
1433
1434 /********************* registration ********************/
1435
1436 /* only called once, from space/spacetypes.c */
1437 void ED_spacetype_clip(void)
1438 {
1439         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1440         ARegionType *art;
1441
1442         st->spaceid = SPACE_CLIP;
1443         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1444
1445         st->new = clip_new;
1446         st->free = clip_free;
1447         st->init = clip_init;
1448         st->duplicate = clip_duplicate;
1449         st->operatortypes = clip_operatortypes;
1450         st->keymap = clip_keymap;
1451         st->listener = clip_listener;
1452         st->context = clip_context;
1453         st->dropboxes = clip_dropboxes;
1454         st->refresh = clip_refresh;
1455
1456         /* regions: main window */
1457         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1458         art->regionid = RGN_TYPE_WINDOW;
1459         art->init = clip_main_area_init;
1460         art->draw = clip_main_area_draw;
1461         art->listener = clip_main_area_listener;
1462         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1463
1464         BLI_addhead(&st->regiontypes, art);
1465
1466         /* preview */
1467         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1468         art->regionid = RGN_TYPE_PREVIEW;
1469         art->prefsizey = 240;
1470         art->init = clip_preview_area_init;
1471         art->draw = clip_preview_area_draw;
1472         art->listener = clip_preview_area_listener;
1473         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1474
1475         BLI_addhead(&st->regiontypes, art);
1476
1477         /* regions: properties */
1478         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1479         art->regionid = RGN_TYPE_UI;
1480         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1481         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1482         art->init = clip_properties_area_init;
1483         art->draw = clip_properties_area_draw;
1484         art->listener = clip_properties_area_listener;
1485         BLI_addhead(&st->regiontypes, art);
1486         ED_clip_buttons_register(art);
1487
1488         /* regions: tools */
1489         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1490         art->regionid = RGN_TYPE_TOOLS;
1491         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1492         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1493         art->listener = clip_props_area_listener;
1494         art->init = clip_tools_area_init;
1495         art->draw = clip_tools_area_draw;
1496
1497         BLI_addhead(&st->regiontypes, art);
1498
1499         /* tool properties */
1500         art = MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
1501         art->regionid = RGN_TYPE_TOOL_PROPS;
1502         art->prefsizex = 0;
1503         art->prefsizey = 120;
1504         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1505         art->listener = clip_props_area_listener;
1506         art->init = clip_tools_area_init;
1507         art->draw = clip_tools_area_draw;
1508         ED_clip_tool_props_register(art);
1509
1510         BLI_addhead(&st->regiontypes, art);
1511
1512         /* regions: header */
1513         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1514         art->regionid = RGN_TYPE_HEADER;
1515         art->prefsizey = HEADERY;
1516         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1517
1518         art->init = clip_header_area_init;
1519         art->draw = clip_header_area_draw;
1520         art->listener = clip_header_area_listener;
1521
1522         BLI_addhead(&st->regiontypes, art);
1523
1524         BKE_spacetype_register(st);
1525
1526         /* channels */
1527         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1528         art->regionid = RGN_TYPE_CHANNELS;
1529         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1530         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1531         art->listener = clip_channels_area_listener;
1532         art->init = clip_channels_area_init;
1533         art->draw = clip_channels_area_draw;
1534
1535         BLI_addhead(&st->regiontypes, art);
1536 }