QTools  7.3.4
Collection of Host-Based Tools
Loading...
Searching...
No Matches
qwin_gui.c
Go to the documentation of this file.
1/**
2* @file
3* @brief QWIN GUI facilities for building realistic embedded front panels
4* @cond
5******************************************************************************
6* Last updated for version 6.7.0
7* Last updated on 2020-01-15
8*
9* Q u a n t u m L e a P s
10* ------------------------
11* Modern Embedded Software
12*
13* Copyright (C) 2020 Quantum Leaps, LLC. All rights reserved.
14*
15* MIT License:
16*
17* Permission is hereby granted, free of charge, to any person obtaining a copy
18* of this software and associated documentation files (the "Software"), to
19* deal in the Software without restriction, including without limitation the
20* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
21* sell copies of the Software, and to permit persons to whom the Software is
22* furnished to do so, subject to the following conditions:
23*
24* The above copyright notice and this permission notice shall be included in
25* all copies or substantial portions of the Software.
26*
27* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33* IN THE SOFTWARE.
34*
35* Contact information:
36* <www.state-machine.com>
37* <info@state-machine.com>
38******************************************************************************
39* @endcond
40*/
41#include "qwin_gui.h"
42#include <stdlib.h>
43
44static HWND l_hWnd;
45static HDC l_hDC;
46
47/*--------------------------------------------------------------------------*/
48HWND CreateCustDialog(HINSTANCE hInst, int iDlg, HWND hParent,
49 WNDPROC lpfnWndProc, LPCTSTR lpWndClass)
50{
51 WNDCLASSEX wndclass;
52
53 wndclass.cbSize = sizeof(wndclass);
54 wndclass.style = CS_HREDRAW | CS_VREDRAW;
55 wndclass.lpfnWndProc = lpfnWndProc;
56 wndclass.cbClsExtra = 0;
57 wndclass.cbWndExtra = DLGWINDOWEXTRA;
58 wndclass.hInstance = hInst;
59 wndclass.hIcon = NULL;
60 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
61 wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
62 wndclass.lpszMenuName = NULL;
63 wndclass.lpszClassName = lpWndClass;
64 wndclass.hIconSm = NULL;
65
66 RegisterClassEx(&wndclass);
67
68 l_hWnd = CreateDialog(hInst, MAKEINTRESOURCE(iDlg), hParent, NULL);
69 l_hDC = GetDC(l_hWnd); /* obtain the DC for the client area of the window */
70
71 /* NOTE: WM_INITDIALOG provides stimulus for initializing dialog controls.
72 * Dialog box procedures typically use this message to initialize controls
73 * and carry out any other initialization tasks that affect the appearance
74 * of the dialog box.
75 */
76 SendMessage(l_hWnd, WM_INITDIALOG, (WPARAM)0, (LPARAM)0);
77
78 return l_hWnd;
79}
80
81/*--------------------------------------------------------------------------*/
83 UINT itemID,
84 HBITMAP hBitmapUp, HBITMAP hBitmapDwn,
85 HCURSOR hCursor)
86{
87 me->itemID = itemID;
88 me->hBitmapUp = hBitmapUp;
89 me->hBitmapDown = hBitmapDwn;
90 me->hCursor = hCursor;
91 me->isDepressed = 0;
92}
93/*..........................................................................*/
95 DeleteObject(me->hBitmapUp);
96 DeleteObject(me->hBitmapDown);
97 DeleteObject(me->hCursor);
98}
99/*..........................................................................*/
101 OwnerDrawnButton * const me,
102 LPDRAWITEMSTRUCT lpdis)
103{
105
106 if ((lpdis->itemAction & ODA_DRAWENTIRE) != 0U) {
107 if (me->hCursor != NULL) {
108 SetClassLongPtr(lpdis->hwndItem,
109 GCLP_HCURSOR, (LONG_PTR)me->hCursor);
110 me->hCursor = NULL; /* mark the cursor set */
111 }
112 DrawBitmap(lpdis->hDC, me->hBitmapUp,
113 lpdis->rcItem.left, lpdis->rcItem.top);
114 me->isDepressed = 0;
115 ret = BTN_PAINTED;
116 }
117 else if ((lpdis->itemAction & ODA_SELECT) != 0U) {
118 if ((lpdis->itemState & ODS_SELECTED) != 0U) {
119 DrawBitmap(lpdis->hDC, me->hBitmapDown,
120 lpdis->rcItem.left, lpdis->rcItem.top);
121 me->isDepressed = !0;
122 ret = BTN_DEPRESSED;
123 }
124 else {
125 /* NOTE: the bitmap for button "UP" look will be
126 * drawn in the ODA_DRAWENTIRE action
127 */
128 me->isDepressed = 0;
129 ret = BTN_RELEASED;
130 }
131 }
132 return ret;
133}
134/*..........................................................................*/
135void OwnerDrawnButton_set(OwnerDrawnButton * const me, int isDepressed) {
136 if (me->isDepressed != isDepressed) {
137 HWND hItem = GetDlgItem(l_hWnd, me->itemID);
138 me->isDepressed = isDepressed;
139 if (isDepressed) {
140 DrawBitmap(GetDC(hItem), me->hBitmapDown, 0, 0);
141 }
142 else {
143 DrawBitmap(GetDC(hItem), me->hBitmapUp, 0, 0);
144 }
145 }
146}
147/*..........................................................................*/
149 return me->isDepressed;
150}
151
152/*--------------------------------------------------------------------------*/
154 UINT width, UINT height,
155 UINT itemID, BYTE const bgColor[3])
156{
157 BITMAPINFO bi24BitInfo;
158 RECT rect;
159
160 me->src_width = width;
161 me->src_height = height;
162
163 me->hItem = GetDlgItem(l_hWnd, itemID);
164 me->dst_hDC = GetDC(me->hItem);
165 GetWindowRect(me->hItem, &rect);
166 me->dst_width = rect.right - rect.left;
167 me->dst_height = rect.bottom - rect.top;
168
169 me->bgColor[0] = bgColor[0];
170 me->bgColor[1] = bgColor[1];
171 me->bgColor[2] = bgColor[2];
172
173 bi24BitInfo.bmiHeader.biBitCount = 3U*8U; /* 3 RGB bytes */
174 bi24BitInfo.bmiHeader.biCompression = BI_RGB; /* RGB color */
175 bi24BitInfo.bmiHeader.biPlanes = 1U;
176 bi24BitInfo.bmiHeader.biSize = sizeof(bi24BitInfo.bmiHeader);
177 bi24BitInfo.bmiHeader.biWidth = me->src_width;
178 bi24BitInfo.bmiHeader.biHeight = me->src_height;
179
180 me->src_hDC = CreateCompatibleDC(me->dst_hDC);
181 me->hBitmap = CreateDIBSection(me->src_hDC, &bi24BitInfo, DIB_RGB_COLORS,
182 (void **)&me->bits, 0, 0);
183 SelectObject(me->src_hDC, me->hBitmap);
184
187}
188/*..........................................................................*/
190 DeleteDC(me->src_hDC);
191 DeleteObject(me->hBitmap);
192 OutputDebugString("GraphicDisplay_xtor\n");
193}
194/*..........................................................................*/
196 UINT n;
197 BYTE r = me->bgColor[0];
198 BYTE g = me->bgColor[1];
199 BYTE b = me->bgColor[2];
200 BYTE *bits = me->bits;
201
202 for (n = me->src_width * me->src_height; n != 0U; --n, bits += 3) {
203 bits[0] = b;
204 bits[1] = g;
205 bits[2] = r;
206 }
207}
208/*..........................................................................*/
210 StretchBlt(me->dst_hDC, 0, 0, me->dst_width, me->dst_height,
211 me->src_hDC, 0, 0, me->src_width, me->src_height, SRCCOPY);
212}
213
214/* SegmentDisplay ----------------------------------------------------------*/
216 UINT segmentNum, UINT bitmapNum)
217{
218 UINT n;
219
220 me->hSegment = (HWND *)calloc(segmentNum, sizeof(HWND));
221 me->segmentNum = segmentNum;
222 for (n = 0U; n < segmentNum; ++n) {
223 me->hSegment[n] = NULL;
224 }
225 me->hBitmap = (HBITMAP *)calloc(bitmapNum, sizeof(HBITMAP));
226 me->bitmapNum = bitmapNum;
227 for (n = 0U; n < bitmapNum; ++n) {
228 me->hBitmap[n] = NULL;
229 }
230}
231/*..........................................................................*/
233 UINT n;
234
235 for (n = 0U; n < me->segmentNum; ++n) {
236 DeleteObject(me->hSegment[n]);
237 }
238 free(me->hSegment);
239
240 for (n = 0U; n < me->bitmapNum; ++n) {
241 DeleteObject(me->hBitmap[n]);
242 }
243 free(me->hBitmap);
244}
245/*..........................................................................*/
247 UINT segmentNum, UINT segmentID)
248{
249 if (segmentNum < me->segmentNum) {
250 me->hSegment[segmentNum] = GetDlgItem(l_hWnd, segmentID);
251 return me->hSegment[segmentNum] != NULL;
252 }
253 else {
254 return FALSE;
255 }
256}
257/*..........................................................................*/
259 UINT bitmapNum, HBITMAP hBitmap)
260{
261 if ((bitmapNum < me->bitmapNum) && (hBitmap != NULL)) {
262 me->hBitmap[bitmapNum] = hBitmap;
263 return TRUE;
264 }
265 else {
266 return FALSE;
267 }
268}
269/*..........................................................................*/
271 UINT segmentNum, UINT bitmapNum)
272{
273 if ((segmentNum < me->segmentNum) && (bitmapNum < me->bitmapNum)) {
274 SendMessage(me->hSegment[segmentNum], STM_SETIMAGE,
275 IMAGE_BITMAP,
276 (LPARAM)me->hBitmap[bitmapNum]);
277 return TRUE;
278 }
279 else {
280 return FALSE;
281 }
282}
283
284/*--------------------------------------------------------------------------*/
285/* DrawBitmap() function adapted from the book "Programming Windows" by
286* Charles Petzold.
287*/
288void DrawBitmap(HDC hdc, HBITMAP hBitmap,
289 int xStart, int yStart)
290{
291 BITMAP bm;
292 POINT ptSize, ptOrg;
293 HDC hdcMem = CreateCompatibleDC(hdc);
294
295 SelectObject(hdcMem, hBitmap);
296 SetMapMode(hdcMem, GetMapMode(hdc));
297
298 GetObject(hBitmap, sizeof(BITMAP), (LPVOID)&bm);
299 ptSize.x = bm.bmWidth;
300 ptSize.y = bm.bmHeight;
301 DPtoLP(hdc, &ptSize, 1);
302
303 ptOrg.x = 0;
304 ptOrg.y = 0;
305 DPtoLP(hdcMem, &ptOrg, 1);
306
307 BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y,
308 hdcMem, ptOrg.x, ptOrg.y, SRCCOPY);
309 DeleteDC(hdcMem);
310}
311
312
void SegmentDisplay_init(SegmentDisplay *const me, UINT segmentNum, UINT bitmapNum)
Definition qwin_gui.c:215
void OwnerDrawnButton_xtor(OwnerDrawnButton *const me)
Definition qwin_gui.c:94
void OwnerDrawnButton_set(OwnerDrawnButton *const me, int isDepressed)
Definition qwin_gui.c:135
enum OwnerDrawnButtonAction OwnerDrawnButton_draw(OwnerDrawnButton *const me, LPDRAWITEMSTRUCT lpdis)
Definition qwin_gui.c:100
void DrawBitmap(HDC hdc, HBITMAP hBitmap, int xStart, int yStart)
Definition qwin_gui.c:288
void OwnerDrawnButton_init(OwnerDrawnButton *const me, UINT itemID, HBITMAP hBitmapUp, HBITMAP hBitmapDwn, HCURSOR hCursor)
Definition qwin_gui.c:82
void GraphicDisplay_redraw(GraphicDisplay *const me)
Definition qwin_gui.c:209
static HDC l_hDC
Definition qwin_gui.c:45
void SegmentDisplay_xtor(SegmentDisplay *const me)
Definition qwin_gui.c:232
void GraphicDisplay_xtor(GraphicDisplay *const me)
Definition qwin_gui.c:189
BOOL SegmentDisplay_initBitmap(SegmentDisplay *const me, UINT bitmapNum, HBITMAP hBitmap)
Definition qwin_gui.c:258
BOOL SegmentDisplay_initSegment(SegmentDisplay *const me, UINT segmentNum, UINT segmentID)
Definition qwin_gui.c:246
BOOL OwnerDrawnButton_isDepressed(OwnerDrawnButton const *const me)
Definition qwin_gui.c:148
void GraphicDisplay_clear(GraphicDisplay *const me)
Definition qwin_gui.c:195
void GraphicDisplay_init(GraphicDisplay *const me, UINT width, UINT height, UINT itemID, BYTE const bgColor[3])
Definition qwin_gui.c:153
BOOL SegmentDisplay_setSegment(SegmentDisplay *const me, UINT segmentNum, UINT bitmapNum)
Definition qwin_gui.c:270
static HWND l_hWnd
Definition qwin_gui.c:44
HWND CreateCustDialog(HINSTANCE hInst, int iDlg, HWND hParent, WNDPROC lpfnWndProc, LPCTSTR lpWndClass)
Definition qwin_gui.c:48
QWIN GUI facilities for building realistic embedded front panels.
OwnerDrawnButtonAction
Definition qwin_gui.h:68
@ BTN_PAINTED
Definition qwin_gui.h:70
@ BTN_RELEASED
Definition qwin_gui.h:72
@ BTN_NOACTION
Definition qwin_gui.h:69
@ BTN_DEPRESSED
Definition qwin_gui.h:71
HBITMAP hBitmap
Definition qwin_gui.h:98
BYTE bgColor[3]
Definition qwin_gui.h:100
BYTE * bits
Definition qwin_gui.h:99
HBITMAP hBitmapDown
Definition qwin_gui.h:63
HBITMAP hBitmapUp
Definition qwin_gui.h:62
HCURSOR hCursor
Definition qwin_gui.h:64
HBITMAP * hBitmap
Definition qwin_gui.h:129
HWND * hSegment
Definition qwin_gui.h:127