+ MSG msg;
+ if ( !GetNextMessage(&msg) )
+ return false;
+
+#if wxUSE_THREADS
+ wxASSERT_MSG( wxThread::IsMain(),
+ wxT("only the main thread can process Windows messages") );
+
+ static bool s_hadGuiLock = true;
+ static wxMsgList s_aSavedMessages;
+
+ // if a secondary thread owning the mutex is doing GUI calls, save all
+ // messages for later processing - we can't process them right now because
+ // it will lead to recursive library calls (and we're not reentrant)
+ if ( !wxGuiOwnedByMainThread() )
+ {
+ s_hadGuiLock = false;
+
+ // leave out WM_COMMAND messages: too dangerous, sometimes
+ // the message will be processed twice
+ if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
+ {
+ MSG* pMsg = new MSG(msg);
+ s_aSavedMessages.Append(pMsg);
+ }
+
+ return true;
+ }
+ else
+ {
+ // have we just regained the GUI lock? if so, post all of the saved
+ // messages
+ //
+ // FIXME of course, it's not _exactly_ the same as processing the
+ // messages normally - expect some things to break...
+ if ( !s_hadGuiLock )
+ {
+ s_hadGuiLock = true;
+
+ wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
+ while (node)
+ {
+ MSG* pMsg = node->GetData();
+ s_aSavedMessages.Erase(node);
+
+ ProcessMessage(pMsg);
+ delete pMsg;
+
+ node = s_aSavedMessages.GetFirst();
+ }
+ }
+ }
+#endif // wxUSE_THREADS
+
+ ProcessMessage(&msg);
+
+ return true;