| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: msw/ownerdrw.cpp |
| 3 | // Purpose: implementation of wxOwnerDrawn class |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 13.11.97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | #include "wx/msw/private.h" |
| 19 | |
| 20 | #ifdef __BORLANDC__ |
| 21 | #pragma hdrstop |
| 22 | #endif |
| 23 | |
| 24 | #ifndef WX_PRECOMP |
| 25 | #include "wx/window.h" |
| 26 | #include "wx/msw/private.h" |
| 27 | #include "wx/font.h" |
| 28 | #include "wx/bitmap.h" |
| 29 | #include "wx/dcmemory.h" |
| 30 | #include "wx/menu.h" |
| 31 | #include "wx/utils.h" |
| 32 | #endif |
| 33 | |
| 34 | #include "wx/settings.h" |
| 35 | #include "wx/ownerdrw.h" |
| 36 | #include "wx/menuitem.h" |
| 37 | #include "wx/fontutil.h" |
| 38 | |
| 39 | #if wxUSE_OWNER_DRAWN |
| 40 | |
| 41 | class wxMSWSystemMenuFontModule : public wxModule |
| 42 | { |
| 43 | public: |
| 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 |
| 75 | private: |
| 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 | |
| 83 | wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL; |
| 84 | int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default |
| 85 | int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default |
| 86 | |
| 87 | IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule) |
| 88 | |
| 89 | // ============================================================================ |
| 90 | // implementation of wxOwnerDrawn class |
| 91 | // ============================================================================ |
| 92 | |
| 93 | // ctor |
| 94 | // ---- |
| 95 | wxOwnerDrawn::wxOwnerDrawn(const wxString& str, |
| 96 | bool bCheckable, bool bMenuItem) |
| 97 | : m_strName(str) |
| 98 | { |
| 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 | { |
| 109 | ms_nDefaultMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth; |
| 110 | ms_nLastMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth; |
| 111 | } |
| 112 | |
| 113 | if (wxMSWSystemMenuFontModule::ms_systemMenuFont->Ok() && bMenuItem) |
| 114 | { |
| 115 | m_font = *wxMSWSystemMenuFontModule::ms_systemMenuFont; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | m_font = *wxNORMAL_FONT; |
| 120 | } |
| 121 | |
| 122 | m_bCheckable = bCheckable; |
| 123 | m_bOwnerDrawn = FALSE; |
| 124 | m_nHeight = 0; |
| 125 | m_nMarginWidth = ms_nLastMarginWidth; |
| 126 | m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight; |
| 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 |
| 133 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0; |
| 134 | size_t wxOwnerDrawn::ms_nLastMarginWidth = 0; |
| 135 | |
| 136 | |
| 137 | // drawing |
| 138 | // ------- |
| 139 | |
| 140 | // get size of the item |
| 141 | // The item size includes the menu string, the accel string, |
| 142 | // the bitmap and size for a submenu expansion arrow... |
| 143 | bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight) |
| 144 | { |
| 145 | wxMemoryDC dc; |
| 146 | |
| 147 | wxString str = wxStripMenuCodes(m_strName); |
| 148 | |
| 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 | } |
| 157 | |
| 158 | if (m_font.Ok()) |
| 159 | dc.SetFont(GetFont()); |
| 160 | |
| 161 | dc.GetTextExtent(str, (long *)pwidth, (long *)pheight); |
| 162 | |
| 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); |
| 172 | *pwidth += accel_width; |
| 173 | } |
| 174 | |
| 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 |
| 177 | *pwidth += (size_t) (GetDefaultMarginWidth() * 1.5); |
| 178 | |
| 179 | // JACS: items still look too tightly packed, so adding 5 pixels. |
| 180 | (*pheight) = (*pheight) + 5; |
| 181 | |
| 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 |
| 184 | // not match the size expected by the system. This will |
| 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() + |
| 191 | wxSystemSettings::GetMetric(wxSYS_EDGE_Y); |
| 192 | if (*pheight < adjustedHeight) |
| 193 | *pheight = adjustedHeight; |
| 194 | |
| 195 | // Does BMP encroach on default check menu position? |
| 196 | size_t adjustedWidth = m_bmpChecked.GetWidth() + |
| 197 | (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2); |
| 198 | // if (ms_nDefaultMarginWidth < adjustedWidth) |
| 199 | // *pwidth += adjustedWidth - ms_nDefaultMarginWidth; |
| 200 | |
| 201 | // Do we need to widen margin to fit BMP? |
| 202 | if ((size_t)GetMarginWidth() != adjustedWidth) |
| 203 | SetMarginWidth(adjustedWidth); |
| 204 | |
| 205 | // add the size of the bitmap to our total size... |
| 206 | *pwidth += GetMarginWidth(); |
| 207 | } |
| 208 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 231 | #if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__TWIN32__) |
| 232 | #define O_DRAW_NATIVE_API // comments below explain why I use it |
| 233 | #endif |
| 234 | |
| 235 | // draw the item |
| 236 | bool wxOwnerDrawn::OnDrawItem(wxDC& dc, |
| 237 | const wxRect& rc, |
| 238 | wxODAction act, |
| 239 | wxODStatus st) |
| 240 | { |
| 241 | // we do nothing on focus change |
| 242 | if ( act == wxODFocusChanged ) |
| 243 | return TRUE; |
| 244 | |
| 245 | // wxColor <-> RGB |
| 246 | #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue()) |
| 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); |
| 254 | if (!(st & wxODDisabled)) |
| 255 | { |
| 256 | colText = GetSysColor(COLOR_HIGHLIGHTTEXT); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | colText = GetSysColor(COLOR_GRAYTEXT); |
| 261 | } |
| 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 | } |
| 268 | |
| 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 | |
| 281 | |
| 282 | // determine where to draw and leave space for a check-mark. |
| 283 | // Add 3 pixel padding so text appears well within highlight rectangle |
| 284 | int x = rc.x + GetMarginWidth() + 3; |
| 285 | |
| 286 | |
| 287 | // using native API because it reckognizes '&' |
| 288 | #ifdef O_DRAW_NATIVE_API |
| 289 | int nPrevMode = SetBkMode(hdc, TRANSPARENT); |
| 290 | HBRUSH hbr = CreateSolidBrush(colBack), |
| 291 | hPrevBrush = (HBRUSH)SelectObject(hdc, hbr); |
| 292 | |
| 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); |
| 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 | |
| 313 | HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont); |
| 314 | |
| 315 | wxString strMenuText = m_strName.BeforeFirst('\t'); |
| 316 | |
| 317 | SIZE sizeRect; |
| 318 | GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect); |
| 319 | ::DrawState(hdc, NULL, NULL, |
| 320 | (LPARAM)strMenuText.c_str(), strMenuText.length(), |
| 321 | x, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically |
| 322 | rc.GetWidth()-GetMarginWidth(), sizeRect.cy, |
| 323 | DST_PREFIXTEXT | |
| 324 | (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0)); |
| 325 | |
| 326 | if ( !m_strAccel.empty() ) |
| 327 | { |
| 328 | // right align accel string with right edge of menu ( offset by the margin width ) |
| 329 | ::SetTextAlign(hdc, TA_RIGHT); |
| 330 | ::DrawState(hdc, NULL, NULL, |
| 331 | (LPARAM)m_strAccel.c_str(), m_strAccel.length(), |
| 332 | rc.GetWidth()-(GetMarginWidth()), rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0), |
| 333 | rc.GetWidth()-GetMarginWidth(), sizeRect.cy, |
| 334 | DST_TEXT | |
| 335 | (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0)); |
| 336 | ::SetTextAlign(hdc, TA_LEFT); |
| 337 | } |
| 338 | |
| 339 | (void)SelectObject(hdc, hPrevBrush); |
| 340 | (void)SelectObject(hdc, hPrevFont); |
| 341 | (void)SetBkMode(hdc, nPrevMode); |
| 342 | |
| 343 | DeleteObject(hbr); |
| 344 | #else |
| 345 | dc.SetFont(GetFont()); |
| 346 | dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y); |
| 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 |
| 354 | #ifdef O_DRAW_NATIVE_API |
| 355 | // what goes on: DrawFrameControl creates a b/w mask, |
| 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 }; |
| 365 | if ( m_nHeight > 0 ) |
| 366 | { |
| 367 | ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
| 368 | } |
| 369 | |
| 370 | // finally copy it to screen DC and clean up |
| 371 | BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight, |
| 372 | hdcMem, 0, 0, SRCCOPY); |
| 373 | |
| 374 | DeleteDC(hdcMem); |
| 375 | DeleteObject(hbmpCheck); |
| 376 | #else |
| 377 | // #### to do: perhaps using Marlett font (create equiv. font under X) |
| 378 | // wxFAIL("not implemented"); |
| 379 | #endif //O_DRAW_NATIVE_API |
| 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 | |
| 396 | int heightDiff = m_nHeight - nBmpHeight; |
| 397 | dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2, |
| 398 | rc.y + heightDiff / 2, |
| 399 | nBmpWidth, nBmpHeight, |
| 400 | &dcMem, 0, 0, wxCOPY, TRUE /* use mask */); |
| 401 | |
| 402 | if ( st & wxODSelected ) { |
| 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 |
| 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); |
| 422 | #endif //O_DRAW_NATIVE_API |
| 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 | |
| 437 | |
| 438 | #endif // wxUSE_OWNER_DRAWN |
| 439 | |