Merging r43909 through r43951 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_clip / space_clip.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/space_clip.c
29  *  \ingroup spclip
30  */
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_scene_types.h"
36 #include "DNA_movieclip_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_math.h"
43
44 #include "BKE_main.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47 #include "BKE_movieclip.h"
48 #include "BKE_tracking.h"
49
50 #include "IMB_imbuf_types.h"
51
52 #include "ED_screen.h"
53 #include "ED_clip.h"
54 #include "ED_transform.h"
55
56 #include "IMB_imbuf.h"
57
58 #include "BIF_gl.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65 #include "UI_view2d.h"
66
67 #include "RNA_access.h"
68
69
70 #include "clip_intern.h"        // own include
71
72 static void init_preview_region(const bContext *C, ARegion *ar)
73 {
74         Scene *scene= CTX_data_scene(C);
75
76         ar->regiontype= RGN_TYPE_PREVIEW;
77         ar->alignment= RGN_ALIGN_TOP;
78         ar->flag|= RGN_FLAG_HIDDEN;
79
80         ar->v2d.tot.xmin= 0.0f;
81         ar->v2d.tot.ymin= -10.0f;
82         ar->v2d.tot.xmax= (float)scene->r.efra;
83         ar->v2d.tot.ymax= 10.0f;
84
85         ar->v2d.cur= ar->v2d.tot;
86
87         ar->v2d.min[0]= FLT_MIN;
88         ar->v2d.min[1]= FLT_MIN;
89
90         ar->v2d.max[0]= MAXFRAMEF;
91         ar->v2d.max[1]= FLT_MAX;
92
93         ar->v2d.scroll= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
94         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
95
96         ar->v2d.keeptot= 0;
97 }
98
99 static ARegion *clip_has_preview_region(const bContext *C, ScrArea *sa)
100 {
101         ARegion *ar, *arnew;
102
103         ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
104         if(ar)
105                 return ar;
106
107         /* add subdiv level; after header */
108         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
109
110         /* is error! */
111         if(ar==NULL)
112                 return NULL;
113
114         arnew= MEM_callocN(sizeof(ARegion), "clip preview region");
115
116         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
117         init_preview_region(C, arnew);
118
119         return arnew;
120 }
121
122 static void clip_scopes_tag_refresh(ScrArea *sa)
123 {
124         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
125         ARegion *ar;
126
127         if(sc->mode!=SC_MODE_TRACKING)
128                 return;
129
130         /* only while proeprties are visible */
131         for (ar=sa->regionbase.first; ar; ar=ar->next) {
132                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
133                         return;
134         }
135
136         sc->scopes.ok= 0;
137 }
138
139 static void clip_stabilization_tag_refresh(ScrArea *sa)
140 {
141         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
142         MovieClip *clip= ED_space_clip(sc);
143
144         if(clip) {
145                 MovieTrackingStabilization *stab= &clip->tracking.stabilization;
146
147                 stab->ok= 0;
148         }
149 }
150
151 /* ******************** default callbacks for clip space ***************** */
152
153 static SpaceLink *clip_new(const bContext *C)
154 {
155         ARegion *ar;
156         SpaceClip *sc;
157
158         sc= MEM_callocN(sizeof(SpaceClip), "initclip");
159         sc->spacetype= SPACE_CLIP;
160         sc->flag= SC_SHOW_MARKER_PATTERN|SC_SHOW_TRACK_PATH|SC_MANUAL_CALIBRATION|SC_SHOW_GRAPH_TRACKS|SC_SHOW_GRAPH_FRAMES;
161         sc->zoom= 1.0f;
162         sc->path_length= 20;
163         sc->scopes.track_preview_height= 120;
164
165         /* header */
166         ar= MEM_callocN(sizeof(ARegion), "header for clip");
167
168         BLI_addtail(&sc->regionbase, ar);
169         ar->regiontype= RGN_TYPE_HEADER;
170         ar->alignment= RGN_ALIGN_BOTTOM;
171
172         /* tools view */
173         ar= MEM_callocN(sizeof(ARegion), "tools for clip");
174
175         BLI_addtail(&sc->regionbase, ar);
176         ar->regiontype= RGN_TYPE_TOOLS;
177         ar->alignment= RGN_ALIGN_LEFT;
178
179         /* tool properties */
180         ar= MEM_callocN(sizeof(ARegion), "tool properties for clip");
181
182         BLI_addtail(&sc->regionbase, ar);
183         ar->regiontype= RGN_TYPE_TOOL_PROPS;
184         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
185
186         /* properties view */
187         ar= MEM_callocN(sizeof(ARegion), "properties for clip");
188
189         BLI_addtail(&sc->regionbase, ar);
190         ar->regiontype= RGN_TYPE_UI;
191         ar->alignment= RGN_ALIGN_RIGHT;
192
193         /* preview view */
194         ar= MEM_callocN(sizeof(ARegion), "preview for clip");
195
196         BLI_addtail(&sc->regionbase, ar);
197         init_preview_region(C, ar);
198
199         /* main area */
200         ar= MEM_callocN(sizeof(ARegion), "main area for clip");
201
202         BLI_addtail(&sc->regionbase, ar);
203         ar->regiontype= RGN_TYPE_WINDOW;
204
205         return (SpaceLink *)sc;
206 }
207
208 /* not spacelink itself */
209 static void clip_free(SpaceLink *sl)
210 {
211         SpaceClip *sc= (SpaceClip*) sl;
212
213         sc->clip= NULL;
214
215         if(sc->scopes.track_preview)
216                 IMB_freeImBuf(sc->scopes.track_preview);
217
218         ED_space_clip_free_texture_buffer(sc);
219 }
220
221 /* spacetype; init callback */
222 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
223 {
224
225 }
226
227 static SpaceLink *clip_duplicate(SpaceLink *sl)
228 {
229         SpaceClip *scn= MEM_dupallocN(sl);
230
231         /* clear or remove stuff from old */
232         scn->scopes.track_preview= NULL;
233         scn->scopes.ok= 0;
234         scn->draw_context= NULL;
235
236         return (SpaceLink *)scn;
237 }
238
239 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
240 {
241         /* context changes */
242         switch(wmn->category) {
243                 case NC_SCENE:
244                         switch(wmn->data) {
245                                 case ND_FRAME:
246                                         clip_scopes_tag_refresh(sa);
247                                         /* no break! */
248
249                                 case ND_FRAME_RANGE:
250                                         ED_area_tag_refresh(sa);
251                                         ED_area_tag_redraw(sa);
252                                         break;
253                         }
254                         break;
255                 case NC_MOVIECLIP:
256                         switch(wmn->data) {
257                                 case ND_DISPLAY:
258                                 case ND_SELECT:
259                                         clip_scopes_tag_refresh(sa);
260                                         ED_area_tag_redraw(sa);
261                                         break;
262                         }
263                         switch(wmn->action) {
264                                 case NA_REMOVED:
265                                 case NA_EDITED:
266                                 case NA_EVALUATED:
267                                         clip_stabilization_tag_refresh(sa);
268                                         /* no break! */
269
270                                 case NA_SELECTED:
271                                         clip_scopes_tag_refresh(sa);
272                                         ED_area_tag_redraw(sa);
273                                         break;
274                         }
275                         break;
276                 case NC_GEOM:
277                         switch(wmn->data) {
278                                 case ND_SELECT:
279                                         clip_scopes_tag_refresh(sa);
280                                         ED_area_tag_redraw(sa);
281                                         break;
282                         }
283                         break;
284                  case NC_SCREEN:
285                         if(wmn->data==ND_ANIMPLAY) {
286                                 ED_area_tag_redraw(sa);
287                         }
288                         break;
289                 case NC_SPACE:
290                         if(wmn->data==ND_SPACE_CLIP) {
291                                 clip_scopes_tag_refresh(sa);
292                                 clip_stabilization_tag_refresh(sa);
293                                 ED_area_tag_redraw(sa);
294                         }
295                         break;
296         }
297 }
298
299 static void clip_operatortypes(void)
300 {
301         /* ** clip_ops.c ** */
302         WM_operatortype_append(CLIP_OT_open);
303         WM_operatortype_append(CLIP_OT_reload);
304         WM_operatortype_append(CLIP_OT_view_pan);
305         WM_operatortype_append(CLIP_OT_view_zoom);
306         WM_operatortype_append(CLIP_OT_view_zoom_in);
307         WM_operatortype_append(CLIP_OT_view_zoom_out);
308         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
309         WM_operatortype_append(CLIP_OT_view_all);
310         WM_operatortype_append(CLIP_OT_view_selected);
311         WM_operatortype_append(CLIP_OT_change_frame);
312         WM_operatortype_append(CLIP_OT_rebuild_proxy);
313         WM_operatortype_append(CLIP_OT_mode_set);
314
315         /* ** clip_toolbar.c ** */
316         WM_operatortype_append(CLIP_OT_tools);
317         WM_operatortype_append(CLIP_OT_properties);
318
319         /* ** tracking_ops.c ** */
320
321         /* navigation */
322         WM_operatortype_append(CLIP_OT_frame_jump);
323
324         /* foorage */
325         WM_operatortype_append(CLIP_OT_set_center_principal);
326
327         /* selection */
328         WM_operatortype_append(CLIP_OT_select);
329         WM_operatortype_append(CLIP_OT_select_all);
330         WM_operatortype_append(CLIP_OT_select_border);
331         WM_operatortype_append(CLIP_OT_select_circle);
332         WM_operatortype_append(CLIP_OT_select_grouped);
333
334         /* markers */
335         WM_operatortype_append(CLIP_OT_add_marker);
336         WM_operatortype_append(CLIP_OT_slide_marker);
337         WM_operatortype_append(CLIP_OT_delete_track);
338         WM_operatortype_append(CLIP_OT_delete_marker);
339
340         /* track */
341         WM_operatortype_append(CLIP_OT_track_markers);
342
343         /* solving */
344         WM_operatortype_append(CLIP_OT_solve_camera);
345         WM_operatortype_append(CLIP_OT_clear_solution);
346
347         WM_operatortype_append(CLIP_OT_disable_markers);
348         WM_operatortype_append(CLIP_OT_hide_tracks);
349         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
350         WM_operatortype_append(CLIP_OT_lock_tracks);
351
352         /* orientation */
353         WM_operatortype_append(CLIP_OT_set_origin);
354         WM_operatortype_append(CLIP_OT_set_floor);
355         WM_operatortype_append(CLIP_OT_set_axis);
356         WM_operatortype_append(CLIP_OT_set_scale);
357         WM_operatortype_append(CLIP_OT_set_solution_scale);
358
359         /* detect */
360         WM_operatortype_append(CLIP_OT_detect_features);
361
362         /* stabilization */
363         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
364         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
365         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
366         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
367
368         /* clean-up */
369         WM_operatortype_append(CLIP_OT_clear_track_path);
370         WM_operatortype_append(CLIP_OT_join_tracks);
371         WM_operatortype_append(CLIP_OT_track_copy_color);
372
373         WM_operatortype_append(CLIP_OT_clean_tracks);
374
375         /* graph editing */
376         WM_operatortype_append(CLIP_OT_graph_select);
377         WM_operatortype_append(CLIP_OT_graph_delete_curve);
378         WM_operatortype_append(CLIP_OT_graph_delete_knot);
379         WM_operatortype_append(CLIP_OT_graph_view_all);
380         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
381
382         /* object tracking */
383         WM_operatortype_append(CLIP_OT_tracking_object_new);
384         WM_operatortype_append(CLIP_OT_tracking_object_remove);
385
386         /* clipboard */
387         WM_operatortype_append(CLIP_OT_copy_tracks);
388         WM_operatortype_append(CLIP_OT_paste_tracks);
389 }
390
391 static void clip_keymap(struct wmKeyConfig *keyconf)
392 {
393         wmKeyMap *keymap;
394         wmKeyMapItem *kmi;
395
396         /* ******** Global hotkeys avalaible for all regions ******** */
397
398         keymap= WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
399
400         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
401
402         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
403         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
404
405         /* 2d tracking */
406         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
407         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
408         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
409         WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
410         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
411         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
412         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
413         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
414         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
415         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
416
417         /* mode */
418         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
419         RNA_enum_set(kmi->ptr, "mode", SC_MODE_RECONSTRUCTION);
420         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
421
422         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
423         RNA_enum_set(kmi->ptr, "mode", SC_MODE_DISTORTION);
424         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
425
426         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle_enum", ZKEY, KM_PRESS, 0, 0);
427         RNA_string_set(kmi->ptr, "data_path", "space_data.view");
428         RNA_string_set(kmi->ptr, "value_1", "CLIP");
429         RNA_string_set(kmi->ptr, "value_2", "GRAPH");
430
431         WM_keymap_add_item(keymap, "CLIP_OT_solve_camera", SKEY, KM_PRESS, KM_SHIFT, 0);
432
433         /* ******** Hotkeys avalaible for main region only ******** */
434
435         keymap= WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
436
437         /* ** View/navigation ** */
438
439         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
440         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
441         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
442
443         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
444         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
445         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
446         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
447         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
448         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
449
450         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
451         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
452         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
453         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
454         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
455         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
456         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
457
458         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
459         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
460
461         /* jump to special frame */
462         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
463         RNA_enum_set(kmi->ptr, "position", 0);
464
465         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
466         RNA_enum_set(kmi->ptr, "position", 1);
467
468         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
469         RNA_enum_set(kmi->ptr, "position", 2);
470
471         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
472         RNA_enum_set(kmi->ptr, "position", 3);
473
474         /* "timeline" */
475         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
476
477         /* selection */
478         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
479         RNA_boolean_set(kmi->ptr, "extend", FALSE);
480         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
481         RNA_boolean_set(kmi->ptr, "extend", TRUE);
482         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
483         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
484         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
485         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
486         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
487         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
488         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
489
490         /* marker */
491         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
492
493         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
494         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
495
496         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
497
498         kmi= WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
499         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
500
501         /* tracks */
502         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
503         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
504
505         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
506         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
507
508         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
509         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
510
511         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
512         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
513
514         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
515         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
516
517         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
518
519         /* clean-up */
520         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
521         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
522         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
523         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
524         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
525         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
526
527         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
528
529         /* menus */
530         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
531
532         /* display */
533         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
534         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
535
536         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", DKEY, KM_PRESS, KM_ALT, 0);
537         RNA_string_set(kmi->ptr, "data_path", "space_data.show_disabled");
538
539         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_ALT, 0);
540         RNA_string_set(kmi->ptr, "data_path", "space_data.show_marker_search");
541
542         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
543         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
544
545         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
546
547         /* ******** Hotkeys avalaible for preview region only ******** */
548
549         keymap= WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
550
551         /* "timeline" */
552         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
553
554         /* selection */
555         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
556         RNA_boolean_set(kmi->ptr, "extend", FALSE);
557         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
558         RNA_boolean_set(kmi->ptr, "extend", TRUE);
559
560         /* delete */
561         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
562         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
563
564         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
565         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
566
567         /* view */
568         WM_keymap_add_item(keymap, "CLIP_OT_graph_view_all", HOMEKEY, KM_PRESS, 0, 0);
569         WM_keymap_add_item(keymap, "CLIP_OT_graph_center_current_frame", PADPERIOD, KM_PRESS, 0, 0);
570
571         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
572         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_time_cursor");
573
574         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
575 }
576
577 const char *clip_context_dir[]= {"edit_movieclip", NULL};
578
579 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
580 {
581         SpaceClip *sc= CTX_wm_space_clip(C);
582
583         if(CTX_data_dir(member)) {
584                 CTX_data_dir_set(result, clip_context_dir);
585                 return 1;
586         }
587         else if(CTX_data_equals(member, "edit_movieclip")) {
588                 CTX_data_id_pointer_set(result, &sc->clip->id);
589                 return 1;
590         }
591
592         return 0;
593 }
594
595 static void clip_refresh(const bContext *C, ScrArea *sa)
596 {
597         wmWindowManager *wm= CTX_wm_manager(C);
598         wmWindow *window= CTX_wm_window(C);
599         Scene *scene = CTX_data_scene(C);
600         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
601         ARegion *ar_main= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
602         ARegion *ar_preview= clip_has_preview_region(C, sa);
603         int view_changed= 0;
604
605         switch (sc->view) {
606                 case SC_VIEW_CLIP:
607                         if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
608                                 ar_preview->flag |= RGN_FLAG_HIDDEN;
609                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
610                                 WM_event_remove_handlers((bContext*)C, &ar_preview->handlers);
611                                 view_changed= 1;
612                         }
613                         if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
614                                 ar_main->alignment= RGN_ALIGN_NONE;
615                                 view_changed= 1;
616                         }
617                         break;
618                 case SC_VIEW_GRAPH:
619                         if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
620                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
621                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
622                                 ar_preview->v2d.cur = ar_preview->v2d.tot;
623                                 view_changed= 1;
624                         }
625                         if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
626                                 ar_main->alignment= RGN_ALIGN_NONE;
627                                 view_changed= 1;
628                         }
629                         if (ar_preview && !ELEM(ar_preview->alignment, RGN_ALIGN_TOP,  RGN_ALIGN_BOTTOM)) {
630                                 ar_preview->alignment= RGN_ALIGN_TOP;
631                                 view_changed= 1;
632                         }
633                         break;
634         }
635
636         if(view_changed) {
637                 ED_area_initialize(wm, window, sa);
638                 ED_area_tag_redraw(sa);
639         }
640
641         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
642 }
643
644 /********************* main region ********************/
645
646 /* sets up the fields of the View2D from zoom and offset */
647 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
648 {
649         MovieClip *clip= ED_space_clip(sc);
650         float x1, y1, w, h;
651         int width, height, winx, winy;
652
653         ED_space_clip_size(sc, &width, &height);
654
655         w= width;
656         h= height;
657
658         if(clip)
659                 h*= clip->aspy/clip->aspx/clip->tracking.camera.pixel_aspect;
660
661         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
662         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
663
664         ar->v2d.tot.xmin= 0;
665         ar->v2d.tot.ymin= 0;
666         ar->v2d.tot.xmax= w;
667         ar->v2d.tot.ymax= h;
668
669         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
670         ar->v2d.mask.xmax= winx;
671         ar->v2d.mask.ymax= winy;
672
673         /* which part of the image space do we see? */
674         x1= ar->winrct.xmin+(winx-sc->zoom*w)/2.0f;
675         y1= ar->winrct.ymin+(winy-sc->zoom*h)/2.0f;
676
677         x1-= sc->zoom*sc->xof;
678         y1-= sc->zoom*sc->yof;
679
680         /* relative display right */
681         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sc->zoom);
682         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sc->zoom);
683
684         /* relative display left */
685         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sc->zoom);
686         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sc->zoom);
687
688         /* normalize 0.0..1.0 */
689         ar->v2d.cur.xmin /= w;
690         ar->v2d.cur.xmax /= w;
691         ar->v2d.cur.ymin /= h;
692         ar->v2d.cur.ymax /= h;
693 }
694
695 /* add handlers, stuff you only do once or on area/region changes */
696 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
697 {
698         wmKeyMap *keymap;
699
700         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
701
702         /* own keymap */
703         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
704         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
705
706         keymap= WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
707         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
708 }
709
710 static void clip_main_area_draw(const bContext *C, ARegion *ar)
711 {
712         /* draw entirely, view changes should be handled here */
713         SpaceClip *sc= CTX_wm_space_clip(C);
714         Scene *scene= CTX_data_scene(C);
715         MovieClip *clip= ED_space_clip(sc);
716
717         /* if trcking is in progress, we should sunchronize framenr from clipuser
718            so latest tracked frame would be shown */
719         if(clip && clip->tracking_context)
720                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
721
722         if(sc->flag&SC_LOCK_SELECTION) {
723                 ImBuf *tmpibuf= NULL;
724
725                 if(clip && clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
726                         tmpibuf= ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
727                 }
728
729                 if(ED_clip_view_selection(sc, ar, 0)) {
730                         sc->xof+= sc->xlockof;
731                         sc->yof+= sc->ylockof;
732                 }
733
734                 if(tmpibuf)
735                         IMB_freeImBuf(tmpibuf);
736         }
737
738         /* clear and setup matrix */
739         UI_ThemeClearColor(TH_BACK);
740         glClear(GL_COLOR_BUFFER_BIT);
741
742         /* data... */
743         movieclip_main_area_set_view2d(sc, ar);
744
745         clip_draw_main(sc, ar, scene);
746
747         /* Grease Pencil */
748         clip_draw_grease_pencil((bContext *)C, 1);
749
750         /* reset view matrix */
751         UI_view2d_view_restore(C);
752
753         /* draw Grease Pencil - screen space only */
754         clip_draw_grease_pencil((bContext *)C, 0);
755 }
756
757 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
758 {
759         /* context changes */
760         switch(wmn->category) {
761                 case NC_SCREEN:
762                         if (wmn->data==ND_GPENCIL)
763                                 ED_region_tag_redraw(ar);
764                 break;
765         }
766 }
767
768 /****************** preview region ******************/
769
770 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
771 {
772         wmKeyMap *keymap;
773
774         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
775
776         /* own keymap */
777         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
778         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
779
780         keymap= WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
781         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
782 }
783
784 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
785 {
786         View2D *v2d= &ar->v2d;
787         View2DScrollers *scrollers;
788         SpaceClip *sc= CTX_wm_space_clip(C);
789         Scene *scene= CTX_data_scene(C);
790         short unitx= V2D_UNIT_FRAMESCALE, unity= V2D_UNIT_VALUES;
791
792         if(sc->flag & SC_LOCK_TIMECURSOR)
793                 ED_clip_graph_center_current_frame(scene, ar);
794
795         /* clear and setup matrix */
796         UI_ThemeClearColor(TH_BACK);
797         glClear(GL_COLOR_BUFFER_BIT);
798
799         UI_view2d_view_ortho(v2d);
800
801         /* data... */
802         clip_draw_graph(sc, ar, scene);
803
804         /* reset view matrix */
805         UI_view2d_view_restore(C);
806
807         /* scrollers */
808         scrollers= UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
809         UI_view2d_scrollers_draw(C, v2d, scrollers);
810         UI_view2d_scrollers_free(scrollers);
811 }
812
813 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
814 {
815 }
816
817 /****************** header region ******************/
818
819 /* add handlers, stuff you only do once or on area/region changes */
820 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
821 {
822         ED_region_header_init(ar);
823 }
824
825 static void clip_header_area_draw(const bContext *C, ARegion *ar)
826 {
827         ED_region_header(C, ar);
828 }
829
830 /****************** tools region ******************/
831
832 /* add handlers, stuff you only do once or on area/region changes */
833 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
834 {
835         ED_region_panels_init(wm, ar);
836 }
837
838 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
839 {
840         ED_region_panels(C, ar, 1, NULL, -1);
841 }
842
843 /****************** tool properties region ******************/
844
845 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
846 {
847         /* context changes */
848         switch(wmn->category) {
849                 case NC_WM:
850                         if(wmn->data == ND_HISTORY)
851                                 ED_region_tag_redraw(ar);
852                         break;
853                 case NC_SCENE:
854                         if(wmn->data == ND_MODE)
855                                 ED_region_tag_redraw(ar);
856                         break;
857                 case NC_SPACE:
858                         if(wmn->data == ND_SPACE_CLIP)
859                                 ED_region_tag_redraw(ar);
860                         break;
861                 case NC_SCREEN:
862                         if(wmn->data == ND_GPENCIL)
863                                 ED_region_tag_redraw(ar);
864                         break;
865         }
866 }
867
868 /****************** properties region ******************/
869
870 /* add handlers, stuff you only do once or on area/region changes */
871 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
872 {
873         wmKeyMap *keymap;
874
875         ED_region_panels_init(wm, ar);
876
877         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
878         WM_event_add_keymap_handler(&ar->handlers, keymap);
879 }
880
881 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
882 {
883         SpaceClip *sc= CTX_wm_space_clip(C);
884
885         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
886
887         ED_region_panels(C, ar, 1, NULL, -1);
888 }
889
890 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
891 {
892         /* context changes */
893         switch(wmn->category) {
894                 case NC_SCREEN:
895                         if (wmn->data==ND_GPENCIL)
896                                 ED_region_tag_redraw(ar);
897                         break;
898                 case NC_BRUSH:
899                         if(wmn->action==NA_EDITED)
900                                 ED_region_tag_redraw(ar);
901                         break;
902         }
903 }
904
905 /********************* registration ********************/
906
907 /* only called once, from space/spacetypes.c */
908 void ED_spacetype_clip(void)
909 {
910         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype clip");
911         ARegionType *art;
912
913         st->spaceid= SPACE_CLIP;
914         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
915
916         st->new= clip_new;
917         st->free= clip_free;
918         st->init= clip_init;
919         st->duplicate= clip_duplicate;
920         st->operatortypes= clip_operatortypes;
921         st->keymap= clip_keymap;
922         st->listener= clip_listener;
923         st->context= clip_context;
924         st->refresh= clip_refresh;
925
926         /* regions: main window */
927         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
928         art->regionid= RGN_TYPE_WINDOW;
929         art->init= clip_main_area_init;
930         art->draw= clip_main_area_draw;
931         art->listener= clip_main_area_listener;
932         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_GPENCIL;
933
934         BLI_addhead(&st->regiontypes, art);
935
936         /* preview */
937         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
938         art->regionid = RGN_TYPE_PREVIEW;
939         art->prefsizey = 240;
940         art->init= clip_preview_area_init;
941         art->draw= clip_preview_area_draw;
942         art->listener= clip_preview_area_listener;
943         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
944
945         BLI_addhead(&st->regiontypes, art);
946
947         /* regions: properties */
948         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
949         art->regionid= RGN_TYPE_UI;
950         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
951         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
952         art->init= clip_properties_area_init;
953         art->draw= clip_properties_area_draw;
954         art->listener= clip_properties_area_listener;
955         BLI_addhead(&st->regiontypes, art);
956         ED_clip_buttons_register(art);
957
958         /* regions: tools */
959         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
960         art->regionid= RGN_TYPE_TOOLS;
961         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
962         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
963         art->listener= clip_props_area_listener;
964         art->init= clip_tools_area_init;
965         art->draw= clip_tools_area_draw;
966
967         BLI_addhead(&st->regiontypes, art);
968
969         /* tool properties */
970         art= MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
971         art->regionid = RGN_TYPE_TOOL_PROPS;
972         art->prefsizex= 0;
973         art->prefsizey= 120;
974         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
975         art->listener= clip_props_area_listener;
976         art->init= clip_tools_area_init;
977         art->draw= clip_tools_area_draw;
978         ED_clip_tool_props_register(art);
979
980         BLI_addhead(&st->regiontypes, art);
981
982         /* regions: header */
983         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
984         art->regionid= RGN_TYPE_HEADER;
985         art->prefsizey= HEADERY;
986         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
987
988         art->init= clip_header_area_init;
989         art->draw= clip_header_area_draw;
990
991         BLI_addhead(&st->regiontypes, art);
992
993         BKE_spacetype_register(st);
994 }