Debian Codegraph @

phoenix/windows/widget/button.cpp

raw
1 namespace phoenix {
2
3 #ifndef Button_SetImageList
4 //MinGW/32-bit has painfully outdated platform headers ...
5 typedef struct {
6 HIMAGELIST himl;
7 RECT margin;
8 UINT uAlign;
9 } BUTTON_IMAGELIST, *PBUTTON_IMAGELIST;
10
11 #define BUTTON_IMAGELIST_ALIGN_LEFT 0
12 #define BUTTON_IMAGELIST_ALIGN_RIGHT 1
13 #define BUTTON_IMAGELIST_ALIGN_TOP 2
14 #define BUTTON_IMAGELIST_ALIGN_BOTTOM 3
15 #define BUTTON_IMAGELIST_ALIGN_CENTER 4
16
17 #define BCM_FIRST 0x1600
18 #define BCM_SETIMAGELIST (BCM_FIRST+2)
19 #define Button_SetImageList(hwnd, pbuttonImagelist) (WINBOOL)SNDMSG((hwnd),BCM_SETIMAGELIST,0,(LPARAM)(pbuttonImagelist))
20 #endif
21
22 Size pButton::minimumSize() {
23 Size size = pFont::size(hfont, button.state.text);
24
25 if(button.state.orientation == Orientation::Horizontal) {
26 size.width += button.state.image.width;
27 size.height = max(button.state.image.height, size.height);
28 }
29
30 if(button.state.orientation == Orientation::Vertical) {
31 size.width = max(button.state.image.width, size.width);
32 size.height += button.state.image.height;
33 }
34
35 return {size.width + 20, size.height + 10};
36 }
37
38 void pButton::setImage(const image& image, Orientation orientation) {
39 nall::image nallImage = image;
40 nallImage.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0);
41
42 if(hbitmap) { DeleteObject(hbitmap); hbitmap = 0; }
43 if(himagelist) { ImageList_Destroy(himagelist); himagelist = 0; }
44
45 //Windows XP and earlier do not support translucent images
46 //so we must blend with the button background color (which does not look great with XP gradient-button themes)
47 if(OsVersion() < WindowsVista) nallImage.alphaBlend(GetSysColor(COLOR_BTNFACE));
48 hbitmap = CreateBitmap(nallImage);
49 himagelist = ImageList_Create(nallImage.width, nallImage.height, ILC_COLOR32, 1, 0);
50 ImageList_Add(himagelist, hbitmap, NULL);
51 BUTTON_IMAGELIST list;
52 list.himl = himagelist;
53 switch(orientation) {
54 case Orientation::Horizontal: SetRect(&list.margin, 5, 0, 0, 0); list.uAlign = BUTTON_IMAGELIST_ALIGN_LEFT; break;
55 case Orientation::Vertical: SetRect(&list.margin, 0, 5, 0, 0); list.uAlign = BUTTON_IMAGELIST_ALIGN_TOP; break;
56 }
57 Button_SetImageList(hwnd, &list);
58
59 setText(button.state.text); //update text to display nicely with image (or lack thereof)
60 }
61
62 void pButton::setText(string text) {
63 if(text.empty()) {
64 //bitmaps will not show up if text is empty
65 SetWindowLongPtr(hwnd, GWL_STYLE, GetWindowLongPtr(hwnd, GWL_STYLE) | BS_BITMAP);
66 } else {
67 //text will not show up if BS_BITMAP is set
68 SetWindowLongPtr(hwnd, GWL_STYLE, GetWindowLongPtr(hwnd, GWL_STYLE) & ~BS_BITMAP);
69 }
70
71 SetWindowText(hwnd, utf16_t(text));
72 }
73
74 void pButton::constructor() {
75 hwnd = CreateWindow(L"BUTTON", L"", WS_CHILD | WS_TABSTOP, 0, 0, 0, 0, parentHwnd, (HMENU)id, GetModuleHandle(0), 0);
76 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&button);
77 setDefaultFont();
78 setImage(button.state.image, button.state.orientation);
79 //setText(button.state.text); //called by setImage();
80 synchronize();
81 }
82
83 void pButton::destructor() {
84 if(hbitmap) { DeleteObject(hbitmap); hbitmap = 0; }
85 if(himagelist) { ImageList_Destroy(himagelist); himagelist = 0; }
86 DestroyWindow(hwnd);
87 }
88
89 void pButton::orphan() {
90 destructor();
91 constructor();
92 }
93
94 void pButton::onActivate() {
95 if(button.onActivate) button.onActivate();
96 }
97
98 }