forked from dolphinsmalltalk/DolphinVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wingui.cpp
172 lines (143 loc) · 4.65 KB
/
wingui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
============
wingui.cpp
============
Interpreter/Windows GUI interface functions
*/
#include "Ist.h"
#ifdef _CONSOLE
#error Not for use in console VM
#endif
#ifndef _DEBUG
//#pragma optimize("s", on)
#pragma auto_inline(off)
#endif
#include "Interprt.h"
#include "ObjMem.h"
#include <stdarg.h>
#include <wtypes.h>
#include "rc_vm.h"
static HHOOK hHookOldCbtFilter;
#pragma code_seg()
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
BOOL __fastcall Interpreter::primitiveHookWindowCreate()
{
Oop argPointer = stackTop();
OTE* underConstruction = m_oteUnderConstruction;
OTE* receiverPointer = reinterpret_cast<OTE*>(stackValue(1));
if (!underConstruction->isNil() && underConstruction != receiverPointer)
{
// Hooked by another window - fail the primitive
return primitiveFailureWith(1, underConstruction);
}
if (argPointer == Oop(Pointers.True))
{
// Hooking
if (underConstruction != receiverPointer)
{
ASSERT(underConstruction->isNil());
m_oteUnderConstruction= receiverPointer;
receiverPointer->countUp();
}
}
else
{
if (argPointer == Oop(Pointers.False))
{
// Unhooking
if (underConstruction == receiverPointer)
{
tracelock lock(TRACESTREAM);
TRACESTREAM << "WARNING: Unhooking create for " << hex << underConstruction << " before HCBT_CREATEWND" << endl;
ObjectMemory::nilOutPointer(m_oteUnderConstruction);
}
else
ASSERT(underConstruction->isNil());
}
else
return primitiveFailureWith(0, argPointer); // Invalid argument
}
popStack();
return TRUE;
}
inline void Interpreter::subclassWindow(OTE* window, HWND hWnd)
{
ASSERT(!ObjectMemoryIsIntegerObject(window));
// As this is called from an external entry point, we must ensure that OT/stack overflows
// are handled, and also that we catch the SE_VMCALLBACKUNWIND exceptions
__try
{
bool bDisabled = disableInterrupts(true);
Oop retVal = performWith(Oop(window), Pointers.subclassWindowSymbol, Oop(ExternalHandle::New(hWnd)));
ObjectMemory::countDown(retVal);
ASSERT(m_bInterruptsDisabled);
disableInterrupts(bDisabled);
}
__except (callbackExceptionFilter(GetExceptionInformation()))
{
trace("WARNING: Unwinding Interpreter::subclassWindow(%#x, %#x)\n", window, hWnd);
}
}
LRESULT CALLBACK Interpreter::CbtFilterHook(int code, WPARAM wParam, LPARAM lParam)
{
// Looking for HCBT_CREATEWND, just pass others on...
if (code == HCBT_CREATEWND)
{
//ASSERT(lParam != NULL);
//LPCREATESTRUCT lpcs = ((LPCBT_CREATEWND)lParam)->lpcs;
//ASSERT(lpcs != NULL);
OTE* underConstruction = m_oteUnderConstruction;
if (!underConstruction->isNil())
{
// Nil this out as soon as possible
m_oteUnderConstruction = Pointers.Nil;
underConstruction->countDown();
ASSERT(wParam != NULL); // should be non-NULL HWND
// set m_bDlgCreate to TRUE if it is a dialog box
// (this controls what kind of subclassing is done later)
//pThreadState->m_bDlgCreate = (lpcs->lpszClass == WC_DIALOG);
// Pass to Smalltalk for subclassing (catch unwind failures so not thrown out)
subclassWindow(underConstruction, HWND(wParam));
}
}
return ::CallNextHookEx(hHookOldCbtFilter, code, wParam, lParam);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// All messages are dispatched through DolphinWndProc. The purpose of this procedure is to act as a
// target for calls to default window processing
LRESULT CALLBACK Interpreter::DolphinDlgProc(HWND /*hWnd*/, UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/)
{
return FALSE;
}
int __stdcall DolphinMessage(UINT flags, const char* msg)
{
char szCaption[512];
HMODULE hExe = GetModuleHandle(NULL);
if (!::LoadString(hExe, IDS_APP_TITLE, szCaption, sizeof(szCaption)-1))
GetModuleFileName(hExe, szCaption, sizeof(szCaption));
return ::MessageBox(NULL, msg, szCaption, flags|MB_TASKMODAL);
}
#pragma code_seg(INIT_SEG)
HRESULT Interpreter::initializeImage()
{
// Before starting the Smalltalk, install the Windows CBT hook for catching window creates
// Note that we don't bother installing and deinstalling this as we forward most messages
// in double quick time (see CbtFilterHook), and also the MFC doesn't!
hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT, CbtFilterHook, NULL, ::GetCurrentThreadId());
if (hHookOldCbtFilter == NULL)
return ReportError(IDP_FAILTOHOOK, ::GetLastError());
return S_OK;
}
#pragma code_seg(TERM_SEG)
void Interpreter::GuiShutdown()
{
if (hHookOldCbtFilter != NULL)
VERIFY(::UnhookWindowsHookEx(hHookOldCbtFilter));
hHookOldCbtFilter = NULL;
}
void __stdcall DolphinFatalExit(int /*exitCode*/, const char* msg)
{
FatalAppExit(0, msg);
}