doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / space_info / space_info.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36 #include "BLI_rand.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_context.h"
40 #include "BKE_global.h"
41 #include "BKE_screen.h"
42
43 #include "ED_space_api.h"
44 #include "ED_screen.h"
45
46 #include "BIF_gl.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "UI_resources.h"
52 #include "UI_interface.h"
53 #include "UI_view2d.h"
54
55 #include "info_intern.h"        // own include
56
57 /* ******************** default callbacks for info space ***************** */
58
59 static SpaceLink *info_new(const bContext *UNUSED(C))
60 {
61         ARegion *ar;
62         SpaceInfo *sinfo;
63         
64         sinfo= MEM_callocN(sizeof(SpaceInfo), "initinfo");
65         sinfo->spacetype= SPACE_INFO;
66
67         sinfo->rpt_mask= INFO_RPT_OP;
68
69         /* header */
70         ar= MEM_callocN(sizeof(ARegion), "header for info");
71         
72         BLI_addtail(&sinfo->regionbase, ar);
73         ar->regiontype= RGN_TYPE_HEADER;
74         ar->alignment= RGN_ALIGN_BOTTOM;
75         
76         /* main area */
77         ar= MEM_callocN(sizeof(ARegion), "main area for info");
78         
79         BLI_addtail(&sinfo->regionbase, ar);
80         ar->regiontype= RGN_TYPE_WINDOW;
81         
82         /* keep in sync with console */
83         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
84         ar->v2d.align |= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
85         ar->v2d.keepofs |= V2D_LOCKOFS_X;
86         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
87         ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
88         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
89
90         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
91         //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
92         
93         return (SpaceLink *)sinfo;
94 }
95
96 /* not spacelink itself */
97 static void info_free(SpaceLink *UNUSED(sl))
98 {       
99 //      SpaceInfo *sinfo= (SpaceInfo*) sl;
100         
101 }
102
103
104 /* spacetype; init callback */
105 static void info_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
106 {
107
108 }
109
110 static SpaceLink *info_duplicate(SpaceLink *sl)
111 {
112         SpaceInfo *sinfon= MEM_dupallocN(sl);
113         
114         /* clear or remove stuff from old */
115         
116         return (SpaceLink *)sinfon;
117 }
118
119
120
121 /* add handlers, stuff you only do once or on area/region changes */
122 static void info_main_area_init(wmWindowManager *wm, ARegion *ar)
123 {
124         wmKeyMap *keymap;
125
126         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
127
128         /* own keymap */
129         keymap= WM_keymap_find(wm->defaultconf, "Info", SPACE_INFO, 0);
130         WM_event_add_keymap_handler(&ar->handlers, keymap);
131 }
132
133 static void info_textview_update_rect(const bContext *C, ARegion *ar)
134 {
135         SpaceInfo *sinfo= CTX_wm_space_info(C);
136         View2D *v2d= &ar->v2d;
137
138         UI_view2d_totRect_set(v2d, ar->winx-1, info_textview_height(sinfo, ar, CTX_wm_reports(C)));
139 }
140
141 static void info_main_area_draw(const bContext *C, ARegion *ar)
142 {
143         /* draw entirely, view changes should be handled here */
144         SpaceInfo *sinfo= CTX_wm_space_info(C);
145         View2D *v2d= &ar->v2d;
146         View2DScrollers *scrollers;
147
148         /* clear and setup matrix */
149         UI_ThemeClearColor(TH_BACK);
150         glClear(GL_COLOR_BUFFER_BIT);
151
152         /* quick way to avoid drawing if not bug enough */
153         if(ar->winy < 16)
154                 return;
155                 
156         info_textview_update_rect(C, ar);
157
158         /* worlks best with no view2d matrix set */
159         UI_view2d_view_ortho(v2d);
160
161         info_textview_main(sinfo, ar, CTX_wm_reports(C));
162
163         /* reset view matrix */
164         UI_view2d_view_restore(C);
165         
166         /* scrollers */
167         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
168         UI_view2d_scrollers_draw(C, v2d, scrollers);
169         UI_view2d_scrollers_free(scrollers);
170 }
171
172 static void info_operatortypes(void)
173 {
174         WM_operatortype_append(FILE_OT_pack_all);
175         WM_operatortype_append(FILE_OT_unpack_all);
176         WM_operatortype_append(FILE_OT_make_paths_relative);
177         WM_operatortype_append(FILE_OT_make_paths_absolute);
178         WM_operatortype_append(FILE_OT_report_missing_files);
179         WM_operatortype_append(FILE_OT_find_missing_files);
180         WM_operatortype_append(INFO_OT_reports_display_update);
181
182         /* info_report.c */
183         WM_operatortype_append(INFO_OT_select_pick);
184         WM_operatortype_append(INFO_OT_select_all_toggle);
185         WM_operatortype_append(INFO_OT_select_border);
186
187         WM_operatortype_append(INFO_OT_report_replay);
188         WM_operatortype_append(INFO_OT_report_delete);
189         WM_operatortype_append(INFO_OT_report_copy);
190 }
191
192 static void info_keymap(struct wmKeyConfig *keyconf)
193 {
194         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
195         
196         WM_keymap_verify_item(keymap, "INFO_OT_reports_display_update", TIMER, KM_ANY, KM_ANY, 0);
197
198         /* info space */
199         keymap= WM_keymap_find(keyconf, "Info", SPACE_INFO, 0);
200         
201         
202         /* report selection */
203         WM_keymap_add_item(keymap, "INFO_OT_select_pick", SELECTMOUSE, KM_PRESS, 0, 0);
204         WM_keymap_add_item(keymap, "INFO_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
205         WM_keymap_add_item(keymap, "INFO_OT_select_border", BKEY, KM_PRESS, 0, 0);
206
207         WM_keymap_add_item(keymap, "INFO_OT_report_replay", RKEY, KM_PRESS, 0, 0);
208         WM_keymap_add_item(keymap, "INFO_OT_report_delete", XKEY, KM_PRESS, 0, 0);
209         WM_keymap_add_item(keymap, "INFO_OT_report_delete", DELKEY, KM_PRESS, 0, 0);
210         WM_keymap_add_item(keymap, "INFO_OT_report_copy", CKEY, KM_PRESS, KM_CTRL, 0);
211 }
212
213 /* add handlers, stuff you only do once or on area/region changes */
214 static void info_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
215 {
216         ED_region_header_init(ar);
217 }
218
219 static void info_header_area_draw(const bContext *C, ARegion *ar)
220 {
221         ED_region_header(C, ar);
222 }
223
224 static void info_main_area_listener(ARegion *ar, wmNotifier *wmn)
225 {
226         // SpaceInfo *sinfo= sa->spacedata.first;
227
228         /* context changes */
229         switch(wmn->category) {
230                 case NC_SPACE:
231                         if(wmn->data == ND_SPACE_INFO_REPORT) {
232                                 /* redraw also but only for report view, could do less redraws by checking the type */
233                                 ED_region_tag_redraw(ar);
234                         }
235                         break;
236         }
237 }
238
239 static void info_header_listener(ARegion *ar, wmNotifier *wmn)
240 {
241         /* context changes */
242         switch(wmn->category) {
243                 case NC_SCREEN:
244                         if(ELEM(wmn->data, ND_SCREENCAST, ND_ANIMPLAY))
245                                 ED_region_tag_redraw(ar);
246                         break;
247                 case NC_WM:     
248                         if(wmn->data == ND_JOB)
249                                 ED_region_tag_redraw(ar);
250                         break;
251                 case NC_SCENE:
252                         if(wmn->data==ND_RENDER_RESULT)
253                                 ED_region_tag_redraw(ar);
254                         break;
255                 case NC_SPACE:  
256                         if(wmn->data == ND_SPACE_INFO)
257                                 ED_region_tag_redraw(ar);
258                         break;
259                 case NC_ID:
260                         if(wmn->action == NA_RENAME)
261                                 ED_region_tag_redraw(ar);
262         }
263         
264 }
265
266 static void recent_files_menu_draw(const bContext *UNUSED(C), Menu *menu)
267 {
268         struct RecentFile *recent;
269         uiLayout *layout= menu->layout;
270         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
271         if (G.recent_files.first) {
272                 for(recent = G.recent_files.first; (recent); recent = recent->next) {
273                         uiItemStringO(layout, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
274                 }
275         } else {
276                 uiItemL(layout, "No Recent Files", ICON_NULL);
277         }
278 }
279
280 static void recent_files_menu_register(void)
281 {
282         MenuType *mt;
283
284         mt= MEM_callocN(sizeof(MenuType), "spacetype info menu recent files");
285         strcpy(mt->idname, "INFO_MT_file_open_recent");
286         strcpy(mt->label, "Open Recent...");
287         mt->draw= recent_files_menu_draw;
288         WM_menutype_add(mt);
289 }
290
291 /* only called once, from space/spacetypes.c */
292 void ED_spacetype_info(void)
293 {
294         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype info");
295         ARegionType *art;
296         
297         st->spaceid= SPACE_INFO;
298         strncpy(st->name, "Info", BKE_ST_MAXNAME);
299         
300         st->new= info_new;
301         st->free= info_free;
302         st->init= info_init;
303         st->duplicate= info_duplicate;
304         st->operatortypes= info_operatortypes;
305         st->keymap= info_keymap;
306         
307         /* regions: main window */
308         art= MEM_callocN(sizeof(ARegionType), "spacetype info region");
309         art->regionid = RGN_TYPE_WINDOW;
310         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
311
312         art->init= info_main_area_init;
313         art->draw= info_main_area_draw;
314         art->listener= info_main_area_listener;
315
316         BLI_addhead(&st->regiontypes, art);
317         
318         /* regions: header */
319         art= MEM_callocN(sizeof(ARegionType), "spacetype info region");
320         art->regionid = RGN_TYPE_HEADER;
321         art->prefsizey= HEADERY;
322         
323         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
324         art->listener= info_header_listener;
325         art->init= info_header_area_init;
326         art->draw= info_header_area_draw;
327         
328         BLI_addhead(&st->regiontypes, art);
329         
330         recent_files_menu_register();
331
332         BKE_spacetype_register(st);
333 }