svn merge ^/trunk/blender -r48592:HEAD
[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 #ifdef WIN32
31 #  include "BLI_winstuff.h"
32 #endif
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39
40 #include "BKE_context.h"
41 #include "BKE_screen.h"
42 #include "BKE_idcode.h"
43
44 #include "ED_space_api.h"
45 #include "ED_screen.h"
46
47 #include "BIF_gl.h"
48
49 #include "RNA_access.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "UI_resources.h"
55 #include "UI_view2d.h"
56
57 #include "console_intern.h" // own include
58
59 /* ******************** default callbacks for console space ***************** */
60
61 static SpaceLink *console_new(const bContext *UNUSED(C))
62 {
63         ARegion *ar;
64         SpaceConsole *sconsole;
65         
66         sconsole = MEM_callocN(sizeof(SpaceConsole), "initconsole");
67         sconsole->spacetype = SPACE_CONSOLE;
68         
69         sconsole->lheight =  14;
70         
71         /* header */
72         ar = MEM_callocN(sizeof(ARegion), "header for console");
73         
74         BLI_addtail(&sconsole->regionbase, ar);
75         ar->regiontype = RGN_TYPE_HEADER;
76         ar->alignment = RGN_ALIGN_BOTTOM;
77         
78         
79         /* main area */
80         ar = MEM_callocN(sizeof(ARegion), "main area for text");
81         
82         BLI_addtail(&sconsole->regionbase, ar);
83         ar->regiontype = RGN_TYPE_WINDOW;
84         
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;
92
93         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
94         //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
95
96         return (SpaceLink *)sconsole;
97 }
98
99 /* not spacelink itself */
100 static void console_free(SpaceLink *sl)
101 {
102         SpaceConsole *sc = (SpaceConsole *) sl;
103         
104         while (sc->scrollback.first)
105                 console_scrollback_free(sc, sc->scrollback.first);
106         
107         while (sc->history.first)
108                 console_history_free(sc, sc->history.first);
109 }
110
111
112 /* spacetype; init callback */
113 static void console_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
114 {
115
116 }
117
118 static SpaceLink *console_duplicate(SpaceLink *sl)
119 {
120         SpaceConsole *sconsolen = MEM_dupallocN(sl);
121         
122         /* clear or remove stuff from old */
123         
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;
127         
128         return (SpaceLink *)sconsolen;
129 }
130
131
132
133 /* add handlers, stuff you only do once or on area/region changes */
134 static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
135 {
136         wmKeyMap *keymap;
137         ListBase *lb;
138
139         const float prev_y_min = ar->v2d.cur.ymin; /* so re-sizing keeps the cursor visible */
140
141         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
142
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;
148         }
149
150         /* own keymap */
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);
153         
154         /* add drop boxes */
155         lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
156         
157         WM_event_add_dropbox_handler(&ar->handlers, lb);
158 }
159
160
161 /* ************* dropboxes ************* */
162
163 static int id_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
164 {
165 //      SpaceConsole *sc= CTX_wm_space_console(C);
166         if (drag->type == WM_DRAG_ID)
167                 return 1;
168         return 0;
169 }
170
171 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
172 {
173         char text[64];
174         ID *id = drag->poin;
175         char id_esc[(sizeof(id->name) - 2) * 2];
176
177         BLI_strescape(id_esc, id->name + 2, sizeof(id_esc));
178
179         BLI_snprintf(text, sizeof(text), "bpy.data.%s[\"%s\"]", BKE_idcode_to_name_plural(GS(id->name)), id_esc);
180
181         /* copy drag path to properties */
182         RNA_string_set(drop->ptr, "text", text);
183 }
184
185 static int path_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
186 {
187 //    SpaceConsole *sc= CTX_wm_space_console(C);
188         if (drag->type == WM_DRAG_PATH)
189                 return 1;
190         return 0;
191 }
192
193 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
194 {
195         char pathname[FILE_MAX + 2];
196         BLI_snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
197         RNA_string_set(drop->ptr, "text", pathname);
198 }
199
200
201 /* this region dropbox definition */
202 static void console_dropboxes(void)
203 {
204         ListBase *lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
205         
206         WM_dropbox_add(lb, "CONSOLE_OT_insert", id_drop_poll, id_drop_copy);
207         WM_dropbox_add(lb, "CONSOLE_OT_insert", path_drop_poll, path_drop_copy);
208 }
209
210 /* ************* end drop *********** */
211
212 static void console_main_area_draw(const bContext *C, ARegion *ar)
213 {
214         /* draw entirely, view changes should be handled here */
215         SpaceConsole *sc = CTX_wm_space_console(C);
216         View2D *v2d = &ar->v2d;
217         View2DScrollers *scrollers;
218
219         if (sc->scrollback.first == NULL)
220                 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
221
222         /* clear and setup matrix */
223         UI_ThemeClearColor(TH_BACK);
224         glClear(GL_COLOR_BUFFER_BIT);
225
226         /* worlks best with no view2d matrix set */
227         UI_view2d_view_ortho(v2d);
228
229         /* data... */
230
231         console_history_verify(C); /* make sure we have some command line */
232         console_textview_main(sc, ar);
233         
234         /* reset view matrix */
235         UI_view2d_view_restore(C);
236         
237         /* scrollers */
238         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
239         UI_view2d_scrollers_draw(C, v2d, scrollers);
240         UI_view2d_scrollers_free(scrollers);
241 }
242
243 static void console_operatortypes(void)
244 {
245         /* console_ops.c */
246         WM_operatortype_append(CONSOLE_OT_move);
247         WM_operatortype_append(CONSOLE_OT_delete);
248         WM_operatortype_append(CONSOLE_OT_insert);
249
250         WM_operatortype_append(CONSOLE_OT_indent);
251         WM_operatortype_append(CONSOLE_OT_unindent);
252         
253         /* for use by python only */
254         WM_operatortype_append(CONSOLE_OT_history_append); 
255         WM_operatortype_append(CONSOLE_OT_scrollback_append);
256
257         WM_operatortype_append(CONSOLE_OT_clear);
258         WM_operatortype_append(CONSOLE_OT_clear_line);
259         WM_operatortype_append(CONSOLE_OT_history_cycle);
260         WM_operatortype_append(CONSOLE_OT_copy);
261         WM_operatortype_append(CONSOLE_OT_paste);
262         WM_operatortype_append(CONSOLE_OT_select_set);
263 }
264
265 static void console_keymap(struct wmKeyConfig *keyconf)
266 {
267         wmKeyMap *keymap = WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
268         wmKeyMapItem *kmi;
269         
270 #ifdef __APPLE__
271         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
272         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
273 #endif
274
275         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
276         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
277         
278         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
279         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
280         
281         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
282         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
283         RNA_boolean_set(kmi->ptr, "reverse", FALSE);
284         
285         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
286         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
287         RNA_boolean_set(kmi->ptr, "reverse", TRUE);
288
289         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
290         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
291         RNA_boolean_set(kmi->ptr, "reverse", FALSE);
292         
293         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
294         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
295         RNA_boolean_set(kmi->ptr, "reverse", TRUE);
296
297         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
298         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
299         
300         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", TRUE);
301         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", FALSE);
302         
303 #if 0
304         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
305         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
306         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
307         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
308         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
309         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
310 #endif
311         
312         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
313         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
314         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] */
315
316         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
317         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
318
319         WM_keymap_add_item(keymap, "CONSOLE_OT_clear_line", RETKEY, KM_PRESS, KM_SHIFT, 0);
320
321 #ifdef WITH_PYTHON
322         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
323         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
324         
325         //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
326         WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
327 #endif
328
329         WM_keymap_add_item(keymap, "CONSOLE_OT_copy_as_script", CKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
330         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
331         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
332 #ifdef __APPLE__
333         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
334         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
335 #endif
336         
337         WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
338
339         RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, KM_CTRL, 0)->ptr, "text", "\t"); /* fake tabs */
340
341         WM_keymap_add_item(keymap, "CONSOLE_OT_indent", TABKEY, KM_PRESS, 0, 0);
342         WM_keymap_add_item(keymap, "CONSOLE_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
343
344         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
345 }
346
347 /****************** header region ******************/
348
349 /* add handlers, stuff you only do once or on area/region changes */
350 static void console_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
351 {
352         ED_region_header_init(ar);
353 }
354
355 static void console_header_area_draw(const bContext *C, ARegion *ar)
356 {
357         ED_region_header(C, ar);
358 }
359
360 static void console_main_area_listener(ARegion *ar, wmNotifier *wmn)
361 {
362         // SpaceInfo *sinfo= sa->spacedata.first;
363
364         /* context changes */
365         switch (wmn->category) {
366                 case NC_SPACE:
367                         if (wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
368                                 ED_region_tag_redraw(ar);
369                         }
370                         break;
371         }
372 }
373
374 /* only called once, from space/spacetypes.c */
375 void ED_spacetype_console(void)
376 {
377         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype console");
378         ARegionType *art;
379         
380         st->spaceid = SPACE_CONSOLE;
381         strncpy(st->name, "Console", BKE_ST_MAXNAME);
382         
383         st->new = console_new;
384         st->free = console_free;
385         st->init = console_init;
386         st->duplicate = console_duplicate;
387         st->operatortypes = console_operatortypes;
388         st->keymap = console_keymap;
389         st->dropboxes = console_dropboxes;
390         
391         /* regions: main window */
392         art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
393         art->regionid = RGN_TYPE_WINDOW;
394         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
395
396         art->init = console_main_area_init;
397         art->draw = console_main_area_draw;
398         art->listener = console_main_area_listener;
399         
400         
401
402         BLI_addhead(&st->regiontypes, art);
403         
404         /* regions: header */
405         art = MEM_callocN(sizeof(ARegionType), "spacetype console region");
406         art->regionid = RGN_TYPE_HEADER;
407         art->prefsizey = HEADERY;
408         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
409         
410         art->init = console_header_area_init;
411         art->draw = console_header_area_draw;
412         
413         BLI_addhead(&st->regiontypes, art);
414
415
416         BKE_spacetype_register(st);
417 }