2.5
[blender-staging.git] / source / blender / editors / space_outliner / space_outliner.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_color_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_oops_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_vec_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_arithb.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_colortools.h"
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
52 #include "BKE_texture.h"
53 #include "BKE_utildefines.h"
54
55 #include "ED_space_api.h"
56 #include "ED_screen.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "BIF_gl.h"
62 #include "BIF_glutil.h"
63
64 #include "UI_interface.h"
65 #include "UI_text.h"
66 #include "UI_resources.h"
67 #include "UI_view2d.h"
68
69 #include "RNA_access.h"
70 #include "RNA_types.h"
71
72 #include "outliner_intern.h"
73
74 static void outliner_main_area_init(wmWindowManager *wm, ARegion *ar)
75 {
76         ListBase *keymap;
77         
78         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
79         
80         /* own keymap */
81         keymap= WM_keymap_listbase(wm, "Outliner", SPACE_OOPS, 0);      /* XXX weak? */
82         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
83 }
84
85 static void outliner_main_area_draw(const bContext *C, ARegion *ar)
86 {
87         View2D *v2d= &ar->v2d;
88         View2DScrollers *scrollers;
89         float col[3];
90         
91         /* clear */
92         UI_GetThemeColor3fv(TH_BACK, col);
93         glClearColor(col[0], col[1], col[2], 0.0);
94         glClear(GL_COLOR_BUFFER_BIT);
95         
96         draw_outliner(C);
97         
98         /* reset view matrix */
99         UI_view2d_view_restore(C);
100         
101         /* scrollers */
102         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
103         UI_view2d_scrollers_draw(C, v2d, scrollers);
104         UI_view2d_scrollers_free(scrollers);
105 }
106
107
108 static void outliner_main_area_free(ARegion *ar)
109 {
110 }
111
112
113 static void outliner_main_area_listener(ARegion *ar, wmNotifier *wmn)
114 {
115         /* context changes */
116         switch(wmn->category) {
117                 case NC_SCENE:
118                         switch(wmn->data) {
119                                 case ND_OB_ACTIVE:
120                                 case ND_OB_SELECT:
121                                 case ND_MODE:
122                                         ED_region_tag_redraw(ar);
123                                         break;
124                         }
125                         break;
126                 case NC_OBJECT:
127                         switch(wmn->data) {
128                                 case ND_BONE_ACTIVE:
129                                 case ND_BONE_SELECT:
130                                 case ND_TRANSFORM:
131                                         ED_region_tag_redraw(ar);
132                                         break;
133                         }
134         }
135         
136 }
137
138
139 /* ************************ header outliner area region *********************** */
140
141 /* add handlers, stuff you only do once or on area/region changes */
142 static void outliner_header_area_init(wmWindowManager *wm, ARegion *ar)
143 {
144         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
145 }
146
147 static void outliner_header_area_draw(const bContext *C, ARegion *ar)
148 {
149         float col[3];
150         
151         if(ED_screen_area_active(C))
152                 UI_GetThemeColor3fv(TH_HEADER, col);
153         else
154                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
155         
156         glClearColor(col[0], col[1], col[2], 0.0);
157         glClear(GL_COLOR_BUFFER_BIT);
158
159         /* set view2d view matrix for scrolling (without scrollers) */
160         UI_view2d_view_ortho(C, &ar->v2d);
161
162         outliner_header_buttons(C, ar);
163 }
164
165 static void outliner_header_area_free(ARegion *ar)
166 {
167 }
168
169 /* ******************** default callbacks for outliner space ***************** */
170
171 static SpaceLink *outliner_new(const bContext *C)
172 {
173         ARegion *ar;
174         SpaceOops *soutliner;
175
176         soutliner= MEM_callocN(sizeof(SpaceOops), "initoutliner");
177         soutliner->spacetype= SPACE_OOPS;
178         
179         /* header */
180         ar= MEM_callocN(sizeof(ARegion), "header for outliner");
181         
182         BLI_addtail(&soutliner->regionbase, ar);
183         ar->regiontype= RGN_TYPE_HEADER;
184         ar->alignment= RGN_ALIGN_BOTTOM;
185         
186         /* main area */
187         ar= MEM_callocN(sizeof(ARegion), "main area for outliner");
188         
189         BLI_addtail(&soutliner->regionbase, ar);
190         ar->regiontype= RGN_TYPE_WINDOW;
191         
192         ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM_O);
193         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
194         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
195         ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
196         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
197         
198         return (SpaceLink*)soutliner;
199 }
200
201 static void free_oops(Oops *oops)       /* also oops itself */
202 {
203         BLI_freelistN(&oops->link);
204         MEM_freeN(oops);
205 }
206
207 /* not spacelink itself */
208 static void outliner_free(SpaceLink *sl)
209 {
210         SpaceOops *soutliner= (SpaceOops*)sl;
211         Oops *oops;
212
213         if(soutliner->rnapath) {
214                 MEM_freeN(soutliner->rnapath);
215                 soutliner->rnapath= NULL;
216         }
217
218         while( (oops= soutliner->oops.first) ) {
219                 BLI_remlink(&soutliner->oops, oops);
220                 free_oops(oops);
221         }
222         
223         outliner_free_tree(&soutliner->tree);
224         if(soutliner->treestore) {
225                 if(soutliner->treestore->data) MEM_freeN(soutliner->treestore->data);
226                 MEM_freeN(soutliner->treestore);
227         }
228         
229 }
230
231 /* spacetype; init callback */
232 static void outliner_init(wmWindowManager *wm, ScrArea *sa)
233 {
234         
235 }
236
237 static SpaceLink *outliner_duplicate(SpaceLink *sl)
238 {
239         SpaceOops *soutliner= (SpaceOops *)sl;
240         SpaceOops *soutlinern= MEM_dupallocN(soutliner);
241
242         if(soutlinern->rnapath)
243                 soutlinern->rnapath= MEM_dupallocN(soutlinern->rnapath);
244         
245         soutlinern->oops.first= soutlinern->oops.last= NULL;
246         soutlinern->tree.first= soutlinern->tree.last= NULL;
247         soutlinern->treestore= NULL;
248         
249         return (SpaceLink *)soutlinern;
250 }
251
252 /* only called once, from space_api/spacetypes.c */
253 void ED_spacetype_outliner(void)
254 {
255         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype time");
256         ARegionType *art;
257         
258         st->spaceid= SPACE_OOPS;
259         strncpy(st->name, "Outliner", BKE_ST_MAXNAME);
260         
261         st->new= outliner_new;
262         st->free= outliner_free;
263         st->init= outliner_init;
264         st->duplicate= outliner_duplicate;
265         st->operatortypes= outliner_operatortypes;
266         st->keymap= outliner_keymap;
267         
268         /* regions: main window */
269         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
270         art->regionid = RGN_TYPE_WINDOW;
271         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
272         
273         art->init= outliner_main_area_init;
274         art->draw= outliner_main_area_draw;
275         art->free= outliner_main_area_free;
276         art->listener= outliner_main_area_listener;
277         BLI_addhead(&st->regiontypes, art);
278         
279         /* regions: header */
280         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
281         art->regionid = RGN_TYPE_HEADER;
282         art->minsizey= HEADERY;
283         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
284         
285         art->init= outliner_header_area_init;
286         art->draw= outliner_header_area_draw;
287         art->free= outliner_header_area_free;
288         BLI_addhead(&st->regiontypes, art);
289         
290         BKE_spacetype_register(st);
291 }
292