Add wxFileType::GetExpandedCommand()

This new method allows to get the command expanded with the given file name
for commands other than "Open" and "Print".

Closes #17367.
This commit is contained in:
Troels Knakkergaard
2016-03-05 03:09:59 +01:00
committed by Vadim Zeitlin
parent 6ea8ba1e9c
commit cda7209101
10 changed files with 69 additions and 28 deletions

View File

@@ -61,6 +61,7 @@ All:
functions. See wxGetOsVersion(), wxPlatformInfo, and wxAppTraits. functions. See wxGetOsVersion(), wxPlatformInfo, and wxAppTraits.
- wxLogInfo() now logs messages if the log level is high enough, even without - wxLogInfo() now logs messages if the log level is high enough, even without
wxLog::SetVerbose() which now only affects wxLogVerbose(). wxLog::SetVerbose() which now only affects wxLogVerbose().
- Add wxFileType::GetExpandedCommand() (troelsk).
wxMSW: wxMSW:

View File

@@ -353,6 +353,9 @@ public:
// dtor (not virtual, shouldn't be derived from) // dtor (not virtual, shouldn't be derived from)
~wxFileType(); ~wxFileType();
wxString
GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const;
private: private:
// default ctor is private because the user code never creates us // default ctor is private because the user code never creates us
wxFileType(); wxFileType();

View File

@@ -42,9 +42,18 @@ public:
bool GetIcon(wxIconLocation *iconLoc) const; bool GetIcon(wxIconLocation *iconLoc) const;
bool GetDescription(wxString *desc) const; bool GetDescription(wxString *desc) const;
bool GetOpenCommand(wxString *openCmd, bool GetOpenCommand(wxString *openCmd,
const wxFileType::MessageParameters& params) const; const wxFileType::MessageParameters& params) const
{
*openCmd = GetExpandedCommand(wxS("open"), params);
return !openCmd->empty();
}
bool GetPrintCommand(wxString *printCmd, bool GetPrintCommand(wxString *printCmd,
const wxFileType::MessageParameters& params) const; const wxFileType::MessageParameters& params) const
{
*printCmd = GetExpandedCommand(wxS("print"), params);
return !printCmd->empty();
}
size_t GetAllCommands(wxArrayString * verbs, wxArrayString * commands, size_t GetAllCommands(wxArrayString * verbs, wxArrayString * commands,
const wxFileType::MessageParameters& params) const; const wxFileType::MessageParameters& params) const;
@@ -76,6 +85,9 @@ public:
// explicitly. // explicitly.
void MSWSuppressNotifications(bool supress); void MSWSuppressNotifications(bool supress);
wxString
GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const;
private: private:
// helper function: reads the command corresponding to the specified verb // helper function: reads the command corresponding to the specified verb
// from the registry (returns an empty string if not found) // from the registry (returns an empty string if not found)

View File

@@ -105,6 +105,9 @@ public:
bool SetDefaultIcon(const wxString& strIcon = wxEmptyString, int index = 0); bool SetDefaultIcon(const wxString& strIcon = wxEmptyString, int index = 0);
bool Unassociate(wxFileType *ft); bool Unassociate(wxFileType *ft);
wxString
GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const;
private: private:
// All that is needed to query type info - UTI and pointer to the manager // All that is needed to query type info - UTI and pointer to the manager

View File

@@ -157,11 +157,10 @@ public:
bool SetCommand(const wxString& cmd, const wxString& verb, bool overwriteprompt = true); bool SetCommand(const wxString& cmd, const wxString& verb, bool overwriteprompt = true);
bool SetDefaultIcon(const wxString& strIcon = wxEmptyString, int index = 0); bool SetDefaultIcon(const wxString& strIcon = wxEmptyString, int index = 0);
private:
wxString wxString
GetExpandedCommand(const wxString & verb, GetExpandedCommand(const wxString & verb,
const wxFileType::MessageParameters& params) const; const wxFileType::MessageParameters& params) const;
private:
wxMimeTypesManagerImpl *m_manager; wxMimeTypesManagerImpl *m_manager;
wxArrayInt m_index; // in the wxMimeTypesManagerImpl arrays wxArrayInt m_index; // in the wxMimeTypesManagerImpl arrays
}; };

View File

@@ -376,6 +376,22 @@ public:
bool GetPrintCommand(wxString* command, bool GetPrintCommand(wxString* command,
const MessageParameters& params) const; const MessageParameters& params) const;
/**
The returned string is the command to be executed in order to
open/print/edit the file of the given type.
If the string is empty, the lookup for the @a verb failed.
The name of the file is retrieved from the MessageParameters class.
@see wxExecute()
@since 3.1.1
*/
wxString GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const;
/** /**
Returns the number of commands for this mime type, and fills the verbs Returns the number of commands for this mime type, and fills the verbs
and commands arrays with the command information. and commands arrays with the command information.

View File

@@ -50,6 +50,7 @@
#include "wx/sizer.h" #include "wx/sizer.h"
#endif #endif
#include "wx/filename.h"
#include "wx/txtstrm.h" #include "wx/txtstrm.h"
#include "wx/numdlg.h" #include "wx/numdlg.h"
#include "wx/textdlg.h" #include "wx/textdlg.h"
@@ -1078,7 +1079,7 @@ void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event))
if ( !AskUserForFileName() ) if ( !AskUserForFileName() )
return; return;
wxString ext = gs_lastFile.AfterLast(wxT('.')); wxString ext = wxFileName(gs_lastFile).GetExt();
wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
if ( !ft ) if ( !ft )
{ {
@@ -1088,8 +1089,15 @@ void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event))
} }
wxString cmd; wxString cmd;
bool ok = ft->GetOpenCommand(&cmd, bool ok = false;
wxFileType::MessageParameters(gs_lastFile)); const wxFileType::MessageParameters params(gs_lastFile);
#ifdef __WXMSW__
// try editor, for instance Notepad if extension is .xml
cmd = ft->GetExpandedCommand(wxT("edit"), params);
ok = !cmd.empty();
#endif
if (!ok) // else try viewer
ok = ft->GetOpenCommand(&cmd, params);
delete ft; delete ft;
if ( !ok ) if ( !ok )
{ {

View File

@@ -428,6 +428,13 @@ wxFileType::GetPrintCommand(wxString *printCmd,
return m_impl->GetPrintCommand(printCmd, params); return m_impl->GetPrintCommand(printCmd, params);
} }
wxString
wxFileType::GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const
{
return m_impl->GetExpandedCommand(verb, params);
}
size_t wxFileType::GetAllCommands(wxArrayString *verbs, size_t wxFileType::GetAllCommands(wxArrayString *verbs,
wxArrayString *commands, wxArrayString *commands,

View File

@@ -338,33 +338,19 @@ wxString wxFileTypeImpl::GetCommand(const wxString& verb) const
return command; return command;
} }
bool
wxFileTypeImpl::GetOpenCommand(wxString *openCmd, wxString
const wxFileType::MessageParameters& params) wxFileTypeImpl::GetExpandedCommand(const wxString & verb,
const const wxFileType::MessageParameters& params) const
{ {
wxString cmd = GetCommand(wxT("open")); wxString cmd = GetCommand(verb);
// Some viewers don't define the "open" verb but do define "show" one, try // Some viewers don't define the "open" verb but do define "show" one, try
// to use it as a fallback. // to use it as a fallback.
if ( cmd.empty() ) if ( cmd.empty() && (verb == wxT("open")) )
cmd = GetCommand(wxT("show")); cmd = GetCommand(wxT("show"));
*openCmd = wxFileType::ExpandCommand(cmd, params); return wxFileType::ExpandCommand(cmd, params);
return !openCmd->empty();
}
bool
wxFileTypeImpl::GetPrintCommand(wxString *printCmd,
const wxFileType::MessageParameters& params)
const
{
wxString cmd = GetCommand(wxT("print"));
*printCmd = wxFileType::ExpandCommand(cmd, params);
return !printCmd->empty();
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -699,6 +699,12 @@ bool wxFileTypeImpl::Unassociate(wxFileType *WXUNUSED(ft))
return false; return false;
} }
wxString
wxFileTypeImpl::GetExpandedCommand(const wxString& WXUNUSED(verb),
const wxFileType::MessageParameters& WXUNUSED(params)) const
{
return wxString();
}
#endif // wxUSE_MIMETYPE #endif // wxUSE_MIMETYPE