comparison dw.hpp @ 2874:585d0053b766

C++: Implement buttons, images, render, pixmap and boxes.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 19 Dec 2022 13:57:43 +0000
parents 0bbfb19022e7
children e201a984d855
comparison
equal deleted inserted replaced
2873:0bbfb19022e7 2874:585d0053b766
47 public: 47 public:
48 HWND GetHWND() { return hwnd; } 48 HWND GetHWND() { return hwnd; }
49 int Unpack() { return dw_box_unpack(hwnd); } 49 int Unpack() { return dw_box_unpack(hwnd); }
50 void SetStyle(unsigned long style, unsigned long mask) { dw_window_set_style(hwnd, style, mask); } 50 void SetStyle(unsigned long style, unsigned long mask) { dw_window_set_style(hwnd, style, mask); }
51 void SetTooltip(char *bubbletext) { dw_window_set_tooltip(hwnd, bubbletext); } 51 void SetTooltip(char *bubbletext) { dw_window_set_tooltip(hwnd, bubbletext); }
52 int SetColor(unsigned long fore, unsigned long back) { return dw_window_set_color(hwnd, fore, back); }
52 }; 53 };
53 54
54 // Box class is a packable object 55 // Box class is a packable object
55 class Box : public Widget 56 class Boxes : public Widget
56 { 57 {
57 public: 58 public:
59 // User functions
58 void PackStart(Widget *item, int width, int height, int hsize, int vsize, int pad) { 60 void PackStart(Widget *item, int width, int height, int hsize, int vsize, int pad) {
59 dw_box_pack_start(hwnd, item ? item->GetHWND() : DW_NOHWND, width, height, hsize, vsize, pad); } 61 dw_box_pack_start(hwnd, item ? item->GetHWND() : DW_NOHWND, width, height, hsize, vsize, pad); }
60 void PackEnd(Widget *item, int width, int height, int hsize, int vsize, int pad) { 62 void PackEnd(Widget *item, int width, int height, int hsize, int vsize, int pad) {
61 dw_box_pack_end(hwnd, item ? item->GetHWND() : DW_NOHWND, width, height, hsize, vsize, pad); } 63 dw_box_pack_end(hwnd, item ? item->GetHWND() : DW_NOHWND, width, height, hsize, vsize, pad); }
62 void PackAtIndex(Widget *item, int index, int width, int height, int hsize, int vsize, int pad) { 64 void PackAtIndex(Widget *item, int index, int width, int height, int hsize, int vsize, int pad) {
65 void *classptr = widget ? dw_window_get_data(widget, "_dw_classptr") : DW_NULL; 67 void *classptr = widget ? dw_window_get_data(widget, "_dw_classptr") : DW_NULL;
66 return reinterpret_cast<Widget *>(classptr); 68 return reinterpret_cast<Widget *>(classptr);
67 } 69 }
68 }; 70 };
69 71
72 class Box : public Boxes
73 {
74 // Constructors
75 Box(int type, int pad) { SetHWND(dw_box_new(type, pad)); }
76 Box(int type) { SetHWND(dw_box_new(type, 0)); }
77 };
78
79 // Special scrollable box
80 class ScrollBox : public Boxes
81 {
82 public:
83 // Constructors
84 ScrollBox(int type, int pad) { SetHWND(dw_scrollbox_new(type, pad)); }
85 ScrollBox(int type) { SetHWND(dw_scrollbox_new(type, 0)); }
86
87 // User functions
88 int GetRange(int orient) { return dw_scrollbox_get_range(hwnd, orient); }
89 int GetPos(int orient) { return dw_scrollbox_get_pos(hwnd, orient); }
90 };
91
70 // TODO: Find a way to implement this cross platform... 92 // TODO: Find a way to implement this cross platform...
71 // That way we can skip adding unused signal handlers 93 // That way we can skip adding unused signal handlers
72 #define IsOverridden(a, b) true 94 #define IsOverridden(a, b) true
73 95
74 // Top-level window class is packable 96 // Top-level window class is packable
75 class Window : public Box 97 class Window : public Boxes
76 { 98 {
77 private: 99 private:
78 void Setup() { 100 void Setup() {
79 if(IsOverridden(Window::OnConfigure, this)) 101 if(IsOverridden(Window::OnDelete, this))
80 dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this); 102 dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this);
81 if(IsOverridden(Window::OnConfigure, this)) 103 if(IsOverridden(Window::OnConfigure, this))
82 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); 104 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this);
83 } 105 }
84 static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); } 106 static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); }
93 Window() { SetHWND(dw_window_new(HWND_DESKTOP, "", DW_FCF_SYSMENU | DW_FCF_TITLEBAR | 115 Window() { SetHWND(dw_window_new(HWND_DESKTOP, "", DW_FCF_SYSMENU | DW_FCF_TITLEBAR |
94 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } 116 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); }
95 117
96 // User functions 118 // User functions
97 void SetText(const char *text) { dw_window_set_text(hwnd, text); } 119 void SetText(const char *text) { dw_window_set_text(hwnd, text); }
120 char *GetText() { return dw_window_get_text(hwnd); }
98 void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); } 121 void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); }
99 int Show() { return dw_window_show(hwnd); } 122 int Show() { return dw_window_show(hwnd); }
100 int Hide() { return dw_window_hide(hwnd); } 123 int Hide() { return dw_window_hide(hwnd); }
101 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); } 124 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); }
102 int Minimize() { return dw_window_minimize(hwnd); } 125 int Minimize() { return dw_window_minimize(hwnd); }
107 // If they are not overridden and an event is generated, remove the unused handler 130 // If they are not overridden and an event is generated, remove the unused handler
108 virtual int OnDelete() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_DELETE); return FALSE; } 131 virtual int OnDelete() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_DELETE); return FALSE; }
109 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; 132 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; };
110 }; 133 };
111 134
135 // Base class for several types of buttons
136 class Buttons : public Widget
137 {
138 private:
139 static int _OnClicked(HWND window, void *data) { return reinterpret_cast<Buttons *>(data)->OnClicked(); }
140 protected:
141 void Setup() {
142 if(IsOverridden(Window::OnClicked, this))
143 dw_signal_connect(hwnd, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_OnClicked), this);
144 }
145 // Our signal handler functions to be overriden...
146 // If they are not overridden and an event is generated, remove the unused handler
147 virtual int OnClicked() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CLICKED); return FALSE; }
148 };
149
150 // Text based button
151 class TextButton : public Buttons
152 {
153 public:
154 // User functions
155 void SetText(const char *text) { dw_window_set_text(hwnd, text); }
156 char *GetText() { return dw_window_get_text(hwnd); }
157 };
158
159 class Button : public TextButton
160 {
161 public:
162 // Constructors
163 Button(const char *text, unsigned long id) { SetHWND(dw_button_new(text, id)); Setup(); }
164 Button(unsigned long id) { SetHWND(dw_button_new("", id)); Setup(); }
165 Button(const char *text) { SetHWND(dw_button_new(text, 0)); Setup(); }
166 Button() { SetHWND(dw_button_new("", 0)); Setup(); }
167 };
168
169 // Image based button
170 class BitmapButton : public Buttons
171 {
172 public:
173 // Constructors
174 BitmapButton(const char *text, unsigned long id) { SetHWND(dw_bitmapbutton_new(text, id)); Setup(); }
175 BitmapButton(unsigned long id) { SetHWND(dw_bitmapbutton_new("", id)); Setup(); }
176 BitmapButton(const char *text, unsigned long id, const char *file) { SetHWND(dw_bitmapbutton_new_from_file(text, id, file)); Setup(); }
177 BitmapButton(const char *text, const char *file) { SetHWND(dw_bitmapbutton_new_from_file(text, 0, file)); Setup(); }
178 BitmapButton(const char *text, unsigned long id, const char *data, int len) { SetHWND(dw_bitmapbutton_new_from_data(text, id, data, len)); Setup(); }
179 BitmapButton(const char *text, const char *data, int len) { SetHWND(dw_bitmapbutton_new_from_data(text, 0, data, len)); Setup(); }
180 };
181
182 class CheckBoxes : public TextButton
183 {
184 // User functions
185 void Set(int value) { dw_checkbox_set(hwnd, value); }
186 int Get() { return dw_checkbox_get(hwnd); }
187 };
188
189 class CheckBox : public CheckBoxes
190 {
191 // Constructors
192 CheckBox(const char *text, unsigned long id) { SetHWND(dw_checkbox_new(text, id)); Setup(); }
193 CheckBox(unsigned long id) { SetHWND(dw_checkbox_new("", id)); Setup(); }
194 CheckBox(const char *text) { SetHWND(dw_checkbox_new(text, 0)); Setup(); }
195 CheckBox() { SetHWND(dw_checkbox_new("", 0)); Setup(); }
196 };
197
198 class RadioButton : public CheckBoxes
199 {
200 // Constructors
201 RadioButton(const char *text, unsigned long id) { SetHWND(dw_radiobutton_new(text, id)); Setup(); }
202 RadioButton(unsigned long id) { SetHWND(dw_radiobutton_new("", id)); Setup(); }
203 RadioButton(const char *text) { SetHWND(dw_radiobutton_new(text, 0)); Setup(); }
204 RadioButton() { SetHWND(dw_radiobutton_new("", 0)); Setup(); }
205 };
206
112 // Class for handling static text widget 207 // Class for handling static text widget
113 class Text : public Widget 208 class Text : public Widget
114 { 209 {
115 public: 210 public:
211 // Constructors
116 Text(const char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); } 212 Text(const char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); }
117 Text(const char *text) { SetHWND(dw_text_new(text, 0)); } 213 Text(const char *text) { SetHWND(dw_text_new(text, 0)); }
118 Text(unsigned long id) { SetHWND(dw_text_new("", id)); } 214 Text(unsigned long id) { SetHWND(dw_text_new("", id)); }
119 Text() { SetHWND(dw_text_new("", 0)); } 215 Text() { SetHWND(dw_text_new("", 0)); }
216
217 // User functions
120 void SetText(const char *text) { dw_window_set_text(hwnd, text); } 218 void SetText(const char *text) { dw_window_set_text(hwnd, text); }
121 int SetFont(const char *font) { return dw_window_set_font(hwnd, font); } 219 int SetFont(const char *font) { return dw_window_set_font(hwnd, font); }
122 char *GetFont() { return dw_window_get_font(hwnd); } 220 char *GetFont() { return dw_window_get_font(hwnd); }
123 }; 221 };
124 222
223 // Class for handing static image widget
224 class Bitmap : public Widget
225 {
226 public:
227 // Constructors
228 Bitmap(const char *data, int len) { SetHWND(dw_bitmap_new(0)); dw_window_set_bitmap_from_data(hwnd, 0, data, len); }
229 Bitmap(const char *file) { SetHWND(dw_bitmap_new(0)); dw_window_set_bitmap(hwnd, 0, file); }
230 Bitmap(unsigned long id) { SetHWND(dw_bitmap_new(id)); }
231 Bitmap() { SetHWND(dw_bitmap_new(0)); }
232
233 // User functions
234 void Set(unsigned long id) { dw_window_set_bitmap(hwnd, id, DW_NULL); }
235 void Set(const char *file) { dw_window_set_bitmap(hwnd, 0, file); }
236 void Set(const char *data, int len) { dw_window_set_bitmap_from_data(hwnd, 0, data, len); }
237 };
238
239 // Forward declare these so our Drawable abstract class can reference
240 class Render;
241 class Pixmap;
242
243 // Abstract class that defines drawing, either to screen or picture (pixmap)
244 class Drawable
245 {
246 public:
247 virtual void DrawPoint(int x, int y) = 0;
248 virtual void DrawLine(int x1, int y1, int x2, int y2) = 0;
249 virtual void DrawPolygon(int flags, int npoints, int x[], int y[]) = 0;
250 virtual void DrawRect(int fill, int x, int y, int width, int height) = 0;
251 virtual void DrawArc(int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2) = 0;
252 virtual void DrawText(int x, int y, const char *text) = 0;
253 virtual int BitBltStretch(int xdest, int ydest, int width, int height, Render *src, int xsrc, int ysrc, int srcwidth, int srcheight) = 0;
254 virtual int BitBltStretch(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc, int srcwidth, int srcheight) = 0;
255 virtual void BitBlt(int xdest, int ydest, int width, int height, Render *src, int xsrc, int ysrc) = 0;
256 virtual void BitBlt(int xdest, int ydest, int width, int height, Pixmap *srcp, int xsrc, int ysrc) = 0;
257 void SetColor(unsigned long fore, unsigned long back) { dw_color_foreground_set(fore); dw_color_background_set(back); }
258 void SetBackgroundColor(unsigned long back) { dw_color_background_set(back); }
259 void SetForegroundColor(unsigned long fore) { dw_color_foreground_set(fore); }
260 };
261
262 class Render : public Drawable, public Widget
263 {
264 private:
265 void Setup() {
266 if(IsOverridden(Window::OnExpose, this))
267 dw_signal_connect(hwnd, DW_SIGNAL_EXPOSE, DW_SIGNAL_FUNC(_OnExpose), this);
268 if(IsOverridden(Window::OnConfigure, this))
269 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this);
270 }
271 static int _OnExpose(HWND window, DWExpose *exp, void *data) { return reinterpret_cast<Render *>(data)->OnExpose(exp); }
272 static int _OnConfigure(HWND window, int width, int height, void *data) { return reinterpret_cast<Render *>(data)->OnConfigure(width, height); }
273 public:
274 // Constructors
275 Render(unsigned long id) { SetHWND(dw_render_new(id)); Setup(); }
276 Render() { SetHWND(dw_render_new(0)); Setup(); }
277
278 // User functions
279 void DrawPoint(int x, int y) { dw_draw_point(hwnd, DW_NULL, x, y); }
280 void DrawLine(int x1, int y1, int x2, int y2) { dw_draw_line(hwnd, DW_NULL, x1, y1, x2, y2); }
281 void DrawPolygon(int flags, int npoints, int x[], int y[]) { dw_draw_polygon(hwnd, DW_NULL, flags, npoints, x, y); }
282 void DrawRect(int fill, int x, int y, int width, int height) { dw_draw_rect(hwnd, DW_NULL, fill, x, y, width, height); }
283 void DrawArc(int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2) { dw_draw_arc(hwnd, DW_NULL, flags, xorigin, yorigin, x1, y1, x2, y2); }
284 void DrawText(int x, int y, const char *text) { dw_draw_text(hwnd, DW_NULL, x, y, text); }
285 int BitBltStretch(int xdest, int ydest, int width, int height, Render *src, int xsrc, int ysrc, int srcwidth, int srcheight) {
286 return dw_pixmap_stretch_bitblt(hwnd, DW_NULL, xdest, ydest, width, height, src ? src->GetHWND() : DW_NOHWND, DW_NULL, xsrc, ysrc, srcwidth, srcheight);
287 }
288 int BitBltStretch(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc, int srcwidth, int srcheight);
289 void BitBlt(int xdest, int ydest, int width, int height, Render *src, int xsrc, int ysrc) {
290 return dw_pixmap_bitblt(hwnd, DW_NULL, xdest, ydest, width, height, src ? src->GetHWND() : DW_NOHWND, DW_NULL, xsrc, ysrc);
291 }
292 void BitBlt(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc);
293 int SetFont(const char *fontname) { return dw_window_set_font(hwnd, fontname); }
294 char *GetFont() { return dw_window_get_font(hwnd); }
295 protected:
296 // Our signal handler functions to be overriden...
297 // If they are not overridden and an event is generated, remove the unused handler
298 virtual int OnExpose(DWExpose *exp) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_EXPOSE); return FALSE; }
299 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; };
300 };
301
302 class Pixmap : public Drawable, public Handle
303 {
304 protected:
305 void SetHPIXMAP(HPIXMAP newpixmap) {
306 hpixmap = newpixmap;
307 SetHandle(reinterpret_cast<void *>(newpixmap));
308 }
309 HPIXMAP hpixmap;
310 public:
311 // Constructors
312 Pixmap(HWND window, unsigned long width, unsigned long height, int depth) { SetHPIXMAP(dw_pixmap_new(window, width, height, depth)); }
313 Pixmap(HWND window, unsigned long width, unsigned long height) { SetHPIXMAP(dw_pixmap_new(window, width, height, 32)); }
314
315 // User functions
316 HPIXMAP GetHPIXMAP() { return hpixmap; }
317 void DrawPoint(int x, int y) { dw_draw_point(DW_NOHWND, hpixmap, x, y); }
318 void DrawLine(int x1, int y1, int x2, int y2) { dw_draw_line(DW_NOHWND, hpixmap, x1, y1, x2, y2); }
319 void DrawPolygon(int flags, int npoints, int x[], int y[]) { dw_draw_polygon(DW_NOHWND, hpixmap, flags, npoints, x, y); }
320 void DrawRect(int fill, int x, int y, int width, int height) { dw_draw_rect(DW_NOHWND, hpixmap, fill, x, y, width, height); }
321 void DrawArc(int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2) { dw_draw_arc(DW_NOHWND, hpixmap, flags, xorigin, yorigin, x1, y1, x2, y2); }
322 void DrawText(int x, int y, const char *text) { dw_draw_text(DW_NOHWND, hpixmap, x, y, text); }
323 int BitBltStretch(int xdest, int ydest, int width, int height, Render *src, int xsrc, int ysrc, int srcwidth, int srcheight) {
324 return dw_pixmap_stretch_bitblt(DW_NOHWND, hpixmap, xdest, ydest, width, height, src ? src->GetHWND() : DW_NOHWND, DW_NULL, xsrc, ysrc, srcwidth, srcheight);
325 }
326 int BitBltStretch(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc, int srcwidth, int srcheight) {
327 return dw_pixmap_stretch_bitblt(DW_NOHWND, hpixmap, xdest, ydest, width, height, DW_NOHWND, src ? src->GetHPIXMAP() : DW_NULL, xsrc, ysrc, srcwidth, srcheight);
328 }
329 void BitBlt(int xdest, int ydest, int width, int height, Render *src, int xsrc, int ysrc) {
330 dw_pixmap_bitblt(DW_NOHWND, hpixmap, xdest, ydest, width, height, src ? src->GetHWND() : DW_NOHWND, DW_NULL, xsrc, ysrc);
331 }
332 void BitBlt(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc) {
333 dw_pixmap_bitblt(DW_NOHWND, hpixmap, xdest, ydest, width, height, DW_NOHWND, src ? src->GetHPIXMAP() : DW_NULL, xsrc, ysrc);
334 }
335 int SetFont(const char *fontname) { return dw_pixmap_set_font(hpixmap, fontname); }
336 };
337
338 // Need to declare these here after Pixmap is defined
339 int Render::BitBltStretch(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc, int srcwidth, int srcheight)
340 {
341 return dw_pixmap_stretch_bitblt(hwnd, DW_NULL, xdest, ydest, width, height, DW_NOHWND, src ? src->GetHPIXMAP() : DW_NULL, xsrc, ysrc, srcwidth, srcheight);
342 }
343
344 void Render::BitBlt(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc)
345 {
346 dw_pixmap_bitblt(hwnd, DW_NULL, xdest, ydest, width, height, DW_NOHWND, src ? src->GetHPIXMAP() : DW_NULL, xsrc, ysrc);
347 }
125 348
126 class App 349 class App
127 { 350 {
128 protected: 351 protected:
129 App() { } 352 App() { }
151 }; 374 };
152 375
153 // Static singleton reference declared outside of the class 376 // Static singleton reference declared outside of the class
154 App* App::_app = DW_NULL; 377 App* App::_app = DW_NULL;
155 378
156 #if 0
157 // Class that allows drawing, either to screen or picture (pixmap)
158 class Drawable
159 {
160 void DrawPoint(int x, int y);
161 void DrawLine(int x1, int y1, int x2, int y2);
162 void DrawPolygon(int flags, int x[], int y[]);
163 void DrawRect(int fill, int x, int y, int width, int height);
164 void DrawArc(int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2);
165 void DrawText(int x, int y, std::string text);
166 int BitBltStretch(int xdest, int ydest, int width, int height, HWND src, int xsrc, int ysrc, int srcwidth, int srcheight);
167 int BitBltStretch(int xdest, int ydest, int width, int height, HPIXMAP srcp, int xsrc, int ysrc, int srcwidth, int srcheight);
168 void BitBlt(int xdest, int ydest, int width, int height, HWND src, int xsrc, int ysrc);
169 void BitBlt(int xdest, int ydest, int width, int height, HPIXMAP srcp, int xsrc, int ysrc);
170 };
171
172 class Render : public Drawable, public Widget
173 {
174 };
175
176 class Pixmap : public Drawable, public Handle
177 {
178 };
179 #endif
180
181 } /* namespace DW */ 379 } /* namespace DW */
182 #endif 380 #endif