* all plugins commands are bindable

support detachable menus in submenus of plugins menus
user_shortcuts_init() and user_shortcuts_save() are moved to not the most convenient spot, as init has to happen after plugins init and before menus creation
This commit is contained in:
Garux 2019-03-13 19:45:51 +03:00
parent 51ee1dcccb
commit 502c0f8bc1
5 changed files with 121 additions and 122 deletions

View File

@ -531,19 +531,6 @@ void remove_local_pid(){
remove( g_pidGameFile.c_str() ); remove( g_pidGameFile.c_str() );
} }
void user_shortcuts_init(){
StringOutputStream path( 256 );
path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
LoadCommandMap( path.c_str() );
SaveCommandMap( path.c_str() );
}
void user_shortcuts_save(){
StringOutputStream path( 256 );
path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
SaveCommandMap( path.c_str() );
}
void add_local_rc_files(){ void add_local_rc_files(){
{ {
StringOutputStream path( 512 ); StringOutputStream path( 512 );
@ -675,7 +662,7 @@ int main( int argc, char* argv[] ){
global_accel_init(); global_accel_init();
user_shortcuts_init(); // user_shortcuts_init();
g_pParentWnd = 0; g_pParentWnd = 0;
g_pParentWnd = new MainFrame(); g_pParentWnd = new MainFrame();
@ -710,7 +697,7 @@ int main( int argc, char* argv[] ){
delete g_pParentWnd; delete g_pParentWnd;
user_shortcuts_save(); // user_shortcuts_save();
global_accel_destroy(); global_accel_destroy();

View File

@ -2948,6 +2948,20 @@ gboolean toolbar_redirect_scroll( GtkWidget* widget, GdkEventScroll* event, gpoi
} }
void user_shortcuts_init(){
StringOutputStream path( 256 );
path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
LoadCommandMap( path.c_str() );
SaveCommandMap( path.c_str() );
}
void user_shortcuts_save(){
StringOutputStream path( 256 );
path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
SaveCommandMap( path.c_str() );
}
void MainFrame::Create(){ void MainFrame::Create(){
GtkWindow* window = GTK_WINDOW( gtk_window_new( GTK_WINDOW_TOPLEVEL ) ); GtkWindow* window = GTK_WINDOW( gtk_window_new( GTK_WINDOW_TOPLEVEL ) );
@ -2976,8 +2990,10 @@ void MainFrame::Create(){
#endif #endif
g_MainWindowActive.connect( window ); g_MainWindowActive.connect( window );
/* GlobalCommands_insert plugins commands */
GetPlugInMgr().Init( GTK_WIDGET( window ) ); GetPlugInMgr().Init( GTK_WIDGET( window ) );
/* then load shortcuts cfg */
user_shortcuts_init();
GtkWidget* vbox = gtk_vbox_new( FALSE, 0 ); GtkWidget* vbox = gtk_vbox_new( FALSE, 0 );
gtk_container_add( GTK_CONTAINER( window ), vbox ); gtk_container_add( GTK_CONTAINER( window ), vbox );
@ -3360,6 +3376,8 @@ void MainFrame::Shutdown(){
// destroying group-dialog last because it may contain texture-browser // destroying group-dialog last because it may contain texture-browser
GroupDialog_destroyWindow(); GroupDialog_destroyWindow();
user_shortcuts_save();
} }
void MainFrame::RedrawStatusText(){ void MainFrame::RedrawStatusText(){

View File

@ -38,6 +38,9 @@
#include "modulesystem.h" #include "modulesystem.h"
#include "stream/stringstream.h"
#include "commands.h"
#include <list> #include <list>
/* plugin manager --------------------------------------- */ /* plugin manager --------------------------------------- */
@ -47,7 +50,20 @@ CopiedString m_menu_name;
const _QERPluginTable *mpTable; const _QERPluginTable *mpTable;
std::list<CopiedString> m_CommandStrings; std::list<CopiedString> m_CommandStrings;
std::list<CopiedString> m_CommandTitleStrings; std::list<CopiedString> m_CommandTitleStrings;
std::list<std::size_t> m_CommandIDs;
std::list<CopiedString> m_globalCommandNames;
class PluginCaller
{
CPluginSlot* slot;
const char* name;
public:
PluginCaller( CPluginSlot* slot_, const char* name_ ) : slot( slot_ ), name( name_ ){
}
void operator()(){
slot->Dispatch( name );
}
};
std::list<PluginCaller> m_callbacks;
public: public:
/*! /*!
@ -64,9 +80,7 @@ const char* getMenuName();
std::size_t getCommandCount(); std::size_t getCommandCount();
const char* getCommand( std::size_t n ); const char* getCommand( std::size_t n );
const char* getCommandTitle( std::size_t n ); const char* getCommandTitle( std::size_t n );
void addMenuID( std::size_t n ); const char* getGlobalCommand( std::size_t n );
bool ownsCommandID( std::size_t n );
}; };
CPluginSlot::CPluginSlot( GtkWidget* main_window, const char* name, const _QERPluginTable& table ){ CPluginSlot::CPluginSlot( GtkWidget* main_window, const char* name, const _QERPluginTable& table ){
@ -79,23 +93,50 @@ CPluginSlot::CPluginSlot( GtkWidget* main_window, const char* name, const _QERPl
StringTokeniser commandTokeniser( commands, ",;" ); StringTokeniser commandTokeniser( commands, ",;" );
StringTokeniser titleTokeniser( titles, ",;" ); StringTokeniser titleTokeniser( titles, ",;" );
while ( 1 ) {
const char* cmdToken = commandTokeniser.getToken(); const char* cmdToken = commandTokeniser.getToken();
const char *titleToken = titleTokeniser.getToken(); const char *titleToken = titleTokeniser.getToken();
while ( !string_empty( cmdToken ) ) if( string_empty( cmdToken ) )
{ break;
if ( string_empty( titleToken ) ) {
m_CommandStrings.push_back( cmdToken ); m_CommandStrings.push_back( cmdToken );
if ( string_empty( titleToken ) )
m_CommandTitleStrings.push_back( cmdToken ); m_CommandTitleStrings.push_back( cmdToken );
cmdToken = commandTokeniser.getToken();
titleToken = "";
}
else else
{
m_CommandStrings.push_back( cmdToken );
m_CommandTitleStrings.push_back( titleToken ); m_CommandTitleStrings.push_back( titleToken );
cmdToken = commandTokeniser.getToken();
titleToken = titleTokeniser.getToken(); m_callbacks.emplace_back( PluginCaller( this, m_CommandStrings.back().c_str() ) );
StringOutputStream str( 64 );
{
if( !string_equal_nocase_n( cmdToken, getMenuName(), string_length( getMenuName() ) ) ){ //plugin name is not part of command name
str << getMenuName() << "::";
} }
/* remove spaces + camelcasify */
const char* p = cmdToken;
bool wasspace = false;
while( *p ){
if( *p == ' ' ){
wasspace = true;
}
else if( wasspace ){
wasspace = false;
str << static_cast<char>( std::toupper( *p ) );
}
else{
str << *p;
}
++p;
}
/* del trailing periods */
char* pp = &( *( str.end() - 1 ) );
while( *pp == '.' ){
*pp = '\0';
--pp;
}
*str.c_str() = std::tolower( *str.c_str() ); //put to the end of the list this way
}
m_globalCommandNames.emplace_back( str.c_str() );
if ( !plugin_menu_special( cmdToken ) ) //ain't special
GlobalCommands_insert( str.c_str(), makeCallback( m_callbacks.back() ) );
} }
mpTable->m_pfnQERPlug_Init( 0, (void*)main_window ); mpTable->m_pfnQERPlug_Init( 0, (void*)main_window );
} }
@ -122,18 +163,11 @@ const char* CPluginSlot::getCommandTitle( std::size_t n ){
return ( *i ).c_str(); return ( *i ).c_str();
} }
void CPluginSlot::addMenuID( std::size_t n ){ const char* CPluginSlot::getGlobalCommand( std::size_t n ){
m_CommandIDs.push_back( n ); std::list<CopiedString>::iterator i = m_globalCommandNames.begin();
} while ( n-- != 0 )
++i;
bool CPluginSlot::ownsCommandID( std::size_t n ){ return ( *i ).c_str();
for ( std::list<std::size_t>::iterator i = m_CommandIDs.begin(); i != m_CommandIDs.end(); ++i )
{
if ( *i == n ) {
return true;
}
}
return false;
} }
void CPluginSlot::Dispatch( const char *p ){ void CPluginSlot::Dispatch( const char *p ){
@ -154,7 +188,6 @@ void AddPluginSlot( GtkWidget* main_window, const char* name, const _QERPluginTa
} }
void PopulateMenu( PluginsVisitor& menu ); void PopulateMenu( PluginsVisitor& menu );
bool Dispatch( std::size_t n, const char* p );
}; };
CPluginSlots::~CPluginSlots(){ CPluginSlots::~CPluginSlots(){
@ -174,19 +207,6 @@ void CPluginSlots::PopulateMenu( PluginsVisitor& menu ){
} }
} }
bool CPluginSlots::Dispatch( std::size_t n, const char* p ){
std::list<CPluginSlot *>::iterator iPlug;
for ( iPlug = mSlots.begin(); iPlug != mSlots.end(); ++iPlug )
{
CPluginSlot *pPlug = *iPlug;
if ( pPlug->ownsCommandID( n ) ) {
pPlug->Dispatch( p );
return true;
}
}
return false;
}
CPluginSlots g_plugin_slots; CPluginSlots g_plugin_slots;
@ -208,18 +228,12 @@ public:
} }
#include "pluginmanager.h"
CPlugInManager g_PlugInMgr; CPlugInManager g_PlugInMgr;
CPlugInManager& GetPlugInMgr(){ CPlugInManager& GetPlugInMgr(){
return g_PlugInMgr; return g_PlugInMgr;
} }
void CPlugInManager::Dispatch( std::size_t n, const char * p ){
g_plugin_slots.Dispatch( n, p );
}
void CPlugInManager::Init( GtkWidget* main_window ){ void CPlugInManager::Init( GtkWidget* main_window ){
FillPluginSlots( g_plugin_slots, main_window ); FillPluginSlots( g_plugin_slots, main_window );
} }

View File

@ -41,8 +41,7 @@ virtual const char* getMenuName() = 0;
virtual std::size_t getCommandCount() = 0; virtual std::size_t getCommandCount() = 0;
virtual const char* getCommand( std::size_t ) = 0; virtual const char* getCommand( std::size_t ) = 0;
virtual const char* getCommandTitle( std::size_t ) = 0; virtual const char* getCommandTitle( std::size_t ) = 0;
virtual void addMenuID( std::size_t ) = 0; virtual const char* getGlobalCommand( std::size_t ) = 0;
virtual bool ownsCommandID( std::size_t n ) = 0;
}; };
class PluginsVisitor class PluginsVisitor
@ -54,7 +53,6 @@ virtual void visit( IPlugIn& plugin ) = 0;
class CPlugInManager class CPlugInManager
{ {
public: public:
void Dispatch( std::size_t n, const char *p );
void Init( GtkWidget* main_window ); void Init( GtkWidget* main_window );
void constructMenu( PluginsVisitor& menu ); void constructMenu( PluginsVisitor& menu );
void Shutdown(); void Shutdown();
@ -62,4 +60,19 @@ void Shutdown();
CPlugInManager& GetPlugInMgr(); CPlugInManager& GetPlugInMgr();
inline bool plugin_submenu_in( const char* text ){
return text[0] == '>' && text[1] == '\0';
}
inline bool plugin_submenu_out( const char* text ){
return text[0] == '<' && text[1] == '\0';
}
inline bool plugin_menu_separator( const char* text ){
return text[0] == '-' && text[1] == '\0';
}
inline bool plugin_menu_special( const char* text ){
return plugin_menu_separator( text )
|| plugin_submenu_in( text )
|| plugin_submenu_out( text );
}
#endif #endif

View File

@ -33,61 +33,45 @@
#include "mainframe.h" #include "mainframe.h"
#include "preferences.h" #include "preferences.h"
#include "gtkmisc.h"
int m_nNextPlugInID = 0;
void plugin_activated( GtkWidget* widget, gpointer data ){
const char* str = (const char*)g_object_get_data( G_OBJECT( widget ),"command" );
GetPlugInMgr().Dispatch( gpointer_to_int( data ), str );
}
#include <stack> #include <stack>
typedef std::stack<GtkWidget*> WidgetStack; typedef std::stack<GtkMenu*> MenuStack;
void PlugInMenu_Add( GtkMenu* plugin_menu, IPlugIn* pPlugIn ){ void PlugInMenu_Add( GtkMenu* plugin_menu, IPlugIn* pPlugIn ){
GtkWidget *menu, *item, *parent, *subMenu; GtkMenu *menu;
const char *menuText, *menuCommand; const char *menuText;
WidgetStack menuStack; MenuStack menuStack;
parent = gtk_menu_item_new_with_label( pPlugIn->getMenuName() ); menu = create_sub_menu_with_mnemonic( plugin_menu, pPlugIn->getMenuName() );
gtk_widget_show( parent ); if ( g_Layout_enableDetachableMenus.m_value ) {
gtk_container_add( GTK_CONTAINER( plugin_menu ), parent ); menu_tearoff( menu );
}
std::size_t nCount = pPlugIn->getCommandCount(); std::size_t nCount = pPlugIn->getCommandCount();
if ( nCount > 0 ) { {
menu = gtk_menu_new();
if ( g_Layout_enableDetachableMenus.m_value ) {
menu_tearoff( GTK_MENU( menu ) );
}
while ( nCount > 0 ) while ( nCount > 0 )
{ {
menuText = pPlugIn->getCommandTitle( --nCount ); menuText = pPlugIn->getCommandTitle( --nCount );
menuCommand = pPlugIn->getCommand( nCount );
if ( menuText != 0 && strlen( menuText ) > 0 ) { if ( menuText != 0 && strlen( menuText ) > 0 ) {
if ( !strcmp( menuText, "-" ) ) { if ( plugin_menu_separator( menuText ) ) {
item = gtk_menu_item_new(); menu_separator( menu );
gtk_widget_set_sensitive( item, FALSE );
} }
else if ( !strcmp( menuText, ">" ) ) { else if ( plugin_submenu_in( menuText ) ) {
menuText = pPlugIn->getCommandTitle( --nCount ); menuText = pPlugIn->getCommandTitle( --nCount );
menuCommand = pPlugIn->getCommand( nCount ); if ( plugin_menu_special( menuText ) ) {
if ( !strcmp( menuText, "-" ) || !strcmp( menuText, ">" ) || !strcmp( menuText, "<" ) ) {
globalErrorStream() << pPlugIn->getMenuName() << " Invalid title (" << menuText << ") for submenu.\n"; globalErrorStream() << pPlugIn->getMenuName() << " Invalid title (" << menuText << ") for submenu.\n";
continue; continue;
} }
item = gtk_menu_item_new_with_label( menuText );
gtk_widget_show( item );
gtk_container_add( GTK_CONTAINER( menu ), item );
subMenu = gtk_menu_new();
gtk_menu_item_set_submenu( GTK_MENU_ITEM( item ), subMenu );
menuStack.push( menu ); menuStack.push( menu );
menu = subMenu; menu = create_sub_menu_with_mnemonic( menu, menuText );
if ( g_Layout_enableDetachableMenus.m_value ) {
menu_tearoff( menu );
}
continue; continue;
} }
else if ( !strcmp( menuText, "<" ) ) { else if ( plugin_submenu_out( menuText ) ) {
if ( !menuStack.empty() ) { if ( !menuStack.empty() ) {
menu = menuStack.top(); menu = menuStack.top();
menuStack.pop(); menuStack.pop();
@ -100,28 +84,13 @@ void PlugInMenu_Add( GtkMenu* plugin_menu, IPlugIn* pPlugIn ){
} }
else else
{ {
item = gtk_menu_item_new_with_label( menuText ); create_menu_item_with_mnemonic( menu, menuText, pPlugIn->getGlobalCommand( nCount ) );
g_object_set_data( G_OBJECT( item ),"command", const_cast<gpointer>( static_cast<const void*>( menuCommand ) ) );
g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( plugin_activated ), gint_to_pointer( m_nNextPlugInID ) );
} }
gtk_widget_show( item );
gtk_container_add( GTK_CONTAINER( menu ), item );
pPlugIn->addMenuID( m_nNextPlugInID++ );
} }
} }
if ( !menuStack.empty() ) { if ( !menuStack.empty() ) {
std::size_t size = menuStack.size(); globalErrorStream() << pPlugIn->getMenuName() << " mismatched > <. " << Unsigned( menuStack.size() ) << " submenu(s) not closed.\n";
if ( size != 0 ) {
globalErrorStream() << pPlugIn->getMenuName() << " mismatched > <. " << Unsigned( size ) << " submenu(s) not closed.\n";
} }
for ( std::size_t i = 0; i < ( size - 1 ); i++ )
{
menuStack.pop();
}
menu = menuStack.top();
menuStack.pop();
}
gtk_menu_item_set_submenu( GTK_MENU_ITEM( parent ), menu );
} }
} }
@ -145,8 +114,6 @@ public:
} }
void PluginsMenu_clear(){ void PluginsMenu_clear(){
m_nNextPlugInID = 0;
GList* lst = g_list_find( gtk_container_children( GTK_CONTAINER( g_plugins_menu ) ), GTK_WIDGET( g_plugins_menu_separator ) ); GList* lst = g_list_find( gtk_container_children( GTK_CONTAINER( g_plugins_menu ) ), GTK_WIDGET( g_plugins_menu_separator ) );
while ( lst->next ) while ( lst->next )
{ {