add compilation options to allow compiling in just the selected wxUniv themes and...
[wxWidgets.git] / src / univ / themes / metal.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/themes/metal.cpp
3 // Purpose: wxUniversal theme implementing Win32-like LNF
4 // Author: Vadim Zeitlin, Robert Roebling
5 // Modified by:
6 // Created: 06.08.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: 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 #if wxUSE_ALL_THEMES || wxUSE_THEME_METAL
28
29 #ifndef WX_PRECOMP
30 #include "wx/timer.h"
31 #include "wx/intl.h"
32 #include "wx/dc.h"
33 #include "wx/window.h"
34
35 #include "wx/dcmemory.h"
36
37 #include "wx/button.h"
38 #include "wx/listbox.h"
39 #include "wx/checklst.h"
40 #include "wx/combobox.h"
41 #include "wx/scrolbar.h"
42 #include "wx/slider.h"
43 #include "wx/textctrl.h"
44 #include "wx/toolbar.h"
45
46 #include "wx/menu.h"
47 #include "wx/settings.h"
48 #include "wx/toplevel.h"
49 #endif // WX_PRECOMP
50
51 #include "wx/notebook.h"
52 #include "wx/spinbutt.h"
53 #include "wx/artprov.h"
54
55 #include "wx/univ/scrtimer.h"
56 #include "wx/univ/renderer.h"
57 #include "wx/univ/inpcons.h"
58 #include "wx/univ/inphand.h"
59 #include "wx/univ/colschem.h"
60 #include "wx/univ/theme.h"
61
62 // wxMetalRenderer: draw the GUI elements in Metal style
63 // ----------------------------------------------------------------------------
64
65 class wxMetalRenderer : public wxDelegateRenderer
66 {
67 // FIXME cut'n'paste from Win32
68 enum wxArrowDirection
69 {
70 Arrow_Left,
71 Arrow_Right,
72 Arrow_Up,
73 Arrow_Down,
74 Arrow_Max
75 };
76
77 enum wxArrowStyle
78 {
79 Arrow_Normal,
80 Arrow_Disabled,
81 Arrow_Pressed,
82 Arrow_Inverted,
83 Arrow_InvertedDisabled,
84 Arrow_StateMax
85 };
86 public:
87 wxMetalRenderer(wxRenderer *renderer, wxColourScheme* scheme);
88
89 virtual void DrawButtonSurface(wxDC& dc,
90 const wxColour& WXUNUSED(col),
91 const wxRect& rect,
92 int WXUNUSED(flags))
93 { DrawMetal(dc, rect); }
94
95 virtual void DrawScrollbarThumb(wxDC& dc,
96 wxOrientation orient,
97 const wxRect& rect,
98 int flags);
99
100 virtual void DrawScrollbarShaft(wxDC& dc,
101 wxOrientation orient,
102 const wxRect& rectBar,
103 int flags);
104
105 virtual void GetComboBitmaps(wxBitmap *bmpNormal,
106 wxBitmap *bmpFocus,
107 wxBitmap *bmpPressed,
108 wxBitmap *bmpDisabled);
109
110 virtual void DrawArrow(wxDC& dc,
111 wxDirection dir,
112 const wxRect& rect,
113 int flags = 0);
114 protected:
115 void DrawArrowButton(wxDC& dc,
116 const wxRect& rectAll,
117 wxArrowDirection arrowDir,
118 wxArrowStyle arrowStyle);
119
120 void DrawRect(wxDC& dc, wxRect *rect, const wxPen& pen);
121
122 void DrawShadedRect(wxDC& dc, wxRect *rect,
123 const wxPen& pen1, const wxPen& pen2);
124
125 void DrawArrowBorder(wxDC& dc, wxRect *rect, bool isPressed = false);
126
127 void DrawArrow(wxDC& dc, const wxRect& rect,
128 wxArrowDirection arrowDir, wxArrowStyle arrowStyle);
129
130 void DrawMetal(wxDC &dc, const wxRect &rect );
131 private:
132 wxPen m_penBlack,
133 m_penDarkGrey,
134 m_penLightGrey,
135 m_penHighlight;
136
137 wxBitmap m_bmpArrows[Arrow_StateMax][Arrow_Max];
138 };
139
140 // ----------------------------------------------------------------------------
141 // wxMetalTheme
142 // ----------------------------------------------------------------------------
143
144 class wxMetalTheme : public wxTheme
145 {
146 public:
147 wxMetalTheme();
148 virtual ~wxMetalTheme();
149
150 virtual wxRenderer *GetRenderer();
151 virtual wxArtProvider *GetArtProvider();
152 virtual wxInputHandler *GetInputHandler(const wxString& control,
153 wxInputConsumer *consumer);
154 virtual wxColourScheme *GetColourScheme();
155
156 private:
157 bool GetOrCreateTheme()
158 {
159 if ( !m_win32Theme )
160 m_win32Theme = wxTheme::Create( wxT("win32") );
161 return m_win32Theme != NULL;
162 }
163
164 wxTheme *m_win32Theme;
165 wxMetalRenderer *m_renderer;
166
167 WX_DECLARE_THEME(Metal)
168 };
169
170 // ============================================================================
171 // implementation
172 // ============================================================================
173
174 WX_IMPLEMENT_THEME(wxMetalTheme, Metal, wxTRANSLATE("Metal theme"));
175
176 // ----------------------------------------------------------------------------
177 // wxMetalTheme
178 // ----------------------------------------------------------------------------
179
180 wxMetalTheme::wxMetalTheme()
181 {
182 m_win32Theme = NULL;
183 m_renderer = NULL;
184 }
185
186 wxMetalTheme::~wxMetalTheme()
187 {
188 delete m_win32Theme;
189 delete m_renderer;
190 }
191
192 wxRenderer *wxMetalTheme::GetRenderer()
193 {
194 if ( !GetOrCreateTheme() )
195 return 0;
196 if ( !m_renderer )
197 m_renderer = new wxMetalRenderer(m_win32Theme->GetRenderer(),
198 m_win32Theme->GetColourScheme());
199
200 return m_renderer;
201 }
202
203 wxArtProvider *wxMetalTheme::GetArtProvider()
204 {
205 if ( !GetOrCreateTheme() )
206 return 0;
207 return m_win32Theme->GetArtProvider();
208 }
209
210 wxInputHandler *wxMetalTheme::GetInputHandler(const wxString& control,
211 wxInputConsumer *consumer)
212 {
213 if ( !GetOrCreateTheme() )
214 return 0;
215 return m_win32Theme->GetInputHandler(control, consumer);
216 }
217
218 wxColourScheme *wxMetalTheme::GetColourScheme()
219 {
220 if ( !GetOrCreateTheme() )
221 return 0;
222 return m_win32Theme->GetColourScheme();
223 }
224
225 // ----------------------------------------------------------------------------
226 // wxMetalRenderer
227 // ----------------------------------------------------------------------------
228
229 wxMetalRenderer::wxMetalRenderer(wxRenderer *renderer, wxColourScheme *scheme)
230 : wxDelegateRenderer(renderer)
231 {
232 // init colours and pens
233 m_penBlack = wxPen(wxSCHEME_COLOUR(scheme, SHADOW_DARK), 0, wxSOLID);
234 m_penDarkGrey = wxPen(wxSCHEME_COLOUR(scheme, SHADOW_OUT), 0, wxSOLID);
235 m_penLightGrey = wxPen(wxSCHEME_COLOUR(scheme, SHADOW_IN), 0, wxSOLID);
236 m_penHighlight = wxPen(wxSCHEME_COLOUR(scheme, SHADOW_HIGHLIGHT), 0, wxSOLID);
237
238 // init the arrow bitmaps
239 static const size_t ARROW_WIDTH = 7;
240 static const size_t ARROW_LENGTH = 4;
241
242 wxMask *mask;
243 wxMemoryDC dcNormal,
244 dcDisabled,
245 dcInverse;
246 for ( size_t n = 0; n < Arrow_Max; n++ )
247 {
248 bool isVertical = n > Arrow_Right;
249 int w, h;
250 if ( isVertical )
251 {
252 w = ARROW_WIDTH;
253 h = ARROW_LENGTH;
254 }
255 else
256 {
257 h = ARROW_WIDTH;
258 w = ARROW_LENGTH;
259 }
260
261 // disabled arrow is larger because of the shadow
262 m_bmpArrows[Arrow_Normal][n].Create(w, h);
263 m_bmpArrows[Arrow_Disabled][n].Create(w + 1, h + 1);
264
265 dcNormal.SelectObject(m_bmpArrows[Arrow_Normal][n]);
266 dcDisabled.SelectObject(m_bmpArrows[Arrow_Disabled][n]);
267
268 dcNormal.SetBackground(*wxWHITE_BRUSH);
269 dcDisabled.SetBackground(*wxWHITE_BRUSH);
270 dcNormal.Clear();
271 dcDisabled.Clear();
272
273 dcNormal.SetPen(m_penBlack);
274 dcDisabled.SetPen(m_penDarkGrey);
275
276 // calculate the position of the point of the arrow
277 wxCoord x1, y1;
278 if ( isVertical )
279 {
280 x1 = (ARROW_WIDTH - 1)/2;
281 y1 = n == Arrow_Up ? 0 : ARROW_LENGTH - 1;
282 }
283 else // horizontal
284 {
285 x1 = n == Arrow_Left ? 0 : ARROW_LENGTH - 1;
286 y1 = (ARROW_WIDTH - 1)/2;
287 }
288
289 wxCoord x2 = x1,
290 y2 = y1;
291
292 if ( isVertical )
293 x2++;
294 else
295 y2++;
296
297 for ( size_t i = 0; i < ARROW_LENGTH; i++ )
298 {
299 dcNormal.DrawLine(x1, y1, x2, y2);
300 dcDisabled.DrawLine(x1, y1, x2, y2);
301
302 if ( isVertical )
303 {
304 x1--;
305 x2++;
306
307 if ( n == Arrow_Up )
308 {
309 y1++;
310 y2++;
311 }
312 else // down arrow
313 {
314 y1--;
315 y2--;
316 }
317 }
318 else // left or right arrow
319 {
320 y1--;
321 y2++;
322
323 if ( n == Arrow_Left )
324 {
325 x1++;
326 x2++;
327 }
328 else
329 {
330 x1--;
331 x2--;
332 }
333 }
334 }
335
336 // draw the shadow for the disabled one
337 dcDisabled.SetPen(m_penHighlight);
338 switch ( n )
339 {
340 case Arrow_Left:
341 y1 += 2;
342 dcDisabled.DrawLine(x1, y1, x2, y2);
343 break;
344
345 case Arrow_Right:
346 x1 = ARROW_LENGTH - 1;
347 y1 = (ARROW_WIDTH - 1)/2 + 1;
348 x2 = 0;
349 y2 = ARROW_WIDTH;
350 dcDisabled.DrawLine(x1, y1, x2, y2);
351 dcDisabled.DrawLine(++x1, y1, x2, ++y2);
352 break;
353
354 case Arrow_Up:
355 x1 += 2;
356 dcDisabled.DrawLine(x1, y1, x2, y2);
357 break;
358
359 case Arrow_Down:
360 x1 = ARROW_WIDTH - 1;
361 y1 = 1;
362 x2 = (ARROW_WIDTH - 1)/2;
363 y2 = ARROW_LENGTH;
364 dcDisabled.DrawLine(x1, y1, x2, y2);
365 dcDisabled.DrawLine(++x1, y1, x2, ++y2);
366 break;
367
368 }
369
370 // create the inverted bitmap but only for the right arrow as we only
371 // use it for the menus
372 if ( n == Arrow_Right )
373 {
374 m_bmpArrows[Arrow_Inverted][n].Create(w, h);
375 dcInverse.SelectObject(m_bmpArrows[Arrow_Inverted][n]);
376 dcInverse.Clear();
377 dcInverse.Blit(0, 0, w, h,
378 &dcNormal, 0, 0,
379 wxXOR);
380 dcInverse.SelectObject(wxNullBitmap);
381
382 mask = new wxMask(m_bmpArrows[Arrow_Inverted][n], *wxBLACK);
383 m_bmpArrows[Arrow_Inverted][n].SetMask(mask);
384
385 m_bmpArrows[Arrow_InvertedDisabled][n].Create(w, h);
386 dcInverse.SelectObject(m_bmpArrows[Arrow_InvertedDisabled][n]);
387 dcInverse.Clear();
388 dcInverse.Blit(0, 0, w, h,
389 &dcDisabled, 0, 0,
390 wxXOR);
391 dcInverse.SelectObject(wxNullBitmap);
392
393 mask = new wxMask(m_bmpArrows[Arrow_InvertedDisabled][n], *wxBLACK);
394 m_bmpArrows[Arrow_InvertedDisabled][n].SetMask(mask);
395 }
396
397 dcNormal.SelectObject(wxNullBitmap);
398 dcDisabled.SelectObject(wxNullBitmap);
399
400 mask = new wxMask(m_bmpArrows[Arrow_Normal][n], *wxWHITE);
401 m_bmpArrows[Arrow_Normal][n].SetMask(mask);
402 mask = new wxMask(m_bmpArrows[Arrow_Disabled][n], *wxWHITE);
403 m_bmpArrows[Arrow_Disabled][n].SetMask(mask);
404
405 m_bmpArrows[Arrow_Pressed][n] = m_bmpArrows[Arrow_Normal][n];
406 }
407 }
408
409 void wxMetalRenderer::DrawScrollbarThumb(wxDC& dc,
410 wxOrientation WXUNUSED(orient),
411 const wxRect& rect,
412 int WXUNUSED(flags))
413 {
414 // we don't use the flags, the thumb never changes appearance
415 wxRect rectThumb = rect;
416 DrawArrowBorder(dc, &rectThumb);
417 DrawMetal(dc, rectThumb);
418 }
419
420 void wxMetalRenderer::DrawScrollbarShaft(wxDC& dc,
421 wxOrientation WXUNUSED(orient),
422 const wxRect& rectBar,
423 int WXUNUSED(flags))
424 {
425 DrawMetal(dc, rectBar);
426 }
427
428 void wxMetalRenderer::GetComboBitmaps(wxBitmap *bmpNormal,
429 wxBitmap * WXUNUSED(bmpFocus),
430 wxBitmap *bmpPressed,
431 wxBitmap *bmpDisabled)
432 {
433 static const wxCoord widthCombo = 16;
434 static const wxCoord heightCombo = 17;
435
436 wxMemoryDC dcMem;
437
438 if ( bmpNormal )
439 {
440 bmpNormal->Create(widthCombo, heightCombo);
441 dcMem.SelectObject(*bmpNormal);
442 DrawArrowButton(dcMem, wxRect(0, 0, widthCombo, heightCombo),
443 Arrow_Down, Arrow_Normal);
444 }
445
446 if ( bmpPressed )
447 {
448 bmpPressed->Create(widthCombo, heightCombo);
449 dcMem.SelectObject(*bmpPressed);
450 DrawArrowButton(dcMem, wxRect(0, 0, widthCombo, heightCombo),
451 Arrow_Down, Arrow_Pressed);
452 }
453
454 if ( bmpDisabled )
455 {
456 bmpDisabled->Create(widthCombo, heightCombo);
457 dcMem.SelectObject(*bmpDisabled);
458 DrawArrowButton(dcMem, wxRect(0, 0, widthCombo, heightCombo),
459 Arrow_Down, Arrow_Disabled);
460 }
461 }
462
463 void wxMetalRenderer::DrawArrow(wxDC& dc,
464 wxDirection dir,
465 const wxRect& rect,
466 int flags)
467 {
468 // get the bitmap for this arrow
469 wxArrowDirection arrowDir;
470 switch ( dir )
471 {
472 case wxLEFT: arrowDir = Arrow_Left; break;
473 case wxRIGHT: arrowDir = Arrow_Right; break;
474 case wxUP: arrowDir = Arrow_Up; break;
475 case wxDOWN: arrowDir = Arrow_Down; break;
476
477 default:
478 wxFAIL_MSG(_T("unknown arrow direction"));
479 return;
480 }
481
482 wxArrowStyle arrowStyle;
483 if ( flags & wxCONTROL_PRESSED )
484 {
485 // can't be pressed and disabled
486 arrowStyle = Arrow_Pressed;
487 }
488 else
489 {
490 arrowStyle = flags & wxCONTROL_DISABLED ? Arrow_Disabled : Arrow_Normal;
491 }
492
493 DrawArrowButton(dc, rect, arrowDir, arrowStyle);
494 }
495
496 //
497 // protected functions
498 //
499
500 void wxMetalRenderer::DrawArrowButton(wxDC& dc,
501 const wxRect& rectAll,
502 wxArrowDirection arrowDir,
503 wxArrowStyle arrowStyle)
504 {
505 wxRect rect = rectAll;
506 DrawMetal( dc, rect );
507 DrawArrowBorder(dc, &rect, arrowStyle == Arrow_Pressed);
508 DrawArrow(dc, rect, arrowDir, arrowStyle);
509 }
510
511 void wxMetalRenderer::DrawRect(wxDC& dc, wxRect *rect, const wxPen& pen)
512 {
513 // draw
514 dc.SetPen(pen);
515 dc.SetBrush(*wxTRANSPARENT_BRUSH);
516 dc.DrawRectangle(*rect);
517
518 // adjust the rect
519 rect->Inflate(-1);
520 }
521
522 void wxMetalRenderer::DrawShadedRect(wxDC& dc, wxRect *rect,
523 const wxPen& pen1, const wxPen& pen2)
524 {
525 // draw the rectangle
526 dc.SetPen(pen1);
527 dc.DrawLine(rect->GetLeft(), rect->GetTop(),
528 rect->GetLeft(), rect->GetBottom());
529 dc.DrawLine(rect->GetLeft() + 1, rect->GetTop(),
530 rect->GetRight(), rect->GetTop());
531 dc.SetPen(pen2);
532 dc.DrawLine(rect->GetRight(), rect->GetTop(),
533 rect->GetRight(), rect->GetBottom());
534 dc.DrawLine(rect->GetLeft(), rect->GetBottom(),
535 rect->GetRight() + 1, rect->GetBottom());
536
537 // adjust the rect
538 rect->Inflate(-1);
539 }
540
541 void wxMetalRenderer::DrawArrowBorder(wxDC& dc, wxRect *rect, bool isPressed)
542 {
543 if ( isPressed )
544 {
545 DrawRect(dc, rect, m_penDarkGrey);
546
547 // the arrow is usually drawn inside border of width 2 and is offset by
548 // another pixel in both directions when it's pressed - as the border
549 // in this case is more narrow as well, we have to adjust rect like
550 // this:
551 rect->Inflate(-1);
552 rect->x++;
553 rect->y++;
554 }
555 else
556 {
557 DrawShadedRect(dc, rect, m_penLightGrey, m_penBlack);
558 DrawShadedRect(dc, rect, m_penHighlight, m_penDarkGrey);
559 }
560 }
561
562 void wxMetalRenderer::DrawArrow(wxDC& dc,
563 const wxRect& rect,
564 wxArrowDirection arrowDir,
565 wxArrowStyle arrowStyle)
566 {
567 const wxBitmap& bmp = m_bmpArrows[arrowStyle][arrowDir];
568
569 // under Windows the arrows always have the same size so just centre it in
570 // the provided rectangle
571 wxCoord x = rect.x + (rect.width - bmp.GetWidth()) / 2,
572 y = rect.y + (rect.height - bmp.GetHeight()) / 2;
573
574 // Windows does it like this...
575 if ( arrowDir == Arrow_Left )
576 x--;
577
578 // draw it
579 dc.DrawBitmap(bmp, x, y, true /* use mask */);
580 }
581
582 // ----------------------------------------------------------------------------
583 // metal gradient
584 // ----------------------------------------------------------------------------
585
586 void wxMetalRenderer::DrawMetal(wxDC &dc, const wxRect &rect )
587 {
588 dc.SetPen(*wxTRANSPARENT_PEN);
589 for (int y = rect.y; y < rect.height+rect.y; y++)
590 {
591 unsigned char intens = (unsigned char)(230 + 80 * (rect.y-y) / rect.height);
592 dc.SetBrush( wxBrush( wxColour(intens,intens,intens), wxSOLID ) );
593 dc.DrawRectangle( rect.x, y, rect.width, 1 );
594 }
595 }
596
597 #endif // wxUSE_ALL_THEMES || wxUSE_THEME_METAL