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"
|
2011-08-26 17:06:52 +02:00
|
|
|
#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>
|
2011-12-03 22:41:37 +01:00
|
|
|
#include <framework/platform/platformwindow.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;
|
2011-08-28 18:02:26 +02:00
|
|
|
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;
|
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;
|
2011-08-28 18:02:26 +02:00
|
|
|
m_id = Fw::mkstr("widget", id++);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +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);
|
|
|
|
|
2011-08-22 21:39:46 +02:00
|
|
|
// remove itself from parent
|
2011-11-13 05:13:07 +01:00
|
|
|
if(UIWidgetPtr parent = getParent()) {
|
|
|
|
if(parent->hasChild(asUIWidget()))
|
|
|
|
parent->removeChild(asUIWidget());
|
|
|
|
}
|
2011-08-14 16:09:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
void UIWidget::render()
|
2011-11-03 10:59:11 +01:00
|
|
|
{
|
|
|
|
renderSelf();
|
|
|
|
renderChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::renderSelf()
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
// draw background
|
|
|
|
if(m_image) {
|
2011-12-07 01:31:55 +01:00
|
|
|
g_painter.setColor(m_backgroundColor);
|
2011-08-26 17:06:52 +02:00
|
|
|
m_image->draw(m_rect);
|
|
|
|
}
|
2011-11-03 10:59:11 +01:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-11-03 10:59:11 +01: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
|
2011-11-04 00:34:32 +01:00
|
|
|
if(child->isExplicitlyVisible() && child->getRect().isValid() && child->getRect().intersects(m_rect)) {
|
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
|
2011-08-14 19:45:25 +02:00
|
|
|
if(child->getOpacity() < oldOpacity)
|
2011-12-07 01:31:55 +01:00
|
|
|
g_painter.setOpacity(child->getOpacity());
|
2011-08-14 19:45:25 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
child->render();
|
2011-08-14 19:45:25 +02:00
|
|
|
|
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());
|
2011-11-01 19:32:48 +01:00
|
|
|
//g_fonts.getDefaultFont()->renderText(child->getId(), child->getPosition() + 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 19:45:25 +02:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 22:00:40 +01:00
|
|
|
void UIWidget::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if(enabled != m_enabled) {
|
|
|
|
m_enabled = enabled;
|
|
|
|
|
|
|
|
updateState(Fw::DisabledState);
|
|
|
|
updateState(Fw::ActiveState);
|
|
|
|
updateState(Fw::HoverState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +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");
|
2011-12-01 23:25:32 +01:00
|
|
|
return;
|
2012-01-02 21:46:40 +01:00
|
|
|
}
|
2011-08-26 17:06:52 +02:00
|
|
|
applyStyle(styleNode);
|
|
|
|
m_style = styleNode;
|
2011-11-16 06:23:16 +01:00
|
|
|
updateStyle();
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::setStyleFromNode(const OTMLNodePtr& styleNode)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
applyStyle(styleNode);
|
|
|
|
m_style = styleNode;
|
2011-11-16 06:23:16 +01:00
|
|
|
updateStyle();
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWidget::setParent(const UIWidgetPtr& parent)
|
|
|
|
{
|
2011-08-14 04:09:11 +02:00
|
|
|
// remove from old parent
|
2011-08-26 17:06:52 +02:00
|
|
|
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
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
// only update if the rect really changed
|
2011-08-14 04:09:11 +02:00
|
|
|
Rect oldRect = m_rect;
|
2011-08-26 17:06:52 +02:00
|
|
|
if(rect == oldRect)
|
|
|
|
return;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
m_rect = rect;
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// 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();
|
2011-08-14 16:09:26 +02:00
|
|
|
g_dispatcher.addEvent([self, oldRect]() {
|
2011-08-21 21:43:05 +02:00
|
|
|
self->m_updateEventScheduled = false;
|
2011-08-26 17:06:52 +02:00
|
|
|
self->onGeometryUpdate(oldRect, self->getRect());
|
2011-08-14 04:09:11 +02:00
|
|
|
});
|
|
|
|
}
|
2011-08-26 17:06:52 +02:00
|
|
|
m_updateEventScheduled = true;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
void UIWidget::lock()
|
|
|
|
{
|
|
|
|
if(UIWidgetPtr parent = getParent())
|
|
|
|
parent->lockChild(asUIWidget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::unlock()
|
|
|
|
{
|
|
|
|
if(UIWidgetPtr parent = getParent())
|
|
|
|
parent->unlockChild(asUIWidget());
|
|
|
|
}
|
|
|
|
|
2011-11-03 10:59:11 +01:00
|
|
|
void UIWidget::focus()
|
|
|
|
{
|
2011-11-16 22:00:40 +01:00
|
|
|
if(!m_focusable)
|
|
|
|
return;
|
2011-11-03 10:59:11 +01:00
|
|
|
if(UIWidgetPtr parent = getParent())
|
|
|
|
parent->focusChild(asUIWidget(), Fw::ActiveFocusReason);
|
|
|
|
}
|
|
|
|
|
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
|
2011-08-26 17:06:52 +02:00
|
|
|
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)
|
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
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);
|
2011-08-21 21:43:05 +02:00
|
|
|
if(widget->isExplicitlyVisible() && widget->getRect().contains(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) {
|
2011-08-21 21:43:05 +02:00
|
|
|
if(child->getRect().contains(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;
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
void UIWidget::focusChild(const UIWidgetPtr& child, Fw::FocusReason reason)
|
2011-08-14 16:09:26 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
if(child && !hasChild(child)) {
|
2011-12-01 23:25:32 +01:00
|
|
|
logError("Attempt to focus an unknown child in a UIWidget");
|
2011-08-26 17:06:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-22 21:13:52 +02:00
|
|
|
|
|
|
|
if(child != m_focusedChild) {
|
2011-08-14 16:09:26 +02:00
|
|
|
UIWidgetPtr oldFocused = m_focusedChild;
|
2011-08-22 21:13:52 +02:00
|
|
|
m_focusedChild = child;
|
2011-08-14 16:09:26 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
if(child) {
|
|
|
|
child->setLastFocusReason(reason);
|
2011-08-28 18:02:26 +02:00
|
|
|
child->updateState(Fw::FocusState);
|
|
|
|
child->updateState(Fw::ActiveState);
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
2011-08-22 21:13:52 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
if(oldFocused) {
|
|
|
|
oldFocused->setLastFocusReason(reason);
|
2011-08-28 18:02:26 +02:00
|
|
|
oldFocused->updateState(Fw::FocusState);
|
|
|
|
oldFocused->updateState(Fw::ActiveState);
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
2011-08-14 16:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
void UIWidget::addChild(const UIWidgetPtr& child)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
if(!child) {
|
|
|
|
logWarning("attempt to add a null child into a UIWidget");
|
2011-08-19 20:53:23 +02:00
|
|
|
return;
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
2011-08-19 20:53:23 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
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
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// create default layout
|
|
|
|
if(!m_layout)
|
2012-01-02 21:46:40 +01:00
|
|
|
m_layout = UIAnchorLayout::create(asUIWidget());
|
2011-08-26 17:06:52 +02:00
|
|
|
|
|
|
|
// add to layout and updates it
|
|
|
|
m_layout->addWidget(child);
|
|
|
|
|
|
|
|
// update new child states
|
|
|
|
child->updateStates();
|
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
|
|
|
{
|
2011-08-26 17:06:52 +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-26 17:06:52 +02:00
|
|
|
}
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-26 17:06:52 +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
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// create default layout if needed
|
|
|
|
if(!m_layout)
|
2012-01-02 21:46:40 +01:00
|
|
|
m_layout = UIAnchorLayout::create(asUIWidget());
|
2011-08-26 17:06:52 +02:00
|
|
|
|
|
|
|
// add to layout and updates it
|
|
|
|
m_layout->addWidget(child);
|
|
|
|
|
|
|
|
// update new child states
|
|
|
|
child->updateStates();
|
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
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
// remove from children list
|
2011-08-29 20:38:01 +02:00
|
|
|
if(hasChild(child)) {
|
2011-08-26 17:06:52 +02:00
|
|
|
// defocus if needed
|
2011-08-29 16:14:21 +02:00
|
|
|
bool focusAnother = false;
|
|
|
|
if(m_focusedChild == child) {
|
2011-08-28 18:02:26 +02:00
|
|
|
focusChild(nullptr, Fw::ActiveFocusReason);
|
2011-08-29 16:14:21 +02:00
|
|
|
focusAnother = true;
|
|
|
|
}
|
2011-08-19 20:53:23 +02:00
|
|
|
|
2011-08-29 20:38:01 +02:00
|
|
|
if(isChildLocked(child))
|
|
|
|
unlockChild(child);
|
2011-08-14 16:09:26 +02:00
|
|
|
|
2011-08-29 20:38:01 +02:00
|
|
|
auto it = std::find(m_children.begin(), m_children.end(), child);
|
2011-08-26 17:06:52 +02:00
|
|
|
m_children.erase(it);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// reset child parent
|
|
|
|
assert(child->getParent() == asUIWidget());
|
|
|
|
child->setParent(nullptr);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
m_layout->removeWidget(child);
|
2011-08-21 21:43:05 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// update child states
|
|
|
|
child->updateStates();
|
2011-08-29 16:14:21 +02:00
|
|
|
|
2011-08-29 20:38:01 +02:00
|
|
|
if(focusAnother && !m_focusedChild)
|
2011-08-29 16:14:21 +02:00
|
|
|
focusPreviousChild(Fw::ActiveFocusReason);
|
2011-08-26 17:06:52 +02:00
|
|
|
} else
|
2011-12-01 23:25:32 +01:00
|
|
|
logError("Attempt to remove an unknown child from a UIWidget");
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +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)
|
2011-08-14 16:09:26 +02:00
|
|
|
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);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-11-11 21:26:10 +01: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);
|
|
|
|
}
|
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
void UIWidget::lockChild(const UIWidgetPtr& child)
|
2011-08-14 16:09:26 +02:00
|
|
|
{
|
2011-08-22 21:13:52 +02:00
|
|
|
if(!child)
|
|
|
|
return;
|
2011-08-14 16:09:26 +02:00
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
assert(hasChild(child));
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// prevent double locks
|
2011-08-29 20:38:01 +02:00
|
|
|
if(isChildLocked(child))
|
|
|
|
unlockChild(child);
|
2011-08-26 17:06:52 +02:00
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
// disable all other children
|
|
|
|
for(const UIWidgetPtr& otherChild : m_children) {
|
|
|
|
if(otherChild == child)
|
|
|
|
child->setEnabled(true);
|
2011-08-14 16:09:26 +02:00
|
|
|
else
|
2011-08-22 21:13:52 +02:00
|
|
|
otherChild->setEnabled(false);
|
2011-08-14 16:09:26 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2011-08-26 17:06:52 +02:00
|
|
|
|
|
|
|
moveChildToTop(child);
|
2011-08-14 16:09:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
void UIWidget::unlockChild(const UIWidgetPtr& child)
|
2011-08-14 16:09:26 +02:00
|
|
|
{
|
2011-08-22 21:13:52 +02:00
|
|
|
if(!child)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(hasChild(child));
|
2011-08-14 16:09:26 +02:00
|
|
|
|
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-14 16:09:26 +02:00
|
|
|
|
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-14 16:09:26 +02:00
|
|
|
}
|
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();
|
2011-08-14 16:09:26 +02:00
|
|
|
}
|
|
|
|
|
2011-11-11 21:26:10 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWidget::updateParentLayout()
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
if(UIWidgetPtr parent = getParent())
|
|
|
|
parent->updateLayout();
|
|
|
|
else
|
|
|
|
updateLayout();
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWidget::updateLayout()
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
if(m_layout)
|
|
|
|
m_layout->update();
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 18:03:11 +01:00
|
|
|
bool UIWidget::setState(Fw::WidgetState state, bool on)
|
2011-11-16 00:47:32 +01:00
|
|
|
{
|
|
|
|
if(state == Fw::InvalidState)
|
2011-11-16 18:03:11 +01:00
|
|
|
return false;
|
2011-11-16 00:47:32 +01:00
|
|
|
|
|
|
|
int oldStates = m_states;
|
|
|
|
if(on)
|
|
|
|
m_states |= state;
|
|
|
|
else
|
|
|
|
m_states &= ~state;
|
|
|
|
|
2011-11-16 18:03:11 +01:00
|
|
|
if(oldStates != m_states) {
|
2011-11-16 00:47:32 +01:00
|
|
|
updateStyle();
|
2011-11-16 18:03:11 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
void UIWidget::updateState(Fw::WidgetState state)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
bool newStatus = true;
|
|
|
|
bool oldStatus = hasState(state);
|
|
|
|
bool updateChildren = false;
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
if(state == Fw::ActiveState) {
|
2011-08-26 17:06:52 +02:00
|
|
|
UIWidgetPtr widget = asUIWidget();
|
|
|
|
UIWidgetPtr parent;
|
|
|
|
do {
|
|
|
|
parent = widget->getParent();
|
|
|
|
if(!widget->isExplicitlyEnabled() ||
|
|
|
|
((parent && parent->getFocusedChild() != widget))) {
|
|
|
|
newStatus = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(widget = parent);
|
|
|
|
|
|
|
|
updateChildren = true;
|
|
|
|
}
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(state == Fw::FocusState) {
|
2011-08-26 17:06:52 +02:00
|
|
|
newStatus = (getParent() && getParent()->getFocusedChild() == asUIWidget());
|
|
|
|
}
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(state == Fw::HoverState) {
|
2011-08-26 17:06:52 +02:00
|
|
|
updateChildren = true;
|
2011-12-03 22:41:37 +01:00
|
|
|
Point mousePos = g_window.getMousePos();
|
2011-08-26 17:06:52 +02:00
|
|
|
UIWidgetPtr widget = asUIWidget();
|
|
|
|
UIWidgetPtr parent;
|
|
|
|
do {
|
|
|
|
parent = widget->getParent();
|
2011-11-16 22:00:40 +01:00
|
|
|
if(!widget->isExplicitlyEnabled() || !widget->isExplicitlyVisible() || !widget->getRect().contains(mousePos) ||
|
2011-08-26 17:06:52 +02:00
|
|
|
(parent && widget != parent->getChildByPos(mousePos))) {
|
|
|
|
newStatus = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(widget = parent);
|
2011-08-23 03:08:36 +02:00
|
|
|
}
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(state == Fw::PressedState) {
|
2011-08-26 17:06:52 +02:00
|
|
|
newStatus = m_pressed;
|
|
|
|
}
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(state == Fw::DisabledState) {
|
2011-08-26 20:56:56 +02:00
|
|
|
bool enabled = true;
|
2011-08-26 17:06:52 +02:00
|
|
|
updateChildren = true;
|
|
|
|
UIWidgetPtr widget = asUIWidget();
|
|
|
|
do {
|
|
|
|
if(!widget->isExplicitlyEnabled()) {
|
2011-08-26 20:56:56 +02:00
|
|
|
enabled = false;
|
2011-08-26 17:06:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(widget = widget->getParent());
|
2011-08-26 20:56:56 +02:00
|
|
|
newStatus = !enabled;
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
2011-08-21 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +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)
|
2011-08-26 17:06:52 +02:00
|
|
|
child->updateState(state);
|
|
|
|
}
|
2011-08-21 21:43:05 +02:00
|
|
|
|
2011-11-16 18:03:11 +01:00
|
|
|
if(setState(state, newStatus)) {
|
2011-11-13 05:13:07 +01:00
|
|
|
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-26 17:06:52 +02:00
|
|
|
}
|
2011-08-21 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWidget::updateStates()
|
2011-08-21 21:43:05 +02:00
|
|
|
{
|
2011-11-16 00:47:32 +01:00
|
|
|
for(int state = 1; state != Fw::LastState; state <<= 1)
|
|
|
|
updateState((Fw::WidgetState)state);
|
2011-08-21 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWidget::updateStyle()
|
2011-08-21 21:43:05 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
if(!m_style)
|
|
|
|
return;
|
2011-08-21 21:43:05 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
OTMLNodePtr newStateStyle = OTMLNode::create();
|
2011-08-21 21:43:05 +02:00
|
|
|
|
2011-08-26 17:06:52 +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
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
applyStyle(newStateStyle);
|
|
|
|
m_stateStyle = newStateStyle;
|
2011-08-21 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
|
2011-08-21 21:43:05 +02:00
|
|
|
{
|
2011-08-26 17:06:52 +02:00
|
|
|
try {
|
|
|
|
onStyleApply(styleNode);
|
2011-11-14 15:30:35 +01:00
|
|
|
callLuaField("onStyleApply", styleNode);
|
2011-12-01 23:25:32 +01:00
|
|
|
} catch(Exception& e) {
|
|
|
|
logError("Failed to apply style to widget '", m_id, "' style: ", e.what());
|
2011-08-21 21:43:05 +02:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
void UIWidget::onStyleApply(const OTMLNodePtr& styleNode)
|
|
|
|
{
|
2011-08-26 17:06: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
|
2011-08-26 17:06:52 +02:00
|
|
|
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));
|
2011-11-16 19:08:42 +01:00
|
|
|
else if(node->tag() == "font")
|
2011-08-22 21:13:52 +02:00
|
|
|
setFont(g_fonts.getFont(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>());
|
|
|
|
else if(node->tag() == "focusable")
|
2011-08-26 17:06:52 +02:00
|
|
|
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")
|
2011-08-26 17:06:52 +02:00
|
|
|
setSizeFixed(node->value<bool>());
|
2011-11-16 19:08:42 +01:00
|
|
|
else if(node->tag() == "position")
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 17:06:52 +02:00
|
|
|
// 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")
|
|
|
|
layout = UIVerticalLayout::create(asUIWidget());
|
|
|
|
else if(layoutType == "anchor")
|
|
|
|
layout = UIAnchorLayout::create(asUIWidget());
|
|
|
|
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-26 17:06:52 +02:00
|
|
|
}
|
2011-08-22 21:13:52 +02:00
|
|
|
// anchors
|
|
|
|
else if(boost::starts_with(node->tag(), "anchors.")) {
|
2011-08-26 17:06:52 +02:00
|
|
|
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") {
|
2011-08-26 17:06:52 +02:00
|
|
|
anchorLayout->fill(asUIWidget(), node->value());
|
2011-08-22 21:13:52 +02:00
|
|
|
} else if(what == "centerIn") {
|
2011-08-26 17:06:52 +02:00
|
|
|
anchorLayout->centerIn(asUIWidget(), node->value());
|
2011-08-22 21:13:52 +02:00
|
|
|
} else {
|
2011-08-28 18:02:26 +02:00
|
|
|
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];
|
2011-08-28 18:02:26 +02:00
|
|
|
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(split[1]);
|
2011-08-22 21:13:52 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
if(anchoredEdge == Fw::AnchorNone)
|
2011-08-22 21:13:52 +02:00
|
|
|
throw OTMLException(node, "invalid anchor edge");
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
if(hookedEdge == Fw::AnchorNone)
|
2011-08-22 21:13:52 +02:00
|
|
|
throw OTMLException(node, "invalid anchor target edge");
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
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(), "@")) {
|
|
|
|
// on load once
|
|
|
|
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) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2011-08-22 14:44:26 +02:00
|
|
|
void UIWidget::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
|
|
|
|
{
|
2011-08-28 23:32:24 +02:00
|
|
|
callLuaField("onGeometryUpdate", oldRect, newRect);
|
2011-08-22 14:44:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
void UIWidget::onFocusChange(bool focused, Fw::FocusReason reason)
|
2011-08-21 03:01:46 +02:00
|
|
|
{
|
2011-10-31 07:04:08 +01: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
|
|
|
{
|
2011-08-28 23:32:24 +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
|
|
|
}
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
bool UIWidget::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
|
2011-08-22 14:44:26 +02:00
|
|
|
{
|
2011-12-03 22:41:37 +01:00
|
|
|
if(callLuaField<bool>("onKeyPress", keyCode, keyText, keyboardModifiers))
|
2011-08-28 23:32:24 +02:00
|
|
|
return true;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// do a backup of children list, because it may change while looping it
|
2011-08-26 17:06:52 +02:00
|
|
|
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 16:09:26 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// key events go only to containers or focused child
|
2011-08-26 17:06:52 +02:00
|
|
|
if(child->isFocused())
|
|
|
|
children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const UIWidgetPtr& child : children) {
|
2011-12-03 22:41:37 +01:00
|
|
|
if(child->onKeyPress(keyCode, keyText, keyboardModifiers))
|
2011-08-26 17:06:52 +02:00
|
|
|
return true;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-08-22 14:44:26 +02:00
|
|
|
|
|
|
|
return false;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
bool UIWidget::onKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-12-03 22:41:37 +01:00
|
|
|
if(callLuaField<bool>("onKeyRelease", keyCode, keyText, keyboardModifiers))
|
2011-08-28 23:32:24 +02:00
|
|
|
return true;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// do a backup of children list, because it may change while looping it
|
2011-08-26 17:06:52 +02:00
|
|
|
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 16:09:26 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
// key events go only to focused child
|
|
|
|
if(child->isFocused())
|
|
|
|
children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const UIWidgetPtr& child : children) {
|
2011-12-03 22:41:37 +01:00
|
|
|
if(child->onKeyRelease(keyCode, keyText, keyboardModifiers))
|
2011-08-26 17:06:52 +02:00
|
|
|
return true;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-08-22 14:44:26 +02:00
|
|
|
|
|
|
|
return false;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-28 23:32:24 +02:00
|
|
|
if(callLuaField<bool>("onMousePress", mousePos, button))
|
|
|
|
return true;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// do a backup of children list, because it may change while looping it
|
2011-08-26 17:06:52 +02:00
|
|
|
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 16:09:26 +02:00
|
|
|
// mouse press events only go to children that contains the mouse position
|
2011-08-26 17:06:52 +02:00
|
|
|
if(child->getRect().contains(mousePos) && child == getChildByPos(mousePos))
|
|
|
|
children.push_back(child);
|
|
|
|
}
|
2011-08-14 16:09:26 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
for(const UIWidgetPtr& child : children) {
|
|
|
|
// when a focusable item is focused it must gain focus
|
|
|
|
if(child->isFocusable())
|
2011-08-28 18:02:26 +02:00
|
|
|
focusChild(child, Fw::MouseFocusReason);
|
2011-08-26 17:06:52 +02:00
|
|
|
|
|
|
|
bool mustEnd = child->onMousePress(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);
|
|
|
|
}
|
2011-08-26 17:06:52 +02:00
|
|
|
|
|
|
|
if(mustEnd)
|
|
|
|
return true;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-08-22 14:44:26 +02:00
|
|
|
|
|
|
|
return false;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-11-13 06:11:47 +01:00
|
|
|
void UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-11-13 06:11:47 +01:00
|
|
|
callLuaField("onMouseRelease", mousePos, button);
|
2011-08-28 23:32:24 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// do a backup of children list, because it may change while looping it
|
2011-08-26 17:06:52 +02:00
|
|
|
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 16:09:26 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// mouse release events go to all children
|
2011-08-26 17:06:52 +02:00
|
|
|
children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const UIWidgetPtr& child : children) {
|
2011-11-13 06:11:47 +01:00
|
|
|
child->onMouseRelease(mousePos, button);
|
2011-08-26 17:06:52 +02:00
|
|
|
|
|
|
|
if(child->isPressed())
|
|
|
|
child->setPressed(false);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 14:44:26 +02:00
|
|
|
bool UIWidget::onMouseMove(const Point& mousePos, const Point& mouseMoved)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-28 23:32:24 +02:00
|
|
|
if(callLuaField<bool>("onMouseMove", mousePos, mouseMoved))
|
|
|
|
return true;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// do a backup of children list, because it may change while looping it
|
2011-08-26 17:06:52 +02:00
|
|
|
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
|
2011-08-26 17:06:52 +02:00
|
|
|
children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const UIWidgetPtr& child : children) {
|
2011-08-22 14:44:26 +02:00
|
|
|
if(child->onMouseMove(mousePos, mouseMoved))
|
|
|
|
return true;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-08-22 14:44:26 +02:00
|
|
|
|
|
|
|
return false;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
bool UIWidget::onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-08-28 23:32:24 +02:00
|
|
|
if(callLuaField<bool>("onMouseWheel", mousePos, direction))
|
|
|
|
return true;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// do a backup of children list, because it may change while looping it
|
2011-08-26 17:06:52 +02:00
|
|
|
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 16:09:26 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// mouse wheel events only go to children that contains the mouse position
|
2011-08-26 17:06:52 +02:00
|
|
|
if(child->getRect().contains(mousePos) && child == getChildByPos(mousePos))
|
|
|
|
children.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const UIWidgetPtr& child : children) {
|
|
|
|
if(child->onMouseWheel(mousePos, direction))
|
|
|
|
return true;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-08-22 14:44:26 +02:00
|
|
|
|
|
|
|
return false;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|