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