Change startup defaults
[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_utildefines.h"
39
40 #include "BLT_translation.h"
41
42 #include "BKE_context.h"
43 #include "BKE_global.h"
44 #include "BKE_screen.h"
45
46 #include "ED_space_api.h"
47 #include "ED_screen.h"
48
49 #include "BIF_gl.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53 #include "WM_message.h"
54
55 #include "RNA_access.h"
56
57 #include "UI_resources.h"
58 #include "UI_interface.h"
59 #include "UI_view2d.h"
60
61 #include "info_intern.h"  /* own include */
62 #include "BLO_readfile.h"
63
64 /* ******************** default callbacks for info space ***************** */
65
66 static SpaceLink *info_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
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_TOP;
82         
83         /* main region */
84         ar = MEM_callocN(sizeof(ARegion), "main region 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_region_init(wmWindowManager *wm, ARegion *ar)
130 {
131         wmKeyMap *keymap;
132
133         /* force it on init, for old files, until it becomes config */
134         ar->v2d.scroll = (V2D_SCROLL_RIGHT);
135         
136         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
137
138         /* own keymap */
139         keymap = WM_keymap_find(wm->defaultconf, "Info", SPACE_INFO, 0);
140         WM_event_add_keymap_handler(&ar->handlers, keymap);
141 }
142
143 static void info_textview_update_rect(const bContext *C, ARegion *ar)
144 {
145         SpaceInfo *sinfo = CTX_wm_space_info(C);
146         View2D *v2d = &ar->v2d;
147
148         UI_view2d_totRect_set(v2d, ar->winx - 1, info_textview_height(sinfo, ar, CTX_wm_reports(C)));
149 }
150
151 static void info_main_region_draw(const bContext *C, ARegion *ar)
152 {
153         /* draw entirely, view changes should be handled here */
154         SpaceInfo *sinfo = CTX_wm_space_info(C);
155         View2D *v2d = &ar->v2d;
156         View2DScrollers *scrollers;
157
158         /* clear and setup matrix */
159         UI_ThemeClearColor(TH_BACK);
160         glClear(GL_COLOR_BUFFER_BIT);
161
162         /* quick way to avoid drawing if not bug enough */
163         if (ar->winy < 16)
164                 return;
165                 
166         info_textview_update_rect(C, ar);
167
168         /* worlks best with no view2d matrix set */
169         UI_view2d_view_ortho(v2d);
170
171         info_textview_main(sinfo, ar, CTX_wm_reports(C));
172
173         /* reset view matrix */
174         UI_view2d_view_restore(C);
175         
176         /* scrollers */
177         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
178         UI_view2d_scrollers_draw(C, v2d, scrollers);
179         UI_view2d_scrollers_free(scrollers);
180 }
181
182 static void info_operatortypes(void)
183 {
184         WM_operatortype_append(FILE_OT_autopack_toggle);
185         WM_operatortype_append(FILE_OT_pack_all);
186         WM_operatortype_append(FILE_OT_pack_libraries);
187         WM_operatortype_append(FILE_OT_unpack_all);
188         WM_operatortype_append(FILE_OT_unpack_item);
189         WM_operatortype_append(FILE_OT_unpack_libraries);
190         
191         WM_operatortype_append(FILE_OT_make_paths_relative);
192         WM_operatortype_append(FILE_OT_make_paths_absolute);
193         WM_operatortype_append(FILE_OT_report_missing_files);
194         WM_operatortype_append(FILE_OT_find_missing_files);
195         WM_operatortype_append(INFO_OT_reports_display_update);
196
197         /* info_report.c */
198         WM_operatortype_append(INFO_OT_select_pick);
199         WM_operatortype_append(INFO_OT_select_all_toggle);
200         WM_operatortype_append(INFO_OT_select_border);
201
202         WM_operatortype_append(INFO_OT_report_replay);
203         WM_operatortype_append(INFO_OT_report_delete);
204         WM_operatortype_append(INFO_OT_report_copy);
205 }
206
207 static void info_keymap(struct wmKeyConfig *keyconf)
208 {
209         wmKeyMap *keymap = WM_keymap_find(keyconf, "Window", 0, 0);
210         
211         WM_keymap_verify_item(keymap, "INFO_OT_reports_display_update", TIMERREPORT, KM_ANY, KM_ANY, 0);
212
213         /* info space */
214         keymap = WM_keymap_find(keyconf, "Info", SPACE_INFO, 0);
215         
216         
217         /* report selection */
218         WM_keymap_add_item(keymap, "INFO_OT_select_pick", SELECTMOUSE, KM_PRESS, 0, 0);
219         WM_keymap_add_item(keymap, "INFO_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
220         WM_keymap_add_item(keymap, "INFO_OT_select_border", BKEY, KM_PRESS, 0, 0);
221
222         WM_keymap_add_item(keymap, "INFO_OT_report_replay", RKEY, KM_PRESS, 0, 0);
223         WM_keymap_add_item(keymap, "INFO_OT_report_delete", XKEY, KM_PRESS, 0, 0);
224         WM_keymap_add_item(keymap, "INFO_OT_report_delete", DELKEY, KM_PRESS, 0, 0);
225         WM_keymap_add_item(keymap, "INFO_OT_report_copy", CKEY, KM_PRESS, KM_CTRL, 0);
226 #ifdef __APPLE__
227         WM_keymap_add_item(keymap, "INFO_OT_report_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
228 #endif
229 }
230
231 /* add handlers, stuff you only do once or on area/region changes */
232 static void info_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
233 {
234         ED_region_header_init(ar);
235 }
236
237 static void info_header_region_draw(const bContext *C, ARegion *ar)
238 {
239         ED_region_header(C, ar);
240 }
241
242 static void info_main_region_listener(
243         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
244         wmNotifier *wmn, const Scene *UNUSED(scene))
245 {
246         // SpaceInfo *sinfo = sa->spacedata.first;
247
248         /* context changes */
249         switch (wmn->category) {
250                 case NC_SPACE:
251                         if (wmn->data == ND_SPACE_INFO_REPORT) {
252                                 /* redraw also but only for report view, could do less redraws by checking the type */
253                                 ED_region_tag_redraw(ar);
254                         }
255                         break;
256         }
257 }
258
259 static void info_header_listener(
260         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
261         wmNotifier *wmn, const Scene *UNUSED(scene))
262 {
263         /* context changes */
264         switch (wmn->category) {
265                 case NC_SCREEN:
266                         if (ELEM(wmn->data, ND_LAYER, ND_SCREENCAST, ND_ANIMPLAY)) {
267                                 ED_region_tag_redraw(ar);
268                         }
269                         break;
270                 case NC_WM:
271                         if (wmn->data == ND_JOB)
272                                 ED_region_tag_redraw(ar);
273                         break;
274                 case NC_SCENE:
275                         if (wmn->data == ND_RENDER_RESULT)
276                                 ED_region_tag_redraw(ar);
277                         break;
278                 case NC_SPACE:
279                         if (wmn->data == ND_SPACE_INFO)
280                                 ED_region_tag_redraw(ar);
281                         break;
282                 case NC_ID:
283                         if (wmn->action == NA_RENAME)
284                                 ED_region_tag_redraw(ar);
285                         break;
286         }
287         
288 }
289
290 static void info_header_region_message_subscribe(
291         const bContext *UNUSED(C),
292         WorkSpace *UNUSED(workspace), Scene *UNUSED(scene),
293         bScreen *UNUSED(screen), ScrArea *UNUSED(sa), ARegion *ar,
294         struct wmMsgBus *mbus)
295 {
296         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
297                 .owner = ar,
298                 .user_data = ar,
299                 .notify = ED_region_do_msg_notify_tag_redraw,
300         };
301
302         WM_msg_subscribe_rna_anon_prop(mbus, Window, view_layer, &msg_sub_value_region_tag_redraw);
303         WM_msg_subscribe_rna_anon_prop(mbus, ViewLayer, name, &msg_sub_value_region_tag_redraw);
304 }
305
306 static void recent_files_menu_draw(const bContext *UNUSED(C), Menu *menu)
307 {
308         struct RecentFile *recent;
309         uiLayout *layout = menu->layout;
310         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
311         if (!BLI_listbase_is_empty(&G.recent_files)) {
312                 for (recent = G.recent_files.first; (recent); recent = recent->next) {
313                         const char *file = BLI_path_basename(recent->filepath);
314                         const int icon = BLO_has_bfile_extension(file) ? ICON_FILE_BLEND : ICON_FILE_BACKUP;
315                         uiItemStringO(layout, file, icon, "WM_OT_open_mainfile", "filepath", recent->filepath);
316                 }
317         }
318         else {
319                 uiItemL(layout, IFACE_("No Recent Files"), ICON_NONE);
320         }
321 }
322
323 static void recent_files_menu_register(void)
324 {
325         MenuType *mt;
326
327         mt = MEM_callocN(sizeof(MenuType), "spacetype info menu recent files");
328         strcpy(mt->idname, "INFO_MT_file_open_recent");
329         strcpy(mt->label, N_("Open Recent..."));
330         strcpy(mt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
331         mt->draw = recent_files_menu_draw;
332         WM_menutype_add(mt);
333 }
334
335 /* only called once, from space/spacetypes.c */
336 void ED_spacetype_info(void)
337 {
338         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype info");
339         ARegionType *art;
340         
341         st->spaceid = SPACE_INFO;
342         strncpy(st->name, "Info", BKE_ST_MAXNAME);
343         
344         st->new = info_new;
345         st->free = info_free;
346         st->init = info_init;
347         st->duplicate = info_duplicate;
348         st->operatortypes = info_operatortypes;
349         st->keymap = info_keymap;
350         
351         /* regions: main window */
352         art = MEM_callocN(sizeof(ARegionType), "spacetype info region");
353         art->regionid = RGN_TYPE_WINDOW;
354         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
355
356         art->init = info_main_region_init;
357         art->draw = info_main_region_draw;
358         art->listener = info_main_region_listener;
359
360         BLI_addhead(&st->regiontypes, art);
361         
362         /* regions: header */
363         art = MEM_callocN(sizeof(ARegionType), "spacetype info region");
364         art->regionid = RGN_TYPE_HEADER;
365         art->prefsizey = HEADERY;
366         
367         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
368         art->listener = info_header_listener;
369         art->message_subscribe = info_header_region_message_subscribe;
370         art->init = info_header_region_init;
371         art->draw = info_header_region_draw;
372         
373         BLI_addhead(&st->regiontypes, art);
374         
375         recent_files_menu_register();
376
377         BKE_spacetype_register(st);
378 }