save window position and size

This commit is contained in:
Eduardo Bart
2012-01-06 06:48:59 -02:00
parent 0cb5facd7a
commit 028441831d
31 changed files with 315 additions and 158 deletions

View File

@@ -31,25 +31,17 @@
#include <framework/graphics/graphics.h>
#include <framework/platform/platformwindow.h>
class LuaRect : public LuaObject, public Rect {
public:
LuaRect() : TRect() { }
};
void Application::registerLuaFunctions()
{
// globals
g_lua.registerStaticClass("Rect");
g_lua.bindClassStaticFunction("Rect", "create", []{ return std::shared_ptr<LuaRect>(new LuaRect); });
g_lua.bindClassMemberFunction<LuaRect>("Rect", "resize", (void (Rect::*)(int,int)) &Rect::resize);
/*
g_lua.bindGlobalFunction("torect", [](const std::string& str) { return Fw::unsafeCast<Rect>(str); });
g_lua.bindGlobalFunction("topoint", [](const std::string& str) { return Fw::unsafeCast<Point>(str); });
g_lua.bindGlobalFunction("tocolor", [](const std::string& str) { return Fw::unsafeCast<Color>(str); });
g_lua.bindGlobalFunction("tosize", [](const std::string& str) { return Fw::unsafeCast<Size>(str); });
g_lua.bindGlobalFunction("toboolean", [](const std::string& str) { return Fw::unsafeCast<bool>(str); });
*/
// conversion globals
g_lua.bindGlobalFunction("torect", [](const std::string& v) { return Fw::fromstring<Rect>(v); });
g_lua.bindGlobalFunction("topoint", [](const std::string& v) { return Fw::fromstring<Point>(v); });
g_lua.bindGlobalFunction("tocolor", [](const std::string& v) { return Fw::fromstring<Color>(v); });
g_lua.bindGlobalFunction("tosize", [](const std::string& v) { return Fw::fromstring<Size>(v); });
g_lua.bindGlobalFunction("recttostring", [](const Rect& v) { return Fw::tostring(v); });
g_lua.bindGlobalFunction("pointtostring", [](const Point& v) { return Fw::tostring(v); });
g_lua.bindGlobalFunction("colortostring", [](const Color& v) { return Fw::tostring(v); });
g_lua.bindGlobalFunction("sizetostring", [](const Size& v) { return Fw::tostring(v); });
// UIWidget
g_lua.registerClass<UIWidget>();
@@ -189,25 +181,25 @@ void Application::registerLuaFunctions()
// UIVerticalLayout
g_lua.registerClass<UIVerticalLayout, UILayout>();
g_lua.bindClassStaticFunction<UILayout>("create", &UIVerticalLayout::create);
g_lua.bindClassStaticFunction<UIVerticalLayout>("create", [](UIWidgetPtr parent){ return UIVerticalLayoutPtr(new UIVerticalLayout(parent)); } );
g_lua.bindClassMemberFunction<UIVerticalLayout>("setFitParent", &UIVerticalLayout::setFitParent);
// UIAnchorLayout
g_lua.registerClass<UIAnchorLayout, UILayout>();
g_lua.bindClassStaticFunction<UIAnchorLayout>("create", &UIAnchorLayout::create);
g_lua.bindClassStaticFunction<UIAnchorLayout>("create", [](UIWidgetPtr parent){ return UIAnchorLayoutPtr(new UIAnchorLayout(parent)); } );
g_lua.bindClassMemberFunction<UIAnchorLayout>("removeAnchors", &UIAnchorLayout::removeAnchors);
g_lua.bindClassMemberFunction<UIAnchorLayout>("centerIn", &UIAnchorLayout::centerIn);
g_lua.bindClassMemberFunction<UIAnchorLayout>("fill", &UIAnchorLayout::fill);
// UIProgressBar
g_lua.registerClass<UIProgressBar, UIWidget>();
g_lua.bindClassStaticFunction<UIProgressBar>("create", &UIWidget::create<UIProgressBar>);
g_lua.bindClassStaticFunction<UIProgressBar>("create", []{ return UIProgressBarPtr(new UIProgressBar); } );
g_lua.bindClassMemberFunction<UIProgressBar>("getPercent", &UIProgressBar::getPercent);
g_lua.bindClassMemberFunction<UIProgressBar>("setPercent", &UIProgressBar::setPercent);
// UILineEdit
g_lua.registerClass<UILineEdit, UIWidget>();
g_lua.bindClassStaticFunction<UILineEdit>("create", &UIWidget::create<UILineEdit>);
g_lua.bindClassStaticFunction<UILineEdit>("create", []{ return UILineEditPtr(new UILineEdit); } );
g_lua.bindClassMemberFunction<UILineEdit>("setTextHorizontalMargin", &UILineEdit::setTextHorizontalMargin);
g_lua.bindClassMemberFunction<UILineEdit>("setCursorPos", &UILineEdit::setCursorPos);
g_lua.bindClassMemberFunction<UILineEdit>("setCursorEnabled", &UILineEdit::setCursorEnabled);
@@ -226,17 +218,17 @@ void Application::registerLuaFunctions()
// UICheckBox
g_lua.registerClass<UICheckBox, UIWidget>();
g_lua.bindClassStaticFunction<UICheckBox>("create", &UIWidget::create<UICheckBox>);
g_lua.bindClassStaticFunction<UICheckBox>("create", []{ return UICheckBoxPtr(new UICheckBox); } );
// UIWindow
g_lua.registerClass<UIWindow, UIWidget>();
g_lua.bindClassStaticFunction<UIWindow>("create", &UIWidget::create<UIWindow>);
g_lua.bindClassStaticFunction<UIWindow>("create", []{ return UIWindowPtr(new UIWindow); } );
g_lua.bindClassMemberFunction<UIWindow>("getTitle", &UIWindow::getTitle);
g_lua.bindClassMemberFunction<UIWindow>("setTitle", &UIWindow::setTitle);
// UIFrameCounter
g_lua.registerClass<UIFrameCounter, UIWidget>();
g_lua.bindClassStaticFunction<UIFrameCounter>("create", &UIWidget::create<UIFrameCounter>);
g_lua.bindClassStaticFunction<UIFrameCounter>("create", []{ return UIFrameCounterPtr(new UIFrameCounter); } );
g_lua.bindClassMemberFunction<UIFrameCounter>("getFrameCount", &UIFrameCounter::getFrameCount);
// Protocol
@@ -250,21 +242,38 @@ void Application::registerLuaFunctions()
g_lua.registerStaticClass("g_window");
g_lua.bindClassStaticFunction("g_window", "show", std::bind(&PlatformWindow::show, &g_window));
g_lua.bindClassStaticFunction("g_window", "hide", std::bind(&PlatformWindow::hide, &g_window));
g_lua.bindClassStaticFunction("g_window", "move", std::bind(&PlatformWindow::move, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "resize", std::bind(&PlatformWindow::resize, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setMinimumSize", std::bind(&PlatformWindow::setMinimumSize, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setVerticalSync", std::bind(&PlatformWindow::setVerticalSync, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setFullscreen", std::bind(&PlatformWindow::setFullscreen, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "show", std::bind(&PlatformWindow::show, &g_window));
g_lua.bindClassStaticFunction("g_window", "hide", std::bind(&PlatformWindow::hide, &g_window));
g_lua.bindClassStaticFunction("g_window", "maximize", std::bind(&PlatformWindow::maximize, &g_window));
g_lua.bindClassStaticFunction("g_window", "showMouse", std::bind(&PlatformWindow::showMouse, &g_window));
g_lua.bindClassStaticFunction("g_window", "hideMouse", std::bind(&PlatformWindow::hideMouse, &g_window));
g_lua.bindClassStaticFunction("g_window", "setTitle", std::bind(&PlatformWindow::setTitle, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setMinimumSize", std::bind(&PlatformWindow::setMinimumSize, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setFullscreen", std::bind(&PlatformWindow::setFullscreen, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setVerticalSync", std::bind(&PlatformWindow::setVerticalSync, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setIcon", std::bind(&PlatformWindow::setIcon, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setClipboardText", std::bind(&PlatformWindow::setClipboardText, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "getMousePos", std::bind(&PlatformWindow::getMousePos, &g_window));
g_lua.bindClassStaticFunction("g_window", "getSize", std::bind(&PlatformWindow::getSize, &g_window));
g_lua.bindClassStaticFunction("g_window", "getDisplaySize", std::bind(&PlatformWindow::getDisplaySize, &g_window));
g_lua.bindClassStaticFunction("g_window", "getPlatformType", std::bind(&PlatformWindow::getPlatformType, &g_window));
g_lua.bindClassStaticFunction("g_window", "getClipboardText", std::bind(&PlatformWindow::getClipboardText, &g_window));
g_lua.bindClassStaticFunction("g_window", "getPlatformType", std::bind(&PlatformWindow::getPlatformType, &g_window));
g_lua.bindClassStaticFunction("g_window", "getDisplayWidth", std::bind(&PlatformWindow::getDisplayWidth, &g_window));
g_lua.bindClassStaticFunction("g_window", "getDisplayHeight", std::bind(&PlatformWindow::getDisplayHeight, &g_window));
g_lua.bindClassStaticFunction("g_window", "getUnmaximizedSize", std::bind(&PlatformWindow::getUnmaximizedSize, &g_window));
g_lua.bindClassStaticFunction("g_window", "getSize", std::bind(&PlatformWindow::getSize, &g_window));
g_lua.bindClassStaticFunction("g_window", "getWidth", std::bind(&PlatformWindow::getWidth, &g_window));
g_lua.bindClassStaticFunction("g_window", "getHeight", std::bind(&PlatformWindow::getHeight, &g_window));
g_lua.bindClassStaticFunction("g_window", "getUnmaximizedPos", std::bind(&PlatformWindow::getUnmaximizedPos, &g_window));
g_lua.bindClassStaticFunction("g_window", "getPos", std::bind(&PlatformWindow::getPos, &g_window));
g_lua.bindClassStaticFunction("g_window", "getX", std::bind(&PlatformWindow::getX, &g_window));
g_lua.bindClassStaticFunction("g_window", "getY", std::bind(&PlatformWindow::getY, &g_window));
g_lua.bindClassStaticFunction("g_window", "getMousePos", std::bind(&PlatformWindow::getMousePos, &g_window));
g_lua.bindClassStaticFunction("g_window", "getKeyboardModifiers", std::bind(&PlatformWindow::getKeyboardModifiers, &g_window));
g_lua.bindClassStaticFunction("g_window", "isVisible", std::bind(&PlatformWindow::isVisible, &g_window));
g_lua.bindClassStaticFunction("g_window", "isFullscreen", std::bind(&PlatformWindow::isFullscreen, &g_window));
g_lua.bindClassStaticFunction("g_window", "isMaximized", std::bind(&PlatformWindow::isMaximized, &g_window));
g_lua.bindClassStaticFunction("g_window", "hasFocus", std::bind(&PlatformWindow::hasFocus, &g_window));
// Logger
g_lua.registerClass<Logger>();

View File

@@ -302,25 +302,26 @@ void LuaInterface::loadScript(const std::string& fileName)
void LuaInterface::loadFunction(const std::string& buffer, const std::string& source)
{
// gets the function contained in that buffer
if(boost::starts_with(buffer, "function")) {
// evaluate the function
std::string buf = Fw::mkstr("__func = ", buffer);
loadBuffer(buf, source);
safeCall();
// get the function
getGlobal("__func");
// reset the global __func
if(buffer.empty()) {
pushNil();
setGlobal("__func");
return;
}
// gets the buffer as a function
else if(!buffer.empty())
loadBuffer(buffer, source);
std::string buf;
if(boost::starts_with(buffer, "function"))
buf = Fw::mkstr("__func = ", buffer);
else
pushNil();
buf = Fw::mkstr("__func = function(self)\n", buffer,"\nend");
loadBuffer(buf, source);
safeCall();
// get the function
getGlobal("__func");
// reset the global __func
pushNil();
setGlobal("__func");
}
void LuaInterface::evaluateExpression(const std::string& expression, const std::string& source)

View File

@@ -31,3 +31,11 @@ X11Window window;
#endif
PlatformWindow& g_window = window;
void PlatformWindow::updateUnmaximizedCoords()
{
if(!isMaximized()) {
m_unmaximizedPos = m_pos;
m_unmaximizedSize = m_size;
}
}

View File

@@ -59,9 +59,11 @@ public:
int getDisplayWidth() { return getDisplaySize().width(); }
int getDisplayHeight() { return getDisplaySize().width(); }
Size getUnmaximizedSize() { return m_unmaximizedSize; }
Size getSize() { return m_size; }
int getWidth() { return m_size.width(); }
int getHeight() { return m_size.height(); }
Point getUnmaximizedPos() { return m_unmaximizedPos; }
Point getPos() { return m_pos; }
int getX() { return m_pos.x; }
int getY() { return m_pos.y; }
@@ -70,6 +72,7 @@ public:
bool isVisible() { return m_visible; }
bool isFullscreen() { return m_fullscreen; }
virtual bool isMaximized() = 0;
bool hasFocus() { return m_focused; }
void setOnClose(const SimpleCallback& onClose) { m_onClose = onClose; }
@@ -77,8 +80,12 @@ public:
void setOnInputEvent(const OnInputEventCallback& onInputEvent) { m_onInputEvent = onInputEvent; }
protected:
void updateUnmaximizedCoords();
Size m_size;
Point m_pos;
Size m_unmaximizedSize;
Point m_unmaximizedPos;
InputEvent m_inputEvent;
Boolean<false> m_created;

View File

@@ -354,6 +354,7 @@ void WIN32Window::resize(const Size& size)
void WIN32Window::show()
{
updateUnmaximizedCoords();
if(m_maximized)
ShowWindow(m_window, SW_MAXIMIZE);
else
@@ -472,6 +473,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
case WM_MOVE: {
m_pos.x = LOWORD(lParam);
m_pos.y = HIWORD(lParam);
updateUnmaximizedCoords();
break;
}
case WM_SIZE: {
@@ -487,6 +489,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
m_visible = !(wParam == SIZE_MINIMIZED);
m_size.setWidth(LOWORD(lParam));
m_size.setHeight(HIWORD(lParam));
updateUnmaximizedCoords();
m_onResize(m_size);
break;
}

View File

@@ -278,6 +278,7 @@ void X11Window::internalCreateWindow()
vis = CopyFromParent;
}
updateUnmaximizedCoords();
m_window = XCreateWindow(m_display, m_rootWindow,
m_pos.x, m_pos.y, m_size.width(), m_size.height(),
0,
@@ -494,6 +495,8 @@ void X11Window::hide()
void X11Window::maximize()
{
updateUnmaximizedCoords();
Atom wmState = XInternAtom(m_display, "_NET_WM_STATE", False);
Atom wmStateMaximizedVert = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
Atom wmStateMaximizedHorz = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
@@ -561,6 +564,7 @@ void X11Window::poll()
// updates window pos
m_pos = newPos;
updateUnmaximizedCoords();
break;
}
case SelectionRequest: {
@@ -916,6 +920,9 @@ std::string X11Window::getPlatformType()
bool X11Window::isMaximized()
{
if(!m_display || !m_window)
return false;
Atom wmState = XInternAtom(m_display, "_NET_WM_STATE", False);
Atom wmStateMaximizedVert = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
Atom wmStateMaximizedHorz = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
@@ -940,7 +947,7 @@ bool X11Window::isMaximized()
}
if(maximizedMask == 3)
maximizedMask = true;
maximized = true;
XFree(propertyValue);
}

View File

@@ -62,7 +62,6 @@ class UIAnchorLayout : public UILayout
{
public:
UIAnchorLayout(UIWidgetPtr parentWidget) : UILayout(parentWidget) { }
static UIAnchorLayoutPtr create(UIWidgetPtr parentWidget) { return UIAnchorLayoutPtr(new UIAnchorLayout(parentWidget)); }
void addAnchor(const UIWidgetPtr& anchoredWidget, Fw::AnchorEdge anchoredEdge,
const std::string& hookedWidgetId, Fw::AnchorEdge hookedEdge);

View File

@@ -32,7 +32,7 @@ UIManager g_ui;
void UIManager::init()
{
// creates root widget
m_rootWidget = UIWidget::create<UIWidget>();
m_rootWidget = UIWidgetPtr(new UIWidget);
m_rootWidget->setId("root");
m_mouseReceiver = m_rootWidget;
m_keyboardReceiver = m_rootWidget;

View File

@@ -119,4 +119,3 @@ void UIVerticalLayout::setFitParent(bool fitParent)
m_fitParent = fitParent;
update();
}

View File

@@ -29,7 +29,6 @@ class UIVerticalLayout : public UILayout
{
public:
UIVerticalLayout(UIWidgetPtr parentWidget);
static UIVerticalLayoutPtr create(UIWidgetPtr parentWidget) { return UIVerticalLayoutPtr(new UIVerticalLayout(parentWidget)); }
virtual void applyStyle(const OTMLNodePtr& styleNode);
virtual void update();

View File

@@ -526,7 +526,7 @@ void UIWidget::addChild(const UIWidgetPtr& child)
// create default layout
if(!m_layout)
m_layout = UIAnchorLayout::create(asUIWidget());
m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
// add to layout and updates it
m_layout->addWidget(child);
@@ -559,7 +559,7 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child)
// create default layout if needed
if(!m_layout)
m_layout = UIAnchorLayout::create(asUIWidget());
m_layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
// add to layout and updates it
m_layout->addWidget(child);
@@ -829,7 +829,7 @@ void UIWidget::updateState(Fw::WidgetState state)
do {
parent = widget->getParent();
if(!widget->isExplicitlyEnabled() ||
((parent && parent->getFocusedChild() != widget))) {
((parent && parent->getFocusedChild() != widget))) {
newStatus = false;
break;
}
@@ -850,7 +850,7 @@ void UIWidget::updateState(Fw::WidgetState state)
do {
parent = widget->getParent();
if(!widget->isExplicitlyEnabled() || !widget->isExplicitlyVisible() || !widget->containsPoint(mousePos) ||
(parent && widget != parent->getChildByPos(mousePos))) {
(parent && widget != parent->getChildByPos(mousePos))) {
newStatus = false;
break;
}
@@ -1093,9 +1093,9 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty
if(!layoutType.empty()) {
UILayoutPtr layout;
if(layoutType == "verticalBox")
layout = UIVerticalLayout::create(asUIWidget());
layout = UIVerticalLayoutPtr(new UIVerticalLayout(asUIWidget()));
else if(layoutType == "anchor")
layout = UIAnchorLayout::create(asUIWidget());
layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
else
throw OTMLException(node, "cannot determine layout type");
setLayout(layout);
@@ -1158,6 +1158,7 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty
}
if(m_firstOnStyle) {
callLuaField("onSetup");
// always focus new child
if(isFocusable() && isExplicitlyVisible() && isExplicitlyEnabled())
focus();

View File

@@ -35,9 +35,6 @@ public:
UIWidget();
virtual ~UIWidget() { }
template<class T>
static std::shared_ptr<T> create() { auto t = std::shared_ptr<T>(new T); return t; }
void destroy();
protected:

View File

@@ -1,3 +1,25 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#include "otclient.h"
#include <framework/luascript/luainterface.h>
#include <otclient/luascript/luavaluecasts.h>
@@ -84,20 +106,20 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassStaticFunction<Game>("getFollowingCreature", std::bind(&Game::getFollowingCreature, &g_game));
g_lua.registerClass<UIItem, UIWidget>();
g_lua.bindClassStaticFunction<UIItem>("create", &UIItem::create<UIItem>);
g_lua.bindClassStaticFunction<UIItem>("create", []{ return UIItemPtr(new UIItem); } );
g_lua.bindClassMemberFunction<UIItem>("getItem", &UIItem::getItem);
g_lua.bindClassMemberFunction<UIItem>("setItem", &UIItem::setItem);
g_lua.registerClass<UICreature, UIWidget>();
g_lua.bindClassStaticFunction<UICreature>("create", &UICreature::create<UICreature>);
g_lua.bindClassStaticFunction<UICreature>("create", []{ return UICreaturePtr(new UICreature); } );
g_lua.bindClassMemberFunction<UICreature>("getCreature", &UICreature::getCreature);
g_lua.bindClassMemberFunction<UICreature>("setCreature", &UICreature::setCreature);
g_lua.registerClass<UIMap, UIWidget>();
g_lua.bindClassStaticFunction<UIMap>("create", &UIWidget::create<UIMap>);
g_lua.bindClassStaticFunction<UIMap>("create", []{ return UIMapPtr(new UIMap); } );
g_lua.registerClass<UIGame, UIWidget>();
g_lua.bindClassStaticFunction<UIGame>("create", &UIWidget::create<UIGame>);
g_lua.bindClassStaticFunction<UIGame>("create", []{ return UIGamePtr(new UIGame); } );
#ifdef FORBIDDEN_FUNCTIONS
g_lua.bindClassStaticFunction<Game>("talkChannel", std::bind(&Game::talkChannel, &g_game, _1, _2, _3));

View File

@@ -20,3 +20,20 @@
* THE SOFTWARE.
*/
#ifndef OTCLIENT_UI_DECLARATIONS_H
#define OTCLIENT_UI_DECLARATIONS_H
#include <otclient/global.h>
#include <framework/ui/declarations.h>
class UIItem;
class UICreature;
class UIMap;
class UIGame;
typedef std::shared_ptr<UIItem> UIItemPtr;
typedef std::shared_ptr<UICreature> UICreaturePtr;
typedef std::shared_ptr<UIMap> UIMapPtr;
typedef std::shared_ptr<UIGame> UIGamePtr;
#endif