]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
update for digital mars - resolve externals in shell and winsock libs
[wxWidgets.git] / src / msw / ownerdrw.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/ownerdrw.cpp
3// Purpose: implementation of wxOwnerDrawn class
4// Author: Vadim Zeitlin
33ac7e6f 5// Modified by:
2bda0e17
KB
6// Created: 13.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
6c9a19aa 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
a3b46648
UU
12#ifdef __GNUG__
13#pragma implementation
14#endif
2bda0e17
KB
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
ff0ea71c 18#include "wx/msw/private.h"
2bda0e17
KB
19
20#ifdef __BORLANDC__
a3b46648 21#pragma hdrstop
2bda0e17
KB
22#endif
23
24#ifndef WX_PRECOMP
0c589ad0 25 #include "wx/window.h"
136cb3c7 26 #include "wx/msw/private.h"
2432b92d
JS
27 #include "wx/font.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
2bda0e17 30 #include "wx/menu.h"
2432b92d 31 #include "wx/utils.h"
2bda0e17
KB
32#endif
33
5100cabf 34#include "wx/settings.h"
2bda0e17
KB
35#include "wx/ownerdrw.h"
36#include "wx/menuitem.h"
d9cf726b 37#include "wx/fontutil.h"
2bda0e17 38
1b24a68e
GRG
39#if wxUSE_OWNER_DRAWN
40
cfbf444a
JS
41class wxMSWSystemMenuFontModule : public wxModule
42{
43public:
44
45 virtual bool OnInit()
46 {
47 ms_systemMenuFont = new wxFont;
48
49#if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
50 NONCLIENTMETRICS nm;
51 nm.cbSize = sizeof(NONCLIENTMETRICS);
52 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
53
54 ms_systemMenuButtonWidth = nm.iMenuHeight;
55 ms_systemMenuHeight = nm.iMenuHeight;
56
57 // create menu font
58 wxNativeFontInfo info;
59 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
60 ms_systemMenuFont->Create(info);
61#endif
62
63 return true;
64 }
65
66 virtual void OnExit()
67 {
68 delete ms_systemMenuFont;
69 ms_systemMenuFont = NULL;
70 }
71
72 static wxFont* ms_systemMenuFont;
73 static int ms_systemMenuButtonWidth; // windows clean install default
74 static int ms_systemMenuHeight; // windows clean install default
75private:
76 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule)
77};
78
79// these static variables are by the wxMSWSystemMenuFontModule object
80// and reflect the system settings returned by the Win32 API's
81// SystemParametersInfo() call.
82
83wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
84int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
85int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
86
87IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
2432b92d 88
2bda0e17
KB
89// ============================================================================
90// implementation of wxOwnerDrawn class
91// ============================================================================
92
93// ctor
94// ----
33ac7e6f 95wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
19b42379 96 bool bCheckable, bool bMenuItem)
2bda0e17
KB
97 : m_strName(str)
98{
d9cf726b
JS
99 // get the default menu height and font from the system
100 NONCLIENTMETRICS nm;
101 nm.cbSize = sizeof (NONCLIENTMETRICS);
102 SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0);
103 m_nMinHeight = nm.iMenuHeight;
104
105 // nm.iMenuWidth is the system default for the width of
106 // menu icons and checkmarks
107 if (ms_nDefaultMarginWidth == 0)
108 {
cfbf444a
JS
109 ms_nDefaultMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth;
110 ms_nLastMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth;
d9cf726b
JS
111 }
112
cfbf444a 113 if (wxMSWSystemMenuFontModule::ms_systemMenuFont->Ok() && bMenuItem)
19b42379 114 {
cfbf444a 115 m_font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
19b42379
JS
116 }
117 else
118 {
119 m_font = *wxNORMAL_FONT;
120 }
2bda0e17 121
d9cf726b
JS
122 m_bCheckable = bCheckable;
123 m_bOwnerDrawn = FALSE;
124 m_nHeight = 0;
125 m_nMarginWidth = ms_nLastMarginWidth;
cfbf444a 126 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
d9cf726b
JS
127}
128
129
130// these items will be set during the first invocation of the c'tor,
131// because the values will be determined by checking the system settings,
132// which is a chunk of code
133size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
134size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
135
2bda0e17
KB
136
137// drawing
138// -------
139
140// get size of the item
2a2a71e3
JS
141// The item size includes the menu string, the accel string,
142// the bitmap and size for a submenu expansion arrow...
c86f1403 143bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17
KB
144{
145 wxMemoryDC dc;
2bda0e17 146
f2ab8671 147 wxString str = wxStripMenuCodes(m_strName);
2bda0e17 148
2a2a71e3
JS
149 // if we have a valid accel string, then pad out
150 // the menu string so the menu and accel string are not
151 // placed ontop of eachother.
152 if ( !m_strAccel.empty() )
153 {
154 str.Pad(str.Length()%8);
155 str += m_strAccel;
156 }
2bda0e17 157
e7dd6ecd
GT
158 if (m_font.Ok())
159 dc.SetFont(GetFont());
160
2bda0e17 161 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
c7527e3f 162
5ac7be7a
JS
163 if (!m_strAccel.IsEmpty())
164 {
165 // measure the accelerator string, and add it's width to
166 // the total item width, plus 16 (Accelerators are right justified,
167 // with the right edge of the text rectangle 16 pixels left of
168 // the right edge of the menu)
169
170 int accel_width, accel_height;
171 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
2a2a71e3 172 *pwidth += accel_width;
5ac7be7a
JS
173 }
174
2a2a71e3
JS
175 // add space at the end of the menu for the submenu expansion arrow
176 // this will also allow offsetting the accel string from the right edge
4fa47e56 177 *pwidth += (size_t) (GetDefaultMarginWidth() * 1.5);
2a2a71e3 178
d9cf726b
JS
179 // JACS: items still look too tightly packed, so adding 5 pixels.
180 (*pheight) = (*pheight) + 5;
c7527e3f 181
e7dd6ecd
GT
182 // Ray Gilbert's changes - Corrects the problem of a BMP
183 // being placed next to text in a menu item, and the BMP does
33ac7e6f 184 // not match the size expected by the system. This will
e7dd6ecd
GT
185 // resize the space so the BMP will fit. Without this, BMPs
186 // must be no larger or smaller than 16x16.
187 if (m_bmpChecked.Ok())
188 {
189 // Is BMP height larger then text height?
190 size_t adjustedHeight = m_bmpChecked.GetHeight() +
a756f210 191 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
e7dd6ecd
GT
192 if (*pheight < adjustedHeight)
193 *pheight = adjustedHeight;
33ac7e6f 194
e7dd6ecd
GT
195 // Does BMP encroach on default check menu position?
196 size_t adjustedWidth = m_bmpChecked.GetWidth() +
a756f210 197 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
2a2a71e3
JS
198// if (ms_nDefaultMarginWidth < adjustedWidth)
199// *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
33ac7e6f 200
e7dd6ecd 201 // Do we need to widen margin to fit BMP?
d2ec53ea 202 if ((size_t)GetMarginWidth() != adjustedWidth)
e7dd6ecd 203 SetMarginWidth(adjustedWidth);
2a2a71e3
JS
204
205 // add the size of the bitmap to our total size...
206 *pwidth += GetMarginWidth();
e7dd6ecd 207 }
33ac7e6f 208
2a2a71e3
JS
209 // add the size of the bitmap to our total size - even if we don't have
210 // a bitmap we leave room for one...
211 *pwidth += GetMarginWidth();
212
d9cf726b
JS
213 // make sure that this item is at least as
214 // tall as the user's system settings specify
215 if (*pheight < m_nMinHeight)
216 *pheight = m_nMinHeight;
217
2bda0e17
KB
218 m_nHeight = *pheight; // remember height for use in OnDrawItem
219
220 return TRUE;
221}
222
223// searching for this macro you'll find all the code where I'm using the native
224// Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
225// port this code to X. (VZ)
226
c7527e3f
JS
227// JACS: TODO. Why does a disabled but highlighted item still
228// get drawn embossed? How can we tell DrawState that we don't want the
229// embossing?
230
2bdf7154 231#if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__TWIN32__)
2bda0e17
KB
232#define O_DRAW_NATIVE_API // comments below explain why I use it
233#endif
234
235// draw the item
6d5b2a57
VZ
236bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
237 const wxRect& rc,
238 wxODAction act,
239 wxODStatus st)
2bda0e17
KB
240{
241 // we do nothing on focus change
242 if ( act == wxODFocusChanged )
243 return TRUE;
244
245 // wxColor <-> RGB
19193a2c 246 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
2bda0e17
KB
247 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
248
249 // set the colors
250 // --------------
251 DWORD colBack, colText;
252 if ( st & wxODSelected ) {
253 colBack = GetSysColor(COLOR_HIGHLIGHT);
5ac7be7a
JS
254 if (!(st & wxODDisabled))
255 {
256 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
257 }
258 else
259 {
260 colText = GetSysColor(COLOR_GRAYTEXT);
261 }
2bda0e17
KB
262 }
263 else {
264 // fall back to default colors if none explicitly specified
265 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
266 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
267 }
33ac7e6f 268
2bda0e17
KB
269 #ifdef O_DRAW_NATIVE_API
270 #define hdc (HDC)dc.GetHDC()
271 COLORREF colOldText = ::SetTextColor(hdc, colText),
272 colOldBack = ::SetBkColor(hdc, colBack);
273 #else
274 dc.SetTextForeground(wxColor(UnRGB(colText)));
275 dc.SetTextBackground(wxColor(UnRGB(colBack)));
276 #endif
277
278 // select the font and draw the text
279 // ---------------------------------
280
d9cf726b 281
33ac7e6f 282 // determine where to draw and leave space for a check-mark.
d9cf726b
JS
283 // Add 3 pixel padding so text appears well within highlight rectangle
284 int x = rc.x + GetMarginWidth() + 3;
285
2bda0e17 286
33ac7e6f 287 // using native API because it reckognizes '&'
2bda0e17
KB
288 #ifdef O_DRAW_NATIVE_API
289 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
07cf98cb
VZ
290 HBRUSH hbr = CreateSolidBrush(colBack),
291 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
2bda0e17 292
d9cf726b
JS
293 RECT rectFill = { rc.GetLeft(), rc.GetTop(), rc.GetRight()+1, rc.GetBottom() };
294
295 if ( st & wxODSelected && m_bmpChecked.Ok()) {
296 // only draw the highlight under the text, not under
297 // the bitmap or checkmark; leave a 1-pixel gap.
298 rectFill.left = GetMarginWidth() + 1;
299 }
300
301 FillRect(hdc, &rectFill, hbr);
2bda0e17
KB
302
303 // use default font if no font set
304 HFONT hfont;
305 if ( m_font.Ok() ) {
306 m_font.RealizeResource();
307 hfont = (HFONT)m_font.GetResourceHandle();
308 }
309 else {
310 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
311 }
312
c4e7c2aa 313 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
f44b4495 314
5ac7be7a 315 wxString strMenuText = m_strName.BeforeFirst('\t');
d9cf726b 316
2a2a71e3
JS
317 SIZE sizeRect;
318 GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect);
4fa47e56 319 ::DrawState(hdc, NULL, NULL,
5ac7be7a 320 (LPARAM)strMenuText.c_str(), strMenuText.length(),
4fa47e56 321 x, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically
2a2a71e3 322 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
5ac7be7a
JS
323 DST_PREFIXTEXT |
324 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
6d5b2a57 325
136cb3c7 326 if ( !m_strAccel.empty() )
6d5b2a57 327 {
2a2a71e3
JS
328 // right align accel string with right edge of menu ( offset by the margin width )
329 ::SetTextAlign(hdc, TA_RIGHT);
4fa47e56 330 ::DrawState(hdc, NULL, NULL,
5ac7be7a 331 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
4fa47e56 332 rc.GetWidth()-(GetMarginWidth()), rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
2a2a71e3 333 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
5ac7be7a
JS
334 DST_TEXT |
335 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
2a2a71e3 336 ::SetTextAlign(hdc, TA_LEFT);
6d5b2a57 337 }
2bda0e17
KB
338
339 (void)SelectObject(hdc, hPrevBrush);
340 (void)SelectObject(hdc, hPrevFont);
341 (void)SetBkMode(hdc, nPrevMode);
2b5f62a0
VZ
342
343 DeleteObject(hbr);
2bda0e17
KB
344 #else
345 dc.SetFont(GetFont());
d9cf726b 346 dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y);
2bda0e17
KB
347 #endif //O_DRAW_NATIVE_API
348
349 // draw the bitmap
350 // ---------------
351 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
352 if ( st & wxODChecked ) {
353 // using native APIs for performance and simplicity
5260b1c5 354#ifdef O_DRAW_NATIVE_API
33ac7e6f 355 // what goes on: DrawFrameControl creates a b/w mask,
2bda0e17
KB
356 // then we copy it to screen to have right colors
357
358 // first create a monochrome bitmap in a memory DC
359 HDC hdcMem = CreateCompatibleDC(hdc);
360 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
361 SelectObject(hdcMem, hbmpCheck);
362
363 // then draw a check mark into it
364 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
07cf98cb
VZ
365 if ( m_nHeight > 0 )
366 {
f44b4495 367 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
07cf98cb 368 }
2bda0e17
KB
369
370 // finally copy it to screen DC and clean up
33ac7e6f 371 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
2bda0e17 372 hdcMem, 0, 0, SRCCOPY);
07cf98cb 373
2bda0e17 374 DeleteDC(hdcMem);
07cf98cb 375 DeleteObject(hbmpCheck);
5260b1c5 376#else
2bda0e17 377 // #### to do: perhaps using Marlett font (create equiv. font under X)
5260b1c5
JS
378// wxFAIL("not implemented");
379#endif //O_DRAW_NATIVE_API
2bda0e17
KB
380 }
381 }
382 else {
383 // for uncheckable item we use only the 'checked' bitmap
384 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
385 if ( bmp.Ok() ) {
386 wxMemoryDC dcMem(&dc);
387 dcMem.SelectObject(bmp);
388
389 // center bitmap
390 int nBmpWidth = bmp.GetWidth(),
391 nBmpHeight = bmp.GetHeight();
392
393 // there should be enough place!
394 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
395
f44b4495 396 int heightDiff = m_nHeight - nBmpHeight;
33ac7e6f
KB
397 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
398 rc.y + heightDiff / 2,
399 nBmpWidth, nBmpHeight,
f44b4495 400 &dcMem, 0, 0, wxCOPY, TRUE /* use mask */);
2bda0e17
KB
401
402 if ( st & wxODSelected ) {
2a2a71e3
JS
403 #ifdef O_DRAW_NATIVE_API
404 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
405 rc.GetLeft() + GetMarginWidth(),
406 rc.GetTop() + m_nHeight-1 };
407 SetBkColor(hdc, colBack);
408 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
409 #else
d9cf726b
JS
410 int x1, y1, x2, y2;
411 x1 = rc.x;
412 y1 = rc.y;
413 x2 = x1 + GetMarginWidth() - 1;
414 y2 = y1 + m_nHeight - 1;
415
416 dc.SetPen(*wxWHITE_PEN);
417 dc.DrawLine(x1, y1, x2, y1);
418 dc.DrawLine(x1, y1, x1, y2);
419 dc.SetPen(*wxGREY_PEN);
420 dc.DrawLine(x1, y2-1, x2, y2-1);
421 dc.DrawLine(x2, y1, x2, y2);
2a2a71e3 422 #endif //O_DRAW_NATIVE_API
2bda0e17
KB
423 }
424 }
425 }
426
427 #ifdef O_DRAW_NATIVE_API
428 ::SetTextColor(hdc, colOldText);
429 ::SetBkColor(hdc, colOldBack);
430
431 #undef hdc
432 #endif //O_DRAW_NATIVE_API
433
434 return TRUE;
435}
436
1b24a68e
GRG
437
438#endif // wxUSE_OWNER_DRAWN
439