2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Campbell Barton
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/editors/space_console/space_console.c
31 # include "BLI_winstuff.h"
34 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
40 #include "BKE_context.h"
41 #include "BKE_screen.h"
42 #include "BKE_idcode.h"
44 #include "ED_space_api.h"
45 #include "ED_screen.h"
49 #include "RNA_access.h"
54 #include "UI_resources.h"
55 #include "UI_view2d.h"
57 #include "console_intern.h" // own include
59 /* ******************** default callbacks for console space ***************** */
61 static SpaceLink *console_new(const bContext *UNUSED(C))
64 SpaceConsole *sconsole;
66 sconsole = MEM_callocN(sizeof(SpaceConsole), "initconsole");
67 sconsole->spacetype = SPACE_CONSOLE;
69 sconsole->lheight = 14;
72 ar = MEM_callocN(sizeof(ARegion), "header for console");
74 BLI_addtail(&sconsole->regionbase, ar);
75 ar->regiontype = RGN_TYPE_HEADER;
76 ar->alignment = RGN_ALIGN_BOTTOM;
80 ar = MEM_callocN(sizeof(ARegion), "main area for text");
82 BLI_addtail(&sconsole->regionbase, ar);
83 ar->regiontype = RGN_TYPE_WINDOW;
85 /* keep in sync with info */
86 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
87 ar->v2d.align |= V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y; /* align bottom left */
88 ar->v2d.keepofs |= V2D_LOCKOFS_X;
89 ar->v2d.keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
90 ar->v2d.keeptot = V2D_KEEPTOT_BOUNDS;
91 ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
93 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
94 //ar->v2d.keepzoom = (V2D_KEEPASPECT|V2D_LIMITZOOM);
96 return (SpaceLink *)sconsole;
99 /* not spacelink itself */
100 static void console_free(SpaceLink *sl)
102 SpaceConsole *sc = (SpaceConsole *) sl;
104 while (sc->scrollback.first)
105 console_scrollback_free(sc, sc->scrollback.first);
107 while (sc->history.first)
108 console_history_free(sc, sc->history.first);
112 /* spacetype; init callback */
113 static void console_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
118 static SpaceLink *console_duplicate(SpaceLink *sl)
120 SpaceConsole *sconsolen = MEM_dupallocN(sl);
122 /* clear or remove stuff from old */
124 /* TODO - duplicate?, then we also need to duplicate the py namespace */
125 sconsolen->scrollback.first = sconsolen->scrollback.last = NULL;
126 sconsolen->history.first = sconsolen->history.last = NULL;
128 return (SpaceLink *)sconsolen;
133 /* add handlers, stuff you only do once or on area/region changes */
134 static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
139 const float prev_y_min = ar->v2d.cur.ymin; /* so re-sizing keeps the cursor visible */
141 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
143 /* always keep the bottom part of the view aligned, less annoying */
144 if (prev_y_min != ar->v2d.cur.ymin) {
145 const float cur_y_range = BLI_rctf_size_y(&ar->v2d.cur);
146 ar->v2d.cur.ymin = prev_y_min;
147 ar->v2d.cur.ymax = prev_y_min + cur_y_range;
151 keymap = WM_keymap_find(wm->defaultconf, "Console", SPACE_CONSOLE, 0);
152 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
155 lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
157 WM_event_add_dropbox_handler(&ar->handlers, lb);
161 /* ************* dropboxes ************* */
163 static int id_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
165 // SpaceConsole *sc = CTX_wm_space_console(C);
166 if (drag->type == WM_DRAG_ID)
171 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
176 /* copy drag path to properties */
177 text = RNA_path_full_ID_py(id);
178 RNA_string_set(drop->ptr, "text", text);
182 static int path_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
184 // SpaceConsole *sc = CTX_wm_space_console(C);
185 if (drag->type == WM_DRAG_PATH)
190 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
192 char pathname[FILE_MAX + 2];
193 BLI_snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
194 RNA_string_set(drop->ptr, "text", pathname);
198 /* this region dropbox definition */
199 static void console_dropboxes(void)
201 ListBase *lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
203 WM_dropbox_add(lb, "CONSOLE_OT_insert", id_drop_poll, id_drop_copy);
204 WM_dropbox_add(lb, "CONSOLE_OT_insert", path_drop_poll, path_drop_copy);
207 /* ************* end drop *********** */
209 static void console_main_area_draw(const bContext *C, ARegion *ar)
211 /* draw entirely, view changes should be handled here */
212 SpaceConsole *sc = CTX_wm_space_console(C);
213 View2D *v2d = &ar->v2d;
214 View2DScrollers *scrollers;
216 if (sc->scrollback.first == NULL)
217 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
219 /* clear and setup matrix */
220 UI_ThemeClearColor(TH_BACK);
221 glClear(GL_COLOR_BUFFER_BIT);
223 /* worlks best with no view2d matrix set */
224 UI_view2d_view_ortho(v2d);
228 console_history_verify(C); /* make sure we have some command line */
229 console_textview_main(sc, ar);
231 /* reset view matrix */
232 UI_view2d_view_restore(C);
235 scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
236 UI_view2d_scrollers_draw(C, v2d, scrollers);
237 UI_view2d_scrollers_free(scrollers);
240 static void console_operatortypes(void)
243 WM_operatortype_append(CONSOLE_OT_move);
244 WM_operatortype_append(CONSOLE_OT_delete);
245 WM_operatortype_append(CONSOLE_OT_insert);
247 WM_operatortype_append(CONSOLE_OT_indent);
248 WM_operatortype_append(CONSOLE_OT_unindent);
250 /* for use by python only */
251 WM_operatortype_append(CONSOLE_OT_history_append);
252 WM_operatortype_append(CONSOLE_OT_scrollback_append);
254 WM_operatortype_append(CONSOLE_OT_clear);
255 WM_operatortype_append(CONSOLE_OT_clear_line);
256 WM_operatortype_append(CONSOLE_OT_history_cycle);
257 WM_operatortype_append(CONSOLE_OT_copy);
258 WM_operatortype_append(CONSOLE_OT_paste);
259 WM_operatortype_append(CONSOLE_OT_select_set);
262 static void console_keymap(struct wmKeyConfig *keyconf)
264 wmKeyMap *keymap = WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
268 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
269 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
272 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
273 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
275 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
276 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
278 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
279 RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
280 RNA_boolean_set(kmi->ptr, "reverse", FALSE);
282 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
283 RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
284 RNA_boolean_set(kmi->ptr, "reverse", TRUE);
286 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
287 RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
288 RNA_boolean_set(kmi->ptr, "reverse", FALSE);
290 kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
291 RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
292 RNA_boolean_set(kmi->ptr, "reverse", TRUE);
294 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
295 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
297 RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", TRUE);
298 RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", FALSE);
301 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
302 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
303 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
304 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
305 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
306 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
309 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
310 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
311 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", DEL_PREV_CHAR); /* same as above [#26623] */
313 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
314 RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
316 WM_keymap_add_item(keymap, "CONSOLE_OT_clear_line", RETKEY, KM_PRESS, KM_SHIFT, 0);
319 WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
320 WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
322 //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
323 WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
326 WM_keymap_add_item(keymap, "CONSOLE_OT_copy_as_script", CKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
327 WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
328 WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
330 WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
331 WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
334 WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
336 RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, KM_CTRL, 0)->ptr, "text", "\t"); /* fake tabs */
338 WM_keymap_add_item(keymap, "CONSOLE_OT_indent", TABKEY, KM_PRESS, 0, 0);
339 WM_keymap_add_item(keymap, "CONSOLE_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
341 WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
344 /****************** header region ******************/
346 /* add handlers, stuff you only do once or on area/region changes */
347 static void console_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
349 ED_region_header_init(ar);
352 static void console_header_area_draw(const bContext *C, ARegion *ar)
354 ED_region_header(C, ar);
357 static void console_main_area_listener(ARegion *ar, wmNotifier *wmn)
359 // SpaceInfo *sinfo = sa->spacedata.first;
361 /* context changes */
362 switch (wmn->category) {
364 if (wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
365 ED_region_tag_redraw(ar);
371 /* only called once, from space/spacetypes.c */
372 void ED_spacetype_console(void)
374 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype console");
377 st->spaceid = SPACE_CONSOLE;
378 strncpy(st->name, "Console", BKE_ST_MAXNAME);
380 st->new = console_new;
381 st->free = console_free;
382 st->init = console_init;
383 st->duplicate = console_duplicate;
384 st->operatortypes = console_operatortypes;
385 st->keymap = console_keymap;
386 st->dropboxes = console_dropboxes;
388 /* regions: main window */
389 art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
390 art->regionid = RGN_TYPE_WINDOW;
391 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
393 art->init = console_main_area_init;
394 art->draw = console_main_area_draw;
395 art->listener = console_main_area_listener;
399 BLI_addhead(&st->regiontypes, art);
401 /* regions: header */
402 art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
403 art->regionid = RGN_TYPE_HEADER;
404 art->prefsizey = HEADERY;
405 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
407 art->init = console_header_area_init;
408 art->draw = console_header_area_draw;
410 BLI_addhead(&st->regiontypes, art);
413 BKE_spacetype_register(st);