]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
23 #include "wx/bitmap.h"
24 #include "wx/dcmemory.h"
29 #include "wx/ownerdrw.h"
30 #include "wx/menuitem.h"
33 // ============================================================================
34 // implementation of wxOwnerDrawn class
35 // ============================================================================
39 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
40 bool bCheckable
, bool bMenuItem
)
43 m_bCheckable
= bCheckable
;
44 m_bOwnerDrawn
= FALSE
;
46 m_nMarginWidth
= ms_nLastMarginWidth
;
49 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
51 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
56 // get size of the item
57 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
60 dc
.SetFont(GetFont());
63 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
64 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
65 wxString str
= szStripped
;
68 // # without this menu items look too tightly packed (at least under Windows)
69 str
+= wxT('W'); // 'W' is typically the widest letter
71 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
73 // JACS: items still look too tightly packed, so adding 2 pixels.
74 (*pheight
) = (*pheight
) + 2;
76 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
81 // searching for this macro you'll find all the code where I'm using the native
82 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
83 // port this code to X. (VZ)
85 // JACS: TODO. Why does a disabled but highlighted item still
86 // get drawn embossed? How can we tell DrawState that we don't want the
90 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
92 ///////////////////////////////////////////////////////////////////////////////////////////////////
93 // Might want to check the native drawing apis for here and doo something like MSW does for WIN95
94 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 // we do nothing on focus change
97 if ( act
== wxODFocusChanged
)
101 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
102 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
106 DWORD colBack
, colText
;
109 if ( st & wxODSelected ) {
110 colBack = GetSysColor(COLOR_HIGHLIGHT);
111 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
114 // fall back to default colors if none explicitly specified
115 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
116 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
119 // dc.SetTextForeground(wxColor(UnRGB(colText)));
120 // dc.SetTextBackground(wxColor(UnRGB(colBack)));
122 // select the font and draw the text
123 // ---------------------------------
125 // determine where to draw and leave space for a check-mark.
126 int x
= rc
.x
+ GetMarginWidth();
128 dc
.SetFont(GetFont());
129 dc
.DrawText(m_strName
, x
, rc
.y
);
133 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
134 if ( st
& wxODChecked
) {
135 // using native APIs for performance and simplicity
138 HDC hdcMem = CreateCompatibleDC(hdc);
139 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
140 SelectObject(hdcMem, hbmpCheck);
141 // then draw a check mark into it
142 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
144 // finally copy it to screen DC and clean up
145 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
146 hdcMem, 0, 0, SRCCOPY);
152 // for uncheckable item we use only the 'checked' bitmap
153 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
155 wxMemoryDC
dcMem(&dc
);
156 dcMem
.SelectObject(bmp
);
159 int nBmpWidth
= bmp
.GetWidth(),
160 nBmpHeight
= bmp
.GetHeight();
162 // there should be enough place!
163 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
165 //MT: blit with mask enabled.
168 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
169 rc.y + (m_nHeight - nBmpHeight) /2,
170 nBmpWidth, nBmpHeight,
171 &dcMem, 0, 0, wxCOPY,true);
173 if ( st & wxODSelected ) {
174 #ifdef O_DRAW_NATIVE_API
175 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
176 rc.GetLeft() + GetMarginWidth(),
177 rc.GetTop() + m_nHeight };
178 SetBkColor(hdc, colBack);
179 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
185 #ifdef O_DRAW_NATIVE_API
186 ::SetTextColor(hdc, colOldText);
187 ::SetBkColor(hdc, colOldBack);
190 #endif //O_DRAW_NATIVE_API