Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_console / space_console.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/space_console/space_console.c
24  *  \ingroup spconsole
25  */
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_blenlib.h"
33 #include "BLI_utildefines.h"
34
35 #include "BKE_context.h"
36 #include "BKE_screen.h"
37
38 #include "ED_space_api.h"
39 #include "ED_screen.h"
40
41 #include "BIF_gl.h"
42
43 #include "RNA_access.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "UI_resources.h"
49 #include "UI_view2d.h"
50
51 #include "console_intern.h" // own include
52
53 /* ******************** default callbacks for console space ***************** */
54
55 static SpaceLink *console_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
56 {
57         ARegion *ar;
58         SpaceConsole *sconsole;
59
60         sconsole = MEM_callocN(sizeof(SpaceConsole), "initconsole");
61         sconsole->spacetype = SPACE_CONSOLE;
62
63         sconsole->lheight =  14;
64
65         /* header */
66         ar = MEM_callocN(sizeof(ARegion), "header for console");
67
68         BLI_addtail(&sconsole->regionbase, ar);
69         ar->regiontype = RGN_TYPE_HEADER;
70         ar->alignment = RGN_ALIGN_TOP;
71
72
73         /* main region */
74         ar = MEM_callocN(sizeof(ARegion), "main region for text");
75
76         BLI_addtail(&sconsole->regionbase, ar);
77         ar->regiontype = RGN_TYPE_WINDOW;
78
79         /* keep in sync with info */
80         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
81         ar->v2d.align |= V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y; /* align bottom left */
82         ar->v2d.keepofs |= V2D_LOCKOFS_X;
83         ar->v2d.keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
84         ar->v2d.keeptot = V2D_KEEPTOT_BOUNDS;
85         ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
86
87         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
88         //ar->v2d.keepzoom = (V2D_KEEPASPECT|V2D_LIMITZOOM);
89
90         return (SpaceLink *)sconsole;
91 }
92
93 /* not spacelink itself */
94 static void console_free(SpaceLink *sl)
95 {
96         SpaceConsole *sc = (SpaceConsole *) sl;
97
98         while (sc->scrollback.first)
99                 console_scrollback_free(sc, sc->scrollback.first);
100
101         while (sc->history.first)
102                 console_history_free(sc, sc->history.first);
103 }
104
105
106 /* spacetype; init callback */
107 static void console_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
108 {
109
110 }
111
112 static SpaceLink *console_duplicate(SpaceLink *sl)
113 {
114         SpaceConsole *sconsolen = MEM_dupallocN(sl);
115
116         /* clear or remove stuff from old */
117
118         /* TODO - duplicate?, then we also need to duplicate the py namespace */
119         BLI_listbase_clear(&sconsolen->scrollback);
120         BLI_listbase_clear(&sconsolen->history);
121
122         return (SpaceLink *)sconsolen;
123 }
124
125
126
127 /* add handlers, stuff you only do once or on area/region changes */
128 static void console_main_region_init(wmWindowManager *wm, ARegion *ar)
129 {
130         wmKeyMap *keymap;
131         ListBase *lb;
132
133         const float prev_y_min = ar->v2d.cur.ymin; /* so re-sizing keeps the cursor visible */
134
135         /* force it on init, for old files, until it becomes config */
136         ar->v2d.scroll = (V2D_SCROLL_RIGHT);
137
138         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
139
140         /* always keep the bottom part of the view aligned, less annoying */
141         if (prev_y_min != ar->v2d.cur.ymin) {
142                 const float cur_y_range = BLI_rctf_size_y(&ar->v2d.cur);
143                 ar->v2d.cur.ymin = prev_y_min;
144                 ar->v2d.cur.ymax = prev_y_min + cur_y_range;
145         }
146
147         /* own keymap */
148         keymap = WM_keymap_find(wm->defaultconf, "Console", SPACE_CONSOLE, 0);
149         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
150
151         /* add drop boxes */
152         lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
153
154         WM_event_add_dropbox_handler(&ar->handlers, lb);
155 }
156
157 /* same as 'text_cursor' */
158 static void console_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
159 {
160         SpaceText *st = sa->spacedata.first;
161         int wmcursor = BC_TEXTEDITCURSOR;
162
163         if (st->text && BLI_rcti_isect_pt(&st->txtbar, win->eventstate->x - ar->winrct.xmin, st->txtbar.ymin)) {
164                 wmcursor = CURSOR_STD;
165         }
166
167         WM_cursor_set(win, wmcursor);
168 }
169
170 /* ************* dropboxes ************* */
171
172 static int id_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
173 {
174 //      SpaceConsole *sc = CTX_wm_space_console(C);
175         if (drag->type == WM_DRAG_ID)
176                 return 1;
177         return 0;
178 }
179
180 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
181 {
182         char *text;
183         ID *id = drag->poin;
184
185         /* copy drag path to properties */
186         text = RNA_path_full_ID_py(id);
187         RNA_string_set(drop->ptr, "text", text);
188         MEM_freeN(text);
189 }
190
191 static int path_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
192 {
193         // SpaceConsole *sc = CTX_wm_space_console(C);
194         if (drag->type == WM_DRAG_PATH)
195                 return 1;
196         return 0;
197 }
198
199 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
200 {
201         char pathname[FILE_MAX + 2];
202         BLI_snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
203         RNA_string_set(drop->ptr, "text", pathname);
204 }
205
206
207 /* this region dropbox definition */
208 static void console_dropboxes(void)
209 {
210         ListBase *lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
211
212         WM_dropbox_add(lb, "CONSOLE_OT_insert", id_drop_poll, id_drop_copy);
213         WM_dropbox_add(lb, "CONSOLE_OT_insert", path_drop_poll, path_drop_copy);
214 }
215
216 /* ************* end drop *********** */
217
218 static void console_main_region_draw(const bContext *C, ARegion *ar)
219 {
220         /* draw entirely, view changes should be handled here */
221         SpaceConsole *sc = CTX_wm_space_console(C);
222         View2D *v2d = &ar->v2d;
223         View2DScrollers *scrollers;
224
225         if (BLI_listbase_is_empty(&sc->scrollback))
226                 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
227
228         /* clear and setup matrix */
229         UI_ThemeClearColor(TH_BACK);
230         glClear(GL_COLOR_BUFFER_BIT);
231
232         /* worlks best with no view2d matrix set */
233         UI_view2d_view_ortho(v2d);
234
235         /* data... */
236
237         console_history_verify(C); /* make sure we have some command line */
238         console_textview_main(sc, ar);
239
240         /* reset view matrix */
241         UI_view2d_view_restore(C);
242
243         /* scrollers */
244         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
245         UI_view2d_scrollers_draw(C, v2d, scrollers);
246         UI_view2d_scrollers_free(scrollers);
247 }
248
249 static void console_operatortypes(void)
250 {
251         /* console_ops.c */
252         WM_operatortype_append(CONSOLE_OT_move);
253         WM_operatortype_append(CONSOLE_OT_delete);
254         WM_operatortype_append(CONSOLE_OT_insert);
255
256         WM_operatortype_append(CONSOLE_OT_indent);
257         WM_operatortype_append(CONSOLE_OT_unindent);
258
259         /* for use by python only */
260         WM_operatortype_append(CONSOLE_OT_history_append);
261         WM_operatortype_append(CONSOLE_OT_scrollback_append);
262
263         WM_operatortype_append(CONSOLE_OT_clear);
264         WM_operatortype_append(CONSOLE_OT_clear_line);
265         WM_operatortype_append(CONSOLE_OT_history_cycle);
266         WM_operatortype_append(CONSOLE_OT_copy);
267         WM_operatortype_append(CONSOLE_OT_paste);
268         WM_operatortype_append(CONSOLE_OT_select_set);
269         WM_operatortype_append(CONSOLE_OT_select_word);
270 }
271
272 static void console_keymap(struct wmKeyConfig *keyconf)
273 {
274         wmKeyMap *keymap = WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
275         wmKeyMapItem *kmi;
276
277 #ifdef __APPLE__
278         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
279         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
280 #endif
281
282         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
283         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
284
285         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
286         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
287
288         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
289         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
290         RNA_boolean_set(kmi->ptr, "reverse", false);
291
292         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
293         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
294         RNA_boolean_set(kmi->ptr, "reverse", true);
295
296         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
297         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
298         RNA_boolean_set(kmi->ptr, "reverse", false);
299
300         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
301         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
302         RNA_boolean_set(kmi->ptr, "reverse", true);
303
304         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
305         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
306
307         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", true);
308         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", false);
309
310 #if 0
311         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
312         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
313         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
314         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
315         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
316         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
317 #endif
318
319         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
320         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
321         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] */
322
323         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
324         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
325
326         WM_keymap_add_item(keymap, "CONSOLE_OT_clear_line", RETKEY, KM_PRESS, KM_SHIFT, 0);
327         WM_keymap_add_item(keymap, "CONSOLE_OT_clear_line", PADENTER, KM_PRESS, KM_SHIFT, 0);
328
329 #ifdef WITH_PYTHON
330         kmi = WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0);
331         RNA_boolean_set(kmi->ptr, "interactive", true);
332         kmi = WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
333         RNA_boolean_set(kmi->ptr, "interactive", true);
334
335         //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
336         WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
337 #endif
338
339         WM_keymap_add_item(keymap, "CONSOLE_OT_copy_as_script", CKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
340         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
341         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
342 #ifdef __APPLE__
343         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
344         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
345 #endif
346
347         WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
348         WM_keymap_add_item(keymap, "CONSOLE_OT_select_word", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
349
350         RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, KM_CTRL, 0)->ptr, "text", "\t"); /* fake tabs */
351
352         WM_keymap_add_item(keymap, "CONSOLE_OT_indent", TABKEY, KM_PRESS, 0, 0);
353         WM_keymap_add_item(keymap, "CONSOLE_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
354
355         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
356 }
357
358 /****************** header region ******************/
359
360 /* add handlers, stuff you only do once or on area/region changes */
361 static void console_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
362 {
363         ED_region_header_init(ar);
364 }
365
366 static void console_header_region_draw(const bContext *C, ARegion *ar)
367 {
368         ED_region_header(C, ar);
369 }
370
371 static void console_main_region_listener(
372         bScreen *UNUSED(sc), ScrArea *sa, ARegion *ar,
373         wmNotifier *wmn, const Scene *UNUSED(scene))
374 {
375         // SpaceInfo *sinfo = sa->spacedata.first;
376
377         /* context changes */
378         switch (wmn->category) {
379                 case NC_SPACE:
380                 {
381                         if (wmn->data == ND_SPACE_CONSOLE) {
382                                 if (wmn->action == NA_EDITED) {
383                                         if ((wmn->reference && sa) && (wmn->reference == sa->spacedata.first)) {
384                                                 /* we've modified the geometry (font size), re-calculate rect */
385                                                 console_textview_update_rect(wmn->reference, ar);
386                                                 ED_region_tag_redraw(ar);
387                                         }
388                                 }
389                                 else {
390                                         /* generic redraw request */
391                                         ED_region_tag_redraw(ar);
392                                 }
393                         }
394                         break;
395                 }
396         }
397 }
398
399 /* only called once, from space/spacetypes.c */
400 void ED_spacetype_console(void)
401 {
402         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype console");
403         ARegionType *art;
404
405         st->spaceid = SPACE_CONSOLE;
406         strncpy(st->name, "Console", BKE_ST_MAXNAME);
407
408         st->new = console_new;
409         st->free = console_free;
410         st->init = console_init;
411         st->duplicate = console_duplicate;
412         st->operatortypes = console_operatortypes;
413         st->keymap = console_keymap;
414         st->dropboxes = console_dropboxes;
415
416         /* regions: main window */
417         art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
418         art->regionid = RGN_TYPE_WINDOW;
419         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
420
421         art->init = console_main_region_init;
422         art->draw = console_main_region_draw;
423         art->cursor = console_cursor;
424         art->listener = console_main_region_listener;
425
426
427
428         BLI_addhead(&st->regiontypes, art);
429
430         /* regions: header */
431         art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
432         art->regionid = RGN_TYPE_HEADER;
433         art->prefsizey = HEADERY;
434         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
435
436         art->init = console_header_region_init;
437         art->draw = console_header_region_draw;
438
439         BLI_addhead(&st->regiontypes, art);
440
441
442         BKE_spacetype_register(st);
443 }