image format arg parsing for creator.c
[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 resizing 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= ar->v2d.cur.ymax - ar->v2d.cur.ymin;
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_MAXDIR+FILE_MAXFILE+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         /* 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_history_cycle);
256         WM_operatortype_append(CONSOLE_OT_copy);
257         WM_operatortype_append(CONSOLE_OT_paste);
258         WM_operatortype_append(CONSOLE_OT_select_set);
259 }
260
261 static void console_keymap(struct wmKeyConfig *keyconf)
262 {
263         wmKeyMap *keymap= WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
264         wmKeyMapItem *kmi;
265         
266 #ifdef __APPLE__
267         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
268         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
269 #endif
270
271         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
272         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
273         
274         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
275         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
276         
277         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
278         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
279         RNA_boolean_set(kmi->ptr, "reverse", 0);
280         
281         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
282         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
283         RNA_boolean_set(kmi->ptr, "reverse", 1);
284
285         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
286         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
287         RNA_boolean_set(kmi->ptr, "reverse", 0);
288         
289         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
290         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
291         RNA_boolean_set(kmi->ptr, "reverse", 1);
292
293         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
294         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
295         
296         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", 1);
297         WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0);
298         
299         /*
300         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
301         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
302         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
303         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
304         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
305         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
306
307
308         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
309         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DKEY, KM_PRESS, KM_CTRL, 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", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
312         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
313         */
314         
315         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
316         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
317         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] */
318
319 #ifdef WITH_PYTHON
320         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
321         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
322         
323         //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
324         WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
325 #endif
326
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, 0, 0)->ptr, "text", "\t"); /* fake tabs */
337         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
338 }
339
340 /****************** header region ******************/
341
342 /* add handlers, stuff you only do once or on area/region changes */
343 static void console_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
344 {
345         ED_region_header_init(ar);
346 }
347
348 static void console_header_area_draw(const bContext *C, ARegion *ar)
349 {
350         ED_region_header(C, ar);
351 }
352
353 static void console_main_area_listener(ARegion *ar, wmNotifier *wmn)
354 {
355         // SpaceInfo *sinfo= sa->spacedata.first;
356
357         /* context changes */
358         switch(wmn->category) {
359                 case NC_SPACE:
360                         if(wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
361                                 ED_region_tag_redraw(ar);
362                         }
363                         break;
364         }
365 }
366
367 /* only called once, from space/spacetypes.c */
368 void ED_spacetype_console(void)
369 {
370         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype console");
371         ARegionType *art;
372         
373         st->spaceid= SPACE_CONSOLE;
374         strncpy(st->name, "Console", BKE_ST_MAXNAME);
375         
376         st->new= console_new;
377         st->free= console_free;
378         st->init= console_init;
379         st->duplicate= console_duplicate;
380         st->operatortypes= console_operatortypes;
381         st->keymap= console_keymap;
382         st->dropboxes= console_dropboxes;
383         
384         /* regions: main window */
385         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
386         art->regionid = RGN_TYPE_WINDOW;
387         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
388
389         art->init= console_main_area_init;
390         art->draw= console_main_area_draw;
391         art->listener= console_main_area_listener;
392         
393         
394
395         BLI_addhead(&st->regiontypes, art);
396         
397         /* regions: header */
398         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
399         art->regionid = RGN_TYPE_HEADER;
400         art->prefsizey= HEADERY;
401         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
402         
403         art->init= console_header_area_init;
404         art->draw= console_header_area_draw;
405         
406         BLI_addhead(&st->regiontypes, art);
407
408
409         BKE_spacetype_register(st);
410 }