rename and negate DISABLE_PYTHON --> WITH_PYTHON
[blender.git] / source / blender / editors / space_console / space_console.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Campbell Barton
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28  
29  #include <string.h>
30 #include <stdio.h>
31
32 #ifdef WIN32
33 #include "BLI_winstuff.h"
34 #endif
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_math.h"
40
41 #include "BKE_context.h"
42 #include "BKE_screen.h"
43 #include "BKE_idcode.h"
44
45 #include "ED_screen.h"
46
47 #include "BIF_gl.h"
48
49
50 #include "RNA_access.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
57
58 #include "console_intern.h"     // own include
59
60 static void console_update_rect(const bContext *C, ARegion *ar)
61 {
62         SpaceConsole *sc= CTX_wm_space_console(C);
63         View2D *v2d= &ar->v2d;
64
65         UI_view2d_totRect_set(v2d, ar->winx-1, console_text_height(sc, ar, CTX_wm_reports(C)));
66 }
67
68 /* ******************** default callbacks for console space ***************** */
69
70 static SpaceLink *console_new(const bContext *UNUSED(C))
71 {
72         ARegion *ar;
73         SpaceConsole *sconsole;
74         
75         sconsole= MEM_callocN(sizeof(SpaceConsole), "initconsole");
76         sconsole->spacetype= SPACE_CONSOLE;
77         
78         sconsole->lheight=      14;
79         sconsole->type=         CONSOLE_TYPE_PYTHON;
80         sconsole->rpt_mask=     CONSOLE_RPT_OP; /* ? - not sure whats a good default here?*/
81         
82         /* header */
83         ar= MEM_callocN(sizeof(ARegion), "header for console");
84         
85         BLI_addtail(&sconsole->regionbase, ar);
86         ar->regiontype= RGN_TYPE_HEADER;
87         ar->alignment= RGN_ALIGN_BOTTOM;
88         
89         
90         /* main area */
91         ar= MEM_callocN(sizeof(ARegion), "main area for text");
92         
93         BLI_addtail(&sconsole->regionbase, ar);
94         ar->regiontype= RGN_TYPE_WINDOW;
95         
96         
97         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
98         ar->v2d.align |= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
99         ar->v2d.keepofs |= V2D_LOCKOFS_X;
100         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
101         ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
102         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
103
104         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
105         //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
106
107         return (SpaceLink *)sconsole;
108 }
109
110 /* not spacelink itself */
111 static void console_free(SpaceLink *sl)
112 {
113         SpaceConsole *sc= (SpaceConsole*) sl;
114         
115         while(sc->scrollback.first)
116                 console_scrollback_free(sc, sc->scrollback.first);
117         
118         while(sc->history.first)
119                 console_history_free(sc, sc->history.first);
120 }
121
122
123 /* spacetype; init callback */
124 static void console_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
125 {
126
127 }
128
129 static SpaceLink *console_duplicate(SpaceLink *sl)
130 {
131         SpaceConsole *sconsolen= MEM_dupallocN(sl);
132         
133         /* clear or remove stuff from old */
134         
135         /* TODO - duplicate?, then we also need to duplicate the py namespace */
136         sconsolen->scrollback.first= sconsolen->scrollback.last= NULL;
137         sconsolen->history.first= sconsolen->history.last= NULL;
138         
139         return (SpaceLink *)sconsolen;
140 }
141
142
143
144 /* add handlers, stuff you only do once or on area/region changes */
145 static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
146 {
147         wmKeyMap *keymap;
148         ListBase *lb;
149
150         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
151
152         /* own keymap */
153         keymap= WM_keymap_find(wm->defaultconf, "Console", SPACE_CONSOLE, 0);
154         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
155         
156         /* add drop boxes */
157         lb= WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
158         
159         WM_event_add_dropbox_handler(&ar->handlers, lb);
160 }
161
162
163 /* ************* dropboxes ************* */
164
165 static int id_drop_poll(bContext *C, wmDrag *drag, wmEvent *UNUSED(event))
166 {
167     SpaceConsole *sc= CTX_wm_space_console(C);
168     if(sc->type==CONSOLE_TYPE_PYTHON)
169         if(drag->type==WM_DRAG_ID)
170             return 1;
171         return 0;
172 }
173
174 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
175 {
176         char text[64];
177         ID *id= drag->poin;
178
179         snprintf(text, sizeof(text), "bpy.data.%s['%s']", BKE_idcode_to_name_plural(GS(id->name)), id->name+2); 
180
181         /* copy drag path to properties */
182         RNA_string_set(drop->ptr, "text", text);
183 }
184
185 static int path_drop_poll(bContext *C, wmDrag *drag, wmEvent *UNUSED(event))
186 {
187     SpaceConsole *sc= CTX_wm_space_console(C);
188     if(sc->type==CONSOLE_TYPE_PYTHON)
189         if(drag->type==WM_DRAG_PATH)
190             return 1;
191         return 0;
192 }
193
194 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
195 {
196     char pathname[FILE_MAXDIR+FILE_MAXFILE+2];
197     snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
198         RNA_string_set(drop->ptr, "text", pathname);
199 }
200
201
202 /* this region dropbox definition */
203 static void console_dropboxes(void)
204 {
205         ListBase *lb= WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
206         
207         WM_dropbox_add(lb, "CONSOLE_OT_insert", id_drop_poll, id_drop_copy);
208         WM_dropbox_add(lb, "CONSOLE_OT_insert", path_drop_poll, path_drop_copy);
209 }
210
211 /* ************* end drop *********** */
212
213 static void console_main_area_draw(const bContext *C, ARegion *ar)
214 {
215         /* draw entirely, view changes should be handled here */
216         SpaceConsole *sc= CTX_wm_space_console(C);
217         View2D *v2d= &ar->v2d;
218         View2DScrollers *scrollers;
219         
220         if((sc->type==CONSOLE_TYPE_PYTHON) && (sc->scrollback.first==NULL))
221                 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
222
223         /* clear and setup matrix */
224         UI_ThemeClearColor(TH_BACK);
225         glClear(GL_COLOR_BUFFER_BIT);
226
227         console_update_rect(C, ar);
228
229         /* worlks best with no view2d matrix set */
230         UI_view2d_view_ortho(v2d);
231
232         /* data... */
233
234         console_history_verify(C); /* make sure we have some command line */
235         console_text_main(sc, ar, CTX_wm_reports(C));
236         
237         /* reset view matrix */
238         UI_view2d_view_restore(C);
239         
240         /* scrollers */
241         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
242         UI_view2d_scrollers_draw(C, v2d, scrollers);
243         UI_view2d_scrollers_free(scrollers);
244 }
245
246 void console_operatortypes(void)
247 {
248         /* console_ops.c */
249         WM_operatortype_append(CONSOLE_OT_move);
250         WM_operatortype_append(CONSOLE_OT_delete);
251         WM_operatortype_append(CONSOLE_OT_insert);
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_history_cycle);
259         WM_operatortype_append(CONSOLE_OT_copy);
260         WM_operatortype_append(CONSOLE_OT_paste);
261         WM_operatortype_append(CONSOLE_OT_select_set);
262
263         /* console_report.c */
264         WM_operatortype_append(CONSOLE_OT_select_pick);
265         WM_operatortype_append(CONSOLE_OT_select_all_toggle);
266         WM_operatortype_append(CONSOLE_OT_select_border);
267
268         WM_operatortype_append(CONSOLE_OT_report_replay);
269         WM_operatortype_append(CONSOLE_OT_report_delete);
270         WM_operatortype_append(CONSOLE_OT_report_copy);
271 }
272
273 void console_keymap(struct wmKeyConfig *keyconf)
274 {
275         wmKeyMap *keymap= WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
276         wmKeyMapItem *kmi;
277         
278 #ifdef __APPLE__
279         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
280         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
281 #endif
282
283         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
284         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
285         
286         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
287         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
288         
289         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
290         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
291         RNA_boolean_set(kmi->ptr, "reverse", 0);
292         
293         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
294         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
295         RNA_boolean_set(kmi->ptr, "reverse", 1);
296
297         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
298         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
299         RNA_boolean_set(kmi->ptr, "reverse", 0);
300         
301         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
302         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
303         RNA_boolean_set(kmi->ptr, "reverse", 1);
304
305         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
306         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
307         
308         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", 1);
309         WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0);
310         
311         /*
312         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
313         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
314         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
315         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
316         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
317         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
318
319
320         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
321         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
322         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
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         
327         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
328         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
329
330 #ifdef WITH_PYTHON
331         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
332         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
333         
334         //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
335         WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
336 #endif
337
338         /* report selection */
339         WM_keymap_add_item(keymap, "CONSOLE_OT_select_pick", SELECTMOUSE, KM_PRESS, 0, 0);
340         WM_keymap_add_item(keymap, "CONSOLE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
341         WM_keymap_add_item(keymap, "CONSOLE_OT_select_border", BKEY, KM_PRESS, 0, 0);
342
343         WM_keymap_add_item(keymap, "CONSOLE_OT_report_replay", RKEY, KM_PRESS, 0, 0);
344         WM_keymap_add_item(keymap, "CONSOLE_OT_report_delete", XKEY, KM_PRESS, 0, 0);
345         WM_keymap_add_item(keymap, "CONSOLE_OT_report_delete", DELKEY, KM_PRESS, 0, 0);
346         WM_keymap_add_item(keymap, "CONSOLE_OT_report_copy", CKEY, KM_PRESS, KM_CTRL, 0);
347         
348         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
349         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
350 #ifdef __APPLE__
351         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
352         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
353 #endif
354         
355         WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
356
357         RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, 0, 0)->ptr, "text", "\t"); /* fake tabs */
358         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
359 }
360
361 /****************** header region ******************/
362
363 /* add handlers, stuff you only do once or on area/region changes */
364 static void console_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
365 {
366         ED_region_header_init(ar);
367 }
368
369 static void console_header_area_draw(const bContext *C, ARegion *ar)
370 {
371         ED_region_header(C, ar);
372 }
373
374 static void console_main_area_listener(ScrArea *sa, wmNotifier *wmn)
375 {
376         SpaceConsole *sc= sa->spacedata.first;
377
378         /* context changes */
379         switch(wmn->category) {
380                 case NC_SPACE:
381                         if(wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
382                                 ED_area_tag_redraw(sa);
383                         }
384                         else if(wmn->data == ND_SPACE_CONSOLE_REPORT && sc->type==CONSOLE_TYPE_REPORT) {
385                                 /* redraw also but only for report view, could do less redraws by checking the type */
386                                 ED_area_tag_redraw(sa);
387                         }
388                         break;
389         }
390 }
391
392 /* only called once, from space/spacetypes.c */
393 void ED_spacetype_console(void)
394 {
395         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype console");
396         ARegionType *art;
397         
398         st->spaceid= SPACE_CONSOLE;
399         strncpy(st->name, "Console", BKE_ST_MAXNAME);
400         
401         st->new= console_new;
402         st->free= console_free;
403         st->init= console_init;
404         st->duplicate= console_duplicate;
405         st->operatortypes= console_operatortypes;
406         st->keymap= console_keymap;
407         st->dropboxes= console_dropboxes;
408         st->listener= console_main_area_listener;
409         
410         /* regions: main window */
411         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
412         art->regionid = RGN_TYPE_WINDOW;
413         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
414
415         art->init= console_main_area_init;
416         art->draw= console_main_area_draw;
417         
418         
419         
420
421         BLI_addhead(&st->regiontypes, art);
422         
423         /* regions: header */
424         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
425         art->regionid = RGN_TYPE_HEADER;
426         art->prefsizey= HEADERY;
427         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
428         
429         art->init= console_header_area_init;
430         art->draw= console_header_area_draw;
431         
432         BLI_addhead(&st->regiontypes, art);
433
434
435         BKE_spacetype_register(st);
436 }