]> git.saurik.com Git - wxWidgets.git/blob - contrib/include/wx/foldbar/captionbar.h
added foldbar contrib
[wxWidgets.git] / contrib / include / wx / foldbar / captionbar.h
1 #ifndef __FOLDPANELBAR_H__
2 #define __FOLDPANELBAR_H__
3
4 #define wxFPB_EXTRA_X 10
5 #define wxFPB_EXTRA_Y 4
6 #define wxFPB_BMP_RIGHTSPACE 2 // pixels of the bmp to be alligned from the right filled with space
7
8 enum
9 {
10 /** Specifies the bars as gradient vertical filled caption bars going from top to bottom. The gradient
11 starts with first colour, and ends with second colour */
12 wxCAPTIONBAR_GRADIENT_V = 1,
13 /** Specifies the gradient going from left to right. The gradient starts with first colour, and
14 ends with second colour on the right */
15 wxCAPTIONBAR_GRADIENT_H,
16 /** Fills the captionbar with a single colour. The first colour is used for this fill */
17 wxCAPTIONBAR_SINGLE,
18 /** Draws a rectangle only using the second colour. The first colour is not used*/
19 wxCAPTIONBAR_RECTANGLE,
20 /** Fills the captionbar with a single colour (first colour) and draws a rectangle around it
21 using the second colour. */
22 wxCAPTIONBAR_FILLED_RECTANGLE
23 };
24
25 /** \class wxCaptionBarStyle
26 This class encapsulates the styles you wish to set for the wxCaptionBar (this is the part of the wxFoldPanel
27 where the caption is displayed). It can either be applied at creation time be reapplied when styles need to
28 be changed.
29
30 At construction time, all styles are set to their default transparency. This means none of the styles will be
31 applied to the wxCaptionBar in question, meaning it will be created using the default internals. When setting i.e
32 the color, font or panel style, these styles become active to be used.
33 */
34 class wxCaptionBarStyle
35 {
36 private:
37 // boolean flags for default transparency on styles
38 bool _firstColourUsed,
39 _secondColourUsed,
40 _textColourUsed,
41 _captionFontUsed,
42 _captionStyleUsed;
43
44 wxFont _captionFont;
45 wxColour _firstColour, _secondColour, _textColour;
46
47 int _captionStyle;
48
49 public:
50 /** Default constructor for this class */
51 wxCaptionBarStyle() {
52 ResetDefaults();
53 };
54
55 ~wxCaptionBarStyle() {
56
57 };
58
59 void ResetDefaults() {
60 _firstColourUsed = false;
61 _secondColourUsed = false;
62 _textColourUsed = false;
63 _captionFontUsed = false;
64 _captionStyleUsed = false;
65 _captionStyle = wxCAPTIONBAR_GRADIENT_V;
66 };
67
68 /** Copy operator. Only the styles in use in the source object are being copied to the destination object. All other
69 styles are not copied */
70 void operator=(const wxCaptionBarStyle &s) {
71 if(s._captionStyleUsed)
72 {
73 _captionStyleUsed = true;
74 _captionStyle = s._captionStyle;
75 }
76 if(s._captionFontUsed)
77 {
78 _captionFontUsed = true;
79 _captionFont = s._captionFont;
80 }
81 if(s._firstColourUsed)
82 {
83 _firstColourUsed = true;
84 _firstColour = s._firstColour;
85 }
86 if(s._secondColourUsed)
87 {
88 _secondColourUsed = true;
89 _secondColour = s._secondColour;
90 }
91 if(s._textColourUsed)
92 {
93 _textColourUsed = true;
94 _textColour = s._textColour;
95 }
96 };
97
98 // ------- CaptionBar Font -------
99
100 /** Set font for the caption bar. If this is not set, the font property is undefined
101 and will not be used. Use CaptionFontUsed() to check if this style is used */
102 void SetCaptionFont(const wxFont &font) {
103 _captionFont = font;
104 _captionFontUsed = true;
105 };
106
107 /** Checks if the caption bar font is set */
108 bool CaptionFontUsed() const {
109 return _captionFontUsed;
110 };
111
112 /** Returns the font for the caption bar. Please be warned this will result in an assertion failure when
113 this property is not previously set
114 \sa SetCaptionFont(), CaptionFontUsed() */
115 wxFont GetCaptionFont() const {
116 wxASSERT(_captionFontUsed);
117 return _captionFont;
118 };
119
120 // ------- FirstColour -------
121
122 /** Set first colour for the caption bar. If this is not set, the colour property is
123 undefined and will not be used. Use FirstColourUsed() to check if this
124 style is used */
125 void SetFirstColour(const wxColour &col) {
126 _firstColour = col;
127 _firstColourUsed = true;
128 };
129
130 /** Checks if the first colour of the caption bar is set */
131 bool FirstColourUsed() const {
132 return _firstColourUsed;
133 };
134
135 /** Returns the first colour for the caption bar. Please be warned this will
136 result in an assertion failure when this property is not previously set.
137 \sa SetCaptionFirstColour(), CaptionFirstColourUsed() */
138 wxColour GetFirstColour() const {
139 wxASSERT(_firstColourUsed);
140 return _firstColour;
141 };
142
143 // ------- SecondColour -------
144
145 /** Set second colour for the caption bar. If this is not set, the colour property is undefined and
146 will not be used. Use SecondColourUsed() to check if this style is used */
147 void SetSecondColour(const wxColour &col) {
148 _secondColour = col;
149 _secondColourUsed = true;
150 };
151
152 /** Checks if the second colour of the caption bar is set */
153 bool SecondColourUsed() const {
154 return _secondColourUsed;
155 };
156
157 /** Returns the second colour for the caption bar. Please be warned this will result in
158 an assertion failure when this property is not previously set.
159 \sa SetSecondColour(), SecondColourUsed() */
160 wxColour GetSecondColour() const {
161 wxASSERT(_secondColourUsed);
162 return _secondColour;
163 };
164
165 // ------- Caption Text Colour -------
166
167 /** Set caption colour for the caption bar. If this is not set, the colour property is
168 undefined and will not be used. Use CaptionColourUsed() to check if this style is used */
169 void SetCaptionColour(const wxColour &col) {
170 _textColour = col;
171 _textColourUsed = true;
172 };
173
174 /** Checks if the caption colour of the caption bar is set */
175 bool CaptionColourUsed() const {
176 return _textColourUsed;
177 };
178
179 /** Returns the caption colour for the caption bar. Please be warned this will
180 result in an assertion failure when this property is not previously set.
181 \sa SetCaptionColour(), CaptionColourUsed() */
182 wxColour GetCaptionColour() const {
183 wxASSERT(_textColourUsed);
184 return _textColour;
185 };
186
187 // ------- CaptionStyle -------
188
189 /** Set caption style for the caption bar. If this is not set, the property is
190 undefined and will not be used. Use CaptionStyleUsed() to check if this style is used.
191 The following styles can be applied:
192 - wxCAPTIONBAR_GRADIENT_V: Draws a vertical gradient from top to bottom
193 - wxCAPTIONBAR_GRADIENT_H: Draws a horizontal gradient from left to right
194 - wxCAPTIONBAR_SINGLE: Draws a single filled rectangle to draw the caption
195 - wxCAPTIONBAR_RECTANGLE: Draws a single colour with a rectangle around the caption
196 - wxCAPTIONBAR_FILLED_RECTANGLE: Draws a filled rectangle and a border around it
197 */
198 void SetCaptionStyle(int style) {
199 _captionStyle = style;
200 _captionStyleUsed = true;
201 };
202
203 /** Checks if the caption style of the caption bar is set */
204 bool CaptionStyleUsed() const {
205 return _captionStyleUsed;
206 };
207
208 /** Returns the caption style for the caption bar. Please be warned this will
209 result in an assertion failure when this property is not previously set.
210 \sa SetCaptionStyle(), CaptionStyleUsed() */
211 int GetCaptionStyle() const {
212 wxASSERT(_captionStyleUsed);
213 return _captionStyle;
214 };
215 };
216
217 #ifndef _NO_CAPTIONBAR_
218
219 /** \class wxCaptionBar
220 This class is a graphical caption component that consists of a caption and a clickable arrow.
221
222 The wxCaptionBar fires an event EVT_CAPTIONBAR which is a wxCaptionBarEvent. This event can be caught
223 and the parent window can act upon the collapsed or expanded state of the bar (which is actually just
224 the icon which changed). The parent panel can reduce size or expand again.
225 */
226
227 #include <wx/imaglist.h>
228
229 /** Defines an empty captionbar style */
230 #define wxEmptyCaptionBarStyle wxCaptionBarStyle()
231
232 class wxCaptionBar: public wxWindow
233 {
234 private:
235 wxString _caption;
236 wxImageList *_foldIcons;
237 wxSize _oldSize;
238 //wxFont _captionFont;
239 int _rightIndent;
240 int _iconWidth, _iconHeight;
241 //int _captionStyle;
242
243 //wxColour _firstColour, _secondColour, _textColour;
244
245 /** True when the caption is in collapsed state (means at the bottom of the wxFoldPanel */
246 bool _collapsed;
247
248 wxCaptionBarStyle _style;
249
250 /** Fills the background of the caption with either a gradient, or a solid color */
251 void FillCaptionBackground(wxPaintDC &dc);
252
253 /* Draw methods */
254 void DrawHorizontalGradient(wxDC &dc, const wxRect &rect );
255 void DrawVerticalGradient(wxDC &dc, const wxRect &rect );
256 void DrawSingleColour(wxDC &dc, const wxRect &rect );
257 void DrawSingleRectangle(wxDC &dc, const wxRect &rect );
258
259 void RedrawIconBitmap();
260
261 void ApplyCaptionStyle(const wxCaptionBarStyle &cbstyle, bool applyDefault);
262
263 public:
264 /** Constructor of wxCaptionBar. To create a wxCaptionBar with the arrow images, simply pass an image list
265 which contains at least two bitmaps. The bitmaps contain the expanded and collapsed icons needed to
266 represent it's state. If you don't want images, simply pass a null pointer and the bitmap is disabled. */
267 wxCaptionBar(wxWindow* parent, const wxString &caption, wxImageList *images,
268 wxWindowID id = -1, const wxCaptionBarStyle &cbstyle = wxEmptyCaptionBarStyle,
269 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxNO_BORDER);
270
271 ~wxCaptionBar();
272
273 /** Set wxCaptionBar styles with wxCapionBarSyle class. All styles that are actually set, are applied. If you
274 set applyDefault to true, all other (not defined) styles will be set to default. If it is false,
275 the styles which are not set in the wxCaptionBarStyle will be ignored */
276 void SetCaptionStyle(bool applyDefault, wxCaptionBarStyle style = wxEmptyCaptionBarStyle) {
277 ApplyCaptionStyle(style, applyDefault);
278 Refresh();
279 };
280
281 /** Returns the current style of the captionbar in a wxCaptionBarStyle class. This can be used to change and set back the
282 changes. */
283 wxCaptionBarStyle GetCaptionStyle() {
284 return _style;
285 };
286
287 #if 0
288 /** Sets a pointer to an image list resource (a non owned pointer) to the collapsed and expand icon bitmap.
289 The reason why it will be assigned a pointer is that it is very likely that multiple caption bars will
290 be used and if they all have their own bitmap resources it will eat up more memory then needed. It will
291 also ease the use of shared icon change, when there is any need to.
292
293 If no wxImageList is assigned, there will be no fold icons and only the doubleclick on the panel
294 will work to collapse / expand.
295
296 The image list must contain 2 bitmaps. Index 0 will be the expanded state, and index 1 will be the
297 collapsed state of the bitmap. The size of the bitmap is taken in account when the minimal height and
298 widht is calculated.
299
300 The bitmaps must be the second thing to be done before using it (SetRightIndent should be the first thing),
301 make sure if the icons are larger than the font, that the parent of this window gets a Fit call to resize
302 all the windows accordingly */
303
304 void SetFoldIcons(wxImageList *images) {
305 _foldIcons = images;
306 _iconWidth = _iconHeight = 0;
307 if(_foldIcons)
308 _foldIcons->GetSize(0, _iconWidth, _iconHeight);
309
310 Refresh();
311 };
312
313 #endif
314
315 /** Returns wether the status of the bar is expanded or collapsed */
316 bool IsCollapsed() const {
317 return _collapsed;
318 };
319
320 /** Sets the amount of pixels on the right from which the bitmap is trailing. If this is 0, it will be
321 drawn all the way to the right, default is equal to wxFPB_BMP_RIGHTSPACE. Assign this before
322 assigning an image list to prevent a redraw */
323
324 void SetRightIndent(int pixels) {
325 wxCHECK2(pixels >= 0, return);
326 _rightIndent = pixels;
327 // issue a refresh (if we have a bmp)
328 if(_foldIcons)
329 Refresh();
330 };
331
332
333 /** Return the best size for this panel, based upon the font assigned to this window, and the
334 caption string */
335 wxSize DoGetBestSize() const;
336
337 /** This sets the internal state / representation to collapsed. This does not trigger a wxCaptionBarEvent
338 to be sent to the parent */
339 void Collapse() {
340 _collapsed = true;
341 RedrawIconBitmap();
342 };
343
344 /** This sets the internal state / representation to expanded. This does not trigger a wxCaptionBarEvent
345 to be sent to the parent */
346 void Expand() {
347 _collapsed = false;
348 RedrawIconBitmap();
349 };
350
351 void SetBoldFont() {
352 GetFont().SetWeight(wxBOLD);
353 };
354
355 void SetNormalFont() {
356 GetFont().SetWeight(wxNORMAL);
357 };
358
359
360 private:
361 /** The paint event for flat or gradient fill */
362 void OnPaint(wxPaintEvent& event);
363
364 /** For clicking the icon, the mouse event must be intercepted */
365 void OnMouseEvent(wxMouseEvent& event);
366
367 /** Maybe when focus (don't know how yet) a cursor left or backspace will collapse or expand */
368 void OnChar(wxKeyEvent& event);
369
370 void OnSize(wxSizeEvent &event);
371
372
373 protected:
374 DECLARE_NO_COPY_CLASS(wxCaptionBar)
375 DECLARE_EVENT_TABLE()
376 };
377
378 /***********************************************************************************************************/
379
380 /** \class wxCaptionBarEvent
381 This event will be sent when a EVT_CAPTIONBAR is mapped in the parent. It is to notify the parent
382 that the bar is now in collapsed or expanded state. The parent should re-arrange the associated
383 windows accordingly */
384
385 class WXDLLEXPORT wxCaptionBarEvent : public wxCommandEvent
386 {
387
388 private:
389 bool _collapsed;
390 wxCaptionBar *_bar;
391 void *_tag;
392
393 public:
394 wxCaptionBarEvent(wxEventType commandType = wxEVT_NULL, int id = 0)
395 : wxCommandEvent(commandType, id)
396 , _collapsed(false)
397 , _bar(0)
398 , _tag(0)
399 { }
400
401 /** Constructor for clone copy */
402 wxCaptionBarEvent(const wxCaptionBarEvent &event);
403
404 /** Clone function */
405 virtual wxEvent *Clone() const {
406 return new wxCaptionBarEvent(*this);
407 };
408
409 /** Returns wether the bar is expanded or collapsed. True means expanded */
410 bool GetFoldStatus() const {
411 wxCHECK(_bar, false);
412 return !_bar->IsCollapsed();
413 };
414
415 /** Returns the bar associated with this event */
416 wxCaptionBar *GetBar() const {
417 return _bar;
418 };
419
420 void SetTag(void *tag) {
421 _tag = tag;
422 };
423
424 void *GetTag() const {
425 return _tag;
426 };
427
428 /** Sets the bar associated with this event, should not used
429 by any other then the originator of the event */
430 void SetBar(wxCaptionBar *bar) {
431 _bar = bar;
432 };
433
434 DECLARE_DYNAMIC_CLASS(wxCaptionBarEvent)
435
436 };
437
438 BEGIN_DECLARE_EVENT_TYPES()
439 DECLARE_EVENT_TYPE(wxEVT_CAPTIONBAR, 7777)
440 END_DECLARE_EVENT_TYPES()
441
442 typedef void (wxEvtHandler::*wxCaptionBarEventFunction)(wxCaptionBarEvent&);
443
444 #define EVT_CAPTIONBAR(id, fn) \
445 DECLARE_EVENT_TABLE_ENTRY( \
446 wxEVT_CAPTIONBAR, id, -1, \
447 (wxObjectEventFunction)(wxEventFunction)(wxCaptionBarEventFunction) \
448 & fn, \
449 (wxObject *) NULL \
450 ),
451
452 #endif // _NO_CAPTIONBAR_
453
454 #endif