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