tibia-client/src/framework/ui/uiwidget.cpp

1387 lines
40 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-08-14 04:09:11 +02:00
#include "uiwidget.h"
#include "uimanager.h"
#include "uianchorlayout.h"
#include "uiverticallayout.h"
2011-11-16 00:47:32 +01:00
#include "uitranslator.h"
2011-08-14 04:09:11 +02:00
2011-08-15 16:06:15 +02:00
#include <framework/core/eventdispatcher.h>
#include <framework/graphics/image.h>
#include <framework/graphics/borderimage.h>
#include <framework/graphics/fontmanager.h>
#include <framework/otml/otmlnode.h>
#include <framework/graphics/graphics.h>
#include <framework/platform/platformwindow.h>
#include <framework/graphics/texturemanager.h>
2011-08-14 04:09:11 +02:00
2011-08-22 14:44:26 +02:00
UIWidget::UIWidget()
2011-08-14 04:09:11 +02:00
{
2011-11-17 18:43:41 +01:00
m_lastFocusReason = Fw::ActiveFocusReason;
m_states = Fw::DefaultState;
2011-11-17 18:43:41 +01:00
m_font = g_fonts.getDefaultFont();
m_opacity = 255;
2011-11-17 22:41:02 +01:00
m_marginTop = m_marginRight = m_marginBottom = m_marginLeft = 0;
//m_backgroundColor = Fw::alpha;
m_backgroundColor = Fw::white;
m_foregroundColor = Fw::white;
m_textAlign = Fw::AlignCenter;
2011-08-14 04:09:11 +02:00
// generate an unique id, this is need because anchored layouts find widgets by id
static unsigned long id = 1;
m_id = Fw::mkstr("widget", id++);
2011-08-14 04:09:11 +02:00
}
void UIWidget::destroy()
2011-08-22 21:39:46 +02:00
{
2011-11-17 18:43:41 +01:00
setVisible(false);
setEnabled(false);
2012-01-02 23:09:49 +01:00
// release input grabs
if(g_ui.getKeyboardReceiver() == asUIWidget())
g_ui.resetKeyboardReceiver();
if(g_ui.getMouseReceiver() == asUIWidget())
g_ui.resetMouseReceiver();
2011-08-22 21:39:46 +02:00
// remove itself from parent
if(UIWidgetPtr parent = getParent()) {
if(parent->hasChild(asUIWidget()))
parent->removeChild(asUIWidget());
}
2012-01-05 19:02:27 +01:00
callLuaField("onDestroy");
}
2011-08-14 04:09:11 +02:00
void UIWidget::render()
{
renderSelf();
renderChildren();
}
void UIWidget::renderSelf()
2011-08-14 04:09:11 +02:00
{
// draw style components in order
drawBackground(m_rect);
drawBorder(m_rect);
drawImage(m_rect);
drawIcon(m_rect);
drawText(m_rect);
}
2011-08-14 04:09:11 +02:00
void UIWidget::renderChildren()
{
2011-08-22 21:13:52 +02:00
// draw children
2011-08-14 04:09:11 +02:00
for(const UIWidgetPtr& child : m_children) {
2011-11-16 22:00:40 +01:00
// render only visible children with a valid rect inside our rect
if(child->isExplicitlyVisible() &&
child->getRect().isValid() &&
child->getRect().intersects(m_rect) &&
child->getOpacity() > 0) {
2011-08-22 21:13:52 +02:00
// store current graphics opacity
2011-12-07 01:31:55 +01:00
int oldOpacity = g_painter.getOpacity();
2011-08-22 21:13:52 +02:00
// decrease to self opacity
if(child->getOpacity() < oldOpacity)
2011-12-07 01:31:55 +01:00
g_painter.setOpacity(child->getOpacity());
2011-08-14 04:09:11 +02:00
child->render();
2011-08-20 22:30:41 +02:00
// debug draw box
2011-12-07 01:31:55 +01:00
//g_painter.setColor(Fw::green);
//g_painter.drawBoundingRect(child->getRect());
//g_fonts.getDefaultFont()->renderText(child->getId(), child->getPos() + Point(2, 0), Fw::red);
2011-08-20 22:30:41 +02:00
2011-12-07 01:31:55 +01:00
g_painter.setOpacity(oldOpacity);
}
2011-08-14 04:09:11 +02:00
}
}
void UIWidget::drawBackground(const Rect& screenCoords)
{
/*
if(m_backgroundColor.a() > 0) {
g_painter.setColor(m_backgroundColor);
g_painter.drawFilledRect(screenCoords);
//g_painter.drawFilledRect(screenCoords.expanded(-m_borderWidth));
}
*/
}
void UIWidget::drawBorder(const Rect& screenCoords)
{
/*
if(m_borderWidth > 0 && m_borderColor.a() > 0) {
g_painter.bindColor(m_borderColor);
g_painter.drawBoundingRect(screenCoords, m_borderWidth);
}
*/
}
void UIWidget::drawImage(const Rect& screenCoords)
{
if(m_image) {
g_painter.setColor(m_backgroundColor);
m_image->draw(screenCoords);
}
}
void UIWidget::drawIcon(const Rect& screenCoords)
{
if(m_icon) {
Rect iconRect;
iconRect.resize(m_icon->getSize());
iconRect.moveCenter(screenCoords.center());
g_painter.setColor(Fw::white);
g_painter.drawTexturedRect(iconRect, m_icon);
}
}
void UIWidget::drawText(const Rect& screenCoords)
{
g_painter.setColor(m_foregroundColor);
if(m_text.length() > 0 && m_foregroundColor.a() > 0) {
Rect textRect = screenCoords;
textRect.translate(m_textOffset);
m_font->renderText(m_text, textRect, m_textAlign, m_foregroundColor);
}
}
2011-08-30 01:40:56 +02:00
void UIWidget::setVisible(bool visible)
{
2011-11-16 22:00:40 +01:00
if(m_visible != visible) {
m_visible = visible;
// make parent focus another child
if(!visible && isFocused()) {
if(UIWidgetPtr parent = getParent())
parent->focusPreviousChild(Fw::ActiveFocusReason);
}
updateState(Fw::ActiveState);
updateState(Fw::HoverState);
}
}
2012-01-05 03:42:17 +01:00
void UIWidget::setEnabled(bool enabled)
{
if(enabled != m_enabled) {
m_enabled = enabled;
updateState(Fw::DisabledState);
updateState(Fw::ActiveState);
updateState(Fw::HoverState);
}
}
void UIWidget::setPressed(bool pressed)
{
if(pressed != m_pressed) {
m_pressed = pressed;
updateState(Fw::PressedState);
}
}
2012-01-05 19:02:27 +01:00
void UIWidget::setOn(bool on)
{
setState(Fw::OnState, on);
}
void UIWidget::setChecked(bool checked)
{
if(setState(Fw::CheckedState, checked))
callLuaField("onCheckChange", checked);
}
2011-11-16 22:00:40 +01:00
void UIWidget::setFocusable(bool focusable)
{
if(m_focusable != focusable) {
m_focusable = focusable;
// make parent focus another child
if(!focusable && isFocused()) {
if(UIWidgetPtr parent = getParent())
parent->focusPreviousChild(Fw::ActiveFocusReason);
}
2011-08-30 01:40:56 +02:00
}
}
void UIWidget::setStyle(const std::string& styleName)
{
OTMLNodePtr styleNode = g_ui.getStyle(styleName);
2012-01-02 21:46:40 +01:00
if(!styleNode) {
logTraceError("unable to retrive style '", styleName, "': not a defined style");
return;
2012-01-02 21:46:40 +01:00
}
applyStyle(styleNode);
m_style = styleNode;
2012-01-03 02:32:34 +01:00
assert(getStyleName() != "");
2011-11-16 06:23:16 +01:00
updateStyle();
}
void UIWidget::setStyleFromNode(const OTMLNodePtr& styleNode)
2011-08-14 04:09:11 +02:00
{
applyStyle(styleNode);
m_style = styleNode;
2011-11-16 06:23:16 +01:00
updateStyle();
}
2011-08-14 04:09:11 +02:00
void UIWidget::setParent(const UIWidgetPtr& parent)
{
2011-08-14 04:09:11 +02:00
// remove from old parent
UIWidgetPtr oldParent = getParent();
2011-11-17 21:40:31 +01:00
// the parent is already the same
if(oldParent == parent)
return;
UIWidgetPtr self = asUIWidget();
2011-08-22 21:13:52 +02:00
if(oldParent && oldParent->hasChild(self))
oldParent->removeChild(self);
// reset parent
2011-08-14 04:09:11 +02:00
m_parent.reset();
2011-08-22 21:13:52 +02:00
// set new parent
2011-08-14 04:09:11 +02:00
if(parent) {
2011-08-22 21:39:46 +02:00
m_parent = parent;
2011-08-22 21:13:52 +02:00
// add to parent if needed
if(!parent->hasChild(self))
parent->addChild(self);
2011-08-14 04:09:11 +02:00
}
}
2011-08-21 21:43:05 +02:00
void UIWidget::setRect(const Rect& rect)
2011-08-14 04:09:11 +02:00
{
// only update if the rect really changed
2011-08-14 04:09:11 +02:00
Rect oldRect = m_rect;
if(rect == oldRect)
return;
2011-08-14 04:09:11 +02:00
m_rect = rect;
// updates own layout
updateLayout();
2011-08-21 21:43:05 +02:00
// avoid massive update events
if(!m_updateEventScheduled) {
2011-08-14 04:09:11 +02:00
UIWidgetPtr self = asUIWidget();
g_dispatcher.addEvent([self, oldRect]() {
2011-08-21 21:43:05 +02:00
self->m_updateEventScheduled = false;
2012-01-05 03:42:17 +01:00
self->onGeometryChange(oldRect, self->getRect());
2011-08-14 04:09:11 +02:00
});
2012-01-05 19:02:27 +01:00
m_updateEventScheduled = true;
2011-08-14 04:09:11 +02:00
}
}
void UIWidget::setIcon(const std::string& iconFile)
{
m_icon = g_textures.getTexture(iconFile);
}
void UIWidget::setText(const std::string& text)
{
if(m_text != text) {
m_text = text;
// update rect size
if(!m_rect.isValid()) {
Size textSize = m_font->calculateTextRectSize(m_text);
Size newSize = getSize();
if(newSize.width() <= 0)
newSize.setWidth(textSize.width());
if(newSize.height() <= 0)
newSize.setHeight(textSize.height());
resize(newSize);
}
onTextChange(text);
}
}
void UIWidget::setFont(const std::string& fontName)
{
m_font = g_fonts.getFont(fontName);
}
2012-01-02 23:09:49 +01:00
void UIWidget::bindRectToParent()
{
Rect boundRect = m_rect;
UIWidgetPtr parent = getParent();
if(parent) {
Rect parentRect = parent->getRect();
boundRect.bound(parentRect);
}
setRect(boundRect);
}
void UIWidget::lock()
{
if(UIWidgetPtr parent = getParent())
parent->lockChild(asUIWidget());
}
void UIWidget::unlock()
{
if(UIWidgetPtr parent = getParent())
parent->unlockChild(asUIWidget());
}
void UIWidget::focus()
{
2011-11-16 22:00:40 +01:00
if(!m_focusable)
return;
if(UIWidgetPtr parent = getParent())
parent->focusChild(asUIWidget(), Fw::ActiveFocusReason);
}
2012-01-02 23:09:49 +01:00
void UIWidget::grabMouse()
{
g_ui.setMouseReceiver(asUIWidget());
}
void UIWidget::ungrabMouse()
{
if(g_ui.getMouseReceiver() == asUIWidget())
g_ui.resetMouseReceiver();
}
void UIWidget::grabKeyboard()
{
g_ui.setKeyboardReceiver(asUIWidget());
}
void UIWidget::ungrabKeyboard()
{
if(g_ui.getKeyboardReceiver() == asUIWidget())
g_ui.resetKeyboardReceiver();
}
2011-08-22 21:13:52 +02:00
bool UIWidget::isVisible()
{
if(!m_visible)
return false;
else if(UIWidgetPtr parent = getParent())
return parent->isVisible();
else
return asUIWidget() == g_ui.getRootWidget();
2011-08-14 04:09:11 +02:00
}
bool UIWidget::hasChild(const UIWidgetPtr& child)
{
auto it = std::find(m_children.begin(), m_children.end(), child);
if(it != m_children.end())
return true;
return false;
}
2011-08-21 21:43:05 +02:00
UIWidgetPtr UIWidget::getRootParent()
2011-08-14 04:09:11 +02:00
{
2011-08-21 21:43:05 +02:00
if(UIWidgetPtr parent = getParent())
return parent->getRootParent();
else
return asUIWidget();
2011-08-14 04:09:11 +02:00
}
UIWidgetPtr UIWidget::getChildAfter(const UIWidgetPtr& relativeChild)
{
2011-08-22 21:13:52 +02:00
auto it = std::find(m_children.begin(), m_children.end(), relativeChild);
if(it != m_children.end() && ++it != m_children.end())
return *it;
2011-08-14 04:09:11 +02:00
return nullptr;
}
UIWidgetPtr UIWidget::getChildBefore(const UIWidgetPtr& relativeChild)
{
2011-08-22 21:13:52 +02:00
auto it = std::find(m_children.rbegin(), m_children.rend(), relativeChild);
if(it != m_children.rend() && ++it != m_children.rend())
return *it;
2011-08-14 04:09:11 +02:00
return nullptr;
}
UIWidgetPtr UIWidget::getChildById(const std::string& childId)
{
for(const UIWidgetPtr& child : m_children) {
if(child->getId() == childId)
return child;
2011-08-14 04:09:11 +02:00
}
return nullptr;
}
UIWidgetPtr UIWidget::getChildByPos(const Point& childPos)
{
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
const UIWidgetPtr& widget = (*it);
2012-01-02 23:09:49 +01:00
if(widget->isExplicitlyVisible() && widget->containsPoint(childPos))
2011-08-14 04:09:11 +02:00
return widget;
}
return nullptr;
}
2011-08-23 03:08:36 +02:00
UIWidgetPtr UIWidget::getChildByIndex(int index)
2011-08-14 04:09:11 +02:00
{
2011-08-23 03:08:36 +02:00
index = index <= 0 ? (m_children.size() + index) : index-1;
if(index >= 0 && (uint)index < m_children.size())
return m_children.at(index);
2011-08-14 04:09:11 +02:00
return nullptr;
}
2011-08-21 21:43:05 +02:00
UIWidgetPtr UIWidget::recursiveGetChildById(const std::string& id)
2011-08-14 04:09:11 +02:00
{
2011-08-21 21:43:05 +02:00
UIWidgetPtr widget = getChildById(id);
if(!widget) {
2011-08-14 04:09:11 +02:00
for(const UIWidgetPtr& child : m_children) {
2011-08-21 21:43:05 +02:00
widget = child->recursiveGetChildById(id);
if(widget)
break;
2011-08-14 04:09:11 +02:00
}
}
2011-08-21 21:43:05 +02:00
return widget;
2011-08-14 04:09:11 +02:00
}
UIWidgetPtr UIWidget::recursiveGetChildByPos(const Point& childPos)
{
for(const UIWidgetPtr& child : m_children) {
2012-01-02 23:09:49 +01:00
if(child->containsPoint(childPos)) {
2011-08-14 04:09:11 +02:00
if(UIWidgetPtr subChild = child->recursiveGetChildByPos(childPos))
return subChild;
return child;
}
}
return nullptr;
}
UIWidgetPtr UIWidget::backwardsGetWidgetById(const std::string& id)
{
2011-08-21 21:43:05 +02:00
UIWidgetPtr widget = getChildById(id);
if(!widget) {
2011-08-14 04:09:11 +02:00
if(UIWidgetPtr parent = getParent())
widget = parent->backwardsGetWidgetById(id);
}
return widget;
}
void UIWidget::focusChild(const UIWidgetPtr& child, Fw::FocusReason reason)
{
if(child && !hasChild(child)) {
logError("Attempt to focus an unknown child in a UIWidget");
return;
}
2011-08-22 21:13:52 +02:00
if(child != m_focusedChild) {
UIWidgetPtr oldFocused = m_focusedChild;
2011-08-22 21:13:52 +02:00
m_focusedChild = child;
if(child) {
child->setLastFocusReason(reason);
child->updateState(Fw::FocusState);
child->updateState(Fw::ActiveState);
}
2011-08-22 21:13:52 +02:00
if(oldFocused) {
oldFocused->setLastFocusReason(reason);
oldFocused->updateState(Fw::FocusState);
oldFocused->updateState(Fw::ActiveState);
}
}
}
2011-08-22 21:13:52 +02:00
void UIWidget::addChild(const UIWidgetPtr& child)
2011-08-14 04:09:11 +02:00
{
if(!child) {
logWarning("attempt to add a null child into a UIWidget");
return;
}
if(hasChild(child)) {
logWarning("attempt to add a child again into a UIWidget");
return;
}
2011-08-22 21:13:52 +02:00
m_children.push_back(child);
child->setParent(asUIWidget());
2011-08-14 04:09:11 +02:00
// create default layout
if(!m_layout)
2012-01-06 09:48:59 +01:00
m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
// add to layout and updates it
m_layout->addWidget(child);
// update new child states
child->updateStates();
updateChildrenIndexStates();
2011-08-14 04:09:11 +02:00
}
2011-08-23 03:08:36 +02:00
void UIWidget::insertChild(int index, const UIWidgetPtr& child)
2011-08-20 22:30:41 +02:00
{
if(!child) {
logWarning("attempt to insert a null child into a UIWidget");
2011-08-20 22:30:41 +02:00
return;
}
2011-08-20 22:30:41 +02:00
if(hasChild(child)) {
logWarning("attempt to insert a child again into a UIWidget");
return;
}
2011-08-20 22:30:41 +02:00
2011-08-23 03:08:36 +02:00
index = index <= 0 ? (m_children.size() + index) : index-1;
2011-08-20 22:30:41 +02:00
2011-08-23 03:08:36 +02:00
assert(index >= 0 && (uint)index <= m_children.size());
2011-08-20 22:30:41 +02:00
2011-08-22 21:13:52 +02:00
// retrieve child by index
2011-08-20 22:30:41 +02:00
auto it = m_children.begin() + index;
2011-08-22 21:13:52 +02:00
m_children.insert(it, child);
child->setParent(asUIWidget());
2011-08-20 22:30:41 +02:00
// create default layout if needed
if(!m_layout)
2012-01-06 09:48:59 +01:00
m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
// add to layout and updates it
m_layout->addWidget(child);
// update new child states
child->updateStates();
updateChildrenIndexStates();
2011-08-20 22:30:41 +02:00
}
2011-08-22 21:13:52 +02:00
void UIWidget::removeChild(const UIWidgetPtr& child)
2011-08-14 04:09:11 +02:00
{
// remove from children list
2011-08-29 20:38:01 +02:00
if(hasChild(child)) {
// defocus if needed
bool focusAnother = false;
if(m_focusedChild == child) {
focusChild(nullptr, Fw::ActiveFocusReason);
focusAnother = true;
}
2011-08-29 20:38:01 +02:00
if(isChildLocked(child))
unlockChild(child);
2011-08-29 20:38:01 +02:00
auto it = std::find(m_children.begin(), m_children.end(), child);
m_children.erase(it);
2011-08-14 04:09:11 +02:00
// reset child parent
assert(child->getParent() == asUIWidget());
child->setParent(nullptr);
2011-08-14 04:09:11 +02:00
m_layout->removeWidget(child);
2011-08-21 21:43:05 +02:00
// update child states
child->updateStates();
updateChildrenIndexStates();
2011-08-29 20:38:01 +02:00
if(focusAnother && !m_focusedChild)
focusPreviousChild(Fw::ActiveFocusReason);
} else
logError("Attempt to remove an unknown child from a UIWidget");
2011-08-14 04:09:11 +02:00
}
void UIWidget::focusNextChild(Fw::FocusReason reason)
2011-08-14 04:09:11 +02:00
{
UIWidgetPtr toFocus;
UIWidgetList rotatedChildren(m_children);
2011-08-22 21:13:52 +02:00
if(m_focusedChild) {
auto focusedIt = std::find(rotatedChildren.begin(), rotatedChildren.end(), m_focusedChild);
if(focusedIt != rotatedChildren.end()) {
std::rotate(rotatedChildren.begin(), focusedIt, rotatedChildren.end());
rotatedChildren.pop_front();
2011-08-14 04:09:11 +02:00
}
2011-08-22 21:13:52 +02:00
}
// finds next child to focus
for(const UIWidgetPtr& child : rotatedChildren) {
2011-08-30 01:40:56 +02:00
if(child->isFocusable() && child->isExplicitlyEnabled() && child->isVisible()) {
2011-08-22 21:13:52 +02:00
toFocus = child;
break;
}
}
2011-08-14 04:09:11 +02:00
if(toFocus)
focusChild(toFocus, reason);
2011-08-14 04:09:11 +02:00
}
2011-08-29 04:18:13 +02:00
void UIWidget::focusPreviousChild(Fw::FocusReason reason)
{
UIWidgetPtr toFocus;
UIWidgetList rotatedChildren(m_children);
2011-11-02 02:55:36 +01:00
std::reverse(rotatedChildren.begin(), rotatedChildren.end());
2011-08-29 04:18:13 +02:00
if(m_focusedChild) {
auto focusedIt = std::find(rotatedChildren.begin(), rotatedChildren.end(), m_focusedChild);
if(focusedIt != rotatedChildren.end()) {
std::rotate(rotatedChildren.begin(), focusedIt, rotatedChildren.end());
rotatedChildren.pop_front();
}
}
// finds next child to focus
for(const UIWidgetPtr& child : rotatedChildren) {
2011-08-30 01:40:56 +02:00
if(child->isFocusable() && child->isExplicitlyEnabled() && child->isVisible()) {
2011-08-29 04:18:13 +02:00
toFocus = child;
break;
}
}
if(toFocus)
focusChild(toFocus, reason);
}
2011-08-22 21:13:52 +02:00
void UIWidget::moveChildToTop(const UIWidgetPtr& child)
2011-08-14 04:09:11 +02:00
{
2011-08-22 21:13:52 +02:00
if(!child)
return;
2011-08-14 04:09:11 +02:00
// remove and push child again
2011-08-22 21:13:52 +02:00
auto it = std::find(m_children.begin(), m_children.end(), child);
2011-08-14 04:09:11 +02:00
assert(it != m_children.end());
m_children.erase(it);
2011-08-22 21:13:52 +02:00
m_children.push_back(child);
updateChildrenIndexStates();
2011-08-14 04:09:11 +02:00
}
void UIWidget::moveChildToIndex(const UIWidgetPtr& child, int index)
{
if(!child)
return;
// remove and push child again
auto it = std::find(m_children.begin(), m_children.end(), child);
assert(it != m_children.end());
m_children.erase(it);
m_children.insert(m_children.begin() + index - 1, child);
updateChildrenIndexStates();
}
2011-08-22 21:13:52 +02:00
void UIWidget::lockChild(const UIWidgetPtr& child)
{
2011-08-22 21:13:52 +02:00
if(!child)
return;
2011-08-22 21:13:52 +02:00
assert(hasChild(child));
// prevent double locks
2011-08-29 20:38:01 +02:00
if(isChildLocked(child))
unlockChild(child);
2011-08-22 21:13:52 +02:00
// disable all other children
for(const UIWidgetPtr& otherChild : m_children) {
if(otherChild == child)
child->setEnabled(true);
else
2011-08-22 21:13:52 +02:00
otherChild->setEnabled(false);
}
2011-08-22 21:13:52 +02:00
m_lockedChildren.push_front(child);
2011-08-20 22:30:41 +02:00
2011-08-21 03:01:46 +02:00
// lock child focus
2011-08-22 21:13:52 +02:00
if(child->isFocusable())
2011-08-29 20:38:01 +02:00
focusChild(child, Fw::ActiveFocusReason);
moveChildToTop(child);
}
2011-08-22 21:13:52 +02:00
void UIWidget::unlockChild(const UIWidgetPtr& child)
{
2011-08-22 21:13:52 +02:00
if(!child)
return;
assert(hasChild(child));
2011-08-22 21:13:52 +02:00
auto it = std::find(m_lockedChildren.begin(), m_lockedChildren.end(), child);
if(it == m_lockedChildren.end())
return;
2011-08-22 21:13:52 +02:00
m_lockedChildren.erase(it);
2011-08-20 22:30:41 +02:00
2011-08-29 20:38:01 +02:00
// find new child to lock
2011-08-22 21:13:52 +02:00
UIWidgetPtr lockedChild;
2011-08-29 20:38:01 +02:00
if(m_lockedChildren.size() > 0) {
2011-08-22 21:13:52 +02:00
lockedChild = m_lockedChildren.front();
2011-08-29 20:38:01 +02:00
assert(hasChild(lockedChild));
}
2011-08-22 21:13:52 +02:00
for(const UIWidgetPtr& otherChild : m_children) {
// lock new child
if(lockedChild) {
if(otherChild == lockedChild)
lockedChild->setEnabled(true);
2011-08-20 22:30:41 +02:00
else
2011-08-22 21:13:52 +02:00
otherChild->setEnabled(false);
}
// else unlock all
else
otherChild->setEnabled(true);
}
2011-08-29 20:38:01 +02:00
if(lockedChild) {
if(lockedChild->isFocusable())
focusChild(lockedChild, Fw::ActiveFocusReason);
moveChildToTop(lockedChild);
}
}
bool UIWidget::isChildLocked(const UIWidgetPtr& child)
{
auto it = std::find(m_lockedChildren.begin(), m_lockedChildren.end(), child);
return it != m_lockedChildren.end();
}
int UIWidget::getChildIndex(const UIWidgetPtr& child)
{
int index = 1;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if(*it == child)
return index;
++index;
}
return -1;
}
void UIWidget::updateParentLayout()
2011-08-14 04:09:11 +02:00
{
if(UIWidgetPtr parent = getParent())
parent->updateLayout();
else
updateLayout();
2011-08-14 04:09:11 +02:00
}
void UIWidget::updateLayout()
2011-08-14 04:09:11 +02:00
{
if(m_layout)
m_layout->update();
2011-08-14 04:09:11 +02:00
}
void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
{
try {
2012-01-05 19:02:27 +01:00
m_loadingStyle = true;
onStyleApply(styleNode->tag(), styleNode);
callLuaField("onStyleApply", styleNode->tag(), styleNode);
2012-01-05 19:02:27 +01:00
m_loadingStyle = false;
} catch(Exception& e) {
logError("Failed to apply style to widget '", m_id, "' style: ", e.what());
}
}
bool UIWidget::setState(Fw::WidgetState state, bool on)
2011-11-16 00:47:32 +01:00
{
if(state == Fw::InvalidState)
return false;
2011-11-16 00:47:32 +01:00
int oldStates = m_states;
if(on)
m_states |= state;
else
m_states &= ~state;
if(oldStates != m_states) {
2011-11-16 00:47:32 +01:00
updateStyle();
return true;
}
return false;
2011-11-16 00:47:32 +01:00
}
bool UIWidget::hasState(Fw::WidgetState state)
{
if(state == Fw::InvalidState)
return false;
return (m_states & state);
}
void UIWidget::updateState(Fw::WidgetState state)
2011-08-14 04:09:11 +02:00
{
bool newStatus = true;
bool oldStatus = hasState(state);
bool updateChildren = false;
2011-08-14 04:09:11 +02:00
switch(state) {
case Fw::ActiveState: {
UIWidgetPtr widget = asUIWidget();
UIWidgetPtr parent;
do {
parent = widget->getParent();
if(!widget->isExplicitlyEnabled() ||
2012-01-06 09:48:59 +01:00
((parent && parent->getFocusedChild() != widget))) {
newStatus = false;
break;
}
} while(widget = parent);
updateChildren = true;
break;
}
case Fw::FocusState: {
newStatus = (getParent() && getParent()->getFocusedChild() == asUIWidget());
break;
}
case Fw::HoverState: {
updateChildren = true;
Point mousePos = g_window.getMousePos();
UIWidgetPtr widget = asUIWidget();
UIWidgetPtr parent;
do {
parent = widget->getParent();
if(!widget->isExplicitlyEnabled() || !widget->isExplicitlyVisible() || !widget->containsPoint(mousePos) ||
2012-01-06 09:48:59 +01:00
(parent && widget != parent->getChildByPos(mousePos))) {
newStatus = false;
break;
}
} while(widget = parent);
break;
}
case Fw::PressedState: {
newStatus = m_pressed;
break;
}
case Fw::DisabledState: {
bool enabled = true;
updateChildren = true;
UIWidgetPtr widget = asUIWidget();
do {
if(!widget->isExplicitlyEnabled()) {
enabled = false;
break;
}
} while(widget = widget->getParent());
newStatus = !enabled;
break;
}
case Fw::FirstState: {
newStatus = (getParent() && getParent()->getFirstChild() == asUIWidget());
break;
}
case Fw::MiddleState: {
newStatus = (getParent() && getParent()->getFirstChild() != asUIWidget() && getParent()->getLastChild() != asUIWidget());
break;
}
case Fw::LastState: {
newStatus = (getParent() && getParent()->getLastChild() == asUIWidget());
break;
}
case Fw::AlternateState: {
newStatus = (getParent() && (getParent()->getChildIndex(asUIWidget()) % 2) == 1);
break;
}
default:
return;
2011-08-21 21:43:05 +02:00
}
if(updateChildren) {
2011-11-03 20:32:50 +01:00
// do a backup of children list, because it may change while looping it
UIWidgetList children = m_children;
for(const UIWidgetPtr& child : children)
child->updateState(state);
}
2011-08-21 21:43:05 +02:00
if(setState(state, newStatus)) {
if(state == Fw::FocusState) {
g_dispatcher.addEvent(std::bind(&UIWidget::onFocusChange, asUIWidget(), newStatus, m_lastFocusReason));
} else if(state == Fw::HoverState)
g_dispatcher.addEvent(std::bind(&UIWidget::onHoverChange, asUIWidget(), newStatus));
}
2011-08-21 21:43:05 +02:00
}
void UIWidget::updateStates()
2011-08-21 21:43:05 +02:00
{
2012-01-05 19:02:27 +01:00
for(int state = 1; state != Fw::LastWidgetState; state <<= 1)
2011-11-16 00:47:32 +01:00
updateState((Fw::WidgetState)state);
2011-08-21 21:43:05 +02:00
}
void UIWidget::updateChildrenIndexStates()
{
for(const UIWidgetPtr& child : m_children) {
child->updateState(Fw::FirstState);
child->updateState(Fw::MiddleState);
child->updateState(Fw::LastState);
child->updateState(Fw::AlternateState);
}
}
void UIWidget::updateStyle()
2011-08-21 21:43:05 +02:00
{
2012-01-05 19:02:27 +01:00
if(m_loadingStyle && !m_updateStyleScheduled) {
UIWidgetPtr self = asUIWidget();
g_dispatcher.addEvent([self] {
self->m_updateStyleScheduled = false;
self->updateStyle();
});
m_updateStyleScheduled = true;
return;
}
if(!m_style)
return;
2011-08-21 21:43:05 +02:00
OTMLNodePtr newStateStyle = OTMLNode::create();
2011-08-21 21:43:05 +02:00
// copy only the changed styles from default style
if(m_stateStyle) {
for(OTMLNodePtr node : m_stateStyle->children()) {
if(OTMLNodePtr otherNode = m_style->get(node->tag()))
newStateStyle->addChild(otherNode->clone());
2011-08-21 21:43:05 +02:00
}
}
2011-11-16 00:47:32 +01:00
// checks for states combination
for(const OTMLNodePtr& style : m_style->children()) {
if(boost::starts_with(style->tag(), "$")) {
std::string statesStr = style->tag().substr(1);
std::vector<std::string> statesSplit;
boost::split(statesSplit, statesStr, boost::is_any_of(std::string(" ")));
2011-08-21 21:43:05 +02:00
2011-11-16 00:47:32 +01:00
bool match = true;
for(std::string stateStr : statesSplit) {
if(stateStr.length() == 0)
continue;
2011-08-21 21:43:05 +02:00
2011-11-16 00:47:32 +01:00
bool notstate = (stateStr[0] == '!');
if(notstate)
stateStr = stateStr.substr(1);
2011-08-21 21:43:05 +02:00
2011-11-16 00:47:32 +01:00
bool stateOn = hasState(Fw::translateState(stateStr));
if((!notstate && !stateOn) || (notstate && stateOn))
match = false;
}
2011-08-23 03:08:36 +02:00
2011-11-16 00:47:32 +01:00
// merge states styles
2011-11-16 06:23:16 +01:00
if(match) {
2011-11-16 00:47:32 +01:00
newStateStyle->merge(style);
2011-11-16 06:23:16 +01:00
}
2011-11-16 00:47:32 +01:00
}
}
2011-08-21 21:43:05 +02:00
2011-11-16 22:00:40 +01:00
//TODO: prevent setting already set proprieties
applyStyle(newStateStyle);
m_stateStyle = newStateStyle;
2011-08-21 21:43:05 +02:00
}
void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
2011-08-22 21:13:52 +02:00
{
// first set id
if(const OTMLNodePtr& node = styleNode->get("id"))
setId(node->value());
2011-08-22 21:13:52 +02:00
// load styles used by all widgets
for(const OTMLNodePtr& node : styleNode->children()) {
// background image
if(node->tag() == "image") {
2011-10-29 01:56:45 +02:00
ImagePtr image = ImagePtr(new Image);
image->loadFromOTML(node);
setImage(image);
2011-08-22 21:13:52 +02:00
}
2011-11-16 19:08:42 +01:00
else if(node->tag() == "border-image")
2011-08-22 21:13:52 +02:00
setImage(BorderImage::loadFromOTML(node));
if(node->tag() == "icon")
setIcon(node->value());
else if(node->tag() == "text")
setText(node->value());
else if(node->tag() == "text-align")
setTextAlign(Fw::translateAlignment(node->value()));
else if(node->tag() == "text-offset")
setTextOffset(node->value<Point>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "font")
setFont(node->value());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "color")
2011-08-22 21:13:52 +02:00
setForegroundColor(node->value<Color>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "background-color")
2011-08-22 21:13:52 +02:00
setBackgroundColor(node->value<Color>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "opacity")
2011-08-22 21:13:52 +02:00
setOpacity(node->value<int>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "enabled")
2011-11-01 17:41:15 +01:00
setEnabled(node->value<bool>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "visible")
setVisible(node->value<bool>());
2012-01-05 19:02:27 +01:00
else if(node->tag() == "checked")
setChecked(node->value<bool>());
else if(node->tag() == "on")
setOn(node->value<bool>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "focusable")
setFocusable(node->value<bool>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "phantom")
2011-11-01 17:41:15 +01:00
setPhantom(node->value<bool>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "size")
2011-08-22 21:13:52 +02:00
resize(node->value<Size>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "width")
2011-08-22 21:13:52 +02:00
setWidth(node->value<int>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "height")
2011-08-22 21:13:52 +02:00
setHeight(node->value<int>());
2011-11-17 22:41:02 +01:00
else if(node->tag() == "fixed-size")
setSizeFixed(node->value<bool>());
else if(node->tag() == "pos")
2011-08-22 21:13:52 +02:00
moveTo(node->value<Point>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "x")
2011-08-22 21:13:52 +02:00
setX(node->value<int>());
2011-11-16 19:08:42 +01:00
else if(node->tag() == "y")
2011-08-22 21:13:52 +02:00
setY(node->value<int>());
2011-11-17 22:41:02 +01:00
else if(node->tag() == "margin-top")
2011-08-22 21:13:52 +02:00
setMarginTop(node->value<int>());
2011-11-17 22:41:02 +01:00
else if(node->tag() == "margin-right")
setMarginRight(node->value<int>());
else if(node->tag() == "margin-bottom")
2011-08-22 21:13:52 +02:00
setMarginBottom(node->value<int>());
2011-11-17 22:41:02 +01:00
else if(node->tag() == "margin-left")
setMarginLeft(node->value<int>());
else if(node->tag() == "margin") {
std::string marginDesc = node->value();
std::vector<std::string> split;
boost::split(split, marginDesc, boost::is_any_of(std::string(" ")));
if(split.size() == 4) {
setMarginTop(Fw::safeCast<int>(split[0]));
setMarginRight(Fw::safeCast<int>(split[1]));
setMarginBottom(Fw::safeCast<int>(split[2]));
setMarginLeft(Fw::safeCast<int>(split[3]));
} else if(split.size() == 3) {
int marginTop = Fw::safeCast<int>(split[0]);
int marginHorizontal = Fw::safeCast<int>(split[1]);
int marginBottom = Fw::safeCast<int>(split[2]);
setMarginTop(marginTop);
setMarginRight(marginHorizontal);
setMarginBottom(marginBottom);
setMarginLeft(marginHorizontal);
} else if(split.size() == 2) {
int marginVertical = Fw::safeCast<int>(split[0]);
int marginHorizontal = Fw::safeCast<int>(split[1]);
setMarginTop(marginVertical);
setMarginRight(marginHorizontal);
setMarginBottom(marginVertical);
setMarginLeft(marginHorizontal);
} else if(split.size() == 1) {
int margin = Fw::safeCast<int>(split[0]);
setMarginTop(margin);
setMarginRight(margin);
setMarginBottom(margin);
setMarginLeft(margin);
}
}
// layouts
else if(node->tag() == "layout") {
2011-11-04 00:34:32 +01:00
std::string layoutType;
if(node->hasValue())
layoutType = node->value();
else
2012-01-02 21:46:40 +01:00
layoutType = node->valueAt<std::string>("type", "");
if(!layoutType.empty()) {
UILayoutPtr layout;
if(layoutType == "verticalBox")
2012-01-06 09:48:59 +01:00
layout = UIVerticalLayoutPtr(new UIVerticalLayout(asUIWidget()));
2012-01-02 21:46:40 +01:00
else if(layoutType == "anchor")
2012-01-06 09:48:59 +01:00
layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
2012-01-02 21:46:40 +01:00
else
throw OTMLException(node, "cannot determine layout type");
setLayout(layout);
}
2011-11-04 00:34:32 +01:00
if(node->hasChildren())
2012-01-02 21:46:40 +01:00
m_layout->applyStyle(node);
}
2011-08-22 21:13:52 +02:00
// anchors
else if(boost::starts_with(node->tag(), "anchors.")) {
UIWidgetPtr parent = getParent();
if(!parent)
throw OTMLException(node, "cannot create anchor, there is no parent widget!");
UIAnchorLayoutPtr anchorLayout = parent->getLayout()->asUIAnchorLayout();
if(!anchorLayout)
throw OTMLException(node, "cannot create anchor, the parent widget doesn't use anchor layout!");
2011-08-22 21:13:52 +02:00
std::string what = node->tag().substr(8);
if(what == "fill") {
anchorLayout->fill(asUIWidget(), node->value());
2011-08-22 21:13:52 +02:00
} else if(what == "centerIn") {
anchorLayout->centerIn(asUIWidget(), node->value());
2011-08-22 21:13:52 +02:00
} else {
Fw::AnchorEdge anchoredEdge = Fw::translateAnchorEdge(what);
2011-08-22 21:13:52 +02:00
std::string anchorDescription = node->value();
std::vector<std::string> split;
boost::split(split, anchorDescription, boost::is_any_of(std::string(".")));
if(split.size() != 2)
throw OTMLException(node, "invalid anchor description");
std::string hookedWidgetId = split[0];
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(split[1]);
2011-08-22 21:13:52 +02:00
if(anchoredEdge == Fw::AnchorNone)
2011-08-22 21:13:52 +02:00
throw OTMLException(node, "invalid anchor edge");
if(hookedEdge == Fw::AnchorNone)
2011-08-22 21:13:52 +02:00
throw OTMLException(node, "invalid anchor target edge");
anchorLayout->addAnchor(asUIWidget(), anchoredEdge, hookedWidgetId, hookedEdge);
2011-08-22 21:13:52 +02:00
}
2011-11-16 00:47:32 +01:00
// lua functions
} else if(boost::starts_with(node->tag(), "@")) {
// load once
2011-11-16 00:47:32 +01:00
if(m_firstOnStyle) {
std::string funcName = node->tag().substr(1);
std::string funcOrigin = "@" + node->source() + "[" + node->tag() + "]";
g_lua.loadFunction(node->value(), funcOrigin);
luaSetField(funcName);
}
// lua fields value
} else if(boost::starts_with(node->tag(), "&")) {
std::string fieldName = node->tag().substr(1);
std::string fieldOrigin = "@" + node->source() + "[" + node->tag() + "]";
g_lua.evaluateExpression(node->value(), fieldOrigin);
luaSetField(fieldName);
2011-08-22 21:13:52 +02:00
}
}
2011-11-16 00:47:32 +01:00
2011-11-16 22:00:40 +01:00
if(m_firstOnStyle) {
2012-01-06 09:48:59 +01:00
callLuaField("onSetup");
2011-11-16 22:00:40 +01:00
// always focus new child
if(isFocusable() && isExplicitlyVisible() && isExplicitlyEnabled())
focus();
}
2011-11-16 00:47:32 +01:00
m_firstOnStyle = false;
2011-08-22 21:13:52 +02:00
}
2012-01-05 03:42:17 +01:00
void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect)
2011-08-22 14:44:26 +02:00
{
2012-01-05 03:42:17 +01:00
callLuaField("onGeometryChange", oldRect, newRect);
2011-08-22 14:44:26 +02:00
}
void UIWidget::onFocusChange(bool focused, Fw::FocusReason reason)
2011-08-21 03:01:46 +02:00
{
callLuaField("onFocusChange", focused, reason);
2011-08-21 03:01:46 +02:00
}
2011-08-22 14:44:26 +02:00
void UIWidget::onHoverChange(bool hovered)
2011-08-14 04:09:11 +02:00
{
callLuaField("onHoverChange", hovered);
2011-11-03 20:07:07 +01:00
// check for new hovered elements when the current widget is removed
2011-11-13 06:11:47 +01:00
if(!hovered && !getParent() && g_ui.getRootWidget())
2011-11-03 20:07:07 +01:00
g_ui.getRootWidget()->updateState(Fw::HoverState);
2011-08-22 14:44:26 +02:00
}
void UIWidget::onTextChange(const std::string& text)
{
callLuaField("onTextChange", text);
}
void UIWidget::onFontChange(const std::string& font)
{
callLuaField("onFontChange", font);
}
bool UIWidget::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
2011-08-22 14:44:26 +02:00
{
2012-01-05 03:42:17 +01:00
return callLuaField<bool>("onKeyPress", keyCode, keyText, keyboardModifiers);
}
bool UIWidget::onKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers)
{
return callLuaField<bool>("onKeyRelease", keyCode, keyText, keyboardModifiers);
}
2012-01-05 03:42:17 +01:00
bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
{
return callLuaField<bool>("onMousePress", mousePos, button);
}
void UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(isPressed() && getRect().contains(mousePos))
callLuaField("onClick");
callLuaField("onMouseRelease", mousePos, button);
}
bool UIWidget::onMouseMove(const Point& mousePos, const Point& mouseMoved)
{
return callLuaField<bool>("onMouseMove", mousePos, mouseMoved);
}
bool UIWidget::onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction)
{
return callLuaField<bool>("onMouseWheel", mousePos, direction);
}
bool UIWidget::propagateOnKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
{
2011-08-14 04:09:11 +02:00
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
2011-08-22 21:13:52 +02:00
// events on hidden or disabled widgets are discarded
2011-08-21 03:01:46 +02:00
if(!child->isExplicitlyEnabled() || !child->isExplicitlyVisible())
2011-08-14 04:09:11 +02:00
continue;
2011-08-14 04:09:11 +02:00
// key events go only to containers or focused child
if(child->isFocused())
children.push_back(child);
}
for(const UIWidgetPtr& child : children) {
2012-01-05 03:42:17 +01:00
if(child->propagateOnKeyPress(keyCode, keyText, keyboardModifiers))
return true;
2011-08-14 04:09:11 +02:00
}
2011-08-22 14:44:26 +02:00
2012-01-05 03:42:17 +01:00
return onKeyPress(keyCode, keyText, keyboardModifiers);
2011-08-14 04:09:11 +02:00
}
2012-01-05 03:42:17 +01:00
bool UIWidget::propagateOnKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers)
2011-08-14 04:09:11 +02:00
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
2011-08-22 21:13:52 +02:00
// events on hidden or disabled widgets are discarded
2011-08-21 03:01:46 +02:00
if(!child->isExplicitlyEnabled() || !child->isExplicitlyVisible())
2011-08-14 04:09:11 +02:00
continue;
// key events go only to focused child
if(child->isFocused())
children.push_back(child);
}
for(const UIWidgetPtr& child : children) {
2012-01-05 03:42:17 +01:00
if(child->propagateOnKeyRelease(keyCode, keyText, keyboardModifiers))
return true;
2011-08-14 04:09:11 +02:00
}
2011-08-22 14:44:26 +02:00
2012-01-05 03:42:17 +01:00
return onKeyRelease(keyCode, keyText, keyboardModifiers);
2011-08-14 04:09:11 +02:00
}
2012-01-05 03:42:17 +01:00
bool UIWidget::propagateOnMousePress(const Point& mousePos, Fw::MouseButton button)
2011-08-14 04:09:11 +02:00
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
2011-08-22 21:13:52 +02:00
// events on hidden or disabled widgets are discarded
2011-08-21 03:01:46 +02:00
if(!child->isExplicitlyEnabled() || !child->isExplicitlyVisible())
2011-08-14 04:09:11 +02:00
continue;
// mouse press events only go to children that contains the mouse position
2012-01-02 23:09:49 +01:00
if(child->containsPoint(mousePos) && child == getChildByPos(mousePos))
children.push_back(child);
}
for(const UIWidgetPtr& child : children) {
// when a focusable item is focused it must gain focus
if(child->isFocusable())
focusChild(child, Fw::MouseFocusReason);
2012-01-05 03:42:17 +01:00
bool mustEnd = child->propagateOnMousePress(mousePos, button);
2011-11-01 17:41:15 +01:00
if(button == Fw::MouseLeftButton && !child->isPressed()) {
UIWidgetPtr clickedChild = child->getChildByPos(mousePos);
if(!clickedChild || clickedChild->isPhantom())
child->setPressed(true);
}
if(mustEnd)
return true;
2011-08-14 04:09:11 +02:00
}
2011-08-22 14:44:26 +02:00
2012-01-05 03:42:17 +01:00
if(!isPhantom())
return onMousePress(mousePos, button);
else
return false;
2011-08-14 04:09:11 +02:00
}
2012-01-05 03:42:17 +01:00
void UIWidget::propagateOnMouseRelease(const Point& mousePos, Fw::MouseButton button)
2011-08-14 04:09:11 +02:00
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
2011-08-22 21:13:52 +02:00
// events on hidden or disabled widgets are discarded
2011-08-21 03:01:46 +02:00
if(!child->isExplicitlyEnabled() || !child->isExplicitlyVisible())
2011-08-14 04:09:11 +02:00
continue;
2011-08-14 04:09:11 +02:00
// mouse release events go to all children
children.push_back(child);
}
for(const UIWidgetPtr& child : children) {
2012-01-05 03:42:17 +01:00
child->propagateOnMouseRelease(mousePos, button);
2012-01-05 03:42:17 +01:00
if(child->isPressed() && button == Fw::MouseLeftButton)
child->setPressed(false);
2011-08-14 04:09:11 +02:00
}
2012-01-05 03:42:17 +01:00
// fire release events only when pressed
if(isPressed())
onMouseRelease(mousePos, button);
2011-08-14 04:09:11 +02:00
}
2012-01-05 03:42:17 +01:00
bool UIWidget::propagateOnMouseMove(const Point& mousePos, const Point& mouseMoved)
2011-08-14 04:09:11 +02:00
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
2011-08-22 21:13:52 +02:00
// events on hidden or disabled widgets are discarded
2011-08-21 03:01:46 +02:00
if(!child->isExplicitlyEnabled() || !child->isExplicitlyVisible())
2011-08-14 04:09:11 +02:00
continue;
// mouse move events go to all children
children.push_back(child);
}
for(const UIWidgetPtr& child : children) {
2012-01-05 03:42:17 +01:00
if(child->propagateOnMouseMove(mousePos, mouseMoved))
2011-08-22 14:44:26 +02:00
return true;
2011-08-14 04:09:11 +02:00
}
2011-08-22 14:44:26 +02:00
2012-01-05 03:42:17 +01:00
if(!isPhantom())
return onMouseMove(mousePos, mouseMoved);
else
return false;
2011-08-14 04:09:11 +02:00
}
2012-01-05 03:42:17 +01:00
bool UIWidget::propagateOnMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction)
2011-08-14 04:09:11 +02:00
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
2011-08-22 21:13:52 +02:00
// events on hidden or disabled widgets are discarded
2011-08-21 03:01:46 +02:00
if(!child->isExplicitlyEnabled() || !child->isExplicitlyVisible())
2011-08-14 04:09:11 +02:00
continue;
2011-08-14 04:09:11 +02:00
// mouse wheel events only go to children that contains the mouse position
2012-01-02 23:09:49 +01:00
if(child->containsPoint(mousePos) && child == getChildByPos(mousePos))
children.push_back(child);
}
for(const UIWidgetPtr& child : children) {
2012-01-05 03:42:17 +01:00
if(child->propagateOnMouseWheel(mousePos, direction))
return true;
2011-08-14 04:09:11 +02:00
}
2011-08-22 14:44:26 +02:00
2012-01-05 03:42:17 +01:00
if(!isPhantom())
return onMouseWheel(mousePos, direction);
else
return false;
2011-08-14 04:09:11 +02:00
}