#include "wx/thread.h"
#include "wx/msw/private.h"
+#include "wx/msw/dc.h"
#include "wx/msw/ole/oleutils.h"
#include "wx/msw/private/timer.h"
WXDLLIMPEXP_CORE wxChar *wxCanvasClassName;
WXDLLIMPEXP_CORE wxChar *wxCanvasClassNameNR;
#else
-WXDLLIMPEXP_CORE const wxChar *wxCanvasClassName = wxT("wxWindowClass");
-WXDLLIMPEXP_CORE const wxChar *wxCanvasClassNameNR = wxT("wxWindowClassNR");
+WXDLLIMPEXP_CORE const wxChar *wxCanvasClassName = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxCanvasClassNameNR = NULL;
#endif
-WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassName = wxT("wxMDIFrameClass");
-WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR");
-WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass");
-WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassNameNoRedraw = wxT("wxMDIChildFrameClassNR");
+WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassName = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassNameNoRedraw = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassName = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassNameNoRedraw = NULL;
// ----------------------------------------------------------------------------
// private functions
return new wxEventLoop;
}
+// ---------------------------------------------------------------------------
+// Stuff for using console from the GUI applications
+// ---------------------------------------------------------------------------
+
+#ifndef __WXWINCE__
+
+#include <wx/dynlib.h>
+
+namespace
+{
+
+/*
+ Helper class to manipulate console from a GUI app.
+
+ Notice that console output is available in the GUI app only if:
+ - AttachConsole() returns TRUE (which means it never works under pre-XP)
+ - we have a valid STD_ERROR_HANDLE
+ - command history hasn't been changed since our startup
+
+ To check if all these conditions are verified, you need to simple call
+ IsOkToUse(). It will check the first two conditions above the first time it
+ is called (and if this fails, the subsequent calls will return immediately)
+ and also recheck the last one every time it is called.
+ */
+class wxConsoleStderr
+{
+public:
+ // default ctor does nothing, call Init() before using this class
+ wxConsoleStderr()
+ {
+ m_hStderr = INVALID_HANDLE_VALUE;
+ m_historyLen =
+ m_dataLen =
+ m_dataLine = 0;
+
+ m_ok = -1;
+ }
+
+ ~wxConsoleStderr()
+ {
+ if ( m_hStderr != INVALID_HANDLE_VALUE )
+ {
+ if ( !::FreeConsole() )
+ {
+ wxLogLastError(_T("FreeConsole"));
+ }
+ }
+ }
+
+ // return true if we were successfully initialized and there had been no
+ // console activity which would interfere with our output since then
+ bool IsOkToUse() const
+ {
+ if ( m_ok == -1 )
+ {
+ wxConsoleStderr * const self = wx_const_cast(wxConsoleStderr *, this);
+ self->m_ok = self->DoInit();
+
+ // no need to call IsHistoryUnchanged() as we just initialized
+ // m_history anyhow
+ return m_ok == 1;
+ }
+
+ return m_ok && IsHistoryUnchanged();
+ }
+
+
+ // output the provided text on the console, return true if ok
+ bool Write(const wxString& text);
+
+private:
+ // called by Init() once only to do the real initialization
+ bool DoInit();
+
+ // retrieve the command line history into the provided buffer and return
+ // its length
+ int GetCommandHistory(wxWxCharBuffer& buf) const;
+
+ // check if the console history has changed
+ bool IsHistoryUnchanged() const;
+
+ int m_ok; // initially -1, set to true or false by Init()
+
+ wxDynamicLibrary m_dllKernel32;
+
+ HANDLE m_hStderr; // console handle, if it's valid we must call
+ // FreeConsole() (even if m_ok != 1)
+
+ wxWxCharBuffer m_history; // command history on startup
+ int m_historyLen; // length command history buffer
+
+ wxCharBuffer m_data; // data between empty line and cursor position
+ int m_dataLen; // length data buffer
+ int m_dataLine; // line offset
+
+ typedef DWORD (WINAPI *GetConsoleCommandHistory_t)(LPTSTR sCommands,
+ DWORD nBufferLength,
+ LPCTSTR sExeName);
+ typedef DWORD (WINAPI *GetConsoleCommandHistoryLength_t)(LPCTSTR sExeName);
+
+ GetConsoleCommandHistory_t m_pfnGetConsoleCommandHistory;
+ GetConsoleCommandHistoryLength_t m_pfnGetConsoleCommandHistoryLength;
+
+ DECLARE_NO_COPY_CLASS(wxConsoleStderr)
+};
+
+bool wxConsoleStderr::DoInit()
+{
+ HANDLE hStderr = ::GetStdHandle(STD_ERROR_HANDLE);
+
+ if ( hStderr == INVALID_HANDLE_VALUE || !hStderr )
+ return false;
+
+ if ( !m_dllKernel32.Load(_T("kernel32.dll")) )
+ return false;
+
+ typedef BOOL (WINAPI *AttachConsole_t)(DWORD dwProcessId);
+ AttachConsole_t wxDL_INIT_FUNC(pfn, AttachConsole, m_dllKernel32);
+
+ if ( !pfnAttachConsole || !pfnAttachConsole(ATTACH_PARENT_PROCESS) )
+ return false;
+
+ // console attached, set m_hStderr now to ensure that we free it in the
+ // dtor
+ m_hStderr = hStderr;
+
+ wxDL_INIT_FUNC_AW(m_pfn, GetConsoleCommandHistory, m_dllKernel32);
+ if ( !m_pfnGetConsoleCommandHistory )
+ return false;
+
+ wxDL_INIT_FUNC_AW(m_pfn, GetConsoleCommandHistoryLength, m_dllKernel32);
+ if ( !m_pfnGetConsoleCommandHistoryLength )
+ return false;
+
+ // remember the current command history to be able to compare with it later
+ // in IsHistoryUnchanged()
+ m_historyLen = GetCommandHistory(m_history);
+ if ( !m_history )
+ return false;
+
+
+ // now find the first blank line above the current position
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+ if ( !::GetConsoleScreenBufferInfo(m_hStderr, &csbi) )
+ {
+ wxLogLastError(_T("GetConsoleScreenBufferInfo"));
+ return false;
+ }
+
+ COORD pos;
+ pos.X = 0;
+ pos.Y = csbi.dwCursorPosition.Y + 1;
+
+ // we decide that a line is empty if first 4 characters are spaces
+ DWORD ret;
+ char buf[4];
+ do
+ {
+ pos.Y--;
+ if ( !::ReadConsoleOutputCharacterA(m_hStderr, buf, WXSIZEOF(buf),
+ pos, &ret) )
+ {
+ wxLogLastError(_T("ReadConsoleOutputCharacterA"));
+ return false;
+ }
+ } while ( wxStrncmp(" ", buf, WXSIZEOF(buf)) != 0 );
+
+ // calculate line offset and length of data
+ m_dataLine = csbi.dwCursorPosition.Y - pos.Y;
+ m_dataLen = m_dataLine*csbi.dwMaximumWindowSize.X + csbi.dwCursorPosition.X;
+
+ if ( m_dataLen > 0 )
+ {
+ m_data.extend(m_dataLen);
+ if ( !::ReadConsoleOutputCharacterA(m_hStderr, m_data.data(), m_dataLen,
+ pos, &ret) )
+ {
+ wxLogLastError(_T("ReadConsoleOutputCharacterA"));
+ return false;
+ }
+ }
+
+ return true;
+}
+
+int wxConsoleStderr::GetCommandHistory(wxWxCharBuffer& buf) const
+{
+ // these functions are internal and may only be called by cmd.exe
+ static const wxChar *CMD_EXE = _T("cmd.exe");
+
+ const int len = m_pfnGetConsoleCommandHistoryLength(CMD_EXE);
+ if ( len )
+ {
+ buf.extend(len);
+
+ int len2 = m_pfnGetConsoleCommandHistory(buf.data(), len, CMD_EXE);
+
+#if !wxUSE_UNICODE
+ // there seems to be a bug in the GetConsoleCommandHistoryA(), it
+ // returns the length of Unicode string and not ANSI one
+ len2 /= 2;
+#endif // !wxUSE_UNICODE
+
+ if ( len2 != len )
+ {
+ wxFAIL_MSG( _T("failed getting history?") );
+ }
+ }
+
+ return len;
+}
+
+bool wxConsoleStderr::IsHistoryUnchanged() const
+{
+ wxASSERT_MSG( m_ok == 1, _T("shouldn't be called if not initialized") );
+
+ // get (possibly changed) command history
+ wxWxCharBuffer history;
+ const int historyLen = GetCommandHistory(history);
+
+ // and compare it with the original one
+ return historyLen == m_historyLen && history &&
+ memcmp(m_history, history, historyLen) == 0;
+}
+
+bool wxConsoleStderr::Write(const wxString& text)
+{
+ wxASSERT_MSG( m_hStderr != INVALID_HANDLE_VALUE,
+ _T("should only be called if Init() returned true") );
+
+ // get current position
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
+ if ( !::GetConsoleScreenBufferInfo(m_hStderr, &csbi) )
+ {
+ wxLogLastError(_T("GetConsoleScreenBufferInfo"));
+ return false;
+ }
+
+ // and calculate new position (where is empty line)
+ csbi.dwCursorPosition.X = 0;
+ csbi.dwCursorPosition.Y -= m_dataLine;
+
+ if ( !::SetConsoleCursorPosition(m_hStderr, csbi.dwCursorPosition) )
+ {
+ wxLogLastError(_T("SetConsoleCursorPosition"));
+ return false;
+ }
+
+ DWORD ret;
+ if ( !::FillConsoleOutputCharacter(m_hStderr, _T(' '), m_dataLen,
+ csbi.dwCursorPosition, &ret) )
+ {
+ wxLogLastError(_T("FillConsoleOutputCharacter"));
+ return false;
+ }
+
+ if ( !::WriteConsole(m_hStderr, text.wx_str(), text.length(), &ret, NULL) )
+ {
+ wxLogLastError(_T("WriteConsole"));
+ return false;
+ }
+
+ WriteConsoleA(m_hStderr, m_data, m_dataLen, &ret, 0);
+
+ return true;
+}
+
+wxConsoleStderr s_consoleStderr;
+
+} // anonymous namespace
+
+bool wxGUIAppTraits::CanUseStderr()
+{
+ return s_consoleStderr.IsOkToUse();
+}
+
+bool wxGUIAppTraits::WriteToStderr(const wxString& text)
+{
+ return s_consoleStderr.IsOkToUse() && s_consoleStderr.Write(text);
+}
+
+#endif // !__WXWINCE__
+
// ===========================================================================
// wxApp implementation
// ===========================================================================
RegisterWindowClasses();
- wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100);
-
#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
wxSetKeyboardHook(true);
#endif
// RegisterWindowClasses
// ---------------------------------------------------------------------------
+// This function registers the given class name and stores a pointer to a
+// heap-allocated copy of it at the specified location, it must be deleted
+// later.
+static void RegisterAndStoreClassName(const wxString& uniqueClassName,
+ const wxChar **className,
+ WNDCLASS *lpWndClass)
+{
+ const size_t length = uniqueClassName.length() + 1; // for trailing NUL
+ wxChar *newChars = new wxChar[length];
+ wxStrncpy(newChars, uniqueClassName, length);
+ *className = newChars;
+ lpWndClass->lpszClassName = *className;
+
+ if ( !::RegisterClass(lpWndClass) )
+ {
+ wxLogLastError(wxString::Format(wxT("RegisterClass(%s)"), newChars));
+ }
+}
+
+// This function registers the class defined by the provided WNDCLASS struct
+// contents using a unique name constructed from the specified base name and
+// and a suffix unique to this library instance. It also stores the generated
+// unique names for normal and "no redraw" versions of the class in the
+// provided variables, caller must delete their contents later.
+static void RegisterClassWithUniqueNames(const wxString& baseName,
+ const wxChar **className,
+ const wxChar **classNameNR,
+ WNDCLASS *lpWndClass)
+{
+ // for each class we register one with CS_(V|H)REDRAW style and one
+ // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
+ static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
+ static const long styleNoRedraw = CS_DBLCLKS;
+
+ const wxString uniqueSuffix(wxString::Format(wxT("@%p"), className));
+
+ wxString uniqueClassName(baseName + uniqueSuffix);
+ lpWndClass->style = styleNormal;
+ RegisterAndStoreClassName(uniqueClassName, className, lpWndClass);
+
+ // NB: remember that code elsewhere supposes that no redraw class names
+ // use the same names as normal classes with "NR" suffix so we must put
+ // "NR" at the end instead of using more natural baseName+"NR"+suffix
+ wxString uniqueClassNameNR(uniqueClassName + wxT("NR"));
+ lpWndClass->style = styleNoRedraw;
+ RegisterAndStoreClassName(uniqueClassNameNR, classNameNR, lpWndClass);
+}
+
// TODO we should only register classes really used by the app. For this it
// would be enough to just delay the class registration until an attempt
// to create a window of this class is made.
WNDCLASS wndclass;
wxZeroMemory(wndclass);
- // for each class we register one with CS_(V|H)REDRAW style and one
- // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
- static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
- static const long styleNoRedraw = CS_DBLCLKS;
-
// the fields which are common to all classes
wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
wndclass.hInstance = wxhInstance;
wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW);
- // register the class for all normal windows
+ // register the class for all normal windows and "no redraw" frames
wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
- wndclass.lpszClassName = wxCanvasClassName;
- wndclass.style = styleNormal;
+ RegisterClassWithUniqueNames(wxT("wxWindowClass"),
+ &wxCanvasClassName,
+ &wxCanvasClassNameNR,
+ &wndclass);
- if ( !RegisterClass(&wndclass) )
- {
- wxLogLastError(wxT("RegisterClass(frame)"));
- }
-
- // "no redraw" frame
- wndclass.lpszClassName = wxCanvasClassNameNR;
- wndclass.style = styleNoRedraw;
-
- if ( !RegisterClass(&wndclass) )
- {
- wxLogLastError(wxT("RegisterClass(no redraw frame)"));
- }
-
- // Register the MDI frame window class.
+ // Register the MDI frame window class and "no redraw" MDI frame
wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves
- wndclass.lpszClassName = wxMDIFrameClassName;
- wndclass.style = styleNormal;
+ RegisterClassWithUniqueNames(wxT("wxMDIFrameClass"),
+ &wxMDIFrameClassName,
+ &wxMDIFrameClassNameNoRedraw,
+ &wndclass);
- if ( !RegisterClass(&wndclass) )
- {
- wxLogLastError(wxT("RegisterClass(MDI parent)"));
- }
-
- // "no redraw" MDI frame
- wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw;
- wndclass.style = styleNoRedraw;
-
- if ( !RegisterClass(&wndclass) )
- {
- wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
- }
-
- // Register the MDI child frame window class.
+ // Register the MDI child frame window class and "no redraw" MDI child frame
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wndclass.lpszClassName = wxMDIChildFrameClassName;
- wndclass.style = styleNormal;
-
- if ( !RegisterClass(&wndclass) )
- {
- wxLogLastError(wxT("RegisterClass(MDI child)"));
- }
-
- // "no redraw" MDI child frame
- wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw;
- wndclass.style = styleNoRedraw;
-
- if ( !RegisterClass(&wndclass) )
- {
- wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
- }
+ RegisterClassWithUniqueNames(wxT("wxMDIChildFrameClass"),
+ &wxMDIChildFrameClassName,
+ &wxMDIChildFrameClassNameNoRedraw,
+ &wndclass);
return true;
}
// UnregisterWindowClasses
// ---------------------------------------------------------------------------
-bool wxApp::UnregisterWindowClasses()
+// This function unregisters the class with the given name and frees memory
+// allocated for it by RegisterAndStoreClassName().
+static bool UnregisterAndFreeClassName(const wxChar **ppClassName)
{
bool retval = true;
-#ifndef __WXMICROWIN__
- // MDI frame window class.
- if ( !::UnregisterClass(wxMDIFrameClassName, wxhInstance) )
+ if ( !::UnregisterClass(*ppClassName, wxhInstance) )
{
- wxLogLastError(wxT("UnregisterClass(MDI parent)"));
+ wxLogLastError(
+ wxString::Format(wxT("UnregisterClass(%s)"), *ppClassName));
retval = false;
}
- // "no redraw" MDI frame
- if ( !::UnregisterClass(wxMDIFrameClassNameNoRedraw, wxhInstance) )
- {
- wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
+ delete [] (wxChar*) *ppClassName;
+ *ppClassName = NULL;
- retval = false;
- }
+ return retval;
+}
- // MDI child frame window class.
- if ( !::UnregisterClass(wxMDIChildFrameClassName, wxhInstance) )
- {
- wxLogLastError(wxT("UnregisterClass(MDI child)"));
+bool wxApp::UnregisterWindowClasses()
+{
+ bool retval = true;
+#ifndef __WXMICROWIN__
+ if ( !UnregisterAndFreeClassName(&wxMDIFrameClassName) )
retval = false;
- }
-
- // "no redraw" MDI child frame
- if ( !::UnregisterClass(wxMDIChildFrameClassNameNoRedraw, wxhInstance) )
- {
- wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
+ if ( !UnregisterAndFreeClassName(&wxMDIFrameClassNameNoRedraw) )
retval = false;
- }
- // canvas class name
- if ( !::UnregisterClass(wxCanvasClassName, wxhInstance) )
- {
- wxLogLastError(wxT("UnregisterClass(canvas)"));
+ if ( !UnregisterAndFreeClassName(&wxMDIChildFrameClassName) )
+ retval = false;
+ if ( !UnregisterAndFreeClassName(&wxMDIChildFrameClassNameNoRedraw) )
retval = false;
- }
- if ( !::UnregisterClass(wxCanvasClassNameNR, wxhInstance) )
- {
- wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
+ if ( !UnregisterAndFreeClassName(&wxCanvasClassName) )
+ retval = false;
+ if ( !UnregisterAndFreeClassName(&wxCanvasClassNameNR) )
retval = false;
- }
#endif // __WXMICROWIN__
return retval;
void wxApp::CleanUp()
{
- // all objects pending for deletion must be deleted first, otherwise we
- // would crash when they use wxWinHandleHash (and UnregisterWindowClasses()
- // call wouldn't succeed as long as any windows still exist), so call the
- // base class method first and only then do our clean up
+ // all objects pending for deletion must be deleted first, otherwise
+ // UnregisterWindowClasses() call wouldn't succeed (because windows
+ // using the classes being unregistered still exist), so call the base
+ // class method first and only then do our clean up
wxAppBase::CleanUp();
#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
// unregister the classes now
UnregisterWindowClasses();
- delete wxWinHandleHash;
- wxWinHandleHash = NULL;
-
#ifdef __WXWINCE__
free( wxCanvasClassName );
free( wxCanvasClassNameNR );
// start up again. Doing it this way ensures that the idle handler
// wakes up in the right thread (see also wxWakeUpMainThread() which does
// the same for the main app thread only)
- wxWindow *topWindow = wxTheApp->GetTopWindow();
+ wxWindow * const topWindow = wxTheApp->GetTopWindow();
if ( topWindow )
{
- if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
+ HWND hwndTop = GetHwndOf(topWindow);
+
+ // Do not post WM_NULL if there's already a pending WM_NULL to avoid
+ // overflowing the message queue.
+ //
+ // Notice that due to a limitation of PeekMessage() API (which handles
+ // 0,0 range specially), we have to check the range from 0-1 instead.
+ // This still makes it possible to overflow the queue with WM_NULLs by
+ // interspersing the calles to WakeUpIdle() with windows creation but
+ // it should be rather hard to do it accidentally.
+ MSG msg;
+ if ( !::PeekMessage(&msg, hwndTop, 0, 1, PM_NOREMOVE) ||
+ ::PeekMessage(&msg, hwndTop, 1, 1, PM_NOREMOVE) )
{
- // should never happen
- wxLogLastError(wxT("PostMessage(WM_NULL)"));
+ if ( !::PostMessage(hwndTop, WM_NULL, 0, 0) )
+ {
+ // should never happen
+ wxLogLastError(wxT("PostMessage(WM_NULL)"));
+ }
}
}
}
void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
{
- if (GetTopWindow())
- GetTopWindow()->Close(true);
+ // Windows will terminate the process soon after we return from
+ // WM_ENDSESSION handler or when we delete our last window, so make sure we
+ // at least execute our cleanup code before
+
+ // prevent the window from being destroyed when the corresponding wxTLW is
+ // destroyed: this will result in a leak of a HWND, of course, but who
+ // cares when the process is being killed anyhow
+ if ( !wxTopLevelWindows.empty() )
+ wxTopLevelWindows[0]->SetHWND(0);
+
+ const int rc = OnExit();
+
+ wxEntryCleanup();
+
+ // calling exit() instead of ExitProcess() or not doing anything at all and
+ // being killed by Windows has the advantage of executing the dtors of
+ // global objects
+ exit(rc);
}
// Default behaviour: close the application with prompts. The
// system DLL versions
// ----------------------------------------------------------------------------
+// these functions have trivial inline implementations for CE
+#ifndef __WXWINCE__
+
#if wxUSE_DYNLIB_CLASS
namespace
#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
+#endif // !__WXWINCE__
+
// ----------------------------------------------------------------------------
// Yield to incoming messages
// ----------------------------------------------------------------------------
// we don't want to process WM_QUIT from here - it should be processed in
// the main event loop in order to stop it
+ wxEventLoopGuarantor dummyLoopIfNeeded;
MSG msg;
while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
msg.message != WM_QUIT )