| 1 |
#ifndef H_WINDOW /* -*- mode: c++ -*- */ |
| 2 |
/* |
| 3 |
* Copyright (C) 2009-2016 TSUBAKIMOTO Hiroya <z0rac@users.sourceforge.jp> |
| 4 |
* |
| 5 |
* This software comes with ABSOLUTELY NO WARRANTY; for details of |
| 6 |
* the license terms, see the LICENSE.txt file included with the program. |
| 7 |
*/ |
| 8 |
#define H_WINDOW |
| 9 |
|
| 10 |
#include <list> |
| 11 |
#include <memory> |
| 12 |
#include <windows.h> |
| 13 |
#include <windowsx.h> |
| 14 |
#include <commctrl.h> |
| 15 |
|
| 16 |
using namespace std; |
| 17 |
|
| 18 |
class window { |
| 19 |
HWND _hwnd; |
| 20 |
WNDPROC _callback; |
| 21 |
HWND _new(LPCSTR classname, LPCSTR menu, HWND owner); |
| 22 |
HWND _new(LPCSTR classname, const window& parent, int id); |
| 23 |
void _initialize(); |
| 24 |
void _release(bool destroy = false); |
| 25 |
static LRESULT CALLBACK _wndproc(HWND h, UINT m, WPARAM w, LPARAM l); |
| 26 |
void _updatemenu(HMENU h); |
| 27 |
protected: |
| 28 |
const window& self() const { return *this; } |
| 29 |
void style(DWORD style, DWORD ex = 0) const; |
| 30 |
virtual LRESULT dispatch(UINT m, WPARAM w, LPARAM l); |
| 31 |
virtual void release() {} |
| 32 |
virtual void resize(int, int) {} |
| 33 |
virtual LRESULT notify(WPARAM w, LPARAM l); |
| 34 |
virtual bool callback(LPMEASUREITEMSTRUCT misp); |
| 35 |
virtual bool callback(LPDRAWITEMSTRUCT disp); |
| 36 |
|
| 37 |
struct commctrl { |
| 38 |
commctrl(DWORD icc); |
| 39 |
}; |
| 40 |
|
| 41 |
class menu { |
| 42 |
HMENU _h; |
| 43 |
public: |
| 44 |
menu(LPCSTR name, int pos = 0); |
| 45 |
~menu() { DestroyMenu(_h); } |
| 46 |
operator HMENU() const { return _h; } |
| 47 |
}; |
| 48 |
void execute(const menu& menu); |
| 49 |
virtual bool popup(const menu& menu, LPARAM pt); |
| 50 |
public: |
| 51 |
window(LPCSTR classname, LPCSTR menu = NULL, HWND owner = NULL); |
| 52 |
window(LPCSTR classname, const window& parent, int id = -1); |
| 53 |
virtual ~window(); |
| 54 |
static int eventloop(); |
| 55 |
static void broadcast(UINT m, WPARAM w, LPARAM l); |
| 56 |
public: |
| 57 |
HWND hwnd() const { return _hwnd; } |
| 58 |
bool visible() const { return IsWindowVisible(_hwnd) != 0; } |
| 59 |
bool child() const; |
| 60 |
void close(bool root = false) const; |
| 61 |
void show(bool show = true, bool active = true) const; |
| 62 |
void foreground(bool force = false) const; |
| 63 |
bool topmost() const; |
| 64 |
void topmost(bool topmost); |
| 65 |
void transparent(int alpha, COLORREF key = COLORREF(-1)); |
| 66 |
void move(int x, int y, int w, int h) const; |
| 67 |
void move(const RECT& r) const; |
| 68 |
void invalidate() const { InvalidateRect(_hwnd, NULL, TRUE); } |
| 69 |
bool hascursor(bool child = true) const; |
| 70 |
POINT extent() const; |
| 71 |
RECT bounds() const; |
| 72 |
public: |
| 73 |
class command { |
| 74 |
command(const command&); void operator=(const command&); // disable to copy |
| 75 |
public: |
| 76 |
const int icon; |
| 77 |
command(int icon = 0) : icon(icon) {} |
| 78 |
virtual ~command() {} |
| 79 |
virtual void execute(window& source) = 0; |
| 80 |
virtual UINT state(window&) { return 0; } |
| 81 |
}; |
| 82 |
struct cmdp : public shared_ptr<command> { |
| 83 |
typedef shared_ptr<command> super; |
| 84 |
cmdp(command* cmd = NULL) : super(cmd) {} |
| 85 |
cmdp(cmdp const& cmd) : super(cmd) {} |
| 86 |
}; |
| 87 |
void addcmd(int id, cmdp cmd); |
| 88 |
virtual void execute(int id); |
| 89 |
private: |
| 90 |
typedef list< pair<int, cmdp> > cmdmap; |
| 91 |
cmdmap _cmdmap; |
| 92 |
command* _cmd(int id); |
| 93 |
public: |
| 94 |
class timer { |
| 95 |
UINT _elapse; |
| 96 |
DWORD _start; |
| 97 |
static VOID CALLBACK _callback(HWND hwnd, UINT, UINT_PTR id, DWORD ticks); |
| 98 |
public: |
| 99 |
timer() : _elapse(0) {} |
| 100 |
virtual ~timer() {} |
| 101 |
void operator()(HWND hwnd, UINT ms); |
| 102 |
virtual void wakeup(window& source) = 0; |
| 103 |
}; |
| 104 |
void settimer(timer& tm, UINT ms) const { tm(_hwnd, ms); } |
| 105 |
}; |
| 106 |
|
| 107 |
class appwindow : public window { |
| 108 |
static LPCSTR _classname(); |
| 109 |
protected: |
| 110 |
LRESULT dispatch(UINT m, WPARAM w, LPARAM l); |
| 111 |
LRESULT notify(WPARAM w, LPARAM l); |
| 112 |
virtual void draw(HDC) {} |
| 113 |
virtual void erase(HDC) {} |
| 114 |
virtual void limit(LPMINMAXINFO) {} |
| 115 |
virtual void raised(bool) {} |
| 116 |
const RECT& adjust(RECT& bounds, int border = 8) const; |
| 117 |
const RECT& adjust(RECT& bounds, const RECT& monitor, int border = 8) const; |
| 118 |
public: |
| 119 |
appwindow(LPCSTR menu = NULL, HWND owner = NULL) |
| 120 |
: window(_classname(), menu, owner) {} |
| 121 |
appwindow(const window& parent, int id = -1) |
| 122 |
: window(_classname(), parent, id) {} |
| 123 |
}; |
| 124 |
|
| 125 |
#endif |