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