14#if !defined(__CYGWIN__) && !defined(WINELIB)
18#if defined(BI_MULTI_THREAD_RTL)
22#if defined(__BORLANDC__)
68#if defined(BI_MULTI_THREAD_RTL)
73 struct TInstance :
public TLocalObject
75 TInstance() : Window(nullptr) {}
86 static TWindow*& GetInstance();
89TWindow*& TCreationWindow::GetInstance()
91#if defined(BI_MULTI_THREAD_RTL)
106 static TTlsContainer<TInstance> container;
107 return container.Get().Window;
110 static TWindow* creationWindow = 0;
111 return creationWindow;
120 TWindow*& InitCreationWindowInstance = TCreationWindow::GetInstance();
130 return TCreationWindow::GetInstance();
148 _T(
"), New(") << (
void*)w <<
_T(
')'));
149 TCreationWindow::GetInstance() = w;
170# define DEFWINDOWPROC "DefWindowProcW"
172# define DEFWINDOWPROC "DefWindowProcA"
175#define DEFWINDOWPROC_MODULE _T("USER32")
178 return reinterpret_cast<WNDPROC
>(GetProcAddress(module,
DEFWINDOWPROC));
197 return ::DefWindowProc(hWnd, msg, param1, param2);
214 return (*wndproc)(hWnd, msg, param1, param2);
222#if defined(BI_NOTHUNK)
227const char OwlObjectProperty [] =
"OWL_TWINDOW";
233TWindow::CreateInstanceProc()
241 static LRESULT CALLBACK CommonWndProc(HWND hWnd, UINT msg, WPARAM p1, LPARAM p2)
245 HANDLE hObj = ::GetPropA(hWnd, OwlObjectProperty);
246 WARNX(OwlThunk, !hObj, 0,
_T(
"CommonWndProc: Handle lookup failed! GetLastError: ") << ::GetLastError());
252 return w->ReceiveMessage(hWnd, msg, p1, p2);
255 return &TLocal::CommonWndProc;
262TWindow::InitInstanceProc()
265 BOOL r = ::SetPropA(
GetHandle(), OwlObjectProperty,
reinterpret_cast<HANDLE
>(
this));
266 WARNX(OwlThunk, !r, 0,
_T(
"InitInstanceProc: Handle registration failed! GetLastError: ") << ::GetLastError());
274TWindow::FreeInstanceProc()
276 HANDLE h = ::RemovePropA(
GetHandle(), OwlObjectProperty);
277 WARNX(OwlThunk, !h, 0,
_T(
"FreeInstanceProc: Handle removal failed! GetLastError: ") << ::GetLastError());
290#define THUNK_ALLOCATOR_DIAGNOSTICS_VARIABLES\
294#define THUNK_ALLOCATOR_DIAGNOSTICS_INITIALIZATION\
295 , Count(0), Maximum(0)
297#define THUNK_ALLOCATOR_DIAGNOSTICS_UPDATE(i)\
299 if ((Count += i) > Maximum)\
302 WARNX(OwlThunk, true, 2, _T("New thunk count record: ") << Maximum);\
304 CHECKX(Count >= 0, _T("Negative thunk count!"));\
309#define THUNK_ALLOCATOR_DIAGNOSTICS_VARIABLES
310#define THUNK_ALLOCATOR_DIAGNOSTICS_INITIALIZATION
311#define THUNK_ALLOCATOR_DIAGNOSTICS_UPDATE(i)
326 static TThunkAllocator& GetInstance()
341 static TThunkAllocator TheAllocator;
350 const auto ok = Handle !=
nullptr;
351 WARN(!ok,
_T(
"TThunkAllocator::~TThunkAllocator: Terminating due to failed precondition."));
352 if (!ok) std::terminate();
354 [[maybe_unused]] BOOL r = ::HeapDestroy(Handle);
355 WARNX(OwlThunk, !r, 0,
_T(
"HeapDestroy failed! GetLastError: ") << ::GetLastError());
356 TRACEX(OwlThunk, 1,
_T(
"TThunkAllocator destructed @") << Handle);
362 LPVOID Allocate(
size_t size)
365 LPVOID p = ::HeapAlloc(Handle, 0, size);
366 WARNX(OwlThunk, !p, 0,
_T(
"HeapAlloc failed!"));
378 [[maybe_unused]] BOOL r = ::HeapFree(Handle, 0, p);
379 WARNX(OwlThunk, !r, 0,
_T(
"HeapFree failed! GetLastError: ") << ::GetLastError());
401 : Handle(::HeapCreate(Flags, InitialSize, MaxSize))
404 WARNX(OwlThunk, !Handle, 0,
_T(
"HeapCreate failed! GetLastError: ") << ::GetLastError());
406 TRACEX(OwlThunk, 1,
_T(
"TThunkAllocator constructed @") << Handle);
412 TThunkAllocator(
const TThunkAllocator&);
413 TThunkAllocator& operator=(
const TThunkAllocator&);
421 TThunkAllocator& InitThunkAllocatorInstance = TThunkAllocator::GetInstance();
430 const uint8* Initializer,
size_t Size,
431 void* (*GetMessageReceiverAddress)(),
int DispatchAddressIndex,
int InstanceAddressIndex
437 struct TCode {
uint8 bytes[Size];};
444 : CodeTemplate(*reinterpret_cast<TCode*>(reinterpret_cast<void*>(const_cast<
uint8*>(Initializer))))
445 {*
reinterpret_cast<void**
>(&CodeTemplate.bytes[DispatchAddressIndex]) = (*GetMessageReceiverAddress)();}
448 typename TFactory::TCode Code;
456 {*
reinterpret_cast<void**
>(
reinterpret_cast<void*
>(&Code.bytes[InstanceAddressIndex])) = w;}
461 void*
operator new(
size_t n)
463 LPVOID p = TThunkAllocator::GetInstance().Allocate (n);
465 TRACEX(OwlThunk, 1,
_T(
"TThunk allocated @") << p);
472 void operator delete(
void* p, size_t)
474 WARNX(OwlThunk, !p, 0,
_T(
"TThunk::delete called with null pointer."));
476 TThunkAllocator::GetInstance().Free(p);
477 TRACEX(OwlThunk, 1,
_T(
"TThunk deallocated @") << p);
484template <const u
int8* i,
size_t s,
void* (*gpa)(),
int dai,
int iai>
485const typename TThunkT<i, s, gpa, dai, iai>::TFactory
486TThunkT<i, s, gpa, dai, iai>::Factory;
500 0xB8, 0x00, 0x00, 0x00, 0x00,
501 0x89, 0x44, 0x24, 0x04,
502 0x68, 0x00, 0x00, 0x00, 0x00,
507{
return w->ReceiveMessage(w->
GetHandle(), msg, param1, param2);}
531 0xB9, 0x00, 0x00, 0x00, 0x00,
532 0x68, 0x00, 0x00, 0x00, 0x00,
546 TSignature f = &TWindow::ReceiveMessage;
547 return *
reinterpret_cast<void**
>(&f);
569 0x48, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
571 0x48, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
588#elif defined(_M_IX86)
593#error OWLNext: Unable to generate thunks for this platform.
599#if defined(__BORLANDC__)
600typedef TThunkForBorland TThunk;
601#elif defined(_MSC_VER)
602typedef TThunkForMicrosoft TThunk;
603#elif defined(__GNUC__)
604typedef TThunkForGnu TThunk;
606#error OWLNext: Unable to generate thunks for this compiler.
613TWindow::CreateInstanceProc()
615 return reinterpret_cast<WNDPROC
>(
new TThunk(
this));
622TWindow::InitInstanceProc()
630TWindow::FreeInstanceProc()
632 delete reinterpret_cast<TThunk*
>(GetInstanceProc());
#define WARNX(group, condition, level, message)
#define WARN(condition, message)
#define PRECONDITION(condition)
#define TRACEX(group, level, message)
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
static LRESULT CALLBACK InitWndProc(HWND, UINT msg, WPARAM, LPARAM)
Callback process for hooking TWindow to native window.
TWindow(TWindow *parent, LPCTSTR title=nullptr, TModule *module=nullptr)
Adds this to the child list of parent if nonzero, and calls EnableAutoCreate so that this will be cre...
void SetHandle(THandle)
Sets the window handle in a derived class.
WNDPROC GetWindowProc() const
friend TResult CALLBACK DispatchWindowMessage(TWindow *w, TMsgId msg, TParam1 param1, TParam2 param2)
void SubclassWindowFunction()
Installs the instance thunk as the WindowProc and saves the old window function in DefaultProc.
friend void * GetMessageReceiverMemberFunctionAddress()
WNDPROC SetWindowProc(WNDPROC wndProc)
HWND GetHandle() const
Returns the handle of the window.
TXOutOfMemory()
Constructs a TXOutOfMemory object.
#define OWLInternalVersion
Reliable platform independent header for common memory and string functions.
Object Windows Library (OWLNext Core).
TModule & OWLGetModule()
Returns a reference to the global OWL module object. When linking dynamically to OWLNext,...
UINT TMsgId
Message ID type.
WNDPROC GetDefWindowProc()
void * GetDispatchHelper()
TThunkT< ThunkInitializerForMicrosoft32Bit, sizeof ThunkInitializerForMicrosoft32Bit, &GetMessageReceiverMemberFunctionAddress, 6, 1 > TThunkForMicrosoft32Bit
void SetCreationWindow(TWindow *w)
const uint8 ThunkInitializerForMicrosoft32Bit[]
LPARAM TParam2
Second parameter type.
const TThunkT< i, s, gpa, dai, iai >::TFactory TThunkT< i, s, gpa, dai, iai >::Factory
WPARAM TParam1
First parameter type.
LRESULT TResult
Result type.
TThunkT< ThunkInitializerForBorland32Bit, sizeof ThunkInitializerForBorland32Bit, &GetDispatchHelper, 10, 1 > TThunkForBorland32Bit
TModule & GetGlobalModule()
TResult CALLBACK DispatchWindowMessage(TWindow *w, TMsgId msg, TParam1 param1, TParam2 param2)
DIAG_DEFINE_GROUP_INIT(OWL_INI, OwlApp, 1, 0)
uint32 OWLGetVersion()
Get version of OWL at runtime.
const uint8 ThunkInitializerForMicrosoft64Bit[]
const uint8 ThunkInitializerForBorland32Bit[]
void * GetMessageReceiverMemberFunctionAddress()
TThunkT< ThunkInitializerForMicrosoft64Bit, sizeof ThunkInitializerForMicrosoft64Bit, &GetDispatchHelper, 2, 13 > TThunkForMicrosoft64Bit
TWindow * GetCreationWindow()
DIAG_DECLARE_GROUP(OwlThread)
#define THUNK_ALLOCATOR_DIAGNOSTICS_UPDATE(i)
#define THUNK_ALLOCATOR_DIAGNOSTICS_INITIALIZATION
#define THUNK_ALLOCATOR_DIAGNOSTICS_VARIABLES
#define DEFWINDOWPROC_MODULE
Base window class TWindow definition, including HWND encapsulation.