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