4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Campbell Barton
26 * ***** END GPL LICENSE BLOCK *****
32 #include "DNA_space_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
36 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
45 #include "ED_space_api.h"
46 #include "ED_screen.h"
51 #include "RNA_access.h"
52 #include "RNA_define.h"
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
61 #include "console_intern.h" // own include
63 static void console_update_rect(const bContext *C, ARegion *ar)
65 SpaceConsole *sc= CTX_wm_space_console(C);
66 View2D *v2d= &ar->v2d;
68 UI_view2d_totRect_set(v2d, ar->winx-1, console_text_height(sc, ar, CTX_wm_reports(C)));
71 /* ******************** default callbacks for console space ***************** */
73 static SpaceLink *console_new(const bContext *C)
76 SpaceConsole *sconsole;
78 sconsole= MEM_callocN(sizeof(SpaceConsole), "initconsole");
79 sconsole->spacetype= SPACE_CONSOLE;
81 sconsole->lheight= 14;
82 sconsole->type= CONSOLE_TYPE_PYTHON;
83 sconsole->rpt_mask= CONSOLE_RPT_OP; /* ? - not sure whats a good default here?*/
86 ar= MEM_callocN(sizeof(ARegion), "header for console");
88 BLI_addtail(&sconsole->regionbase, ar);
89 ar->regiontype= RGN_TYPE_HEADER;
90 ar->alignment= RGN_ALIGN_BOTTOM;
94 ar= MEM_callocN(sizeof(ARegion), "main area for text");
96 BLI_addtail(&sconsole->regionbase, ar);
97 ar->regiontype= RGN_TYPE_WINDOW;
100 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
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_LIMITZOOM|V2D_KEEPASPECT);
104 ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
105 ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
107 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
108 //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
110 return (SpaceLink *)sconsole;
113 /* not spacelink itself */
114 static void console_free(SpaceLink *sl)
116 SpaceConsole *sc= (SpaceConsole*) sl;
118 while(sc->scrollback.first)
119 console_scrollback_free(sc, sc->scrollback.first);
121 while(sc->history.first)
122 console_history_free(sc, sc->history.first);
126 /* spacetype; init callback */
127 static void console_init(struct wmWindowManager *wm, ScrArea *sa)
132 static SpaceLink *console_duplicate(SpaceLink *sl)
134 SpaceConsole *sconsolen= MEM_dupallocN(sl);
136 /* clear or remove stuff from old */
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;
142 return (SpaceLink *)sconsolen;
147 /* add handlers, stuff you only do once or on area/region changes */
148 static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
152 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
155 keymap= WM_keymap_find(wm->defaultconf, "Console", SPACE_CONSOLE, 0);
156 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
159 static void console_main_area_draw(const bContext *C, ARegion *ar)
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;
167 if((sc->type==CONSOLE_TYPE_PYTHON) && (sc->scrollback.first==NULL))
168 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
170 /* clear and setup matrix */
171 //UI_GetThemeColor3fv(TH_BACK, col);
172 //glClearColor(col[0], col[1], col[2], 0.0);
173 glClearColor(0, 0, 0, 1.0);
174 glClear(GL_COLOR_BUFFER_BIT);
176 console_update_rect(C, ar);
178 /* worlks best with no view2d matrix set */
179 UI_view2d_view_ortho(C, v2d);
183 console_history_verify(C); /* make sure we have some command line */
184 console_text_main(sc, ar, CTX_wm_reports(C));
186 /* reset view matrix */
187 UI_view2d_view_restore(C);
190 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
191 UI_view2d_scrollers_draw(C, v2d, scrollers);
192 UI_view2d_scrollers_free(scrollers);
195 void console_operatortypes(void)
198 WM_operatortype_append(CONSOLE_OT_move);
199 WM_operatortype_append(CONSOLE_OT_delete);
200 WM_operatortype_append(CONSOLE_OT_insert);
202 /* for use by python only */
203 WM_operatortype_append(CONSOLE_OT_history_append);
204 WM_operatortype_append(CONSOLE_OT_scrollback_append);
206 WM_operatortype_append(CONSOLE_OT_clear);
207 WM_operatortype_append(CONSOLE_OT_history_cycle);
208 WM_operatortype_append(CONSOLE_OT_copy);
209 WM_operatortype_append(CONSOLE_OT_paste);
210 WM_operatortype_append(CONSOLE_OT_select_set);
212 /* console_report.c */
213 WM_operatortype_append(CONSOLE_OT_select_pick);
214 WM_operatortype_append(CONSOLE_OT_select_all_toggle);
215 WM_operatortype_append(CONSOLE_OT_select_border);
217 WM_operatortype_append(CONSOLE_OT_report_replay);
218 WM_operatortype_append(CONSOLE_OT_report_delete);
219 WM_operatortype_append(CONSOLE_OT_report_copy);
222 void console_keymap(struct wmKeyConfig *keyconf)
224 wmKeyMap *keymap= WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
228 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
229 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
232 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
233 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
235 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
236 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
238 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
239 RNA_string_set(kmi->ptr, "path", "space_data.font_size");
240 RNA_boolean_set(kmi->ptr, "reverse", 0);
242 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
243 RNA_string_set(kmi->ptr, "path", "space_data.font_size");
244 RNA_boolean_set(kmi->ptr, "reverse", 1);
246 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
247 RNA_string_set(kmi->ptr, "path", "space_data.font_size");
248 RNA_boolean_set(kmi->ptr, "reverse", 0);
250 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
251 RNA_string_set(kmi->ptr, "path", "space_data.font_size");
252 RNA_boolean_set(kmi->ptr, "reverse", 1);
254 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
255 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
257 RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", 1);
258 WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0);
261 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
262 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
263 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
264 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
265 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
266 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
269 //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
270 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
271 //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
272 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
273 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
276 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
277 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
279 #ifndef DISABLE_PYTHON
280 WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
281 WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
283 //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
284 WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
287 /* report selection */
288 WM_keymap_add_item(keymap, "CONSOLE_OT_select_pick", SELECTMOUSE, KM_PRESS, 0, 0);
289 WM_keymap_add_item(keymap, "CONSOLE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
290 WM_keymap_add_item(keymap, "CONSOLE_OT_select_border", BKEY, KM_PRESS, 0, 0);
292 WM_keymap_add_item(keymap, "CONSOLE_OT_report_replay", RKEY, KM_PRESS, 0, 0);
293 WM_keymap_add_item(keymap, "CONSOLE_OT_report_delete", XKEY, KM_PRESS, 0, 0);
294 WM_keymap_add_item(keymap, "CONSOLE_OT_report_delete", DELKEY, KM_PRESS, 0, 0);
295 WM_keymap_add_item(keymap, "CONSOLE_OT_report_copy", CKEY, KM_PRESS, KM_CTRL, 0);
297 WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
298 WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
299 WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
301 RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, 0, 0)->ptr, "text", " "); /* fake tabs */
302 WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
305 /****************** header region ******************/
307 /* add handlers, stuff you only do once or on area/region changes */
308 static void console_header_area_init(wmWindowManager *wm, ARegion *ar)
310 ED_region_header_init(ar);
313 static void console_header_area_draw(const bContext *C, ARegion *ar)
315 ED_region_header(C, ar);
318 static void console_main_area_listener(ScrArea *sa, wmNotifier *wmn)
320 SpaceConsole *sc= sa->spacedata.first;
322 /* context changes */
323 switch(wmn->category) {
325 if(wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
326 ED_area_tag_redraw(sa);
328 else if(wmn->data == ND_SPACE_CONSOLE_REPORT && sc->type==CONSOLE_TYPE_REPORT) {
329 /* redraw also but only for report view, could do less redraws by checking the type */
330 ED_area_tag_redraw(sa);
336 /* only called once, from space/spacetypes.c */
337 void ED_spacetype_console(void)
339 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype console");
342 st->spaceid= SPACE_CONSOLE;
343 strncpy(st->name, "Console", BKE_ST_MAXNAME);
345 st->new= console_new;
346 st->free= console_free;
347 st->init= console_init;
348 st->duplicate= console_duplicate;
349 st->operatortypes= console_operatortypes;
350 st->keymap= console_keymap;
351 st->listener= console_main_area_listener;
353 /* regions: main window */
354 art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
355 art->regionid = RGN_TYPE_WINDOW;
356 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
358 art->init= console_main_area_init;
359 art->draw= console_main_area_draw;
364 BLI_addhead(&st->regiontypes, art);
366 /* regions: header */
367 art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
368 art->regionid = RGN_TYPE_HEADER;
369 art->prefsizey= HEADERY;
370 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
372 art->init= console_header_area_init;
373 art->draw= console_header_area_draw;
375 BLI_addhead(&st->regiontypes, art);
378 BKE_spacetype_register(st);