]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxAppTraits::GetStandardCmdLineOptions() allowing to add the description of...
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 22 Apr 2007 20:54:07 +0000 (20:54 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 22 Apr 2007 20:54:07 +0000 (20:54 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45589 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/apptrait.h
include/wx/unix/apptrait.h
src/common/cmdline.cpp
src/gtk/utilsgtk.cpp

index 64f032246e42ae30a3f0d5ced59c4be083510e7c..cf5a6479671e7db7ff42404df06c81afeb57917b 100644 (file)
@@ -109,6 +109,7 @@ All (GUI):
 - Added wxWindow::NavigateIn() in addition to existing Navigate()
 - Add support for <data> tags to wxrc
 - Support wxAPPLY and wxCLOSE in CreateStdDialogButtonSizer() (Marcin Wojdyr)
+- Show standard options in wxCmdLineParser usage message (Francesco Montorsi)
 
 wxGTK:
 
index 06d77247b74ae1cd4c5148d7a6faca787353c473..213ccc01e857871ad872d86b506762b9f88efcc5 100644 (file)
@@ -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
index e4495aa2f61b8e60d2f1b8457fef7399a907317c..9d83f9b8efd0c0439f4aab4b0805e8404b732e28 100644 (file)
@@ -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__)
index be17b4d7470a7a96156c4703ac324bae067f1b25..30bf7f09d6e50d1db4b6b8a767d3e475592c0649 100644 (file)
@@ -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')
index 2ce48b8fb1ca19dae25f4be50873dd70148074b8..c0d0309f9532ad008870bb0e8b970137c1ba3f7b 100644 (file)
@@ -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;
+}