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