Fix some inconsistencies in object visibility/selectability tests.
[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 #include "GPU_framebuffer.h"
64
65 /* ******************** default callbacks for info space ***************** */
66
67 static SpaceLink *info_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
68 {
69         ARegion *ar;
70         SpaceInfo *sinfo;
71
72         sinfo = MEM_callocN(sizeof(SpaceInfo), "initinfo");
73         sinfo->spacetype = SPACE_INFO;
74
75         sinfo->rpt_mask = INFO_RPT_OP;
76
77         /* header */
78         ar = MEM_callocN(sizeof(ARegion), "header for info");
79
80         BLI_addtail(&sinfo->regionbase, ar);
81         ar->regiontype = RGN_TYPE_HEADER;
82         ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP;
83
84         /* main region */
85         ar = MEM_callocN(sizeof(ARegion), "main region for info");
86
87         BLI_addtail(&sinfo->regionbase, ar);
88         ar->regiontype = RGN_TYPE_WINDOW;
89
90         /* keep in sync with console */
91         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
92         ar->v2d.align |= V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y; /* align bottom left */
93         ar->v2d.keepofs |= V2D_LOCKOFS_X;
94         ar->v2d.keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
95         ar->v2d.keeptot = V2D_KEEPTOT_BOUNDS;
96         ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
97
98         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
99         //ar->v2d.keepzoom = (V2D_KEEPASPECT|V2D_LIMITZOOM);
100
101         return (SpaceLink *)sinfo;
102 }
103
104 /* not spacelink itself */
105 static void info_free(SpaceLink *UNUSED(sl))
106 {
107 //      SpaceInfo *sinfo = (SpaceInfo *) sl;
108
109 }
110
111
112 /* spacetype; init callback */
113 static void info_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
114 {
115
116 }
117
118 static SpaceLink *info_duplicate(SpaceLink *sl)
119 {
120         SpaceInfo *sinfon = MEM_dupallocN(sl);
121
122         /* clear or remove stuff from old */
123
124         return (SpaceLink *)sinfon;
125 }
126
127
128
129 /* add handlers, stuff you only do once or on area/region changes */
130 static void info_main_region_init(wmWindowManager *wm, ARegion *ar)
131 {
132         wmKeyMap *keymap;
133
134         /* force it on init, for old files, until it becomes config */
135         ar->v2d.scroll = (V2D_SCROLL_RIGHT);
136
137         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
138
139         /* own keymap */
140         keymap = WM_keymap_ensure(wm->defaultconf, "Info", SPACE_INFO, 0);
141         WM_event_add_keymap_handler(&ar->handlers, keymap);
142 }
143
144 static void info_textview_update_rect(const bContext *C, ARegion *ar)
145 {
146         SpaceInfo *sinfo = CTX_wm_space_info(C);
147         View2D *v2d = &ar->v2d;
148
149         UI_view2d_totRect_set(v2d, ar->winx - 1, info_textview_height(sinfo, ar, CTX_wm_reports(C)));
150 }
151
152 static void info_main_region_draw(const bContext *C, ARegion *ar)
153 {
154         /* draw entirely, view changes should be handled here */
155         SpaceInfo *sinfo = CTX_wm_space_info(C);
156         View2D *v2d = &ar->v2d;
157         View2DScrollers *scrollers;
158
159         /* clear and setup matrix */
160         UI_ThemeClearColor(TH_BACK);
161         GPU_clear(GPU_COLOR_BIT);
162
163         /* quick way to avoid drawing if not bug enough */
164         if (ar->winy < 16)
165                 return;
166
167         info_textview_update_rect(C, ar);
168
169         /* worlks best with no view2d matrix set */
170         UI_view2d_view_ortho(v2d);
171
172         info_textview_main(sinfo, ar, CTX_wm_reports(C));
173
174         /* reset view matrix */
175         UI_view2d_view_restore(C);
176
177         /* scrollers */
178         scrollers = UI_view2d_scrollers_calc(C, v2d, NULL, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
179         UI_view2d_scrollers_draw(C, v2d, scrollers);
180         UI_view2d_scrollers_free(scrollers);
181 }
182
183 static void info_operatortypes(void)
184 {
185         WM_operatortype_append(FILE_OT_autopack_toggle);
186         WM_operatortype_append(FILE_OT_pack_all);
187         WM_operatortype_append(FILE_OT_pack_libraries);
188         WM_operatortype_append(FILE_OT_unpack_all);
189         WM_operatortype_append(FILE_OT_unpack_item);
190         WM_operatortype_append(FILE_OT_unpack_libraries);
191
192         WM_operatortype_append(FILE_OT_make_paths_relative);
193         WM_operatortype_append(FILE_OT_make_paths_absolute);
194         WM_operatortype_append(FILE_OT_report_missing_files);
195         WM_operatortype_append(FILE_OT_find_missing_files);
196         WM_operatortype_append(INFO_OT_reports_display_update);
197
198         /* info_report.c */
199         WM_operatortype_append(INFO_OT_select_pick);
200         WM_operatortype_append(INFO_OT_select_all_toggle);
201         WM_operatortype_append(INFO_OT_select_box);
202
203         WM_operatortype_append(INFO_OT_report_replay);
204         WM_operatortype_append(INFO_OT_report_delete);
205         WM_operatortype_append(INFO_OT_report_copy);
206 }
207
208 static void info_keymap(struct wmKeyConfig *keyconf)
209 {
210         WM_keymap_ensure(keyconf, "Window", 0, 0);
211         WM_keymap_ensure(keyconf, "Info", SPACE_INFO, 0);
212 }
213
214 /* add handlers, stuff you only do once or on area/region changes */
215 static void info_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
216 {
217         ED_region_header_init(ar);
218 }
219
220 static void info_header_region_draw(const bContext *C, ARegion *ar)
221 {
222         ED_region_header(C, ar);
223 }
224
225 static void info_main_region_listener(
226         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
227         wmNotifier *wmn, const Scene *UNUSED(scene))
228 {
229         // SpaceInfo *sinfo = sa->spacedata.first;
230
231         /* context changes */
232         switch (wmn->category) {
233                 case NC_SPACE:
234                         if (wmn->data == ND_SPACE_INFO_REPORT) {
235                                 /* redraw also but only for report view, could do less redraws by checking the type */
236                                 ED_region_tag_redraw(ar);
237                         }
238                         break;
239         }
240 }
241
242 static void info_header_listener(
243         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
244         wmNotifier *wmn, const Scene *UNUSED(scene))
245 {
246         /* context changes */
247         switch (wmn->category) {
248                 case NC_SCREEN:
249                         if (ELEM(wmn->data, ND_LAYER, ND_ANIMPLAY)) {
250                                 ED_region_tag_redraw(ar);
251                         }
252                         break;
253                 case NC_WM:
254                         if (wmn->data == ND_JOB)
255                                 ED_region_tag_redraw(ar);
256                         break;
257                 case NC_SCENE:
258                         if (wmn->data == ND_RENDER_RESULT)
259                                 ED_region_tag_redraw(ar);
260                         break;
261                 case NC_SPACE:
262                         if (wmn->data == ND_SPACE_INFO)
263                                 ED_region_tag_redraw(ar);
264                         break;
265                 case NC_ID:
266                         if (wmn->action == NA_RENAME)
267                                 ED_region_tag_redraw(ar);
268                         break;
269         }
270
271 }
272
273 static void info_header_region_message_subscribe(
274         const bContext *UNUSED(C),
275         WorkSpace *UNUSED(workspace), Scene *UNUSED(scene),
276         bScreen *UNUSED(screen), ScrArea *UNUSED(sa), ARegion *ar,
277         struct wmMsgBus *mbus)
278 {
279         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
280                 .owner = ar,
281                 .user_data = ar,
282                 .notify = ED_region_do_msg_notify_tag_redraw,
283         };
284
285         WM_msg_subscribe_rna_anon_prop(mbus, Window, view_layer, &msg_sub_value_region_tag_redraw);
286         WM_msg_subscribe_rna_anon_prop(mbus, ViewLayer, name, &msg_sub_value_region_tag_redraw);
287 }
288
289 /* only called once, from space/spacetypes.c */
290 void ED_spacetype_info(void)
291 {
292         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype info");
293         ARegionType *art;
294
295         st->spaceid = SPACE_INFO;
296         strncpy(st->name, "Info", BKE_ST_MAXNAME);
297
298         st->new = info_new;
299         st->free = info_free;
300         st->init = info_init;
301         st->duplicate = info_duplicate;
302         st->operatortypes = info_operatortypes;
303         st->keymap = info_keymap;
304
305         /* regions: main window */
306         art = MEM_callocN(sizeof(ARegionType), "spacetype info region");
307         art->regionid = RGN_TYPE_WINDOW;
308         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
309
310         art->init = info_main_region_init;
311         art->draw = info_main_region_draw;
312         art->listener = info_main_region_listener;
313
314         BLI_addhead(&st->regiontypes, art);
315
316         /* regions: header */
317         art = MEM_callocN(sizeof(ARegionType), "spacetype info region");
318         art->regionid = RGN_TYPE_HEADER;
319         art->prefsizey = HEADERY;
320
321         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
322         art->listener = info_header_listener;
323         art->message_subscribe = info_header_region_message_subscribe;
324         art->init = info_header_region_init;
325         art->draw = info_header_region_draw;
326
327         BLI_addhead(&st->regiontypes, art);
328
329         BKE_spacetype_register(st);
330 }