]> git.saurik.com Git - wxWidgets.git/blob - src/os2/ownerdrw.cpp
update window before changing the scrolling position and really scrolling it to avoid...
[wxWidgets.git] / src / os2 / ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/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 licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_OWNER_DRAWN
16
17 #ifndef WX_PRECOMP
18 #include "wx/window.h"
19 #include "wx/os2/private.h"
20 #include "wx/font.h"
21 #include "wx/bitmap.h"
22 #include "wx/dcmemory.h"
23 #include "wx/menu.h"
24 #include "wx/utils.h"
25 #include "wx/settings.h"
26 #include "wx/menuitem.h"
27 #endif
28
29 #include "wx/ownerdrw.h"
30
31 // ============================================================================
32 // implementation of wxOwnerDrawn class
33 // ============================================================================
34
35 //
36 // ctor
37 // ----
38 //
39 wxOwnerDrawn::wxOwnerDrawn( const wxString& rsStr,
40 bool bCheckable,
41 bool WXUNUSED(bMenuItem) )
42 : m_strName(rsStr)
43 {
44 m_bCheckable = bCheckable;
45 m_bOwnerDrawn = false;
46 m_nHeight = 0;
47 m_nMarginWidth = ms_nLastMarginWidth;
48 if (wxNORMAL_FONT)
49 m_font = *wxNORMAL_FONT;
50 } // end of wxOwnerDrawn::wxOwnerDrawn
51
52 wxOwnerDrawn::~wxOwnerDrawn() { }
53
54 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15;
55
56 size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
57
58 //
59 // Drawing
60 // -------
61 //
62
63 bool wxOwnerDrawn::OnMeasureItem( size_t* pWidth,
64 size_t* pHeight )
65 {
66 wxMemoryDC vDC;
67
68 wxString sStr = wxStripMenuCodes(m_strName);
69
70 //
71 // If we have a valid accel string, then pad out
72 // the menu string so that the menu and accel string are not
73 // placed on top of each other.
74 if (!m_strAccel.empty() )
75 {
76 sStr.Pad(sStr.length()%8);
77 sStr += m_strAccel;
78 }
79 vDC.SetFont(GetFont());
80 vDC.GetTextExtent( sStr
81 ,(long *)pWidth
82 ,(long *)pHeight
83 );
84 if (!m_strAccel.empty())
85 {
86 //
87 // Measure the accelerator string, and add its width to
88 // the total item width, plus 16 (Accelerators are right justified,
89 // with the right edge of the text rectangle 16 pixels left of
90 // the right edge of the menu)
91 //
92 int nAccelWidth;
93 int nAccelHeight;
94
95 vDC.GetTextExtent( m_strAccel
96 ,&nAccelWidth
97 ,&nAccelHeight
98 );
99 *pWidth += nAccelWidth;
100 }
101
102 //
103 // Add space at the end of the menu for the submenu expansion arrow.
104 // This will also allow offsetting the accel string from the right edge
105 //
106 *pWidth = (size_t)(*pWidth + GetDefaultMarginWidth() * 1.5);
107
108 //
109 // JACS: items still look too tightly packed, so adding 5 pixels.
110 //
111 (*pHeight) += 5;
112
113 //
114 // Ray Gilbert's changes - Corrects the problem of a BMP
115 // being placed next to text in a menu item, and the BMP does
116 // not match the size expected by the system. This will
117 // resize the space so the BMP will fit. Without this, BMPs
118 // must be no larger or smaller than 16x16.
119 //
120 if (m_bmpChecked.Ok())
121 {
122 //
123 // Is BMP height larger then text height?
124 //
125 size_t nAdjustedHeight = m_bmpChecked.GetHeight() +
126 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
127 if (*pHeight < nAdjustedHeight)
128 *pHeight = nAdjustedHeight;
129
130 //
131 // Does BMP encroach on default check menu position?
132 //
133 size_t nAdjustedWidth = m_bmpChecked.GetWidth() +
134 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
135
136 //
137 // Do we need to widen margin to fit BMP?
138 //
139 if ((size_t)GetMarginWidth() < nAdjustedWidth)
140 SetMarginWidth(nAdjustedWidth);
141
142 //
143 // Add the size of the bitmap to our total size...
144 //
145 *pWidth += GetMarginWidth();
146 }
147
148 //
149 // Add the size of the bitmap to our total size - even if we don't have
150 // a bitmap we leave room for one...
151 //
152 *pWidth += GetMarginWidth();
153
154 //
155 // Make sure that this item is at least as
156 // tall as the user's system settings specify
157 //
158 if (*pHeight < m_nMinHeight)
159 *pHeight = m_nMinHeight;
160 m_nHeight = *pHeight; // remember height for use in OnDrawItem
161 return true;
162 } // end of wxOwnerDrawn::OnMeasureItem
163
164 // draw the item
165 bool wxOwnerDrawn::OnDrawItem( wxDC& rDC,
166 const wxRect& rRect,
167 wxODAction eAction,
168 wxODStatus eStatus )
169 {
170 //
171 // We do nothing on focus change
172 //
173 if (eAction == wxODFocusChanged )
174 return true;
175
176 //
177 // Select the font and draw the text
178 // ---------------------------------
179 //
180
181 CHARBUNDLE vCbnd;
182 HPS hPS= rDC.GetHPS();
183 wxColour vColBack;
184 wxColour vColText;
185 COLORREF vRef;
186 RECTL vRect = {rRect.x + 4, rRect.y + 1, rRect.x + (rRect.width - 2), rRect.y + rRect.height};
187
188 memset(&vCbnd, 0, sizeof(CHARBUNDLE));
189
190 //
191 // Use default font if no font set
192 //
193 if (m_font.Ok())
194 {
195 m_font.RealizeResource();
196 }
197 else
198 {
199 ::GpiSetCharSet(hPS, LCID_DEFAULT);
200 }
201
202 //
203 // Based on the status of the menu item, pick the right colors
204 //
205 if (eStatus & wxODSelected)
206 {
207 vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP
208 ,SYSCLR_MENUHILITEBGND
209 ,0L
210 );
211 vColBack.Set( GetRValue(vRef)
212 ,GetGValue(vRef)
213 ,GetBValue(vRef)
214 );
215 vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP
216 ,SYSCLR_MENUHILITE
217 ,0L
218 );
219 vColText.Set( GetRValue(vRef)
220 ,GetGValue(vRef)
221 ,GetBValue(vRef)
222 );
223 }
224 else if (eStatus & wxODDisabled)
225 {
226 vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP
227 ,SYSCLR_MENU // Light gray
228 ,0L
229 );
230 vColBack.Set( GetRValue(vRef)
231 ,GetGValue(vRef)
232 ,GetBValue(vRef)
233 );
234 vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP
235 ,SYSCLR_MENUDISABLEDTEXT // dark gray
236 ,0L
237 );
238 vColText.Set( GetRValue(vRef)
239 ,GetGValue(vRef)
240 ,GetBValue(vRef)
241 );
242 }
243 else
244 {
245 //
246 // Fall back to default colors if none explicitly specified
247 //
248 vRef = ::WinQuerySysColor( HWND_DESKTOP
249 ,SYSCLR_MENU // we are using gray for all our window backgrounds in wxWidgets
250 ,0L
251 );
252 vColBack.Set( GetRValue(vRef)
253 ,GetGValue(vRef)
254 ,GetBValue(vRef)
255 );
256 vRef = ::WinQuerySysColor( HWND_DESKTOP
257 ,SYSCLR_WINDOWTEXT // Black
258 ,0L
259 );
260 vColText.Set( GetRValue(vRef)
261 ,GetGValue(vRef)
262 ,GetBValue(vRef)
263 );
264 }
265
266 rDC.SetTextBackground(vColBack);
267 rDC.SetTextForeground(vColText);
268 rDC.SetBackgroundMode(wxTRANSPARENT);
269 vCbnd.lColor = vColText.GetPixel();
270 vCbnd.lBackColor = vColBack.GetPixel();
271 ::GpiSetAttrs( hPS
272 ,PRIM_CHAR
273 ,CBB_BACK_COLOR | CBB_COLOR
274 ,0
275 ,&vCbnd
276 );
277 ::GpiSetBackMix( hPS
278 ,BM_LEAVEALONE
279 );
280
281 //
282 // Paint the background
283 //
284 ::WinFillRect(hPS, &vRect, vColBack.GetPixel());
285
286 //
287 // Determine where to draw and leave space for a check-mark.
288 //
289 int nX = rRect.x + GetMarginWidth();
290
291 //
292 // Unfortunately, unlike Win32, PM has no owner drawn specific text
293 // drawing methods like ::DrawState that can cleanly handle accel
294 // mnemonics and deal, automatically, with various states, so we have
295 // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn
296 // strings either. We cannot handle mnemonics either. We display
297 // them, though, in the hope we can figure them out some day.
298 //
299
300 //
301 // Display main text and accel text separately to align better
302 //
303 wxString sTgt = wxT("\t");
304 wxString sFullString = m_strName; // need to save the original text
305 wxString sAccel;
306 int nIndex;
307 size_t nWidth;
308 size_t nCharWidth;
309 size_t nHeight;
310 bool bFoundMnemonic = false;
311 bool bFoundAccel = false;
312
313 //
314 // Deal with the tab, extracting the Accel text
315 //
316 nIndex = sFullString.Find(sTgt.c_str());
317 if (nIndex != -1)
318 {
319 bFoundAccel = true;
320 sAccel = sFullString.Mid(nIndex + 1);
321 sFullString.Remove(nIndex);
322 }
323
324 //
325 // Deal with the mnemonic character
326 //
327 sTgt = wxT("~");
328 nIndex = sFullString.Find(sTgt.c_str());
329 if (nIndex != -1)
330 {
331 wxString sTmp = sFullString;
332
333 bFoundMnemonic = true;
334 sTmp.Remove(nIndex);
335 rDC.GetTextExtent( sTmp
336 ,(long *)&nWidth
337 ,(long *)&nHeight
338 );
339 sTmp = sFullString[(size_t)(nIndex + 1)];
340 rDC.GetTextExtent( sTmp
341 ,(long *)&nCharWidth
342 ,(long *)&nHeight
343 );
344 sFullString.Replace(sTgt.c_str(), wxEmptyString, true);
345 }
346
347 //
348 // Draw the main item text sans the accel text
349 //
350 POINTL vPntStart = {nX, rRect.y + 4};
351 ::GpiCharStringAt( rDC.GetHPS()
352 ,&vPntStart
353 ,sFullString.length()
354 ,(PCH)sFullString.c_str()
355 );
356 if (bFoundMnemonic)
357 {
358 //
359 // Underline the mnemonic -- still won't work, but at least it "looks" right
360 //
361 wxPen vPen;
362 POINTL vPntEnd = {nX + nWidth + nCharWidth - 3, rRect.y + 2}; //CharWidth is bit wide
363
364 vPntStart.x = nX + nWidth - 1;
365 vPntStart.y = rRect.y + 2; // Make it look pretty!
366 vPen = wxPen(vColText, 1, wxSOLID); // Assuming we are always black
367 rDC.SetPen(vPen);
368 ::GpiMove(hPS, &vPntStart);
369 ::GpiLine(hPS, &vPntEnd);
370 }
371
372 //
373 // Now draw the accel text
374 //
375 if (bFoundAccel)
376 {
377 size_t nWidth;
378 size_t nHeight;
379
380 rDC.GetTextExtent( sAccel
381 ,(long *)&nWidth
382 ,(long *)&nHeight
383 );
384 //
385 // Back off the starting position from the right edge
386 //
387 vPntStart.x = rRect.width - (nWidth + 7);
388 vPntStart.y = rRect.y + 4;
389 ::GpiCharStringAt( rDC.GetHPS()
390 ,&vPntStart
391 ,sAccel.length()
392 ,(PCH)sAccel.c_str()
393 );
394 }
395
396 //
397 // Draw the bitmap
398 // ---------------
399 //
400 if (IsCheckable() && !m_bmpChecked.Ok())
401 {
402 if (eStatus & wxODChecked)
403 {
404 RECTL vRect;
405 HBITMAP hBmpCheck = ::WinGetSysBitmap(HWND_DESKTOP, SBMP_MENUCHECK);
406
407 vRect.xLeft = rRect.x;
408 vRect.xRight = rRect.x + GetMarginWidth();
409 vRect.yBottom = rRect.y;
410 vRect.yTop = rRect.y + m_nHeight - 3;
411
412 ::WinDrawBitmap( hPS // PS for this menuitem
413 ,hBmpCheck // system checkmark
414 ,NULL // draw the whole bitmap
415 ,(PPOINTL)&vRect // destination -- bottom left corner of the menuitem area
416 ,0L // ignored
417 ,0L // draw a bitmap
418 ,DBM_NORMAL // draw normal size
419 );
420 }
421 }
422 else
423 {
424 //
425 // For uncheckable item we use only the 'checked' bitmap
426 //
427 wxBitmap vBmp(GetBitmap(IsCheckable() ? ((eStatus & wxODChecked) != 0) : TRUE));
428
429 if (vBmp.Ok())
430 {
431
432 wxMemoryDC vDCMem(&rDC);
433 wxMemoryDC* pOldDC = (wxMemoryDC*)vBmp.GetSelectedInto();
434
435 if(pOldDC != NULL)
436 {
437 vBmp.SetSelectedInto(NULL);
438 }
439 vDCMem.SelectObject(vBmp);
440
441 //
442 // Center bitmap
443 //
444 int nBmpWidth = vBmp.GetWidth();
445 int nBmpHeight = vBmp.GetHeight();
446
447 //
448 // There should be enough space!
449 //
450 wxASSERT((nBmpWidth <= rRect.width) && (nBmpHeight <= rRect.height));
451
452 int nHeightDiff = m_nHeight - nBmpHeight;
453
454 rDC.Blit( rRect.x + (GetMarginWidth() - nBmpWidth) / 2
455 ,rRect.y + nHeightDiff / 2
456 ,nBmpWidth
457 ,nBmpHeight
458 ,&vDCMem
459 ,0
460 ,0
461 ,wxCOPY
462 ,true
463 );
464
465 if (eStatus & wxODSelected)
466 {
467 POINTL vPnt1 = {rRect.x + 1, rRect.y + 3}; // Leave a little background border
468 POINTL vPnt2 = {rRect.x + GetMarginWidth(), rRect.y + m_nHeight - 3};
469
470 LINEBUNDLE vLine;
471
472 vLine.lColor = vColBack.GetPixel();
473 ::GpiSetAttrs( hPS
474 ,PRIM_LINE
475 ,LBB_COLOR
476 ,0
477 ,&vLine
478 );
479 ::GpiMove(hPS, &vPnt1);
480 ::GpiBox( hPS
481 ,DRO_OUTLINE
482 ,&vPnt2
483 ,0L
484 ,0L
485 );
486 }
487 vBmp.SetSelectedInto(NULL);
488 }
489 }
490 return true;
491 } // end of wxOwnerDrawn::OnDrawItem
492
493 #endif //wxUSE_OWNER_DRAWN