changing RNA properties now prints python script in the info view.
[blender-staging.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;
174         ID *id = drag->poin;
175
176         /* copy drag path to properties */
177         text = RNA_path_full_ID_py(id);
178         RNA_string_set(drop->ptr, "text", text);
179         MEM_freeN(text);
180 }
181
182 static int path_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
183 {
184         // SpaceConsole *sc = CTX_wm_space_console(C);
185         if (drag->type == WM_DRAG_PATH)
186                 return 1;
187         return 0;
188 }
189
190 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
191 {
192         char pathname[FILE_MAX + 2];
193         BLI_snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
194         RNA_string_set(drop->ptr, "text", pathname);
195 }
196
197
198 /* this region dropbox definition */
199 static void console_dropboxes(void)
200 {
201         ListBase *lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
202         
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);
205 }
206
207 /* ************* end drop *********** */
208
209 static void console_main_area_draw(const bContext *C, ARegion *ar)
210 {
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;
215
216         if (sc->scrollback.first == NULL)
217                 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
218
219         /* clear and setup matrix */
220         UI_ThemeClearColor(TH_BACK);
221         glClear(GL_COLOR_BUFFER_BIT);
222
223         /* worlks best with no view2d matrix set */
224         UI_view2d_view_ortho(v2d);
225
226         /* data... */
227
228         console_history_verify(C); /* make sure we have some command line */
229         console_textview_main(sc, ar);
230         
231         /* reset view matrix */
232         UI_view2d_view_restore(C);
233         
234         /* scrollers */
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);
238 }
239
240 static void console_operatortypes(void)
241 {
242         /* console_ops.c */
243         WM_operatortype_append(CONSOLE_OT_move);
244         WM_operatortype_append(CONSOLE_OT_delete);
245         WM_operatortype_append(CONSOLE_OT_insert);
246
247         WM_operatortype_append(CONSOLE_OT_indent);
248         WM_operatortype_append(CONSOLE_OT_unindent);
249         
250         /* for use by python only */
251         WM_operatortype_append(CONSOLE_OT_history_append); 
252         WM_operatortype_append(CONSOLE_OT_scrollback_append);
253
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);
260 }
261
262 static void console_keymap(struct wmKeyConfig *keyconf)
263 {
264         wmKeyMap *keymap = WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
265         wmKeyMapItem *kmi;
266         
267 #ifdef __APPLE__
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);
270 #endif
271
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);
274         
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);
277         
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);
281         
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);
285
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);
289         
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);
293
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);
296         
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);
299         
300 #if 0
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);
307 #endif
308         
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] */
312
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);
315
316         WM_keymap_add_item(keymap, "CONSOLE_OT_clear_line", RETKEY, KM_PRESS, KM_SHIFT, 0);
317
318 #ifdef WITH_PYTHON
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);
321         
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 */
324 #endif
325
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);
329 #ifdef __APPLE__
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);
332 #endif
333         
334         WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
335
336         RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, KM_CTRL, 0)->ptr, "text", "\t"); /* fake tabs */
337
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);
340
341         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
342 }
343
344 /****************** header region ******************/
345
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)
348 {
349         ED_region_header_init(ar);
350 }
351
352 static void console_header_area_draw(const bContext *C, ARegion *ar)
353 {
354         ED_region_header(C, ar);
355 }
356
357 static void console_main_area_listener(ARegion *ar, wmNotifier *wmn)
358 {
359         // SpaceInfo *sinfo = sa->spacedata.first;
360
361         /* context changes */
362         switch (wmn->category) {
363                 case NC_SPACE:
364                         if (wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
365                                 ED_region_tag_redraw(ar);
366                         }
367                         break;
368         }
369 }
370
371 /* only called once, from space/spacetypes.c */
372 void ED_spacetype_console(void)
373 {
374         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype console");
375         ARegionType *art;
376         
377         st->spaceid = SPACE_CONSOLE;
378         strncpy(st->name, "Console", BKE_ST_MAXNAME);
379         
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;
387         
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;
392
393         art->init = console_main_area_init;
394         art->draw = console_main_area_draw;
395         art->listener = console_main_area_listener;
396         
397         
398
399         BLI_addhead(&st->regiontypes, art);
400         
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;
406         
407         art->init = console_header_area_init;
408         art->draw = console_header_area_draw;
409         
410         BLI_addhead(&st->regiontypes, art);
411
412
413         BKE_spacetype_register(st);
414 }