Blender  V2.59
space_console.c
Go to the documentation of this file.
00001 /*
00002  * $Id: space_console.c 36271 2011-04-21 13:11:51Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  * 
00020  * Contributor(s): Campbell Barton
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00029 #include <string.h>
00030 #include <stdio.h>
00031 
00032 #ifdef WIN32
00033 #include "BLI_winstuff.h"
00034 #endif
00035 
00036 #include "MEM_guardedalloc.h"
00037 
00038 #include "BLI_blenlib.h"
00039 #include "BLI_math.h"
00040 #include "BLI_utildefines.h"
00041 
00042 #include "BKE_context.h"
00043 #include "BKE_screen.h"
00044 #include "BKE_idcode.h"
00045 
00046 #include "ED_space_api.h"
00047 #include "ED_screen.h"
00048 
00049 #include "BIF_gl.h"
00050 
00051 #include "RNA_access.h"
00052 
00053 #include "WM_api.h"
00054 #include "WM_types.h"
00055 
00056 #include "UI_resources.h"
00057 #include "UI_view2d.h"
00058 
00059 #include "console_intern.h"     // own include
00060 
00061 /* ******************** default callbacks for console space ***************** */
00062 
00063 static SpaceLink *console_new(const bContext *UNUSED(C))
00064 {
00065         ARegion *ar;
00066         SpaceConsole *sconsole;
00067         
00068         sconsole= MEM_callocN(sizeof(SpaceConsole), "initconsole");
00069         sconsole->spacetype= SPACE_CONSOLE;
00070         
00071         sconsole->lheight=      14;
00072         
00073         /* header */
00074         ar= MEM_callocN(sizeof(ARegion), "header for console");
00075         
00076         BLI_addtail(&sconsole->regionbase, ar);
00077         ar->regiontype= RGN_TYPE_HEADER;
00078         ar->alignment= RGN_ALIGN_BOTTOM;
00079         
00080         
00081         /* main area */
00082         ar= MEM_callocN(sizeof(ARegion), "main area for text");
00083         
00084         BLI_addtail(&sconsole->regionbase, ar);
00085         ar->regiontype= RGN_TYPE_WINDOW;
00086         
00087         /* keep in sync with info */
00088         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
00089         ar->v2d.align |= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
00090         ar->v2d.keepofs |= V2D_LOCKOFS_X;
00091         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
00092         ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
00093         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
00094 
00095         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
00096         //ar->v2d.keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
00097 
00098         return (SpaceLink *)sconsole;
00099 }
00100 
00101 /* not spacelink itself */
00102 static void console_free(SpaceLink *sl)
00103 {
00104         SpaceConsole *sc= (SpaceConsole*) sl;
00105         
00106         while(sc->scrollback.first)
00107                 console_scrollback_free(sc, sc->scrollback.first);
00108         
00109         while(sc->history.first)
00110                 console_history_free(sc, sc->history.first);
00111 }
00112 
00113 
00114 /* spacetype; init callback */
00115 static void console_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
00116 {
00117 
00118 }
00119 
00120 static SpaceLink *console_duplicate(SpaceLink *sl)
00121 {
00122         SpaceConsole *sconsolen= MEM_dupallocN(sl);
00123         
00124         /* clear or remove stuff from old */
00125         
00126         /* TODO - duplicate?, then we also need to duplicate the py namespace */
00127         sconsolen->scrollback.first= sconsolen->scrollback.last= NULL;
00128         sconsolen->history.first= sconsolen->history.last= NULL;
00129         
00130         return (SpaceLink *)sconsolen;
00131 }
00132 
00133 
00134 
00135 /* add handlers, stuff you only do once or on area/region changes */
00136 static void console_main_area_init(wmWindowManager *wm, ARegion *ar)
00137 {
00138         wmKeyMap *keymap;
00139         ListBase *lb;
00140 
00141         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
00142 
00143         /* own keymap */
00144         keymap= WM_keymap_find(wm->defaultconf, "Console", SPACE_CONSOLE, 0);
00145         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
00146         
00147         /* add drop boxes */
00148         lb= WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
00149         
00150         WM_event_add_dropbox_handler(&ar->handlers, lb);
00151 }
00152 
00153 
00154 /* ************* dropboxes ************* */
00155 
00156 static int id_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
00157 {
00158 //      SpaceConsole *sc= CTX_wm_space_console(C);
00159         if(drag->type==WM_DRAG_ID)
00160                 return 1;
00161         return 0;
00162 }
00163 
00164 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
00165 {
00166         char text[64];
00167         ID *id= drag->poin;
00168 
00169         snprintf(text, sizeof(text), "bpy.data.%s['%s']", BKE_idcode_to_name_plural(GS(id->name)), id->name+2); 
00170 
00171         /* copy drag path to properties */
00172         RNA_string_set(drop->ptr, "text", text);
00173 }
00174 
00175 static int path_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
00176 {
00177 //    SpaceConsole *sc= CTX_wm_space_console(C);
00178         if(drag->type==WM_DRAG_PATH)
00179                 return 1;
00180         return 0;
00181 }
00182 
00183 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
00184 {
00185         char pathname[FILE_MAXDIR+FILE_MAXFILE+2];
00186         snprintf(pathname, sizeof(pathname), "\"%s\"", drag->path);
00187         RNA_string_set(drop->ptr, "text", pathname);
00188 }
00189 
00190 
00191 /* this region dropbox definition */
00192 static void console_dropboxes(void)
00193 {
00194         ListBase *lb= WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
00195         
00196         WM_dropbox_add(lb, "CONSOLE_OT_insert", id_drop_poll, id_drop_copy);
00197         WM_dropbox_add(lb, "CONSOLE_OT_insert", path_drop_poll, path_drop_copy);
00198 }
00199 
00200 /* ************* end drop *********** */
00201 
00202 static void console_main_area_draw(const bContext *C, ARegion *ar)
00203 {
00204         /* draw entirely, view changes should be handled here */
00205         SpaceConsole *sc= CTX_wm_space_console(C);
00206         View2D *v2d= &ar->v2d;
00207         View2DScrollers *scrollers;
00208 
00209         if(sc->scrollback.first==NULL)
00210                 WM_operator_name_call((bContext *)C, "CONSOLE_OT_banner", WM_OP_EXEC_DEFAULT, NULL);
00211 
00212         /* clear and setup matrix */
00213         UI_ThemeClearColor(TH_BACK);
00214         glClear(GL_COLOR_BUFFER_BIT);
00215 
00216         /* worlks best with no view2d matrix set */
00217         UI_view2d_view_ortho(v2d);
00218 
00219         /* data... */
00220 
00221         console_history_verify(C); /* make sure we have some command line */
00222         console_textview_main(sc, ar);
00223         
00224         /* reset view matrix */
00225         UI_view2d_view_restore(C);
00226         
00227         /* scrollers */
00228         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_GRID_CLAMP);
00229         UI_view2d_scrollers_draw(C, v2d, scrollers);
00230         UI_view2d_scrollers_free(scrollers);
00231 }
00232 
00233 static void console_operatortypes(void)
00234 {
00235         /* console_ops.c */
00236         WM_operatortype_append(CONSOLE_OT_move);
00237         WM_operatortype_append(CONSOLE_OT_delete);
00238         WM_operatortype_append(CONSOLE_OT_insert);
00239         
00240         /* for use by python only */
00241         WM_operatortype_append(CONSOLE_OT_history_append); 
00242         WM_operatortype_append(CONSOLE_OT_scrollback_append);
00243         
00244         WM_operatortype_append(CONSOLE_OT_clear); 
00245         WM_operatortype_append(CONSOLE_OT_history_cycle);
00246         WM_operatortype_append(CONSOLE_OT_copy);
00247         WM_operatortype_append(CONSOLE_OT_paste);
00248         WM_operatortype_append(CONSOLE_OT_select_set);
00249 }
00250 
00251 static void console_keymap(struct wmKeyConfig *keyconf)
00252 {
00253         wmKeyMap *keymap= WM_keymap_find(keyconf, "Console", SPACE_CONSOLE, 0);
00254         wmKeyMapItem *kmi;
00255         
00256 #ifdef __APPLE__
00257         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
00258         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
00259 #endif
00260 
00261         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
00262         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
00263         
00264         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
00265         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
00266         
00267         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
00268         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
00269         RNA_boolean_set(kmi->ptr, "reverse", 0);
00270         
00271         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
00272         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
00273         RNA_boolean_set(kmi->ptr, "reverse", 1);
00274 
00275         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
00276         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
00277         RNA_boolean_set(kmi->ptr, "reverse", 0);
00278         
00279         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
00280         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
00281         RNA_boolean_set(kmi->ptr, "reverse", 1);
00282 
00283         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
00284         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
00285         
00286         RNA_boolean_set(WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "reverse", 1);
00287         WM_keymap_add_item(keymap, "CONSOLE_OT_history_cycle", DOWNARROWKEY, KM_PRESS, 0, 0);
00288         
00289         /*
00290         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
00291         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
00292         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
00293         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
00294         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
00295         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
00296 
00297 
00298         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
00299         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
00300         //RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
00301         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
00302         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
00303         */
00304         
00305         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
00306         RNA_enum_set(WM_keymap_add_item(keymap, "CONSOLE_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
00307         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] */
00308 
00309 #ifdef WITH_PYTHON
00310         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", RETKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
00311         WM_keymap_add_item(keymap, "CONSOLE_OT_execute", PADENTER, KM_PRESS, 0, 0);
00312         
00313         //WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", TABKEY, KM_PRESS, 0, 0); /* python operator - space_text.py */
00314         WM_keymap_add_item(keymap, "CONSOLE_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* python operator - space_text.py */
00315 #endif
00316 
00317         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
00318         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
00319 #ifdef __APPLE__
00320         WM_keymap_add_item(keymap, "CONSOLE_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
00321         WM_keymap_add_item(keymap, "CONSOLE_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
00322 #endif
00323         
00324         WM_keymap_add_item(keymap, "CONSOLE_OT_select_set", LEFTMOUSE, KM_PRESS, 0, 0);
00325 
00326         RNA_string_set(WM_keymap_add_item(keymap, "CONSOLE_OT_insert", TABKEY, KM_PRESS, 0, 0)->ptr, "text", "\t"); /* fake tabs */
00327         WM_keymap_add_item(keymap, "CONSOLE_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
00328 }
00329 
00330 /****************** header region ******************/
00331 
00332 /* add handlers, stuff you only do once or on area/region changes */
00333 static void console_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
00334 {
00335         ED_region_header_init(ar);
00336 }
00337 
00338 static void console_header_area_draw(const bContext *C, ARegion *ar)
00339 {
00340         ED_region_header(C, ar);
00341 }
00342 
00343 static void console_main_area_listener(ARegion *ar, wmNotifier *wmn)
00344 {
00345         // SpaceInfo *sinfo= sa->spacedata.first;
00346 
00347         /* context changes */
00348         switch(wmn->category) {
00349                 case NC_SPACE:
00350                         if(wmn->data == ND_SPACE_CONSOLE) { /* generic redraw request */
00351                                 ED_region_tag_redraw(ar);
00352                         }
00353                         break;
00354         }
00355 }
00356 
00357 /* only called once, from space/spacetypes.c */
00358 void ED_spacetype_console(void)
00359 {
00360         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype console");
00361         ARegionType *art;
00362         
00363         st->spaceid= SPACE_CONSOLE;
00364         strncpy(st->name, "Console", BKE_ST_MAXNAME);
00365         
00366         st->new= console_new;
00367         st->free= console_free;
00368         st->init= console_init;
00369         st->duplicate= console_duplicate;
00370         st->operatortypes= console_operatortypes;
00371         st->keymap= console_keymap;
00372         st->dropboxes= console_dropboxes;
00373         
00374         /* regions: main window */
00375         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
00376         art->regionid = RGN_TYPE_WINDOW;
00377         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
00378 
00379         art->init= console_main_area_init;
00380         art->draw= console_main_area_draw;
00381         art->listener= console_main_area_listener;
00382         
00383         
00384 
00385         BLI_addhead(&st->regiontypes, art);
00386         
00387         /* regions: header */
00388         art= MEM_callocN(sizeof(ARegionType), "spacetype console region");
00389         art->regionid = RGN_TYPE_HEADER;
00390         art->prefsizey= HEADERY;
00391         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
00392         
00393         art->init= console_header_area_init;
00394         art->draw= console_header_area_draw;
00395         
00396         BLI_addhead(&st->regiontypes, art);
00397 
00398 
00399         BKE_spacetype_register(st);
00400 }