comparison dw.hpp @ 2868:5ee1aaa48fc7

C++: The last signal handler change only worked with Clang/LLVM. Less desirable but cross platform solution, attach all signal handlers on widget creation, when called if not overridden remove the unused handler. Started the work of filling in the widget classes and functions.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 18 Dec 2022 11:40:22 +0000
parents ada74f4d3f39
children c873b6f862b9
comparison
equal deleted inserted replaced
2867:ada74f4d3f39 2868:5ee1aaa48fc7
12 12
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 private: 17 protected:
18 void *handle; 18 void *handle;
19 void SetHandle(void *newhandle) { handle = newhandle; }
19 public: 20 public:
20 void SetHandle(void *newhandle) { handle = newhandle; }
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:
28 void SetHWND(HWND newhwnd) { hwnd = newhwnd;
29 SetHandle(reinterpret_cast<void *>(newhwnd));
30 // Save the C++ class pointer in the window data for later
31 dw_window_set_data(hwnd, "_dw_classptr", this);
32 }
33 HWND hwnd;
27 public: 34 public:
28 HWND hwnd; 35 HWND GetHWND() { return hwnd; }
29 HWND GetHWND() { return reinterpret_cast<HWND>(GetHandle()); } 36 int Unpack() { return dw_box_unpack(hwnd); }
30 void SetHWND(HWND newhwnd) { hwnd = newhwnd; SetHandle(reinterpret_cast<void *>(newhwnd)); } 37 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); }
31 }; 39 };
32 40
33 // Box class is a packable object 41 // Box class is a packable object
34 class Box : public Widget 42 class Box : public Widget
35 { 43 {
44 public:
45 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 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 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 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 return reinterpret_cast<Widget *>(classptr);
54 }
36 }; 55 };
37 56
57 // TODO: Find a way to implement this cross platform...
58 // That way we can skip adding unused signal handlers
59 #define IsOverridden(a, b) true
60
38 // Top-level window class is packable 61 // Top-level window class is packable
39 template <class Derived>
40 class Window : public Box 62 class Window : public Box
41 { 63 {
42 private: 64 private:
43 void Setup() { 65 void Setup() {
44 if(&Derived::OnDelete != &Window::OnDelete) { 66 if(IsOverridden(Window::OnConfigure, this))
45 dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this); 67 dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this);
46 dw_debug("DW_SIGNAL_DELETE\n"); 68 if(IsOverridden(Window::OnConfigure, this))
47 }
48 if(&Derived::OnConfigure != &Window::OnConfigure) {
49 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); 69 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this);
50 dw_debug("DW_SIGNAL_CONFIGURE\n");
51 }
52 } 70 }
53 static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); } 71 static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); }
54 static int _OnConfigure(HWND window, int width, int height, void *data) { return reinterpret_cast<Window *>(data)->OnConfigure(width, height); } 72 static int _OnConfigure(HWND window, int width, int height, void *data) { return reinterpret_cast<Window *>(data)->OnConfigure(width, height); }
55 public: 73 public:
74 // Constructors
56 Window(HWND owner, const char *title, unsigned long style) { SetHWND(dw_window_new(owner, title, style)); Setup(); } 75 Window(HWND owner, const char *title, unsigned long style) { SetHWND(dw_window_new(owner, title, style)); Setup(); }
57 Window(const char *title, unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, title, style)); Setup(); } 76 Window(const char *title, unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, title, style)); Setup(); }
58 Window(unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, "", style)); Setup(); } 77 Window(unsigned long style) { SetHWND(dw_window_new(HWND_DESKTOP, "", style)); Setup(); }
59 Window(const char *title) { SetHWND(dw_window_new(HWND_DESKTOP, title, DW_FCF_SYSMENU | DW_FCF_TITLEBAR | 78 Window(const char *title) { SetHWND(dw_window_new(HWND_DESKTOP, title, DW_FCF_SYSMENU | DW_FCF_TITLEBAR |
60 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } 79 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); }
61 Window() { SetHWND(dw_window_new(HWND_DESKTOP, "", DW_FCF_SYSMENU | DW_FCF_TITLEBAR | 80 Window() { SetHWND(dw_window_new(HWND_DESKTOP, "", DW_FCF_SYSMENU | DW_FCF_TITLEBAR |
62 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); } 81 DW_FCF_TASKLIST | DW_FCF_SIZEBORDER | DW_FCF_MINMAX)); Setup(); }
63 82
83 // User functions
64 void SetText(const char *text) { dw_window_set_text(hwnd, text); } 84 void SetText(const char *text) { dw_window_set_text(hwnd, text); }
65 void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); } 85 void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); }
66 void Show() { dw_window_show(hwnd); } 86 int Show() { return dw_window_show(hwnd); }
67 // Our signal handler functions to be overriden 87 int Hide() { return dw_window_hide(hwnd); }
68 virtual int OnDelete() { return FALSE; } 88 void SetGravity(int horz, int vert) { dw_window_set_gravity(hwnd, horz, vert); }
69 virtual int OnConfigure(int width, int height) { return FALSE; }; 89 int Minimize() { return dw_window_minimize(hwnd); }
90 int Raise() { return dw_window_raise(hwnd); }
91 int Lower() { return dw_window_lower(hwnd); }
92 protected:
93 // Our signal handler functions to be overriden...
94 // 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 OnConfigure(int width, int height) { dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); return FALSE; };
70 }; 97 };
98
99 // Class for handling static text widget
100 class Text : public Widget
101 {
102 public:
103 Text(char *text, unsigned long id) { SetHWND(dw_text_new(text, id)); }
104 Text(char *text) { SetHWND(dw_text_new(text, 0)); }
105 Text(unsigned long id) { SetHWND(dw_text_new("", id)); }
106 Text() { SetHWND(dw_text_new("", 0)); }
107 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 char *GetFont() { return dw_window_get_font(hwnd); }
110 };
111
71 112
72 class App 113 class App
73 { 114 {
74 protected: 115 protected:
75 App() { } 116 App() { }