1 #ifndef NEO_WINDOW_BASE_H
2 #define NEO_WINDOW_BASE_H
17 virtual bool operator() (HWND hwnd, UINT message, WPARAM wp, LPARAM lp, LRESULT& result, WindowBase* origin) = 0;
20 class MessageHandlerMap
23 typedef std::pair<MessageId, std::unique_ptr<Functor>> PAIR;
24 typedef std::vector<PAIR> DATA;
26 MessageHandlerMap() {}
28 MessageHandlerMap(
const MessageHandlerMap& rhs) {}
30 DATA::const_iterator FindHandler(
const MessageId& key)
const
33 for(
auto it = map.begin(); it < end; ++it)
39 DATA::iterator GetLowerBound(
const MessageId& key)
41 auto it = map.begin();
45 if(!(it->first < key))
52 void AddHandler(
const MessageId& key, Functor* handler)
54 auto it = GetLowerBound(key);
59 it->second.reset(handler);
63 map.insert(it, std::make_pair(key, std::unique_ptr<Functor>(handler)));
67 map.push_back(std::make_pair(key, std::unique_ptr<Functor>(handler)));
69 void RemoveHandler(
const MessageId& key)
71 auto found = FindHandler(key);
72 if(found != map.end())
75 Functor* GetHandler(
const MessageId& key)
const
77 for(
size_t i = 0; i < map.size(); ++i)
78 if(key.EqualOrSpecializationOf(map[i].first))
79 return map[i].second.get();
91 StrongHwnd(
const StrongHwnd& rhs);
94 void Create(DWORD exStyle,
103 void Attach(HWND hwnd);
105 HWND GetHandle()
const {
return handle; }
108 WNDPROC subclassedProc;
115 WeakHwnd() { handle = 0; }
117 void Create(DWORD exStyle,
125 void Attach(HWND hwnd) { handle = hwnd; }
126 void Clear() { handle = 0; }
127 HWND GetHandle()
const {
return handle; }
160 operator HWND()
const {
return GetHandle(); }
161 HWND GetHandle()
const {
return strong.handle ? strong.handle : weak.handle; }
163 void Create(DWORD exStyle,
178 template<
typename MESSAGE>
179 void Bind(UINT message,
void (*
function)(MESSAGE&))
187 template<
typename MESSAGE,
typename FUNCTOR>
188 void Bind(UINT message, FUNCTOR
function)
196 template<
typename CLASS,
typename MESSAGE,
typename HANDLER>
197 void Bind(UINT message,
void (CLASS::*
function)(MESSAGE&), HANDLER* handler)
207 template<
typename MESSAGE>
210 handlerMap.AddHandler(message,
new FunctionCallback<MESSAGE>(
function));
213 template<
typename MESSAGE,
typename FUNCTOR>
216 handlerMap.AddHandler(message,
new FunctorCallback<MESSAGE, FUNCTOR>(
function));
219 template<
typename CLASS,
typename MESSAGE,
typename HANDLER>
220 void Bind(
const MessageId& message,
void (CLASS::*
function)(MESSAGE&), HANDLER* handler)
222 handlerMap.AddHandler(message,
new MemberCallback<CLASS, MESSAGE, HANDLER>(
function, handler));
228 handlerMap.RemoveHandler(
MessageId(message));
234 handlerMap.RemoveHandler(message);
252 void SetText(
const tstring& text);
254 bool IsEnabled()
const;
255 void Enable(
bool enable);
257 bool IsVisible()
const;
258 void Show(
bool show =
true);
259 void Hide() { Show(
false); }
266 HFONT GetFont()
const;
267 void SetFont(HFONT font,
bool redraw =
true);
272 HICON
GetIcon(
bool bigIcon)
const;
278 HICON
SetIcon(HICON icon,
bool bigIcon);
279 HMENU GetMenu()
const;
280 void SetMenu(HMENU menu);
306 UINT_PTR
SetTimer(
int timerID, UINT interval, TIMERPROC proc = 0);
330 void Move(
int x,
int y,
bool redraw =
true);
331 void Move(
const POINT& p,
bool redraw =
true);
332 void Move(
int x,
int y,
int width,
int height,
bool redraw =
true);
333 void Move(
const POINT& p,
const SIZE& s,
bool redraw =
true);
334 void Move(
const RECT& r,
bool redraw =
true);
336 void SetClientSize(
int width,
int height);
338 bool Invalidate(
const RECT* rect = 0,
bool eraseBkgnd =
true);
339 bool Redraw(
const RECT* rect = 0, HRGN region = 0, UINT flags = RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
357 LRESULT GenerateMessage(UINT message, WPARAM wp, LPARAM lp)
const;
359 static HFONT GetDefaultFont();
360 static void SetDefaultFont(HFONT font);
362 static HCURSOR GetDefaultCursor();
363 static void SetDefaultCursor(HCURSOR cursor);
365 static HICON GetDefaultIcon();
366 static void SetDefaultIcon(HICON icon);
368 static WindowBase* GetWindowFromHWND(HWND _handle);
370 static LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
371 LRESULT DefaultWndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
376 virtual LRESULT
WndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
378 bool ExecuteMessageHandler(HWND hwnd, UINT message, WPARAM wp, LPARAM lp, LRESULT& result);
380 static HFONT& _DefaultFont()
382 static HFONT font = 0;
386 static HCURSOR& _DefaultCursor()
388 static HCURSOR defaultCursor = LoadCursor(0, IDC_ARROW);
389 return defaultCursor;
392 static HICON& _DefaultIcon()
394 static HICON defaultIcon = LoadIcon(0, IDI_APPLICATION);
399 typedef std::unordered_map<HWND, WindowBase*> HwndMap;
400 static HwndMap& _GetHwndMap()
407 template<
typename MESSAGE>
408 struct FunctionCallback :
public dev::Functor
410 typedef void (*FUNCTION)(MESSAGE&);
412 FunctionCallback(
const FUNCTION& f)
416 virtual bool operator() (HWND hwnd, UINT message, WPARAM wp, LPARAM lp, LRESULT& result, WindowBase* origin)
419 m.Init(hwnd, message, wp, lp);
420 m.messageHandler = origin;
424 if(m.resultSet && result)
433 template<
typename MESSAGE,
typename FUNCTOR>
434 struct FunctorCallback :
public dev::Functor
436 FunctorCallback(
const FUNCTOR& f)
440 virtual bool operator() (HWND hwnd, UINT message, WPARAM wp, LPARAM lp, LRESULT& result, WindowBase* origin)
443 m.Init(hwnd, message, wp, lp);
444 m.messageHandler = origin;
448 if(m.resultSet && result)
457 template<
typename CLASS,
typename MESSAGE,
typename HANDLER>
458 struct MemberCallback :
public dev::Functor
460 typedef void (CLASS::*FUNCTION)(MESSAGE& m);
462 MemberCallback(FUNCTION f, HANDLER* inst)
467 virtual bool operator() (HWND hwnd, UINT message, WPARAM wp, LPARAM lp, LRESULT& result, WindowBase* origin)
470 m.Init(hwnd, message, wp, lp);
471 m.messageHandler = origin;
473 (instance->*
function)(m);
475 if(m.resultSet && result)
484 dev::StrongHwnd strong;
486 dev::MessageHandlerMap handlerMap;
void Bind(UINT message, FUNCTOR function)
Definition: WindowBase.h:188
virtual LRESULT WndProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
Can be overridden for old school message handling.
Definition: WindowBase.inl:202
The big window class that every other class descends from.
Definition: WindowBase.h:152
HWND GetParentHandle() const
Definition: WindowBase.inl:387
void Bind(UINT message, void(*function)(MESSAGE &))
Definition: WindowBase.h:179
DWORD GetExStyle() const
Definition: WindowBase.inl:377
void Bind(UINT message, void(CLASS::*function)(MESSAGE &), HANDLER *handler)
Definition: WindowBase.h:197
void SetRedraw(bool allowRedraw)
Definition: WindowBase.inl:337
bool IsMessageHandlerBound(UINT message) const
Definition: WindowBase.h:240
DWORD GetID() const
Definition: WindowBase.inl:382
void SetText(const tstring &text)
Definition: WindowBase.inl:312
Rect GetWindowRect() const
Definition: WindowBase.inl:409
UINT_PTR SetTimer(int timerID, UINT interval, TIMERPROC proc=0)
Definition: WindowBase.inl:392
HICON GetIcon(bool bigIcon) const
Definition: WindowBase.inl:352
Rect GetRectOnParent() const
Definition: WindowBase.inl:416
void ModifyStyle(DWORD add, DWORD remove)
Definition: WindowBase.inl:467
void UnBind(const MessageId &message)
Definition: WindowBase.h:232
Unified message identifier.
Definition: MessageId.h:14
void DragAcceptFiles(bool accept=true)
Definition: WindowBase.inl:473
void Bind(const MessageId &message, void(*function)(MESSAGE &))
Definition: WindowBase.h:208
HWND SetFocus()
Definition: WindowBase.inl:462
Windows RECT structure with constructor.
Definition: Rect.h:108
void KillTimer(int timerID)
Definition: WindowBase.inl:397
void UnBind(UINT message)
Definition: WindowBase.h:226
HICON SetIcon(HICON icon, bool bigIcon)
Definition: WindowBase.inl:357
tstring GetText() const
Definition: WindowBase.inl:302
NEO_WINDOW_MODE
Definition: neo.h:23
DWORD GetStyle() const
Definition: WindowBase.inl:372
Rect GetClientRect() const
Definition: WindowBase.inl:402