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|SC_SHOW_GPENCIL|SC_MANUAL_CALIBRATION;
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         WM_operatortype_append(CLIP_OT_mode_set);
254
255         /* ** clip_toolbar.c ** */
256         WM_operatortype_append(CLIP_OT_tools);
257         WM_operatortype_append(CLIP_OT_properties);
258
259         /* ** tracking_ops.c ** */
260
261         /* navigation */
262         WM_operatortype_append(CLIP_OT_frame_jump);
263
264         /* foorage */
265         WM_operatortype_append(CLIP_OT_set_center_principal);
266
267         /* selection */
268         WM_operatortype_append(CLIP_OT_select);
269         WM_operatortype_append(CLIP_OT_select_all);
270         WM_operatortype_append(CLIP_OT_select_border);
271         WM_operatortype_append(CLIP_OT_select_circle);
272         WM_operatortype_append(CLIP_OT_select_grouped);
273
274         /* markers */
275         WM_operatortype_append(CLIP_OT_add_marker);
276         WM_operatortype_append(CLIP_OT_slide_marker);
277         WM_operatortype_append(CLIP_OT_delete_track);
278         WM_operatortype_append(CLIP_OT_delete_marker);
279
280         /* track */
281         WM_operatortype_append(CLIP_OT_track_markers);
282
283         /* solving */
284         WM_operatortype_append(CLIP_OT_solve_camera);
285         WM_operatortype_append(CLIP_OT_clear_solution);
286
287         WM_operatortype_append(CLIP_OT_disable_markers);
288         WM_operatortype_append(CLIP_OT_hide_tracks);
289         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
290         WM_operatortype_append(CLIP_OT_lock_tracks);
291
292         /* orientation */
293         WM_operatortype_append(CLIP_OT_set_origin);
294         WM_operatortype_append(CLIP_OT_set_floor);
295         WM_operatortype_append(CLIP_OT_set_axis);
296         WM_operatortype_append(CLIP_OT_set_scale);
297
298         /* detect */
299         WM_operatortype_append(CLIP_OT_detect_features);
300
301         /* stabilization */
302         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
303         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
304         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
305
306         /* clean-up */
307         WM_operatortype_append(CLIP_OT_clear_track_path);
308         WM_operatortype_append(CLIP_OT_join_tracks);
309         WM_operatortype_append(CLIP_OT_track_copy_color);
310
311         WM_operatortype_append(CLIP_OT_clean_tracks);
312 }
313
314 static void clip_keymap(struct wmKeyConfig *keyconf)
315 {
316         wmKeyMap *keymap;
317         wmKeyMapItem *kmi;
318
319         /* ******** Global hotkeys avalaible for all regions ******** */
320
321         keymap= WM_keymap_find(keyconf, "Clip Globals", SPACE_CLIP, 0);
322
323         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
324
325         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
326         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
327
328         /* 2d tracking */
329         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
330         RNA_boolean_set(kmi->ptr, "backwards", 1);
331         WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
332         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
333         RNA_boolean_set(kmi->ptr, "sequence", 1);
334         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
335         RNA_boolean_set(kmi->ptr, "backwards", 1);
336         RNA_boolean_set(kmi->ptr, "sequence", 1);
337
338         /* ******** Hotkeys avalaible for main region only ******** */
339
340         keymap= WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
341
342         /* mode */
343         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
344         RNA_enum_set(kmi->ptr, "mode", SC_MODE_RECONSTRUCTION);
345         RNA_boolean_set(kmi->ptr, "toggle", 1);
346
347         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
348         RNA_enum_set(kmi->ptr, "mode", SC_MODE_DISTORTION);
349         RNA_boolean_set(kmi->ptr, "toggle", 1);
350
351         /* ** View/navigation ** */
352
353         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
354         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
355         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
356
357         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
358         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
359         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
360         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
361         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
362         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
363
364         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
365         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
366         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
367         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
368         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
369         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
370         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
371
372         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
373         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
374
375         /* jump to special frame */
376         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
377         RNA_enum_set(kmi->ptr, "position", 0);
378
379         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
380         RNA_enum_set(kmi->ptr, "position", 1);
381
382         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
383         RNA_enum_set(kmi->ptr, "position", 2);
384
385         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
386         RNA_enum_set(kmi->ptr, "position", 3);
387
388         /* "timeline" */
389         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
390
391         /* selection */
392         WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
393         RNA_boolean_set(WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
394         WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
395         RNA_enum_set(WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "action", SEL_INVERT);
396         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
397         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
398         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
399
400         /* marker */
401         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
402
403         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
404         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
405
406         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
407
408         kmi= WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
409         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
410
411         /* tracks */
412         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
413         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
414
415         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
416         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
417
418         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
419         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
420
421         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
422
423         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
424         RNA_boolean_set(kmi->ptr, "unselected", 1);
425
426         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
427
428         /* clean-up */
429         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
430         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
431         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
432         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
433         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
434         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
435
436         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
437
438         /* menus */
439         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
440
441         /* display */
442         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
443         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
444
445         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
446         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
447
448         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
449 }
450
451 const char *clip_context_dir[]= {"edit_movieclip", NULL};
452
453 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
454 {
455         SpaceClip *sc= CTX_wm_space_clip(C);
456
457         if(CTX_data_dir(member)) {
458                 CTX_data_dir_set(result, clip_context_dir);
459                 return 1;
460         }
461         else if(CTX_data_equals(member, "edit_movieclip")) {
462                 CTX_data_id_pointer_set(result, &sc->clip->id);
463                 return 1;
464         }
465
466         return 0;
467 }
468
469 static void clip_refresh(const bContext *C, ScrArea *UNUSED(sa))
470 {
471         SpaceClip *sc= CTX_wm_space_clip(C);
472
473         BKE_movieclip_user_set_frame(&sc->user, CTX_data_scene(C)->r.cfra);
474 }
475
476 /********************* main region ********************/
477
478 /* sets up the fields of the View2D from zoom and offset */
479 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
480 {
481         MovieClip *clip= ED_space_clip(sc);
482         float x1, y1, w, h;
483         int width, height, winx, winy;
484
485         ED_space_clip_size(sc, &width, &height);
486
487         w= width;
488         h= height;
489
490         if(clip)
491                 h*= clip->aspy/clip->aspx/clip->tracking.camera.pixel_aspect;
492
493         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
494         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
495
496         ar->v2d.tot.xmin= 0;
497         ar->v2d.tot.ymin= 0;
498         ar->v2d.tot.xmax= w;
499         ar->v2d.tot.ymax= h;
500
501         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
502         ar->v2d.mask.xmax= winx;
503         ar->v2d.mask.ymax= winy;
504
505         /* which part of the image space do we see? */
506         x1= ar->winrct.xmin+(winx-sc->zoom*w)/2.0f;
507         y1= ar->winrct.ymin+(winy-sc->zoom*h)/2.0f;
508
509         x1-= sc->zoom*sc->xof;
510         y1-= sc->zoom*sc->yof;
511
512         /* relative display right */
513         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sc->zoom);
514         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sc->zoom);
515
516         /* relative display left */
517         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sc->zoom);
518         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sc->zoom);
519
520         /* normalize 0.0..1.0 */
521         ar->v2d.cur.xmin /= w;
522         ar->v2d.cur.xmax /= w;
523         ar->v2d.cur.ymin /= h;
524         ar->v2d.cur.ymax /= h;
525 }
526
527 /* add handlers, stuff you only do once or on area/region changes */
528 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
529 {
530         wmKeyMap *keymap;
531
532         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
533
534         /* own keymap */
535         keymap= WM_keymap_find(wm->defaultconf, "Clip Globals", SPACE_CLIP, 0);
536         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
537
538         keymap= WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
539         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
540 }
541
542 static void clip_main_area_draw(const bContext *C, ARegion *ar)
543 {
544         /* draw entirely, view changes should be handled here */
545         SpaceClip *sc= CTX_wm_space_clip(C);
546         Scene *scene= CTX_data_scene(C);
547         MovieClip *clip= ED_space_clip(sc);
548
549         /* if trcking is in progress, we should sunchronize framenr from clipuser
550            so latest tracked frame would be shown */
551         if(clip && clip->tracking_context)
552                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
553
554         if(sc->flag&SC_LOCK_SELECTION) {
555                 ImBuf *tmpibuf= NULL;
556
557                 if(clip && clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
558                         tmpibuf= ED_space_clip_acquire_stable_buffer(sc, NULL, NULL);
559                 }
560
561                 ED_clip_view_selection(sc, ar, 0);
562
563                 if(tmpibuf)
564                         IMB_freeImBuf(tmpibuf);
565         }
566
567         /* clear and setup matrix */
568         UI_ThemeClearColor(TH_BACK);
569         glClear(GL_COLOR_BUFFER_BIT);
570
571         /* data... */
572         movieclip_main_area_set_view2d(sc, ar);
573
574         draw_clip_main(sc, ar, scene);
575
576         /* Grease Pencil */
577         draw_clip_grease_pencil((bContext *)C, 1);
578
579         /* reset view matrix */
580         UI_view2d_view_restore(C);
581
582         /* draw Grease Pencil - screen space only */
583         draw_clip_grease_pencil((bContext *)C, 0);
584 }
585
586 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
587 {
588         /* context changes */
589         switch(wmn->category) {
590                 case NC_SCREEN:
591                         if (wmn->data==ND_GPENCIL)
592                                 ED_region_tag_redraw(ar);
593                 break;
594         }
595 }
596
597 /****************** header region ******************/
598
599 /* add handlers, stuff you only do once or on area/region changes */
600 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
601 {
602         ED_region_header_init(ar);
603 }
604
605 static void clip_header_area_draw(const bContext *C, ARegion *ar)
606 {
607         ED_region_header(C, ar);
608 }
609
610 /****************** tools region ******************/
611
612 /* add handlers, stuff you only do once or on area/region changes */
613 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
614 {
615         ED_region_panels_init(wm, ar);
616 }
617
618 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
619 {
620         ED_region_panels(C, ar, 1, NULL, -1);
621 }
622
623 /****************** tool properties region ******************/
624
625 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
626 {
627         /* context changes */
628         switch(wmn->category) {
629                 case NC_WM:
630                         if(wmn->data == ND_HISTORY)
631                                 ED_region_tag_redraw(ar);
632                         break;
633                 case NC_SCENE:
634                         if(wmn->data == ND_MODE)
635                                 ED_region_tag_redraw(ar);
636                         break;
637                 case NC_SPACE:
638                         if(wmn->data == ND_SPACE_VIEW3D)
639                                 ED_region_tag_redraw(ar);
640                         break;
641         }
642 }
643
644 /****************** properties region ******************/
645
646 /* add handlers, stuff you only do once or on area/region changes */
647 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
648 {
649         wmKeyMap *keymap;
650
651         ED_region_panels_init(wm, ar);
652
653         keymap= WM_keymap_find(wm->defaultconf, "Clip Globals", SPACE_CLIP, 0);
654         WM_event_add_keymap_handler(&ar->handlers, keymap);
655 }
656
657 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
658 {
659         SpaceClip *sc= CTX_wm_space_clip(C);
660
661         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
662
663         ED_region_panels(C, ar, 1, NULL, -1);
664 }
665
666 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
667 {
668         /* context changes */
669         switch(wmn->category) {
670                 case NC_SCREEN:
671                         if (wmn->data==ND_GPENCIL)
672                                 ED_region_tag_redraw(ar);
673                         break;
674                 case NC_BRUSH:
675                         if(wmn->action==NA_EDITED)
676                                 ED_region_tag_redraw(ar);
677                         break;
678         }
679 }
680
681 /********************* registration ********************/
682
683 /* only called once, from space/spacetypes.c */
684 void ED_spacetype_clip(void)
685 {
686         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype clip");
687         ARegionType *art;
688
689         st->spaceid= SPACE_CLIP;
690         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
691
692         st->new= clip_new;
693         st->free= clip_free;
694         st->init= clip_init;
695         st->duplicate= clip_duplicate;
696         st->operatortypes= clip_operatortypes;
697         st->keymap= clip_keymap;
698         st->listener= clip_listener;
699         st->context= clip_context;
700         st->refresh= clip_refresh;
701
702         /* regions: main window */
703         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
704         art->regionid= RGN_TYPE_WINDOW;
705         art->init= clip_main_area_init;
706         art->draw= clip_main_area_draw;
707         art->listener= clip_main_area_listener;
708         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_GPENCIL;
709
710         BLI_addhead(&st->regiontypes, art);
711
712         /* regions: properties */
713         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
714         art->regionid= RGN_TYPE_UI;
715         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
716         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
717         art->init= clip_properties_area_init;
718         art->draw= clip_properties_area_draw;
719         art->listener= clip_properties_area_listener;
720         BLI_addhead(&st->regiontypes, art);
721         ED_clip_buttons_register(art);
722
723         /* regions: tools */
724         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
725         art->regionid= RGN_TYPE_TOOLS;
726         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
727         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
728         art->init= clip_tools_area_init;
729         art->draw= clip_tools_area_draw;
730
731         BLI_addhead(&st->regiontypes, art);
732
733         /* tool properties */
734         art= MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
735         art->regionid = RGN_TYPE_TOOL_PROPS;
736         art->prefsizex= 0;
737         art->prefsizey= 120;
738         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
739         art->listener= clip_props_area_listener;
740         art->init= clip_tools_area_init;
741         art->draw= clip_tools_area_draw;
742         ED_clip_tool_props_register(art);
743
744         BLI_addhead(&st->regiontypes, art);
745
746         /* regions: header */
747         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
748         art->regionid= RGN_TYPE_HEADER;
749         art->prefsizey= HEADERY;
750         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
751
752         art->init= clip_header_area_init;
753         art->draw= clip_header_area_draw;
754
755         BLI_addhead(&st->regiontypes, art);
756
757         BKE_spacetype_register(st);
758 }