Respond to WM_MEASUREITEM with the font height.
[reactos.git] / rosapps / templates / dialog / page1.c
1 /*
2 * ReactOS Standard Dialog Application Template
3 *
4 * page1.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define WIN32_LEAN_AND_MEAN
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <tchar.h>
27 #include <assert.h>
28 #include "resource.h"
29 #include "trace.h"
30
31
32 #define XBITMAP 80
33 #define YBITMAP 20
34
35 #define BUFFER_LEN MAX_PATH
36
37 extern HINSTANCE hInst;
38
39 HBITMAP hbmpPicture;
40 HBITMAP hbmpOld;
41
42
43 ////////////////////////////////////////////////////////////////////////////////
44
45 static void AddItem(HWND hListBox, LPCTSTR lpstr, HBITMAP hbmp)
46 {
47 int nItem = SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)lpstr);
48 SendMessage(hListBox, LB_SETITEMDATA, nItem, (LPARAM)hbmp);
49 }
50
51 static TCHAR* items[] = {
52 _T("services"),
53 _T("event log"),
54 _T("workstation"),
55 _T("server")
56 };
57
58 static void InitListCtrl(HWND hDlg)
59 {
60 TCHAR szBuffer[200];
61 int i;
62
63 HWND hListBox = GetDlgItem(hDlg, IDC_LIST1);
64
65 _tcscpy(szBuffer, _T("foobar item"));
66 SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)szBuffer);
67
68 for (i = 0; i < sizeof(items)/sizeof(items[0]); i++) {
69 _tcscpy(szBuffer, items[i]);
70 SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)szBuffer);
71 }
72
73 SetFocus(hListBox);
74 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
75 }
76
77 static void OnDrawItem(HWND hWnd, LPARAM lParam)
78 {
79 // int nItem;
80 TCHAR tchBuffer[BUFFER_LEN];
81 // HBITMAP hbmp;
82 TEXTMETRIC tm;
83 int y;
84 HDC hdcMem;
85 LPDRAWITEMSTRUCT lpdis;
86 RECT rcBitmap;
87
88 lpdis = (LPDRAWITEMSTRUCT)lParam;
89 // If there are no list box items, skip this message.
90 if (lpdis->itemID != -1) {
91 // Draw the bitmap and text for the list box item. Draw a rectangle around the bitmap if it is selected.
92 switch (lpdis->itemAction) {
93 case ODA_SELECT:
94 case ODA_DRAWENTIRE:
95 // Display the bitmap associated with the item.
96 hbmpPicture = (HBITMAP)SendMessage(lpdis->hwndItem, LB_GETITEMDATA, lpdis->itemID, (LPARAM)0);
97 hdcMem = CreateCompatibleDC(lpdis->hDC);
98 hbmpOld = SelectObject(hdcMem, hbmpPicture);
99 BitBlt(lpdis->hDC,
100 lpdis->rcItem.left, lpdis->rcItem.top,
101 lpdis->rcItem.right - lpdis->rcItem.left,
102 lpdis->rcItem.bottom - lpdis->rcItem.top,
103 hdcMem, 0, 0, SRCCOPY);
104 // Display the text associated with the item.
105 SendMessage(lpdis->hwndItem, LB_GETTEXT, lpdis->itemID, (LPARAM)tchBuffer);
106 GetTextMetrics(lpdis->hDC, &tm);
107 y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tm.tmHeight) / 2;
108 TextOut(lpdis->hDC, XBITMAP + 6, y, tchBuffer, _tcslen(tchBuffer));
109 SelectObject(hdcMem, hbmpOld);
110 DeleteDC(hdcMem);
111 // Is the item selected?
112 if (lpdis->itemState & ODS_SELECTED) {
113 // Set RECT coordinates to surround only the bitmap.
114 rcBitmap.left = lpdis->rcItem.left;
115 rcBitmap.top = lpdis->rcItem.top;
116 rcBitmap.right = lpdis->rcItem.left + XBITMAP;
117 rcBitmap.bottom = lpdis->rcItem.top + YBITMAP;
118 // Draw a rectangle around bitmap to indicate the selection.
119 DrawFocusRect(lpdis->hDC, &rcBitmap);
120 }
121 break;
122 case ODA_FOCUS:
123 // Do not process focus changes. The focus caret (outline rectangle)
124 // indicates the selection. The IDOK button indicates the final selection.
125 break;
126 }
127 }
128 }
129
130
131 void OnSetFont(HWND hWnd, WPARAM wParam, LPARAM lParam)
132 {
133 RECT rc;
134 WINDOWPOS wp;
135
136 GetWindowRect(hWnd, &rc);
137 wp.hwnd = hWnd;
138 wp.cx = rc.right - rc.left;
139 wp.cy = rc.bottom - rc.top;
140 wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
141 SendMessage(hWnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
142 }
143
144 void OnMeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
145 {
146 HFONT hFont;
147 LOGFONT lf;
148
149 hFont = GetStockObject(SYSTEM_FONT);
150 GetObject(hFont, sizeof(LOGFONT), &lf);
151 if (lf.lfHeight < 0)
152 lpMeasureItemStruct->itemHeight = -lf.lfHeight;
153 else
154 lpMeasureItemStruct->itemHeight = lf.lfHeight;
155 }
156
157 LRESULT CALLBACK PageWndProc1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
158 {
159 switch (message) {
160 case WM_INITDIALOG:
161 InitListCtrl(hDlg);
162 return TRUE;
163 case WM_SETFONT:
164 OnSetFont(hDlg, wParam, lParam);
165 return TRUE;
166 case WM_MEASUREITEM:
167 OnMeasureItem((LPMEASUREITEMSTRUCT)lParam);
168 return TRUE;
169 case WM_DRAWITEM:
170 OnDrawItem(hDlg, lParam);
171 return TRUE;
172 case WM_COMMAND:
173 switch (LOWORD(wParam)) {
174 case IDOK:
175 case IDCANCEL:
176 break;
177 }
178 break;
179 }
180 return 0;
181 }
182
183 ////////////////////////////////////////////////////////////////////////////////