QTools Host-Based Tools 8.1.5
Toggle main menu visibility
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
44
static
HWND
l_hWnd
;
45
static
HDC
l_hDC
;
46
47
/*--------------------------------------------------------------------------*/
48
HWND
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
/*--------------------------------------------------------------------------*/
82
void
OwnerDrawnButton_init
(
OwnerDrawnButton
*
const
me,
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
/*..........................................................................*/
94
void
OwnerDrawnButton_xtor
(
OwnerDrawnButton
*
const
me) {
95
DeleteObject(me->
hBitmapUp
);
96
DeleteObject(me->
hBitmapDown
);
97
DeleteObject(me->
hCursor
);
98
}
99
/*..........................................................................*/
100
enum
OwnerDrawnButtonAction
OwnerDrawnButton_draw
(
101
OwnerDrawnButton
*
const
me,
102
LPDRAWITEMSTRUCT lpdis)
103
{
104
enum
OwnerDrawnButtonAction
ret =
BTN_NOACTION
;
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
/*..........................................................................*/
135
void
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
/*..........................................................................*/
148
BOOL
OwnerDrawnButton_isDepressed
(
OwnerDrawnButton
const
*
const
me) {
149
return
me->
isDepressed
;
150
}
151
152
/*--------------------------------------------------------------------------*/
153
void
GraphicDisplay_init
(
GraphicDisplay
*
const
me,
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
185
GraphicDisplay_clear
(me);
186
GraphicDisplay_redraw
(me);
187
}
188
/*..........................................................................*/
189
void
GraphicDisplay_xtor
(
GraphicDisplay
*
const
me) {
190
DeleteDC(me->
src_hDC
);
191
DeleteObject(me->
hBitmap
);
192
OutputDebugString(
"GraphicDisplay_xtor\n"
);
193
}
194
/*..........................................................................*/
195
void
GraphicDisplay_clear
(
GraphicDisplay
*
const
me) {
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
/*..........................................................................*/
209
void
GraphicDisplay_redraw
(
GraphicDisplay
*
const
me) {
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 ----------------------------------------------------------*/
215
void
SegmentDisplay_init
(
SegmentDisplay
*
const
me,
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
/*..........................................................................*/
232
void
SegmentDisplay_xtor
(
SegmentDisplay
*
const
me) {
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
/*..........................................................................*/
246
BOOL
SegmentDisplay_initSegment
(
SegmentDisplay
*
const
me,
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
/*..........................................................................*/
258
BOOL
SegmentDisplay_initBitmap
(
SegmentDisplay
*
const
me,
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
/*..........................................................................*/
270
BOOL
SegmentDisplay_setSegment
(
SegmentDisplay
*
const
me,
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
*/
288
void
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
l_hDC
static HDC l_hDC
Definition
qwin_gui.c:45
l_hWnd
static HWND l_hWnd
Definition
qwin_gui.c:44
qwin_gui.h
QWIN GUI facilities for building realistic embedded front panels.
OwnerDrawnButton_xtor
void OwnerDrawnButton_xtor(OwnerDrawnButton *const me)
Definition
qwin_gui.c:94
OwnerDrawnButton_set
void OwnerDrawnButton_set(OwnerDrawnButton *const me, int isDepressed)
Definition
qwin_gui.c:135
OwnerDrawnButton_draw
enum OwnerDrawnButtonAction OwnerDrawnButton_draw(OwnerDrawnButton *const me, LPDRAWITEMSTRUCT lpdis)
Definition
qwin_gui.c:100
DrawBitmap
void DrawBitmap(HDC hdc, HBITMAP hBitmap, int xStart, int yStart)
Definition
qwin_gui.c:288
OwnerDrawnButton_init
void OwnerDrawnButton_init(OwnerDrawnButton *const me, UINT itemID, HBITMAP hBitmapUp, HBITMAP hBitmapDwn, HCURSOR hCursor)
Definition
qwin_gui.c:82
GraphicDisplay_redraw
void GraphicDisplay_redraw(GraphicDisplay *const me)
Definition
qwin_gui.c:209
SegmentDisplay_xtor
void SegmentDisplay_xtor(SegmentDisplay *const me)
Definition
qwin_gui.c:232
GraphicDisplay_xtor
void GraphicDisplay_xtor(GraphicDisplay *const me)
Definition
qwin_gui.c:189
SegmentDisplay_initBitmap
BOOL SegmentDisplay_initBitmap(SegmentDisplay *const me, UINT bitmapNum, HBITMAP hBitmap)
Definition
qwin_gui.c:258
SegmentDisplay_init
void SegmentDisplay_init(SegmentDisplay *const me, UINT segNum, UINT bitmapNum)
Definition
qwin_gui.c:215
SegmentDisplay_initSegment
BOOL SegmentDisplay_initSegment(SegmentDisplay *const me, UINT segmentNum, UINT segmentID)
Definition
qwin_gui.c:246
OwnerDrawnButton_isDepressed
BOOL OwnerDrawnButton_isDepressed(OwnerDrawnButton const *const me)
Definition
qwin_gui.c:148
GraphicDisplay_clear
void GraphicDisplay_clear(GraphicDisplay *const me)
Definition
qwin_gui.c:195
GraphicDisplay_init
void GraphicDisplay_init(GraphicDisplay *const me, UINT width, UINT height, UINT itemID, BYTE const bgColor[3])
Definition
qwin_gui.c:153
SegmentDisplay_setSegment
BOOL SegmentDisplay_setSegment(SegmentDisplay *const me, UINT segmentNum, UINT bitmapNum)
Definition
qwin_gui.c:270
OwnerDrawnButtonAction
OwnerDrawnButtonAction
Definition
qwin_gui.h:68
BTN_PAINTED
@ BTN_PAINTED
Definition
qwin_gui.h:70
BTN_RELEASED
@ BTN_RELEASED
Definition
qwin_gui.h:72
BTN_NOACTION
@ BTN_NOACTION
Definition
qwin_gui.h:69
BTN_DEPRESSED
@ BTN_DEPRESSED
Definition
qwin_gui.h:71
CreateCustDialog
HWND CreateCustDialog(HINSTANCE hInst, int iDlg, HWND hParent, WNDPROC lpfnWndProc, LPCTSTR lpWndClass)
Definition
qwin_gui.c:48
GraphicDisplay
Definition
qwin_gui.h:90
GraphicDisplay::hBitmap
HBITMAP hBitmap
Definition
qwin_gui.h:98
GraphicDisplay::bgColor
BYTE bgColor[3]
Definition
qwin_gui.h:100
GraphicDisplay::dst_width
int dst_width
Definition
qwin_gui.h:95
GraphicDisplay::src_hDC
HDC src_hDC
Definition
qwin_gui.h:91
GraphicDisplay::hItem
HWND hItem
Definition
qwin_gui.h:97
GraphicDisplay::src_height
int src_height
Definition
qwin_gui.h:93
GraphicDisplay::dst_hDC
HDC dst_hDC
Definition
qwin_gui.h:94
GraphicDisplay::bits
BYTE * bits
Definition
qwin_gui.h:99
GraphicDisplay::src_width
int src_width
Definition
qwin_gui.h:92
GraphicDisplay::dst_height
int dst_height
Definition
qwin_gui.h:96
OwnerDrawnButton
Definition
qwin_gui.h:60
OwnerDrawnButton::hBitmapDown
HBITMAP hBitmapDown
Definition
qwin_gui.h:63
OwnerDrawnButton::hBitmapUp
HBITMAP hBitmapUp
Definition
qwin_gui.h:62
OwnerDrawnButton::itemID
UINT itemID
Definition
qwin_gui.h:61
OwnerDrawnButton::hCursor
HCURSOR hCursor
Definition
qwin_gui.h:64
OwnerDrawnButton::isDepressed
int isDepressed
Definition
qwin_gui.h:65
SegmentDisplay
Definition
qwin_gui.h:126
SegmentDisplay::segmentNum
UINT segmentNum
Definition
qwin_gui.h:128
SegmentDisplay::bitmapNum
UINT bitmapNum
Definition
qwin_gui.h:130
SegmentDisplay::hBitmap
HBITMAP * hBitmap
Definition
qwin_gui.h:129
SegmentDisplay::hSegment
HWND * hSegment
Definition
qwin_gui.h:127
qtools
qwin
qwin_gui.c
©
Quantum Leaps
|
YouTube
|
GitHub
| created with
Spexygen
©
Quantum Leaps
|
YouTube
|
GitHub
| created with
Spexygen