View2D: Presets for Initialising Views
[blender.git] / source / blender / editors / space_image / space_image.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_image_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_space_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_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_colortools.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 #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 "image_intern.h"       // own include
63
64 /* ******************** default callbacks for image space ***************** */
65
66 static SpaceLink *image_new(void)
67 {
68         ARegion *ar;
69         SpaceImage *simage;
70         
71         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
72         simage->spacetype= SPACE_IMAGE;
73         simage->zoom= 1;
74         
75         simage->iuser.ok= 1;
76         simage->iuser.fie_ima= 2;
77         simage->iuser.frames= 100;
78         
79         
80         /* header */
81         ar= MEM_callocN(sizeof(ARegion), "header for image");
82         
83         BLI_addtail(&simage->regionbase, ar);
84         ar->regiontype= RGN_TYPE_HEADER;
85         ar->alignment= RGN_ALIGN_BOTTOM;
86         
87         /* main area */
88         ar= MEM_callocN(sizeof(ARegion), "main area for image");
89         
90         BLI_addtail(&simage->regionbase, ar);
91         ar->regiontype= RGN_TYPE_WINDOW;
92         
93         /* channel list region XXX */
94
95         
96         return (SpaceLink *)simage;
97 }
98
99 /* not spacelink itself */
100 static void image_free(SpaceLink *sl)
101 {       
102         SpaceImage *simage= (SpaceImage*) sl;
103         
104         if(simage->cumap)
105                 curvemapping_free(simage->cumap);
106 //      if(simage->gpd)
107 // XXX          free_gpencil_data(simage->gpd);
108         
109 }
110
111
112 /* spacetype; init callback */
113 static void image_init(struct wmWindowManager *wm, ScrArea *sa)
114 {
115
116 }
117
118 static SpaceLink *image_duplicate(SpaceLink *sl)
119 {
120         SpaceImage *simagen= MEM_dupallocN(sl);
121         
122         /* clear or remove stuff from old */
123         
124         return (SpaceLink *)simagen;
125 }
126
127
128
129 /* add handlers, stuff you only do once or on area/region changes */
130 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
131 {
132         ListBase *keymap;
133         
134         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_VIEWCANVAS, ar->winx, ar->winy);
135         
136         /* own keymap */
137         keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);        /* XXX weak? */
138         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
139 }
140
141 static void image_main_area_draw(const bContext *C, ARegion *ar)
142 {
143         /* draw entirely, view changes should be handled here */
144         // SpaceImage *simage= C->area->spacedata.first;
145         View2D *v2d= &ar->v2d;
146         float col[3];
147         
148         /* clear and setup matrix */
149         UI_GetThemeColor3fv(TH_BACK, col);
150         glClearColor(col[0], col[1], col[2], 0.0);
151         glClear(GL_COLOR_BUFFER_BIT);
152         
153         UI_view2d_view_ortho(C, v2d);
154                 
155         /* data... */
156         
157         
158         /* reset view matrix */
159         UI_view2d_view_restore(C);
160         
161         /* scrollers? */
162 }
163
164 void image_operatortypes(void)
165 {
166         
167 }
168
169 void image_keymap(struct wmWindowManager *wm)
170 {
171         
172 }
173
174 /* add handlers, stuff you only do once or on area/region changes */
175 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
176 {
177         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
178 }
179
180 static void image_header_area_draw(const bContext *C, ARegion *ar)
181 {
182         float col[3];
183         
184         /* clear */
185         if(ED_screen_area_active(C))
186                 UI_GetThemeColor3fv(TH_HEADER, col);
187         else
188                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
189         
190         glClearColor(col[0], col[1], col[2], 0.0);
191         glClear(GL_COLOR_BUFFER_BIT);
192         
193         /* set view2d view matrix for scrolling (without scrollers) */
194         UI_view2d_view_ortho(C, &ar->v2d);
195         
196         image_header_buttons(C, ar);
197         
198         /* restore view matrix? */
199         UI_view2d_view_restore(C);
200 }
201
202 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
203 {
204         /* context changes */
205 }
206
207 /* only called once, from space/spacetypes.c */
208 void ED_spacetype_image(void)
209 {
210         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
211         ARegionType *art;
212         
213         st->spaceid= SPACE_IMAGE;
214         
215         st->new= image_new;
216         st->free= image_free;
217         st->init= image_init;
218         st->duplicate= image_duplicate;
219         st->operatortypes= image_operatortypes;
220         st->keymap= image_keymap;
221         
222         /* regions: main window */
223         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
224         art->regionid = RGN_TYPE_WINDOW;
225         art->init= image_main_area_init;
226         art->draw= image_main_area_draw;
227         art->listener= image_main_area_listener;
228         art->keymapflag= ED_KEYMAP_VIEW2D;
229
230         BLI_addhead(&st->regiontypes, art);
231         
232         /* regions: header */
233         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
234         art->regionid = RGN_TYPE_HEADER;
235         art->minsizey= HEADERY;
236         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
237         
238         art->init= image_header_area_init;
239         art->draw= image_header_area_draw;
240         
241         BLI_addhead(&st->regiontypes, art);
242         
243         /* regions: channels */
244         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
245         art->regionid = RGN_TYPE_CHANNELS;
246         art->minsizex= 80;
247         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
248         
249 //      art->init= image_channel_area_init;
250 //      art->draw= image_channel_area_draw;
251         
252         BLI_addhead(&st->regiontypes, art);
253         
254         
255         BKE_spacetype_register(st);
256 }
257