]> git.saurik.com Git - wxWidgets.git/blame - src/ribbon/buttonbar.cpp
Call Dismiss rather than Show(false) on the infobar to correct the layout.
[wxWidgets.git] / src / ribbon / buttonbar.cpp
CommitLineData
3c3ead1d
PC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/ribbon/buttonbar.cpp
3// Purpose: Ribbon control similar to a tool bar
4// Author: Peter Cawley
5// Modified by:
6// Created: 2009-07-01
7// RCS-ID: $Id$
8// Copyright: (C) Peter Cawley
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
3c3ead1d
PC
18#if wxUSE_RIBBON
19
4cf018e1 20#include "wx/ribbon/buttonbar.h"
3c3ead1d
PC
21#include "wx/ribbon/art.h"
22#include "wx/dcbuffer.h"
23
24#ifndef WX_PRECOMP
25#endif
26
27#ifdef __WXMSW__
28#include "wx/msw/private.h"
29#endif
30
31wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBUTTON_CLICKED, wxRibbonButtonBarEvent);
32wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBUTTON_DROPDOWN_CLICKED, wxRibbonButtonBarEvent);
33
34IMPLEMENT_DYNAMIC_CLASS(wxRibbonButtonBarEvent, wxCommandEvent)
35IMPLEMENT_CLASS(wxRibbonButtonBar, wxRibbonControl)
36
37BEGIN_EVENT_TABLE(wxRibbonButtonBar, wxRibbonControl)
38 EVT_ERASE_BACKGROUND(wxRibbonButtonBar::OnEraseBackground)
39 EVT_ENTER_WINDOW(wxRibbonButtonBar::OnMouseEnter)
40 EVT_LEAVE_WINDOW(wxRibbonButtonBar::OnMouseLeave)
41 EVT_MOTION(wxRibbonButtonBar::OnMouseMove)
42 EVT_PAINT(wxRibbonButtonBar::OnPaint)
43 EVT_SIZE(wxRibbonButtonBar::OnSize)
44 EVT_LEFT_DOWN(wxRibbonButtonBar::OnMouseDown)
45 EVT_LEFT_UP(wxRibbonButtonBar::OnMouseUp)
46END_EVENT_TABLE()
47
48class wxRibbonButtonBarButtonSizeInfo
49{
50public:
51 bool is_supported;
52 wxSize size;
53 wxRect normal_region;
54 wxRect dropdown_region;
55};
56
57class wxRibbonButtonBarButtonInstance
58{
59public:
60 wxPoint position;
61 wxRibbonButtonBarButtonBase* base;
62 wxRibbonButtonBarButtonState size;
63};
64
65class wxRibbonButtonBarButtonBase
66{
67public:
68 wxRibbonButtonBarButtonInstance NewInstance()
69 {
70 wxRibbonButtonBarButtonInstance i;
71 i.base = this;
72 return i;
73 }
74
75 wxRibbonButtonBarButtonState GetLargestSize()
76 {
77 if(sizes[wxRIBBON_BUTTONBAR_BUTTON_LARGE].is_supported)
78 return wxRIBBON_BUTTONBAR_BUTTON_LARGE;
79 if(sizes[wxRIBBON_BUTTONBAR_BUTTON_MEDIUM].is_supported)
80 return wxRIBBON_BUTTONBAR_BUTTON_MEDIUM;
81 wxASSERT(sizes[wxRIBBON_BUTTONBAR_BUTTON_SMALL].is_supported);
82 return wxRIBBON_BUTTONBAR_BUTTON_SMALL;
83 }
84
85 bool GetSmallerSize(
86 wxRibbonButtonBarButtonState* size, int n = 1)
87 {
88 for(; n > 0; --n)
89 {
90 switch(*size)
91 {
92 case wxRIBBON_BUTTONBAR_BUTTON_LARGE:
93 if(sizes[wxRIBBON_BUTTONBAR_BUTTON_MEDIUM].is_supported)
94 {
95 *size = wxRIBBON_BUTTONBAR_BUTTON_MEDIUM;
96 break;
97 }
98 case wxRIBBON_BUTTONBAR_BUTTON_MEDIUM:
99 if(sizes[wxRIBBON_BUTTONBAR_BUTTON_SMALL].is_supported)
100 {
101 *size = wxRIBBON_BUTTONBAR_BUTTON_SMALL;
102 break;
103 }
104 case wxRIBBON_BUTTONBAR_BUTTON_SMALL:
105 default:
106 return false;
107 }
108 }
109 return true;
110 }
111
112 wxString label;
113 wxString help_string;
114 wxBitmap bitmap_large;
115 wxBitmap bitmap_large_disabled;
116 wxBitmap bitmap_small;
117 wxBitmap bitmap_small_disabled;
118 wxRibbonButtonBarButtonSizeInfo sizes[3];
119 wxObject* client_data;
120 int id;
121 wxRibbonButtonKind kind;
122 long state;
123};
124
125WX_DECLARE_OBJARRAY(wxRibbonButtonBarButtonInstance, wxArrayRibbonButtonBarButtonInstance);
14371273 126#include "wx/arrimpl.cpp"
65391c8f 127WX_DEFINE_OBJARRAY(wxArrayRibbonButtonBarButtonInstance)
3c3ead1d
PC
128
129class wxRibbonButtonBarLayout
130{
131public:
132 wxSize overall_size;
133 wxArrayRibbonButtonBarButtonInstance buttons;
134
135 void CalculateOverallSize()
136 {
137 overall_size = wxSize(0, 0);
138 size_t btn_count = buttons.Count();
139 size_t btn_i;
140 for(btn_i = 0; btn_i < btn_count; ++btn_i)
141 {
142 wxRibbonButtonBarButtonInstance& instance = buttons.Item(btn_i);
143 wxSize size = instance.base->sizes[instance.size].size;
144 int right = instance.position.x + size.GetWidth();
145 int bottom = instance.position.y + size.GetHeight();
146 if(right > overall_size.GetWidth())
147 {
148 overall_size.SetWidth(right);
149 }
150 if(bottom > overall_size.GetHeight())
151 {
152 overall_size.SetHeight(bottom);
153 }
154 }
155 }
156
157 wxRibbonButtonBarButtonInstance* FindSimilarInstance(
158 wxRibbonButtonBarButtonInstance* inst)
159 {
160 if(inst == NULL)
161 {
162 return NULL;
163 }
164 size_t btn_count = buttons.Count();
165 size_t btn_i;
166 for(btn_i = 0; btn_i < btn_count; ++btn_i)
167 {
168 wxRibbonButtonBarButtonInstance& instance = buttons.Item(btn_i);
169 if(instance.base == inst->base)
170 {
171 return &instance;
172 }
173 }
174 return NULL;
175 }
176};
177
178wxRibbonButtonBar::wxRibbonButtonBar()
179{
180 m_layouts_valid = false;
fd6e1597 181 CommonInit (0);
3c3ead1d
PC
182}
183
184wxRibbonButtonBar::wxRibbonButtonBar(wxWindow* parent,
185 wxWindowID id,
186 const wxPoint& pos,
187 const wxSize& size,
188 long style)
189 : wxRibbonControl(parent, id, pos, size, wxBORDER_NONE)
190{
191 m_layouts_valid = false;
192
193 CommonInit(style);
194}
195
196wxRibbonButtonBar::~wxRibbonButtonBar()
197{
198 size_t count = m_buttons.GetCount();
199 size_t i;
200 for(i = 0; i < count; ++i)
201 {
202 wxRibbonButtonBarButtonBase* button = m_buttons.Item(i);
203 delete button;
204 }
205 m_buttons.Clear();
206
207 count = m_layouts.GetCount();
208 for(i = 0; i < count; ++i)
209 {
210 wxRibbonButtonBarLayout* layout = m_layouts.Item(i);
211 delete layout;
212 }
213 m_layouts.Clear();
214}
215
216bool wxRibbonButtonBar::Create(wxWindow* parent,
217 wxWindowID id,
218 const wxPoint& pos,
219 const wxSize& size,
220 long style)
221{
222 if(!wxRibbonControl::Create(parent, id, pos, size, wxBORDER_NONE))
223 {
224 return false;
225 }
226
227 CommonInit(style);
228 return true;
229}
230
231wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddButton(
232 int button_id,
233 const wxString& label,
234 const wxBitmap& bitmap,
235 const wxString& help_string,
236 wxRibbonButtonKind kind)
237{
238 return AddButton(button_id, label, bitmap, wxNullBitmap, wxNullBitmap,
239 wxNullBitmap, kind, help_string);
240}
241
242wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddDropdownButton(
243 int button_id,
244 const wxString& label,
245 const wxBitmap& bitmap,
246 const wxString& help_string)
247{
248 return AddButton(button_id, label, bitmap, help_string,
249 wxRIBBON_BUTTON_DROPDOWN);
250}
251
955bad41
PC
252wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddToggleButton(
253 int button_id,
254 const wxString& label,
255 const wxBitmap& bitmap,
256 const wxString& help_string)
257{
258 return AddButton(button_id, label, bitmap, help_string,
259 wxRIBBON_BUTTON_TOGGLE);
260}
261
3c3ead1d
PC
262wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddHybridButton(
263 int button_id,
264 const wxString& label,
265 const wxBitmap& bitmap,
266 const wxString& help_string)
267{
268 return AddButton(button_id, label, bitmap, help_string,
269 wxRIBBON_BUTTON_HYBRID);
270}
ce00f59b 271
3c3ead1d
PC
272wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddButton(
273 int button_id,
274 const wxString& label,
275 const wxBitmap& bitmap,
276 const wxBitmap& bitmap_small,
277 const wxBitmap& bitmap_disabled,
278 const wxBitmap& bitmap_small_disabled,
279 wxRibbonButtonKind kind,
280 const wxString& help_string,
281 wxObject* client_data)
282{
283 wxASSERT(bitmap.IsOk() || bitmap_small.IsOk());
284 if(m_buttons.IsEmpty())
285 {
286 if(bitmap.IsOk())
287 {
288 m_bitmap_size_large = bitmap.GetSize();
289 if(!bitmap_small.IsOk())
290 {
291 m_bitmap_size_small = m_bitmap_size_large;
292 m_bitmap_size_small *= 0.5;
293 }
294 }
295 if(bitmap_small.IsOk())
296 {
297 m_bitmap_size_small = bitmap_small.GetSize();
298 if(!bitmap.IsOk())
299 {
300 m_bitmap_size_large = m_bitmap_size_small;
301 m_bitmap_size_large *= 2.0;
302 }
303 }
304 }
305
306 wxRibbonButtonBarButtonBase* base = new wxRibbonButtonBarButtonBase;
307 base->id = button_id;
308 base->label = label;
309 base->bitmap_large = bitmap;
310 if(!base->bitmap_large.IsOk())
311 {
312 base->bitmap_large = MakeResizedBitmap(base->bitmap_small,
313 m_bitmap_size_large);
314 }
315 else if(base->bitmap_large.GetSize() != m_bitmap_size_large)
316 {
317 base->bitmap_large = MakeResizedBitmap(base->bitmap_large,
318 m_bitmap_size_large);
319 }
320 base->bitmap_small = bitmap_small;
321 if(!base->bitmap_small.IsOk())
322 {
323 base->bitmap_small = MakeResizedBitmap(base->bitmap_large,
324 m_bitmap_size_small);
325 }
326 else if(base->bitmap_small.GetSize() != m_bitmap_size_small)
327 {
328 base->bitmap_small = MakeResizedBitmap(base->bitmap_small,
329 m_bitmap_size_small);
330 }
331 base->bitmap_large_disabled = bitmap_disabled;
332 if(!base->bitmap_large_disabled.IsOk())
333 {
334 base->bitmap_large_disabled = MakeDisabledBitmap(base->bitmap_large);
335 }
336 base->bitmap_small_disabled = bitmap_small_disabled;
337 if(!base->bitmap_small_disabled.IsOk())
338 {
339 base->bitmap_small_disabled = MakeDisabledBitmap(base->bitmap_small);
340 }
341 base->kind = kind;
342 base->help_string = help_string;
343 base->client_data = client_data;
344 base->state = 0;
345
089ca539 346 wxClientDC temp_dc(this);
3c3ead1d
PC
347 FetchButtonSizeInfo(base, wxRIBBON_BUTTONBAR_BUTTON_SMALL, temp_dc);
348 FetchButtonSizeInfo(base, wxRIBBON_BUTTONBAR_BUTTON_MEDIUM, temp_dc);
349 FetchButtonSizeInfo(base, wxRIBBON_BUTTONBAR_BUTTON_LARGE, temp_dc);
350
351 // TODO
352 m_buttons.Add(base);
353 m_layouts_valid = false;
354 return base;
355}
356
357void wxRibbonButtonBar::FetchButtonSizeInfo(wxRibbonButtonBarButtonBase* button,
358 wxRibbonButtonBarButtonState size, wxDC& dc)
359{
360 wxRibbonButtonBarButtonSizeInfo& info = button->sizes[size];
361 if(m_art)
362 {
363 info.is_supported = m_art->GetButtonBarButtonSize(dc, this,
364 button->kind, size, button->label, m_bitmap_size_large,
365 m_bitmap_size_small, &info.size, &info.normal_region,
366 &info.dropdown_region);
367 }
368 else
369 info.is_supported = false;
370}
371
372wxBitmap wxRibbonButtonBar::MakeResizedBitmap(const wxBitmap& original, wxSize size)
373{
374 wxImage img(original.ConvertToImage());
375 img.Rescale(size.GetWidth(), size.GetHeight(), wxIMAGE_QUALITY_HIGH);
376 return wxBitmap(img);
377}
378
379wxBitmap wxRibbonButtonBar::MakeDisabledBitmap(const wxBitmap& original)
380{
381 wxImage img(original.ConvertToImage());
382 return wxBitmap(img.ConvertToGreyscale());
383}
384
385bool wxRibbonButtonBar::Realize()
386{
387 if(!m_layouts_valid)
388 {
389 MakeLayouts();
390 m_layouts_valid = true;
391 }
392 return true;
393}
394
395void wxRibbonButtonBar::ClearButtons()
396{
397 m_layouts_valid = false;
398 size_t count = m_buttons.GetCount();
399 size_t i;
400 for(i = 0; i < count; ++i)
401 {
402 wxRibbonButtonBarButtonBase* button = m_buttons.Item(i);
403 delete button;
404 }
405 m_buttons.Clear();
406 Realize();
407}
408
409bool wxRibbonButtonBar::DeleteButton(int button_id)
410{
411 size_t count = m_buttons.GetCount();
412 size_t i;
413 for(i = 0; i < count; ++i)
414 {
415 wxRibbonButtonBarButtonBase* button = m_buttons.Item(i);
416 if(button->id == button_id)
417 {
418 m_layouts_valid = false;
419 m_buttons.RemoveAt(i);
420 Realize();
421 Refresh();
422 return true;
423 }
424 }
425 return false;
426}
427
428void wxRibbonButtonBar::EnableButton(int button_id, bool enable)
429{
430 size_t count = m_buttons.GetCount();
431 size_t i;
432 for(i = 0; i < count; ++i)
433 {
434 wxRibbonButtonBarButtonBase* button = m_buttons.Item(i);
435 if(button->id == button_id)
436 {
437 if(enable)
438 {
439 if(button->state & wxRIBBON_BUTTONBAR_BUTTON_DISABLED)
440 {
441 button->state &= ~wxRIBBON_BUTTONBAR_BUTTON_DISABLED;
442 Refresh();
443 }
444 }
445 else
446 {
447 if((button->state & wxRIBBON_BUTTONBAR_BUTTON_DISABLED) == 0)
448 {
449 button->state |= wxRIBBON_BUTTONBAR_BUTTON_DISABLED;
450 Refresh();
451 }
452 }
453 return;
454 }
455 }
456}
457
955bad41
PC
458void wxRibbonButtonBar::ToggleButton(int button_id, bool checked)
459{
460 size_t count = m_buttons.GetCount();
461 size_t i;
462 for(i = 0; i < count; ++i)
463 {
464 wxRibbonButtonBarButtonBase* button = m_buttons.Item(i);
465 if(button->id == button_id)
466 {
467 if(checked)
468 {
469 if((button->state & wxRIBBON_BUTTONBAR_BUTTON_TOGGLED) == 0)
470 {
471 button->state |= wxRIBBON_BUTTONBAR_BUTTON_TOGGLED;
472 Refresh();
473 }
474 }
475 else
476 {
477 if(button->state & wxRIBBON_BUTTONBAR_BUTTON_TOGGLED)
478 {
479 button->state &= ~wxRIBBON_BUTTONBAR_BUTTON_TOGGLED;
480 Refresh();
481 }
482 }
483 return;
484 }
485 }
486}
487
3c3ead1d
PC
488void wxRibbonButtonBar::SetArtProvider(wxRibbonArtProvider* art)
489{
490 if(art == m_art)
491 {
492 return;
493 }
494
495 wxRibbonControl::SetArtProvider(art);
496
089ca539 497 wxClientDC temp_dc(this);
3c3ead1d
PC
498 size_t btn_count = m_buttons.Count();
499 size_t btn_i;
500 for(btn_i = 0; btn_i < btn_count; ++btn_i)
501 {
502 wxRibbonButtonBarButtonBase* base = m_buttons.Item(btn_i);
503
504 FetchButtonSizeInfo(base, wxRIBBON_BUTTONBAR_BUTTON_SMALL, temp_dc);
505 FetchButtonSizeInfo(base, wxRIBBON_BUTTONBAR_BUTTON_MEDIUM, temp_dc);
506 FetchButtonSizeInfo(base, wxRIBBON_BUTTONBAR_BUTTON_LARGE, temp_dc);
507 }
508
509 m_layouts_valid = false;
510 Realize();
511}
512
513bool wxRibbonButtonBar::IsSizingContinuous() const
514{
515 return false;
516}
517
518wxSize wxRibbonButtonBar::DoGetNextSmallerSize(wxOrientation direction,
519 wxSize result) const
520{
521 size_t nlayouts = m_layouts.GetCount();
522 size_t i;
523 for(i = 0; i < nlayouts; ++i)
524 {
525 wxRibbonButtonBarLayout* layout = m_layouts.Item(i);
526 wxSize size = layout->overall_size;
527 switch(direction)
528 {
529 case wxHORIZONTAL:
530 if(size.x < result.x && size.y <= result.y)
531 {
532 result.x = size.x;
533 break;
534 }
535 else
536 continue;
537 case wxVERTICAL:
538 if(size.x <= result.x && size.y < result.y)
539 {
540 result.y = size.y;
541 break;
542 }
543 else
544 continue;
545 case wxBOTH:
546 if(size.x < result.x && size.y < result.y)
547 {
548 result = size;
549 break;
550 }
551 else
552 continue;
553 }
554 break;
555 }
556 return result;
557}
558
559wxSize wxRibbonButtonBar::DoGetNextLargerSize(wxOrientation direction,
560 wxSize result) const
561{
562 size_t nlayouts = m_layouts.GetCount();
563 size_t i = nlayouts;
564 while(i > 0)
565 {
566 --i;
567 wxRibbonButtonBarLayout* layout = m_layouts.Item(i);
568 wxSize size = layout->overall_size;
569 switch(direction)
570 {
571 case wxHORIZONTAL:
572 if(size.x > result.x && size.y <= result.y)
573 {
574 result.x = size.x;
575 break;
576 }
577 else
578 continue;
579 case wxVERTICAL:
580 if(size.x <= result.x && size.y > result.y)
581 {
582 result.y = size.y;
583 break;
584 }
585 else
586 continue;
587 case wxBOTH:
588 if(size.x > result.x && size.y > result.y)
589 {
590 result = size;
591 break;
592 }
593 else
594 continue;
595 }
596 break;
597 }
598 return result;
599}
600
601void wxRibbonButtonBar::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
602{
603 // All painting done in main paint handler to minimise flicker
604}
605
606void wxRibbonButtonBar::OnPaint(wxPaintEvent& WXUNUSED(evt))
607{
608 wxAutoBufferedPaintDC dc(this);
609 m_art->DrawButtonBarBackground(dc, this, GetSize());
610
611 wxRibbonButtonBarLayout* layout = m_layouts.Item(m_current_layout);
612
613 size_t btn_count = layout->buttons.Count();
614 size_t btn_i;
615 for(btn_i = 0; btn_i < btn_count; ++btn_i)
616 {
617 wxRibbonButtonBarButtonInstance& button = layout->buttons.Item(btn_i);
618 wxRibbonButtonBarButtonBase* base = button.base;
619
620 wxBitmap* bitmap = &base->bitmap_large;
621 wxBitmap* bitmap_small = &base->bitmap_small;
622 if(base->state & wxRIBBON_BUTTONBAR_BUTTON_DISABLED)
623 {
624 bitmap = &base->bitmap_large_disabled;
625 bitmap_small = &base->bitmap_small_disabled;
626 }
627 wxRect rect(button.position + m_layout_offset, base->sizes[button.size].size);
ce00f59b 628
3c3ead1d
PC
629 m_art->DrawButtonBarButton(dc, this, rect, base->kind,
630 base->state | button.size, base->label, *bitmap, *bitmap_small);
631 }
632}
633
634void wxRibbonButtonBar::OnSize(wxSizeEvent& evt)
635{
636 wxSize new_size = evt.GetSize();
637 size_t layout_count = m_layouts.GetCount();
638 size_t layout_i;
639 m_current_layout = layout_count - 1;
640 for(layout_i = 0; layout_i < layout_count; ++layout_i)
641 {
642 wxSize layout_size = m_layouts.Item(layout_i)->overall_size;
643 if(layout_size.x <= new_size.x && layout_size.y <= new_size.y)
644 {
645 m_layout_offset.x = (new_size.x - layout_size.x) / 2;
646 m_layout_offset.y = (new_size.y - layout_size.y) / 2;
647 m_current_layout = layout_i;
648 break;
649 }
650 }
651 m_hovered_button = m_layouts.Item(m_current_layout)->FindSimilarInstance(m_hovered_button);
652 Refresh();
653}
654
655void wxRibbonButtonBar::CommonInit(long WXUNUSED(style))
656{
657 m_bitmap_size_large = wxSize(32, 32);
658 m_bitmap_size_small = wxSize(16, 16);
659
660 wxRibbonButtonBarLayout* placeholder_layout = new wxRibbonButtonBarLayout;
661 placeholder_layout->overall_size = wxSize(20, 20);
662 m_layouts.Add(placeholder_layout);
663 m_current_layout = 0;
664 m_layout_offset = wxPoint(0, 0);
665 m_hovered_button = NULL;
666 m_active_button = NULL;
667 m_lock_active_state = false;
668
669 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
65391c8f 670}
3c3ead1d
PC
671
672wxSize wxRibbonButtonBar::GetMinSize() const
673{
674 return m_layouts.Last()->overall_size;
675}
676
677wxSize wxRibbonButtonBar::DoGetBestSize() const
678{
679 return m_layouts.Item(0)->overall_size;
680}
681
682void wxRibbonButtonBar::MakeLayouts()
683{
684 if(m_layouts_valid || m_art == NULL)
685 {
686 return;
687 }
688 {
689 // Clear existing layouts
690 if(m_hovered_button)
691 {
692 m_hovered_button->base->state &= ~wxRIBBON_BUTTONBAR_BUTTON_HOVER_MASK;
693 m_hovered_button = NULL;
694 }
695 if(m_active_button)
696 {
697 m_active_button->base->state &= ~wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK;
698 m_active_button = NULL;
699 }
700 size_t count = m_layouts.GetCount();
701 size_t i;
702 for(i = 0; i < count; ++i)
703 {
704 wxRibbonButtonBarLayout* layout = m_layouts.Item(i);
705 delete layout;
706 }
707 m_layouts.Clear();
708 }
709 size_t btn_count = m_buttons.Count();
710 size_t btn_i;
711 {
712 // Best layout : all buttons large, stacking horizontally
713 wxRibbonButtonBarLayout* layout = new wxRibbonButtonBarLayout;
714 wxPoint cursor(0, 0);
715 layout->overall_size.SetHeight(0);
716 for(btn_i = 0; btn_i < btn_count; ++btn_i)
717 {
718 wxRibbonButtonBarButtonBase* button = m_buttons.Item(btn_i);
719 wxRibbonButtonBarButtonInstance instance = button->NewInstance();
720 instance.position = cursor;
721 instance.size = button->GetLargestSize();
722 wxSize& size = button->sizes[instance.size].size;
723 cursor.x += size.GetWidth();
724 layout->overall_size.SetHeight(wxMax(layout->overall_size.GetHeight(),
725 size.GetHeight()));
726 layout->buttons.Add(instance);
727 }
728 layout->overall_size.SetWidth(cursor.x);
729 m_layouts.Add(layout);
730 }
731 if(btn_count >= 2)
732 {
733 // Collapse the rightmost buttons and stack them vertically
734 size_t iLast = btn_count - 1;
735 while(TryCollapseLayout(m_layouts.Last(), iLast, &iLast) && iLast > 0)
736 {
737 --iLast;
738 }
739 }
740}
741
742bool wxRibbonButtonBar::TryCollapseLayout(wxRibbonButtonBarLayout* original,
743 size_t first_btn, size_t* last_button)
744{
745 size_t btn_count = m_buttons.Count();
746 size_t btn_i;
747 int used_height = 0;
748 int used_width = 0;
749 int available_width = 0;
750 int available_height = 0;
751
752 for(btn_i = first_btn + 1; btn_i > 0; /* decrement is inside loop */)
753 {
754 --btn_i;
755 wxRibbonButtonBarButtonBase* button = m_buttons.Item(btn_i);
756 wxRibbonButtonBarButtonState large_size_class = button->GetLargestSize();
757 wxSize large_size = button->sizes[large_size_class].size;
758 int t_available_height = wxMax(available_height,
759 large_size.GetHeight());
760 int t_available_width = available_width + large_size.GetWidth();
761 wxRibbonButtonBarButtonState small_size_class = large_size_class;
762 if(!button->GetSmallerSize(&small_size_class))
763 {
764 return false;
765 }
766 wxSize small_size = button->sizes[small_size_class].size;
767 int t_used_height = used_height + small_size.GetHeight();
768 int t_used_width = wxMax(used_width, small_size.GetWidth());
769
770 if(t_used_height > t_available_height)
771 {
772 ++btn_i;
773 break;
774 }
775 else
776 {
777 used_height = t_used_height;
778 used_width = t_used_width;
779 available_width = t_available_width;
780 available_height = t_available_height;
781 }
782 }
783
784 if(btn_i >= first_btn || used_width >= available_width)
785 {
786 return false;
787 }
788 if(last_button != NULL)
789 {
790 *last_button = btn_i;
791 }
792
793 wxRibbonButtonBarLayout* layout = new wxRibbonButtonBarLayout;
794 WX_APPEND_ARRAY(layout->buttons, original->buttons);
795 wxPoint cursor(layout->buttons.Item(btn_i).position);
796 bool preserve_height = false;
797 if(btn_i == 0)
798 {
799 // If height isn't preserved (i.e. it is reduced), then the minimum
800 // size for the button bar will decrease, preventing the original
801 // layout from being used (in some cases).
802 // It may be a good idea to always preverse the height, but for now
803 // it is only done when the first button is involved in a collapse.
804 preserve_height = true;
805 }
806
807 for(; btn_i <= first_btn; ++btn_i)
808 {
809 wxRibbonButtonBarButtonInstance& instance = layout->buttons.Item(btn_i);
810 instance.base->GetSmallerSize(&instance.size);
811 instance.position = cursor;
812 cursor.y += instance.base->sizes[instance.size].size.GetHeight();
813 }
814
815 int x_adjust = available_width - used_width;
816
817 for(; btn_i < btn_count; ++btn_i)
818 {
819 wxRibbonButtonBarButtonInstance& instance = layout->buttons.Item(btn_i);
820 instance.position.x -= x_adjust;
821 }
822
823 layout->CalculateOverallSize();
824
825 // Sanity check
826 if(layout->overall_size.GetWidth() >= original->overall_size.GetWidth() ||
827 layout->overall_size.GetHeight() > original->overall_size.GetHeight())
828 {
829 delete layout;
65391c8f 830 wxFAIL_MSG("Layout collapse resulted in increased size");
3c3ead1d
PC
831 return false;
832 }
833
834 if(preserve_height)
835 {
836 layout->overall_size.SetHeight(original->overall_size.GetHeight());
837 }
838
839 m_layouts.Add(layout);
840 return true;
841}
842
843void wxRibbonButtonBar::OnMouseMove(wxMouseEvent& evt)
844{
845 wxPoint cursor(evt.GetPosition());
846 wxRibbonButtonBarButtonInstance* new_hovered = NULL;
847 long new_hovered_state = 0;
848
849 wxRibbonButtonBarLayout* layout = m_layouts.Item(m_current_layout);
850 size_t btn_count = layout->buttons.Count();
851 size_t btn_i;
852 for(btn_i = 0; btn_i < btn_count; ++btn_i)
853 {
854 wxRibbonButtonBarButtonInstance& instance = layout->buttons.Item(btn_i);
855 wxRibbonButtonBarButtonSizeInfo& size = instance.base->sizes[instance.size];
856 wxRect btn_rect;
857 btn_rect.SetTopLeft(m_layout_offset + instance.position);
858 btn_rect.SetSize(size.size);
859 if(btn_rect.Contains(cursor))
860 {
861 new_hovered = &instance;
862 new_hovered_state = instance.base->state;
863 new_hovered_state &= ~wxRIBBON_BUTTONBAR_BUTTON_HOVER_MASK;
864 wxPoint offset(cursor);
865 offset -= btn_rect.GetTopLeft();
866 if(size.normal_region.Contains(offset))
867 {
868 new_hovered_state |= wxRIBBON_BUTTONBAR_BUTTON_NORMAL_HOVERED;
869 }
870 if(size.dropdown_region.Contains(offset))
871 {
872 new_hovered_state |= wxRIBBON_BUTTONBAR_BUTTON_DROPDOWN_HOVERED;
873 }
874 break;
875 }
876 }
877
878 if(new_hovered != m_hovered_button || (m_hovered_button != NULL &&
879 new_hovered_state != m_hovered_button->base->state))
880 {
881 if(m_hovered_button != NULL)
882 {
883 m_hovered_button->base->state &= ~wxRIBBON_BUTTONBAR_BUTTON_HOVER_MASK;
884 }
885 m_hovered_button = new_hovered;
886 if(m_hovered_button != NULL)
887 {
888 m_hovered_button->base->state = new_hovered_state;
889 }
890 Refresh(false);
891 }
892
893 if(m_active_button && !m_lock_active_state)
894 {
895 long new_active_state = m_active_button->base->state;
896 new_active_state &= ~wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK;
897 wxRibbonButtonBarButtonSizeInfo& size =
898 m_active_button->base->sizes[m_active_button->size];
899 wxRect btn_rect;
900 btn_rect.SetTopLeft(m_layout_offset + m_active_button->position);
901 btn_rect.SetSize(size.size);
902 if(btn_rect.Contains(cursor))
903 {
904 wxPoint offset(cursor);
905 offset -= btn_rect.GetTopLeft();
906 if(size.normal_region.Contains(offset))
907 {
908 new_active_state |= wxRIBBON_BUTTONBAR_BUTTON_NORMAL_ACTIVE;
909 }
910 if(size.dropdown_region.Contains(offset))
911 {
912 new_active_state |= wxRIBBON_BUTTONBAR_BUTTON_DROPDOWN_ACTIVE;
913 }
914 }
915 if(new_active_state != m_active_button->base->state)
916 {
917 m_active_button->base->state = new_active_state;
918 Refresh(false);
919 }
920 }
921}
922
923void wxRibbonButtonBar::OnMouseDown(wxMouseEvent& evt)
924{
925 wxPoint cursor(evt.GetPosition());
926 m_active_button = NULL;
927
928 wxRibbonButtonBarLayout* layout = m_layouts.Item(m_current_layout);
929 size_t btn_count = layout->buttons.Count();
930 size_t btn_i;
931 for(btn_i = 0; btn_i < btn_count; ++btn_i)
932 {
933 wxRibbonButtonBarButtonInstance& instance = layout->buttons.Item(btn_i);
934 wxRibbonButtonBarButtonSizeInfo& size = instance.base->sizes[instance.size];
935 wxRect btn_rect;
936 btn_rect.SetTopLeft(m_layout_offset + instance.position);
937 btn_rect.SetSize(size.size);
938 if(btn_rect.Contains(cursor))
939 {
940 m_active_button = &instance;
941 cursor -= btn_rect.GetTopLeft();
942 long state = 0;
943 if(size.normal_region.Contains(cursor))
944 state = wxRIBBON_BUTTONBAR_BUTTON_NORMAL_ACTIVE;
945 else if(size.dropdown_region.Contains(cursor))
946 state = wxRIBBON_BUTTONBAR_BUTTON_DROPDOWN_ACTIVE;
947 instance.base->state |= state;
948 Refresh(false);
949 break;
950 }
951 }
952}
953
954void wxRibbonButtonBar::OnMouseUp(wxMouseEvent& evt)
955{
956 wxPoint cursor(evt.GetPosition());
957
958 if(m_active_button)
959 {
960 wxRibbonButtonBarButtonSizeInfo& size =
961 m_active_button->base->sizes[m_active_button->size];
962 wxRect btn_rect;
963 btn_rect.SetTopLeft(m_layout_offset + m_active_button->position);
964 btn_rect.SetSize(size.size);
965 if(btn_rect.Contains(cursor))
966 {
967 int id = m_active_button->base->id;
968 cursor -= btn_rect.GetTopLeft();
969 wxEventType event_type;
970 do
971 {
972 if(size.normal_region.Contains(cursor))
973 event_type = wxEVT_COMMAND_RIBBONBUTTON_CLICKED;
974 else if(size.dropdown_region.Contains(cursor))
975 event_type = wxEVT_COMMAND_RIBBONBUTTON_DROPDOWN_CLICKED;
976 else
977 break;
978 wxRibbonButtonBarEvent notification(event_type, id);
955bad41
PC
979 if(m_active_button->base->kind == wxRIBBON_BUTTON_TOGGLE)
980 {
981 m_active_button->base->state ^=
982 wxRIBBON_BUTTONBAR_BUTTON_TOGGLED;
983 notification.SetInt(m_active_button->base->state &
984 wxRIBBON_BUTTONBAR_BUTTON_TOGGLED);
985 }
3c3ead1d
PC
986 notification.SetEventObject(this);
987 notification.SetBar(this);
988 m_lock_active_state = true;
989 ProcessWindowEvent(notification);
990 m_lock_active_state = false;
991 } while(false);
992 if(m_active_button) // may have been NULLed by event handler
993 {
994 m_active_button->base->state &= ~wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK;
995 m_active_button = NULL;
996 }
997 Refresh(false);
998 }
999 }
1000}
1001
1002void wxRibbonButtonBar::OnMouseEnter(wxMouseEvent& evt)
1003{
1004 if(m_active_button && !evt.LeftIsDown())
1005 {
1006 m_active_button = NULL;
1007 }
1008}
1009
1010void wxRibbonButtonBar::OnMouseLeave(wxMouseEvent& WXUNUSED(evt))
1011{
1012 bool repaint = false;
1013 if(m_hovered_button != NULL)
1014 {
1015 m_hovered_button->base->state &= ~wxRIBBON_BUTTONBAR_BUTTON_HOVER_MASK;
1016 m_hovered_button = NULL;
1017 repaint = true;
1018 }
1019 if(m_active_button != NULL && !m_lock_active_state)
1020 {
1021 m_active_button->base->state &= ~wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK;
1022 repaint = true;
1023 }
1024 if(repaint)
1025 Refresh(false);
1026}
1027
1028bool wxRibbonButtonBarEvent::PopupMenu(wxMenu* menu)
1029{
1030 wxPoint pos = wxDefaultPosition;
1031 if(m_bar->m_active_button)
1032 {
1033 wxRibbonButtonBarButtonSizeInfo& size =
1034 m_bar->m_active_button->base->sizes[m_bar->m_active_button->size];
1035 wxRect btn_rect;
1036 btn_rect.SetTopLeft(m_bar->m_layout_offset +
1037 m_bar->m_active_button->position);
1038 btn_rect.SetSize(size.size);
1039 pos = btn_rect.GetBottomLeft();
1040 pos.y++;
1041 }
1042 return m_bar->PopupMenu(menu, pos);
1043}
1044
1045#endif // wxUSE_RIBBON