From d3a0a0eeab1965fcf85d15123c32a8f3a1900351 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 22 Apr 2007 20:54:07 +0000 Subject: [PATCH] added wxAppTraits::GetStandardCmdLineOptions() allowing to add the description of the standard toolkit options to the usage message and implement it for wxGTK2 (patch 1703077) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45589 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/apptrait.h | 19 +++++++- include/wx/unix/apptrait.h | 2 + src/common/cmdline.cpp | 20 +++++++-- src/gtk/utilsgtk.cpp | 91 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 5 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 64f032246e..cf5a647967 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -109,6 +109,7 @@ All (GUI): - Added wxWindow::NavigateIn() in addition to existing Navigate() - Add support for tags to wxrc - Support wxAPPLY and wxCLOSE in CreateStdDialogButtonSizer() (Marcin Wojdyr) +- Show standard options in wxCmdLineParser usage message (Francesco Montorsi) wxGTK: diff --git a/include/wx/apptrait.h b/include/wx/apptrait.h index 06d77247b7..213ccc01e8 100644 --- a/include/wx/apptrait.h +++ b/include/wx/apptrait.h @@ -138,7 +138,21 @@ public: // return the name of the Desktop Environment such as // "KDE" or "GNOME". May return an empty string. - virtual wxString GetDesktopEnvironment() const { return wxEmptyString; } + virtual wxString GetDesktopEnvironment() const = 0; + + // returns a short string to identify the block of the standard command + // line options parsed automatically by current port: if this string is + // empty, there are no such options, otherwise the function also fills + // passed arrays with the names and the descriptions of those options. + virtual wxString GetStandardCmdLineOptions(wxArrayString& names, + wxArrayString& desc) const + { + wxUnusedVar(names); + wxUnusedVar(desc); + + return wxEmptyString; + } + protected: #if wxUSE_STACKWALKER && defined( __WXDEBUG__ ) @@ -214,6 +228,7 @@ public: } virtual bool IsUsingUniversalWidgets() const { return false; } + virtual wxString GetDesktopEnvironment() const { return wxEmptyString; } }; // ---------------------------------------------------------------------------- @@ -253,6 +268,8 @@ public: return false; #endif } + + virtual wxString GetDesktopEnvironment() const { return wxEmptyString; } }; #endif // wxUSE_GUI diff --git a/include/wx/unix/apptrait.h b/include/wx/unix/apptrait.h index e4495aa2f6..9d83f9b8ef 100644 --- a/include/wx/unix/apptrait.h +++ b/include/wx/unix/apptrait.h @@ -49,6 +49,8 @@ public: #ifdef __WXGTK__ virtual void SetLocale(); virtual wxString GetDesktopEnvironment() const; + virtual wxString GetStandardCmdLineOptions(wxArrayString& names, + wxArrayString& desc) const; #endif #if defined(__WXDEBUG__) && defined(__WXGTK20__) diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index be17b4d747..30bf7f09d6 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -41,6 +41,7 @@ #include "wx/datetime.h" #include "wx/msgout.h" #include "wx/filename.h" +#include "wx/apptrait.h" // ---------------------------------------------------------------------------- // private functions @@ -179,7 +180,7 @@ struct wxCmdLineParserData // cmd line data wxArrayString m_arguments; // == argv, argc == m_arguments.GetCount() - wxArrayOptions m_options; // all possible options and switchrs + wxArrayOptions m_options; // all possible options and switches wxArrayParams m_paramDesc; // description of all possible params wxArrayString m_parameters; // all params found @@ -1068,18 +1069,29 @@ wxString wxCmdLineParser::GetUsageString() usage << _T('\n'); + // set to number of our own options, not counting the standard ones + count = namesOptions.size(); + + // get option names & descriptions for standard options, if any: + wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; + wxString stdDesc; + if ( traits ) + stdDesc = traits->GetStandardCmdLineOptions(namesOptions, descOptions); + // now construct the detailed help message size_t len, lenMax = 0; - count = namesOptions.size(); - for ( n = 0; n < count; n++ ) + for ( n = 0; n < namesOptions.size(); n++ ) { len = namesOptions[n].length(); if ( len > lenMax ) lenMax = len; } - for ( n = 0; n < count; n++ ) + for ( n = 0; n < namesOptions.size(); n++ ) { + if ( n == count ) + usage << _T('\n') << stdDesc; + len = namesOptions[n].length(); usage << namesOptions[n] << wxString(_T(' '), lenMax - len) << _T('\t') diff --git a/src/gtk/utilsgtk.cpp b/src/gtk/utilsgtk.cpp index 2ce48b8fb1..c0d0309f95 100644 --- a/src/gtk/utilsgtk.cpp +++ b/src/gtk/utilsgtk.cpp @@ -482,5 +482,96 @@ wxString wxGUIAppTraits::GetDesktopEnvironment() const return wxEmptyString; } +#ifdef __WXGTK26__ +// see the hack below in wxCmdLineParser::GetUsageString(). +// TODO: replace this hack with a g_option_group_get_entries() +// call as soon as such function exists +struct _GOptionGroup +{ + gchar *name; + gchar *description; + gchar *help_description; + + GDestroyNotify destroy_notify; + gpointer user_data; + + GTranslateFunc translate_func; + GDestroyNotify translate_notify; + gpointer translate_data; + + GOptionEntry *entries; + gint n_entries; + + GOptionParseFunc pre_parse_func; + GOptionParseFunc post_parse_func; + GOptionErrorFunc error_func; +}; + +wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt) +{ + wxString ret; + + if (opt->short_name) + ret << _T("-") << opt->short_name; + if (opt->long_name) + { + if (!ret.empty()) + ret << _T(", "); + ret << _T("--") << opt->long_name; + + if (opt->arg_description) + ret << _T("=") << opt->arg_description; + } + + return _T(" ") + ret; +} + +#endif // __WXGTK26__ + +wxString +wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names, + wxArrayString& desc) const +{ + wxString usage; + +#ifdef __WXGTK26__ + // check whether GTK version is greater than 2.6 but also lower than 2.12 + // because, as we use the undocumented _GOptionGroup struct, we don't want + // to run this code with future versions which might change it (2.11 is the + // latest one at the time of this writing) + if (!gtk_check_version(2,6,0) && + gtk_check_version(2,12,0)) + { + usage << _("The following standard GTK+ options are also supported:\n"); + + // passing true here means that the function can open the default + // display while parsing (not really used here anyhow) + GOptionGroup *gtkOpts = gtk_get_option_group(true); + + // WARNING: here we access the internals of GOptionGroup: + GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries; + unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries; + wxArrayString namesOptions, descOptions; + + for ( size_t n = 0; n < n_entries; n++ ) + { + if ( entries[n].flags & G_OPTION_FLAG_HIDDEN ) + continue; // skip + + names.push_back(wxGetNameFromGtkOptionEntry(&entries[n])); + + const gchar * const entryDesc = entries[n].description; + desc.push_back(entryDesc ? wxString(entryDesc) : _T("")); + } + + g_option_group_free (gtkOpts); + } +#else + wxUnusedVar(names); + wxUnusedVar(desc); +#endif // __WXGTK26__ + + return usage; +} -- 2.45.2