diff options
author | Ian Thompson <quornian@googlemail.com> | 2008-07-15 11:04:31 +0400 |
---|---|---|
committer | Ian Thompson <quornian@googlemail.com> | 2008-07-15 11:04:31 +0400 |
commit | dbb61988fdaa6085912dee6ab8f5569e63ef88fb (patch) | |
tree | 2b1334417dbe2536851bed215777f89e416b6007 /source/blender/python | |
parent | 14c1ed08108df6178a55f6d7f818f19a926ae1df (diff) |
Any script can now register a unique key combination as part of its bpy header. For a supported space type, the user may press this shortcut to invoke the script.
Space types that are to support shortcuts like this should call BPY_menu_do_shortcut(...) from the event queue read method (See winqreadtextspace in drawtext.c for example)
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/BPY_extern.h | 2 | ||||
-rw-r--r-- | source/blender/python/BPY_interface.c | 34 | ||||
-rw-r--r-- | source/blender/python/BPY_menus.c | 35 | ||||
-rw-r--r-- | source/blender/python/BPY_menus.h | 1 |
4 files changed, 64 insertions, 8 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index f6a27b80733..3a826d49542 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -91,6 +91,8 @@ extern "C" { int BPY_txt_do_python_Text( struct Text *text ); int BPY_menu_do_python( short menutype, int event ); + int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short modifiers ); + int BPY_menu_invoke( struct BPyMenu *pym, short menutype ); void BPY_run_python_script( char *filename ); int BPY_run_script(struct Script *script); void BPY_free_compiled_text( struct Text *text ); diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c index 360c8fd7f04..e9600e9369f 100644 --- a/source/blender/python/BPY_interface.c +++ b/source/blender/python/BPY_interface.c @@ -972,8 +972,38 @@ int BPY_run_script(Script *script) *****************************************************************************/ int BPY_menu_do_python( short menutype, int event ) { - char *argstr = NULL; BPyMenu *pym; + pym = BPyMenu_GetEntry( menutype, ( short ) event ); + return BPY_menu_invoke( pym, menutype ); +} + +/**************************************************************************** +* Description: This function executes the script by its shortcut. +* Notes: It is called by the ui code in src/???.c when a user presses an +* unassigned key combination. Scripts are searched in the BPyMenuTable, +* using the given menutype and event values to know which one to invoke. +*****************************************************************************/ +int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short qual ) +{ + BPyMenu *pym; + pym = BPyMenu_GetEntry( menutype, 0 ); + + while ( pym ) { + if ( pym->key && pym->key == key && pym->qual == qual ) { + return BPY_menu_invoke( pym, menutype ); + } + pym = pym->next; + } + + return 0; +} + +/**************************************************************************** +* Description: This function executes the script described by a menu item. +*****************************************************************************/ +int BPY_menu_invoke( BPyMenu *pym, short menutype ) +{ + char *argstr = NULL; BPySubMenu *pysm; char scriptname[21]; Script *script = NULL; @@ -981,8 +1011,6 @@ int BPY_menu_do_python( short menutype, int event ) PyGILState_STATE gilstate; char filestr[FILE_MAX]; - pym = BPyMenu_GetEntry( menutype, ( short ) event ); - if( !pym ) return 0; diff --git a/source/blender/python/BPY_menus.c b/source/blender/python/BPY_menus.c index 08691973a92..69b50e5c47a 100644 --- a/source/blender/python/BPY_menus.c +++ b/source/blender/python/BPY_menus.c @@ -42,6 +42,7 @@ #endif #include "BKE_global.h" #include "BKE_utildefines.h" +#include "BIF_keyval.h" #include "BLI_blenlib.h" #include "MEM_guardedalloc.h" #include "DNA_userdef_types.h" /* for U.pythondir */ @@ -333,6 +334,23 @@ static void bpymenu_set_tooltip( BPyMenu * pymenu, char *tip ) return; } +static void bpymenu_set_shortcut( BPyMenu * pymenu, char *combi ) +{ + unsigned short key, qual; + + if( !pymenu ) + return; + + if (!decode_key_string(combi, &key, &qual)) { + return; /* TODO: Print some error */ + } + + pymenu->key = key; + pymenu->qual = qual; + + return; +} + /* bpymenu_AddEntry: * try to find an existing pymenu entry with the given type and name; * if found, update it with new info, otherwise create a new one and fill it. @@ -693,6 +711,7 @@ void BPyMenu_PrintAllEntries( void ) * # Blender: <code>short int</code> (minimal Blender version) * # Group: 'group name' (defines menu) * # Submenu: 'submenu name' related_1word_arg + * # Shortcut: Modifier+Key (optional shortcut combination for supported groups) * # Tooltip: 'tooltip for the menu' * # \"\"\" * @@ -801,13 +820,19 @@ static int bpymenu_ParseFile(FILE *file, char *fname, int is_userdir) if ((matches == 3) && (strstr(head, "Submenu:") != NULL)) { bpymenu_AddSubEntry(scriptMenu, middle, tail); } else { - /* Tooltip: 'tooltip for the menu */ + /* Shortcut: 'key+combination' */ matches = sscanf(line, "%[^']'%[^']'%c", head, middle, tail); - if ((matches == 3) && ((strstr(head, "Tooltip:") != NULL) || - (strstr(head, "Tip:") != NULL))) { - bpymenu_set_tooltip(scriptMenu, middle); + if ((matches == 3) && (strstr(head, "Shortcut:") != NULL)) { + bpymenu_set_shortcut(scriptMenu, middle); + } else { + /* Tooltip: 'tooltip for the menu */ + matches = sscanf(line, "%[^']'%[^']'%c", head, middle, tail); + if ((matches == 3) && ((strstr(head, "Tooltip:") != NULL) || + (strstr(head, "Tip:") != NULL))) { + bpymenu_set_tooltip(scriptMenu, middle); + } + parser_state = 0; } - parser_state = 0; } break; diff --git a/source/blender/python/BPY_menus.h b/source/blender/python/BPY_menus.h index e8bca09d50e..576d7b8dcd6 100644 --- a/source/blender/python/BPY_menus.h +++ b/source/blender/python/BPY_menus.h @@ -59,6 +59,7 @@ typedef struct BPyMenu { char *name; char *filename; char *tooltip; + unsigned short key, qual; /* Registered shortcut key */ short version; /* Blender version */ int dir; /* 0: default, 1: U.pythondir */ struct BPySubMenu *submenus; |