many compilation fixes for WinCE using VC8 (it now build, although still doesn't...
[wxWidgets.git] / src / msw / renderer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/renderer.cpp
3 // Purpose: implementation of wxRendererNative for Windows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.07.2003
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/window.h"
30 #include "wx/dc.h"
31 #include "wx/settings.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/splitter.h"
35 #include "wx/renderer.h"
36 #include "wx/msw/private.h"
37 #include "wx/msw/uxtheme.h"
38
39 // tmschema.h is in Win32 Platform SDK and might not be available with earlier
40 // compilers
41 #ifndef CP_DROPDOWNBUTTON
42 #define BP_PUSHBUTTON 1
43 #define BP_CHECKBOX 3
44 #define CBS_UNCHECKEDNORMAL 1
45 #define CBS_CHECKEDNORMAL (CBS_UNCHECKEDNORMAL + 4)
46 #define CBS_MIXEDNORMAL (CBS_CHECKEDNORMAL + 4)
47
48 #define PBS_NORMAL 1
49 #define PBS_HOT 2
50 #define PBS_PRESSED 3
51 #define PBS_DISABLED 4
52 #define PBS_DEFAULTED 5
53
54 #define CP_DROPDOWNBUTTON 1
55
56 #define CBXS_NORMAL 1
57 #define CBXS_HOT 2
58 #define CBXS_PRESSED 3
59 #define CBXS_DISABLED 4
60
61 #define TVP_GLYPH 2
62
63 #define GLPS_CLOSED 1
64 #define GLPS_OPENED 2
65
66 #define HP_HEADERITEM 1
67
68 #define HIS_NORMAL 1
69 #define HIS_HOT 2
70 #define HIS_PRESSED 3
71
72 #define TMT_HEIGHT 2417
73
74 #define HP_HEADERSORTARROW 4
75 #define HSAS_SORTEDUP 1
76 #define HSAS_SORTEDDOWN 2
77 #endif
78
79 #if defined(__WXWINCE__)
80 #ifndef DFCS_FLAT
81 #define DFCS_FLAT 0
82 #endif
83 #ifndef DFCS_MONO
84 #define DFCS_MONO 0
85 #endif
86 #endif
87
88 #ifndef DFCS_HOT
89 #define DFCS_HOT 0x1000
90 #endif
91
92 // ----------------------------------------------------------------------------
93 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
94 // ----------------------------------------------------------------------------
95
96 class WXDLLEXPORT wxRendererMSW : public wxDelegateRendererNative
97 {
98 public:
99 wxRendererMSW() { }
100
101 static wxRendererNative& Get();
102
103 virtual void DrawComboBoxDropButton(wxWindow *win,
104 wxDC& dc,
105 const wxRect& rect,
106 int flags = 0);
107
108 virtual void DrawCheckBox(wxWindow *win,
109 wxDC& dc,
110 const wxRect& rect,
111 int flags = 0);
112
113 virtual void DrawPushButton(wxWindow *win,
114 wxDC& dc,
115 const wxRect& rect,
116 int flags = 0);
117
118 private:
119 DECLARE_NO_COPY_CLASS(wxRendererMSW)
120 };
121
122 // ----------------------------------------------------------------------------
123 // wxRendererXP: wxRendererNative implementation for Windows XP and later
124 // ----------------------------------------------------------------------------
125
126 #if wxUSE_UXTHEME
127
128 class WXDLLEXPORT wxRendererXP : public wxDelegateRendererNative
129 {
130 public:
131 wxRendererXP() : wxDelegateRendererNative(wxRendererMSW::Get()) { }
132
133 static wxRendererNative& Get();
134
135 virtual int DrawHeaderButton(wxWindow *win,
136 wxDC& dc,
137 const wxRect& rect,
138 int flags = 0,
139 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
140 wxHeaderButtonParams* params = NULL);
141 virtual int GetHeaderButtonHeight(wxWindow *win);
142
143 virtual void DrawTreeItemButton(wxWindow *win,
144 wxDC& dc,
145 const wxRect& rect,
146 int flags = 0);
147 virtual void DrawSplitterBorder(wxWindow *win,
148 wxDC& dc,
149 const wxRect& rect,
150 int flags = 0);
151 virtual void DrawSplitterSash(wxWindow *win,
152 wxDC& dc,
153 const wxSize& size,
154 wxCoord position,
155 wxOrientation orient,
156 int flags = 0);
157 virtual void DrawComboBoxDropButton(wxWindow *win,
158 wxDC& dc,
159 const wxRect& rect,
160 int flags = 0);
161 virtual void DrawCheckBox(wxWindow *win,
162 wxDC& dc,
163 const wxRect& rect,
164 int flags = 0);
165
166 virtual void DrawPushButton(wxWindow *win,
167 wxDC& dc,
168 const wxRect& rect,
169 int flags = 0);
170
171 virtual void DrawItemSelectionRect(wxWindow *win,
172 wxDC& dc,
173 const wxRect& rect,
174 int flags = 0 );
175
176
177 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
178 private:
179 DECLARE_NO_COPY_CLASS(wxRendererXP)
180 };
181
182 #endif // wxUSE_UXTHEME
183
184 // ============================================================================
185 // wxRendererNative and wxRendererMSW implementation
186 // ============================================================================
187
188 /* static */
189 wxRendererNative& wxRendererNative::GetDefault()
190 {
191 #if wxUSE_UXTHEME
192 wxUxThemeEngine *themeEngine = wxUxThemeEngine::Get();
193 if ( themeEngine && themeEngine->IsAppThemed() )
194 return wxRendererXP::Get();
195 #endif // wxUSE_UXTHEME
196
197 return wxRendererMSW::Get();
198 }
199
200 /* static */
201 wxRendererNative& wxRendererMSW::Get()
202 {
203 static wxRendererMSW s_rendererMSW;
204
205 return s_rendererMSW;
206 }
207
208 void
209 wxRendererMSW::DrawComboBoxDropButton(wxWindow * WXUNUSED(win),
210 wxDC& dc,
211 const wxRect& rect,
212 int flags)
213 {
214 RECT r;
215 wxCopyRectToRECT(rect, r);
216
217 int style = DFCS_SCROLLCOMBOBOX;
218 if ( flags & wxCONTROL_DISABLED )
219 style |= DFCS_INACTIVE;
220 if ( flags & wxCONTROL_PRESSED )
221 style |= DFCS_PUSHED | DFCS_FLAT;
222
223 ::DrawFrameControl(GetHdcOf(dc), &r, DFC_SCROLL, style);
224 }
225
226 void
227 wxRendererMSW::DrawCheckBox(wxWindow * WXUNUSED(win),
228 wxDC& dc,
229 const wxRect& rect,
230 int flags)
231 {
232 RECT r;
233 wxCopyRectToRECT(rect, r);
234
235 int style = DFCS_BUTTONCHECK;
236 if ( flags & wxCONTROL_CHECKED )
237 style |= DFCS_CHECKED;
238 if ( flags & wxCONTROL_DISABLED )
239 style |= DFCS_INACTIVE;
240 if ( flags & wxCONTROL_FLAT )
241 style |= DFCS_MONO;
242 if ( flags & wxCONTROL_PRESSED )
243 style |= DFCS_PUSHED;
244 if ( flags & wxCONTROL_CURRENT )
245 style |= DFCS_HOT;
246
247 ::DrawFrameControl(GetHdcOf(dc), &r, DFC_BUTTON, style);
248 }
249
250 void
251 wxRendererMSW::DrawPushButton(wxWindow * WXUNUSED(win),
252 wxDC& dc,
253 const wxRect& rectOrig,
254 int flags)
255 {
256 wxRect rect(rectOrig);
257
258 int style = DFCS_BUTTONPUSH;
259 if ( flags & wxCONTROL_DISABLED )
260 style |= DFCS_INACTIVE;
261 if ( flags & wxCONTROL_PRESSED )
262 style |= DFCS_PUSHED | DFCS_FLAT;
263 if ( flags & wxCONTROL_ISDEFAULT )
264 {
265 // DrawFrameControl() doesn't seem to support default buttons so we
266 // have to draw the border ourselves
267 wxDCPenChanger pen(dc, *wxBLACK_PEN);
268 wxDCBrushChanger brush(dc, *wxTRANSPARENT_BRUSH);
269 dc.DrawRectangle(rect);
270 rect.Deflate(1);
271 }
272
273 RECT rc;
274 wxCopyRectToRECT(rect, rc);
275
276 ::DrawFrameControl(GetHdcOf(dc), &rc, DFC_BUTTON, style);
277 }
278
279 // ============================================================================
280 // wxRendererXP implementation
281 // ============================================================================
282
283 #if wxUSE_UXTHEME
284
285 /* static */
286 wxRendererNative& wxRendererXP::Get()
287 {
288 static wxRendererXP s_rendererXP;
289
290 return s_rendererXP;
291 }
292
293 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
294 // default theme, for example), so the caller should have cleared button's
295 // background before this call. This is quite likely a wxMSW-specific thing.
296 void
297 wxRendererXP::DrawComboBoxDropButton(wxWindow * win,
298 wxDC& dc,
299 const wxRect& rect,
300 int flags)
301 {
302 wxUxThemeHandle hTheme(win, L"COMBOBOX");
303 if ( !hTheme )
304 {
305 m_rendererNative.DrawComboBoxDropButton(win, dc, rect, flags);
306 return;
307 }
308
309 RECT r;
310 wxCopyRectToRECT(rect, r);
311
312 int state;
313 if ( flags & wxCONTROL_PRESSED )
314 state = CBXS_PRESSED;
315 else if ( flags & wxCONTROL_CURRENT )
316 state = CBXS_HOT;
317 else if ( flags & wxCONTROL_DISABLED )
318 state = CBXS_DISABLED;
319 else
320 state = CBXS_NORMAL;
321
322 wxUxThemeEngine::Get()->DrawThemeBackground
323 (
324 hTheme,
325 GetHdcOf(dc),
326 CP_DROPDOWNBUTTON,
327 state,
328 &r,
329 NULL
330 );
331
332 }
333
334 int
335 wxRendererXP::DrawHeaderButton(wxWindow *win,
336 wxDC& dc,
337 const wxRect& rect,
338 int flags,
339 wxHeaderSortIconType sortArrow,
340 wxHeaderButtonParams* params)
341 {
342 wxUxThemeHandle hTheme(win, L"HEADER");
343 if ( !hTheme )
344 {
345 return m_rendererNative.DrawHeaderButton(win, dc, rect, flags, sortArrow, params);
346 }
347
348 RECT r;
349 wxCopyRectToRECT(rect, r);
350
351 int state;
352 if ( flags & wxCONTROL_PRESSED )
353 state = HIS_PRESSED;
354 else if ( flags & wxCONTROL_CURRENT )
355 state = HIS_HOT;
356 else
357 state = HIS_NORMAL;
358 wxUxThemeEngine::Get()->DrawThemeBackground
359 (
360 hTheme,
361 GetHdcOf(dc),
362 HP_HEADERITEM,
363 state,
364 &r,
365 NULL
366 );
367
368 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
369 // Why? If this can be fixed then draw the sort arrows using the theme
370 // and then clear those flags before calling DrawHeaderButtonContents.
371
372 // Add any extras that are specified in flags and params
373 return DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
374 }
375
376
377 int
378 wxRendererXP::GetHeaderButtonHeight(wxWindow *win)
379 {
380 wxUxThemeHandle hTheme(win, L"HEADER");
381 if ( !hTheme )
382 {
383 return m_rendererNative.GetHeaderButtonHeight(win);
384 }
385
386 HRESULT hr;
387 int value = -1;
388
389 hr = wxUxThemeEngine::Get()->GetThemeMetric( hTheme,
390 NULL,
391 HP_HEADERITEM,
392 HIS_NORMAL,
393 TMT_HEIGHT,
394 &value );
395 if ( hr == S_OK )
396 return value;
397 else
398 return 20;
399 }
400
401
402 void
403 wxRendererXP::DrawTreeItemButton(wxWindow *win,
404 wxDC& dc,
405 const wxRect& rect,
406 int flags)
407 {
408 wxUxThemeHandle hTheme(win, L"TREEVIEW");
409 if ( !hTheme )
410 {
411 m_rendererNative.DrawTreeItemButton(win, dc, rect, flags);
412 return;
413 }
414
415 RECT r;
416 wxCopyRectToRECT(rect, r);
417
418 int state = flags & wxCONTROL_EXPANDED ? GLPS_OPENED : GLPS_CLOSED;
419 wxUxThemeEngine::Get()->DrawThemeBackground
420 (
421 hTheme,
422 GetHdcOf(dc),
423 TVP_GLYPH,
424 state,
425 &r,
426 NULL
427 );
428 }
429
430 void
431 wxRendererXP::DrawCheckBox(wxWindow *win,
432 wxDC& dc,
433 const wxRect& rect,
434 int flags)
435 {
436 wxUxThemeHandle hTheme(win, L"BUTTON");
437 if ( !hTheme )
438 {
439 m_rendererNative.DrawCheckBox(win, dc, rect, flags);
440 return;
441 }
442
443 RECT r;
444 wxCopyRectToRECT(rect, r);
445
446 int state;
447 if ( flags & wxCONTROL_CHECKED )
448 state = CBS_CHECKEDNORMAL;
449 else if ( flags & wxCONTROL_UNDETERMINED )
450 state = CBS_MIXEDNORMAL;
451 else
452 state = CBS_UNCHECKEDNORMAL;
453
454 // CBS_XXX is followed by CBX_XXXHOT, then CBS_XXXPRESSED and DISABLED
455 enum
456 {
457 CBS_HOT_OFFSET = 1,
458 CBS_PRESSED_OFFSET = 2,
459 CBS_DISABLED_OFFSET = 3
460 };
461
462 if ( flags & wxCONTROL_DISABLED )
463 state += CBS_DISABLED_OFFSET;
464 else if ( flags & wxCONTROL_PRESSED )
465 state += CBS_PRESSED_OFFSET;
466 else if ( flags & wxCONTROL_CURRENT )
467 state += CBS_HOT_OFFSET;
468
469 wxUxThemeEngine::Get()->DrawThemeBackground
470 (
471 hTheme,
472 GetHdcOf(dc),
473 BP_CHECKBOX,
474 state,
475 &r,
476 NULL
477 );
478 }
479
480 void
481 wxRendererXP::DrawPushButton(wxWindow * win,
482 wxDC& dc,
483 const wxRect& rect,
484 int flags)
485 {
486 wxUxThemeHandle hTheme(win, L"BUTTON");
487 if ( !hTheme )
488 {
489 m_rendererNative.DrawPushButton(win, dc, rect, flags);
490 return;
491 }
492
493 RECT r;
494 wxCopyRectToRECT(rect, r);
495
496 int state;
497 if ( flags & wxCONTROL_PRESSED )
498 state = PBS_PRESSED;
499 else if ( flags & wxCONTROL_CURRENT )
500 state = PBS_HOT;
501 else if ( flags & wxCONTROL_DISABLED )
502 state = PBS_DISABLED;
503 else if ( flags & wxCONTROL_ISDEFAULT )
504 state = PBS_DEFAULTED;
505 else
506 state = PBS_NORMAL;
507
508 wxUxThemeEngine::Get()->DrawThemeBackground
509 (
510 hTheme,
511 GetHdcOf(dc),
512 BP_PUSHBUTTON,
513 state,
514 &r,
515 NULL
516 );
517
518 }
519
520 void
521 wxRendererXP::DrawItemSelectionRect(wxWindow * WXUNUSED(win),
522 wxDC& dc,
523 const wxRect& rect,
524 int flags)
525 {
526 wxBrush brush;
527 if ( flags & wxCONTROL_SELECTED )
528 {
529 if ( flags & wxCONTROL_FOCUSED )
530 {
531 brush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
532 }
533 else // !focused
534 {
535 brush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));
536 }
537 }
538 else // !selected
539 {
540 brush = *wxTRANSPARENT_BRUSH;
541 }
542
543 dc.SetBrush(brush);
544
545 // unlike for wxRendererGeneric, on windows we _never_ want to draw
546 // the outline of the rectangle:
547 dc.SetPen(*wxTRANSPARENT_PEN);
548
549 dc.DrawRectangle( rect );
550 }
551
552
553
554 // ----------------------------------------------------------------------------
555 // splitter drawing
556 // ----------------------------------------------------------------------------
557
558 // the width of the sash: this is the same as used by Explorer...
559 static const wxCoord SASH_WIDTH = 4;
560
561 wxSplitterRenderParams
562 wxRendererXP::GetSplitterParams(const wxWindow * win)
563 {
564 if ( win->HasFlag(wxSP_NO_XP_THEME) )
565 return m_rendererNative.GetSplitterParams(win);
566 else
567 return wxSplitterRenderParams(SASH_WIDTH, 0, false);
568 }
569
570 void
571 wxRendererXP::DrawSplitterBorder(wxWindow * win,
572 wxDC& dc,
573 const wxRect& rect,
574 int flags)
575 {
576 if ( win->HasFlag(wxSP_NO_XP_THEME) )
577 {
578 m_rendererNative.DrawSplitterBorder(win, dc, rect, flags);
579 }
580 }
581
582 void
583 wxRendererXP::DrawSplitterSash(wxWindow *win,
584 wxDC& dc,
585 const wxSize& size,
586 wxCoord position,
587 wxOrientation orient,
588 int flags)
589 {
590 if ( !win->HasFlag(wxSP_NO_XP_THEME) )
591 {
592 dc.SetPen(*wxTRANSPARENT_PEN);
593 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)));
594 if ( orient == wxVERTICAL )
595 {
596 dc.DrawRectangle(position, 0, SASH_WIDTH, size.y);
597 }
598 else // wxHORIZONTAL
599 {
600 dc.DrawRectangle(0, position, size.x, SASH_WIDTH);
601 }
602
603 return;
604 }
605
606 m_rendererNative.DrawSplitterSash(win, dc, size, position, orient, flags);
607 }
608
609 #endif // wxUSE_UXTHEME