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