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.
- wxLogInfo() now logs messages if the log level is high enough, even without
wxLog::SetVerbose() which now only affects wxLogVerbose().
- Add wxFileType::GetExpandedCommand() (troelsk).
wxMSW:

View File

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

View File

@@ -42,9 +42,18 @@ public:
bool GetIcon(wxIconLocation *iconLoc) const;
bool GetDescription(wxString *desc) const;
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,
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,
const wxFileType::MessageParameters& params) const;
@@ -76,6 +85,9 @@ public:
// explicitly.
void MSWSuppressNotifications(bool supress);
wxString
GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const;
private:
// helper function: reads the command corresponding to the specified verb
// 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 Unassociate(wxFileType *ft);
wxString
GetExpandedCommand(const wxString& verb,
const wxFileType::MessageParameters& params) const;
private:
// 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 SetDefaultIcon(const wxString& strIcon = wxEmptyString, int index = 0);
private:
wxString
GetExpandedCommand(const wxString & verb,
const wxFileType::MessageParameters& params) const;
private:
wxMimeTypesManagerImpl *m_manager;
wxArrayInt m_index; // in the wxMimeTypesManagerImpl arrays
};

View File

@@ -376,6 +376,22 @@ public:
bool GetPrintCommand(wxString* command,
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
and commands arrays with the command information.

View File

@@ -50,6 +50,7 @@
#include "wx/sizer.h"
#endif
#include "wx/filename.h"
#include "wx/txtstrm.h"
#include "wx/numdlg.h"
#include "wx/textdlg.h"
@@ -1078,7 +1079,7 @@ void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event))
if ( !AskUserForFileName() )
return;
wxString ext = gs_lastFile.AfterLast(wxT('.'));
wxString ext = wxFileName(gs_lastFile).GetExt();
wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
if ( !ft )
{
@@ -1088,8 +1089,15 @@ void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event))
}
wxString cmd;
bool ok = ft->GetOpenCommand(&cmd,
wxFileType::MessageParameters(gs_lastFile));
bool ok = false;
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;
if ( !ok )
{

View File

@@ -428,6 +428,13 @@ wxFileType::GetPrintCommand(wxString *printCmd,
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,
wxArrayString *commands,

View File

@@ -338,33 +338,19 @@ wxString wxFileTypeImpl::GetCommand(const wxString& verb) const
return command;
}
bool
wxFileTypeImpl::GetOpenCommand(wxString *openCmd,
const wxFileType::MessageParameters& params)
const
wxString
wxFileTypeImpl::GetExpandedCommand(const wxString & verb,
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
// to use it as a fallback.
if ( cmd.empty() )
if ( cmd.empty() && (verb == wxT("open")) )
cmd = GetCommand(wxT("show"));
*openCmd = 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();
return wxFileType::ExpandCommand(cmd, params);
}
// ----------------------------------------------------------------------------

View File

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