- Synchronize with changes in trunk
[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                 return TRUE;
722         }
723         else if (CTX_data_equals(member, "edit_movieclip")) {
724                 CTX_data_id_pointer_set(result, &sc->clip->id);
725                 return TRUE;
726         }
727
728         return FALSE;
729 }
730
731 static void clip_refresh(const bContext *C, ScrArea *sa)
732 {
733         wmWindowManager *wm = CTX_wm_manager(C);
734         wmWindow *window = CTX_wm_window(C);
735         Scene *scene = CTX_data_scene(C);
736         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
737         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
738         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
739         ARegion *ar_tool_props = BKE_area_find_region_type(sa, RGN_TYPE_TOOL_PROPS);
740         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
741         ARegion *ar_properties = ED_clip_has_properties_region(sa);
742         ARegion *ar_channels = ED_clip_has_channels_region(sa);
743         int main_visible = FALSE, preview_visible = FALSE, tools_visible = FALSE;
744         int tool_props_visible = FALSE, properties_visible = FALSE, channels_visible = FALSE;
745         int view_changed = FALSE;
746
747         switch (sc->view) {
748                 case SC_VIEW_CLIP:
749                         main_visible = TRUE;
750                         preview_visible = FALSE;
751                         tools_visible = TRUE;
752                         tool_props_visible = TRUE;
753                         properties_visible = TRUE;
754                         channels_visible = FALSE;
755                         break;
756                 case SC_VIEW_GRAPH:
757                         main_visible = FALSE;
758                         preview_visible = TRUE;
759                         tools_visible = FALSE;
760                         tool_props_visible = FALSE;
761                         properties_visible = FALSE;
762                         channels_visible = FALSE;
763
764                         reinit_preview_region(C, ar_preview);
765                         break;
766                 case SC_VIEW_DOPESHEET:
767                         main_visible = FALSE;
768                         preview_visible = TRUE;
769                         tools_visible = FALSE;
770                         tool_props_visible = FALSE;
771                         properties_visible = FALSE;
772                         channels_visible = TRUE;
773
774                         reinit_preview_region(C, ar_preview);
775                         break;
776         }
777
778         if (main_visible) {
779                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
780                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
781                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
782                         view_changed = TRUE;
783                 }
784
785                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
786                         ar_main->alignment = RGN_ALIGN_NONE;
787                         view_changed = TRUE;
788                 }
789         }
790         else {
791                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
792                         ar_main->flag |= RGN_FLAG_HIDDEN;
793                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
794                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
795                         view_changed = TRUE;
796                 }
797                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
798                         ar_main->alignment = RGN_ALIGN_NONE;
799                         view_changed = TRUE;
800                 }
801         }
802
803         if (properties_visible) {
804                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
805                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
806                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
807                         view_changed = TRUE;
808                 }
809                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
810                         ar_properties->alignment = RGN_ALIGN_RIGHT;
811                         view_changed = TRUE;
812                 }
813         }
814         else {
815                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
816                         ar_properties->flag |= RGN_FLAG_HIDDEN;
817                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
818                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
819                         view_changed = TRUE;
820                 }
821                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
822                         ar_properties->alignment = RGN_ALIGN_NONE;
823                         view_changed = TRUE;
824                 }
825         }
826
827         if (tools_visible) {
828                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
829                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
830                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
831                         view_changed = TRUE;
832                 }
833                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
834                         ar_tools->alignment = RGN_ALIGN_LEFT;
835                         view_changed = TRUE;
836                 }
837         }
838         else {
839                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
840                         ar_tools->flag |= RGN_FLAG_HIDDEN;
841                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
842                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
843                         view_changed = TRUE;
844                 }
845                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
846                         ar_tools->alignment = RGN_ALIGN_NONE;
847                         view_changed = TRUE;
848                 }
849         }
850
851         if (tool_props_visible) {
852                 if (ar_tool_props && (ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
853                         ar_tool_props->flag &= ~RGN_FLAG_HIDDEN;
854                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
855                         view_changed = TRUE;
856                 }
857                 if (ar_tool_props && (ar_tool_props->alignment != (RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV))) {
858                         ar_tool_props->alignment = RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
859                         view_changed = TRUE;
860                 }
861         }
862         else {
863                 if (ar_tool_props && !(ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
864                         ar_tool_props->flag |= RGN_FLAG_HIDDEN;
865                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
866                         WM_event_remove_handlers((bContext *)C, &ar_tool_props->handlers);
867                         view_changed = TRUE;
868                 }
869                 if (ar_tool_props && ar_tool_props->alignment != RGN_ALIGN_NONE) {
870                         ar_tool_props->alignment = RGN_ALIGN_NONE;
871                         view_changed = TRUE;
872                 }
873         }
874
875         if (preview_visible) {
876                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
877                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
878                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
879                         ar_preview->v2d.cur = ar_preview->v2d.tot;
880                         view_changed = TRUE;
881                 }
882                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
883                         ar_preview->alignment = RGN_ALIGN_NONE;
884                         view_changed = TRUE;
885                 }
886         }
887         else {
888                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
889                         ar_preview->flag |= RGN_FLAG_HIDDEN;
890                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
891                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
892                         view_changed = TRUE;
893                 }
894                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
895                         ar_preview->alignment = RGN_ALIGN_NONE;
896                         view_changed = TRUE;
897                 }
898         }
899
900         if (channels_visible) {
901                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
902                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
903                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
904                         view_changed = TRUE;
905                 }
906                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
907                         ar_channels->alignment = RGN_ALIGN_LEFT;
908                         view_changed = TRUE;
909                 }
910         }
911         else {
912                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
913                         ar_channels->flag |= RGN_FLAG_HIDDEN;
914                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
915                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
916                         view_changed = TRUE;
917                 }
918                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
919                         ar_channels->alignment = RGN_ALIGN_NONE;
920                         view_changed = TRUE;
921                 }
922         }
923
924         if (view_changed) {
925                 ED_area_initialize(wm, window, sa);
926                 ED_area_tag_redraw(sa);
927         }
928
929         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
930 }
931
932 /********************* main region ********************/
933
934 /* sets up the fields of the View2D from zoom and offset */
935 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
936 {
937         MovieClip *clip = ED_space_clip(sc);
938         float x1, y1, w, h;
939         int width, height, winx, winy;
940
941         ED_space_clip_size(sc, &width, &height);
942
943         w = width;
944         h = height;
945
946         if (clip)
947                 h *= clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
948
949         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
950         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
951
952         ar->v2d.tot.xmin = 0;
953         ar->v2d.tot.ymin = 0;
954         ar->v2d.tot.xmax = w;
955         ar->v2d.tot.ymax = h;
956
957         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
958         ar->v2d.mask.xmax = winx;
959         ar->v2d.mask.ymax = winy;
960
961         /* which part of the image space do we see? */
962         x1= ar->winrct.xmin + (winx-sc->zoom * w) / 2.0f;
963         y1= ar->winrct.ymin + (winy-sc->zoom * h) / 2.0f;
964
965         x1-= sc->zoom * sc->xof;
966         y1-= sc->zoom * sc->yof;
967
968         /* relative display right */
969         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
970         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
971
972         /* relative display left */
973         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
974         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
975
976         /* normalize 0.0..1.0 */
977         ar->v2d.cur.xmin /= w;
978         ar->v2d.cur.xmax /= w;
979         ar->v2d.cur.ymin /= h;
980         ar->v2d.cur.ymax /= h;
981 }
982
983 /* add handlers, stuff you only do once or on area/region changes */
984 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
985 {
986         wmKeyMap *keymap;
987
988         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
989
990         /* own keymap */
991         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
992         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
993
994         keymap = WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
995         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
996 }
997
998 static void clip_main_area_draw(const bContext *C, ARegion *ar)
999 {
1000         /* draw entirely, view changes should be handled here */
1001         SpaceClip *sc = CTX_wm_space_clip(C);
1002         Scene *scene = CTX_data_scene(C);
1003         MovieClip *clip = ED_space_clip(sc);
1004
1005         /* if tracking is in progress, we should synchronize framenr from clipuser
1006          * so latest tracked frame would be shown */
1007         if (clip && clip->tracking_context)
1008                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
1009
1010         if (sc->flag & SC_LOCK_SELECTION) {
1011                 ImBuf *tmpibuf = NULL;
1012
1013                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1014                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
1015                 }
1016
1017                 if (ED_clip_view_selection(sc, ar, 0)) {
1018                         sc->xof += sc->xlockof;
1019                         sc->yof += sc->ylockof;
1020                 }
1021
1022                 if (tmpibuf)
1023                         IMB_freeImBuf(tmpibuf);
1024         }
1025
1026         /* clear and setup matrix */
1027         UI_ThemeClearColor(TH_BACK);
1028         glClear(GL_COLOR_BUFFER_BIT);
1029
1030         /* data... */
1031         movieclip_main_area_set_view2d(sc, ar);
1032
1033         clip_draw_main(sc, ar, scene);
1034
1035         /* Grease Pencil */
1036         clip_draw_grease_pencil((bContext *)C, 1);
1037
1038         /* reset view matrix */
1039         UI_view2d_view_restore(C);
1040
1041         /* draw Grease Pencil - screen space only */
1042         clip_draw_grease_pencil((bContext *)C, 0);
1043 }
1044
1045 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
1046 {
1047         /* context changes */
1048         switch (wmn->category) {
1049                 case NC_SCREEN:
1050                         if (wmn->data == ND_GPENCIL)
1051                                 ED_region_tag_redraw(ar);
1052                 break;
1053         }
1054 }
1055
1056 /****************** preview region ******************/
1057
1058 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
1059 {
1060         wmKeyMap *keymap;
1061
1062         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
1063
1064         /* own keymap */
1065         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1066         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1067
1068         keymap = WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
1069         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1070 }
1071
1072 static void graph_area_draw(const bContext *C, ARegion *ar)
1073 {
1074         View2D *v2d = &ar->v2d;
1075         View2DScrollers *scrollers;
1076         SpaceClip *sc = CTX_wm_space_clip(C);
1077         Scene *scene = CTX_data_scene(C);
1078         short unitx, unity;
1079
1080         if (sc->flag & SC_LOCK_TIMECURSOR)
1081                 ED_clip_graph_center_current_frame(scene, ar);
1082
1083         /* clear and setup matrix */
1084         UI_ThemeClearColor(TH_BACK);
1085         glClear(GL_COLOR_BUFFER_BIT);
1086
1087         UI_view2d_view_ortho(v2d);
1088
1089         /* data... */
1090         clip_draw_graph(sc, ar, scene);
1091
1092         /* reset view matrix */
1093         UI_view2d_view_restore(C);
1094
1095         /* scrollers */
1096         unitx = (sc->flag & SC_SHOW_SECONDS)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1097         unity = V2D_UNIT_VALUES;
1098         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1099         UI_view2d_scrollers_draw(C, v2d, scrollers);
1100         UI_view2d_scrollers_free(scrollers);
1101 }
1102
1103 static void dopesheet_area_draw(const bContext *C, ARegion *ar)
1104 {
1105         Scene *scene = CTX_data_scene(C);
1106         SpaceClip *sc = CTX_wm_space_clip(C);
1107         View2D *v2d = &ar->v2d;
1108         View2DGrid *grid;
1109         View2DScrollers *scrollers;
1110         short unit = 0;
1111
1112         /* clear and setup matrix */
1113         UI_ThemeClearColor(TH_BACK);
1114         glClear(GL_COLOR_BUFFER_BIT);
1115
1116         UI_view2d_view_ortho(v2d);
1117
1118         /* time grid */
1119         unit = (sc->flag & SC_SHOW_SECONDS)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1120         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1121         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1122         UI_view2d_grid_free(grid);
1123
1124         /* data... */
1125         clip_draw_dopesheet_main(sc, ar, scene);
1126
1127         /* reset view matrix */
1128         UI_view2d_view_restore(C);
1129
1130         /* scrollers */
1131         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1132         UI_view2d_scrollers_draw(C, v2d, scrollers);
1133         UI_view2d_scrollers_free(scrollers);
1134 }
1135
1136 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
1137 {
1138         SpaceClip *sc = CTX_wm_space_clip(C);
1139
1140         if (sc->view == SC_VIEW_GRAPH)
1141                 graph_area_draw(C, ar);
1142         else if (sc->view == SC_VIEW_DOPESHEET)
1143                 dopesheet_area_draw(C, ar);
1144 }
1145
1146 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1147 {
1148 }
1149
1150 /****************** channels region ******************/
1151
1152 static void clip_channels_area_init(wmWindowManager *wm, ARegion *ar)
1153 {
1154         wmKeyMap *keymap;
1155         
1156         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1157
1158         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1159         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1160 }
1161
1162 static void clip_channels_area_draw(const bContext *C, ARegion *ar)
1163 {
1164         View2D *v2d = &ar->v2d;
1165         View2DScrollers *scrollers;
1166
1167         /* clear and setup matrix */
1168         UI_ThemeClearColor(TH_BACK);
1169         glClear(GL_COLOR_BUFFER_BIT);
1170
1171         UI_view2d_view_ortho(v2d);
1172
1173         /* data... */
1174         clip_draw_dopesheet_channels(C, ar);
1175
1176         /* reset view matrix */
1177         UI_view2d_view_restore(C);
1178
1179         /* scrollers */
1180         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1181         UI_view2d_scrollers_draw(C, v2d, scrollers);
1182         UI_view2d_scrollers_free(scrollers);
1183 }
1184
1185 static void clip_channels_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1186 {
1187 }
1188
1189 /****************** header region ******************/
1190
1191 /* add handlers, stuff you only do once or on area/region changes */
1192 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1193 {
1194         ED_region_header_init(ar);
1195 }
1196
1197 static void clip_header_area_draw(const bContext *C, ARegion *ar)
1198 {
1199         ED_region_header(C, ar);
1200 }
1201
1202 /****************** tools region ******************/
1203
1204 /* add handlers, stuff you only do once or on area/region changes */
1205 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
1206 {
1207         ED_region_panels_init(wm, ar);
1208 }
1209
1210 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
1211 {
1212         ED_region_panels(C, ar, 1, NULL, -1);
1213 }
1214
1215 /****************** tool properties region ******************/
1216
1217 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
1218 {
1219         /* context changes */
1220         switch (wmn->category) {
1221                 case NC_WM:
1222                         if (wmn->data == ND_HISTORY)
1223                                 ED_region_tag_redraw(ar);
1224                         break;
1225                 case NC_SCENE:
1226                         if (wmn->data == ND_MODE)
1227                                 ED_region_tag_redraw(ar);
1228                         break;
1229                 case NC_SPACE:
1230                         if (wmn->data == ND_SPACE_CLIP)
1231                                 ED_region_tag_redraw(ar);
1232                         break;
1233                 case NC_SCREEN:
1234                         if (wmn->data == ND_GPENCIL)
1235                                 ED_region_tag_redraw(ar);
1236                         break;
1237         }
1238 }
1239
1240 /****************** properties region ******************/
1241
1242 /* add handlers, stuff you only do once or on area/region changes */
1243 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
1244 {
1245         wmKeyMap *keymap;
1246
1247         ED_region_panels_init(wm, ar);
1248
1249         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1250         WM_event_add_keymap_handler(&ar->handlers, keymap);
1251 }
1252
1253 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
1254 {
1255         SpaceClip *sc = CTX_wm_space_clip(C);
1256
1257         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1258
1259         ED_region_panels(C, ar, 1, NULL, -1);
1260 }
1261
1262 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
1263 {
1264         /* context changes */
1265         switch (wmn->category) {
1266                 case NC_SCREEN:
1267                         if (wmn->data ==ND_GPENCIL)
1268                                 ED_region_tag_redraw(ar);
1269                         break;
1270                 case NC_BRUSH:
1271                         if (wmn->action ==NA_EDITED)
1272                                 ED_region_tag_redraw(ar);
1273                         break;
1274         }
1275 }
1276
1277 /********************* registration ********************/
1278
1279 /* only called once, from space/spacetypes.c */
1280 void ED_spacetype_clip(void)
1281 {
1282         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1283         ARegionType *art;
1284
1285         st->spaceid = SPACE_CLIP;
1286         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1287
1288         st->new = clip_new;
1289         st->free = clip_free;
1290         st->init = clip_init;
1291         st->duplicate = clip_duplicate;
1292         st->operatortypes = clip_operatortypes;
1293         st->keymap = clip_keymap;
1294         st->listener = clip_listener;
1295         st->context = clip_context;
1296         st->refresh = clip_refresh;
1297
1298         /* regions: main window */
1299         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1300         art->regionid = RGN_TYPE_WINDOW;
1301         art->init = clip_main_area_init;
1302         art->draw = clip_main_area_draw;
1303         art->listener = clip_main_area_listener;
1304         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_GPENCIL;
1305
1306         BLI_addhead(&st->regiontypes, art);
1307
1308         /* preview */
1309         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1310         art->regionid = RGN_TYPE_PREVIEW;
1311         art->prefsizey = 240;
1312         art->init = clip_preview_area_init;
1313         art->draw = clip_preview_area_draw;
1314         art->listener = clip_preview_area_listener;
1315         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
1316
1317         BLI_addhead(&st->regiontypes, art);
1318
1319         /* regions: properties */
1320         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1321         art->regionid = RGN_TYPE_UI;
1322         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1323         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
1324         art->init = clip_properties_area_init;
1325         art->draw = clip_properties_area_draw;
1326         art->listener = clip_properties_area_listener;
1327         BLI_addhead(&st->regiontypes, art);
1328         ED_clip_buttons_register(art);
1329
1330         /* regions: tools */
1331         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1332         art->regionid = RGN_TYPE_TOOLS;
1333         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1334         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
1335         art->listener = clip_props_area_listener;
1336         art->init = clip_tools_area_init;
1337         art->draw = clip_tools_area_draw;
1338
1339         BLI_addhead(&st->regiontypes, art);
1340
1341         /* tool properties */
1342         art = MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
1343         art->regionid = RGN_TYPE_TOOL_PROPS;
1344         art->prefsizex = 0;
1345         art->prefsizey = 120;
1346         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
1347         art->listener = clip_props_area_listener;
1348         art->init = clip_tools_area_init;
1349         art->draw = clip_tools_area_draw;
1350         ED_clip_tool_props_register(art);
1351
1352         BLI_addhead(&st->regiontypes, art);
1353
1354         /* regions: header */
1355         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1356         art->regionid = RGN_TYPE_HEADER;
1357         art->prefsizey = HEADERY;
1358         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
1359
1360         art->init = clip_header_area_init;
1361         art->draw = clip_header_area_draw;
1362
1363         BLI_addhead(&st->regiontypes, art);
1364
1365         BKE_spacetype_register(st);
1366
1367         /* channels */
1368         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1369         art->regionid = RGN_TYPE_CHANNELS;
1370         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1371         art->keymapflag = ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
1372         art->listener = clip_channels_area_listener;
1373         art->init = clip_channels_area_init;
1374         art->draw = clip_channels_area_draw;
1375
1376         BLI_addhead(&st->regiontypes, art);
1377 }