Restore selected ButtonSet item when we offer the mouse capture

When a ButtonSet triggers an onClick() when a mouse up message is received
(ButtonSet::m_multipleSelection == true), in case the mouse capture is
offered to other ButtonSet, we've to restore the selected item when we
capture the mouse, because we've never generated the onClick() event
in the first place.
This commit is contained in:
David Capello 2015-12-16 16:55:49 -03:00
parent 0f35102a4b
commit 1f513a1ed5
4 changed files with 36 additions and 5 deletions

View File

@ -219,6 +219,7 @@ BrushPopup::BrushPopup(BrushPopupDelegate* delegate)
setBorder(gfx::Border(2)*guiscale());
setChildSpacing(0);
m_box.noBorderNoChildSpacing();
m_standardBrushes.setTriggerOnMouseUp(true);
addChild(&m_box);

View File

@ -33,6 +33,10 @@ namespace app {
using namespace ui;
using namespace app::skin;
// Last selected item for ButtonSet activated on mouse up when the
// mouse capture is get.
static int g_itemBeforeCapture = -1;
WidgetType buttonset_item_type()
{
static WidgetType type = kGenericWidget;
@ -172,6 +176,15 @@ bool ButtonSet::Item::onProcessMessage(ui::Message* msg)
break;
case ui::kMouseDownMessage:
// Only for single-item and trigerred on mouse up ButtonSets: We
// save the current selected item to restore it just in case the
// user leaves the ButtonSet without releasing the mouse button
// and the mouse capture if offered to other ButtonSet.
if (buttonSet()->m_triggerOnMouseUp) {
ASSERT(g_itemBeforeCapture < 0);
g_itemBeforeCapture = buttonSet()->selectedItem();
}
captureMouse();
buttonSet()->setSelectedItem(this);
invalidate();
@ -184,6 +197,9 @@ bool ButtonSet::Item::onProcessMessage(ui::Message* msg)
case ui::kMouseUpMessage:
if (hasCapture()) {
if (g_itemBeforeCapture >= 0)
g_itemBeforeCapture = -1;
releaseMouse();
invalidate();
@ -199,8 +215,19 @@ bool ButtonSet::Item::onProcessMessage(ui::Message* msg)
case ui::kMouseMoveMessage:
if (hasCapture()) {
if (buttonSet()->m_offerCapture)
offerCapture(static_cast<ui::MouseMessage*>(msg), buttonset_item_type());
if (buttonSet()->m_offerCapture) {
if (offerCapture(static_cast<ui::MouseMessage*>(msg), buttonset_item_type())) {
// Only for ButtonSets trigerred on mouse up.
if (buttonSet()->m_triggerOnMouseUp &&
g_itemBeforeCapture >= 0) {
// As we never received a kMouseUpMessage (so we never
// called onClick()), we have to restore the selected
// item at the point when we received the mouse capture.
buttonSet()->setSelectedItem(g_itemBeforeCapture);
g_itemBeforeCapture = -1;
}
}
}
}
break;

View File

@ -1236,7 +1236,7 @@ void Widget::releaseMouse()
}
}
void Widget::offerCapture(ui::MouseMessage* mouseMsg, int widget_type)
bool Widget::offerCapture(ui::MouseMessage* mouseMsg, int widget_type)
{
if (hasCapture()) {
Widget* pick = manager()->pick(mouseMsg->position());
@ -1250,8 +1250,10 @@ void Widget::offerCapture(ui::MouseMessage* mouseMsg, int widget_type)
mouseMsg->position());
mouseMsg2->addRecipient(pick);
manager()->enqueueMessage(mouseMsg2);
return true;
}
}
return false;
}
bool Widget::hasFocus()

View File

@ -340,8 +340,9 @@ namespace ui {
bool hasMouseOver();
bool hasCapture();
// Offer the capture to widgets of the given type
void offerCapture(ui::MouseMessage* mouseMsg, int widget_type);
// Offer the capture to widgets of the given type. Returns true if
// the capture was passed to other widget.
bool offerCapture(ui::MouseMessage* mouseMsg, int widget_type);
// Returns lower-case letter that represet the mnemonic of the widget
// (the underscored character, i.e. the letter after & symbol).