]>
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 licence
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/os2/private.h"
23 #include "wx/bitmap.h"
24 #include "wx/dcmemory.h"
31 #include "wx/settings.h"
32 #include "wx/ownerdrw.h"
33 #include "wx/menuitem.h"
36 // ============================================================================
37 // implementation of wxOwnerDrawn class
38 // ============================================================================
44 wxOwnerDrawn::wxOwnerDrawn(
51 m_bCheckable
= bCheckable
;
52 m_bOwnerDrawn
= FALSE
;
54 m_nMarginWidth
= ms_nLastMarginWidth
;
56 m_font
= *wxNORMAL_FONT
;
57 } // end of wxOwnerDrawn::wxOwnerDrawn
59 wxOwnerDrawn::~wxOwnerDrawn() { }
61 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
63 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
70 bool wxOwnerDrawn::OnMeasureItem(
78 wxString sStr
= wxStripMenuCodes(m_strName
);
81 // If we have a valid accel string, then pad out
82 // the menu string so that the menu and accel string are not
83 // placed on top of each other.
84 if (!m_strAccel
.empty() )
86 sStr
.Pad(sStr
.Length()%8
);
89 vDC
.SetFont(GetFont());
90 vDC
.GetTextExtent( sStr
94 if (!m_strAccel
.IsEmpty())
97 // Measure the accelerator string, and add its width to
98 // the total item width, plus 16 (Accelerators are right justified,
99 // with the right edge of the text rectangle 16 pixels left of
100 // the right edge of the menu)
105 vDC
.GetTextExtent( m_strAccel
109 *pWidth
+= nAccelWidth
;
113 // Add space at the end of the menu for the submenu expansion arrow.
114 // This will also allow offsetting the accel string from the right edge
116 *pWidth
= (size_t)(*pWidth
+ GetDefaultMarginWidth() * 1.5);
119 // JACS: items still look too tightly packed, so adding 5 pixels.
124 // Ray Gilbert's changes - Corrects the problem of a BMP
125 // being placed next to text in a menu item, and the BMP does
126 // not match the size expected by the system. This will
127 // resize the space so the BMP will fit. Without this, BMPs
128 // must be no larger or smaller than 16x16.
130 if (m_bmpChecked
.Ok())
133 // Is BMP height larger then text height?
135 size_t nAdjustedHeight
= m_bmpChecked
.GetHeight() +
136 wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
137 if (*pHeight
< nAdjustedHeight
)
138 *pHeight
= nAdjustedHeight
;
141 // Does BMP encroach on default check menu position?
143 size_t nAdjustedWidth
= m_bmpChecked
.GetWidth() +
144 (wxSystemSettings::GetMetric(wxSYS_EDGE_X
) * 2);
147 // Do we need to widen margin to fit BMP?
149 if ((size_t)GetMarginWidth() < nAdjustedWidth
)
150 SetMarginWidth(nAdjustedWidth
);
153 // Add the size of the bitmap to our total size...
155 *pWidth
+= GetMarginWidth();
159 // Add the size of the bitmap to our total size - even if we don't have
160 // a bitmap we leave room for one...
162 *pWidth
+= GetMarginWidth();
165 // Make sure that this item is at least as
166 // tall as the user's system settings specify
168 if (*pHeight
< m_nMinHeight
)
169 *pHeight
= m_nMinHeight
;
170 m_nHeight
= *pHeight
; // remember height for use in OnDrawItem
172 } // end of wxOwnerDrawn::OnMeasureItem
175 bool wxOwnerDrawn::OnDrawItem(
177 , const wxRect
& rRect
183 // We do nothing on focus change
185 if (eAction
== wxODFocusChanged
)
189 // Select the font and draw the text
190 // ---------------------------------
194 HPS hPS
= rDC
.GetHPS();
198 RECTL vRect
= {rRect
.x
+ 4, rRect
.y
+ 1, rRect
.x
+ (rRect
.width
- 2), rRect
.y
+ rRect
.height
};
200 memset(&vCbnd
, 0, sizeof(CHARBUNDLE
));
203 // Use default font if no font set
207 m_font
.RealizeResource();
211 ::GpiSetCharSet(hPS
, LCID_DEFAULT
);
215 // Based on the status of the menu item, pick the right colors
217 if (eStatus
& wxODSelected
)
219 wxColour
vCol2(wxT("WHITE"));
220 vColBack
.Set( (unsigned char)0
223 ); // no dark blue in color table
226 else if (eStatus
& wxODDisabled
)
228 vRef
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
229 ,SYSCLR_MENU
// Light gray
232 vColBack
.Set( GetRValue(vRef
)
236 vRef
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
237 ,SYSCLR_MENUDISABLEDTEXT
// dark gray
240 vColText
.Set( GetRValue(vRef
)
248 // Fall back to default colors if none explicitly specified
250 vRef
= ::WinQuerySysColor( HWND_DESKTOP
251 ,SYSCLR_MENU
// we are using gray for all our window backgrounds in wxWidgets
254 vColBack
.Set( GetRValue(vRef
)
258 vRef
= ::WinQuerySysColor( HWND_DESKTOP
259 ,SYSCLR_WINDOWTEXT
// Black
262 vColText
.Set( GetRValue(vRef
)
268 rDC
.SetTextBackground(vColBack
);
269 rDC
.SetTextForeground(vColText
);
270 rDC
.SetBackgroundMode(wxTRANSPARENT
);
271 vCbnd
.lColor
= vColText
.GetPixel();
272 vCbnd
.lBackColor
= vColBack
.GetPixel();
275 ,CBB_BACK_COLOR
| CBB_COLOR
284 // Paint the background
286 ::WinFillRect(hPS
, &vRect
, vColBack
.GetPixel());
289 // Determine where to draw and leave space for a check-mark.
291 int nX
= rRect
.x
+ GetMarginWidth();
294 // Unfortunately, unlike Win32, PM has no owner drawn specific text
295 // drawing methods like ::DrawState that can cleanly handle accel
296 // mnemonics and deal, automatically, with various states, so we have
297 // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn
298 // strings either. We cannot handle mnemonics either. We display
299 // them, though, in the hope we can figure them out some day.
303 // Display main text and accel text separately to align better
305 wxString sTgt
= wxT("\t");
306 wxString sFullString
= m_strName
; // need to save the original text
312 bool bFoundMnemonic
= FALSE
;
313 bool bFoundAccel
= FALSE
;
316 // Deal with the tab, extracting the Accel text
318 nIndex
= sFullString
.Find(sTgt
.c_str());
322 sAccel
= sFullString
.Mid(nIndex
+ 1);
323 sFullString
.Remove(nIndex
);
327 // Deal with the mnemonic character
330 nIndex
= sFullString
.Find(sTgt
.c_str());
333 wxString sTmp
= sFullString
;
335 bFoundMnemonic
= TRUE
;
337 rDC
.GetTextExtent( sTmp
341 sTmp
= sFullString
[(size_t)(nIndex
+ 1)];
342 rDC
.GetTextExtent( sTmp
346 sFullString
.Replace(sTgt
.c_str(), wxEmptyString
, TRUE
);
350 // Draw the main item text sans the accel text
352 POINTL vPntStart
= {nX
, rRect
.y
+ 4};
353 ::GpiCharStringAt( rDC
.GetHPS()
355 ,sFullString
.length()
356 ,(PCH
)sFullString
.c_str()
361 // Underline the mnemonic -- still won't work, but at least it "looks" right
364 POINTL vPntEnd
= {nX
+ nWidth
+ nCharWidth
- 3, rRect
.y
+ 2}; //CharWidth is bit wide
366 vPntStart
.x
= nX
+ nWidth
- 1;
367 vPntStart
.y
= rRect
.y
+ 2; // Make it look pretty!
368 vPen
= wxPen(vColText
, 1, wxSOLID
); // Assuming we are always black
370 ::GpiMove(hPS
, &vPntStart
);
371 ::GpiLine(hPS
, &vPntEnd
);
375 // Now draw the accel text
382 rDC
.GetTextExtent( sAccel
387 // Back off the starting position from the right edge
389 vPntStart
.x
= rRect
.width
- (nWidth
+ 7);
390 vPntStart
.y
= rRect
.y
+ 4;
391 ::GpiCharStringAt( rDC
.GetHPS()
402 if (IsCheckable() && !m_bmpChecked
.Ok())
404 if (eStatus
& wxODChecked
)
407 HBITMAP hBmpCheck
= ::WinGetSysBitmap(HWND_DESKTOP
, SBMP_MENUCHECK
);
409 vRect
.xLeft
= rRect
.x
;
410 vRect
.xRight
= rRect
.x
+ GetMarginWidth();
411 vRect
.yBottom
= rRect
.y
;
412 vRect
.yTop
= rRect
.y
+ m_nHeight
- 3;
414 ::WinDrawBitmap( hPS
// PS for this menuitem
415 ,hBmpCheck
// system checkmark
416 ,NULL
// draw the whole bitmap
417 ,(PPOINTL
)&vRect
// destination -- bottom left corner of the menuitem area
420 ,DBM_NORMAL
// draw normal size
427 // For uncheckable item we use only the 'checked' bitmap
429 wxBitmap
vBmp(GetBitmap(IsCheckable() ? ((eStatus
& wxODChecked
) != 0) : TRUE
));
434 wxMemoryDC
vDCMem(&rDC
);
435 wxMemoryDC
* pOldDC
= (wxMemoryDC
*)vBmp
.GetSelectedInto();
439 vBmp
.SetSelectedInto(NULL
);
441 vDCMem
.SelectObject(vBmp
);
446 int nBmpWidth
= vBmp
.GetWidth();
447 int nBmpHeight
= vBmp
.GetHeight();
450 // There should be enough space!
452 wxASSERT((nBmpWidth
<= rRect
.width
) && (nBmpHeight
<= rRect
.height
));
454 int nHeightDiff
= m_nHeight
- nBmpHeight
;
456 rDC
.Blit( rRect
.x
+ (GetMarginWidth() - nBmpWidth
) / 2
457 ,rRect
.y
+ nHeightDiff
/ 2
467 if (eStatus
& wxODSelected
)
469 POINTL vPnt1
= {rRect
.x
+ 1, rRect
.y
+ 3}; // Leave a little background border
470 POINTL vPnt2
= {rRect
.x
+ GetMarginWidth(), rRect
.y
+ m_nHeight
- 3};
474 vLine
.lColor
= vColBack
.GetPixel();
481 ::GpiMove(hPS
, &vPnt1
);
489 vBmp
.SetSelectedInto(NULL
);
493 } // end of wxOwnerDrawn::OnDrawItem
495 #endif //wxUSE_OWNER_DRAWN