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