From fe33cfc83fc8b0c95cec6aac9c0700ef6becb8af Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 10 Mar 2015 20:28:22 +0100 Subject: [PATCH] Add wxProcess::Activate() and implement it for MSW. When launching child processes it can be convenient to be able to switch to them later, provide a method in wxProcess to do it. Currently this is only implemented in wxMSW but could almost certainly be done for wxOSX too (it can be done using Apple Script, so presumably there is a way to do it programmatically as well) and could be also made to work at least under some Unix systems by emulating what wmctrl does (or just launching it?). --- docs/changes.txt | 1 + include/wx/process.h | 6 ++++++ interface/wx/process.h | 14 ++++++++++++++ src/common/process.cpp | 12 ++++++++++++ src/msw/utils.cpp | 22 ++++++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index 2157b8be5e..a294f521c3 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -69,6 +69,7 @@ All (GUI): - Add wxAddRemoveCtrl. - Add wxAppProgressIndicator for MSW (Chaobin Zhang) and OS X (Tobias Taschner). - Add wxEVT_MAGNIFY mouse event (Joost Nieuwenhuijse). +- Add wxProcess::Activate(). - Make results of wxDC::DrawEllipticArc() consistent across all platforms. - XRC handler for wxAuiToolBar added (Kinaou HervĂ©, David Hart). - Add wxCursor::GetHotSpot(). diff --git a/include/wx/process.h b/include/wx/process.h index 73df968a55..3278829776 100644 --- a/include/wx/process.h +++ b/include/wx/process.h @@ -77,6 +77,12 @@ public: // before the process it started terminates void Detach(); + // Activates a GUI process by bringing its (main) window to the front. + // + // Currently only implemented in wxMSW, simply returns false under the + // other platforms. + bool Activate() const; + #if wxUSE_STREAMS // Pipe handling wxInputStream *GetInputStream() const { return m_inputStream; } diff --git a/interface/wx/process.h b/interface/wx/process.h index 69573bf362..5484338f63 100644 --- a/interface/wx/process.h +++ b/interface/wx/process.h @@ -80,6 +80,20 @@ public: */ virtual ~wxProcess(); + /** + Activates a GUI process by bringing up its main window to the front. + + This is a convenient method which tries to bring this process to the + users attention. + + Currently this is implemented in wxMSW only and simply returns @false + under the other platforms. Notice that this function can also return + @false under MSW if, for example, the process doesn't have any windows. + + @since 3.1.0 + */ + bool Activate() const; + /** Closes the output stream (the one connected to the stdin of the child process). diff --git a/src/common/process.cpp b/src/common/process.cpp index ca19db0fa6..4c4bbc8bfd 100644 --- a/src/common/process.cpp +++ b/src/common/process.cpp @@ -176,6 +176,18 @@ bool wxProcess::Exists(int pid) } } +bool wxProcess::Activate() const +{ +#ifdef __WINDOWS__ + // This function is defined in src/msw/utils.cpp. + extern bool wxMSWActivatePID(long pid); + + return wxMSWActivatePID(m_pid); +#else + return false; +#endif +} + void wxProcess::SetPriority(unsigned priority) { wxCHECK_RET( priority <= wxPRIORITY_MAX, diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index 482a9b9e0f..89afce850e 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -795,6 +795,28 @@ int wxKill(long pid, wxSignal sig, wxKillError *krc, int flags) return 0; } +// This is used by wxProcess::Activate(). +extern +bool wxMSWActivatePID(long pid) +{ + wxFindByPidParams params; + params.pid = (DWORD)pid; + + if ( ::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) != 0 ) + { + // No windows corresponding to this PID were found. + return false; + } + + if ( !::BringWindowToTop(params.hwnd) ) + { + wxLogLastError(wxS("BringWindowToTop")); + return false; + } + + return true; +} + // By John Skiff int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc) {