Mercurial > dwindows
comparison dw.hpp @ 2878:a290573a8b7c
C++: Implement StatusText class, reorganize Text widgets to eliminate
duplication and implement a bunch of random functions from the list.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 20 Dec 2022 05:50:15 +0000 |
parents | 6f31b7991fa0 |
children | ec7f6e28166d |
comparison
equal
deleted
inserted
replaced
2877:6f31b7991fa0 | 2878:a290573a8b7c |
---|---|
1 /* Dynamic Windows C++ Language Bindings | 1 /* Dynamic Windows C++ Language Bindings |
2 * Copyright 2022 Brian Smith | 2 * Copyright 2022 Brian Smith |
3 * Requires a C++11 compatible compiler. | 3 * Recommends a C++11 compatible compiler. |
4 */ | 4 */ |
5 | 5 |
6 #ifndef _HPP_DW | 6 #ifndef _HPP_DW |
7 #define _HPP_DW | 7 #define _HPP_DW |
8 #include <dw.h> | 8 #include <dw.h> |
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 int SetColor(unsigned long fore, unsigned long back) { return dw_window_set_color(hwnd, fore, back); } |
53 void SetData(const char *dataname, void *data) { dw_window_set_data(hwnd, dataname, data); } | 53 void SetData(const char *dataname, void *data) { dw_window_set_data(hwnd, dataname, data); } |
54 void *GetData(const char *dataname) { return dw_window_get_data(hwnd, dataname); } | 54 void *GetData(const char *dataname) { return dw_window_get_data(hwnd, dataname); } |
55 void SetPointer(int cursortype) { dw_window_set_pointer(hwnd, cursortype); } | |
56 Widget *FromID(int id) { | |
57 HWND child = dw_window_from_id(hwnd, id); | |
58 if(child) { | |
59 return reinterpret_cast<Widget *>(dw_window_get_data(child, "_dw_classptr")); | |
60 } | |
61 return DW_NULL; | |
62 } | |
63 void GetPreferredSize(int *width, int *height) { dw_window_get_preferred_size(hwnd, width, height); } | |
55 }; | 64 }; |
56 | 65 |
57 // Box class is a packable object | 66 // Box class is a packable object |
58 class Boxes : virtual public Widget | 67 class Boxes : virtual public Widget |
59 { | 68 { |
126 int Hide() { return dw_window_hide(hwnd); } | 135 int Hide() { return dw_window_hide(hwnd); } |
127 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); } | 136 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); } |
128 int Minimize() { return dw_window_minimize(hwnd); } | 137 int Minimize() { return dw_window_minimize(hwnd); } |
129 int Raise() { return dw_window_raise(hwnd); } | 138 int Raise() { return dw_window_raise(hwnd); } |
130 int Lower() { return dw_window_lower(hwnd); } | 139 int Lower() { return dw_window_lower(hwnd); } |
140 void Redraw() { dw_window_redraw(hwnd); } | |
141 void Default(Widget *defaultitem) { if(defaultitem) dw_window_default(hwnd, defaultitem->GetHWND()); } | |
142 void SetIcon(HICN icon) { dw_window_set_icon(hwnd, icon); } | |
131 protected: | 143 protected: |
132 // Our signal handler functions to be overriden... | 144 // Our signal handler functions to be overriden... |
133 // If they are not overridden and an event is generated, remove the unused handler | 145 // If they are not overridden and an event is generated, remove the unused handler |
134 virtual int OnDelete() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_DELETE); return FALSE; } | 146 virtual int OnDelete() { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_DELETE); return FALSE; } |
135 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; | 147 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; |
136 }; | 148 }; |
137 | 149 |
150 // Class for focusable widgets | |
151 class Focusable : virtual public Widget | |
152 { | |
153 public: | |
154 void Enable() { dw_window_enable(hwnd); } | |
155 void Disable() { dw_window_disable(hwnd); } | |
156 void SetFocus() { dw_window_set_focus(hwnd); } | |
157 }; | |
158 | |
138 // Base class for several types of buttons | 159 // Base class for several types of buttons |
139 class Buttons : virtual public Widget | 160 class Buttons : virtual public Focusable |
140 { | 161 { |
141 private: | 162 private: |
142 static int _OnClicked(HWND window, void *data) { return reinterpret_cast<Buttons *>(data)->OnClicked(); } | 163 static int _OnClicked(HWND window, void *data) { return reinterpret_cast<Buttons *>(data)->OnClicked(); } |
143 protected: | 164 protected: |
144 void Setup() { | 165 void Setup() { |
209 RadioButton(const char *text) { SetHWND(dw_radiobutton_new(text, 0)); Setup(); } | 230 RadioButton(const char *text) { SetHWND(dw_radiobutton_new(text, 0)); Setup(); } |
210 RadioButton() { SetHWND(dw_radiobutton_new("", 0)); Setup(); } | 231 RadioButton() { SetHWND(dw_radiobutton_new("", 0)); Setup(); } |
211 }; | 232 }; |
212 | 233 |
213 // Class for handling static text widget | 234 // Class for handling static text widget |
214 class Text : public Widget | 235 class TextWidget : virtual public Widget |
236 { | |
237 public: | |
238 // User functions | |
239 void SetText(const char *text) { dw_window_set_text(hwnd, text); } | |
240 char *GetText() { return dw_window_get_text(hwnd); } | |
241 int SetFont(const char *font) { return dw_window_set_font(hwnd, font); } | |
242 char *GetFont() { return dw_window_get_font(hwnd); } | |
243 }; | |
244 | |
245 // Class for handling static text widget | |
246 class Text : public TextWidget | |
215 { | 247 { |
216 public: | 248 public: |
217 // Constructors | 249 // Constructors |
218 Text(const char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); } | 250 Text(const char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); } |
219 Text(const char *text) { SetHWND(dw_text_new(text, 0)); } | 251 Text(const char *text) { SetHWND(dw_text_new(text, 0)); } |
220 Text(unsigned long id) { SetHWND(dw_text_new("", id)); } | 252 Text(unsigned long id) { SetHWND(dw_text_new("", id)); } |
221 Text() { SetHWND(dw_text_new("", 0)); } | 253 Text() { SetHWND(dw_text_new("", 0)); } |
222 | 254 }; |
223 // User functions | 255 |
224 void SetText(const char *text) { dw_window_set_text(hwnd, text); } | 256 class StatusText : public TextWidget |
225 int SetFont(const char *font) { return dw_window_set_font(hwnd, font); } | 257 { |
226 char *GetFont() { return dw_window_get_font(hwnd); } | 258 public: |
259 // Constructors | |
260 StatusText(const char *text, unsigned long id) { SetHWND(dw_status_text_new(text, id)); } | |
261 StatusText(const char *text) { SetHWND(dw_status_text_new(text, 0)); } | |
262 StatusText(unsigned long id) { SetHWND(dw_status_text_new("", id)); } | |
263 StatusText() { SetHWND(dw_status_text_new("", 0)); } | |
227 }; | 264 }; |
228 | 265 |
229 // Class for handing static image widget | 266 // Class for handing static image widget |
230 class Bitmap : public Widget | 267 class Bitmap : public Widget |
231 { | 268 { |
297 } | 334 } |
298 void BitBlt(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc); | 335 void BitBlt(int xdest, int ydest, int width, int height, Pixmap *src, int xsrc, int ysrc); |
299 int SetFont(const char *fontname) { return dw_window_set_font(hwnd, fontname); } | 336 int SetFont(const char *fontname) { return dw_window_set_font(hwnd, fontname); } |
300 void GetTextExtents(const char *text, int *width, int *height) { dw_font_text_extents_get(hwnd, DW_NULL, text, width, height); } | 337 void GetTextExtents(const char *text, int *width, int *height) { dw_font_text_extents_get(hwnd, DW_NULL, text, width, height); } |
301 char *GetFont() { return dw_window_get_font(hwnd); } | 338 char *GetFont() { return dw_window_get_font(hwnd); } |
339 void Redraw() { dw_render_redraw(hwnd); } | |
302 protected: | 340 protected: |
303 // Our signal handler functions to be overriden... | 341 // Our signal handler functions to be overriden... |
304 // If they are not overridden and an event is generated, remove the unused handler | 342 // If they are not overridden and an event is generated, remove the unused handler |
305 virtual int OnExpose(DWExpose *exp) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_EXPOSE); return FALSE; } | 343 virtual int OnExpose(DWExpose *exp) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_EXPOSE); return FALSE; } |
306 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; | 344 virtual int OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; }; |
382 virtual int OnChanged(int status, char *url) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_HTML_CHANGED); return FALSE; } | 420 virtual int OnChanged(int status, char *url) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_HTML_CHANGED); return FALSE; } |
383 virtual int OnResult(int status, char *result, void *scriptdata) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_HTML_RESULT); return FALSE; }; | 421 virtual int OnResult(int status, char *result, void *scriptdata) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_HTML_RESULT); return FALSE; }; |
384 }; | 422 }; |
385 | 423 |
386 // Base class for several widgets that allow text entry | 424 // Base class for several widgets that allow text entry |
387 class TextEntry : virtual public Widget | 425 class TextEntry : virtual public Focusable, virtual public TextWidget |
388 { | 426 { |
389 public: | 427 public: |
390 // User functions | 428 // User functions |
391 void SetText(const char *text) { dw_window_set_text(hwnd, text); } | 429 void ClickDefault(Focusable *next) { dw_window_click_default(hwnd, next); } |
392 char *GetText() { return dw_window_get_text(hwnd); } | |
393 }; | 430 }; |
394 | 431 |
395 class Entryfield : public TextEntry | 432 class Entryfield : public TextEntry |
396 { | 433 { |
397 public: | 434 public: |
411 EntryfieldPassword(const char *text) { SetHWND(dw_entryfield_password_new(text, 0)); } | 448 EntryfieldPassword(const char *text) { SetHWND(dw_entryfield_password_new(text, 0)); } |
412 EntryfieldPassword() { SetHWND(dw_entryfield_password_new("", 0)); } | 449 EntryfieldPassword() { SetHWND(dw_entryfield_password_new("", 0)); } |
413 }; | 450 }; |
414 | 451 |
415 // Base class for several widgets that have a list of elements | 452 // Base class for several widgets that have a list of elements |
416 class ListBoxes : virtual public Widget | 453 class ListBoxes : virtual public Focusable |
417 { | 454 { |
418 private: | 455 private: |
419 void Setup() { | 456 void Setup() { |
420 if(IsOverridden(ListBoxes::OnListSelect, this)) | 457 if(IsOverridden(ListBoxes::OnListSelect, this)) |
421 dw_signal_connect(hwnd, DW_SIGNAL_LIST_SELECT, DW_SIGNAL_FUNC(_OnListSelect), this); | 458 dw_signal_connect(hwnd, DW_SIGNAL_LIST_SELECT, DW_SIGNAL_FUNC(_OnListSelect), this); |