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