Generate right click events for all kinds of items in wxAuiToolBar.
[wxWidgets.git] / src / aui / auibar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2
3 // Name: src/aui/auibar.cpp
4 // Purpose: wxaui: wx advanced user interface - docking window manager
5 // Author: Benjamin I. Williams
6 // Modified by:
7 // Created: 2005-05-17
8 // RCS-ID: $Id$
9 // Copyright: (C) Copyright 2005-2006, Kirix Corporation, All Rights Reserved
10 // Licence: wxWindows Library Licence, Version 3.1
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_AUI
28
29 #include "wx/statline.h"
30 #include "wx/dcbuffer.h"
31 #include "wx/sizer.h"
32 #include "wx/image.h"
33 #include "wx/settings.h"
34 #include "wx/menu.h"
35
36 #include "wx/aui/auibar.h"
37 #include "wx/aui/framemanager.h"
38
39 #ifdef __WXMAC__
40 #include "wx/osx/private.h"
41 #endif
42
43 #include "wx/arrimpl.cpp"
44 WX_DEFINE_OBJARRAY(wxAuiToolBarItemArray)
45
46
47 wxDEFINE_EVENT( wxEVT_COMMAND_AUITOOLBAR_TOOL_DROPDOWN, wxAuiToolBarEvent );
48 wxDEFINE_EVENT( wxEVT_COMMAND_AUITOOLBAR_OVERFLOW_CLICK, wxAuiToolBarEvent );
49 wxDEFINE_EVENT( wxEVT_COMMAND_AUITOOLBAR_RIGHT_CLICK, wxAuiToolBarEvent );
50 wxDEFINE_EVENT( wxEVT_COMMAND_AUITOOLBAR_MIDDLE_CLICK, wxAuiToolBarEvent );
51 wxDEFINE_EVENT( wxEVT_COMMAND_AUITOOLBAR_BEGIN_DRAG, wxAuiToolBarEvent );
52
53
54 IMPLEMENT_CLASS(wxAuiToolBar, wxControl)
55 IMPLEMENT_DYNAMIC_CLASS(wxAuiToolBarEvent, wxEvent)
56
57
58 // missing wxITEM_* items
59 enum
60 {
61 wxITEM_CONTROL = wxITEM_MAX,
62 wxITEM_LABEL,
63 wxITEM_SPACER
64 };
65
66 const int BUTTON_DROPDOWN_WIDTH = 10;
67
68
69 wxBitmap wxAuiBitmapFromBits(const unsigned char bits[], int w, int h,
70 const wxColour& color);
71
72 static wxColor GetBaseColor()
73 {
74
75 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
76 wxColor baseColour = wxColour( wxMacCreateCGColorFromHITheme(kThemeBrushToolbarBackground));
77 #else
78 wxColor baseColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
79 #endif
80
81 // the baseColour is too pale to use as our base colour,
82 // so darken it a bit --
83 if ((255-baseColour.Red()) +
84 (255-baseColour.Green()) +
85 (255-baseColour.Blue()) < 60)
86 {
87 baseColour = baseColour.ChangeLightness(92);
88 }
89
90 return baseColour;
91 }
92
93
94
95 class ToolbarCommandCapture : public wxEvtHandler
96 {
97 public:
98
99 ToolbarCommandCapture() { m_lastId = 0; }
100 int GetCommandId() const { return m_lastId; }
101
102 bool ProcessEvent(wxEvent& evt)
103 {
104 if (evt.GetEventType() == wxEVT_COMMAND_MENU_SELECTED)
105 {
106 m_lastId = evt.GetId();
107 return true;
108 }
109
110 if (GetNextHandler())
111 return GetNextHandler()->ProcessEvent(evt);
112
113 return false;
114 }
115
116 private:
117 int m_lastId;
118 };
119
120
121
122 static const unsigned char
123 DISABLED_TEXT_GREY_HUE = wxColour::AlphaBlend(0, 255, 0.4);
124 const wxColour DISABLED_TEXT_COLOR(DISABLED_TEXT_GREY_HUE,
125 DISABLED_TEXT_GREY_HUE,
126 DISABLED_TEXT_GREY_HUE);
127
128 wxAuiDefaultToolBarArt::wxAuiDefaultToolBarArt()
129 {
130 m_baseColour = GetBaseColor();
131
132 m_flags = 0;
133 m_textOrientation = wxAUI_TBTOOL_TEXT_BOTTOM;
134 m_highlightColour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
135
136 m_separatorSize = 7;
137 m_gripperSize = 7;
138 m_overflowSize = 16;
139
140 wxColor darker1Colour = m_baseColour.ChangeLightness(85);
141 wxColor darker2Colour = m_baseColour.ChangeLightness(75);
142 wxColor darker3Colour = m_baseColour.ChangeLightness(60);
143 wxColor darker4Colour = m_baseColour.ChangeLightness(50);
144 wxColor darker5Colour = m_baseColour.ChangeLightness(40);
145
146 m_gripperPen1 = wxPen(darker5Colour);
147 m_gripperPen2 = wxPen(darker3Colour);
148 m_gripperPen3 = *wxWHITE_PEN;
149
150 static const unsigned char buttonDropdownBits[] = { 0xe0, 0xf1, 0xfb };
151 static const unsigned char overflowBits[] = { 0x80, 0xff, 0x80, 0xc1, 0xe3, 0xf7 };
152
153 m_buttonDropDownBmp = wxAuiBitmapFromBits(buttonDropdownBits, 5, 3,
154 *wxBLACK);
155 m_disabledButtonDropDownBmp = wxAuiBitmapFromBits(
156 buttonDropdownBits, 5, 3,
157 wxColor(128,128,128));
158 m_overflowBmp = wxAuiBitmapFromBits(overflowBits, 7, 6, *wxBLACK);
159 m_disabledOverflowBmp = wxAuiBitmapFromBits(overflowBits, 7, 6, wxColor(128,128,128));
160
161 m_font = *wxNORMAL_FONT;
162 }
163
164 wxAuiDefaultToolBarArt::~wxAuiDefaultToolBarArt()
165 {
166 m_font = *wxNORMAL_FONT;
167 }
168
169
170 wxAuiToolBarArt* wxAuiDefaultToolBarArt::Clone()
171 {
172 return static_cast<wxAuiToolBarArt*>(new wxAuiDefaultToolBarArt);
173 }
174
175 void wxAuiDefaultToolBarArt::SetFlags(unsigned int flags)
176 {
177 m_flags = flags;
178 }
179
180 void wxAuiDefaultToolBarArt::SetFont(const wxFont& font)
181 {
182 m_font = font;
183 }
184
185 void wxAuiDefaultToolBarArt::SetTextOrientation(int orientation)
186 {
187 m_textOrientation = orientation;
188 }
189
190 unsigned int wxAuiDefaultToolBarArt::GetFlags()
191 {
192 return m_flags;
193 }
194
195 wxFont wxAuiDefaultToolBarArt::GetFont()
196 {
197 return m_font;
198 }
199
200 int wxAuiDefaultToolBarArt::GetTextOrientation()
201 {
202 return m_textOrientation;
203 }
204
205 void wxAuiDefaultToolBarArt::DrawBackground(
206 wxDC& dc,
207 wxWindow* WXUNUSED(wnd),
208 const wxRect& _rect)
209 {
210 wxRect rect = _rect;
211 rect.height++;
212 wxColour startColour = m_baseColour.ChangeLightness(150);
213 wxColour endColour = m_baseColour.ChangeLightness(90);
214 dc.GradientFillLinear(rect, startColour, endColour, wxSOUTH);
215 }
216
217 void wxAuiDefaultToolBarArt::DrawLabel(
218 wxDC& dc,
219 wxWindow* WXUNUSED(wnd),
220 const wxAuiToolBarItem& item,
221 const wxRect& rect)
222 {
223 dc.SetFont(m_font);
224 dc.SetTextForeground(*wxBLACK);
225
226 // we only care about the text height here since the text
227 // will get cropped based on the width of the item
228 int textWidth = 0, textHeight = 0;
229 dc.GetTextExtent(wxT("ABCDHgj"), &textWidth, &textHeight);
230
231 // set the clipping region
232 wxRect clipRect = rect;
233 clipRect.width -= 1;
234 dc.SetClippingRegion(clipRect);
235
236 int textX, textY;
237 textX = rect.x + 1;
238 textY = rect.y + (rect.height-textHeight)/2;
239 dc.DrawText(item.GetLabel(), textX, textY);
240 dc.DestroyClippingRegion();
241 }
242
243
244 void wxAuiDefaultToolBarArt::DrawButton(
245 wxDC& dc,
246 wxWindow* WXUNUSED(wnd),
247 const wxAuiToolBarItem& item,
248 const wxRect& rect)
249 {
250 int textWidth = 0, textHeight = 0;
251
252 if (m_flags & wxAUI_TB_TEXT)
253 {
254 dc.SetFont(m_font);
255
256 int tx, ty;
257
258 dc.GetTextExtent(wxT("ABCDHgj"), &tx, &textHeight);
259 textWidth = 0;
260 dc.GetTextExtent(item.GetLabel(), &textWidth, &ty);
261 }
262
263 int bmpX = 0, bmpY = 0;
264 int textX = 0, textY = 0;
265
266 if (m_textOrientation == wxAUI_TBTOOL_TEXT_BOTTOM)
267 {
268 bmpX = rect.x +
269 (rect.width/2) -
270 (item.GetBitmap().GetWidth()/2);
271
272 bmpY = rect.y +
273 ((rect.height-textHeight)/2) -
274 (item.GetBitmap().GetHeight()/2);
275
276 textX = rect.x + (rect.width/2) - (textWidth/2) + 1;
277 textY = rect.y + rect.height - textHeight - 1;
278 }
279 else if (m_textOrientation == wxAUI_TBTOOL_TEXT_RIGHT)
280 {
281 bmpX = rect.x + 3;
282
283 bmpY = rect.y +
284 (rect.height/2) -
285 (item.GetBitmap().GetHeight()/2);
286
287 textX = bmpX + 3 + item.GetBitmap().GetWidth();
288 textY = rect.y +
289 (rect.height/2) -
290 (textHeight/2);
291 }
292
293
294 if (!(item.GetState() & wxAUI_BUTTON_STATE_DISABLED))
295 {
296 if (item.GetState() & wxAUI_BUTTON_STATE_PRESSED)
297 {
298 dc.SetPen(wxPen(m_highlightColour));
299 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(150)));
300 dc.DrawRectangle(rect);
301 }
302 else if ((item.GetState() & wxAUI_BUTTON_STATE_HOVER) || item.IsSticky())
303 {
304 dc.SetPen(wxPen(m_highlightColour));
305 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(170)));
306
307 // draw an even lighter background for checked item hovers (since
308 // the hover background is the same color as the check background)
309 if (item.GetState() & wxAUI_BUTTON_STATE_CHECKED)
310 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(180)));
311
312 dc.DrawRectangle(rect);
313 }
314 else if (item.GetState() & wxAUI_BUTTON_STATE_CHECKED)
315 {
316 // it's important to put this code in an else statment after the
317 // hover, otherwise hovers won't draw properly for checked items
318 dc.SetPen(wxPen(m_highlightColour));
319 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(170)));
320 dc.DrawRectangle(rect);
321 }
322 }
323
324 wxBitmap bmp;
325 if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED)
326 bmp = item.GetDisabledBitmap();
327 else
328 bmp = item.GetBitmap();
329
330 if ( bmp.IsOk() )
331 dc.DrawBitmap(bmp, bmpX, bmpY, true);
332
333 // set the item's text color based on if it is disabled
334 dc.SetTextForeground(*wxBLACK);
335 if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED)
336 dc.SetTextForeground(DISABLED_TEXT_COLOR);
337
338 if ( (m_flags & wxAUI_TB_TEXT) && !item.GetLabel().empty() )
339 {
340 dc.DrawText(item.GetLabel(), textX, textY);
341 }
342 }
343
344
345 void wxAuiDefaultToolBarArt::DrawDropDownButton(
346 wxDC& dc,
347 wxWindow* WXUNUSED(wnd),
348 const wxAuiToolBarItem& item,
349 const wxRect& rect)
350 {
351 int textWidth = 0, textHeight = 0, textX = 0, textY = 0;
352 int bmpX = 0, bmpY = 0, dropBmpX = 0, dropBmpY = 0;
353
354 wxRect buttonRect = wxRect(rect.x,
355 rect.y,
356 rect.width-BUTTON_DROPDOWN_WIDTH,
357 rect.height);
358 wxRect dropDownRect = wxRect(rect.x+rect.width-BUTTON_DROPDOWN_WIDTH-1,
359 rect.y,
360 BUTTON_DROPDOWN_WIDTH+1,
361 rect.height);
362
363 if (m_flags & wxAUI_TB_TEXT)
364 {
365 dc.SetFont(m_font);
366
367 int tx, ty;
368 if (m_flags & wxAUI_TB_TEXT)
369 {
370 dc.GetTextExtent(wxT("ABCDHgj"), &tx, &textHeight);
371 textWidth = 0;
372 }
373
374 dc.GetTextExtent(item.GetLabel(), &textWidth, &ty);
375 }
376
377
378
379 dropBmpX = dropDownRect.x +
380 (dropDownRect.width/2) -
381 (m_buttonDropDownBmp.GetWidth()/2);
382 dropBmpY = dropDownRect.y +
383 (dropDownRect.height/2) -
384 (m_buttonDropDownBmp.GetHeight()/2);
385
386
387 if (m_textOrientation == wxAUI_TBTOOL_TEXT_BOTTOM)
388 {
389 bmpX = buttonRect.x +
390 (buttonRect.width/2) -
391 (item.GetBitmap().GetWidth()/2);
392 bmpY = buttonRect.y +
393 ((buttonRect.height-textHeight)/2) -
394 (item.GetBitmap().GetHeight()/2);
395
396 textX = rect.x + (rect.width/2) - (textWidth/2) + 1;
397 textY = rect.y + rect.height - textHeight - 1;
398 }
399 else if (m_textOrientation == wxAUI_TBTOOL_TEXT_RIGHT)
400 {
401 bmpX = rect.x + 3;
402
403 bmpY = rect.y +
404 (rect.height/2) -
405 (item.GetBitmap().GetHeight()/2);
406
407 textX = bmpX + 3 + item.GetBitmap().GetWidth();
408 textY = rect.y +
409 (rect.height/2) -
410 (textHeight/2);
411 }
412
413
414 if (item.GetState() & wxAUI_BUTTON_STATE_PRESSED)
415 {
416 dc.SetPen(wxPen(m_highlightColour));
417 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(140)));
418 dc.DrawRectangle(buttonRect);
419
420 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(170)));
421 dc.DrawRectangle(dropDownRect);
422 }
423 else if (item.GetState() & wxAUI_BUTTON_STATE_HOVER ||
424 item.IsSticky())
425 {
426 dc.SetPen(wxPen(m_highlightColour));
427 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(170)));
428 dc.DrawRectangle(buttonRect);
429 dc.DrawRectangle(dropDownRect);
430 }
431 else if (item.GetState() & wxAUI_BUTTON_STATE_CHECKED)
432 {
433 // Notice that this branch must come after the hover one to ensure the
434 // correct appearance when the mouse hovers over a checked item.m_
435 dc.SetPen(wxPen(m_highlightColour));
436 dc.SetBrush(wxBrush(m_highlightColour.ChangeLightness(170)));
437 dc.DrawRectangle(buttonRect);
438 dc.DrawRectangle(dropDownRect);
439 }
440
441 wxBitmap bmp;
442 wxBitmap dropbmp;
443 if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED)
444 {
445 bmp = item.GetDisabledBitmap();
446 dropbmp = m_disabledButtonDropDownBmp;
447 }
448 else
449 {
450 bmp = item.GetBitmap();
451 dropbmp = m_buttonDropDownBmp;
452 }
453
454 if (!bmp.IsOk())
455 return;
456
457 dc.DrawBitmap(bmp, bmpX, bmpY, true);
458 dc.DrawBitmap(dropbmp, dropBmpX, dropBmpY, true);
459
460 // set the item's text color based on if it is disabled
461 dc.SetTextForeground(*wxBLACK);
462 if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED)
463 dc.SetTextForeground(DISABLED_TEXT_COLOR);
464
465 if ( (m_flags & wxAUI_TB_TEXT) && !item.GetLabel().empty() )
466 {
467 dc.DrawText(item.GetLabel(), textX, textY);
468 }
469 }
470
471 void wxAuiDefaultToolBarArt::DrawControlLabel(
472 wxDC& dc,
473 wxWindow* WXUNUSED(wnd),
474 const wxAuiToolBarItem& item,
475 const wxRect& rect)
476 {
477 if (!(m_flags & wxAUI_TB_TEXT))
478 return;
479
480 if (m_textOrientation != wxAUI_TBTOOL_TEXT_BOTTOM)
481 return;
482
483 int textX = 0, textY = 0;
484 int textWidth = 0, textHeight = 0;
485
486 dc.SetFont(m_font);
487
488 int tx, ty;
489 if (m_flags & wxAUI_TB_TEXT)
490 {
491 dc.GetTextExtent(wxT("ABCDHgj"), &tx, &textHeight);
492 textWidth = 0;
493 }
494
495 dc.GetTextExtent(item.GetLabel(), &textWidth, &ty);
496
497 // don't draw the label if it is wider than the item width
498 if (textWidth > rect.width)
499 return;
500
501 // set the label's text color
502 dc.SetTextForeground(*wxBLACK);
503
504 textX = rect.x + (rect.width/2) - (textWidth/2) + 1;
505 textY = rect.y + rect.height - textHeight - 1;
506
507 if ( (m_flags & wxAUI_TB_TEXT) && !item.GetLabel().empty() )
508 {
509 dc.DrawText(item.GetLabel(), textX, textY);
510 }
511 }
512
513 wxSize wxAuiDefaultToolBarArt::GetLabelSize(
514 wxDC& dc,
515 wxWindow* WXUNUSED(wnd),
516 const wxAuiToolBarItem& item)
517 {
518 dc.SetFont(m_font);
519
520 // get label's height
521 int width = 0, height = 0;
522 dc.GetTextExtent(wxT("ABCDHgj"), &width, &height);
523
524 // get item's width
525 width = item.GetMinSize().GetWidth();
526
527 if (width == -1)
528 {
529 // no width specified, measure the text ourselves
530 width = dc.GetTextExtent(item.GetLabel()).GetX();
531 }
532
533 return wxSize(width, height);
534 }
535
536 wxSize wxAuiDefaultToolBarArt::GetToolSize(
537 wxDC& dc,
538 wxWindow* WXUNUSED(wnd),
539 const wxAuiToolBarItem& item)
540 {
541 if (!item.GetBitmap().IsOk() && !(m_flags & wxAUI_TB_TEXT))
542 return wxSize(16,16);
543
544 int width = item.GetBitmap().GetWidth();
545 int height = item.GetBitmap().GetHeight();
546
547 if (m_flags & wxAUI_TB_TEXT)
548 {
549 dc.SetFont(m_font);
550 int tx, ty;
551
552 if (m_textOrientation == wxAUI_TBTOOL_TEXT_BOTTOM)
553 {
554 dc.GetTextExtent(wxT("ABCDHgj"), &tx, &ty);
555 height += ty;
556
557 if ( !item.GetLabel().empty() )
558 {
559 dc.GetTextExtent(item.GetLabel(), &tx, &ty);
560 width = wxMax(width, tx+6);
561 }
562 }
563 else if ( m_textOrientation == wxAUI_TBTOOL_TEXT_RIGHT &&
564 !item.GetLabel().empty() )
565 {
566 width += 3; // space between left border and bitmap
567 width += 3; // space between bitmap and text
568
569 if ( !item.GetLabel().empty() )
570 {
571 dc.GetTextExtent(item.GetLabel(), &tx, &ty);
572 width += tx;
573 height = wxMax(height, ty);
574 }
575 }
576 }
577
578 // if the tool has a dropdown button, add it to the width
579 if (item.HasDropDown())
580 width += (BUTTON_DROPDOWN_WIDTH+4);
581
582 return wxSize(width, height);
583 }
584
585 void wxAuiDefaultToolBarArt::DrawSeparator(
586 wxDC& dc,
587 wxWindow* WXUNUSED(wnd),
588 const wxRect& _rect)
589 {
590 bool horizontal = true;
591 if (m_flags & wxAUI_TB_VERTICAL)
592 horizontal = false;
593
594 wxRect rect = _rect;
595
596 if (horizontal)
597 {
598 rect.x += (rect.width/2);
599 rect.width = 1;
600 int new_height = (rect.height*3)/4;
601 rect.y += (rect.height/2) - (new_height/2);
602 rect.height = new_height;
603 }
604 else
605 {
606 rect.y += (rect.height/2);
607 rect.height = 1;
608 int new_width = (rect.width*3)/4;
609 rect.x += (rect.width/2) - (new_width/2);
610 rect.width = new_width;
611 }
612
613 wxColour startColour = m_baseColour.ChangeLightness(80);
614 wxColour endColour = m_baseColour.ChangeLightness(80);
615 dc.GradientFillLinear(rect, startColour, endColour, horizontal ? wxSOUTH : wxEAST);
616 }
617
618 void wxAuiDefaultToolBarArt::DrawGripper(wxDC& dc,
619 wxWindow* WXUNUSED(wnd),
620 const wxRect& rect)
621 {
622 int i = 0;
623 while (1)
624 {
625 int x, y;
626
627 if (m_flags & wxAUI_TB_VERTICAL)
628 {
629 x = rect.x + (i*4) + 5;
630 y = rect.y + 3;
631 if (x > rect.GetWidth()-5)
632 break;
633 }
634 else
635 {
636 x = rect.x + 3;
637 y = rect.y + (i*4) + 5;
638 if (y > rect.GetHeight()-5)
639 break;
640 }
641
642 dc.SetPen(m_gripperPen1);
643 dc.DrawPoint(x, y);
644 dc.SetPen(m_gripperPen2);
645 dc.DrawPoint(x, y+1);
646 dc.DrawPoint(x+1, y);
647 dc.SetPen(m_gripperPen3);
648 dc.DrawPoint(x+2, y+1);
649 dc.DrawPoint(x+2, y+2);
650 dc.DrawPoint(x+1, y+2);
651
652 i++;
653 }
654
655 }
656
657 void wxAuiDefaultToolBarArt::DrawOverflowButton(wxDC& dc,
658 wxWindow* wnd,
659 const wxRect& rect,
660 int state)
661 {
662 if (state & wxAUI_BUTTON_STATE_HOVER ||
663 state & wxAUI_BUTTON_STATE_PRESSED)
664 {
665 wxRect cli_rect = wnd->GetClientRect();
666 wxColor light_gray_bg = m_highlightColour.ChangeLightness(170);
667
668 if (m_flags & wxAUI_TB_VERTICAL)
669 {
670 dc.SetPen(wxPen(m_highlightColour));
671 dc.DrawLine(rect.x, rect.y, rect.x+rect.width, rect.y);
672 dc.SetPen(wxPen(light_gray_bg));
673 dc.SetBrush(wxBrush(light_gray_bg));
674 dc.DrawRectangle(rect.x, rect.y+1, rect.width, rect.height);
675 }
676 else
677 {
678 dc.SetPen(wxPen(m_highlightColour));
679 dc.DrawLine(rect.x, rect.y, rect.x, rect.y+rect.height);
680 dc.SetPen(wxPen(light_gray_bg));
681 dc.SetBrush(wxBrush(light_gray_bg));
682 dc.DrawRectangle(rect.x+1, rect.y, rect.width, rect.height);
683 }
684 }
685
686 int x = rect.x+1+(rect.width-m_overflowBmp.GetWidth())/2;
687 int y = rect.y+1+(rect.height-m_overflowBmp.GetHeight())/2;
688 dc.DrawBitmap(m_overflowBmp, x, y, true);
689 }
690
691 int wxAuiDefaultToolBarArt::GetElementSize(int element_id)
692 {
693 switch (element_id)
694 {
695 case wxAUI_TBART_SEPARATOR_SIZE: return m_separatorSize;
696 case wxAUI_TBART_GRIPPER_SIZE: return m_gripperSize;
697 case wxAUI_TBART_OVERFLOW_SIZE: return m_overflowSize;
698 default: return 0;
699 }
700 }
701
702 void wxAuiDefaultToolBarArt::SetElementSize(int element_id, int size)
703 {
704 switch (element_id)
705 {
706 case wxAUI_TBART_SEPARATOR_SIZE: m_separatorSize = size; break;
707 case wxAUI_TBART_GRIPPER_SIZE: m_gripperSize = size; break;
708 case wxAUI_TBART_OVERFLOW_SIZE: m_overflowSize = size; break;
709 }
710 }
711
712 int wxAuiDefaultToolBarArt::ShowDropDown(wxWindow* wnd,
713 const wxAuiToolBarItemArray& items)
714 {
715 wxMenu menuPopup;
716
717 size_t items_added = 0;
718
719 size_t i, count = items.GetCount();
720 for (i = 0; i < count; ++i)
721 {
722 wxAuiToolBarItem& item = items.Item(i);
723
724 if (item.GetKind() == wxITEM_NORMAL)
725 {
726 wxString text = item.GetShortHelp();
727 if (text.empty())
728 text = item.GetLabel();
729
730 if (text.empty())
731 text = wxT(" ");
732
733 wxMenuItem* m = new wxMenuItem(&menuPopup, item.GetId(), text, item.GetShortHelp());
734
735 m->SetBitmap(item.GetBitmap());
736 menuPopup.Append(m);
737 items_added++;
738 }
739 else if (item.GetKind() == wxITEM_SEPARATOR)
740 {
741 if (items_added > 0)
742 menuPopup.AppendSeparator();
743 }
744 }
745
746 // find out where to put the popup menu of window items
747 wxPoint pt = ::wxGetMousePosition();
748 pt = wnd->ScreenToClient(pt);
749
750 // find out the screen coordinate at the bottom of the tab ctrl
751 wxRect cli_rect = wnd->GetClientRect();
752 pt.y = cli_rect.y + cli_rect.height;
753
754 ToolbarCommandCapture* cc = new ToolbarCommandCapture;
755 wnd->PushEventHandler(cc);
756 wnd->PopupMenu(&menuPopup, pt);
757 int command = cc->GetCommandId();
758 wnd->PopEventHandler(true);
759
760 return command;
761 }
762
763
764
765
766 static wxOrientation GetOrientation(long& style)
767 {
768 switch (style & wxAUI_ORIENTATION_MASK)
769 {
770 case wxAUI_TB_HORIZONTAL:
771 return wxHORIZONTAL;
772 case wxAUI_TB_VERTICAL:
773 return wxVERTICAL;
774 default:
775 wxFAIL_MSG("toolbar cannot be locked in both horizontal and vertical orientations (maybe no lock was intended?)");
776 // fall through
777 case 0:
778 return wxBOTH;
779 }
780 }
781
782 BEGIN_EVENT_TABLE(wxAuiToolBar, wxControl)
783 EVT_SIZE(wxAuiToolBar::OnSize)
784 EVT_IDLE(wxAuiToolBar::OnIdle)
785 EVT_ERASE_BACKGROUND(wxAuiToolBar::OnEraseBackground)
786 EVT_PAINT(wxAuiToolBar::OnPaint)
787 EVT_LEFT_DOWN(wxAuiToolBar::OnLeftDown)
788 EVT_LEFT_DCLICK(wxAuiToolBar::OnLeftDown)
789 EVT_LEFT_UP(wxAuiToolBar::OnLeftUp)
790 EVT_RIGHT_DOWN(wxAuiToolBar::OnRightDown)
791 EVT_RIGHT_DCLICK(wxAuiToolBar::OnRightDown)
792 EVT_RIGHT_UP(wxAuiToolBar::OnRightUp)
793 EVT_MIDDLE_DOWN(wxAuiToolBar::OnMiddleDown)
794 EVT_MIDDLE_DCLICK(wxAuiToolBar::OnMiddleDown)
795 EVT_MIDDLE_UP(wxAuiToolBar::OnMiddleUp)
796 EVT_MOTION(wxAuiToolBar::OnMotion)
797 EVT_LEAVE_WINDOW(wxAuiToolBar::OnLeaveWindow)
798 EVT_MOUSE_CAPTURE_LOST(wxAuiToolBar::OnCaptureLost)
799 EVT_SET_CURSOR(wxAuiToolBar::OnSetCursor)
800 END_EVENT_TABLE()
801
802
803 wxAuiToolBar::wxAuiToolBar(wxWindow* parent,
804 wxWindowID id,
805 const wxPoint& position,
806 const wxSize& size,
807 long style)
808 : wxControl(parent,
809 id,
810 position,
811 size,
812 style | wxBORDER_NONE)
813 {
814 m_sizer = new wxBoxSizer(wxHORIZONTAL);
815 m_buttonWidth = -1;
816 m_buttonHeight = -1;
817 m_sizerElementCount = 0;
818 m_actionPos = wxPoint(-1,-1);
819 m_actionItem = NULL;
820 m_tipItem = NULL;
821 m_art = new wxAuiDefaultToolBarArt;
822 m_toolPacking = 2;
823 m_toolBorderPadding = 3;
824 m_toolTextOrientation = wxAUI_TBTOOL_TEXT_BOTTOM;
825 m_gripperSizerItem = NULL;
826 m_overflowSizerItem = NULL;
827 m_dragging = false;
828 m_orientation = GetOrientation(style);
829 if (m_orientation == wxBOTH)
830 {
831 m_orientation = wxHORIZONTAL;
832 }
833 m_style = style | wxBORDER_NONE;
834 m_gripperVisible = (m_style & wxAUI_TB_GRIPPER) ? true : false;
835 m_overflowVisible = (m_style & wxAUI_TB_OVERFLOW) ? true : false;
836 m_overflowState = 0;
837 SetMargins(5, 5, 2, 2);
838 SetFont(*wxNORMAL_FONT);
839 SetArtFlags();
840 SetExtraStyle(wxWS_EX_PROCESS_IDLE);
841 if (style & wxAUI_TB_HORZ_LAYOUT)
842 SetToolTextOrientation(wxAUI_TBTOOL_TEXT_RIGHT);
843 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
844 }
845
846
847 wxAuiToolBar::~wxAuiToolBar()
848 {
849 delete m_art;
850 delete m_sizer;
851 }
852
853 void wxAuiToolBar::SetWindowStyleFlag(long style)
854 {
855 GetOrientation(style); // assert if style is invalid
856 wxCHECK_RET(IsPaneValid(style),
857 "window settings and pane settings are incompatible");
858
859 wxControl::SetWindowStyleFlag(style);
860
861 m_style = style;
862
863 if (m_art)
864 {
865 SetArtFlags();
866 }
867
868 if (m_style & wxAUI_TB_GRIPPER)
869 m_gripperVisible = true;
870 else
871 m_gripperVisible = false;
872
873
874 if (m_style & wxAUI_TB_OVERFLOW)
875 m_overflowVisible = true;
876 else
877 m_overflowVisible = false;
878
879 if (style & wxAUI_TB_HORZ_LAYOUT)
880 SetToolTextOrientation(wxAUI_TBTOOL_TEXT_RIGHT);
881 else
882 SetToolTextOrientation(wxAUI_TBTOOL_TEXT_BOTTOM);
883 }
884
885 long wxAuiToolBar::GetWindowStyleFlag() const
886 {
887 return m_style;
888 }
889
890 void wxAuiToolBar::SetArtProvider(wxAuiToolBarArt* art)
891 {
892 delete m_art;
893
894 m_art = art;
895
896 if (m_art)
897 {
898 SetArtFlags();
899 m_art->SetTextOrientation(m_toolTextOrientation);
900 }
901 }
902
903 wxAuiToolBarArt* wxAuiToolBar::GetArtProvider() const
904 {
905 return m_art;
906 }
907
908
909
910
911 wxAuiToolBarItem* wxAuiToolBar::AddTool(int tool_id,
912 const wxString& label,
913 const wxBitmap& bitmap,
914 const wxString& shortHelp_string,
915 wxItemKind kind)
916 {
917 return AddTool(tool_id,
918 label,
919 bitmap,
920 wxNullBitmap,
921 kind,
922 shortHelp_string,
923 wxEmptyString,
924 NULL);
925 }
926
927
928 wxAuiToolBarItem* wxAuiToolBar::AddTool(int tool_id,
929 const wxString& label,
930 const wxBitmap& bitmap,
931 const wxBitmap& disabledBitmap,
932 wxItemKind kind,
933 const wxString& shortHelpString,
934 const wxString& longHelpString,
935 wxObject* WXUNUSED(client_data))
936 {
937 wxAuiToolBarItem item;
938 item.m_window = NULL;
939 item.m_label = label;
940 item.m_bitmap = bitmap;
941 item.m_disabledBitmap = disabledBitmap;
942 item.m_shortHelp = shortHelpString;
943 item.m_longHelp = longHelpString;
944 item.m_active = true;
945 item.m_dropDown = false;
946 item.m_spacerPixels = 0;
947 item.m_toolId = tool_id;
948 item.m_state = 0;
949 item.m_proportion = 0;
950 item.m_kind = kind;
951 item.m_sizerItem = NULL;
952 item.m_minSize = wxDefaultSize;
953 item.m_userData = 0;
954 item.m_sticky = false;
955
956 if (item.m_toolId == wxID_ANY)
957 item.m_toolId = wxNewId();
958
959 if (!item.m_disabledBitmap.IsOk())
960 {
961 // no disabled bitmap specified, we need to make one
962 if (item.m_bitmap.IsOk())
963 {
964 item.m_disabledBitmap = item.m_bitmap.ConvertToDisabled();
965 }
966 }
967 m_items.Add(item);
968 return &m_items.Last();
969 }
970
971 wxAuiToolBarItem* wxAuiToolBar::AddControl(wxControl* control,
972 const wxString& label)
973 {
974 wxAuiToolBarItem item;
975 item.m_window = (wxWindow*)control;
976 item.m_label = label;
977 item.m_bitmap = wxNullBitmap;
978 item.m_disabledBitmap = wxNullBitmap;
979 item.m_active = true;
980 item.m_dropDown = false;
981 item.m_spacerPixels = 0;
982 item.m_toolId = control->GetId();
983 item.m_state = 0;
984 item.m_proportion = 0;
985 item.m_kind = wxITEM_CONTROL;
986 item.m_sizerItem = NULL;
987 item.m_minSize = control->GetEffectiveMinSize();
988 item.m_userData = 0;
989 item.m_sticky = false;
990
991 m_items.Add(item);
992 return &m_items.Last();
993 }
994
995 wxAuiToolBarItem* wxAuiToolBar::AddLabel(int tool_id,
996 const wxString& label,
997 const int width)
998 {
999 wxSize min_size = wxDefaultSize;
1000 if (width != -1)
1001 min_size.x = width;
1002
1003 wxAuiToolBarItem item;
1004 item.m_window = NULL;
1005 item.m_label = label;
1006 item.m_bitmap = wxNullBitmap;
1007 item.m_disabledBitmap = wxNullBitmap;
1008 item.m_active = true;
1009 item.m_dropDown = false;
1010 item.m_spacerPixels = 0;
1011 item.m_toolId = tool_id;
1012 item.m_state = 0;
1013 item.m_proportion = 0;
1014 item.m_kind = wxITEM_LABEL;
1015 item.m_sizerItem = NULL;
1016 item.m_minSize = min_size;
1017 item.m_userData = 0;
1018 item.m_sticky = false;
1019
1020 if (item.m_toolId == wxID_ANY)
1021 item.m_toolId = wxNewId();
1022
1023 m_items.Add(item);
1024 return &m_items.Last();
1025 }
1026
1027 wxAuiToolBarItem* wxAuiToolBar::AddSeparator()
1028 {
1029 wxAuiToolBarItem item;
1030 item.m_window = NULL;
1031 item.m_label = wxEmptyString;
1032 item.m_bitmap = wxNullBitmap;
1033 item.m_disabledBitmap = wxNullBitmap;
1034 item.m_active = true;
1035 item.m_dropDown = false;
1036 item.m_toolId = -1;
1037 item.m_state = 0;
1038 item.m_proportion = 0;
1039 item.m_kind = wxITEM_SEPARATOR;
1040 item.m_sizerItem = NULL;
1041 item.m_minSize = wxDefaultSize;
1042 item.m_userData = 0;
1043 item.m_sticky = false;
1044
1045 m_items.Add(item);
1046 return &m_items.Last();
1047 }
1048
1049 wxAuiToolBarItem* wxAuiToolBar::AddSpacer(int pixels)
1050 {
1051 wxAuiToolBarItem item;
1052 item.m_window = NULL;
1053 item.m_label = wxEmptyString;
1054 item.m_bitmap = wxNullBitmap;
1055 item.m_disabledBitmap = wxNullBitmap;
1056 item.m_active = true;
1057 item.m_dropDown = false;
1058 item.m_spacerPixels = pixels;
1059 item.m_toolId = -1;
1060 item.m_state = 0;
1061 item.m_proportion = 0;
1062 item.m_kind = wxITEM_SPACER;
1063 item.m_sizerItem = NULL;
1064 item.m_minSize = wxDefaultSize;
1065 item.m_userData = 0;
1066 item.m_sticky = false;
1067
1068 m_items.Add(item);
1069 return &m_items.Last();
1070 }
1071
1072 wxAuiToolBarItem* wxAuiToolBar::AddStretchSpacer(int proportion)
1073 {
1074 wxAuiToolBarItem item;
1075 item.m_window = NULL;
1076 item.m_label = wxEmptyString;
1077 item.m_bitmap = wxNullBitmap;
1078 item.m_disabledBitmap = wxNullBitmap;
1079 item.m_active = true;
1080 item.m_dropDown = false;
1081 item.m_spacerPixels = 0;
1082 item.m_toolId = -1;
1083 item.m_state = 0;
1084 item.m_proportion = proportion;
1085 item.m_kind = wxITEM_SPACER;
1086 item.m_sizerItem = NULL;
1087 item.m_minSize = wxDefaultSize;
1088 item.m_userData = 0;
1089 item.m_sticky = false;
1090
1091 m_items.Add(item);
1092 return &m_items.Last();
1093 }
1094
1095 void wxAuiToolBar::Clear()
1096 {
1097 m_items.Clear();
1098 m_sizerElementCount = 0;
1099 }
1100
1101 bool wxAuiToolBar::DeleteTool(int tool_id)
1102 {
1103 int idx = GetToolIndex(tool_id);
1104 if (idx >= 0 && idx < (int)m_items.GetCount())
1105 {
1106 m_items.RemoveAt(idx);
1107 Realize();
1108 return true;
1109 }
1110
1111 return false;
1112 }
1113
1114 bool wxAuiToolBar::DeleteByIndex(int idx)
1115 {
1116 if (idx >= 0 && idx < (int)m_items.GetCount())
1117 {
1118 m_items.RemoveAt(idx);
1119 Realize();
1120 return true;
1121 }
1122
1123 return false;
1124 }
1125
1126
1127 wxControl* wxAuiToolBar::FindControl(int id)
1128 {
1129 wxWindow* wnd = FindWindow(id);
1130 return (wxControl*)wnd;
1131 }
1132
1133 wxAuiToolBarItem* wxAuiToolBar::FindTool(int tool_id) const
1134 {
1135 size_t i, count;
1136 for (i = 0, count = m_items.GetCount(); i < count; ++i)
1137 {
1138 wxAuiToolBarItem& item = m_items.Item(i);
1139 if (item.m_toolId == tool_id)
1140 return &item;
1141 }
1142
1143 return NULL;
1144 }
1145
1146 wxAuiToolBarItem* wxAuiToolBar::FindToolByPosition(wxCoord x, wxCoord y) const
1147 {
1148 size_t i, count;
1149 for (i = 0, count = m_items.GetCount(); i < count; ++i)
1150 {
1151 wxAuiToolBarItem& item = m_items.Item(i);
1152
1153 if (!item.m_sizerItem)
1154 continue;
1155
1156 wxRect rect = item.m_sizerItem->GetRect();
1157 if (rect.Contains(x,y))
1158 {
1159 // if the item doesn't fit on the toolbar, return NULL
1160 if (!GetToolFitsByIndex(i))
1161 return NULL;
1162
1163 return &item;
1164 }
1165 }
1166
1167 return NULL;
1168 }
1169
1170 wxAuiToolBarItem* wxAuiToolBar::FindToolByPositionWithPacking(wxCoord x, wxCoord y) const
1171 {
1172 size_t i, count;
1173 for (i = 0, count = m_items.GetCount(); i < count; ++i)
1174 {
1175 wxAuiToolBarItem& item = m_items.Item(i);
1176
1177 if (!item.m_sizerItem)
1178 continue;
1179
1180 wxRect rect = item.m_sizerItem->GetRect();
1181
1182 // apply tool packing
1183 if (i+1 < count)
1184 rect.width += m_toolPacking;
1185
1186 if (rect.Contains(x,y))
1187 {
1188 // if the item doesn't fit on the toolbar, return NULL
1189 if (!GetToolFitsByIndex(i))
1190 return NULL;
1191
1192 return &item;
1193 }
1194 }
1195
1196 return NULL;
1197 }
1198
1199 wxAuiToolBarItem* wxAuiToolBar::FindToolByIndex(int idx) const
1200 {
1201 if (idx < 0)
1202 return NULL;
1203
1204 if (idx >= (int)m_items.size())
1205 return NULL;
1206
1207 return &(m_items[idx]);
1208 }
1209
1210 void wxAuiToolBar::SetToolBitmapSize(const wxSize& WXUNUSED(size))
1211 {
1212 // TODO: wxToolBar compatibility
1213 }
1214
1215 wxSize wxAuiToolBar::GetToolBitmapSize() const
1216 {
1217 // TODO: wxToolBar compatibility
1218 return wxSize(16,15);
1219 }
1220
1221 void wxAuiToolBar::SetToolProportion(int tool_id, int proportion)
1222 {
1223 wxAuiToolBarItem* item = FindTool(tool_id);
1224 if (!item)
1225 return;
1226
1227 item->m_proportion = proportion;
1228 }
1229
1230 int wxAuiToolBar::GetToolProportion(int tool_id) const
1231 {
1232 wxAuiToolBarItem* item = FindTool(tool_id);
1233 if (!item)
1234 return 0;
1235
1236 return item->m_proportion;
1237 }
1238
1239 void wxAuiToolBar::SetToolSeparation(int separation)
1240 {
1241 if (m_art)
1242 m_art->SetElementSize(wxAUI_TBART_SEPARATOR_SIZE, separation);
1243 }
1244
1245 int wxAuiToolBar::GetToolSeparation() const
1246 {
1247 if (m_art)
1248 return m_art->GetElementSize(wxAUI_TBART_SEPARATOR_SIZE);
1249 else
1250 return 5;
1251 }
1252
1253
1254 void wxAuiToolBar::SetToolDropDown(int tool_id, bool dropdown)
1255 {
1256 wxAuiToolBarItem* item = FindTool(tool_id);
1257 if (!item)
1258 return;
1259
1260 item->m_dropDown = dropdown;
1261 }
1262
1263 bool wxAuiToolBar::GetToolDropDown(int tool_id) const
1264 {
1265 wxAuiToolBarItem* item = FindTool(tool_id);
1266 if (!item)
1267 return 0;
1268
1269 return item->m_dropDown;
1270 }
1271
1272 void wxAuiToolBar::SetToolSticky(int tool_id, bool sticky)
1273 {
1274 // ignore separators
1275 if (tool_id == -1)
1276 return;
1277
1278 wxAuiToolBarItem* item = FindTool(tool_id);
1279 if (!item)
1280 return;
1281
1282 if (item->m_sticky == sticky)
1283 return;
1284
1285 item->m_sticky = sticky;
1286
1287 Refresh(false);
1288 Update();
1289 }
1290
1291 bool wxAuiToolBar::GetToolSticky(int tool_id) const
1292 {
1293 wxAuiToolBarItem* item = FindTool(tool_id);
1294 if (!item)
1295 return 0;
1296
1297 return item->m_sticky;
1298 }
1299
1300
1301
1302
1303 void wxAuiToolBar::SetToolBorderPadding(int padding)
1304 {
1305 m_toolBorderPadding = padding;
1306 }
1307
1308 int wxAuiToolBar::GetToolBorderPadding() const
1309 {
1310 return m_toolBorderPadding;
1311 }
1312
1313 void wxAuiToolBar::SetToolTextOrientation(int orientation)
1314 {
1315 m_toolTextOrientation = orientation;
1316
1317 if (m_art)
1318 {
1319 m_art->SetTextOrientation(orientation);
1320 }
1321 }
1322
1323 int wxAuiToolBar::GetToolTextOrientation() const
1324 {
1325 return m_toolTextOrientation;
1326 }
1327
1328 void wxAuiToolBar::SetToolPacking(int packing)
1329 {
1330 m_toolPacking = packing;
1331 }
1332
1333 int wxAuiToolBar::GetToolPacking() const
1334 {
1335 return m_toolPacking;
1336 }
1337
1338
1339 void wxAuiToolBar::SetOrientation(int orientation)
1340 {
1341 wxCHECK_RET(orientation == wxHORIZONTAL ||
1342 orientation == wxVERTICAL,
1343 "invalid orientation value");
1344 if (orientation != m_orientation)
1345 {
1346 m_orientation = wxOrientation(orientation);
1347 SetArtFlags();
1348 }
1349 }
1350
1351 void wxAuiToolBar::SetMargins(int left, int right, int top, int bottom)
1352 {
1353 if (left != -1)
1354 m_leftPadding = left;
1355 if (right != -1)
1356 m_rightPadding = right;
1357 if (top != -1)
1358 m_topPadding = top;
1359 if (bottom != -1)
1360 m_bottomPadding = bottom;
1361 }
1362
1363 bool wxAuiToolBar::GetGripperVisible() const
1364 {
1365 return m_gripperVisible;
1366 }
1367
1368 void wxAuiToolBar::SetGripperVisible(bool visible)
1369 {
1370 m_gripperVisible = visible;
1371 if (visible)
1372 m_style |= wxAUI_TB_GRIPPER;
1373 else
1374 m_style &= ~wxAUI_TB_GRIPPER;
1375 Realize();
1376 Refresh(false);
1377 }
1378
1379
1380 bool wxAuiToolBar::GetOverflowVisible() const
1381 {
1382 return m_overflowVisible;
1383 }
1384
1385 void wxAuiToolBar::SetOverflowVisible(bool visible)
1386 {
1387 m_overflowVisible = visible;
1388 if (visible)
1389 m_style |= wxAUI_TB_OVERFLOW;
1390 else
1391 m_style &= ~wxAUI_TB_OVERFLOW;
1392 Refresh(false);
1393 }
1394
1395 bool wxAuiToolBar::SetFont(const wxFont& font)
1396 {
1397 bool res = wxWindow::SetFont(font);
1398
1399 if (m_art)
1400 {
1401 m_art->SetFont(font);
1402 }
1403
1404 return res;
1405 }
1406
1407
1408 void wxAuiToolBar::SetHoverItem(wxAuiToolBarItem* pitem)
1409 {
1410 wxAuiToolBarItem* former_hover = NULL;
1411
1412 size_t i, count;
1413 for (i = 0, count = m_items.GetCount(); i < count; ++i)
1414 {
1415 wxAuiToolBarItem& item = m_items.Item(i);
1416 if (item.m_state & wxAUI_BUTTON_STATE_HOVER)
1417 former_hover = &item;
1418 item.m_state &= ~wxAUI_BUTTON_STATE_HOVER;
1419 }
1420
1421 if (pitem)
1422 {
1423 pitem->m_state |= wxAUI_BUTTON_STATE_HOVER;
1424 }
1425
1426 if (former_hover != pitem)
1427 {
1428 Refresh(false);
1429 Update();
1430 }
1431 }
1432
1433 void wxAuiToolBar::SetPressedItem(wxAuiToolBarItem* pitem)
1434 {
1435 wxAuiToolBarItem* former_item = NULL;
1436
1437 size_t i, count;
1438 for (i = 0, count = m_items.GetCount(); i < count; ++i)
1439 {
1440 wxAuiToolBarItem& item = m_items.Item(i);
1441 if (item.m_state & wxAUI_BUTTON_STATE_PRESSED)
1442 former_item = &item;
1443 item.m_state &= ~wxAUI_BUTTON_STATE_PRESSED;
1444 }
1445
1446 if (pitem)
1447 {
1448 pitem->m_state &= ~wxAUI_BUTTON_STATE_HOVER;
1449 pitem->m_state |= wxAUI_BUTTON_STATE_PRESSED;
1450 }
1451
1452 if (former_item != pitem)
1453 {
1454 Refresh(false);
1455 Update();
1456 }
1457 }
1458
1459 void wxAuiToolBar::RefreshOverflowState()
1460 {
1461 if (!m_overflowSizerItem)
1462 {
1463 m_overflowState = 0;
1464 return;
1465 }
1466
1467 int overflow_state = 0;
1468
1469 wxRect overflow_rect = GetOverflowRect();
1470
1471
1472 // find out the mouse's current position
1473 wxPoint pt = ::wxGetMousePosition();
1474 pt = this->ScreenToClient(pt);
1475
1476 // find out if the mouse cursor is inside the dropdown rectangle
1477 if (overflow_rect.Contains(pt.x, pt.y))
1478 {
1479 if (::wxGetMouseState().LeftIsDown())
1480 overflow_state = wxAUI_BUTTON_STATE_PRESSED;
1481 else
1482 overflow_state = wxAUI_BUTTON_STATE_HOVER;
1483 }
1484
1485 if (overflow_state != m_overflowState)
1486 {
1487 m_overflowState = overflow_state;
1488 Refresh(false);
1489 Update();
1490 }
1491
1492 m_overflowState = overflow_state;
1493 }
1494
1495 void wxAuiToolBar::ToggleTool(int tool_id, bool state)
1496 {
1497 wxAuiToolBarItem* tool = FindTool(tool_id);
1498
1499 if (tool && (tool->m_kind == wxITEM_CHECK || tool->m_kind == wxITEM_RADIO))
1500 {
1501 if (tool->m_kind == wxITEM_RADIO)
1502 {
1503 int i, idx, count;
1504 idx = GetToolIndex(tool_id);
1505 count = (int)m_items.GetCount();
1506
1507 if (idx >= 0 && idx < count)
1508 {
1509 for (i = idx + 1; i < count; ++i)
1510 {
1511 if (m_items[i].m_kind != wxITEM_RADIO)
1512 break;
1513 m_items[i].m_state &= ~wxAUI_BUTTON_STATE_CHECKED;
1514 }
1515 for (i = idx - 1; i >= 0; i--)
1516 {
1517 if (m_items[i].m_kind != wxITEM_RADIO)
1518 break;
1519 m_items[i].m_state &= ~wxAUI_BUTTON_STATE_CHECKED;
1520 }
1521 }
1522
1523 tool->m_state |= wxAUI_BUTTON_STATE_CHECKED;
1524 }
1525 else if (tool->m_kind == wxITEM_CHECK)
1526 {
1527 if (state == true)
1528 tool->m_state |= wxAUI_BUTTON_STATE_CHECKED;
1529 else
1530 tool->m_state &= ~wxAUI_BUTTON_STATE_CHECKED;
1531 }
1532 }
1533 }
1534
1535 bool wxAuiToolBar::GetToolToggled(int tool_id) const
1536 {
1537 wxAuiToolBarItem* tool = FindTool(tool_id);
1538
1539 if (tool)
1540 {
1541 if ( (tool->m_kind != wxITEM_CHECK) && (tool->m_kind != wxITEM_RADIO) )
1542 return false;
1543
1544 return (tool->m_state & wxAUI_BUTTON_STATE_CHECKED) ? true : false;
1545 }
1546
1547 return false;
1548 }
1549
1550 void wxAuiToolBar::EnableTool(int tool_id, bool state)
1551 {
1552 wxAuiToolBarItem* tool = FindTool(tool_id);
1553
1554 if (tool)
1555 {
1556 if (state == true)
1557 tool->m_state &= ~wxAUI_BUTTON_STATE_DISABLED;
1558 else
1559 tool->m_state |= wxAUI_BUTTON_STATE_DISABLED;
1560 }
1561 }
1562
1563 bool wxAuiToolBar::GetToolEnabled(int tool_id) const
1564 {
1565 wxAuiToolBarItem* tool = FindTool(tool_id);
1566
1567 if (tool)
1568 return (tool->m_state & wxAUI_BUTTON_STATE_DISABLED) ? false : true;
1569
1570 return false;
1571 }
1572
1573 wxString wxAuiToolBar::GetToolLabel(int tool_id) const
1574 {
1575 wxAuiToolBarItem* tool = FindTool(tool_id);
1576 wxASSERT_MSG(tool, wxT("can't find tool in toolbar item array"));
1577 if (!tool)
1578 return wxEmptyString;
1579
1580 return tool->m_label;
1581 }
1582
1583 void wxAuiToolBar::SetToolLabel(int tool_id, const wxString& label)
1584 {
1585 wxAuiToolBarItem* tool = FindTool(tool_id);
1586 if (tool)
1587 {
1588 tool->m_label = label;
1589 }
1590 }
1591
1592 wxBitmap wxAuiToolBar::GetToolBitmap(int tool_id) const
1593 {
1594 wxAuiToolBarItem* tool = FindTool(tool_id);
1595 wxASSERT_MSG(tool, wxT("can't find tool in toolbar item array"));
1596 if (!tool)
1597 return wxNullBitmap;
1598
1599 return tool->m_bitmap;
1600 }
1601
1602 void wxAuiToolBar::SetToolBitmap(int tool_id, const wxBitmap& bitmap)
1603 {
1604 wxAuiToolBarItem* tool = FindTool(tool_id);
1605 if (tool)
1606 {
1607 tool->m_bitmap = bitmap;
1608 }
1609 }
1610
1611 wxString wxAuiToolBar::GetToolShortHelp(int tool_id) const
1612 {
1613 wxAuiToolBarItem* tool = FindTool(tool_id);
1614 wxASSERT_MSG(tool, wxT("can't find tool in toolbar item array"));
1615 if (!tool)
1616 return wxEmptyString;
1617
1618 return tool->m_shortHelp;
1619 }
1620
1621 void wxAuiToolBar::SetToolShortHelp(int tool_id, const wxString& help_string)
1622 {
1623 wxAuiToolBarItem* tool = FindTool(tool_id);
1624 if (tool)
1625 {
1626 tool->m_shortHelp = help_string;
1627 }
1628 }
1629
1630 wxString wxAuiToolBar::GetToolLongHelp(int tool_id) const
1631 {
1632 wxAuiToolBarItem* tool = FindTool(tool_id);
1633 wxASSERT_MSG(tool, wxT("can't find tool in toolbar item array"));
1634 if (!tool)
1635 return wxEmptyString;
1636
1637 return tool->m_longHelp;
1638 }
1639
1640 void wxAuiToolBar::SetToolLongHelp(int tool_id, const wxString& help_string)
1641 {
1642 wxAuiToolBarItem* tool = FindTool(tool_id);
1643 if (tool)
1644 {
1645 tool->m_longHelp = help_string;
1646 }
1647 }
1648
1649 void wxAuiToolBar::SetCustomOverflowItems(const wxAuiToolBarItemArray& prepend,
1650 const wxAuiToolBarItemArray& append)
1651 {
1652 m_customOverflowPrepend = prepend;
1653 m_customOverflowAppend = append;
1654 }
1655
1656 // get size of hint rectangle for a particular dock location
1657 wxSize wxAuiToolBar::GetHintSize(int dock_direction) const
1658 {
1659 switch (dock_direction)
1660 {
1661 case wxAUI_DOCK_TOP:
1662 case wxAUI_DOCK_BOTTOM:
1663 return m_horzHintSize;
1664 case wxAUI_DOCK_RIGHT:
1665 case wxAUI_DOCK_LEFT:
1666 return m_vertHintSize;
1667 default:
1668 wxCHECK_MSG(false, wxDefaultSize, "invalid dock location value");
1669 }
1670 }
1671
1672 bool wxAuiToolBar::IsPaneValid(const wxAuiPaneInfo& pane) const
1673 {
1674 return IsPaneValid(m_style, pane);
1675 }
1676
1677 bool wxAuiToolBar::IsPaneValid(long style, const wxAuiPaneInfo& pane)
1678 {
1679 if (style & wxAUI_TB_HORIZONTAL)
1680 {
1681 if (pane.IsLeftDockable() || pane.IsRightDockable())
1682 {
1683 return false;
1684 }
1685 }
1686 else if (style & wxAUI_TB_VERTICAL)
1687 {
1688 if (pane.IsTopDockable() || pane.IsBottomDockable())
1689 {
1690 return false;
1691 }
1692 }
1693 return true;
1694 }
1695
1696 bool wxAuiToolBar::IsPaneValid(long style) const
1697 {
1698 wxAuiManager* manager = wxAuiManager::GetManager(const_cast<wxAuiToolBar*>(this));
1699 if (manager)
1700 {
1701 return IsPaneValid(style, manager->GetPane(const_cast<wxAuiToolBar*>(this)));
1702 }
1703 return true;
1704 }
1705
1706 void wxAuiToolBar::SetArtFlags() const
1707 {
1708 unsigned int artflags = m_style & ~wxAUI_ORIENTATION_MASK;
1709 if (m_orientation == wxVERTICAL)
1710 {
1711 artflags |= wxAUI_TB_VERTICAL;
1712 }
1713 m_art->SetFlags(artflags);
1714 }
1715
1716 size_t wxAuiToolBar::GetToolCount() const
1717 {
1718 return m_items.size();
1719 }
1720
1721 int wxAuiToolBar::GetToolIndex(int tool_id) const
1722 {
1723 // this will prevent us from returning the index of the
1724 // first separator in the toolbar since its id is equal to -1
1725 if (tool_id == -1)
1726 return wxNOT_FOUND;
1727
1728 size_t i, count = m_items.GetCount();
1729 for (i = 0; i < count; ++i)
1730 {
1731 wxAuiToolBarItem& item = m_items.Item(i);
1732 if (item.m_toolId == tool_id)
1733 return i;
1734 }
1735
1736 return wxNOT_FOUND;
1737 }
1738
1739 bool wxAuiToolBar::GetToolFitsByIndex(int tool_idx) const
1740 {
1741 if (tool_idx < 0 || tool_idx >= (int)m_items.GetCount())
1742 return false;
1743
1744 if (!m_items[tool_idx].m_sizerItem)
1745 return false;
1746
1747 int cli_w, cli_h;
1748 GetClientSize(&cli_w, &cli_h);
1749
1750 wxRect rect = m_items[tool_idx].m_sizerItem->GetRect();
1751
1752 if (m_orientation == wxVERTICAL)
1753 {
1754 // take the dropdown size into account
1755 if (m_overflowVisible)
1756 cli_h -= m_overflowSizerItem->GetSize().y;
1757
1758 if (rect.y+rect.height < cli_h)
1759 return true;
1760 }
1761 else
1762 {
1763 // take the dropdown size into account
1764 if (m_overflowVisible)
1765 cli_w -= m_overflowSizerItem->GetSize().x;
1766
1767 if (rect.x+rect.width < cli_w)
1768 return true;
1769 }
1770
1771 return false;
1772 }
1773
1774
1775 bool wxAuiToolBar::GetToolFits(int tool_id) const
1776 {
1777 return GetToolFitsByIndex(GetToolIndex(tool_id));
1778 }
1779
1780 wxRect wxAuiToolBar::GetToolRect(int tool_id) const
1781 {
1782 wxAuiToolBarItem* tool = FindTool(tool_id);
1783 if (tool && tool->m_sizerItem)
1784 {
1785 return tool->m_sizerItem->GetRect();
1786 }
1787
1788 return wxRect();
1789 }
1790
1791 bool wxAuiToolBar::GetToolBarFits() const
1792 {
1793 if (m_items.GetCount() == 0)
1794 {
1795 // empty toolbar always 'fits'
1796 return true;
1797 }
1798
1799 // entire toolbar content fits if the last tool fits
1800 return GetToolFitsByIndex(m_items.GetCount() - 1);
1801 }
1802
1803 bool wxAuiToolBar::Realize()
1804 {
1805 wxClientDC dc(this);
1806 if (!dc.IsOk())
1807 return false;
1808
1809 // calculate hint sizes for both horizontal and vertical
1810 // in the order that leaves toolbar in correct final state
1811 bool retval = false;
1812 if (m_orientation == wxHORIZONTAL)
1813 {
1814 if (RealizeHelper(dc, false))
1815 {
1816 m_vertHintSize = GetSize();
1817 if (RealizeHelper(dc, true))
1818 {
1819 m_horzHintSize = GetSize();
1820 retval = true;
1821 }
1822 }
1823 }
1824 else
1825 {
1826 if (RealizeHelper(dc, true))
1827 {
1828 m_horzHintSize = GetSize();
1829 if (RealizeHelper(dc, false))
1830 {
1831 m_vertHintSize = GetSize();
1832 retval = true;
1833 }
1834 }
1835 }
1836
1837 Refresh(false);
1838 return retval;
1839 }
1840
1841 bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
1842 {
1843 // create the new sizer to add toolbar elements to
1844 wxBoxSizer* sizer = new wxBoxSizer(horizontal ? wxHORIZONTAL : wxVERTICAL);
1845
1846 // add gripper area
1847 int separatorSize = m_art->GetElementSize(wxAUI_TBART_SEPARATOR_SIZE);
1848 int gripperSize = m_art->GetElementSize(wxAUI_TBART_GRIPPER_SIZE);
1849 if (gripperSize > 0 && m_gripperVisible)
1850 {
1851 if (horizontal)
1852 m_gripperSizerItem = sizer->Add(gripperSize, 1, 0, wxEXPAND);
1853 else
1854 m_gripperSizerItem = sizer->Add(1, gripperSize, 0, wxEXPAND);
1855 }
1856 else
1857 {
1858 m_gripperSizerItem = NULL;
1859 }
1860
1861 // add "left" padding
1862 if (m_leftPadding > 0)
1863 {
1864 if (horizontal)
1865 sizer->Add(m_leftPadding, 1);
1866 else
1867 sizer->Add(1, m_leftPadding);
1868 }
1869
1870 size_t i, count;
1871 for (i = 0, count = m_items.GetCount(); i < count; ++i)
1872 {
1873 wxAuiToolBarItem& item = m_items.Item(i);
1874 wxSizerItem* m_sizerItem = NULL;
1875
1876 switch (item.m_kind)
1877 {
1878 case wxITEM_LABEL:
1879 {
1880 wxSize size = m_art->GetLabelSize(dc, this, item);
1881 m_sizerItem = sizer->Add(size.x + (m_toolBorderPadding*2),
1882 size.y + (m_toolBorderPadding*2),
1883 item.m_proportion,
1884 item.m_alignment);
1885 if (i+1 < count)
1886 {
1887 sizer->AddSpacer(m_toolPacking);
1888 }
1889
1890 break;
1891 }
1892
1893 case wxITEM_CHECK:
1894 case wxITEM_NORMAL:
1895 case wxITEM_RADIO:
1896 {
1897 wxSize size = m_art->GetToolSize(dc, this, item);
1898 m_sizerItem = sizer->Add(size.x + (m_toolBorderPadding*2),
1899 size.y + (m_toolBorderPadding*2),
1900 0,
1901 item.m_alignment);
1902 // add tool packing
1903 if (i+1 < count)
1904 {
1905 sizer->AddSpacer(m_toolPacking);
1906 }
1907
1908 break;
1909 }
1910
1911 case wxITEM_SEPARATOR:
1912 {
1913 if (horizontal)
1914 m_sizerItem = sizer->Add(separatorSize, 1, 0, wxEXPAND);
1915 else
1916 m_sizerItem = sizer->Add(1, separatorSize, 0, wxEXPAND);
1917
1918 // add tool packing
1919 if (i+1 < count)
1920 {
1921 sizer->AddSpacer(m_toolPacking);
1922 }
1923
1924 break;
1925 }
1926
1927 case wxITEM_SPACER:
1928 if (item.m_proportion > 0)
1929 m_sizerItem = sizer->AddStretchSpacer(item.m_proportion);
1930 else
1931 m_sizerItem = sizer->Add(item.m_spacerPixels, 1);
1932 break;
1933
1934 case wxITEM_CONTROL:
1935 {
1936 //m_sizerItem = sizer->Add(item.m_window, item.m_proportion, wxEXPAND);
1937 wxSizerItem* ctrl_m_sizerItem;
1938
1939 wxBoxSizer* vert_sizer = new wxBoxSizer(wxVERTICAL);
1940 vert_sizer->AddStretchSpacer(1);
1941 ctrl_m_sizerItem = vert_sizer->Add(item.m_window, 0, wxEXPAND);
1942 vert_sizer->AddStretchSpacer(1);
1943 if ( (m_style & wxAUI_TB_TEXT) &&
1944 m_toolTextOrientation == wxAUI_TBTOOL_TEXT_BOTTOM &&
1945 !item.GetLabel().empty() )
1946 {
1947 wxSize s = GetLabelSize(item.GetLabel());
1948 vert_sizer->Add(1, s.y);
1949 }
1950
1951
1952 m_sizerItem = sizer->Add(vert_sizer, item.m_proportion, wxEXPAND);
1953
1954 wxSize min_size = item.m_minSize;
1955
1956
1957 // proportional items will disappear from the toolbar if
1958 // their min width is not set to something really small
1959 if (item.m_proportion != 0)
1960 {
1961 min_size.x = 1;
1962 }
1963
1964 if (min_size.IsFullySpecified())
1965 {
1966 m_sizerItem->SetMinSize(min_size);
1967 ctrl_m_sizerItem->SetMinSize(min_size);
1968 }
1969
1970 // add tool packing
1971 if (i+1 < count)
1972 {
1973 sizer->AddSpacer(m_toolPacking);
1974 }
1975 }
1976 }
1977
1978 item.m_sizerItem = m_sizerItem;
1979 }
1980
1981 // add "right" padding
1982 if (m_rightPadding > 0)
1983 {
1984 if (horizontal)
1985 sizer->Add(m_rightPadding, 1);
1986 else
1987 sizer->Add(1, m_rightPadding);
1988 }
1989
1990 // add drop down area
1991 m_overflowSizerItem = NULL;
1992
1993 if (m_style & wxAUI_TB_OVERFLOW)
1994 {
1995 int overflow_size = m_art->GetElementSize(wxAUI_TBART_OVERFLOW_SIZE);
1996 if (overflow_size > 0 && m_overflowVisible)
1997 {
1998 if (horizontal)
1999 m_overflowSizerItem = sizer->Add(overflow_size, 1, 0, wxEXPAND);
2000 else
2001 m_overflowSizerItem = sizer->Add(1, overflow_size, 0, wxEXPAND);
2002 }
2003 else
2004 {
2005 m_overflowSizerItem = NULL;
2006 }
2007 }
2008
2009
2010 // the outside sizer helps us apply the "top" and "bottom" padding
2011 wxBoxSizer* outside_sizer = new wxBoxSizer(horizontal ? wxVERTICAL : wxHORIZONTAL);
2012
2013 // add "top" padding
2014 if (m_topPadding > 0)
2015 {
2016 if (horizontal)
2017 outside_sizer->Add(1, m_topPadding);
2018 else
2019 outside_sizer->Add(m_topPadding, 1);
2020 }
2021
2022 // add the sizer that contains all of the toolbar elements
2023 outside_sizer->Add(sizer, 1, wxEXPAND);
2024
2025 // add "bottom" padding
2026 if (m_bottomPadding > 0)
2027 {
2028 if (horizontal)
2029 outside_sizer->Add(1, m_bottomPadding);
2030 else
2031 outside_sizer->Add(m_bottomPadding, 1);
2032 }
2033
2034 delete m_sizer; // remove old sizer
2035 m_sizer = outside_sizer;
2036
2037 // calculate the rock-bottom minimum size
2038 for (i = 0, count = m_items.GetCount(); i < count; ++i)
2039 {
2040 wxAuiToolBarItem& item = m_items.Item(i);
2041 if (item.m_sizerItem && item.m_proportion > 0 && item.m_minSize.IsFullySpecified())
2042 item.m_sizerItem->SetMinSize(0,0);
2043 }
2044
2045 m_absoluteMinSize = m_sizer->GetMinSize();
2046
2047 // reset the min sizes to what they were
2048 for (i = 0, count = m_items.GetCount(); i < count; ++i)
2049 {
2050 wxAuiToolBarItem& item = m_items.Item(i);
2051 if (item.m_sizerItem && item.m_proportion > 0 && item.m_minSize.IsFullySpecified())
2052 item.m_sizerItem->SetMinSize(item.m_minSize);
2053 }
2054
2055 // set control size
2056 wxSize size = m_sizer->GetMinSize();
2057 m_minWidth = size.x;
2058 m_minHeight = size.y;
2059
2060 if ((m_style & wxAUI_TB_NO_AUTORESIZE) == 0)
2061 {
2062 wxSize curSize = GetClientSize();
2063 wxSize new_size = GetMinSize();
2064 if (new_size != curSize)
2065 {
2066 SetClientSize(new_size);
2067 }
2068 else
2069 {
2070 m_sizer->SetDimension(0, 0, curSize.x, curSize.y);
2071 }
2072 }
2073 else
2074 {
2075 wxSize curSize = GetClientSize();
2076 m_sizer->SetDimension(0, 0, curSize.x, curSize.y);
2077 }
2078
2079 return true;
2080 }
2081
2082 int wxAuiToolBar::GetOverflowState() const
2083 {
2084 return m_overflowState;
2085 }
2086
2087 wxRect wxAuiToolBar::GetOverflowRect() const
2088 {
2089 wxRect cli_rect(wxPoint(0,0), GetClientSize());
2090 wxRect overflow_rect = m_overflowSizerItem->GetRect();
2091 int overflow_size = m_art->GetElementSize(wxAUI_TBART_OVERFLOW_SIZE);
2092
2093 if (m_orientation == wxVERTICAL)
2094 {
2095 overflow_rect.y = cli_rect.height - overflow_size;
2096 overflow_rect.x = 0;
2097 overflow_rect.width = cli_rect.width;
2098 overflow_rect.height = overflow_size;
2099 }
2100 else
2101 {
2102 overflow_rect.x = cli_rect.width - overflow_size;
2103 overflow_rect.y = 0;
2104 overflow_rect.width = overflow_size;
2105 overflow_rect.height = cli_rect.height;
2106 }
2107
2108 return overflow_rect;
2109 }
2110
2111 wxSize wxAuiToolBar::GetLabelSize(const wxString& label)
2112 {
2113 wxClientDC dc(this);
2114
2115 int tx, ty;
2116 int textWidth = 0, textHeight = 0;
2117
2118 dc.SetFont(m_font);
2119
2120 // get the text height
2121 dc.GetTextExtent(wxT("ABCDHgj"), &tx, &textHeight);
2122
2123 // get the text width
2124 dc.GetTextExtent(label, &textWidth, &ty);
2125
2126 return wxSize(textWidth, textHeight);
2127 }
2128
2129
2130 void wxAuiToolBar::DoIdleUpdate()
2131 {
2132 wxEvtHandler* handler = GetEventHandler();
2133
2134 bool need_refresh = false;
2135
2136 size_t i, count;
2137 for (i = 0, count = m_items.GetCount(); i < count; ++i)
2138 {
2139 wxAuiToolBarItem& item = m_items.Item(i);
2140
2141 if (item.m_toolId == -1)
2142 continue;
2143
2144 wxUpdateUIEvent evt(item.m_toolId);
2145 evt.SetEventObject(this);
2146
2147 if (handler->ProcessEvent(evt))
2148 {
2149 if (evt.GetSetEnabled())
2150 {
2151 bool is_enabled;
2152 if (item.m_window)
2153 is_enabled = item.m_window->IsEnabled();
2154 else
2155 is_enabled = (item.m_state & wxAUI_BUTTON_STATE_DISABLED) ? false : true;
2156
2157 bool new_enabled = evt.GetEnabled();
2158 if (new_enabled != is_enabled)
2159 {
2160 if (item.m_window)
2161 {
2162 item.m_window->Enable(new_enabled);
2163 }
2164 else
2165 {
2166 if (new_enabled)
2167 item.m_state &= ~wxAUI_BUTTON_STATE_DISABLED;
2168 else
2169 item.m_state |= wxAUI_BUTTON_STATE_DISABLED;
2170 }
2171 need_refresh = true;
2172 }
2173 }
2174
2175 if (evt.GetSetChecked())
2176 {
2177 // make sure we aren't checking an item that can't be
2178 if (item.m_kind != wxITEM_CHECK && item.m_kind != wxITEM_RADIO)
2179 continue;
2180
2181 bool is_checked = (item.m_state & wxAUI_BUTTON_STATE_CHECKED) ? true : false;
2182 bool new_checked = evt.GetChecked();
2183
2184 if (new_checked != is_checked)
2185 {
2186 if (new_checked)
2187 item.m_state |= wxAUI_BUTTON_STATE_CHECKED;
2188 else
2189 item.m_state &= ~wxAUI_BUTTON_STATE_CHECKED;
2190
2191 need_refresh = true;
2192 }
2193 }
2194
2195 }
2196 }
2197
2198
2199 if (need_refresh)
2200 {
2201 Refresh(false);
2202 }
2203 }
2204
2205
2206 void wxAuiToolBar::OnSize(wxSizeEvent& WXUNUSED(evt))
2207 {
2208 int x, y;
2209 GetClientSize(&x, &y);
2210
2211 if (((x >= y) && m_absoluteMinSize.x > x) ||
2212 ((y > x) && m_absoluteMinSize.y > y))
2213 {
2214 // hide all flexible items
2215 size_t i, count;
2216 for (i = 0, count = m_items.GetCount(); i < count; ++i)
2217 {
2218 wxAuiToolBarItem& item = m_items.Item(i);
2219 if (item.m_sizerItem && item.m_proportion > 0 && item.m_sizerItem->IsShown())
2220 {
2221 item.m_sizerItem->Show(false);
2222 item.m_sizerItem->SetProportion(0);
2223 }
2224 }
2225 }
2226 else
2227 {
2228 // show all flexible items
2229 size_t i, count;
2230 for (i = 0, count = m_items.GetCount(); i < count; ++i)
2231 {
2232 wxAuiToolBarItem& item = m_items.Item(i);
2233 if (item.m_sizerItem && item.m_proportion > 0 && !item.m_sizerItem->IsShown())
2234 {
2235 item.m_sizerItem->Show(true);
2236 item.m_sizerItem->SetProportion(item.m_proportion);
2237 }
2238 }
2239 }
2240
2241 m_sizer->SetDimension(0, 0, x, y);
2242
2243 Refresh(false);
2244 Update();
2245
2246 // idle events aren't sent while user is resizing frame (why?),
2247 // but resizing toolbar here causes havoc,
2248 // so force idle handler to run after size handling complete
2249 QueueEvent(new wxIdleEvent);
2250 }
2251
2252
2253
2254 void wxAuiToolBar::DoSetSize(int x,
2255 int y,
2256 int width,
2257 int height,
2258 int sizeFlags)
2259 {
2260 wxSize parent_size = GetParent()->GetClientSize();
2261 if (x + width > parent_size.x)
2262 width = wxMax(0, parent_size.x - x);
2263 if (y + height > parent_size.y)
2264 height = wxMax(0, parent_size.y - y);
2265
2266 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
2267 }
2268
2269
2270 void wxAuiToolBar::OnIdle(wxIdleEvent& evt)
2271 {
2272 // if orientation doesn't match dock, fix it
2273 wxAuiManager* manager = wxAuiManager::GetManager(this);
2274 if (manager)
2275 {
2276 wxAuiPaneInfo& pane = manager->GetPane(this);
2277 // pane state member is public, so it might have been changed
2278 // without going through wxPaneInfo::SetFlag() check
2279 bool ok = pane.IsOk();
2280 wxCHECK2_MSG(!ok || IsPaneValid(m_style, pane), ok = false,
2281 "window settings and pane settings are incompatible");
2282 if (ok)
2283 {
2284 wxOrientation newOrientation = m_orientation;
2285 if (pane.IsDocked())
2286 {
2287 switch (pane.dock_direction)
2288 {
2289 case wxAUI_DOCK_TOP:
2290 case wxAUI_DOCK_BOTTOM:
2291 newOrientation = wxHORIZONTAL;
2292 break;
2293 case wxAUI_DOCK_LEFT:
2294 case wxAUI_DOCK_RIGHT:
2295 newOrientation = wxVERTICAL;
2296 break;
2297 default:
2298 wxFAIL_MSG("invalid dock location value");
2299 }
2300 }
2301 else if (pane.IsResizable() &&
2302 GetOrientation(m_style) == wxBOTH)
2303 {
2304 // changing orientation in OnSize causes havoc
2305 int x, y;
2306 GetClientSize(&x, &y);
2307
2308 if (x > y)
2309 {
2310 newOrientation = wxHORIZONTAL;
2311 }
2312 else
2313 {
2314 newOrientation = wxVERTICAL;
2315 }
2316 }
2317 if (newOrientation != m_orientation)
2318 {
2319 SetOrientation(newOrientation);
2320 Realize();
2321 if (newOrientation == wxHORIZONTAL)
2322 {
2323 pane.best_size = GetHintSize(wxAUI_DOCK_TOP);
2324 }
2325 else
2326 {
2327 pane.best_size = GetHintSize(wxAUI_DOCK_LEFT);
2328 }
2329 if (pane.IsDocked())
2330 {
2331 pane.floating_size = wxDefaultSize;
2332 }
2333 else
2334 {
2335 SetSize(GetParent()->GetClientSize());
2336 }
2337 manager->Update();
2338 }
2339 }
2340 }
2341
2342 DoIdleUpdate();
2343 evt.Skip();
2344 }
2345
2346 void wxAuiToolBar::OnPaint(wxPaintEvent& WXUNUSED(evt))
2347 {
2348 wxAutoBufferedPaintDC dc(this);
2349 wxRect cli_rect(wxPoint(0,0), GetClientSize());
2350
2351
2352 bool horizontal = m_orientation == wxHORIZONTAL;
2353
2354
2355 m_art->DrawBackground(dc, this, cli_rect);
2356
2357 int gripperSize = m_art->GetElementSize(wxAUI_TBART_GRIPPER_SIZE);
2358 int dropdown_size = m_art->GetElementSize(wxAUI_TBART_OVERFLOW_SIZE);
2359
2360 // paint the gripper
2361 if (gripperSize > 0 && m_gripperSizerItem)
2362 {
2363 wxRect gripper_rect = m_gripperSizerItem->GetRect();
2364 if (horizontal)
2365 gripper_rect.width = gripperSize;
2366 else
2367 gripper_rect.height = gripperSize;
2368 m_art->DrawGripper(dc, this, gripper_rect);
2369 }
2370
2371 // calculated how far we can draw items
2372 int last_extent;
2373 if (horizontal)
2374 last_extent = cli_rect.width;
2375 else
2376 last_extent = cli_rect.height;
2377 if (m_overflowVisible)
2378 last_extent -= dropdown_size;
2379
2380 // paint each individual tool
2381 size_t i, count = m_items.GetCount();
2382 for (i = 0; i < count; ++i)
2383 {
2384 wxAuiToolBarItem& item = m_items.Item(i);
2385
2386 if (!item.m_sizerItem)
2387 continue;
2388
2389 wxRect item_rect = item.m_sizerItem->GetRect();
2390
2391
2392 if ((horizontal && item_rect.x + item_rect.width >= last_extent) ||
2393 (!horizontal && item_rect.y + item_rect.height >= last_extent))
2394 {
2395 break;
2396 }
2397
2398 if (item.m_kind == wxITEM_SEPARATOR)
2399 {
2400 // draw a separator
2401 m_art->DrawSeparator(dc, this, item_rect);
2402 }
2403 else if (item.m_kind == wxITEM_LABEL)
2404 {
2405 // draw a text label only
2406 m_art->DrawLabel(dc, this, item, item_rect);
2407 }
2408 else if (item.m_kind == wxITEM_NORMAL)
2409 {
2410 // draw a regular button or dropdown button
2411 if (!item.m_dropDown)
2412 m_art->DrawButton(dc, this, item, item_rect);
2413 else
2414 m_art->DrawDropDownButton(dc, this, item, item_rect);
2415 }
2416 else if (item.m_kind == wxITEM_CHECK)
2417 {
2418 // draw either a regular or dropdown toggle button
2419 if (!item.m_dropDown)
2420 m_art->DrawButton(dc, this, item, item_rect);
2421 else
2422 m_art->DrawDropDownButton(dc, this, item, item_rect);
2423 }
2424 else if (item.m_kind == wxITEM_RADIO)
2425 {
2426 // draw a toggle button
2427 m_art->DrawButton(dc, this, item, item_rect);
2428 }
2429 else if (item.m_kind == wxITEM_CONTROL)
2430 {
2431 // draw the control's label
2432 m_art->DrawControlLabel(dc, this, item, item_rect);
2433 }
2434
2435 // fire a signal to see if the item wants to be custom-rendered
2436 OnCustomRender(dc, item, item_rect);
2437 }
2438
2439 // paint the overflow button
2440 if (dropdown_size > 0 && m_overflowSizerItem)
2441 {
2442 wxRect dropDownRect = GetOverflowRect();
2443 m_art->DrawOverflowButton(dc, this, dropDownRect, m_overflowState);
2444 }
2445 }
2446
2447 void wxAuiToolBar::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
2448 {
2449 // empty
2450 }
2451
2452 void wxAuiToolBar::OnLeftDown(wxMouseEvent& evt)
2453 {
2454 wxRect cli_rect(wxPoint(0,0), GetClientSize());
2455
2456 if (m_gripperSizerItem)
2457 {
2458 wxRect gripper_rect = m_gripperSizerItem->GetRect();
2459 if (gripper_rect.Contains(evt.GetX(), evt.GetY()))
2460 {
2461 // find aui manager
2462 wxAuiManager* manager = wxAuiManager::GetManager(this);
2463 if (!manager)
2464 return;
2465
2466 int x_drag_offset = evt.GetX() - gripper_rect.GetX();
2467 int y_drag_offset = evt.GetY() - gripper_rect.GetY();
2468
2469 // gripper was clicked
2470 manager->StartPaneDrag(this, wxPoint(x_drag_offset, y_drag_offset));
2471 return;
2472 }
2473 }
2474
2475 if (m_overflowSizerItem)
2476 {
2477 wxRect overflow_rect = GetOverflowRect();
2478
2479 if (m_art &&
2480 m_overflowVisible &&
2481 overflow_rect.Contains(evt.m_x, evt.m_y))
2482 {
2483 wxAuiToolBarEvent e(wxEVT_COMMAND_AUITOOLBAR_OVERFLOW_CLICK, -1);
2484 e.SetEventObject(this);
2485 e.SetToolId(-1);
2486 e.SetClickPoint(wxPoint(evt.GetX(), evt.GetY()));
2487 bool processed = GetEventHandler()->ProcessEvent(e);
2488
2489 if (processed)
2490 {
2491 DoIdleUpdate();
2492 }
2493 else
2494 {
2495 size_t i, count;
2496 wxAuiToolBarItemArray overflow_items;
2497
2498
2499 // add custom overflow prepend items, if any
2500 count = m_customOverflowPrepend.GetCount();
2501 for (i = 0; i < count; ++i)
2502 overflow_items.Add(m_customOverflowPrepend[i]);
2503
2504 // only show items that don't fit in the dropdown
2505 count = m_items.GetCount();
2506 for (i = 0; i < count; ++i)
2507 {
2508 if (!GetToolFitsByIndex(i))
2509 overflow_items.Add(m_items[i]);
2510 }
2511
2512 // add custom overflow append items, if any
2513 count = m_customOverflowAppend.GetCount();
2514 for (i = 0; i < count; ++i)
2515 overflow_items.Add(m_customOverflowAppend[i]);
2516
2517 int res = m_art->ShowDropDown(this, overflow_items);
2518 m_overflowState = 0;
2519 Refresh(false);
2520 if (res != -1)
2521 {
2522 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, res);
2523 e.SetEventObject(this);
2524 GetParent()->GetEventHandler()->ProcessEvent(e);
2525 }
2526 }
2527
2528 return;
2529 }
2530 }
2531
2532 m_dragging = false;
2533 m_actionPos = wxPoint(evt.GetX(), evt.GetY());
2534 m_actionItem = FindToolByPosition(evt.GetX(), evt.GetY());
2535
2536 if (m_actionItem)
2537 {
2538 if (m_actionItem->m_state & wxAUI_BUTTON_STATE_DISABLED)
2539 {
2540 m_actionPos = wxPoint(-1,-1);
2541 m_actionItem = NULL;
2542 return;
2543 }
2544
2545 UnsetToolTip();
2546
2547 // fire the tool dropdown event
2548 wxAuiToolBarEvent e(wxEVT_COMMAND_AUITOOLBAR_TOOL_DROPDOWN, m_actionItem->m_toolId);
2549 e.SetEventObject(this);
2550 e.SetToolId(m_actionItem->m_toolId);
2551
2552 int mouse_x = evt.GetX();
2553 wxRect rect = m_actionItem->m_sizerItem->GetRect();
2554 const bool dropDownHit = m_actionItem->m_dropDown &&
2555 mouse_x >= (rect.x+rect.width-BUTTON_DROPDOWN_WIDTH-1) &&
2556 mouse_x < (rect.x+rect.width);
2557 e.SetDropDownClicked(dropDownHit);
2558
2559 e.SetClickPoint(evt.GetPosition());
2560 e.SetItemRect(rect);
2561
2562 // we only set the 'pressed button' state if we hit the actual button
2563 // and not just the drop-down
2564 SetPressedItem(dropDownHit ? 0 : m_actionItem);
2565
2566 if(dropDownHit)
2567 {
2568 m_actionPos = wxPoint(-1,-1);
2569 m_actionItem = NULL;
2570 }
2571
2572 if(!GetEventHandler()->ProcessEvent(e) || e.GetSkipped())
2573 CaptureMouse();
2574
2575 DoIdleUpdate();
2576 }
2577 }
2578
2579 void wxAuiToolBar::OnLeftUp(wxMouseEvent& evt)
2580 {
2581 if (!HasCapture())
2582 return;
2583
2584 SetPressedItem(NULL);
2585
2586 wxAuiToolBarItem* hitItem = FindToolByPosition(evt.GetX(), evt.GetY());
2587 if (hitItem && !(hitItem->m_state & wxAUI_BUTTON_STATE_DISABLED))
2588 {
2589 SetHoverItem(hitItem);
2590 }
2591
2592 if (m_dragging)
2593 {
2594 // TODO: it would make sense to send out an 'END_DRAG' event here,
2595 // otherwise a client would never know what to do with the 'BEGIN_DRAG'
2596 // event
2597
2598 // OnCaptureLost() will be called now and this will reset all our state
2599 // tracking variables
2600 ReleaseMouse();
2601 }
2602 else
2603 {
2604 if (m_actionItem && hitItem == m_actionItem)
2605 {
2606 UnsetToolTip();
2607
2608 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, m_actionItem->m_toolId);
2609 e.SetEventObject(this);
2610
2611 if (hitItem->m_kind == wxITEM_CHECK || hitItem->m_kind == wxITEM_RADIO)
2612 {
2613 const bool toggle = !(m_actionItem->m_state & wxAUI_BUTTON_STATE_CHECKED);
2614
2615 ToggleTool(m_actionItem->m_toolId, toggle);
2616
2617 // repaint immediately
2618 Refresh(false);
2619 Update();
2620
2621 e.SetInt(toggle);
2622 }
2623
2624 // we have to release the mouse *before* sending the event, because
2625 // we don't know what a handler might do. It could open up a popup
2626 // menu for example and that would make us lose our capture anyway.
2627
2628 ReleaseMouse();
2629
2630 GetEventHandler()->ProcessEvent(e);
2631 DoIdleUpdate();
2632 }
2633 else
2634 ReleaseMouse();
2635 }
2636 }
2637
2638 void wxAuiToolBar::OnRightDown(wxMouseEvent& evt)
2639 {
2640 wxRect cli_rect(wxPoint(0,0), GetClientSize());
2641
2642 if (m_gripperSizerItem)
2643 {
2644 wxRect gripper_rect = m_gripperSizerItem->GetRect();
2645 if (gripper_rect.Contains(evt.GetX(), evt.GetY()))
2646 return;
2647 }
2648
2649 if (m_overflowSizerItem)
2650 {
2651 int dropdown_size = m_art->GetElementSize(wxAUI_TBART_OVERFLOW_SIZE);
2652 if (dropdown_size > 0 &&
2653 evt.m_x > cli_rect.width - dropdown_size &&
2654 evt.m_y >= 0 &&
2655 evt.m_y < cli_rect.height &&
2656 m_art)
2657 {
2658 return;
2659 }
2660 }
2661
2662 m_actionPos = wxPoint(evt.GetX(), evt.GetY());
2663 m_actionItem = FindToolByPosition(evt.GetX(), evt.GetY());
2664
2665 if (m_actionItem && m_actionItem->m_state & wxAUI_BUTTON_STATE_DISABLED)
2666 {
2667 m_actionPos = wxPoint(-1,-1);
2668 m_actionItem = NULL;
2669 return;
2670 }
2671
2672 UnsetToolTip();
2673 }
2674
2675 void wxAuiToolBar::OnRightUp(wxMouseEvent& evt)
2676 {
2677 wxAuiToolBarItem* hitItem;
2678 hitItem = FindToolByPosition(evt.GetX(), evt.GetY());
2679
2680 if (m_actionItem && hitItem == m_actionItem)
2681 {
2682 wxAuiToolBarEvent e(wxEVT_COMMAND_AUITOOLBAR_RIGHT_CLICK, m_actionItem->m_toolId);
2683 e.SetEventObject(this);
2684 e.SetToolId(m_actionItem->m_toolId);
2685 e.SetClickPoint(m_actionPos);
2686 GetEventHandler()->ProcessEvent(e);
2687 DoIdleUpdate();
2688 }
2689 else
2690 {
2691 // right-clicked on the invalid area of the toolbar
2692 wxAuiToolBarEvent e(wxEVT_COMMAND_AUITOOLBAR_RIGHT_CLICK, -1);
2693 e.SetEventObject(this);
2694 e.SetToolId(-1);
2695 e.SetClickPoint(m_actionPos);
2696 GetEventHandler()->ProcessEvent(e);
2697 DoIdleUpdate();
2698 }
2699
2700 // reset member variables
2701 m_actionPos = wxPoint(-1,-1);
2702 m_actionItem = NULL;
2703 }
2704
2705 void wxAuiToolBar::OnMiddleDown(wxMouseEvent& evt)
2706 {
2707 wxRect cli_rect(wxPoint(0,0), GetClientSize());
2708
2709 if (m_gripperSizerItem)
2710 {
2711 wxRect gripper_rect = m_gripperSizerItem->GetRect();
2712 if (gripper_rect.Contains(evt.GetX(), evt.GetY()))
2713 return;
2714 }
2715
2716 if (m_overflowSizerItem)
2717 {
2718 int dropdown_size = m_art->GetElementSize(wxAUI_TBART_OVERFLOW_SIZE);
2719 if (dropdown_size > 0 &&
2720 evt.m_x > cli_rect.width - dropdown_size &&
2721 evt.m_y >= 0 &&
2722 evt.m_y < cli_rect.height &&
2723 m_art)
2724 {
2725 return;
2726 }
2727 }
2728
2729 m_actionPos = wxPoint(evt.GetX(), evt.GetY());
2730 m_actionItem = FindToolByPosition(evt.GetX(), evt.GetY());
2731
2732 if (m_actionItem)
2733 {
2734 if (m_actionItem->m_state & wxAUI_BUTTON_STATE_DISABLED)
2735 {
2736 m_actionPos = wxPoint(-1,-1);
2737 m_actionItem = NULL;
2738 return;
2739 }
2740 }
2741
2742 UnsetToolTip();
2743 }
2744
2745 void wxAuiToolBar::OnMiddleUp(wxMouseEvent& evt)
2746 {
2747 wxAuiToolBarItem* hitItem;
2748 hitItem = FindToolByPosition(evt.GetX(), evt.GetY());
2749
2750 if (m_actionItem && hitItem == m_actionItem)
2751 {
2752 if (hitItem->m_kind == wxITEM_NORMAL)
2753 {
2754 wxAuiToolBarEvent e(wxEVT_COMMAND_AUITOOLBAR_MIDDLE_CLICK, m_actionItem->m_toolId);
2755 e.SetEventObject(this);
2756 e.SetToolId(m_actionItem->m_toolId);
2757 e.SetClickPoint(m_actionPos);
2758 GetEventHandler()->ProcessEvent(e);
2759 DoIdleUpdate();
2760 }
2761 }
2762
2763 // reset member variables
2764 m_actionPos = wxPoint(-1,-1);
2765 m_actionItem = NULL;
2766 }
2767
2768 void wxAuiToolBar::OnMotion(wxMouseEvent& evt)
2769 {
2770 const bool button_pressed = HasCapture();
2771
2772 // start a drag event
2773 if (!m_dragging && button_pressed &&
2774 abs(evt.GetX() - m_actionPos.x) + abs(evt.GetY() - m_actionPos.y) > 5)
2775 {
2776 // TODO: sending this event only makes sense if there is an 'END_DRAG'
2777 // event sent sometime in the future (see OnLeftUp())
2778 wxAuiToolBarEvent e(wxEVT_COMMAND_AUITOOLBAR_BEGIN_DRAG, GetId());
2779 e.SetEventObject(this);
2780 e.SetToolId(m_actionItem->m_toolId);
2781 m_dragging = GetEventHandler()->ProcessEvent(e) && !e.GetSkipped();
2782
2783 DoIdleUpdate();
2784 }
2785
2786 if(m_dragging)
2787 return;
2788
2789 wxAuiToolBarItem* hitItem = FindToolByPosition(evt.GetX(), evt.GetY());
2790 if(button_pressed)
2791 {
2792 // if we have a button pressed we want it to be shown in 'depressed'
2793 // state unless we move the mouse outside the button, then we want it
2794 // to show as just 'highlighted'
2795 if (hitItem == m_actionItem)
2796 SetPressedItem(m_actionItem);
2797 else
2798 {
2799 SetPressedItem(NULL);
2800 SetHoverItem(m_actionItem);
2801 }
2802 }
2803 else
2804 {
2805 if (hitItem && (hitItem->m_state & wxAUI_BUTTON_STATE_DISABLED))
2806 SetHoverItem(NULL);
2807 else
2808 SetHoverItem(hitItem);
2809
2810 // tooltips handling
2811 wxAuiToolBarItem* packingHitItem;
2812 packingHitItem = FindToolByPositionWithPacking(evt.GetX(), evt.GetY());
2813 if (packingHitItem)
2814 {
2815 if (packingHitItem != m_tipItem)
2816 {
2817 m_tipItem = packingHitItem;
2818
2819 if ( !packingHitItem->m_shortHelp.empty() )
2820 SetToolTip(packingHitItem->m_shortHelp);
2821 else
2822 UnsetToolTip();
2823 }
2824 }
2825 else
2826 {
2827 UnsetToolTip();
2828 m_tipItem = NULL;
2829 }
2830
2831 // figure out the dropdown button state (are we hovering or pressing it?)
2832 RefreshOverflowState();
2833 }
2834 }
2835
2836 void wxAuiToolBar::DoResetMouseState()
2837 {
2838 RefreshOverflowState();
2839 SetHoverItem(NULL);
2840 SetPressedItem(NULL);
2841
2842 m_tipItem = NULL;
2843
2844 // we have to reset those here, because the mouse-up handlers which do
2845 // it usually won't be called if we let go of a mouse button while we
2846 // are outside of the window
2847 m_actionPos = wxPoint(-1,-1);
2848 m_actionItem = NULL;
2849 }
2850
2851 void wxAuiToolBar::OnLeaveWindow(wxMouseEvent& evt)
2852 {
2853 if(HasCapture())
2854 {
2855 evt.Skip();
2856 return;
2857 }
2858
2859 DoResetMouseState();
2860 }
2861
2862 void wxAuiToolBar::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(evt))
2863 {
2864 m_dragging = false;
2865
2866 DoResetMouseState();
2867 }
2868
2869 void wxAuiToolBar::OnSetCursor(wxSetCursorEvent& evt)
2870 {
2871 wxCursor cursor = wxNullCursor;
2872
2873 if (m_gripperSizerItem)
2874 {
2875 wxRect gripper_rect = m_gripperSizerItem->GetRect();
2876 if (gripper_rect.Contains(evt.GetX(), evt.GetY()))
2877 {
2878 cursor = wxCursor(wxCURSOR_SIZING);
2879 }
2880 }
2881
2882 evt.SetCursor(cursor);
2883 }
2884
2885
2886 #endif // wxUSE_AUI
2887