| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: msw/ownerdrw.cpp |
| 3 | // Purpose: implementation of wxOwnerDrawn class |
| 4 | // Author: David Webster |
| 5 | // Modified by: |
| 6 | // Created: 10/12/99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) David Webster |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifndef WX_PRECOMP |
| 16 | #include "wx/window.h" |
| 17 | #include "wx/msw/private.h" |
| 18 | #include "wx/font.h" |
| 19 | #include "wx/bitmap.h" |
| 20 | #include "wx/dcmemory.h" |
| 21 | #include "wx/menu.h" |
| 22 | #include "wx/utils.h" |
| 23 | #endif |
| 24 | |
| 25 | #include "wx/ownerdrw.h" |
| 26 | #include "wx/menuitem.h" |
| 27 | |
| 28 | |
| 29 | // ============================================================================ |
| 30 | // implementation of wxOwnerDrawn class |
| 31 | // ============================================================================ |
| 32 | |
| 33 | // ctor |
| 34 | // ---- |
| 35 | wxOwnerDrawn::wxOwnerDrawn(const wxString& str, |
| 36 | bool bCheckable, bool bMenuItem) |
| 37 | : m_strName(str) |
| 38 | { |
| 39 | m_bCheckable = bCheckable; |
| 40 | m_bOwnerDrawn = FALSE; |
| 41 | m_nHeight = 0; |
| 42 | m_nMarginWidth = ms_nLastMarginWidth; |
| 43 | } |
| 44 | |
| 45 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15; |
| 46 | |
| 47 | size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth; |
| 48 | |
| 49 | // drawing |
| 50 | // ------- |
| 51 | |
| 52 | // get size of the item |
| 53 | bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight) |
| 54 | { |
| 55 | wxMemoryDC dc; |
| 56 | dc.SetFont(GetFont()); |
| 57 | |
| 58 | // ## ugly... |
| 59 | wxChar *szStripped = new wxChar[m_strName.Len()]; |
| 60 | wxStripMenuCodes((wxChar *)m_strName.c_str(), szStripped); |
| 61 | wxString str = szStripped; |
| 62 | delete [] szStripped; |
| 63 | |
| 64 | // # without this menu items look too tightly packed (at least under Windows) |
| 65 | str += wxT('W'); // 'W' is typically the widest letter |
| 66 | |
| 67 | dc.GetTextExtent(str, (long *)pwidth, (long *)pheight); |
| 68 | |
| 69 | // JACS: items still look too tightly packed, so adding 2 pixels. |
| 70 | (*pheight) = (*pheight) + 2; |
| 71 | |
| 72 | m_nHeight = *pheight; // remember height for use in OnDrawItem |
| 73 | |
| 74 | return TRUE; |
| 75 | } |
| 76 | |
| 77 | // searching for this macro you'll find all the code where I'm using the native |
| 78 | // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to |
| 79 | // port this code to X. (VZ) |
| 80 | |
| 81 | // JACS: TODO. Why does a disabled but highlighted item still |
| 82 | // get drawn embossed? How can we tell DrawState that we don't want the |
| 83 | // embossing? |
| 84 | |
| 85 | // draw the item |
| 86 | bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus st) |
| 87 | { |
| 88 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 89 | // Might want to check the native drawing apis for here and doo something like MSW does for WIN95 |
| 90 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | // we do nothing on focus change |
| 93 | if ( act == wxODFocusChanged ) |
| 94 | return TRUE; |
| 95 | |
| 96 | // wxColor <-> RGB |
| 97 | #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue()) |
| 98 | #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col) |
| 99 | |
| 100 | // set the colors |
| 101 | // -------------- |
| 102 | DWORD colBack, colText; |
| 103 | // TODO: |
| 104 | /* |
| 105 | if ( st & wxODSelected ) { |
| 106 | colBack = GetSysColor(COLOR_HIGHLIGHT); |
| 107 | colText = GetSysColor(COLOR_HIGHLIGHTTEXT); |
| 108 | } |
| 109 | else { |
| 110 | // fall back to default colors if none explicitly specified |
| 111 | colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW); |
| 112 | colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT); |
| 113 | } |
| 114 | */ |
| 115 | // dc.SetTextForeground(wxColor(UnRGB(colText))); |
| 116 | // dc.SetTextBackground(wxColor(UnRGB(colBack))); |
| 117 | |
| 118 | // select the font and draw the text |
| 119 | // --------------------------------- |
| 120 | |
| 121 | // determine where to draw and leave space for a check-mark. |
| 122 | int x = rc.x + GetMarginWidth(); |
| 123 | |
| 124 | dc.SetFont(GetFont()); |
| 125 | dc.DrawText(m_strName, x, rc.y); |
| 126 | |
| 127 | // draw the bitmap |
| 128 | // --------------- |
| 129 | if ( IsCheckable() && !m_bmpChecked.Ok() ) { |
| 130 | if ( st & wxODChecked ) { |
| 131 | // using native APIs for performance and simplicity |
| 132 | // TODO: |
| 133 | /* |
| 134 | HDC hdcMem = CreateCompatibleDC(hdc); |
| 135 | HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0); |
| 136 | SelectObject(hdcMem, hbmpCheck); |
| 137 | // then draw a check mark into it |
| 138 | RECT rect = { 0, 0, GetMarginWidth(), m_nHeight }; |
| 139 | |
| 140 | // finally copy it to screen DC and clean up |
| 141 | BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight, |
| 142 | hdcMem, 0, 0, SRCCOPY); |
| 143 | DeleteDC(hdcMem); |
| 144 | */ |
| 145 | } |
| 146 | } |
| 147 | else { |
| 148 | // for uncheckable item we use only the 'checked' bitmap |
| 149 | wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE)); |
| 150 | if ( bmp.Ok() ) { |
| 151 | wxMemoryDC dcMem(&dc); |
| 152 | dcMem.SelectObject(bmp); |
| 153 | |
| 154 | // center bitmap |
| 155 | int nBmpWidth = bmp.GetWidth(), |
| 156 | nBmpHeight = bmp.GetHeight(); |
| 157 | |
| 158 | // there should be enough place! |
| 159 | wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight())); |
| 160 | |
| 161 | //MT: blit with mask enabled. |
| 162 | // TODO: |
| 163 | /* |
| 164 | dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2, |
| 165 | rc.y + (m_nHeight - nBmpHeight) /2, |
| 166 | nBmpWidth, nBmpHeight, |
| 167 | &dcMem, 0, 0, wxCOPY,true); |
| 168 | |
| 169 | if ( st & wxODSelected ) { |
| 170 | #ifdef O_DRAW_NATIVE_API |
| 171 | RECT rectBmp = { rc.GetLeft(), rc.GetTop(), |
| 172 | rc.GetLeft() + GetMarginWidth(), |
| 173 | rc.GetTop() + m_nHeight }; |
| 174 | SetBkColor(hdc, colBack); |
| 175 | DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT); |
| 176 | } |
| 177 | */ |
| 178 | } |
| 179 | } |
| 180 | /* |
| 181 | #ifdef O_DRAW_NATIVE_API |
| 182 | ::SetTextColor(hdc, colOldText); |
| 183 | ::SetBkColor(hdc, colOldBack); |
| 184 | |
| 185 | #undef hdc |
| 186 | #endif //O_DRAW_NATIVE_API |
| 187 | */ |
| 188 | return TRUE; |
| 189 | } |
| 190 | |