]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/foldbar/captionbar.cpp
made XPM data const
[wxWidgets.git] / contrib / src / foldbar / captionbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: captionbar.cpp
3 // Purpose: wxCaptionBar class belonging to the wxFoldPanel (but can be used independent)
4 // Author: Jorgen Bodde
5 // Modified by: ABX - 19/12/2004 : possibility of horizontal orientation
6 // : wxWidgets coding standards
7 // Created: 18/06/2004
8 // RCS-ID: $Id$
9 // Copyright: (c) Jorgen Bodde
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "foldpanelbar.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/foldbar/foldpanelbar.h"
29
30 /*
31 * wxCaptionBar
32 */
33
34 BEGIN_EVENT_TABLE(wxCaptionBar, wxWindow)
35 EVT_PAINT(wxCaptionBar::OnPaint)
36 EVT_CHAR(wxCaptionBar::OnChar)
37 EVT_MOUSE_EVENTS(wxCaptionBar::OnMouseEvent)
38 EVT_SIZE(wxCaptionBar::OnSize)
39 END_EVENT_TABLE()
40
41 wxCaptionBar::wxCaptionBar(wxWindow* parent, const wxString &caption, wxImageList *images, wxWindowID id,
42 const wxCaptionBarStyle &cbstyle, const wxPoint& pos, const wxSize& size, long style)
43 : wxWindow(parent, id, pos, size, style)
44 , m_caption(caption)
45 , m_foldIcons(images)
46 , m_rightIndent(wxFPB_BMP_RIGHTSPACE)
47 , m_iconWidth(16)
48 , m_iconHeight(16)
49 , m_collapsed(false)
50 {
51 // do initialisy thingy stuff
52
53 ApplyCaptionStyle(cbstyle, true);
54
55 // set initial size
56 if(m_foldIcons)
57 {
58 wxASSERT(m_foldIcons->GetImageCount() > 1);
59 m_foldIcons->GetSize(0, m_iconWidth, m_iconHeight);
60 }
61 }
62
63 wxCaptionBar::~wxCaptionBar()
64 {
65
66 }
67
68 void wxCaptionBar::ApplyCaptionStyle(const wxCaptionBarStyle &cbstyle, bool applyDefault)
69 {
70 wxASSERT(GetParent());
71
72 wxCaptionBarStyle newstyle = cbstyle;
73
74 // set defaults in newly created style copy if needed
75 if(applyDefault)
76 {
77 // get first colour from style or make it default
78 if(!newstyle.FirstColourUsed())
79 newstyle.SetFirstColour(*wxWHITE);
80
81 // get second colour from style or make it default
82 if(!newstyle.SecondColourUsed())
83 {
84 // make the second colour slightly darker then the background
85 wxColour col = GetParent()->GetBackgroundColour();
86 col.Set((unsigned char)((col.Red() >> 1) + 20),
87 (unsigned char)((col.Green() >> 1) + 20),
88 (unsigned char)((col.Blue() >> 1) + 20));
89 newstyle.SetSecondColour(col);
90 }
91
92 // get text colour
93 if(!newstyle.CaptionColourUsed())
94 newstyle.SetCaptionColour(*wxBLACK);
95
96 // get font colour
97 if(!newstyle.CaptionFontUsed())
98 newstyle.SetCaptionFont(GetParent()->GetFont());
99
100 // apply caption style
101 if(!newstyle.CaptionStyleUsed())
102 newstyle.SetCaptionStyle(wxCAPTIONBAR_GRADIENT_V);
103 }
104
105 // apply the style
106 m_captionStyle = newstyle;
107 }
108
109 void wxCaptionBar::OnPaint(wxPaintEvent& WXUNUSED(event))
110 {
111 wxPaintDC dc(this);
112 wxRect wndRect = GetRect();
113 bool vertical = IsVertical();
114
115 // TODO: Maybe first a memory DC should draw all, and then paint it on the
116 // caption. This way a flickering arrow during resize is not visible
117
118 // draw basics
119
120 FillCaptionBackground(dc);
121
122 dc.SetFont(m_captionStyle.GetCaptionFont());
123 if(vertical)
124 dc.DrawText(m_caption, 4, (wxFPB_EXTRA_Y / 2));
125 else
126 dc.DrawRotatedText(m_caption, (wxFPB_EXTRA_Y / 2) , wndRect.GetBottom() - 4 , 90 );
127
128 // draw small icon, either collapsed or expanded
129 // based on the state of the bar. If we have
130 // any bmp's
131
132 if(m_foldIcons)
133 {
134 wxCHECK2(m_foldIcons->GetImageCount() > 1, return);
135
136 int index = 0;
137 if(m_collapsed)
138 index = 1;
139
140 if(vertical)
141 m_foldIcons->Draw(index,
142 dc,
143 wndRect.GetRight() - m_iconWidth - m_rightIndent,
144 (wndRect.GetHeight() - m_iconHeight) / 2,
145 wxIMAGELIST_DRAW_TRANSPARENT);
146 else
147 m_foldIcons->Draw(index,
148 dc,
149 (wndRect.GetWidth() - m_iconWidth) / 2,
150 m_rightIndent,
151 wxIMAGELIST_DRAW_TRANSPARENT);
152 }
153 }
154
155 void wxCaptionBar::FillCaptionBackground(wxPaintDC &dc)
156 {
157 // dispatch right style for caption drawing
158
159 switch(m_captionStyle.GetCaptionStyle())
160 {
161 case wxCAPTIONBAR_GRADIENT_V:
162 if (IsVertical())
163 DrawVerticalGradient(dc, GetRect());
164 else
165 DrawHorizontalGradient(dc, GetRect());
166 break;
167 case wxCAPTIONBAR_GRADIENT_H:
168 if (IsVertical())
169 DrawHorizontalGradient(dc, GetRect());
170 else
171 DrawVerticalGradient(dc, GetRect());
172 break;
173 case wxCAPTIONBAR_SINGLE:
174 DrawSingleColour(dc, GetRect());
175 break;
176 case wxCAPTIONBAR_RECTANGLE:
177 case wxCAPTIONBAR_FILLED_RECTANGLE:
178 DrawSingleRectangle(dc, GetRect());
179 break;
180 default:
181 break;
182 }
183 }
184
185 void wxCaptionBar::OnMouseEvent(wxMouseEvent& event)
186 {
187 // if clicked on the arrow (single) or double on the caption
188 // we change state and an event must be fired to let this
189 // panel collapse or expand
190
191 bool send_event = false;
192
193 if (event.LeftDown() && m_foldIcons)
194 {
195 wxPoint pt(event.GetPosition());
196 wxRect rect = GetRect();
197 bool vertical = IsVertical();
198
199 if((vertical && pt.x > (rect.GetWidth() - m_iconWidth - m_rightIndent))||
200 (!vertical && pt.y < m_iconHeight + m_rightIndent))
201 send_event = true;
202 }
203 else if(event.LeftDClick())
204 send_event = true;
205
206 // send the collapse, expand event to the parent
207
208 if(send_event)
209 {
210 wxCaptionBarEvent event(wxEVT_CAPTIONBAR);
211 event.SetCaptionBar(this);
212
213 ::wxPostEvent(this, event);
214
215 }
216 }
217
218 void wxCaptionBar::OnChar(wxKeyEvent &event)
219 {
220 // TODO: Anything here?
221
222 event.Skip();
223 }
224
225 wxSize wxCaptionBar::DoGetBestSize() const
226 {
227 int x,y;
228
229 if(IsVertical())
230 GetTextExtent(m_caption, &x, &y);
231 else
232 GetTextExtent(m_caption, &y, &x);
233
234 if(x < m_iconWidth)
235 x = m_iconWidth;
236
237 if(y < m_iconHeight)
238 y = m_iconHeight;
239
240 // TODO: The extra wxFPB_EXTRA_X constants should be adjustable as well
241
242 return wxSize(x + wxFPB_EXTRA_X, y + wxFPB_EXTRA_Y);
243 }
244
245
246 void wxCaptionBar::DrawVerticalGradient(wxDC &dc, const wxRect &rect )
247 {
248 // gradient fill from colour 1 to colour 2 with top to bottom
249
250 if(rect.height < 1 || rect.width < 1)
251 return;
252
253 int size = rect.height;
254
255 dc.SetPen(*wxTRANSPARENT_PEN);
256
257
258 // calculate gradient coefficients
259 wxColour col2 = m_captionStyle.GetSecondColour(),
260 col1 = m_captionStyle.GetFirstColour();
261
262 double rstep = double((col2.Red() - col1.Red())) / double(size), rf = 0,
263 gstep = double((col2.Green() - col1.Green())) / double(size), gf = 0,
264 bstep = double((col2.Blue() - col1.Blue())) / double(size), bf = 0;
265
266 wxColour currCol;
267 for(int y = rect.y; y < rect.y + size; y++)
268 {
269 currCol.Set(
270 (unsigned char)(col1.Red() + rf),
271 (unsigned char)(col1.Green() + gf),
272 (unsigned char)(col1.Blue() + bf)
273 );
274 dc.SetBrush( wxBrush( currCol, wxSOLID ) );
275 dc.DrawRectangle( rect.x, rect.y + (y - rect.y), rect.width, size );
276 //currCol.Set(currCol.Red() + rstep, currCol.Green() + gstep, currCol.Blue() + bstep);
277 rf += rstep; gf += gstep; bf += bstep;
278 }
279 }
280
281 void wxCaptionBar::DrawHorizontalGradient(wxDC &dc, const wxRect &rect )
282 {
283 // gradient fill from colour 1 to colour 2 with left to right
284
285 if(rect.height < 1 || rect.width < 1)
286 return;
287
288 int size = rect.width;
289
290 dc.SetPen(*wxTRANSPARENT_PEN);
291
292 // calculate gradient coefficients
293 wxColour col2 = m_captionStyle.GetSecondColour(),
294 col1 = m_captionStyle.GetFirstColour();
295
296 double rstep = double((col2.Red() - col1.Red())) / double(size), rf = 0,
297 gstep = double((col2.Green() - col1.Green())) / double(size), gf = 0,
298 bstep = double((col2.Blue() - col1.Blue())) / double(size), bf = 0;
299
300 wxColour currCol;
301 for(int x = rect.x; x < rect.x + size; x++)
302 {
303 currCol.Set(
304 (unsigned char)(col1.Red() + rf),
305 (unsigned char)(col1.Green() + gf),
306 (unsigned char)(col1.Blue() + bf)
307 );
308 dc.SetBrush( wxBrush( currCol, wxSOLID ) );
309 dc.DrawRectangle( rect.x + (x - rect.x), rect.y, 1, rect.height );
310 rf += rstep; gf += gstep; bf += bstep;
311 }
312 }
313
314 void wxCaptionBar::DrawSingleColour(wxDC &dc, const wxRect &rect )
315 {
316 // single colour fill. This is the most easy one to find
317
318 if(rect.height < 1 || rect.width < 1)
319 return;
320
321 dc.SetPen(*wxTRANSPARENT_PEN);
322
323 // draw simple rectangle
324 dc.SetBrush( wxBrush( m_captionStyle.GetFirstColour(), wxSOLID ) );
325 dc.DrawRectangle( rect.x, rect.y, rect.width, rect.height );
326 }
327
328 void wxCaptionBar::DrawSingleRectangle(wxDC &dc, const wxRect &rect )
329 {
330 wxASSERT(GetParent());
331
332 // single colour fill. This is the most easy one to find
333
334 if(rect.height < 2 || rect.width < 1)
335 return;
336
337 // single frame, set up internal fill colour
338
339 wxBrush br;
340 br.SetStyle(wxSOLID);
341
342 if(m_captionStyle.GetCaptionStyle() == wxCAPTIONBAR_RECTANGLE)
343 br.SetColour(GetParent()->GetBackgroundColour());
344 else
345 br.SetColour(m_captionStyle.GetFirstColour());
346
347 // setup the pen frame
348
349 wxPen pen(m_captionStyle.GetSecondColour());
350 dc.SetPen(pen);
351
352 dc.SetBrush( br );
353 dc.DrawRectangle( rect.x, rect.y, rect.width, rect.height - 1);
354
355 wxPen bgpen(GetParent()->GetBackgroundColour());
356 dc.SetPen(bgpen);
357 dc.DrawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width, rect.y + rect.height - 1);
358 }
359
360
361 void wxCaptionBar::OnSize(wxSizeEvent &event)
362 {
363 wxSize size = event.GetSize();
364
365 if(m_foldIcons)
366 {
367 // What I am doing here is simply invalidating the part of the window exposed. So when I
368 // make a rect with as width the newly exposed part, and the x,y of the old window size origin,
369 // I don't need a bitmap calulation in it, or do I ? The bitmap needs redrawing anyway. Leave it
370 // like this until I figured it out
371
372 // set rect to redraw as old bitmap area which is entitled to redraw
373
374 wxRect rect(size.GetWidth() - m_iconWidth - m_rightIndent, 0, m_iconWidth + m_rightIndent,
375 m_iconWidth + m_rightIndent);
376
377 // adjust rectangle when more is slided so we need to redraw all
378 // the old stuff but not all (ugly flickering)
379
380 int diffX = size.GetWidth() - m_oldSize.GetWidth();
381 if(diffX > 1)
382 {
383 // adjust the rect with all the crap to redraw
384
385 rect.SetWidth(rect.GetWidth() + diffX + 10);
386 rect.SetX(rect.GetX() - diffX - 10);
387 }
388
389 RefreshRect(rect);
390 }
391 else
392 {
393 wxRect rect = GetRect();
394 RefreshRect(rect);
395 }
396
397 m_oldSize = size;
398 }
399
400 void wxCaptionBar::RedrawIconBitmap()
401 {
402 if(m_foldIcons)
403 {
404 // invalidate the bitmap area and force a redraw
405
406 wxRect rect = GetRect();
407
408 rect.SetX(rect.GetWidth() - m_iconWidth - m_rightIndent);
409 rect.SetWidth(m_iconWidth + m_rightIndent);
410 RefreshRect(rect);
411 }
412 }
413
414 bool wxCaptionBar::IsVertical() const
415 {
416 // parent of wxCaptionBar is wxFoldPanelItem
417 // default is vertical
418 wxFoldPanelItem *bar = wxDynamicCast(GetParent(), wxFoldPanelItem);
419 wxCHECK_MSG( bar, true, _T("wrong parent") );
420 return bar->IsVertical();
421 }
422
423 /*
424 * wxCaptionBarEvent
425 */
426
427 DEFINE_EVENT_TYPE(wxEVT_CAPTIONBAR)
428
429 wxCaptionBarEvent::wxCaptionBarEvent(const wxCaptionBarEvent &event)
430 : wxCommandEvent(event)
431 {
432 m_captionBar = event.m_captionBar;
433 }
434
435 //DEFINE_EVENT_TYPE(wxEVT_CAPTIONBAR)
436 //IMPLEMENT_DYNAMIC_CLASS(wxCaptionBarEvent, wxEvent)
437 IMPLEMENT_DYNAMIC_CLASS(wxCaptionBarEvent, wxCommandEvent)