65ef3273e30d016a3f395e9d9c2a6e93f4b0f29e
[blender.git] / source / blender / editors / space_time / space_time.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_time/space_time.c
28  *  \ingroup sptime
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_dlrbTree.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_screen.h"
47 #include "BKE_pointcache.h"
48
49 #include "ED_anim_api.h"
50 #include "ED_keyframes_draw.h"
51 #include "ED_screen.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_glutil.h"
58
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #include "ED_space_api.h"
63 #include "ED_markers.h"
64
65 #include "time_intern.h"
66
67 /* ************************ main time area region *********************** */
68
69 static void time_draw_sfra_efra(Scene *scene, View2D *v2d)
70 {       
71         /* draw darkened area outside of active timeline 
72          * frame range used is preview range or scene range 
73          */
74         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75         glEnable(GL_BLEND);
76                 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
77                 
78                 if (PSFRA < PEFRA) {
79                         glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
80                         glRectf((float)PEFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
81                 }
82                 else {
83                         glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
84                 }
85         glDisable(GL_BLEND);
86
87         UI_ThemeColorShade(TH_BACK, -60);
88         /* thin lines where the actual frames are */
89         fdrawline((float)PSFRA, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
90         fdrawline((float)PEFRA, v2d->cur.ymin, (float)PEFRA, v2d->cur.ymax);
91 }
92
93 #define CACHE_DRAW_HEIGHT       3.0f
94
95 static void time_draw_cache(SpaceTime *stime, Object *ob)
96 {
97         PTCacheID *pid;
98         ListBase pidlist;
99         SpaceTimeCache *stc = stime->caches.first;
100         float yoffs=0.f;
101         
102         if (!(stime->cache_display & TIME_CACHE_DISPLAY) || (!ob))
103                 return;
104
105         BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
106
107         /* iterate over pointcaches on the active object, 
108          * add spacetimecache and vertex array for each */
109         for(pid=pidlist.first; pid; pid=pid->next) {
110                 float col[4], *fp;
111                 int i, sta = pid->cache->startframe, end = pid->cache->endframe;
112                 int len = (end - sta + 1)*4;
113
114                 switch(pid->type) {
115                         case PTCACHE_TYPE_SOFTBODY:
116                                 if (!(stime->cache_display & TIME_CACHE_SOFTBODY)) continue;
117                                 break;
118                         case PTCACHE_TYPE_PARTICLES:
119                                 if (!(stime->cache_display & TIME_CACHE_PARTICLES))     continue;
120                                 break;
121                         case PTCACHE_TYPE_CLOTH:
122                                 if (!(stime->cache_display & TIME_CACHE_CLOTH)) continue;
123                                 break;
124                         case PTCACHE_TYPE_SMOKE_DOMAIN:
125                         case PTCACHE_TYPE_SMOKE_HIGHRES:
126                                 if (!(stime->cache_display & TIME_CACHE_SMOKE)) continue;
127                                 break;
128                         case PTCACHE_TYPE_DYNAMICPAINT:
129                                 if (!(stime->cache_display & TIME_CACHE_DYNAMICPAINT))  continue;
130                                 break;
131                 }
132
133                 if(pid->cache->cached_frames == NULL)
134                         continue;
135
136                 /* make sure we have stc with correct array length */
137                 if(stc == NULL || MEM_allocN_len(stc->array) != len*2*sizeof(float)) {
138                         if(stc) {
139                                 MEM_freeN(stc->array);
140                         }
141                         else {
142                                 stc = MEM_callocN(sizeof(SpaceTimeCache), "spacetimecache");
143                                 BLI_addtail(&stime->caches, stc);
144                         }
145
146                         stc->array = MEM_callocN(len*2*sizeof(float), "SpaceTimeCache array");
147                 }
148
149                 /* fill the vertex array with a quad for each cached frame */
150                 for (i=sta, fp=stc->array; i<=end; i++) {
151                         if (pid->cache->cached_frames[i-sta]) {
152                                 fp[0] = (float)i-0.5f;
153                                 fp[1] = 0.0;
154                                 fp+=2;
155                                 
156                                 fp[0] = (float)i-0.5f;
157                                 fp[1] = 1.0;
158                                 fp+=2;
159                                 
160                                 fp[0] = (float)i+0.5f;
161                                 fp[1] = 1.0;
162                                 fp+=2;
163                                 
164                                 fp[0] = (float)i+0.5f;
165                                 fp[1] = 0.0;
166                                 fp+=2;
167                         }
168                 }
169                 
170                 glPushMatrix();
171                 glTranslatef(0.0, (float)V2D_SCROLL_HEIGHT+yoffs, 0.0);
172                 glScalef(1.0, CACHE_DRAW_HEIGHT, 0.0);
173                 
174                 switch(pid->type) {
175                         case PTCACHE_TYPE_SOFTBODY:
176                                 col[0] = 1.0;   col[1] = 0.4;   col[2] = 0.02;
177                                 col[3] = 0.1;
178                                 break;
179                         case PTCACHE_TYPE_PARTICLES:
180                                 col[0] = 1.0;   col[1] = 0.1;   col[2] = 0.02;
181                                 col[3] = 0.1;
182                                 break;
183                         case PTCACHE_TYPE_CLOTH:
184                                 col[0] = 0.1;   col[1] = 0.1;   col[2] = 0.75;
185                                 col[3] = 0.1;
186                                 break;
187                         case PTCACHE_TYPE_SMOKE_DOMAIN:
188                         case PTCACHE_TYPE_SMOKE_HIGHRES:
189                                 col[0] = 0.2;   col[1] = 0.2;   col[2] = 0.2;
190                                 col[3] = 0.1;
191                                 break;
192                         case PTCACHE_TYPE_DYNAMICPAINT:
193                                 col[0] = 1.0;   col[1] = 0.1;   col[2] = 0.75;
194                                 col[3] = 0.1;
195                                 break;
196                 }
197                 glColor4fv(col);
198                 
199                 glEnable(GL_BLEND);
200                 
201                 glRectf((float)sta, 0.0, (float)end, 1.0);
202                 
203                 col[3] = 0.4f;
204                 if (pid->cache->flag & PTCACHE_BAKED) {
205                         col[0] -= 0.4f; col[1] -= 0.4f; col[2] -= 0.4f;
206                 }
207                 glColor4fv(col);
208                 
209                 glEnableClientState(GL_VERTEX_ARRAY);
210                 glVertexPointer(2, GL_FLOAT, 0, stc->array);
211                 glDrawArrays(GL_QUADS, 0, (fp-stc->array)/2);
212                 glDisableClientState(GL_VERTEX_ARRAY);
213                 
214                 glDisable(GL_BLEND);
215                 
216                 glPopMatrix();
217                 
218                 yoffs += CACHE_DRAW_HEIGHT;
219
220                 stc = stc->next;
221         }
222
223         BLI_freelistN(&pidlist);
224
225         /* free excessive caches */
226         while(stc) {
227                 SpaceTimeCache *tmp = stc->next;
228                 BLI_remlink(&stime->caches, stc);
229                 MEM_freeN(stc->array);
230                 MEM_freeN(stc);
231                 stc = tmp;
232         }
233 }
234
235 static void time_cache_free(SpaceTime *stime)
236 {
237         SpaceTimeCache *stc;
238         
239         for (stc= stime->caches.first; stc; stc=stc->next) {
240                 if (stc->array) {
241                         MEM_freeN(stc->array);
242                         stc->array = NULL;
243                 }
244         }
245         
246         BLI_freelistN(&stime->caches);
247 }
248
249 static void time_cache_refresh(SpaceTime *stime)
250 {
251         /* Free previous caches to indicate full refresh */
252         time_cache_free(stime);
253 }
254
255 /* helper function - find actkeycolumn that occurs on cframe, or the nearest one if not found */
256 static ActKeyColumn *time_cfra_find_ak (ActKeyColumn *ak, float cframe)
257 {
258         ActKeyColumn *akn= NULL;
259         
260         /* sanity checks */
261         if (ak == NULL)
262                 return NULL;
263         
264         /* check if this is a match, or whether it is in some subtree */
265         if (cframe < ak->cfra)
266                 akn= time_cfra_find_ak(ak->left, cframe);
267         else if (cframe > ak->cfra)
268                 akn= time_cfra_find_ak(ak->right, cframe);
269                 
270         /* if no match found (or found match), just use the current one */
271         if (akn == NULL)
272                 return ak;
273         else
274                 return akn;
275 }
276
277 /* helper for time_draw_keyframes() */
278 static void time_draw_idblock_keyframes(View2D *v2d, ID *id, short onlysel)
279 {
280         bDopeSheet ads= {NULL};
281         DLRBT_Tree keys;
282         ActKeyColumn *ak;
283         
284         /* init binarytree-list for getting keyframes */
285         BLI_dlrbTree_init(&keys);
286         
287         /* init dopesheet settings */
288         if (onlysel)
289                 ads.filterflag |= ADS_FILTER_ONLYSEL;
290         
291         /* populate tree with keyframe nodes */
292         switch (GS(id->name)) {
293                 case ID_SCE:
294                         scene_to_keylist(&ads, (Scene *)id, &keys, NULL);
295                         break;
296                 case ID_OB:
297                         ob_to_keylist(&ads, (Object *)id, &keys, NULL);
298                         break;
299         }
300                 
301         /* build linked-list for searching */
302         BLI_dlrbTree_linkedlist_sync(&keys);
303         
304         /* start drawing keyframes 
305          *      - we use the binary-search capabilities of the tree to only start from 
306          *        the first visible keyframe (last one can then be easily checked)
307          *      - draw within a single GL block to be faster
308          */
309         glBegin(GL_LINES);
310                 for ( ak=time_cfra_find_ak(keys.root, v2d->cur.xmin); 
311                          (ak) && (ak->cfra <= v2d->cur.xmax); 
312                           ak=ak->next ) 
313                 {
314                         glVertex2f(ak->cfra, v2d->tot.ymin);
315                         glVertex2f(ak->cfra, v2d->tot.ymax);
316                 }
317         glEnd(); // GL_LINES
318                 
319         /* free temp stuff */
320         BLI_dlrbTree_free(&keys);
321 }
322
323 /* draw keyframe lines for timeline */
324 static void time_draw_keyframes(const bContext *C, SpaceTime *stime, ARegion *ar)
325 {
326         Scene *scene= CTX_data_scene(C);
327         Object *ob= CTX_data_active_object(C);
328         View2D *v2d= &ar->v2d;
329         short onlysel= (stime->flag & TIME_ONLYACTSEL);
330         
331         /* draw scene keyframes first 
332          *      - don't try to do this when only drawing active/selected data keyframes,
333          *        since this can become quite slow
334          */
335         if (scene && onlysel==0) {
336                 /* set draw color */
337                 glColor3ub(0xDD, 0xA7, 0x00);
338                 time_draw_idblock_keyframes(v2d, (ID *)scene, onlysel);
339         }
340         
341         /* draw keyframes from selected objects 
342          *      - only do the active object if in posemode (i.e. showing only keyframes for the bones)
343          *        OR the onlysel flag was set, which means that only active object's keyframes should
344          *        be considered
345          */
346         glColor3ub(0xDD, 0xD7, 0x00);
347         
348         if (ob && ((ob->mode == OB_MODE_POSE) || onlysel)) {
349                 /* draw keyframes for active object only */
350                 time_draw_idblock_keyframes(v2d, (ID *)ob, onlysel);
351         }
352         else {
353                 short active_done = 0;
354                 
355                 /* draw keyframes from all selected objects */
356                 CTX_DATA_BEGIN(C, Object*, obsel, selected_objects) 
357                 {
358                         /* last arg is 0, since onlysel doesn't apply here... */
359                         time_draw_idblock_keyframes(v2d, (ID *)obsel, 0);
360                         
361                         /* if this object is the active one, set flag so that we don't draw again */
362                         if (obsel == ob)
363                                 active_done= 1;
364                 }
365                 CTX_DATA_END;
366                 
367                 /* if active object hasn't been done yet, draw it... */
368                 if (ob && (active_done == 0))
369                         time_draw_idblock_keyframes(v2d, (ID *)ob, 0);
370         }
371 }
372
373 /* ---------------- */
374
375 static void time_refresh(const bContext *UNUSED(C), ScrArea *sa)
376 {
377         /* find the main timeline region and refresh cache display*/
378         ARegion *ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
379         if(ar) {
380                 SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
381                 time_cache_refresh(stime);
382         }
383 }
384
385 /* editor level listener */
386 static void time_listener(ScrArea *sa, wmNotifier *wmn)
387 {
388
389         /* mainly for updating cache display */
390         switch (wmn->category) {
391                 case NC_OBJECT:
392                         switch (wmn->data) {
393                                 case ND_BONE_ACTIVE:
394                                 case ND_POINTCACHE:
395                                 case ND_MODIFIER:
396                                 case ND_PARTICLE:
397                                         ED_area_tag_refresh(sa);
398                                         ED_area_tag_redraw(sa);
399                                         break;
400                         }
401                         break;
402                 case NC_SCENE:
403                         switch (wmn->data) {
404                                 case ND_OB_ACTIVE:
405                                 case ND_FRAME:
406                                         ED_area_tag_refresh(sa);
407                                         break;
408                                 case ND_FRAME_RANGE:
409                                         {
410                                                 ARegion *ar;
411                                                 Scene *scene = wmn->reference;
412
413                                                 for (ar= sa->regionbase.first; ar; ar= ar->next) {
414                                                         if (ar->regiontype==RGN_TYPE_WINDOW) {
415                                                                 ar->v2d.tot.xmin= (float)(SFRA - 4);
416                                                                 ar->v2d.tot.xmax= (float)(EFRA + 4);
417                                                                 break;
418                                                         }
419                                                 }
420                                         }
421                                         break;
422                         }
423                 case NC_SPACE:
424                         switch (wmn->data) {
425                                 case ND_SPACE_CHANGED:
426                                         ED_area_tag_refresh(sa);
427                                         break;
428                         }
429                 case NC_WM:
430                         switch (wmn->data) {
431                                 case ND_FILEREAD:
432                                         ED_area_tag_refresh(sa);
433                                         break;
434                         }
435         }
436 }
437
438 /* ---------------- */
439
440 /* add handlers, stuff you only do once or on area/region changes */
441 static void time_main_area_init(wmWindowManager *wm, ARegion *ar)
442 {
443         wmKeyMap *keymap;
444         
445         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
446         
447         /* own keymap */
448         keymap= WM_keymap_find(wm->defaultconf, "Timeline", SPACE_TIME, 0);
449         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
450 }
451
452 static void time_main_area_draw(const bContext *C, ARegion *ar)
453 {
454         /* draw entirely, view changes should be handled here */
455         Scene *scene= CTX_data_scene(C);
456         SpaceTime *stime= CTX_wm_space_time(C);
457         Object *obact = CTX_data_active_object(C);
458         View2D *v2d= &ar->v2d;
459         View2DGrid *grid;
460         View2DScrollers *scrollers;
461         int unit, flag=0;
462         
463         /* clear and setup matrix */
464         UI_ThemeClearColor(TH_BACK);
465         glClear(GL_COLOR_BUFFER_BIT);
466         
467         UI_view2d_view_ortho(v2d);
468         
469         /* grid */
470         unit= (stime->flag & TIME_DRAWFRAMES)? V2D_UNIT_FRAMES: V2D_UNIT_SECONDS;
471         grid= UI_view2d_grid_calc(scene, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
472         UI_view2d_grid_draw(v2d, grid, (V2D_VERTICAL_LINES|V2D_VERTICAL_AXIS));
473         UI_view2d_grid_free(grid);
474         
475         /* start and end frame */
476         time_draw_sfra_efra(scene, v2d);
477         
478         /* current frame */
479         flag = DRAWCFRA_WIDE; /* this is only really needed on frames where there's a keyframe, but this will do... */
480         if ((stime->flag & TIME_DRAWFRAMES)==0)         flag |= DRAWCFRA_UNIT_SECONDS;
481         if (stime->flag & TIME_CFRA_NUM)                        flag |= DRAWCFRA_SHOW_NUMBOX;
482         ANIM_draw_cfra(C, v2d, flag);
483         
484         UI_view2d_view_ortho(v2d);
485         
486         /* keyframes */
487         time_draw_keyframes(C, stime, ar);
488         
489         /* markers */
490         UI_view2d_view_orthoSpecial(ar, v2d, 1);
491         draw_markers_time(C, 0);
492         
493         /* caches */
494         time_draw_cache(stime, obact);
495         
496         /* reset view matrix */
497         UI_view2d_view_restore(C);
498         
499         /* scrollers */
500         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
501         UI_view2d_scrollers_draw(C, v2d, scrollers);
502         UI_view2d_scrollers_free(scrollers);
503 }
504
505 static void time_main_area_listener(ARegion *ar, wmNotifier *wmn)
506 {
507         /* context changes */
508         switch(wmn->category) {
509                 case NC_SPACE:
510                         if(wmn->data == ND_SPACE_TIME)
511                                 ED_region_tag_redraw(ar);
512                         break;
513
514                 case NC_ANIMATION:
515                         ED_region_tag_redraw(ar);
516                         break;
517                 
518                 case NC_SCENE:
519                         switch (wmn->data) {
520                                 case ND_OB_SELECT:
521                                 case ND_OB_ACTIVE:
522                                 case ND_FRAME:
523                                 case ND_FRAME_RANGE:
524                                 case ND_KEYINGSET:
525                                 case ND_RENDER_OPTIONS:
526                                         ED_region_tag_redraw(ar);
527                                 break;
528                         }
529         }
530 }
531
532 /* ************************ header time area region *********************** */
533
534 /* add handlers, stuff you only do once or on area/region changes */
535 static void time_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
536 {
537         ED_region_header_init(ar);
538 }
539
540 static void time_header_area_draw(const bContext *C, ARegion *ar)
541 {
542         ED_region_header(C, ar);
543 }
544
545 static void time_header_area_listener(ARegion *ar, wmNotifier *wmn)
546 {
547         /* context changes */
548         switch(wmn->category) {
549                 case NC_SCREEN:
550                         if(wmn->data==ND_ANIMPLAY)
551                                 ED_region_tag_redraw(ar);
552                         break;
553
554                 case NC_SCENE:
555                         switch (wmn->data) {
556                                 case ND_OB_SELECT:
557                                 case ND_FRAME:
558                                 case ND_FRAME_RANGE:
559                                 case ND_KEYINGSET:
560                                 case ND_RENDER_OPTIONS:
561                                         ED_region_tag_redraw(ar);
562                                 break;
563                         }
564
565                 case NC_SPACE:
566                         if(wmn->data == ND_SPACE_TIME)
567                                 ED_region_tag_redraw(ar);
568                         break;
569         }
570 }
571
572 /* ******************** default callbacks for time space ***************** */
573
574 static SpaceLink *time_new(const bContext *C)
575 {
576         Scene *scene= CTX_data_scene(C);
577         ARegion *ar;
578         SpaceTime *stime;
579
580         stime= MEM_callocN(sizeof(SpaceTime), "inittime");
581
582         stime->spacetype= SPACE_TIME;
583         stime->flag |= TIME_DRAWFRAMES;
584
585         /* header */
586         ar= MEM_callocN(sizeof(ARegion), "header for time");
587         
588         BLI_addtail(&stime->regionbase, ar);
589         ar->regiontype= RGN_TYPE_HEADER;
590         ar->alignment= RGN_ALIGN_BOTTOM;
591         
592         /* main area */
593         ar= MEM_callocN(sizeof(ARegion), "main area for time");
594         
595         BLI_addtail(&stime->regionbase, ar);
596         ar->regiontype= RGN_TYPE_WINDOW;
597         
598         ar->v2d.tot.xmin= (float)(SFRA - 4);
599         ar->v2d.tot.ymin= 0.0f;
600         ar->v2d.tot.xmax= (float)(EFRA + 4);
601         ar->v2d.tot.ymax= 50.0f;
602         
603         ar->v2d.cur= ar->v2d.tot;
604
605         ar->v2d.min[0]= 1.0f;
606         ar->v2d.min[1]= 50.0f;
607
608         ar->v2d.max[0]= MAXFRAMEF;
609         ar->v2d.max[1]= 50.0; 
610
611         ar->v2d.minzoom= 0.1f;
612         ar->v2d.maxzoom= 10.0;
613
614         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
615         ar->v2d.align |= V2D_ALIGN_NO_NEG_Y;
616         ar->v2d.keepofs |= V2D_LOCKOFS_Y;
617         ar->v2d.keepzoom |= V2D_LOCKZOOM_Y;
618
619
620         return (SpaceLink*)stime;
621 }
622
623 /* not spacelink itself */
624 static void time_free(SpaceLink *sl)
625 {
626         SpaceTime *stime= (SpaceTime *)sl;
627         
628         time_cache_free(stime);
629 }
630 /* spacetype; init callback in ED_area_initialize() */
631 /* init is called to (re)initialize an existing editor (file read, screen changes) */
632 /* validate spacedata, add own area level handlers */
633 static void time_init(wmWindowManager *UNUSED(wm), ScrArea *sa)
634 {
635         SpaceTime *stime= (SpaceTime *)sa->spacedata.first;
636         
637         time_cache_free(stime);
638         
639         /* enable all cache display */
640         stime->cache_display |= TIME_CACHE_DISPLAY;
641         stime->cache_display |= (TIME_CACHE_SOFTBODY|TIME_CACHE_PARTICLES);
642         stime->cache_display |= (TIME_CACHE_CLOTH|TIME_CACHE_SMOKE|TIME_CACHE_DYNAMICPAINT);
643 }
644
645 static SpaceLink *time_duplicate(SpaceLink *sl)
646 {
647         SpaceTime *stime= (SpaceTime *)sl;
648         SpaceTime *stimen= MEM_dupallocN(stime);
649         
650         stimen->caches.first = stimen->caches.last = NULL;
651         
652         return (SpaceLink *)stimen;
653 }
654
655 /* only called once, from space_api/spacetypes.c */
656 /* it defines all callbacks to maintain spaces */
657 void ED_spacetype_time(void)
658 {
659         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype time");
660         ARegionType *art;
661         
662         st->spaceid= SPACE_TIME;
663         strncpy(st->name, "Timeline", BKE_ST_MAXNAME);
664         
665         st->new= time_new;
666         st->free= time_free;
667         st->init= time_init;
668         st->duplicate= time_duplicate;
669         st->operatortypes= time_operatortypes;
670         st->keymap= NULL;
671         st->listener= time_listener;
672         st->refresh= time_refresh;
673         
674         /* regions: main window */
675         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
676         art->regionid = RGN_TYPE_WINDOW;
677         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_MARKERS|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
678         
679         art->init= time_main_area_init;
680         art->draw= time_main_area_draw;
681         art->listener= time_main_area_listener;
682         art->keymap= time_keymap;
683         BLI_addhead(&st->regiontypes, art);
684         
685         /* regions: header */
686         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
687         art->regionid = RGN_TYPE_HEADER;
688         art->prefsizey= HEADERY;
689         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
690         
691         art->init= time_header_area_init;
692         art->draw= time_header_area_draw;
693         art->listener= time_header_area_listener;
694         BLI_addhead(&st->regiontypes, art);
695                 
696         BKE_spacetype_register(st);
697 }
698