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