- Scrollbars for the console (use View2D functions)
[blender.git] / source / blender / editors / space_console / space_console.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Campbell Barton
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28  
29  #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_space_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44
45 #include "ED_space_api.h"
46 #include "ED_screen.h"
47
48 #include "BIF_gl.h"
49
50
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
60
61 #include "console_intern.h"     // own include
62
63 static void console_update_rect(bContext *C, ARegion *ar)
64 {
65         SpaceConsole *sc= CTX_wm_space_console(C);
66         View2D *v2d= &ar->v2d;
67
68         UI_view2d_totRect_set(v2d, ar->winx-1, console_text_height(sc, ar, CTX_wm_reports(C)));
69 }
70
71 /* ******************** default callbacks for console space ***************** */
72
73 static SpaceLink *console_new(const bContext *C)
74 {
75         ARegion *ar;
76         SpaceConsole *sconsole;
77         
78         sconsole= MEM_callocN(sizeof(SpaceConsole), "initconsole");
79         sconsole->spacetype= SPACE_CONSOLE;
80         
81         sconsole->lheight=      14;
82         sconsole->type=         CONSOLE_TYPE_PYTHON;
83         sconsole->rpt_mask=     CONSOLE_RPT_OP; /* ? - not sure whats a good default here?*/
84         
85         /* header */
86         ar= MEM_callocN(sizeof(ARegion), "header for console");
87         
88         BLI_addtail(&sconsole->regionbase, ar);
89         ar->regiontype= RGN_TYPE_HEADER;
90         ar->alignment= RGN_ALIGN_BOTTOM;
91         
92         
93         /* main area */
94         ar= MEM_callocN(sizeof(ARegion), "main area for text");
95         
96         BLI_addtail(&sconsole->regionbase, ar);
97         ar->regiontype= RGN_TYPE_WINDOW;
98         
99         
100         ar->v2d.scroll |= (V2D_SCROLL_LEFT);
101         ar->v2d.align |= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
102         ar->v2d.keepofs |= V2D_LOCKOFS_X;
103         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
104         ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
105         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
106
107         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
108         //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_KEEPZOOM);
109
110         return (SpaceLink *)sconsole;
111 }
112
113 /* not spacelink itself */
114 static void console_free(SpaceLink *sl)
115 {
116         SpaceConsole *sc= (SpaceConsole*) sl;
117         
118         while(sc->scrollback.first)
119                 console_scrollback_free(sc, sc->scrollback.first);
120         
121         while(sc->history.first)
122                 console_history_free(sc, sc->history.first);
123 }
124
125
126 /* spacetype; init callback */
127 static void console_init(struct wmWindowManager *wm, ScrArea *sa)
128 {
129
130 }
131
132 static SpaceLink *console_duplicate(SpaceLink *sl)
133 {
134         SpaceConsole *sconsolen= MEM_dupallocN(sl);
135         
136         /* clear or remove stuff from old */
137         
138         /* TODO - duplicate?, then we also need to duplicate the py namespace */
139         sconsolen->scrollback.first= sconsolen->scrollback.last= NULL;
140         sconsolen->history.first= sconsolen->history.last= NULL;
141         
142         return (SpaceLink *)sconsolen;
143 }
144
145
146
147 /* add handlers, stuff you only do once or on area/region changes */
148 static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
149 {
150         ListBase *keymap;
151
152         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
153
154         /* own keymap */
155         keymap= WM_keymap_listbase(wm, "Console", SPACE_CONSOLE, 0);    /* XXX weak? */
156         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
157 }
158
159 static void console_main_area_draw(const bContext *C, ARegion *ar)
160 {
161         /* draw entirely, view changes should be handled here */
162         SpaceConsole *sc= CTX_wm_space_console(C);
163         View2D *v2d= &ar->v2d;
164         View2DScrollers *scrollers;
165         //float col[3];
166         
167         /* add helper text, why not? */
168         if(sc->scrollback.first==NULL) {
169                 console_scrollback_add_str(C, " * Python Interactive Console *", 0);
170                 console_scrollback_add_str(C, "Command History:  Up/Down Arrow", 0);
171                 console_scrollback_add_str(C, "Cursor:           Left/Right Home/End", 0);
172                 console_scrollback_add_str(C, "Remove:           Backspace/Delete", 0);
173                 console_scrollback_add_str(C, "Execute:          Enter", 0);
174                 console_scrollback_add_str(C, "Autocomplete:     Ctrl+Enter", 0);
175                 console_scrollback_add_str(C, "Ctrl +/-  Wheel:  Zoom", 0);
176                 console_scrollback_add_str(C, "Builtin Modules: bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.ui", 0);
177         }
178         
179         /* clear and setup matrix */
180         //UI_GetThemeColor3fv(TH_BACK, col);
181         //glClearColor(col[0], col[1], col[2], 0.0);
182         glClearColor(0, 0, 0, 1.0);
183         glClear(GL_COLOR_BUFFER_BIT);
184
185         console_update_rect(C, ar);
186
187         /* worlks best with no view2d matrix set */
188         UI_view2d_view_ortho(C, v2d);
189
190         /* data... */
191
192         console_history_verify(C); /* make sure we have some command line */
193         console_text_main(sc, ar, CTX_wm_reports(C));
194         
195         /* reset view matrix */
196         UI_view2d_view_restore(C);
197         
198         /* scrollers */
199         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
200         UI_view2d_scrollers_draw(C, v2d, scrollers);
201         UI_view2d_scrollers_free(scrollers);
202 }
203
204 void console_operatortypes(void)
205 {
206         WM_operatortype_append(CONSOLE_OT_move);
207         WM_operatortype_append(CONSOLE_OT_delete);
208         WM_operatortype_append(CONSOLE_OT_insert);
209         
210         /* for use by python only */
211         WM_operatortype_append(CONSOLE_OT_history_append); 
212         WM_operatortype_append(CONSOLE_OT_scrollback_append);
213         
214         
215         WM_operatortype_append(CONSOLE_OT_clear); 
216         WM_operatortype_append(CONSOLE_OT_history_cycle);
217         WM_operatortype_append(CONSOLE_OT_zoom);
218 }
219
220 void console_keymap(struct wmWindowManager *wm)
221 {
222         ListBase *keymap= WM_keymap_listbase(wm, "Console", SPACE_CONSOLE, 0);
223         
224         /*
225         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
226         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
227         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
228         
229         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", EKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
230         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", EKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "type", LINE_END);
231         */
232         
233         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
234         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
235         
236         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_zoom", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "delta", 1);
237         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_zoom", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "delta", -1);
238         
239         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_zoom", PADPLUSKEY, KM_PRESS, KM_CTRL, 0)->ptr, "delta", 1);
240         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_zoom", PADMINUS, KM_PRESS, KM_CTRL, 0)->ptr, "delta", -1);
241         
242         
243         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
244         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
245         
246         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", 1);
247         WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0);
248         
249         /*
250         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
251         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
252         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
253         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
254         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
255         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
256
257
258         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
259         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
260         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
261         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
262         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
263         */
264         
265         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
266         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
267
268 #ifndef DISABLE_PYTHON
269         WM_keymap_add_item(keymap, "CONSOLE_OT_exec", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
270         //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
271         WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", RETKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
272 #endif
273
274         RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, 0, 0)->ptr, "text", "    "); /* fake tabs */
275         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_PRESS, KM_ANY, 0); // last!
276 }
277
278 /****************** header region ******************/
279
280 /* add handlers, stuff you only do once or on area/region changes */
281 static void console_header_area_init(wmWindowManager *wm, ARegion *ar)
282 {
283         ED_region_header_init(ar);
284 }
285
286 static void console_header_area_draw(const bContext *C, ARegion *ar)
287 {
288         ED_region_header(C, ar);
289 }
290
291 static void console_main_area_listener(ScrArea *sa, wmNotifier *wmn)
292 {
293         SpaceConsole *sc= sa->spacedata.first;
294
295         /* context changes */
296         switch(wmn->category) {
297                 case NC_CONSOLE:
298                         if(wmn->data == ND_CONSOLE) { /* generic redraw request */
299                                 ED_area_tag_redraw(sa);
300                         }
301                         else if(wmn->data == ND_CONSOLE_REPORT && sc->type==CONSOLE_TYPE_REPORT) {
302                                 /* redraw also but only for report view, could do less redraws by checking the type */
303                                 ED_area_tag_redraw(sa);
304                         }
305                         break;
306         }
307 }
308
309 /* only called once, from space/spacetypes.c */
310 void ED_spacetype_console(void)
311 {
312         SpaceType *sc= MEM_callocN(sizeof(SpaceType), "spacetype console");
313         ARegionType *art;
314         
315         sc->spaceid= SPACE_CONSOLE;
316         
317         sc->new= console_new;
318         sc->free= console_free;
319         sc->init= console_init;
320         sc->duplicate= console_duplicate;
321         sc->operatortypes= console_operatortypes;
322         sc->keymap= console_keymap;
323         sc->listener= console_main_area_listener;
324         
325         /* regions: main window */
326         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
327         art->regionid = RGN_TYPE_WINDOW;
328         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
329
330         art->init= console_main_area_init;
331         art->draw= console_main_area_draw;
332         
333         
334         
335
336         BLI_addhead(&sc->regiontypes, art);
337         
338         /* regions: header */
339         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
340         art->regionid = RGN_TYPE_HEADER;
341         art->minsizey= HEADERY;
342         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
343         
344         art->init= console_header_area_init;
345         art->draw= console_header_area_draw;
346         
347         BLI_addhead(&sc->regiontypes, art);
348         
349         
350         
351         BKE_spacetype_register(sc);
352 }
353
354