]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/bannerwindow.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/bannerwindow.h
3 // Purpose: wxBannerWindow class implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #if wxUSE_BANNERWINDOW
19 #include "wx/bannerwindow.h"
22 #include "wx/bitmap.h"
23 #include "wx/colour.h"
26 #include "wx/dcbuffer.h"
31 // Some constants for banner layout, currently they're hard coded but we could
32 // easily make them configurable if needed later.
33 const int MARGIN_X
= 5;
34 const int MARGIN_Y
= 5;
36 } // anonymous namespace
38 const char wxBannerWindowNameStr
[] = "bannerwindow";
40 BEGIN_EVENT_TABLE(wxBannerWindow
, wxWindow
)
41 EVT_SIZE(wxBannerWindow::OnSize
)
42 EVT_PAINT(wxBannerWindow::OnPaint
)
45 void wxBannerWindow::Init()
49 m_colStart
= *wxWHITE
;
54 wxBannerWindow::Create(wxWindow
* parent
,
62 if ( !wxWindow::Create(parent
, winid
, pos
, size
, style
, name
) )
67 dir
== wxLEFT
|| dir
== wxRIGHT
|| dir
== wxTOP
|| dir
== wxBOTTOM
,
68 wxS("Invalid banner direction")
73 SetBackgroundStyle(wxBG_STYLE_PAINT
);
78 void wxBannerWindow::SetBitmap(const wxBitmap
& bmp
)
87 void wxBannerWindow::SetText(const wxString
& title
, const wxString
& message
)
97 void wxBannerWindow::SetGradient(const wxColour
& start
, const wxColour
& end
)
105 wxFont
wxBannerWindow::GetTitleFont() const
107 wxFont font
= GetFont();
108 font
.MakeBold().MakeLarger();
112 wxSize
wxBannerWindow::DoGetBestClientSize() const
114 if ( m_bitmap
.IsOk() )
116 return m_bitmap
.GetSize();
120 wxClientDC
dc(const_cast<wxBannerWindow
*>(this));
121 const wxSize sizeText
= dc
.GetMultiLineTextExtent(m_message
);
123 dc
.SetFont(GetTitleFont());
125 const wxSize sizeTitle
= dc
.GetTextExtent(m_title
);
127 wxSize
sizeWin(wxMax(sizeTitle
.x
, sizeText
.x
), sizeTitle
.y
+ sizeText
.y
);
129 // If we draw the text vertically width and height are swapped.
130 if ( m_direction
== wxLEFT
|| m_direction
== wxRIGHT
)
131 wxSwap(sizeWin
.x
, sizeWin
.y
);
133 sizeWin
+= 2*wxSize(MARGIN_X
, MARGIN_Y
);
139 void wxBannerWindow::OnSize(wxSizeEvent
& event
)
146 void wxBannerWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
148 if ( m_bitmap
.IsOk() && m_title
.empty() && m_message
.empty() )
150 // No need for buffering in this case.
153 DrawBitmapBackground(dc
);
155 else // We need to compose our contents ourselves.
157 wxAutoBufferedPaintDC
dc(this);
159 // Deal with the background first.
160 if ( m_bitmap
.IsOk() )
162 DrawBitmapBackground(dc
);
164 else // Draw gradient background.
166 wxDirection gradientDir
;
167 if ( m_direction
== wxLEFT
)
171 else if ( m_direction
== wxRIGHT
)
173 gradientDir
= wxBOTTOM
;
175 else // For both wxTOP and wxBOTTOM.
177 gradientDir
= wxRIGHT
;
180 dc
.GradientFillLinear(GetClientRect(), m_colStart
, m_colEnd
,
184 // Now draw the text on top of it.
185 dc
.SetFont(GetTitleFont());
187 wxPoint
pos(MARGIN_X
, MARGIN_Y
);
188 DrawBannerTextLine(dc
, m_title
, pos
);
189 pos
.y
+= dc
.GetTextExtent(m_title
).y
;
191 dc
.SetFont(GetFont());
193 wxArrayString lines
= wxSplit(m_message
, '\n', '\0');
194 const unsigned numLines
= lines
.size();
195 for ( unsigned n
= 0; n
< numLines
; n
++ )
197 const wxString
& line
= lines
[n
];
199 DrawBannerTextLine(dc
, line
, pos
);
200 pos
.y
+= dc
.GetTextExtent(line
).y
;
205 void wxBannerWindow::DrawBitmapBackground(wxDC
& dc
)
207 switch ( m_direction
)
212 // Draw the bitmap normally, its rightmost or bottom part could be
213 // truncated, as it's meant to be.
214 dc
.DrawBitmap(m_bitmap
, 0, 0);
218 // The top most part of the bitmap may be truncated but its bottom
219 // must be always visible so intentionally draw it possibly partly
220 // outside of the window.
221 dc
.DrawBitmap(m_bitmap
,
222 0, GetClientSize().y
- m_bitmap
.GetHeight());
225 // This case is there only to prevent g++ warnings about not handling
226 // some enum elements in the switch, it can't really happen.
228 wxFAIL_MSG( wxS("Unreachable") );
233 wxBannerWindow::DrawBannerTextLine(wxDC
& dc
,
237 switch ( m_direction
)
241 // The simple case: we just draw the text normally.
242 dc
.DrawText(str
, pos
);
246 // We draw the text vertically and start from the lower left
247 // corner and not the upper left one as usual.
248 dc
.DrawRotatedText(str
, pos
.y
, GetClientSize().y
- pos
.x
, 90);
252 // We also draw the text vertically but now we start from the upper
253 // right corner and draw it from top to bottom.
254 dc
.DrawRotatedText(str
, GetClientSize().x
- pos
.y
, pos
.x
, -90);
258 wxFAIL_MSG( wxS("Unreachable") );
262 #endif // wxUSE_BANNERWINDOW