e20490c0c075dabc2507d1d0d308ccd88cee5b93
[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
48 #include "ED_space_api.h"
49 #include "ED_screen.h"
50
51 #include "BIF_gl.h"
52
53 #include "WM_api.h"
54
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57 #include "UI_view2d.h"
58
59 #include "view3d_intern.h"      // own include
60
61 /* ******************** default callbacks for view3d space ***************** */
62
63 static SpaceLink *view3d_new(void)
64 {
65         ARegion *ar;
66         View3D *vd;
67         
68         vd= MEM_callocN(sizeof(View3D), "initview3d");
69         vd->spacetype= SPACE_VIEW3D;
70         vd->blockscale= 0.7f;
71         vd->lay= vd->layact= 1;
72         if(G.scene) {
73                 vd->lay= vd->layact= G.scene->lay;
74                 vd->camera= G.scene->camera;
75         }
76         vd->scenelock= 1;
77         vd->grid= 1.0f;
78         vd->gridlines= 16;
79         vd->gridsubdiv = 10;
80         vd->drawtype= OB_WIRE;
81         
82         vd->gridflag |= V3D_SHOW_X;
83         vd->gridflag |= V3D_SHOW_Y;
84         vd->gridflag |= V3D_SHOW_FLOOR;
85         vd->gridflag &= ~V3D_SHOW_Z;
86         
87         vd->depths= NULL;
88         
89         /* XXX move view data to region? */
90         vd->viewquat[0]= 1.0f;
91         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
92         vd->persp= 1;
93         vd->view= 7;
94         vd->dist= 10.0;
95         vd->lens= 35.0f;
96         vd->near= 0.01f;
97         vd->far= 500.0f;
98         
99         /* header */
100         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
101         
102         BLI_addtail(&vd->regionbase, ar);
103         ar->regiontype= RGN_TYPE_HEADER;
104         ar->alignment= RGN_ALIGN_BOTTOM;
105         
106         /* main area */
107         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
108         
109         BLI_addtail(&vd->regionbase, ar);
110         ar->regiontype= RGN_TYPE_WINDOW;
111         
112         return (SpaceLink *)vd;
113 }
114
115 /* not spacelink itself */
116 static void view3d_free(SpaceLink *sl)
117 {
118         View3D *vd= (View3D *) sl;
119         
120         if(vd->bgpic) {
121                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
122                 MEM_freeN(vd->bgpic);
123         }
124         
125         if(vd->localvd) MEM_freeN(vd->localvd);
126         if(vd->clipbb) MEM_freeN(vd->clipbb);
127         if(vd->depths) {
128                 if(vd->depths->depths) MEM_freeN(vd->depths->depths);
129                 MEM_freeN(vd->depths);
130                 vd->depths= NULL;
131         }
132         
133 // XXX  retopo_free_view_data(vd);
134         
135         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
136         if(vd->ri) { 
137 // XXX          BIF_view3d_previewrender_free(vd);
138         }
139         
140 }
141
142
143 /* spacetype; init callback */
144 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
145 {
146
147 }
148
149 static SpaceLink *view3d_duplicate(SpaceLink *sl)
150 {
151         View3D *v3do= (View3D *)sl;
152         View3D *v3dn= MEM_dupallocN(sl);
153         
154         /* clear or remove stuff from old */
155         
156 // XXX  BIF_view3d_previewrender_free(v3do);
157         v3do->depths= NULL;
158         v3do->retopo_view_data= NULL;
159         
160         if(v3do->localvd) {
161 // XXX          restore_localviewdata(v3do);
162                 v3do->localvd= NULL;
163                 v3do->properties_storage= NULL;
164                 v3do->localview= 0;
165                 v3do->lay &= 0xFFFFFF;
166         }
167         
168         /* copy or clear inside new stuff */
169
170         if(v3dn->bgpic) {
171                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
172                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
173         }
174         v3dn->clipbb= MEM_dupallocN(v3dn->clipbb);
175         v3dn->ri= NULL;
176         v3dn->properties_storage= NULL;
177         
178         return (SpaceLink *)v3dn;
179 }
180
181 static void view3d_main_area_draw(const bContext *C, ARegion *ar)
182 {
183         /* draw entirely, view changes should be handled here */
184         ScrArea *sa= CTX_wm_area(C);
185         View3D *v3d= sa->spacedata.first;       /* XXX get from region */
186         
187         drawview3dspace(CTX_data_scene(C), ar, v3d);
188 }
189
190 /* add handlers, stuff you only do once or on area/region changes */
191 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
192 {
193         ListBase *keymap;
194         
195         /* own keymap */
196         keymap= WM_keymap_listbase(wm, "View3D", SPACE_VIEW3D, 0);      /* XXX weak? */
197         WM_event_add_keymap_handler_bb(&ar->handlers, keymap,NULL, NULL);
198                                                            
199 }
200
201
202 /* add handlers, stuff you only do once or on area/region changes */
203 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
204 {
205         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
206 }
207
208 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
209 {
210         float col[3];
211         
212         /* clear */
213         if(ED_screen_area_active(C))
214                 UI_GetThemeColor3fv(TH_HEADER, col);
215         else
216                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
217         
218         glClearColor(col[0], col[1], col[2], 0.0);
219         glClear(GL_COLOR_BUFFER_BIT);
220         
221         /* set view2d view matrix for scrolling (without scrollers) */
222         UI_view2d_view_ortho(C, &ar->v2d);
223         
224         view3d_header_buttons(C, ar);
225         
226         /* restore view matrix? */
227         UI_view2d_view_restore(C);
228 }
229
230
231 /* only called once, from space/spacetypes.c */
232 void ED_spacetype_view3d(void)
233 {
234         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype time");
235         ARegionType *art;
236         
237         st->spaceid= SPACE_VIEW3D;
238         
239         st->new= view3d_new;
240         st->free= view3d_free;
241         st->init= view3d_init;
242         st->duplicate= view3d_duplicate;
243         st->operatortypes= view3d_operatortypes;
244         st->keymap= view3d_keymap;
245         
246         /* regions: main window */
247         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
248         art->regionid = RGN_TYPE_WINDOW;
249         art->draw= view3d_main_area_draw;
250         art->init= view3d_main_area_init;
251         
252         BLI_addhead(&st->regiontypes, art);
253         
254         /* regions: header */
255         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
256         art->regionid = RGN_TYPE_HEADER;
257         art->minsizey= HEADERY;
258         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
259         
260         art->init= view3d_header_area_init;
261         art->draw= view3d_header_area_draw;
262         
263         BLI_addhead(&st->regiontypes, art);
264         
265         BKE_spacetype_register(st);
266 }
267