1503dd878d3bd85520856b752f22e3ea05b108ff
[blender-staging.git] / source / blender / editors / space_view3d / space_view3d.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 #include "DNA_view3d_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_screen.h"
47 #include "BKE_utildefines.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51
52 #include "BIF_gl.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
60
61 #include "view3d_intern.h"      // own include
62
63 /* ******************** default callbacks for view3d space ***************** */
64
65 static SpaceLink *view3d_new(const bContext *C)
66 {
67         Scene *scene= CTX_data_scene(C);
68         ARegion *ar;
69         View3D *vd;
70         
71         vd= MEM_callocN(sizeof(View3D), "initview3d");
72         vd->spacetype= SPACE_VIEW3D;
73         vd->blockscale= 0.7f;
74         vd->lay= vd->layact= 1;
75         if(scene) {
76                 vd->lay= vd->layact= scene->lay;
77                 vd->camera= scene->camera;
78         }
79         vd->scenelock= 1;
80         vd->grid= 1.0f;
81         vd->gridlines= 16;
82         vd->gridsubdiv = 10;
83         vd->drawtype= OB_WIRE;
84         
85         vd->gridflag |= V3D_SHOW_X;
86         vd->gridflag |= V3D_SHOW_Y;
87         vd->gridflag |= V3D_SHOW_FLOOR;
88         vd->gridflag &= ~V3D_SHOW_Z;
89         
90         vd->depths= NULL;
91         
92         /* XXX move view data to region? */
93         vd->viewquat[0]= 1.0f;
94         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
95         vd->persp= 1;
96         vd->view= 7;
97         vd->dist= 10.0;
98         vd->lens= 35.0f;
99         vd->near= 0.01f;
100         vd->far= 500.0f;
101         
102         /* header */
103         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
104         
105         BLI_addtail(&vd->regionbase, ar);
106         ar->regiontype= RGN_TYPE_HEADER;
107         ar->alignment= RGN_ALIGN_BOTTOM;
108         
109         /* main area */
110         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
111         
112         BLI_addtail(&vd->regionbase, ar);
113         ar->regiontype= RGN_TYPE_WINDOW;
114         
115         return (SpaceLink *)vd;
116 }
117
118 /* not spacelink itself */
119 static void view3d_free(SpaceLink *sl)
120 {
121         View3D *vd= (View3D *) sl;
122         
123         if(vd->bgpic) {
124                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
125                 MEM_freeN(vd->bgpic);
126         }
127         
128         if(vd->localvd) MEM_freeN(vd->localvd);
129         if(vd->clipbb) MEM_freeN(vd->clipbb);
130         if(vd->depths) {
131                 if(vd->depths->depths) MEM_freeN(vd->depths->depths);
132                 MEM_freeN(vd->depths);
133                 vd->depths= NULL;
134         }
135         
136 // XXX  retopo_free_view_data(vd);
137         
138         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
139         if(vd->ri) { 
140 // XXX          BIF_view3d_previewrender_free(vd);
141         }
142         
143 }
144
145
146 /* spacetype; init callback */
147 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
148 {
149
150 }
151
152 static SpaceLink *view3d_duplicate(SpaceLink *sl)
153 {
154         View3D *v3do= (View3D *)sl;
155         View3D *v3dn= MEM_dupallocN(sl);
156         
157         /* clear or remove stuff from old */
158         
159 // XXX  BIF_view3d_previewrender_free(v3do);
160         v3do->depths= NULL;
161         v3do->retopo_view_data= NULL;
162         
163         if(v3do->localvd) {
164 // XXX          restore_localviewdata(v3do);
165                 v3do->localvd= NULL;
166                 v3do->properties_storage= NULL;
167                 v3do->localview= 0;
168                 v3do->lay &= 0xFFFFFF;
169         }
170         
171         /* copy or clear inside new stuff */
172
173         if(v3dn->bgpic) {
174                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
175                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
176         }
177         v3dn->clipbb= MEM_dupallocN(v3dn->clipbb);
178         v3dn->ri= NULL;
179         v3dn->properties_storage= NULL;
180         
181         return (SpaceLink *)v3dn;
182 }
183
184 static void view3d_main_area_draw(const bContext *C, ARegion *ar)
185 {
186         /* draw entirely, view changes should be handled here */
187         ScrArea *sa= CTX_wm_area(C);
188         View3D *v3d= sa->spacedata.first;       /* XXX get from region */
189         
190         drawview3dspace(CTX_data_scene(C), ar, v3d);
191 }
192
193 /* add handlers, stuff you only do once or on area/region changes */
194 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
195 {
196         ListBase *keymap;
197         
198         /* own keymap */
199         keymap= WM_keymap_listbase(wm, "View3D", SPACE_VIEW3D, 0);
200         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
201         /* object ops, modal later... */
202         keymap= WM_keymap_listbase(wm, "View3D Object", SPACE_VIEW3D, 0);
203         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
204         
205 }
206
207 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
208 {
209         /* context changes */
210         switch(wmn->category) {
211                 case NC_SCENE:
212                         switch(wmn->data) {
213                                 case ND_FRAME:
214                                         ED_region_tag_redraw(ar);
215                                         break;
216                         }
217                         break;
218         }
219 }
220
221 /* add handlers, stuff you only do once or on area/region changes */
222 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
223 {
224         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
225 }
226
227 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
228 {
229         float col[3];
230         
231         /* clear */
232         if(ED_screen_area_active(C))
233                 UI_GetThemeColor3fv(TH_HEADER, col);
234         else
235                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
236         
237         glClearColor(col[0], col[1], col[2], 0.0);
238         glClear(GL_COLOR_BUFFER_BIT);
239         
240         /* set view2d view matrix for scrolling (without scrollers) */
241         UI_view2d_view_ortho(C, &ar->v2d);
242         
243         view3d_header_buttons(C, ar);
244         
245         /* restore view matrix? */
246         UI_view2d_view_restore(C);
247 }
248
249 static int view3d_context(const bContext *C, const bContextDataMember *member, bContextDataResult *result)
250 {
251         View3D *v3d= (View3D*)CTX_wm_space_data(C);
252         Scene *scene= CTX_data_scene(C);
253         Base *base;
254
255         if(ELEM(member, CTX_data_selected_objects, CTX_data_selected_bases)) {
256                 for(base=scene->base.first; base; base=base->next) {
257                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
258                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
259                                         if(member == CTX_data_selected_objects)
260                                                 CTX_data_list_add(result, base->object);
261                                         else
262                                                 CTX_data_list_add(result, base);
263                                 }
264                         }
265                 }
266
267                 return 1;
268         }
269         else if(ELEM(member, CTX_data_visible_objects, CTX_data_visible_bases)) {
270                 for(base=scene->base.first; base; base=base->next) {
271                         if(base->lay & v3d->lay) {
272                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
273                                         if(member == CTX_data_selected_objects)
274                                                 CTX_data_list_add(result, base->object);
275                                         else
276                                                 CTX_data_list_add(result, base);
277                                 }
278                         }
279                 }
280                 
281                 return 1;
282         }
283         else if(member == CTX_data_active_base) {
284                 if(scene->basact && (scene->basact->lay & v3d->lay))
285                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
286                                 CTX_data_pointer_set(result, scene->basact);
287
288                 return 1;
289         }
290         else if(member == CTX_data_active_object) {
291                 if(scene->basact && (scene->basact->lay & v3d->lay))
292                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
293                                 CTX_data_pointer_set(result, scene->basact->object);
294
295                 return 1;
296         }
297         else if(member == CTX_data_edit_object) {
298                 CTX_data_pointer_set(result, G.obedit);
299                 return 1;
300         }
301
302         return 0;
303 }
304
305 /* only called once, from space/spacetypes.c */
306 void ED_spacetype_view3d(void)
307 {
308         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
309         ARegionType *art;
310         
311         st->spaceid= SPACE_VIEW3D;
312         
313         st->new= view3d_new;
314         st->free= view3d_free;
315         st->init= view3d_init;
316         st->duplicate= view3d_duplicate;
317         st->operatortypes= view3d_operatortypes;
318         st->keymap= view3d_keymap;
319         st->context= view3d_context;
320         
321         /* regions: main window */
322         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
323         art->regionid = RGN_TYPE_WINDOW;
324         art->draw= view3d_main_area_draw;
325         art->init= view3d_main_area_init;
326         art->listener= view3d_main_area_listener;
327         BLI_addhead(&st->regiontypes, art);
328         
329         /* regions: header */
330         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
331         art->regionid = RGN_TYPE_HEADER;
332         art->minsizey= HEADERY;
333         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
334         
335         art->init= view3d_header_area_init;
336         art->draw= view3d_header_area_draw;
337         
338         BLI_addhead(&st->regiontypes, art);
339         
340         BKE_spacetype_register(st);
341 }
342