Drag and drop 2.5 integration! Finally, slashdot regulars can use
[blender-staging.git] / source / blender / editors / space_sequencer / space_sequencer.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_rand.h"
42
43 #include "BKE_colortools.h"
44 #include "BKE_context.h"
45 #include "BKE_screen.h"
46 #include "BKE_sequencer.h"
47
48 #include "ED_space_api.h"
49 #include "ED_screen.h"
50
51 #include "BIF_gl.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58 #include "UI_view2d.h"
59
60 #include "ED_markers.h"
61
62 #include "sequencer_intern.h"   // own include
63
64 /* ******************** manage regions ********************* */
65
66 ARegion *sequencer_has_buttons_region(ScrArea *sa)
67 {
68         ARegion *ar, *arnew;
69         
70         for(ar= sa->regionbase.first; ar; ar= ar->next)
71                 if(ar->regiontype==RGN_TYPE_UI)
72                         return ar;
73         
74         /* add subdiv level; after header */
75         for(ar= sa->regionbase.first; ar; ar= ar->next)
76                 if(ar->regiontype==RGN_TYPE_HEADER)
77                         break;
78         
79         /* is error! */
80         if(ar==NULL) return NULL;
81         
82         arnew= MEM_callocN(sizeof(ARegion), "buttons for sequencer");
83         
84         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
85         arnew->regiontype= RGN_TYPE_UI;
86         arnew->alignment= RGN_ALIGN_RIGHT;
87         
88         arnew->flag = RGN_FLAG_HIDDEN;
89         
90         return arnew;
91 }
92
93 ARegion *sequencer_find_region(ScrArea *sa, short type)
94 {
95         ARegion *ar=NULL;
96         
97         for(ar= sa->regionbase.first; ar; ar= ar->next)
98                 if(ar->regiontype==type)
99                         return ar;
100
101         return ar;
102 }
103
104 void ED_sequencer_update_view(bContext *C, int view)
105 {
106         ScrArea *sa= CTX_wm_area(C);
107         
108         ARegion *ar_main= sequencer_find_region(sa, RGN_TYPE_WINDOW);
109         ARegion *ar_preview= sequencer_find_region(sa, RGN_TYPE_PREVIEW);
110
111         switch (view) {
112                 case SEQ_VIEW_SEQUENCE:
113                         if (ar_main->flag & RGN_FLAG_HIDDEN) {
114                                 ar_main->flag &= ~RGN_FLAG_HIDDEN;
115                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
116                         }
117                         if (!(ar_preview->flag & RGN_FLAG_HIDDEN)) {
118                                 ar_preview->flag |= RGN_FLAG_HIDDEN;
119                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
120                                 WM_event_remove_handlers(C, &ar_preview->handlers);
121                         }
122                         ar_main->alignment= RGN_ALIGN_NONE;
123                         ar_preview->alignment= RGN_ALIGN_NONE;
124                         break;
125                 case SEQ_VIEW_PREVIEW:
126                         if (!(ar_main->flag & RGN_FLAG_HIDDEN)) {
127                                 ar_main->flag |= RGN_FLAG_HIDDEN;
128                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
129                                 WM_event_remove_handlers(C, &ar_main->handlers);
130                         }
131                         if (ar_preview->flag & RGN_FLAG_HIDDEN) {
132                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
133                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
134                         }
135                         ar_main->alignment= RGN_ALIGN_NONE;
136                         ar_preview->alignment= RGN_ALIGN_NONE;
137                         break;
138                 case SEQ_VIEW_SEQUENCE_PREVIEW:
139                         if (ar_main->flag & RGN_FLAG_HIDDEN) {
140                                 ar_main->flag &= ~RGN_FLAG_HIDDEN;
141                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
142                         }
143                         if (ar_preview->flag & RGN_FLAG_HIDDEN) {
144                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
145                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
146                         }
147                         ar_main->alignment= RGN_ALIGN_NONE;
148                         ar_preview->alignment= RGN_ALIGN_TOP;
149                         break;
150         }
151
152         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
153         ED_area_tag_redraw(sa);
154 }
155
156
157 /* ******************** default callbacks for sequencer space ***************** */
158
159 static SpaceLink *sequencer_new(const bContext *C)
160 {
161         Scene *scene= CTX_data_scene(C);
162         ARegion *ar;
163         SpaceSeq *sseq;
164         
165         sseq= MEM_callocN(sizeof(SpaceSeq), "initsequencer");
166         sseq->spacetype= SPACE_SEQ;
167         sseq->zoom= 4;
168         sseq->chanshown = 0;
169         sseq->view = SEQ_VIEW_SEQUENCE;
170         sseq->mainb = SEQ_DRAW_IMG_IMBUF;
171         /* header */
172         ar= MEM_callocN(sizeof(ARegion), "header for sequencer");
173         
174         BLI_addtail(&sseq->regionbase, ar);
175         ar->regiontype= RGN_TYPE_HEADER;
176         ar->alignment= RGN_ALIGN_BOTTOM;
177         
178         /* buttons/list view */
179         ar= MEM_callocN(sizeof(ARegion), "buttons for sequencer");
180         
181         BLI_addtail(&sseq->regionbase, ar);
182         ar->regiontype= RGN_TYPE_UI;
183         ar->alignment= RGN_ALIGN_RIGHT;
184         ar->flag = RGN_FLAG_HIDDEN;
185         
186         /* preview area */
187         ar= MEM_callocN(sizeof(ARegion), "preview area for sequencer");
188         BLI_addtail(&sseq->regionbase, ar);
189         ar->regiontype= RGN_TYPE_PREVIEW;
190         ar->alignment= RGN_ALIGN_TOP;
191         ar->flag |= RGN_FLAG_HIDDEN;
192
193         /* main area */
194         ar= MEM_callocN(sizeof(ARegion), "main area for sequencer");
195         
196         BLI_addtail(&sseq->regionbase, ar);
197         ar->regiontype= RGN_TYPE_WINDOW;
198         
199         
200         /* seq space goes from (0,8) to (0, efra) */
201         
202         ar->v2d.tot.xmin= 0.0f;
203         ar->v2d.tot.ymin= 0.0f;
204         ar->v2d.tot.xmax= scene->r.efra;
205         ar->v2d.tot.ymax= 8.0f;
206         
207         ar->v2d.cur= ar->v2d.tot;
208         
209         ar->v2d.min[0]= 10.0f;
210         ar->v2d.min[1]= 4.0f;
211         
212         ar->v2d.max[0]= MAXFRAMEF;
213         ar->v2d.max[1]= MAXSEQ;
214         
215         ar->v2d.minzoom= 0.01f;
216         ar->v2d.maxzoom= 100.0f;
217         
218         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
219         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
220         ar->v2d.keepzoom= 0;
221         ar->v2d.keeptot= 0;
222         ar->v2d.align= V2D_ALIGN_NO_NEG_Y;
223
224         return (SpaceLink *)sseq;
225 }
226
227 /* not spacelink itself */
228 static void sequencer_free(SpaceLink *sl)
229 {       
230 //      SpaceSeq *sseq= (SpaceSequencer*) sl;
231         
232 // XXX  if(sseq->gpd) free_gpencil_data(sseq->gpd);
233
234 }
235
236
237 /* spacetype; init callback */
238 static void sequencer_init(struct wmWindowManager *wm, ScrArea *sa)
239 {
240         
241 }
242
243 static SpaceLink *sequencer_duplicate(SpaceLink *sl)
244 {
245         SpaceSeq *sseqn= MEM_dupallocN(sl);
246         
247         /* clear or remove stuff from old */
248 // XXX  sseq->gpd= gpencil_data_duplicate(sseq->gpd);
249
250         return (SpaceLink *)sseqn;
251 }
252
253
254
255 /* *********************** sequencer (main) region ************************ */
256 /* add handlers, stuff you only do once or on area/region changes */
257 static void sequencer_main_area_init(wmWindowManager *wm, ARegion *ar)
258 {
259         wmKeyMap *keymap;
260         ListBase *lb;
261         
262         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
263         
264         keymap= WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
265         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
266         
267         /* own keymap */
268         keymap= WM_keymap_find(wm->defaultconf, "Sequencer", SPACE_SEQ, 0);
269         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
270         
271         /* add drop boxes */
272         lb= WM_dropboxmap_find("Sequencer", SPACE_SEQ, RGN_TYPE_WINDOW);
273         
274         WM_event_add_dropbox_handler(&ar->handlers, lb);
275         
276 }
277
278 static void sequencer_main_area_draw(const bContext *C, ARegion *ar)
279 {
280 //      ScrArea *sa= CTX_wm_area(C);
281         
282         /* NLE - strip editing timeline interface */
283         draw_timeline_seq(C, ar);
284 }
285
286 /* ************* dropboxes ************* */
287
288 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
289 {
290         if(drag->type==WM_DRAG_PATH)
291                 if(ELEM(drag->icon, ICON_FILE_IMAGE, ICON_FILE_BLANK))  /* rule might not work? */
292                         return 1;
293         return 0;
294 }
295
296 static int movie_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
297 {
298         if(drag->type==WM_DRAG_PATH)
299                 if(ELEM(drag->icon, ICON_FILE_MOVIE, ICON_FILE_BLANK))  /* rule might not work? */
300                         return 1;
301         return 0;
302 }
303
304 static void sequencer_drop_copy(wmDrag *drag, wmDropBox *drop)
305 {
306         /* copy drag path to properties */
307         RNA_string_set(drop->ptr, "path", drag->path);
308 }
309
310 /* this region dropbox definition */
311 static void sequencer_dropboxes(void)
312 {
313         ListBase *lb= WM_dropboxmap_find("Sequencer", SPACE_SEQ, RGN_TYPE_WINDOW);
314         
315         WM_dropbox_add(lb, "SEQUENCER_OT_image_strip_add", image_drop_poll, sequencer_drop_copy);
316         WM_dropbox_add(lb, "SEQUENCER_OT_movie_strip_add", movie_drop_poll, sequencer_drop_copy);
317 }
318
319 /* ************* end drop *********** */
320
321 /* add handlers, stuff you only do once or on area/region changes */
322 static void sequencer_header_area_init(wmWindowManager *wm, ARegion *ar)
323 {
324         ED_region_header_init(ar);
325 }
326
327 static void sequencer_header_area_draw(const bContext *C, ARegion *ar)
328 {
329         ED_region_header(C, ar);
330 }
331
332 static void sequencer_main_area_listener(ARegion *ar, wmNotifier *wmn)
333 {
334         /* context changes */
335         switch(wmn->category) {
336                 case NC_SCENE:
337                         switch(wmn->data) {
338                                 case ND_FRAME:
339                                 case ND_MARKERS:
340                                 case ND_SEQUENCER:
341                                 case ND_SEQUENCER_SELECT:
342                                         ED_region_tag_redraw(ar);
343                                         break;
344                         }
345                         break;
346                 case NC_SPACE:
347                         if(wmn->data == ND_SPACE_SEQUENCER)
348                                 ED_region_tag_redraw(ar);
349                         break;
350                 case NC_ID:
351                         if(wmn->action == NA_RENAME)
352                                 ED_region_tag_redraw(ar);
353                         break;
354         }
355 }
356
357 /* *********************** preview region ************************ */
358 static void sequencer_preview_area_init(wmWindowManager *wm, ARegion *ar)
359 {
360         wmKeyMap *keymap;
361         
362         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
363         
364         keymap= WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
365         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
366
367         /* own keymap */
368         keymap= WM_keymap_find(wm->defaultconf, "SequencerPreview", SPACE_SEQ, 0);
369         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
370 }
371
372 static void sequencer_preview_area_draw(const bContext *C, ARegion *ar)
373 {
374         ScrArea *sa= CTX_wm_area(C);
375         SpaceSeq *sseq= sa->spacedata.first;
376         Scene *scene= CTX_data_scene(C);
377         
378         /* XXX temp fix for wrong setting in sseq->mainb */
379         if (sseq->mainb == SEQ_DRAW_SEQUENCE) sseq->mainb = SEQ_DRAW_IMG_IMBUF;
380         draw_image_seq(scene, ar, sseq);
381 }
382
383 static void sequencer_preview_area_listener(ARegion *ar, wmNotifier *wmn)
384 {
385         /* context changes */
386         switch(wmn->category) {
387                 case NC_SCENE:
388                         switch(wmn->data) {
389                                 case ND_FRAME:
390                                 case ND_MARKERS:
391                                 case ND_SEQUENCER:
392                                 case ND_SEQUENCER_SELECT:
393                                         ED_region_tag_redraw(ar);
394                                         break;
395                         }
396                         break;
397                 case NC_SPACE:
398                         if(wmn->data == ND_SPACE_SEQUENCER)
399                                 ED_region_tag_redraw(ar);
400                         break;
401                 case NC_ID:
402                         switch(wmn->data) {
403                                 case NA_RENAME:
404                                         ED_region_tag_redraw(ar);
405                                         break;
406                         }
407                         break;
408         }
409 }
410
411 /* *********************** buttons region ************************ */
412
413 /* add handlers, stuff you only do once or on area/region changes */
414 static void sequencer_buttons_area_init(wmWindowManager *wm, ARegion *ar)
415 {
416         
417         ED_region_panels_init(wm, ar);
418         
419 }
420
421 static void sequencer_buttons_area_draw(const bContext *C, ARegion *ar)
422 {
423         ED_region_panels(C, ar, 1, NULL, -1);
424 }
425
426 static void sequencer_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
427 {
428         /* context changes */
429         switch(wmn->category) {
430                 case NC_SCENE:
431                 switch(wmn->data) {
432                         case ND_FRAME:
433                         case ND_SEQUENCER:
434                         case ND_SEQUENCER_SELECT:
435                                 ED_region_tag_redraw(ar);
436                                 break;
437                 }
438                 break;
439                 case NC_SPACE:
440                         if(wmn->data == ND_SPACE_SEQUENCER)
441                                 ED_region_tag_redraw(ar);
442                         break;
443                 case NC_ID:
444                         if(wmn->action == NA_RENAME)
445                                 ED_region_tag_redraw(ar);
446                         break;
447         }
448 }
449 /* ************************************* */
450
451 /* only called once, from space/spacetypes.c */
452 void ED_spacetype_sequencer(void)
453 {
454         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype sequencer");
455         ARegionType *art;
456         
457         st->spaceid= SPACE_SEQ;
458         strncpy(st->name, "Sequencer", BKE_ST_MAXNAME);
459         
460         st->new= sequencer_new;
461         st->free= sequencer_free;
462         st->init= sequencer_init;
463         st->duplicate= sequencer_duplicate;
464         st->operatortypes= sequencer_operatortypes;
465         st->keymap= sequencer_keymap;
466         st->dropboxes= sequencer_dropboxes;
467
468         /* regions: main window */
469         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
470         art->regionid = RGN_TYPE_WINDOW;
471         art->init= sequencer_main_area_init;
472         art->draw= sequencer_main_area_draw;
473         art->listener= sequencer_main_area_listener;
474         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_ANIMATION;
475
476         BLI_addhead(&st->regiontypes, art);
477
478         /* preview */
479         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
480         art->regionid = RGN_TYPE_PREVIEW;
481         art->prefsizey = 240; // XXX
482         art->init= sequencer_preview_area_init;
483         art->draw= sequencer_preview_area_draw;
484         art->listener= sequencer_preview_area_listener;
485         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_ANIMATION;
486         BLI_addhead(&st->regiontypes, art);
487         
488         /* regions: listview/buttons */
489         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
490         art->regionid = RGN_TYPE_UI;
491         art->prefsizex= 220; // XXX
492         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
493         art->listener= sequencer_buttons_area_listener;
494         art->init= sequencer_buttons_area_init;
495         art->draw= sequencer_buttons_area_draw;
496         BLI_addhead(&st->regiontypes, art);
497         
498         /* Keep as python only for now
499         sequencer_buttons_register(art);
500         */
501
502         /* regions: header */
503         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
504         art->regionid = RGN_TYPE_HEADER;
505         art->prefsizey= HEADERY;
506         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
507         
508         art->init= sequencer_header_area_init;
509         art->draw= sequencer_header_area_draw;
510         art->listener= sequencer_main_area_listener;
511         
512         BLI_addhead(&st->regiontypes, art);
513         
514         BKE_spacetype_register(st);
515 }
516