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