Mercurial > dwindows
comparison dw.hpp @ 2869:c873b6f862b9
C++: Add text widget and packing to the window.
Also reformat both files using spaces instead of tabs.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sun, 18 Dec 2022 12:13:46 +0000 |
parents | 5ee1aaa48fc7 |
children | 7b4e30c19757 |
comparison
equal
deleted
inserted
replaced
2868:5ee1aaa48fc7 | 2869:c873b6f862b9 |
---|---|
13 // Base handle class which allows opaque access to | 13 // Base handle class which allows opaque access to |
14 // The base system handles | 14 // The base system handles |
15 class Handle | 15 class Handle |
16 { | 16 { |
17 protected: | 17 protected: |
18 void *handle; | 18 void *handle; |
19 void SetHandle(void *newhandle) { handle = newhandle; } | 19 void SetHandle(void *newhandle) { handle = newhandle; } |
20 public: | 20 public: |
21 void *GetHandle() { return handle; } | 21 void *GetHandle() { return handle; } |
22 }; | 22 }; |
23 | 23 |
24 // Widget class allows packing and style | 24 // Widget class allows packing and style |
25 class Widget : public Handle | 25 class Widget : public Handle |
26 { | 26 { |
27 protected: | 27 protected: |
28 void SetHWND(HWND newhwnd) { hwnd = newhwnd; | 28 void SetHWND(HWND newhwnd) { |
29 SetHandle(reinterpret_cast<void *>(newhwnd)); | 29 hwnd = newhwnd; |
30 // Save the C++ class pointer in the window data for later | 30 SetHandle(reinterpret_cast<void *>(newhwnd)); |
31 dw_window_set_data(hwnd, "_dw_classptr", this); | 31 // Save the C++ class pointer in the window data for later |
32 } | 32 dw_window_set_data(hwnd, "_dw_classptr", this); |
33 HWND hwnd; | 33 } |
34 HWND hwnd; | |
34 public: | 35 public: |
35 HWND GetHWND() { return hwnd; } | 36 HWND GetHWND() { return hwnd; } |
36 int Unpack() { return dw_box_unpack(hwnd); } | 37 int Unpack() { return dw_box_unpack(hwnd); } |
37 void SetStyle(unsigned long style, unsigned long mask) { dw_window_set_style(hwnd, style, mask); } | 38 void SetStyle(unsigned long style, unsigned long mask) { dw_window_set_style(hwnd, style, mask); } |
38 void SetTooltip(char *bubbletext) { dw_window_set_tooltip(hwnd, bubbletext); } | 39 void SetTooltip(char *bubbletext) { dw_window_set_tooltip(hwnd, bubbletext); } |
39 }; | 40 }; |
40 | 41 |
41 // Box class is a packable object | 42 // Box class is a packable object |
42 class Box : public Widget | 43 class Box : public Widget |
43 { | 44 { |
44 public: | 45 public: |
45 void PackStart(Widget *item, int width, int height, int hsize, int vsize, int pad) { | 46 void PackStart(Widget *item, int width, int height, int hsize, int vsize, int pad) { |
46 dw_box_pack_start(hwnd, item ? item->GetHWND() : nullptr, width, height, hsize, vsize, pad); } | 47 dw_box_pack_start(hwnd, item ? item->GetHWND() : nullptr, width, height, hsize, vsize, pad); } |
47 void PackEnd(Widget *item, int width, int height, int hsize, int vsize, int pad) { | 48 void PackEnd(Widget *item, int width, int height, int hsize, int vsize, int pad) { |
48 dw_box_pack_end(hwnd, item ? item->GetHWND() : nullptr, width, height, hsize, vsize, pad); } | 49 dw_box_pack_end(hwnd, item ? item->GetHWND() : nullptr, width, height, hsize, vsize, pad); } |
49 void PackAtIndex(Widget *item, int index, int width, int height, int hsize, int vsize, int pad) { | 50 void PackAtIndex(Widget *item, int index, int width, int height, int hsize, int vsize, int pad) { |
50 dw_box_pack_at_index(hwnd, item ? item->GetHWND() : nullptr, index, width, height, hsize, vsize, pad); } | 51 dw_box_pack_at_index(hwnd, item ? item->GetHWND() : nullptr, index, width, height, hsize, vsize, pad); } |
51 Widget *UnpackAtIndex(int index) { HWND widget = dw_box_unpack_at_index(hwnd, index); | 52 Widget *UnpackAtIndex(int index) { HWND widget = dw_box_unpack_at_index(hwnd, index); |
52 void *classptr = widget ? dw_window_get_data(widget, "_dw_classptr") : nullptr; | 53 void *classptr = widget ? dw_window_get_data(widget, "_dw_classptr") : nullptr; |
53 return reinterpret_cast<Widget *>(classptr); | 54 return reinterpret_cast<Widget *>(classptr); |
54 } | 55 } |
55 }; | 56 }; |
56 | 57 |
57 // TODO: Find a way to implement this cross platform... | 58 // TODO: Find a way to implement this cross platform... |
58 // That way we can skip adding unused signal handlers | 59 // That way we can skip adding unused signal handlers |
59 #define IsOverridden(a, b) true | 60 #define IsOverridden(a, b) true |
60 | 61 |
61 // Top-level window class is packable | 62 // Top-level window class is packable |
62 class Window : public Box | 63 class Window : public Box |
63 { | 64 { |
64 private: | 65 private: |
65 void Setup() { | 66 void Setup() { |
66 if(IsOverridden(Window::OnConfigure, this)) | 67 if(IsOverridden(Window::OnConfigure, this)) |
67 dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this); | 68 dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this); |
68 if(IsOverridden(Window::OnConfigure, this)) | 69 if(IsOverridden(Window::OnConfigure, this)) |
69 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); | 70 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); |
70 } | 71 } |
71 static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); } | 72 static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); } |
72 static int _OnConfigure(HWND window, int width, int height, void *data) { return reinterpret_cast<Window *>(data)->OnConfigure(width, height); } | 73 static int _OnConfigure(HWND window, int width, int height, void *data) { return reinterpret_cast<Window *>(data)->OnConfigure(width, height); } |
73 public: | 74 public: |
74 // Constructors | 75 // Constructors |
75 Window(HWND owner, const char *title, unsigned long style) { SetHWND(dw_window_new(owner, title, style)); Setup(); } | 76 Window(HWND owner, const char *title, unsigned long style) { SetHWND(dw_window_new(owner, title, style)); Setup(); } |
76 Window(const char *title, unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, title, style)); Setup(); } | 77 Window(const char *title, unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, title, style)); Setup(); } |
77 Window(unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, "", style)); Setup(); } | 78 Window(unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, "", style)); Setup(); } |
78 Window(const char *title) { SetHWND(dw_window_new(HWND_DESKTOP, title, DW_FCF_SYSMENU | DW_FCF_TITLEBAR | | 79 Window(const char *title) { SetHWND(dw_window_new(HWND_DESKTOP, title, DW_FCF_SYSMENU | DW_FCF_TITLEBAR | |
79 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } | 80 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } |
80 Window() { SetHWND(dw_window_new(HWND_DESKTOP, "", DW_FCF_SYSMENU | DW_FCF_TITLEBAR | | 81 Window() { SetHWND(dw_window_new(HWND_DESKTOP, "", DW_FCF_SYSMENU | DW_FCF_TITLEBAR | |
81 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } | 82 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } |
82 | 83 |
83 // User functions | 84 // User functions |
84 void SetText(const char *text) { dw_window_set_text(hwnd, text); } | 85 void SetText(const char *text) { dw_window_set_text(hwnd, text); } |
85 void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); } | 86 void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); } |
86 int Show() { return dw_window_show(hwnd); } | 87 int Show() { return dw_window_show(hwnd); } |
87 int Hide() { return dw_window_hide(hwnd); } | 88 int Hide() { return dw_window_hide(hwnd); } |
88 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); } | 89 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); } |
89 int Minimize() { return dw_window_minimize(hwnd); } | 90 int Minimize() { return dw_window_minimize(hwnd); } |
90 int Raise() { return dw_window_raise(hwnd); } | 91 int Raise() { return dw_window_raise(hwnd); } |
91 int Lower() { return dw_window_lower(hwnd); } | 92 int Lower() { return dw_window_lower(hwnd); } |
92 protected: | 93 protected: |
93 // Our signal handler functions to be overriden... | 94 // Our signal handler functions to be overriden... |
94 // If they are not overridden and an event is generated, remove the unused handler | 95 // If they are not overridden and an event is generated, remove the unused handler |
95 virtual int OnDelete() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_DELETE); return FALSE; } | 96 virtual int OnDelete() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_DELETE); return FALSE; } |
96 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; | 97 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; |
97 }; | 98 }; |
98 | 99 |
99 // Class for handling static text widget | 100 // Class for handling static text widget |
100 class Text : public Widget | 101 class Text : public Widget |
101 { | 102 { |
102 public: | 103 public: |
103 Text(char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); } | 104 Text(const char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); } |
104 Text(char *text) { SetHWND(dw_text_new(text, 0)); } | 105 Text(const char *text) { SetHWND(dw_text_new(text, 0)); } |
105 Text(unsigned long id) { SetHWND(dw_text_new("", id)); } | 106 Text(unsigned long id) { SetHWND(dw_text_new("", id)); } |
106 Text() { SetHWND(dw_text_new("", 0)); } | 107 Text() { SetHWND(dw_text_new("", 0)); } |
107 void SetText(const char *text) { dw_window_set_text(hwnd, text); } | 108 void SetText(const char *text) { dw_window_set_text(hwnd, text); } |
108 int SetFont(const char *font) { return dw_window_set_font(hwnd, font); } | 109 int SetFont(const char *font) { return dw_window_set_font(hwnd, font); } |
109 char *GetFont() { return dw_window_get_font(hwnd); } | 110 char *GetFont() { return dw_window_get_font(hwnd); } |
110 }; | 111 }; |
111 | 112 |
112 | 113 |
113 class App | 114 class App |
114 { | 115 { |
115 protected: | 116 protected: |
116 App() { } | 117 App() { } |
117 static App *_app; | 118 static App *_app; |
118 public: | 119 public: |
119 // Singletons should not be cloneable. | 120 // Singletons should not be cloneable. |
120 App(App &other) = delete; | 121 App(App &other) = delete; |
121 // Singletons should not be assignable. | 122 // Singletons should not be assignable. |
122 void operator=(const App &) = delete; | 123 void operator=(const App &) = delete; |
123 // Initialization functions for creating App | 124 // Initialization functions for creating App |
124 static App *Init() { if(!_app) { _app = new App; dw_init(TRUE, 0, NULL); } return _app; } | 125 static App *Init() { if(!_app) { _app = new App; dw_init(TRUE, 0, NULL); } return _app; } |
125 static App *Init(const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, NULL); dw_init(TRUE, 0, NULL); } return _app; } | 126 static App *Init(const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, NULL); dw_init(TRUE, 0, NULL); } return _app; } |
126 static App *Init(const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, 0, NULL); } return _app; } | 127 static App *Init(const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, 0, NULL); } return _app; } |
127 static App *Init(int argc, char *argv[]) { if(!_app) { _app = new App(); dw_init(TRUE, argc, argv); } return _app; } | 128 static App *Init(int argc, char *argv[]) { if(!_app) { _app = new App(); dw_init(TRUE, argc, argv); } return _app; } |
128 static App *Init(int argc, char *argv[], const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, NULL); dw_init(TRUE, argc, argv); } return _app; } | 129 static App *Init(int argc, char *argv[], const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, NULL); dw_init(TRUE, argc, argv); } return _app; } |
129 static App *Init(int argc, char *argv[], const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, argc, argv); } return _app; } | 130 static App *Init(int argc, char *argv[], const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, argc, argv); } return _app; } |
130 | 131 |
131 void Main() { dw_main(); } | 132 void Main() { dw_main(); } |
132 void MainIteration() { dw_main_iteration(); } | 133 void MainIteration() { dw_main_iteration(); } |
133 void MainQuit() { dw_main_quit(); } | 134 void MainQuit() { dw_main_quit(); } |
134 void Exit(int exitcode) { dw_exit(exitcode); } | 135 void Exit(int exitcode) { dw_exit(exitcode); } |
135 }; | 136 }; |
136 | 137 |
137 // Static singleton reference declared outside of the class | 138 // Static singleton reference declared outside of the class |
138 App* App::_app = nullptr; | 139 App* App::_app = nullptr; |
139 | 140 |
140 #if 0 | 141 #if 0 |
141 // Class that allows drawing, either to screen or picture (pixmap) | 142 // Class that allows drawing, either to screen or picture (pixmap) |
142 class Drawable | 143 class Drawable |
143 { | 144 { |
144 void DrawPoint(int x, int y); | 145 void DrawPoint(int x, int y); |
145 void DrawLine(int x1, int y1, int x2, int y2); | 146 void DrawLine(int x1, int y1, int x2, int y2); |
146 void DrawPolygon(int flags, int x[], int y[]); | 147 void DrawPolygon(int flags, int x[], int y[]); |
147 void DrawRect(int fill, int x, int y, int width, int height); | 148 void DrawRect(int fill, int x, int y, int width, int height); |
148 void DrawArc(int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2); | 149 void DrawArc(int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2); |
149 void DrawText(int x, int y, std::string text); | 150 void DrawText(int x, int y, std::string text); |
150 int BitBltStretch(int xdest, int ydest, int width, int height, HWND src, int xsrc, int ysrc, int srcwidth, int srcheight); | 151 int BitBltStretch(int xdest, int ydest, int width, int height, HWND src, int xsrc, int ysrc, int srcwidth, int srcheight); |
151 int BitBltStretch(int xdest, int ydest, int width, int height, HPIXMAP srcp, int xsrc, int ysrc, int srcwidth, int srcheight); | 152 int BitBltStretch(int xdest, int ydest, int width, int height, HPIXMAP srcp, int xsrc, int ysrc, int srcwidth, int srcheight); |
152 void BitBlt(int xdest, int ydest, int width, int height, HWND src, int xsrc, int ysrc); | 153 void BitBlt(int xdest, int ydest, int width, int height, HWND src, int xsrc, int ysrc); |
153 void BitBlt(int xdest, int ydest, int width, int height, HPIXMAP srcp, int xsrc, int ysrc); | 154 void BitBlt(int xdest, int ydest, int width, int height, HPIXMAP srcp, int xsrc, int ysrc); |
154 }; | 155 }; |
155 | 156 |
156 class Render : public Drawable, public Widget | 157 class Render : public Drawable, public Widget |
157 { | 158 { |
158 }; | 159 }; |