Camera tracking integration
[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
45 #include "BKE_main.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48 #include "BKE_movieclip.h"
49 #include "BKE_tracking.h"
50
51 #include "IMB_imbuf_types.h"
52
53 #include "ED_screen.h"
54 #include "ED_clip.h"
55 #include "ED_transform.h"
56
57 #include "IMB_imbuf.h"
58
59 #include "BIF_gl.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "UI_interface.h"
65 #include "UI_resources.h"
66 #include "UI_view2d.h"
67
68 #include "RNA_access.h"
69
70
71 #include "clip_intern.h"        // own include
72
73 static void clip_scopes_tag_refresh(ScrArea *sa)
74 {
75         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
76         ARegion *ar;
77
78         /* only while proeprties are visible */
79         for (ar=sa->regionbase.first; ar; ar=ar->next) {
80                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
81                         return;
82         }
83
84         sc->scopes.ok= 0;
85 }
86
87 static void clip_stabilization_tag_refresh(ScrArea *sa)
88 {
89         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
90         MovieClip *clip= ED_space_clip(sc);
91         MovieTrackingStabilization *stab= &clip->tracking.stabilization;
92
93         stab->ok= 0;
94 }
95
96 /* ******************** default callbacks for clip space ***************** */
97
98 static SpaceLink *clip_new(const bContext *UNUSED(C))
99 {
100         ARegion *ar;
101         SpaceClip *sc;
102
103         sc= MEM_callocN(sizeof(SpaceClip), "initclip");
104         sc->spacetype= SPACE_CLIP;
105         sc->flag= SC_SHOW_MARKER_PATTERN|SC_SHOW_TRACK_PATH;
106         sc->zoom= 1.0f;
107         sc->path_length= 20;
108         sc->scopes.track_preview_height= 120;
109
110         /* header */
111         ar= MEM_callocN(sizeof(ARegion), "header for clip");
112
113         BLI_addtail(&sc->regionbase, ar);
114         ar->regiontype= RGN_TYPE_HEADER;
115         ar->alignment= RGN_ALIGN_BOTTOM;
116
117         /* tools view */
118         ar= MEM_callocN(sizeof(ARegion), "tools for logic");
119
120         BLI_addtail(&sc->regionbase, ar);
121         ar->regiontype= RGN_TYPE_TOOLS;
122         ar->alignment= RGN_ALIGN_LEFT;
123
124         /* properties view */
125         ar= MEM_callocN(sizeof(ARegion), "properties for logic");
126
127         BLI_addtail(&sc->regionbase, ar);
128         ar->regiontype= RGN_TYPE_UI;
129         ar->alignment= RGN_ALIGN_RIGHT;
130
131         /* main area */
132         ar= MEM_callocN(sizeof(ARegion), "main area for clip");
133
134         BLI_addtail(&sc->regionbase, ar);
135         ar->regiontype= RGN_TYPE_WINDOW;
136
137         return (SpaceLink *)sc;
138 }
139
140 /* not spacelink itself */
141 static void clip_free(SpaceLink *sl)
142 {
143         SpaceClip *sc= (SpaceClip*) sl;
144
145         sc->clip= NULL;
146
147         if(sc->scopes.track_preview)
148                 IMB_freeImBuf(sc->scopes.track_preview);
149 }
150
151 /* spacetype; init callback */
152 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
153 {
154
155 }
156
157 static SpaceLink *clip_duplicate(SpaceLink *sl)
158 {
159         SpaceClip *scn= MEM_dupallocN(sl);
160
161         /* clear or remove stuff from old */
162         scn->scopes.track_preview= NULL;
163         scn->scopes.ok= 0;
164
165         return (SpaceLink *)scn;
166 }
167
168 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
169 {
170         /* context changes */
171         switch(wmn->category) {
172                 case NC_SCENE:
173                         switch(wmn->data) {
174                                 case ND_FRAME:
175                                         clip_scopes_tag_refresh(sa);
176                                         /* no break! */
177
178                                 case ND_FRAME_RANGE:
179                                         ED_area_tag_refresh(sa);
180                                         ED_area_tag_redraw(sa);
181                                         break;
182                         }
183                         break;
184                 case NC_MOVIECLIP:
185                         switch(wmn->data) {
186                                 case ND_DISPLAY:
187                                 case ND_SELECT:
188                                         clip_scopes_tag_refresh(sa);
189                                         ED_area_tag_redraw(sa);
190                                         break;
191                         }
192                         switch(wmn->action) {
193                                 case NA_REMOVED:
194                                 case NA_EDITED:
195                                 case NA_EVALUATED:
196                                         clip_stabilization_tag_refresh(sa);
197                                         /* no break! */
198
199                                 case NA_SELECTED:
200                                         clip_scopes_tag_refresh(sa);
201                                         ED_area_tag_redraw(sa);
202                                         break;
203                         }
204                         break;
205                 case NC_GEOM:
206                         switch(wmn->data) {
207                                 case ND_SELECT:
208                                         clip_scopes_tag_refresh(sa);
209                                         ED_area_tag_redraw(sa);
210                                         break;
211                         }
212                         break;
213                  case NC_SCREEN:
214                         if(wmn->data==ND_ANIMPLAY) {
215                                 ED_area_tag_redraw(sa);
216                         }
217                         break;
218                 case NC_SPACE:
219                         if(wmn->data==ND_SPACE_CLIP) {
220                                 clip_scopes_tag_refresh(sa);
221                                 clip_stabilization_tag_refresh(sa);
222                                 ED_area_tag_redraw(sa);
223                         }
224                         break;
225         }
226 }
227
228 static void clip_operatortypes(void)
229 {
230         WM_operatortype_append(CLIP_OT_open);
231         WM_operatortype_append(CLIP_OT_reload);
232         WM_operatortype_append(CLIP_OT_tools);
233          WM_operatortype_append(CLIP_OT_properties);
234         // WM_operatortype_append(CLIP_OT_unlink);
235         WM_operatortype_append(CLIP_OT_view_pan);
236         WM_operatortype_append(CLIP_OT_view_zoom);
237         WM_operatortype_append(CLIP_OT_view_zoom_in);
238         WM_operatortype_append(CLIP_OT_view_zoom_out);
239         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
240         WM_operatortype_append(CLIP_OT_view_all);
241         WM_operatortype_append(CLIP_OT_view_selected);
242
243         WM_operatortype_append(CLIP_OT_select);
244         WM_operatortype_append(CLIP_OT_select_all);
245         WM_operatortype_append(CLIP_OT_select_border);
246         WM_operatortype_append(CLIP_OT_select_circle);
247         WM_operatortype_append(CLIP_OT_select_grouped);
248
249         WM_operatortype_append(CLIP_OT_add_marker);
250         WM_operatortype_append(CLIP_OT_delete_track);
251         WM_operatortype_append(CLIP_OT_delete_marker);
252
253         WM_operatortype_append(CLIP_OT_track_markers);
254         WM_operatortype_append(CLIP_OT_solve_camera);
255         WM_operatortype_append(CLIP_OT_clear_reconstruction);
256
257         WM_operatortype_append(CLIP_OT_disable_markers);
258         WM_operatortype_append(CLIP_OT_hide_tracks);
259         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
260         WM_operatortype_append(CLIP_OT_lock_tracks);
261
262         WM_operatortype_append(CLIP_OT_set_origin);
263         WM_operatortype_append(CLIP_OT_set_floor);
264         WM_operatortype_append(CLIP_OT_set_axis);
265         WM_operatortype_append(CLIP_OT_set_scale);
266
267         WM_operatortype_append(CLIP_OT_set_center_principal);
268
269         WM_operatortype_append(CLIP_OT_clear_track_path);
270         WM_operatortype_append(CLIP_OT_join_tracks);
271         WM_operatortype_append(CLIP_OT_track_copy_color);
272
273         WM_operatortype_append(CLIP_OT_slide_marker);
274
275         WM_operatortype_append(CLIP_OT_frame_jump);
276
277         WM_operatortype_append(CLIP_OT_detect_features);
278
279         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
280         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
281         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
282 }
283
284 static void clip_keymap(struct wmKeyConfig *keyconf)
285 {
286         wmKeyMap *keymap;
287         wmKeyMapItem *kmi;
288
289         /* ******** Global hotkeys avalaible for all regions ******** */
290
291         keymap= WM_keymap_find(keyconf, "Clip Globals", SPACE_CLIP, 0);
292
293         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
294
295         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
296         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
297
298         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
299         RNA_boolean_set(kmi->ptr, "backwards", 1);
300         WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
301         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
302         RNA_boolean_set(kmi->ptr, "sequence", 1);
303         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
304         RNA_boolean_set(kmi->ptr, "backwards", 1);
305         RNA_boolean_set(kmi->ptr, "sequence", 1);
306
307         /* ******** Hotkeys avalaible for main region only ******** */
308
309         keymap= WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
310
311         /* View/navigation */
312
313         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
314         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
315         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
316
317         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
318         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
319         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
320         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
321         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
322         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
323
324         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
325         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
326         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
327         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
328         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
329         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
330         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
331
332         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
333
334         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
335
336         WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
337         RNA_boolean_set(WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
338         WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
339         RNA_enum_set(WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "action", SEL_INVERT);
340         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
341         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
342         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
343
344         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
345
346         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
347         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
348         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
349         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
350         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
351         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
352
353         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
354         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
355
356         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
357         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
358
359         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
360         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
361
362         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
363
364         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
365
366         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
367         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
368         RNA_boolean_set(kmi->ptr, "unselected", 1);
369         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
370
371         kmi= WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, 0, 0);
372         RNA_enum_set(kmi->ptr, "action", 2);
373
374         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
375         RNA_enum_set(kmi->ptr, "action", 0);
376
377         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
378         RNA_enum_set(kmi->ptr, "action", 1);
379
380         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
381         RNA_enum_set(kmi->ptr, "position", 0);
382
383         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
384         RNA_enum_set(kmi->ptr, "position", 1);
385
386         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
387         RNA_enum_set(kmi->ptr, "position", 2);
388
389         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
390         RNA_enum_set(kmi->ptr, "position", 3);
391
392         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
393
394         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
395 }
396
397 const char *clip_context_dir[]= {"edit_movieclip", NULL};
398
399 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
400 {
401         SpaceClip *sc= CTX_wm_space_clip(C);
402
403         if(CTX_data_dir(member)) {
404                 CTX_data_dir_set(result, clip_context_dir);
405                 return 1;
406         }
407         else if(CTX_data_equals(member, "edit_movieclip")) {
408                 CTX_data_id_pointer_set(result, &sc->clip->id);
409                 return 1;
410         }
411
412         return 0;
413 }
414
415 static void clip_refresh(const bContext *C, ScrArea *UNUSED(sa))
416 {
417         SpaceClip *sc= CTX_wm_space_clip(C);
418
419         BKE_movieclip_user_set_frame(&sc->user, CTX_data_scene(C)->r.cfra);
420 }
421
422 /********************* main region ********************/
423
424 /* sets up the fields of the View2D from zoom and offset */
425 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
426 {
427         MovieClip *clip= ED_space_clip(sc);
428         float x1, y1, w, h;
429         int width, height, winx, winy;
430
431         ED_space_clip_size(sc, &width, &height);
432
433         w= width;
434         h= height;
435
436         if(clip)
437                 h*= clip->aspy/clip->aspx;
438
439         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
440         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
441
442         ar->v2d.tot.xmin= 0;
443         ar->v2d.tot.ymin= 0;
444         ar->v2d.tot.xmax= w;
445         ar->v2d.tot.ymax= h;
446
447         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
448         ar->v2d.mask.xmax= winx;
449         ar->v2d.mask.ymax= winy;
450
451         /* which part of the image space do we see? */
452         x1= ar->winrct.xmin+(winx-sc->zoom*w)/2.0f;
453         y1= ar->winrct.ymin+(winy-sc->zoom*h)/2.0f;
454
455         x1-= sc->zoom*sc->xof;
456         y1-= sc->zoom*sc->yof;
457
458         /* relative display right */
459         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sc->zoom);
460         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sc->zoom);
461
462         /* relative display left */
463         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sc->zoom);
464         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sc->zoom);
465
466         /* normalize 0.0..1.0 */
467         ar->v2d.cur.xmin /= w;
468         ar->v2d.cur.xmax /= w;
469         ar->v2d.cur.ymin /= h;
470         ar->v2d.cur.ymax /= h;
471 }
472
473 /* add handlers, stuff you only do once or on area/region changes */
474 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
475 {
476         wmKeyMap *keymap;
477
478         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
479
480         /* own keymap */
481         keymap= WM_keymap_find(wm->defaultconf, "Clip Globals", SPACE_CLIP, 0);
482         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
483
484         keymap= WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
485         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
486 }
487
488 static void clip_main_area_draw(const bContext *C, ARegion *ar)
489 {
490         /* draw entirely, view changes should be handled here */
491         SpaceClip *sc= CTX_wm_space_clip(C);
492         Scene *scene= CTX_data_scene(C);
493         MovieClip *clip= ED_space_clip(sc);
494
495         /* if trcking is in progress, we should sunchronize framenr from clipuser
496            so latest tracked frame would be shown */
497         if(clip && clip->tracking_context)
498                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
499
500         if(sc->flag&SC_LOCK_SELECTION) {
501                 ImBuf *tmpibuf= NULL;
502
503                 if(clip && clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
504                         tmpibuf= ED_space_clip_acquire_stable_buffer(sc, NULL, NULL);
505                 }
506
507                 ED_clip_view_selection(sc, ar, 0);
508
509                 if(tmpibuf)
510                         IMB_freeImBuf(tmpibuf);
511         }
512
513         /* clear and setup matrix */
514         UI_ThemeClearColor(TH_BACK);
515         glClear(GL_COLOR_BUFFER_BIT);
516
517         /* data... */
518         movieclip_main_area_set_view2d(sc, ar);
519         draw_clip_main(sc, ar, scene);
520
521         /* reset view matrix */
522         UI_view2d_view_restore(C);
523 }
524
525 /****************** header region ******************/
526
527 /* add handlers, stuff you only do once or on area/region changes */
528 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
529 {
530         ED_region_header_init(ar);
531 }
532
533 static void clip_header_area_draw(const bContext *C, ARegion *ar)
534 {
535         ED_region_header(C, ar);
536 }
537
538 /****************** tools region ******************/
539
540 /* add handlers, stuff you only do once or on area/region changes */
541 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
542 {
543         ED_region_panels_init(wm, ar);
544 }
545
546 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
547 {
548         ED_region_panels(C, ar, 1, NULL, -1);
549 }
550
551 /****************** properties region ******************/
552
553 /* add handlers, stuff you only do once or on area/region changes */
554 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
555 {
556         wmKeyMap *keymap;
557
558         ED_region_panels_init(wm, ar);
559
560         keymap= WM_keymap_find(wm->defaultconf, "Clip Globals", SPACE_CLIP, 0);
561         WM_event_add_keymap_handler(&ar->handlers, keymap);
562 }
563
564 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
565 {
566         SpaceClip *sc= CTX_wm_space_clip(C);
567
568         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
569
570         ED_region_panels(C, ar, 1, NULL, -1);
571 }
572
573 /********************* registration ********************/
574
575 /* only called once, from space/spacetypes.c */
576 void ED_spacetype_clip(void)
577 {
578         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype clip");
579         ARegionType *art;
580
581         st->spaceid= SPACE_CLIP;
582         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
583
584         st->new= clip_new;
585         st->free= clip_free;
586         st->init= clip_init;
587         st->duplicate= clip_duplicate;
588         st->operatortypes= clip_operatortypes;
589         st->keymap= clip_keymap;
590         st->listener= clip_listener;
591         st->context= clip_context;
592         st->refresh= clip_refresh;
593
594         /* regions: main window */
595         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
596         art->regionid= RGN_TYPE_WINDOW;
597         art->init= clip_main_area_init;
598         art->draw= clip_main_area_draw;
599         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
600
601         BLI_addhead(&st->regiontypes, art);
602
603         /* regions: properties */
604         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
605         art->regionid= RGN_TYPE_UI;
606         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
607         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
608         art->init= clip_properties_area_init;
609         art->draw= clip_properties_area_draw;
610         BLI_addhead(&st->regiontypes, art);
611         ED_clip_buttons_register(art);
612
613         /* regions: tools */
614         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
615         art->regionid= RGN_TYPE_TOOLS;
616         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
617         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
618         art->init= clip_tools_area_init;
619         art->draw= clip_tools_area_draw;
620
621         BLI_addhead(&st->regiontypes, art);
622
623         /* regions: header */
624         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
625         art->regionid= RGN_TYPE_HEADER;
626         art->prefsizey= HEADERY;
627         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
628
629         art->init= clip_header_area_init;
630         art->draw= clip_header_area_draw;
631
632         BLI_addhead(&st->regiontypes, art);
633
634         BKE_spacetype_register(st);
635 }