- Set View2D operators not to register, got in the way a lot with the console.
- Made autocomplete Ctrl+Enter so Tab can be used.
- Should work with python 2.5 now. (patch from Vilda)
- Moved report struct definitions into DNA_windowmanager_types.h, could also have DNA_report_types.h however the reports are not saved, its just needed so the report list can be used in the wmWindowManager struct. Fixes a crash reported by ZanQdo.
- Store the report message length in the report so calculating the total height including word wrap is not so slow.
console_id can be any hashable type
'''
- import sys, code, io
+ import sys, code
try: consoles = get_console.consoles
except:consoles = get_console.consoles = {}
console = code.InteractiveConsole(namespace)
if sys.version.startswith('2'):
- stdout = io.BytesIO() # Py2x support
- stderr = io.BytesIO()
+ import cStringIO
+ stdout = cStringIO.BytesIO() # Py2x support
+ stderr = cStringIO.BytesIO()
else:
+ import io
stdout = io.StringIO()
stderr = io.StringIO()
extern "C" {
#endif
-#include "DNA_listBase.h"
+#include "DNA_windowmanager_types.h"
/* Reporting Information and Errors
*
* These functions also accept NULL in case no error reporting
* is needed. */
-typedef enum ReportType {
- RPT_DEBUG = 1<<0,
- RPT_INFO = 1<<1,
- RPT_OPERATOR = 1<<2,
- RPT_WARNING = 1<<3,
- RPT_ERROR = 1<<4,
- RPT_ERROR_INVALID_INPUT = 1<<5,
- RPT_ERROR_INVALID_CONTEXT = 1<<6,
- RPT_ERROR_OUT_OF_MEMORY = 1<<7
-} ReportType;
-
-#define RPT_DEBUG_ALL (RPT_DEBUG)
-#define RPT_INFO_ALL (RPT_INFO)
-#define RPT_OPERATOR_ALL (RPT_OPERATOR)
-#define RPT_WARNING_ALL (RPT_WARNING)
-#define RPT_ERROR_ALL (RPT_ERROR|RPT_ERROR_INVALID_INPUT|RPT_ERROR_INVALID_CONTEXT|RPT_ERROR_OUT_OF_MEMORY)
-
-enum ReportListFlags {
- RPT_PRINT = 1,
- RPT_STORE = 2,
-};
-
-typedef struct Report {
- struct Report *next, *prev;
- ReportType type;
- char *typestr;
- char *message;
-} Report;
-
-typedef struct ReportList {
- ListBase list;
- ReportType printlevel;
- ReportType storelevel;
- int flag;
-} ReportList;
+/* report structures are stored in DNA */
void BKE_reports_init(ReportList *reports, int flag);
void BKE_reports_clear(ReportList *reports);
struct ReportList *CTX_wm_reports(const bContext *C)
{
- return C->wm.manager->reports;
+ return &(C->wm.manager->reports);
}
View3D *CTX_wm_view3d(const bContext *C)
len= strlen(message);
report->message= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
memcpy(report->message, message, sizeof(char)*(len+1));
-
+ report->len= len;
BLI_addtail(&reports->list, report);
}
}
va_end(args);
report->message= BLI_dynstr_get_cstring(ds);
-
+ report->len= BLI_dynstr_get_len(ds);
BLI_dynstr_free(ds);
report->type= type;
MEM_freeN(report->message);
report->message= BLI_dynstr_get_cstring(ds);
+ report->len= BLI_dynstr_get_len(ds);
BLI_dynstr_free(ds);
}
MEM_freeN(report->message);
report->message= BLI_dynstr_get_cstring(ds);
+ report->len= BLI_dynstr_get_len(ds);
BLI_dynstr_free(ds);
}
wm->keymaps.first= wm->keymaps.last= NULL;
wm->paintcursors.first= wm->paintcursors.last= NULL;
wm->queue.first= wm->queue.last= NULL;
- wm->reports= NULL;
+ BKE_reports_init(&wm->reports, RPT_STORE);
+
wm->jobs.first= wm->jobs.last= NULL;
wm->windrawable= NULL;
ot->modal= view_pan_modal;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
+ ot->flag= OPTYPE_BLOCKING;
/* rna - must keep these in sync with the other operators */
RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
ot->exec= view_scrollright_exec;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER;
+ // ot->flag= OPTYPE_REGISTER;
/* rna - must keep these in sync with the other operators */
RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
ot->exec= view_scrollleft_exec;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER;
+ // ot->flag= OPTYPE_REGISTER;
/* rna - must keep these in sync with the other operators */
RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
ot->exec= view_scrolldown_exec;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER;
+ // ot->flag= OPTYPE_REGISTER;
/* rna - must keep these in sync with the other operators */
RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
ot->exec= view_scrollup_exec;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER;
+ // ot->flag= OPTYPE_REGISTER;
/* rna - must keep these in sync with the other operators */
RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
ot->exec= view_zoomin_exec;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER;
+ // ot->flag= OPTYPE_REGISTER;
/* rna - must keep these in sync with the other operators */
RNA_def_float(ot->srna, "zoomfacx", 0, -FLT_MAX, FLT_MAX, "Zoom Factor X", "", -FLT_MAX, FLT_MAX);
ot->exec= view_zoomout_exec;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER;
+ // ot->flag= OPTYPE_REGISTER;
/* rna - must keep these in sync with the other operators */
RNA_def_float(ot->srna, "zoomfacx", 0, -FLT_MAX, FLT_MAX, "Zoom Factor X", "", -FLT_MAX, FLT_MAX);
ot->modal= view_zoomdrag_modal;
/* operator is repeatable */
- ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
+ // ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
/* rna - must keep these in sync with the other operators */
RNA_def_float(ot->srna, "deltax", 0, -FLT_MAX, FLT_MAX, "Delta X", "", -FLT_MAX, FLT_MAX);
BLF_set(mono);
BLF_aspect(1.0);
- BLF_size(sc->lheight, 72);
+ BLF_size(sc->lheight-2, 72);
}
static void console_line_color(unsigned char *fg, int type)
/* return 0 if the last line is off the screen
* should be able to use this for any string type */
-static int console_draw_string(char *str, int str_len, int console_width, int lheight, unsigned char *fg, unsigned char *bg, int winx, int winy, int *x, int *y)
+static int console_draw_string( char *str, int str_len,
+ int console_width, int lheight,
+ unsigned char *fg, unsigned char *bg,
+ int winx, int winy,
+ int *x, int *y, int draw)
{
int rct_ofs= lheight/4;
+ int tot_lines = (str_len/console_width)+1; /* total number of lines for wrapping */
+
+ /* just advance the height */
+ if(draw==0) {
+ if(str_len > console_width) (*y) += tot_lines * lheight;
+ else (*y) += lheight;
+
+ return 1;
+ }
if(str_len > console_width) { /* wrap? */
- int tot_lines = (str_len/console_width)+1; /* total number of lines for wrapping */
char *line_stride= str + ((tot_lines-1) * console_width); /* advance to the last line and draw it first */
char eol; /* baclup the end of wrapping */
return 1;
}
-#define CONSOLE_DRAW_MARGIN 8
-#define CONSOLE_LINE_MARGIN 6
+#define CONSOLE_DRAW_MARGIN 4
+#define CONSOLE_DRAW_SCROLL 16
-void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
+static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw)
{
+ View2D *v2d= &ar->v2d;
+
ConsoleLine *cl= sc->history.last;
int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
console_font_begin(sc);
cwidth = BLF_fixed_width();
- console_width= (ar->winx - CONSOLE_DRAW_MARGIN*2)/cwidth;
+ console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
if (console_width < 8) console_width= 8;
x= x_orig; y= y_orig;
if(sc->type==CONSOLE_TYPE_PYTHON) {
- int prompt_len= strlen(sc->prompt);
+ int prompt_len;
/* text */
- console_line_color(fg, CONSOLE_LINE_INPUT);
- glColor3ub(fg[0], fg[1], fg[2]);
-
- /* command line */
- if(prompt_len) {
- BLF_position(x, y, 0); x += cwidth * prompt_len;
- BLF_draw(sc->prompt);
+ if(draw) {
+ prompt_len= strlen(sc->prompt);
+ console_line_color(fg, CONSOLE_LINE_INPUT);
+ glColor3ub(fg[0], fg[1], fg[2]);
+
+ /* command line */
+ if(prompt_len) {
+ BLF_position(x, y, 0); x += cwidth * prompt_len;
+ BLF_draw(sc->prompt);
+ }
+ BLF_position(x, y, 0);
+ BLF_draw(cl->line);
+
+ /* cursor */
+ console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
+ glColor3ub(fg[0], fg[1], fg[2]);
+ glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
+
+ x= x_orig; /* remove prompt offset */
}
- BLF_position(x, y, 0);
- BLF_draw(cl->line);
-
- /* cursor */
- console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
- glColor3ub(fg[0], fg[1], fg[2]);
- glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
-
- x= x_orig; /* remove prompt offset */
y += sc->lheight;
for(cl= sc->scrollback.last; cl; cl= cl->prev) {
- console_line_color(fg, cl->type);
- if(!console_draw_string(cl->line, cl->len, console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, NULL, ar->winx, ar->winy, &x, &y))
- break; /* past the y limits */
-
+ if(draw)
+ console_line_color(fg, cl->type);
+
+ if(!console_draw_string( cl->line, cl->len,
+ console_width, sc->lheight,
+ fg, NULL,
+ ar->winx-CONSOLE_DRAW_MARGIN, v2d->cur.ymax,
+ &x, &y, draw))
+ {
+ /* when drawing, if we pass v2d->cur.ymax, then quit */
+ if(draw) {
+ break; /* past the y limits */
+ }
+ }
}
}
else {
int bool= 0;
unsigned char bg[3] = {114, 114, 114};
- glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
+ if(draw) {
+ glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+ }
/* convert our display toggles into a flag compatible with BKE_report flags */
if(sc->rpt_mask & CONSOLE_RPT_DEBUG) report_mask |= RPT_DEBUG_ALL;
for(report=reports->list.last; report; report=report->prev) {
if(report->type & report_mask) {
- console_report_color(fg, report->type);
- if(!console_draw_string(report->message, strlen(report->message), console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, bool?bg:NULL, ar->winx, ar->winy, &x, &y))
- break; /* past the y limits */
- y+=CONSOLE_LINE_MARGIN;
+ if(draw)
+ console_report_color(fg, report->type);
+
+ if(!console_draw_string( report->message, report->len,
+ console_width, sc->lheight,
+ fg, bool?bg:NULL,
+ ar->winx-CONSOLE_DRAW_MARGIN, v2d->cur.ymax,
+ &x, &y, draw))
+ {
+ /* when drawing, if we pass v2d->cur.ymax, then quit */
+ if(draw) {
+ break; /* past the y limits */
+ }
+ }
+
bool = !(bool);
}
}
}
+ y += sc->lheight*2;
+
+ return y-y_orig;
+}
+
+void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
+{
+ console_text_main__internal(sc, ar, reports, 1);
+}
+
+int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
+{
+ return console_text_main__internal(sc, ar, reports, 0);
}
/* console_draw.c */
void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, struct ReportList *reports);
+int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, struct ReportList *reports); /* needed to calculate the scrollbar */
/* console_ops.c */
void console_history_free(SpaceConsole *sc, ConsoleLine *cl);
static int insert_invoke(const bContext *C, wmOperator *op, wmEvent *event)
{
- char str[2] = {event->ascii, '\0'};
- RNA_string_set(op->ptr, "text", str);
+ if(!RNA_property_is_set(op->ptr, "text")) {
+ char str[2] = {event->ascii, '\0'};
+ RNA_string_set(op->ptr, "text", str);
+ }
return insert_exec(C, op);
}
#include "console_intern.h" // own include
+static void console_update_rect(bContext *C, ARegion *ar)
+{
+ SpaceConsole *sc= CTX_wm_space_console(C);
+ View2D *v2d= &ar->v2d;
+
+ UI_view2d_totRect_set(v2d, ar->winx-1, console_text_height(sc, ar, CTX_wm_reports(C)));
+}
/* ******************** default callbacks for console space ***************** */
BLI_addtail(&sconsole->regionbase, ar);
ar->regiontype= RGN_TYPE_WINDOW;
- ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM_O);
- ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
+
+ ar->v2d.scroll |= (V2D_SCROLL_LEFT);
+ ar->v2d.align |= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
+ ar->v2d.keepofs |= V2D_LOCKOFS_X;
ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
- ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
+ ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
-
+
+ /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
+ //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_KEEPZOOM);
+
return (SpaceLink *)sconsole;
}
static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
{
ListBase *keymap;
-
- UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
-
+
+ UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
+
/* own keymap */
keymap= WM_keymap_listbase(wm, "Console", SPACE_CONSOLE, 0); /* XXX weak? */
WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
{
/* draw entirely, view changes should be handled here */
SpaceConsole *sc= CTX_wm_space_console(C);
- //View2D *v2d= &ar->v2d;
+ View2D *v2d= &ar->v2d;
+ View2DScrollers *scrollers;
//float col[3];
- /* clear and setup matrix */
- //UI_GetThemeColor3fv(TH_BACK, col);
- //glClearColor(col[0], col[1], col[2], 0.0);
- glClearColor(0, 0, 0, 1.0);
-
- glClear(GL_COLOR_BUFFER_BIT);
-
- /* worlks best with no view2d matrix set */
- /*UI_view2d_view_ortho(C, v2d);*/
-
- /* data... */
-
/* add helper text, why not? */
if(sc->scrollback.first==NULL) {
console_scrollback_add_str(C, " * Python Interactive Console *", 0);
console_scrollback_add_str(C, "Cursor: Left/Right Home/End", 0);
console_scrollback_add_str(C, "Remove: Backspace/Delete", 0);
console_scrollback_add_str(C, "Execute: Enter", 0);
- console_scrollback_add_str(C, "Autocomplete: Tab", 0);
+ console_scrollback_add_str(C, "Autocomplete: Ctrl+Enter", 0);
console_scrollback_add_str(C, "Ctrl +/- Wheel: Zoom", 0);
console_scrollback_add_str(C, "Builtin Modules: bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.ui", 0);
}
+ /* clear and setup matrix */
+ //UI_GetThemeColor3fv(TH_BACK, col);
+ //glClearColor(col[0], col[1], col[2], 0.0);
+ glClearColor(0, 0, 0, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ console_update_rect(C, ar);
+
+ /* worlks best with no view2d matrix set */
+ UI_view2d_view_ortho(C, v2d);
+
+ /* data... */
+
console_history_verify(C); /* make sure we have some command line */
console_text_main(sc, ar, CTX_wm_reports(C));
/* reset view matrix */
- /* UI_view2d_view_restore(C); */
+ UI_view2d_view_restore(C);
/* scrollers */
- /*
- scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
+ scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
UI_view2d_scrollers_draw(C, v2d, scrollers);
UI_view2d_scrollers_free(scrollers);
- */
}
void console_operatortypes(void)
#ifndef DISABLE_PYTHON
WM_keymap_add_item(keymap, "CONSOLE_OT_exec", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
- WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
+ //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
+ WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", RETKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
#endif
+
+ RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, 0, 0)->ptr, "text", " "); /* fake tabs */
WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_PRESS, KM_ANY, 0); // last!
}
/* regions: main window */
art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
art->regionid = RGN_TYPE_WINDOW;
+ art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
+
art->init= console_main_area_init;
art->draw= console_main_area_draw;
+
BLI_addhead(&sc->regiontypes, art);
/* regions: header */
int type; /* console/report/..? */
int rpt_mask; /* which reports to display */
int flag, lheight;
-
+
ListBase scrollback; /* ConsoleLine; output */
ListBase history; /* ConsoleLine; command history, current edited line is the first */
char prompt[8];
struct StructRNA;
struct PointerRNA;
struct ReportList;
+struct Report;
+
+typedef enum ReportType {
+ RPT_DEBUG = 1<<0,
+ RPT_INFO = 1<<1,
+ RPT_OPERATOR = 1<<2,
+ RPT_WARNING = 1<<3,
+ RPT_ERROR = 1<<4,
+ RPT_ERROR_INVALID_INPUT = 1<<5,
+ RPT_ERROR_INVALID_CONTEXT = 1<<6,
+ RPT_ERROR_OUT_OF_MEMORY = 1<<7
+} ReportType;
+
+#define RPT_DEBUG_ALL (RPT_DEBUG)
+#define RPT_INFO_ALL (RPT_INFO)
+#define RPT_OPERATOR_ALL (RPT_OPERATOR)
+#define RPT_WARNING_ALL (RPT_WARNING)
+#define RPT_ERROR_ALL (RPT_ERROR|RPT_ERROR_INVALID_INPUT|RPT_ERROR_INVALID_CONTEXT|RPT_ERROR_OUT_OF_MEMORY)
+
+enum ReportListFlags {
+ RPT_PRINT = 1,
+ RPT_STORE = 2,
+};
+typedef struct Report {
+ struct Report *next, *prev;
+ int type; /* ReportType */
+ int len; /* strlen(message), saves some time calculating the word wrap */
+ char *typestr;
+ char *message;
+} Report;
+typedef struct ReportList {
+ ListBase list;
+ int printlevel; /* ReportType */
+ int storelevel; /* ReportType */
+ int flag, pad;
+} ReportList;
+/* reports need to be before wmWindowManager */
+
/* windowmanager is saved, tag WMAN */
typedef struct wmWindowManager {
ListBase queue; /* refresh/redraw wmNotifier structs */
- struct ReportList *reports; /* information and error reports */
+ struct ReportList reports; /* information and error reports */
ListBase jobs; /* threaded jobs manager */
WM_RADIALCONTROL_ANGLE
} wmRadialControlMode;
-
#endif /* DNA_WINDOWMANAGER_TYPES_H */
/* Report the string representation of the operator */
buf = WM_operator_pystring(op);
- BKE_report(wm->reports, RPT_OPERATOR, buf);
+ BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
MEM_freeN(buf);
/* so the console is redrawn */
static void wm_init_reports(bContext *C)
{
- wmWindowManager *wm= CTX_wm_manager(C);
- wm->reports= MEM_callocN(sizeof(ReportList), "wmReportList");
- BKE_reports_init(wm->reports, RPT_STORE);
+ BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
}
static void wm_free_reports(bContext *C)
{
- wmWindowManager *wm= CTX_wm_manager(C);
- BKE_reports_clear(wm->reports);
- MEM_freeN(wm->reports);
+ BKE_reports_clear(CTX_wm_reports(C));
}