]>
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 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/window.h"
17 #include "wx/msw/private.h"
19 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
25 #include "wx/ownerdrw.h"
26 #include "wx/menuitem.h"
29 // ============================================================================
30 // implementation of wxOwnerDrawn class
31 // ============================================================================
35 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
36 bool bCheckable
, bool bMenuItem
)
39 m_bCheckable
= bCheckable
;
40 m_bOwnerDrawn
= FALSE
;
42 m_nMarginWidth
= ms_nLastMarginWidth
;
45 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
47 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
52 // get size of the item
53 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
56 dc
.SetFont(GetFont());
59 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
60 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
61 wxString str
= szStripped
;
64 // # without this menu items look too tightly packed (at least under Windows)
65 str
+= wxT('W'); // 'W' is typically the widest letter
67 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
69 // JACS: items still look too tightly packed, so adding 2 pixels.
70 (*pheight
) = (*pheight
) + 2;
72 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
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)
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
86 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
88 ///////////////////////////////////////////////////////////////////////////////////////////////////
89 // Might want to check the native drawing apis for here and doo something like MSW does for WIN95
90 ///////////////////////////////////////////////////////////////////////////////////////////////////
92 // we do nothing on focus change
93 if ( act
== wxODFocusChanged
)
97 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
98 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
102 DWORD colBack
, colText
;
105 if ( st & wxODSelected ) {
106 colBack = GetSysColor(COLOR_HIGHLIGHT);
107 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
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);
115 // dc.SetTextForeground(wxColor(UnRGB(colText)));
116 // dc.SetTextBackground(wxColor(UnRGB(colBack)));
118 // select the font and draw the text
119 // ---------------------------------
121 // determine where to draw and leave space for a check-mark.
122 int x
= rc
.x
+ GetMarginWidth();
124 dc
.SetFont(GetFont());
125 dc
.DrawText(m_strName
, x
, rc
.y
);
129 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
130 if ( st
& wxODChecked
) {
131 // using native APIs for performance and simplicity
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 };
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);
148 // for uncheckable item we use only the 'checked' bitmap
149 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
151 wxMemoryDC
dcMem(&dc
);
152 dcMem
.SelectObject(bmp
);
155 int nBmpWidth
= bmp
.GetWidth(),
156 nBmpHeight
= bmp
.GetHeight();
158 // there should be enough place!
159 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
161 //MT: blit with mask enabled.
164 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
165 rc.y + (m_nHeight - nBmpHeight) /2,
166 nBmpWidth, nBmpHeight,
167 &dcMem, 0, 0, wxCOPY,true);
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);
181 #ifdef O_DRAW_NATIVE_API
182 ::SetTextColor(hdc, colOldText);
183 ::SetBkColor(hdc, colOldBack);
186 #endif //O_DRAW_NATIVE_API