Blender  V2.93
space_console.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLI_blenlib.h"
27 #include "BLI_utildefines.h"
28 
29 #include "BKE_context.h"
30 #include "BKE_global.h"
31 #include "BKE_screen.h"
32 
33 #include "ED_screen.h"
34 #include "ED_space_api.h"
35 
36 #include "RNA_access.h"
37 
38 #include "WM_api.h"
39 #include "WM_types.h"
40 
41 #include "UI_resources.h"
42 #include "UI_view2d.h"
43 
44 #include "console_intern.h" /* own include */
45 
46 /* ******************** default callbacks for console space ***************** */
47 
49 {
50  ARegion *region;
51  SpaceConsole *sconsole;
52 
53  sconsole = MEM_callocN(sizeof(SpaceConsole), "initconsole");
54  sconsole->spacetype = SPACE_CONSOLE;
55 
56  sconsole->lheight = 14;
57 
58  /* header */
59  region = MEM_callocN(sizeof(ARegion), "header for console");
60 
61  BLI_addtail(&sconsole->regionbase, region);
62  region->regiontype = RGN_TYPE_HEADER;
64 
65  /* main region */
66  region = MEM_callocN(sizeof(ARegion), "main region for text");
67 
68  BLI_addtail(&sconsole->regionbase, region);
69  region->regiontype = RGN_TYPE_WINDOW;
70 
71  /* keep in sync with info */
72  region->v2d.scroll |= V2D_SCROLL_RIGHT;
73  region->v2d.align |= V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y; /* align bottom left */
74  region->v2d.keepofs |= V2D_LOCKOFS_X;
76  region->v2d.keeptot = V2D_KEEPTOT_BOUNDS;
77  region->v2d.minzoom = region->v2d.maxzoom = 1.0f;
78 
79  /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
80  // region->v2d.keepzoom = (V2D_KEEPASPECT|V2D_LIMITZOOM);
81 
82  return (SpaceLink *)sconsole;
83 }
84 
85 /* not spacelink itself */
86 static void console_free(SpaceLink *sl)
87 {
88  SpaceConsole *sc = (SpaceConsole *)sl;
89 
90  while (sc->scrollback.first) {
92  }
93 
94  while (sc->history.first) {
96  }
97 }
98 
99 /* spacetype; init callback */
101 {
102 }
103 
105 {
106  SpaceConsole *sconsolen = MEM_dupallocN(sl);
107 
108  /* clear or remove stuff from old */
109 
110  /* TODO - duplicate?, then we also need to duplicate the py namespace */
111  BLI_listbase_clear(&sconsolen->scrollback);
112  BLI_listbase_clear(&sconsolen->history);
113 
114  return (SpaceLink *)sconsolen;
115 }
116 
117 /* add handlers, stuff you only do once or on area/region changes */
119 {
120  wmKeyMap *keymap;
121  ListBase *lb;
122 
123  const float prev_y_min = region->v2d.cur.ymin; /* so re-sizing keeps the cursor visible */
124 
125  /* force it on init, for old files, until it becomes config */
126  region->v2d.scroll = (V2D_SCROLL_RIGHT);
127 
128  UI_view2d_region_reinit(&region->v2d, V2D_COMMONVIEW_CUSTOM, region->winx, region->winy);
129 
130  /* always keep the bottom part of the view aligned, less annoying */
131  if (prev_y_min != region->v2d.cur.ymin) {
132  const float cur_y_range = BLI_rctf_size_y(&region->v2d.cur);
133  region->v2d.cur.ymin = prev_y_min;
134  region->v2d.cur.ymax = prev_y_min + cur_y_range;
135  }
136 
137  /* own keymap */
138  keymap = WM_keymap_ensure(wm->defaultconf, "Console", SPACE_CONSOLE, 0);
140 
141  /* add drop boxes */
143 
145 }
146 
147 /* same as 'text_cursor' */
148 static void console_cursor(wmWindow *win, ScrArea *UNUSED(area), ARegion *region)
149 {
150  int wmcursor = WM_CURSOR_TEXT_EDIT;
151  const wmEvent *event = win->eventstate;
152  if (UI_view2d_mouse_in_scrollers(region, &region->v2d, event->x, event->y)) {
153  wmcursor = WM_CURSOR_DEFAULT;
154  }
155 
156  WM_cursor_set(win, wmcursor);
157 }
158 
159 /* ************* dropboxes ************* */
160 
162  wmDrag *drag,
163  const wmEvent *UNUSED(event),
164  const char **UNUSED(tooltip))
165 {
166  return WM_drag_get_local_ID(drag, 0) != NULL;
167 }
168 
169 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
170 {
171  ID *id = WM_drag_get_local_ID(drag, 0);
172 
173  /* copy drag path to properties */
174  char *text = RNA_path_full_ID_py(G_MAIN, id);
175  RNA_string_set(drop->ptr, "text", text);
176  MEM_freeN(text);
177 }
178 
180  wmDrag *drag,
181  const wmEvent *UNUSED(event),
182  const char **UNUSED(tooltip))
183 {
184  return (drag->type == WM_DRAG_PATH);
185 }
186 
187 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
188 {
189  char pathname[FILE_MAX + 2];
190  BLI_snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
191  RNA_string_set(drop->ptr, "text", pathname);
192 }
193 
194 /* this region dropbox definition */
195 static void console_dropboxes(void)
196 {
198 
199  WM_dropbox_add(lb, "CONSOLE_OT_insert", id_drop_poll, id_drop_copy, NULL);
200  WM_dropbox_add(lb, "CONSOLE_OT_insert", path_drop_poll, path_drop_copy, NULL);
201 }
202 
203 /* ************* end drop *********** */
204 
205 static void console_main_region_draw(const bContext *C, ARegion *region)
206 {
207  /* draw entirely, view changes should be handled here */
209  View2D *v2d = &region->v2d;
210 
211  if (BLI_listbase_is_empty(&sc->scrollback)) {
212  WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
213  }
214 
215  /* clear and setup matrix */
217 
218  /* Works best with no view2d matrix set. */
220 
221  /* data... */
222 
223  console_history_verify(C); /* make sure we have some command line */
224  console_textview_main(sc, region);
225 
226  /* reset view matrix */
228 
229  /* scrollers */
231 }
232 
233 static void console_operatortypes(void)
234 {
235  /* console_ops.c */
239 
243 
244  /* for use by python only */
247 
255 }
256 
257 static void console_keymap(struct wmKeyConfig *keyconf)
258 {
259  WM_keymap_ensure(keyconf, "Console", SPACE_CONSOLE, 0);
260 }
261 
262 /****************** header region ******************/
263 
264 /* add handlers, stuff you only do once or on area/region changes */
266 {
267  ED_region_header_init(region);
268 }
269 
270 static void console_header_region_draw(const bContext *C, ARegion *region)
271 {
272  ED_region_header(C, region);
273 }
274 
276 {
277  ScrArea *area = params->area;
278  ARegion *region = params->region;
279  wmNotifier *wmn = params->notifier;
280 
281  /* context changes */
282  switch (wmn->category) {
283  case NC_SPACE: {
284  if (wmn->data == ND_SPACE_CONSOLE) {
285  if (wmn->action == NA_EDITED) {
286  if ((wmn->reference && area) && (wmn->reference == area->spacedata.first)) {
287  /* we've modified the geometry (font size), re-calculate rect */
289  ED_region_tag_redraw(region);
290  }
291  }
292  else {
293  /* generic redraw request */
294  ED_region_tag_redraw(region);
295  }
296  }
297  break;
298  }
299  }
300 }
301 
302 /* only called once, from space/spacetypes.c */
304 {
305  SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype console");
306  ARegionType *art;
307 
308  st->spaceid = SPACE_CONSOLE;
309  strncpy(st->name, "Console", BKE_ST_MAXNAME);
310 
311  st->create = console_create;
312  st->free = console_free;
313  st->init = console_init;
316  st->keymap = console_keymap;
318 
319  /* regions: main window */
320  art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
321  art->regionid = RGN_TYPE_WINDOW;
323 
326  art->cursor = console_cursor;
327  art->event_cursor = true;
329 
330  BLI_addhead(&st->regiontypes, art);
331 
332  /* regions: header */
333  art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
334  art->regionid = RGN_TYPE_HEADER;
335  art->prefsizey = HEADERY;
337 
340 
341  BLI_addhead(&st->regiontypes, art);
342 
344 }
struct SpaceConsole * CTX_wm_space_console(const bContext *C)
Definition: context.c:791
#define G_MAIN
Definition: BKE_global.h:232
#define BKE_ST_MAXNAME
Definition: BKE_screen.h:68
void BKE_spacetype_register(struct SpaceType *st)
Definition: screen.c:420
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
#define FILE_MAX
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition: BLI_rect.h:165
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define UNUSED(x)
#define HEADERY
@ RGN_ALIGN_BOTTOM
@ RGN_ALIGN_TOP
@ RGN_TYPE_WINDOW
@ RGN_TYPE_HEADER
@ SPACE_CONSOLE
@ USER_HEADER_BOTTOM
@ V2D_KEEPTOT_BOUNDS
@ V2D_SCROLL_RIGHT
@ V2D_ALIGN_NO_NEG_X
@ V2D_ALIGN_NO_NEG_Y
@ V2D_LIMITZOOM
@ V2D_LOCKZOOM_X
@ V2D_KEEPASPECT
@ V2D_LOCKZOOM_Y
@ V2D_LOCKOFS_X
void ED_region_header(const struct bContext *C, struct ARegion *region)
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
void ED_region_header_init(struct ARegion *region)
Definition: area.c:3358
@ ED_KEYMAP_UI
Definition: ED_screen.h:440
@ ED_KEYMAP_HEADER
Definition: ED_screen.h:446
@ ED_KEYMAP_VIEW2D
Definition: ED_screen.h:443
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
@ TH_BACK
Definition: UI_resources.h:55
void UI_ThemeClearColor(int colorid)
Definition: resources.c:1478
void UI_view2d_region_reinit(struct View2D *v2d, short type, int winx, int winy)
Definition: view2d.c:240
char UI_view2d_mouse_in_scrollers(const struct ARegion *region, const struct View2D *v2d, int x, int y)
void UI_view2d_view_restore(const struct bContext *C)
void UI_view2d_view_ortho(const struct View2D *v2d)
void UI_view2d_scrollers_draw(struct View2D *v2d, const struct rcti *mask_custom)
@ V2D_COMMONVIEW_CUSTOM
Definition: UI_view2d.h:49
#define WM_DRAG_PATH
Definition: WM_types.h:876
@ WM_OP_EXEC_DEFAULT
Definition: WM_types.h:204
#define NA_EDITED
Definition: WM_types.h:462
#define ND_SPACE_CONSOLE
Definition: WM_types.h:414
#define NC_SPACE
Definition: WM_types.h:293
unsigned int U
Definition: btGjkEpa3.h:78
void console_textview_main(SpaceConsole *sc, const ARegion *region)
Definition: console_draw.c:239
void CONSOLE_OT_insert(struct wmOperatorType *ot)
Definition: console_ops.c:446
void CONSOLE_OT_unindent(struct wmOperatorType *ot)
Definition: console_ops.c:591
void CONSOLE_OT_copy(struct wmOperatorType *ot)
Definition: console_ops.c:1038
void console_textview_update_rect(SpaceConsole *sc, ARegion *region)
Definition: console_ops.c:59
void CONSOLE_OT_scrollback_append(struct wmOperatorType *ot)
Definition: console_ops.c:947
void CONSOLE_OT_paste(struct wmOperatorType *ot)
Definition: console_ops.c:1093
void CONSOLE_OT_select_word(struct wmOperatorType *ot)
Definition: console_ops.c:1271
void console_history_free(SpaceConsole *sc, ConsoleLine *cl)
Definition: console_ops.c:72
void console_scrollback_free(SpaceConsole *sc, ConsoleLine *cl)
Definition: console_ops.c:78
ConsoleLine * console_history_verify(const struct bContext *C)
void CONSOLE_OT_history_cycle(struct wmOperatorType *ot)
Definition: console_ops.c:840
void CONSOLE_OT_history_append(struct wmOperatorType *ot)
Definition: console_ops.c:896
void CONSOLE_OT_indent_or_autocomplete(struct wmOperatorType *ot)
Definition: console_ops.c:483
void CONSOLE_OT_move(struct wmOperatorType *ot)
Definition: console_ops.c:365
void CONSOLE_OT_delete(struct wmOperatorType *ot)
Definition: console_ops.c:686
void CONSOLE_OT_clear_line(struct wmOperatorType *ot)
Definition: console_ops.c:729
void CONSOLE_OT_indent(struct wmOperatorType *ot)
Definition: console_ops.c:537
void CONSOLE_OT_clear(struct wmOperatorType *ot)
Definition: console_ops.c:771
void CONSOLE_OT_select_set(struct wmOperatorType *ot)
Definition: console_ops.c:1221
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void area(int d1, int d2, int e1, int e2, float weights[2])
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
char * RNA_path_full_ID_py(Main *bmain, ID *id)
Definition: rna_access.c:6088
static void console_header_region_init(wmWindowManager *UNUSED(wm), ARegion *region)
static void console_operatortypes(void)
static void console_cursor(wmWindow *win, ScrArea *UNUSED(area), ARegion *region)
static SpaceLink * console_create(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
Definition: space_console.c:48
void ED_spacetype_console(void)
static void console_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(area))
static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
static void console_free(SpaceLink *sl)
Definition: space_console.c:86
static void console_header_region_draw(const bContext *C, ARegion *region)
static SpaceLink * console_duplicate(SpaceLink *sl)
static void console_main_region_draw(const bContext *C, ARegion *region)
static void console_dropboxes(void)
static bool path_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
static void console_main_region_listener(const wmRegionListenerParams *params)
static void console_keymap(struct wmKeyConfig *keyconf)
static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
static bool id_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
static void console_main_region_init(wmWindowManager *wm, ARegion *region)
void(* draw)(const struct bContext *C, struct ARegion *region)
Definition: BKE_screen.h:169
void(* cursor)(struct wmWindow *win, struct ScrArea *area, struct ARegion *region)
Definition: BKE_screen.h:197
void(* listener)(const wmRegionListenerParams *params)
Definition: BKE_screen.h:183
int keymapflag
Definition: BKE_screen.h:226
short event_cursor
Definition: BKE_screen.h:233
void(* init)(struct wmWindowManager *wm, struct ARegion *region)
Definition: BKE_screen.h:165
ListBase handlers
short alignment
short regiontype
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
ListBase regionbase
ListBase scrollback
struct SpaceLink *(* duplicate)(struct SpaceLink *sl)
Definition: BKE_screen.h:104
ListBase regiontypes
Definition: BKE_screen.h:130
void(* keymap)(struct wmKeyConfig *keyconf)
Definition: BKE_screen.h:109
void(* operatortypes)(void)
Definition: BKE_screen.h:107
void(* free)(struct SpaceLink *sl)
Definition: BKE_screen.h:88
struct SpaceLink *(* create)(const struct ScrArea *area, const struct Scene *scene)
Definition: BKE_screen.h:86
void(* init)(struct wmWindowManager *wm, struct ScrArea *area)
Definition: BKE_screen.h:91
int spaceid
Definition: BKE_screen.h:81
char name[BKE_ST_MAXNAME]
Definition: BKE_screen.h:80
void(* dropboxes)(void)
Definition: BKE_screen.h:111
float minzoom
short align
short keeptot
short keepzoom
short keepofs
short scroll
float maxzoom
float ymax
Definition: DNA_vec_types.h:86
float ymin
Definition: DNA_vec_types.h:86
char path[1024]
Definition: WM_types.h:909
int type
Definition: WM_types.h:907
struct PointerRNA * ptr
Definition: WM_types.h:953
unsigned int data
Definition: WM_types.h:260
unsigned int action
Definition: WM_types.h:260
unsigned int category
Definition: WM_types.h:260
void * reference
Definition: WM_types.h:262
struct wmKeyConfig * defaultconf
struct wmEvent * eventstate
void WM_cursor_set(wmWindow *win, int curs)
Definition: wm_cursors.c:142
@ WM_CURSOR_DEFAULT
Definition: wm_cursors.h:34
@ WM_CURSOR_TEXT_EDIT
Definition: wm_cursors.h:35
wmDropBox * WM_dropbox_add(ListBase *lb, const char *idname, bool(*poll)(bContext *, wmDrag *, const wmEvent *, const char **), void(*copy)(wmDrag *, wmDropBox *), void(*cancel)(struct Main *, wmDrag *, wmDropBox *))
Definition: wm_dragdrop.c:96
ID * WM_drag_get_local_ID(const wmDrag *drag, short idcode)
Definition: wm_dragdrop.c:335
ListBase * WM_dropboxmap_find(const char *idname, int spaceid, int regionid)
Definition: wm_dragdrop.c:77
wmEventHandler_Dropbox * WM_event_add_dropbox_handler(ListBase *handlers, ListBase *dropboxes)
int WM_operator_name_call(bContext *C, const char *opstring, short context, PointerRNA *properties)
wmEventHandler_Keymap * WM_event_add_keymap_handler_v2d_mask(ListBase *handlers, wmKeyMap *keymap)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition: wm_keymap.c:852
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))