mask now draws in the image view, misc minor edits.
[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;
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                                 case ND_GPENCIL:
411                                         clip_scopes_check_gpencil_change(sa);
412                                         ED_area_tag_redraw(sa);
413                                         break;
414                         }
415                         break;
416                 case NC_SPACE:
417                         if (wmn->data == ND_SPACE_CLIP) {
418                                 clip_scopes_tag_refresh(sa);
419                                 clip_stabilization_tag_refresh(sa);
420                                 ED_area_tag_redraw(sa);
421                         }
422                         break;
423         }
424 }
425
426 static void clip_operatortypes(void)
427 {
428         /* ** clip_ops.c ** */
429         WM_operatortype_append(CLIP_OT_open);
430         WM_operatortype_append(CLIP_OT_reload);
431         WM_operatortype_append(CLIP_OT_view_pan);
432         WM_operatortype_append(CLIP_OT_view_zoom);
433         WM_operatortype_append(CLIP_OT_view_zoom_in);
434         WM_operatortype_append(CLIP_OT_view_zoom_out);
435         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
436         WM_operatortype_append(CLIP_OT_view_all);
437         WM_operatortype_append(CLIP_OT_view_selected);
438         WM_operatortype_append(CLIP_OT_change_frame);
439         WM_operatortype_append(CLIP_OT_rebuild_proxy);
440         WM_operatortype_append(CLIP_OT_mode_set);
441
442         /* ** clip_toolbar.c ** */
443         WM_operatortype_append(CLIP_OT_tools);
444         WM_operatortype_append(CLIP_OT_properties);
445
446         /* ** tracking_ops.c ** */
447
448         /* navigation */
449         WM_operatortype_append(CLIP_OT_frame_jump);
450
451         /* foorage */
452         WM_operatortype_append(CLIP_OT_set_center_principal);
453
454         /* selection */
455         WM_operatortype_append(CLIP_OT_select);
456         WM_operatortype_append(CLIP_OT_select_all);
457         WM_operatortype_append(CLIP_OT_select_border);
458         WM_operatortype_append(CLIP_OT_select_lasso);
459         WM_operatortype_append(CLIP_OT_select_circle);
460         WM_operatortype_append(CLIP_OT_select_grouped);
461
462         /* markers */
463         WM_operatortype_append(CLIP_OT_add_marker);
464         WM_operatortype_append(CLIP_OT_slide_marker);
465         WM_operatortype_append(CLIP_OT_delete_track);
466         WM_operatortype_append(CLIP_OT_delete_marker);
467
468         /* track */
469         WM_operatortype_append(CLIP_OT_track_markers);
470
471         /* solving */
472         WM_operatortype_append(CLIP_OT_solve_camera);
473         WM_operatortype_append(CLIP_OT_clear_solution);
474
475         WM_operatortype_append(CLIP_OT_disable_markers);
476         WM_operatortype_append(CLIP_OT_hide_tracks);
477         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
478         WM_operatortype_append(CLIP_OT_lock_tracks);
479
480         /* orientation */
481         WM_operatortype_append(CLIP_OT_set_origin);
482         WM_operatortype_append(CLIP_OT_set_plane);
483         WM_operatortype_append(CLIP_OT_set_axis);
484         WM_operatortype_append(CLIP_OT_set_scale);
485         WM_operatortype_append(CLIP_OT_set_solution_scale);
486
487         /* detect */
488         WM_operatortype_append(CLIP_OT_detect_features);
489
490         /* stabilization */
491         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
492         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
493         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
494         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
495
496         /* clean-up */
497         WM_operatortype_append(CLIP_OT_clear_track_path);
498         WM_operatortype_append(CLIP_OT_join_tracks);
499         WM_operatortype_append(CLIP_OT_track_copy_color);
500
501         WM_operatortype_append(CLIP_OT_clean_tracks);
502
503         /* object tracking */
504         WM_operatortype_append(CLIP_OT_tracking_object_new);
505         WM_operatortype_append(CLIP_OT_tracking_object_remove);
506
507         /* clipboard */
508         WM_operatortype_append(CLIP_OT_copy_tracks);
509         WM_operatortype_append(CLIP_OT_paste_tracks);
510
511         /* ** clip_graph_ops.c  ** */
512
513         /* graph editing */
514
515         /* selection */
516         WM_operatortype_append(CLIP_OT_graph_select);
517         WM_operatortype_append(CLIP_OT_graph_select_border);
518         WM_operatortype_append(CLIP_OT_graph_select_all_markers);
519
520         WM_operatortype_append(CLIP_OT_graph_delete_curve);
521         WM_operatortype_append(CLIP_OT_graph_delete_knot);
522         WM_operatortype_append(CLIP_OT_graph_view_all);
523         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
524
525         WM_operatortype_append(CLIP_OT_graph_disable_markers);
526
527         /* ** clip_dopesheet_ops.c  ** */
528
529         WM_operatortype_append(CLIP_OT_dopesheet_select_channel);
530 }
531
532 static void clip_keymap(struct wmKeyConfig *keyconf)
533 {
534         wmKeyMap *keymap;
535         wmKeyMapItem *kmi;
536
537         /* ******** Global hotkeys avalaible for all regions ******** */
538
539         keymap = WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
540
541         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
542
543         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
544         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
545
546         /* 2d tracking */
547         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
548         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
549         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
550         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
551         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
552         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
553         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
554         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
555         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
556         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
557         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
558         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
559
560         /* mode */
561         WM_keymap_add_menu(keymap, "CLIP_MT_select_mode", TABKEY, KM_PRESS, 0, 0);
562
563         WM_keymap_add_item(keymap, "CLIP_OT_solve_camera", SKEY, KM_PRESS, KM_SHIFT, 0);
564
565         /* ******** Hotkeys avalaible for main region only ******** */
566
567         keymap = WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
568 //      keymap->poll = ED_space_clip_tracking_poll;
569         /* ** View/navigation ** */
570
571         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
572         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
573         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
574
575         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
576         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
577         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
578         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
579         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
580         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
581
582         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
583         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
584         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
585         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
586         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
587         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
588         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
589
590         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
591
592         kmi = WM_keymap_add_item(keymap, "CLIP_OT_view_all", FKEY, KM_PRESS, 0, 0);
593         RNA_boolean_set(kmi->ptr, "fit_view", TRUE);
594
595         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
596
597         /* jump to special frame */
598         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
599         RNA_enum_set(kmi->ptr, "position", 0);
600
601         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
602         RNA_enum_set(kmi->ptr, "position", 1);
603
604         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
605         RNA_enum_set(kmi->ptr, "position", 2);
606
607         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
608         RNA_enum_set(kmi->ptr, "position", 3);
609
610         /* "timeline" */
611         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
612
613         /* selection */
614         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
615         RNA_boolean_set(kmi->ptr, "extend", FALSE);
616         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
617         RNA_boolean_set(kmi->ptr, "extend", TRUE);
618         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
619         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
620         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
621         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
622         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
623         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
624         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
625
626         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
627         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
628         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
629         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
630
631         /* marker */
632         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
633
634         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
635         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
636
637         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
638
639         kmi = WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
640         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
641
642         /* tracks */
643         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
644         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
645
646         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
647         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
648
649         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
650         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
651
652         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
653         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
654
655         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
656         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
657
658         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
659
660         /* clean-up */
661         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
662
663         /* menus */
664         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
665
666         /* display */
667         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
668         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
669
670         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", DKEY, KM_PRESS, KM_ALT, 0);
671         RNA_string_set(kmi->ptr, "data_path", "space_data.show_disabled");
672
673         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_ALT, 0);
674         RNA_string_set(kmi->ptr, "data_path", "space_data.show_marker_search");
675
676         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
677         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
678
679         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
680
681         /* clean-up */
682         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
683         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
684         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
685         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
686         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
687         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
688         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
689         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
690         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
691
692         /* ******** Hotkeys avalaible for preview region only ******** */
693
694         keymap = WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
695
696         /* "timeline" */
697         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
698
699         /* selection */
700         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
701         RNA_boolean_set(kmi->ptr, "extend", FALSE);
702         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
703         RNA_boolean_set(kmi->ptr, "extend", TRUE);
704
705         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", AKEY, KM_PRESS, 0, 0);
706         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
707         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", IKEY, KM_PRESS, KM_CTRL, 0);
708         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
709
710         WM_keymap_add_item(keymap, "CLIP_OT_graph_select_border", BKEY, KM_PRESS, 0, 0);
711
712         /* delete */
713         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
714         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
715
716         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
717         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
718
719         /* view */
720         WM_keymap_add_item(keymap, "CLIP_OT_graph_view_all", HOMEKEY, KM_PRESS, 0, 0);
721         WM_keymap_add_item(keymap, "CLIP_OT_graph_center_current_frame", PADPERIOD, KM_PRESS, 0, 0);
722
723         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
724         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_time_cursor");
725
726         /* clean-up */
727         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
728         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
729         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
730         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
731         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
732         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
733         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
734         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
735         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
736
737         /* tracks */
738         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
739         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
740
741         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
742
743         /* ******** Hotkeys avalaible for channels region only ******** */
744
745         keymap = WM_keymap_find(keyconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
746
747         kmi = WM_keymap_add_item(keymap, "CLIP_OT_dopesheet_select_channel", ACTIONMOUSE, KM_PRESS, 0, 0);
748         RNA_boolean_set(kmi->ptr, "extend", TRUE);  /* toggle */
749 }
750
751 const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL};
752
753 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
754 {
755         SpaceClip *sc = CTX_wm_space_clip(C);
756
757         if (CTX_data_dir(member)) {
758                 CTX_data_dir_set(result, clip_context_dir);
759
760                 return TRUE;
761         }
762         else if (CTX_data_equals(member, "edit_movieclip")) {
763                 if (sc->clip)
764                         CTX_data_id_pointer_set(result, &sc->clip->id);
765                 return TRUE;
766         }
767         else if (CTX_data_equals(member, "edit_mask")) {
768                 if (sc->mask_info.mask)
769                         CTX_data_id_pointer_set(result, &sc->mask_info.mask->id);
770                 return TRUE;
771         }
772
773         return FALSE;
774 }
775
776 /* dropboxes */
777 static int clip_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
778 {
779         if (drag->type == WM_DRAG_PATH)
780                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
781                         return TRUE;
782
783         return FALSE;
784 }
785
786 static void clip_drop_copy(wmDrag *drag, wmDropBox *drop)
787 {
788         PointerRNA itemptr;
789         char dir[FILE_MAX], file[FILE_MAX];
790
791         BLI_split_dirfile(drag->path, dir, file, sizeof(dir), sizeof(file));
792
793         RNA_string_set(drop->ptr, "directory", dir);
794
795         RNA_collection_clear(drop->ptr, "files");
796         RNA_collection_add(drop->ptr, "files", &itemptr);
797         RNA_string_set(&itemptr, "name", file);
798 }
799
800 /* area+region dropbox definition */
801 static void clip_dropboxes(void)
802 {
803         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
804
805         WM_dropbox_add(lb, "CLIP_OT_open", clip_drop_poll, clip_drop_copy);
806 }
807
808 static void clip_refresh(const bContext *C, ScrArea *sa)
809 {
810         wmWindowManager *wm = CTX_wm_manager(C);
811         wmWindow *window = CTX_wm_window(C);
812         Scene *scene = CTX_data_scene(C);
813         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
814         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
815         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
816         ARegion *ar_tool_props = BKE_area_find_region_type(sa, RGN_TYPE_TOOL_PROPS);
817         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
818         ARegion *ar_properties = ED_clip_has_properties_region(sa);
819         ARegion *ar_channels = ED_clip_has_channels_region(sa);
820         int main_visible = FALSE, preview_visible = FALSE, tools_visible = FALSE;
821         int tool_props_visible = FALSE, properties_visible = FALSE, channels_visible = FALSE;
822         int view_changed = FALSE;
823
824         switch (sc->view) {
825                 case SC_VIEW_CLIP:
826                         main_visible = TRUE;
827                         preview_visible = FALSE;
828                         tools_visible = TRUE;
829                         tool_props_visible = TRUE;
830                         properties_visible = TRUE;
831                         channels_visible = FALSE;
832                         break;
833                 case SC_VIEW_GRAPH:
834                         main_visible = FALSE;
835                         preview_visible = TRUE;
836                         tools_visible = FALSE;
837                         tool_props_visible = FALSE;
838                         properties_visible = FALSE;
839                         channels_visible = FALSE;
840
841                         reinit_preview_region(C, ar_preview);
842                         break;
843                 case SC_VIEW_DOPESHEET:
844                         main_visible = FALSE;
845                         preview_visible = TRUE;
846                         tools_visible = FALSE;
847                         tool_props_visible = FALSE;
848                         properties_visible = FALSE;
849                         channels_visible = TRUE;
850
851                         reinit_preview_region(C, ar_preview);
852                         break;
853         }
854
855         if (main_visible) {
856                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
857                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
858                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
859                         view_changed = TRUE;
860                 }
861
862                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
863                         ar_main->alignment = RGN_ALIGN_NONE;
864                         view_changed = TRUE;
865                 }
866         }
867         else {
868                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
869                         ar_main->flag |= RGN_FLAG_HIDDEN;
870                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
871                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
872                         view_changed = TRUE;
873                 }
874                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
875                         ar_main->alignment = RGN_ALIGN_NONE;
876                         view_changed = TRUE;
877                 }
878         }
879
880         if (properties_visible) {
881                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
882                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
883                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
884                         view_changed = TRUE;
885                 }
886                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
887                         ar_properties->alignment = RGN_ALIGN_RIGHT;
888                         view_changed = TRUE;
889                 }
890         }
891         else {
892                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
893                         ar_properties->flag |= RGN_FLAG_HIDDEN;
894                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
895                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
896                         view_changed = TRUE;
897                 }
898                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
899                         ar_properties->alignment = RGN_ALIGN_NONE;
900                         view_changed = TRUE;
901                 }
902         }
903
904         if (tools_visible) {
905                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
906                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
907                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
908                         view_changed = TRUE;
909                 }
910                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
911                         ar_tools->alignment = RGN_ALIGN_LEFT;
912                         view_changed = TRUE;
913                 }
914         }
915         else {
916                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
917                         ar_tools->flag |= RGN_FLAG_HIDDEN;
918                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
919                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
920                         view_changed = TRUE;
921                 }
922                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
923                         ar_tools->alignment = RGN_ALIGN_NONE;
924                         view_changed = TRUE;
925                 }
926         }
927
928         if (tool_props_visible) {
929                 if (ar_tool_props && (ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
930                         ar_tool_props->flag &= ~RGN_FLAG_HIDDEN;
931                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
932                         view_changed = TRUE;
933                 }
934                 if (ar_tool_props && (ar_tool_props->alignment != (RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV))) {
935                         ar_tool_props->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
936                         view_changed = TRUE;
937                 }
938         }
939         else {
940                 if (ar_tool_props && !(ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
941                         ar_tool_props->flag |= RGN_FLAG_HIDDEN;
942                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
943                         WM_event_remove_handlers((bContext *)C, &ar_tool_props->handlers);
944                         view_changed = TRUE;
945                 }
946                 if (ar_tool_props && ar_tool_props->alignment != RGN_ALIGN_NONE) {
947                         ar_tool_props->alignment = RGN_ALIGN_NONE;
948                         view_changed = TRUE;
949                 }
950         }
951
952         if (preview_visible) {
953                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
954                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
955                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
956                         ar_preview->v2d.cur = ar_preview->v2d.tot;
957                         view_changed = TRUE;
958                 }
959                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
960                         ar_preview->alignment = RGN_ALIGN_NONE;
961                         view_changed = TRUE;
962                 }
963         }
964         else {
965                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
966                         ar_preview->flag |= RGN_FLAG_HIDDEN;
967                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
968                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
969                         view_changed = TRUE;
970                 }
971                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
972                         ar_preview->alignment = RGN_ALIGN_NONE;
973                         view_changed = TRUE;
974                 }
975         }
976
977         if (channels_visible) {
978                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
979                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
980                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
981                         view_changed = TRUE;
982                 }
983                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
984                         ar_channels->alignment = RGN_ALIGN_LEFT;
985                         view_changed = TRUE;
986                 }
987         }
988         else {
989                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
990                         ar_channels->flag |= RGN_FLAG_HIDDEN;
991                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
992                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
993                         view_changed = TRUE;
994                 }
995                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
996                         ar_channels->alignment = RGN_ALIGN_NONE;
997                         view_changed = TRUE;
998                 }
999         }
1000
1001         if (view_changed) {
1002                 ED_area_initialize(wm, window, sa);
1003                 ED_area_tag_redraw(sa);
1004         }
1005
1006         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
1007 }
1008
1009 /********************* main region ********************/
1010
1011 /* sets up the fields of the View2D from zoom and offset */
1012 static void movieclip_main_area_set_view2d(const bContext *C, ARegion *ar)
1013 {
1014         SpaceClip *sc = CTX_wm_space_clip(C);
1015         MovieClip *clip = ED_space_clip_get_clip(sc);
1016         float x1, y1, w, h;
1017         int width, height, winx, winy;
1018
1019         ED_space_clip_get_size(C, &width, &height);
1020
1021         w = width;
1022         h = height;
1023
1024         if (clip)
1025                 h *= clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1026
1027         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
1028         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
1029
1030         ar->v2d.tot.xmin = 0;
1031         ar->v2d.tot.ymin = 0;
1032         ar->v2d.tot.xmax = w;
1033         ar->v2d.tot.ymax = h;
1034
1035         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
1036         ar->v2d.mask.xmax = winx;
1037         ar->v2d.mask.ymax = winy;
1038
1039         /* which part of the image space do we see? */
1040         x1 = ar->winrct.xmin + (winx - sc->zoom * w) / 2.0f;
1041         y1 = ar->winrct.ymin + (winy - sc->zoom * h) / 2.0f;
1042
1043         x1 -= sc->zoom * sc->xof;
1044         y1 -= sc->zoom * sc->yof;
1045
1046         /* relative display right */
1047         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
1048         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
1049
1050         /* relative display left */
1051         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
1052         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
1053
1054         /* normalize 0.0..1.0 */
1055         ar->v2d.cur.xmin /= w;
1056         ar->v2d.cur.xmax /= w;
1057         ar->v2d.cur.ymin /= h;
1058         ar->v2d.cur.ymax /= h;
1059 }
1060
1061 /* add handlers, stuff you only do once or on area/region changes */
1062 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
1063 {
1064         wmKeyMap *keymap;
1065
1066         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
1067
1068         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
1069         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1070
1071         /* own keymap */
1072         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1073         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1074
1075         keymap = WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
1076         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1077 }
1078
1079 static void clip_main_area_draw(const bContext *C, ARegion *ar)
1080 {
1081         /* draw entirely, view changes should be handled here */
1082         SpaceClip *sc = CTX_wm_space_clip(C);
1083         MovieClip *clip = ED_space_clip_get_clip(sc);
1084
1085         /* if tracking is in progress, we should synchronize framenr from clipuser
1086          * so latest tracked frame would be shown */
1087         if (clip && clip->tracking_context)
1088                 BKE_tracking_context_sync_user(clip->tracking_context, &sc->user);
1089
1090         if (sc->flag & SC_LOCK_SELECTION) {
1091                 ImBuf *tmpibuf = NULL;
1092
1093                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1094                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
1095                 }
1096
1097                 if (ED_clip_view_selection(C, ar, 0)) {
1098                         sc->xof += sc->xlockof;
1099                         sc->yof += sc->ylockof;
1100                 }
1101
1102                 if (tmpibuf)
1103                         IMB_freeImBuf(tmpibuf);
1104         }
1105
1106         /* clear and setup matrix */
1107         UI_ThemeClearColor(TH_BACK);
1108         glClear(GL_COLOR_BUFFER_BIT);
1109
1110         /* data... */
1111         movieclip_main_area_set_view2d(C, ar);
1112
1113         clip_draw_main(C, ar);
1114
1115         if (sc->mode == SC_MODE_MASKEDIT) {
1116
1117                 Mask *mask = CTX_data_edit_mask(C);
1118                 if (mask) {
1119                         int width, height;
1120                         ED_mask_size(C, &width, &height);
1121                         ED_mask_draw_region(mask, ar,
1122                                             sc->mask_info.draw_flag, sc->mask_info.draw_type,
1123                                             width, height,
1124                                             TRUE, TRUE,
1125                                             sc->stabmat, C);
1126                 }
1127
1128
1129         }
1130
1131         /* Grease Pencil */
1132         clip_draw_grease_pencil((bContext *)C, 1);
1133
1134         /* reset view matrix */
1135         UI_view2d_view_restore(C);
1136
1137         /* draw Grease Pencil - screen space only */
1138         clip_draw_grease_pencil((bContext *)C, 0);
1139 }
1140
1141 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
1142 {
1143         /* context changes */
1144         switch (wmn->category) {
1145                 case NC_SCREEN:
1146                         if (wmn->data == ND_GPENCIL)
1147                                 ED_region_tag_redraw(ar);
1148                         break;
1149         }
1150 }
1151
1152 /****************** preview region ******************/
1153
1154 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
1155 {
1156         wmKeyMap *keymap;
1157
1158         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
1159
1160         /* own keymap */
1161         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1162         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1163
1164         keymap = WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
1165         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1166 }
1167
1168 static void graph_area_draw(const bContext *C, ARegion *ar)
1169 {
1170         View2D *v2d = &ar->v2d;
1171         View2DScrollers *scrollers;
1172         SpaceClip *sc = CTX_wm_space_clip(C);
1173         Scene *scene = CTX_data_scene(C);
1174         short unitx, unity;
1175
1176         if (sc->flag & SC_LOCK_TIMECURSOR)
1177                 ED_clip_graph_center_current_frame(scene, ar);
1178
1179         /* clear and setup matrix */
1180         UI_ThemeClearColor(TH_BACK);
1181         glClear(GL_COLOR_BUFFER_BIT);
1182
1183         UI_view2d_view_ortho(v2d);
1184
1185         /* data... */
1186         clip_draw_graph(sc, ar, scene);
1187
1188         /* reset view matrix */
1189         UI_view2d_view_restore(C);
1190
1191         /* scrollers */
1192         unitx = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1193         unity = V2D_UNIT_VALUES;
1194         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1195         UI_view2d_scrollers_draw(C, v2d, scrollers);
1196         UI_view2d_scrollers_free(scrollers);
1197 }
1198
1199 static void dopesheet_area_draw(const bContext *C, ARegion *ar)
1200 {
1201         Scene *scene = CTX_data_scene(C);
1202         SpaceClip *sc = CTX_wm_space_clip(C);
1203         MovieClip *clip = ED_space_clip_get_clip(sc);
1204         View2D *v2d = &ar->v2d;
1205         View2DGrid *grid;
1206         View2DScrollers *scrollers;
1207         short unit = 0;
1208
1209         if (clip)
1210                 BKE_tracking_dopesheet_update(&clip->tracking);
1211
1212         /* clear and setup matrix */
1213         UI_ThemeClearColor(TH_BACK);
1214         glClear(GL_COLOR_BUFFER_BIT);
1215
1216         UI_view2d_view_ortho(v2d);
1217
1218         /* time grid */
1219         unit = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1220         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP,
1221                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1222         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1223         UI_view2d_grid_free(grid);
1224
1225         /* data... */
1226         clip_draw_dopesheet_main(sc, ar, scene);
1227
1228         /* reset view matrix */
1229         UI_view2d_view_restore(C);
1230
1231         /* scrollers */
1232         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1233         UI_view2d_scrollers_draw(C, v2d, scrollers);
1234         UI_view2d_scrollers_free(scrollers);
1235 }
1236
1237 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
1238 {
1239         SpaceClip *sc = CTX_wm_space_clip(C);
1240
1241         if (sc->view == SC_VIEW_GRAPH)
1242                 graph_area_draw(C, ar);
1243         else if (sc->view == SC_VIEW_DOPESHEET)
1244                 dopesheet_area_draw(C, ar);
1245 }
1246
1247 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1248 {
1249 }
1250
1251 /****************** channels region ******************/
1252
1253 static void clip_channels_area_init(wmWindowManager *wm, ARegion *ar)
1254 {
1255         wmKeyMap *keymap;
1256
1257         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1258
1259         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1260         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1261 }
1262
1263 static void clip_channels_area_draw(const bContext *C, ARegion *ar)
1264 {
1265         SpaceClip *sc = CTX_wm_space_clip(C);
1266         MovieClip *clip = ED_space_clip_get_clip(sc);
1267         View2D *v2d = &ar->v2d;
1268         View2DScrollers *scrollers;
1269
1270         if (clip)
1271                 BKE_tracking_dopesheet_update(&clip->tracking);
1272
1273         /* clear and setup matrix */
1274         UI_ThemeClearColor(TH_BACK);
1275         glClear(GL_COLOR_BUFFER_BIT);
1276
1277         UI_view2d_view_ortho(v2d);
1278
1279         /* data... */
1280         clip_draw_dopesheet_channels(C, ar);
1281
1282         /* reset view matrix */
1283         UI_view2d_view_restore(C);
1284
1285         /* scrollers */
1286         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1287         UI_view2d_scrollers_draw(C, v2d, scrollers);
1288         UI_view2d_scrollers_free(scrollers);
1289 }
1290
1291 static void clip_channels_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1292 {
1293 }
1294
1295 /****************** header region ******************/
1296
1297 /* add handlers, stuff you only do once or on area/region changes */
1298 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1299 {
1300         ED_region_header_init(ar);
1301 }
1302
1303 static void clip_header_area_draw(const bContext *C, ARegion *ar)
1304 {
1305         ED_region_header(C, ar);
1306 }
1307
1308 static void clip_header_area_listener(ARegion *ar, wmNotifier *wmn)
1309 {
1310         /* context changes */
1311         switch (wmn->category) {
1312                 case NC_SCENE:
1313                         switch (wmn->data) {
1314                                 /* for proportional editmode only */
1315                                 case ND_TOOLSETTINGS:
1316                                         /* TODO - should do this when in mask mode only but no datas available */
1317                                         // if (sc->mode == SC_MODE_MASKEDIT)
1318                                 {
1319                                         ED_region_tag_redraw(ar);
1320                                 }
1321                                 break;
1322                         }
1323                         break;
1324         }
1325 }
1326
1327
1328 /****************** tools region ******************/
1329
1330 /* add handlers, stuff you only do once or on area/region changes */
1331 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
1332 {
1333         ED_region_panels_init(wm, ar);
1334 }
1335
1336 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
1337 {
1338         ED_region_panels(C, ar, 1, NULL, -1);
1339 }
1340
1341 /****************** tool properties region ******************/
1342
1343 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
1344 {
1345         /* context changes */
1346         switch (wmn->category) {
1347                 case NC_WM:
1348                         if (wmn->data == ND_HISTORY)
1349                                 ED_region_tag_redraw(ar);
1350                         break;
1351                 case NC_SCENE:
1352                         if (wmn->data == ND_MODE)
1353                                 ED_region_tag_redraw(ar);
1354                         break;
1355                 case NC_SPACE:
1356                         if (wmn->data == ND_SPACE_CLIP)
1357                                 ED_region_tag_redraw(ar);
1358                         break;
1359                 case NC_SCREEN:
1360                         if (wmn->data == ND_GPENCIL)
1361                                 ED_region_tag_redraw(ar);
1362                         break;
1363         }
1364 }
1365
1366 /****************** properties region ******************/
1367
1368 /* add handlers, stuff you only do once or on area/region changes */
1369 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
1370 {
1371         wmKeyMap *keymap;
1372
1373         ED_region_panels_init(wm, ar);
1374
1375         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1376         WM_event_add_keymap_handler(&ar->handlers, keymap);
1377 }
1378
1379 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
1380 {
1381         SpaceClip *sc = CTX_wm_space_clip(C);
1382
1383         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1384
1385         ED_region_panels(C, ar, 1, NULL, -1);
1386 }
1387
1388 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
1389 {
1390         /* context changes */
1391         switch (wmn->category) {
1392                 case NC_SCREEN:
1393                         if (wmn->data == ND_GPENCIL)
1394                                 ED_region_tag_redraw(ar);
1395                         break;
1396                 case NC_BRUSH:
1397                         if (wmn->action == NA_EDITED)
1398                                 ED_region_tag_redraw(ar);
1399                         break;
1400         }
1401 }
1402
1403 /********************* registration ********************/
1404
1405 /* only called once, from space/spacetypes.c */
1406 void ED_spacetype_clip(void)
1407 {
1408         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1409         ARegionType *art;
1410
1411         st->spaceid = SPACE_CLIP;
1412         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1413
1414         st->new = clip_new;
1415         st->free = clip_free;
1416         st->init = clip_init;
1417         st->duplicate = clip_duplicate;
1418         st->operatortypes = clip_operatortypes;
1419         st->keymap = clip_keymap;
1420         st->listener = clip_listener;
1421         st->context = clip_context;
1422         st->dropboxes = clip_dropboxes;
1423         st->refresh = clip_refresh;
1424
1425         /* regions: main window */
1426         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1427         art->regionid = RGN_TYPE_WINDOW;
1428         art->init = clip_main_area_init;
1429         art->draw = clip_main_area_draw;
1430         art->listener = clip_main_area_listener;
1431         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1432
1433         BLI_addhead(&st->regiontypes, art);
1434
1435         /* preview */
1436         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1437         art->regionid = RGN_TYPE_PREVIEW;
1438         art->prefsizey = 240;
1439         art->init = clip_preview_area_init;
1440         art->draw = clip_preview_area_draw;
1441         art->listener = clip_preview_area_listener;
1442         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1443
1444         BLI_addhead(&st->regiontypes, art);
1445
1446         /* regions: properties */
1447         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1448         art->regionid = RGN_TYPE_UI;
1449         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1450         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1451         art->init = clip_properties_area_init;
1452         art->draw = clip_properties_area_draw;
1453         art->listener = clip_properties_area_listener;
1454         BLI_addhead(&st->regiontypes, art);
1455         ED_clip_buttons_register(art);
1456
1457         /* regions: tools */
1458         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1459         art->regionid = RGN_TYPE_TOOLS;
1460         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1461         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1462         art->listener = clip_props_area_listener;
1463         art->init = clip_tools_area_init;
1464         art->draw = clip_tools_area_draw;
1465
1466         BLI_addhead(&st->regiontypes, art);
1467
1468         /* tool properties */
1469         art = MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
1470         art->regionid = RGN_TYPE_TOOL_PROPS;
1471         art->prefsizex = 0;
1472         art->prefsizey = 120;
1473         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1474         art->listener = clip_props_area_listener;
1475         art->init = clip_tools_area_init;
1476         art->draw = clip_tools_area_draw;
1477         ED_clip_tool_props_register(art);
1478
1479         BLI_addhead(&st->regiontypes, art);
1480
1481         /* regions: header */
1482         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1483         art->regionid = RGN_TYPE_HEADER;
1484         art->prefsizey = HEADERY;
1485         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1486
1487         art->init = clip_header_area_init;
1488         art->draw = clip_header_area_draw;
1489         art->listener = clip_header_area_listener;
1490
1491         BLI_addhead(&st->regiontypes, art);
1492
1493         BKE_spacetype_register(st);
1494
1495         /* channels */
1496         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1497         art->regionid = RGN_TYPE_CHANNELS;
1498         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1499         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1500         art->listener = clip_channels_area_listener;
1501         art->init = clip_channels_area_init;
1502         art->draw = clip_channels_area_draw;
1503
1504         BLI_addhead(&st->regiontypes, art);
1505 }