added padding support to layouts

This commit is contained in:
Peter Schojer 2008-07-01 08:08:27 +00:00
parent 04f61f101b
commit 474f84b6cf
15 changed files with 254 additions and 24 deletions

View File

@ -66,14 +66,15 @@ public:
/// Emits code for the page body to the given output stream.
protected:
void renderLayoutHead(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig);
void renderLayoutHead(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig, int cols, const std::string& htmlPadding);
/// layoutId contains the id for the layout parameter (e.g. table for GridLayoutRenderer)
/// layoutConfig contains the string for the layoutConfig parameter (e.g. {columns:3})
/// htmlPadding contains the optional padding string to use, e.g.:'<p class=\"lbl\" style=\"padding-left:10px\">&nbsp;</p>'
void renderParameters(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig);
void renderParameters(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig, int cols, const std::string& htmlPadding);
/// Render the config parameters
void visitChildren(const Layout* pLayout, const RenderContext& context, std::ostream& ostr);
void visitChildren(const Layout* pLayout, int cols, const std::string& htmlPadding, const RenderContext& context, std::ostream& ostr);
/// Visits children
};

View File

@ -60,10 +60,29 @@ void GridLayoutRenderer::renderHead(const Renderable* pRenderable, const RenderC
poco_assert_dbg (pRenderable != 0);
poco_assert_dbg (pRenderable->type() == typeid(Poco::WebWidgets::GridLayout));
const GridLayout* pLayout = static_cast<const Poco::WebWidgets::GridLayout*>(pRenderable);
std::size_t cols = pLayout->columns();
int padHorVal = pLayout->getHorizontalPadding();
int padVertVal = pLayout->getVerticalPadding();
std::string padding;
if (padHorVal > 0 || padVertVal > 0)
{
std::ostringstream pad;
// or style=\"background:#deecfd;padding-left:10px\"
// or transparent gif: <img src=\"resources/images/default/s.gif\" width=\"10\" height=\"1\" alt=\"\">'
pad << "<p style=\"";
if (padHorVal > 0)
pad << "padding-left:" << padHorVal << "px; ";
if (padVertVal > 0)
pad << "padding-top:" << padVertVal << "px ";
pad << "\">&nbsp;</p>";
padding = pad.str();
cols = 2 * cols - 1;
}
std::ostringstream layoutConfig;
layoutConfig << "{columns:" << pLayout->columns() << "}";
layoutConfig << "{columns:" << cols << "}";
std::string layout("table");
LayoutRenderer::renderLayoutHead(pLayout, context, ostr, layout, layoutConfig.str());
// when padding is used we can no longer use LayoutRenderer
LayoutRenderer::renderLayoutHead(pLayout, context, ostr, layout, layoutConfig.str(), (int)cols, padding);
}

View File

@ -61,11 +61,23 @@ void HorizontalLayoutRenderer::renderHead(const Renderable* pRenderable, const R
poco_assert_dbg (pRenderable->type() == typeid(Poco::WebWidgets::HorizontalLayout));
const HorizontalLayout* pLayout = static_cast<const Poco::WebWidgets::HorizontalLayout*>(pRenderable);
std::ostringstream layoutConfig;
layoutConfig << "{columns:" << pLayout->size() << "}";
std::size_t cols = pLayout->size();
int padVal = pLayout->getPadding();
std::string padding;
if (padVal > 0)
{
std::ostringstream pad;
// or style=\"background:#deecfd;padding-left:10px\"
// or transparent gif: <img src=\"resources/images/default/s.gif\" width=\"10\" height=\"1\" alt=\"\">'
pad << "<p style=\"padding-left:" << padVal << "px\">&nbsp;</p>";
padding = pad.str();
cols = 2 * cols - 1;
}
layoutConfig << "{columns:" << cols << "}";
static std::string layout("table");
//static std::string layout("column"); -> this works with firefox but faila with IE7!
LayoutRenderer::renderLayoutHead(pLayout, context, ostr, layout, layoutConfig.str());
//static std::string layout("column"); -> this works with firefox but fails with IE7!
LayoutRenderer::renderLayoutHead(pLayout, context, ostr, layout, layoutConfig.str(), (int)cols, padding);
}

View File

@ -38,6 +38,7 @@
#include "Poco/WebWidgets/ExtJS/FormRenderer.h"
#include "Poco/WebWidgets/ExtJS/Utility.h"
#include "Poco/WebWidgets/Layout.h"
#include "Poco/WebWidgets/VerticalLayout.h"
#include "Poco/WebWidgets/Frame.h"
#include "Poco/WebWidgets/Panel.h"
@ -57,7 +58,7 @@ LayoutRenderer::~LayoutRenderer()
}
void LayoutRenderer::renderLayoutHead(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig)
void LayoutRenderer::renderLayoutHead(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig, int cols, const std::string& htmlPadding)
{
poco_assert_dbg(pLayout != 0);
Renderable::ID id(0);
@ -67,12 +68,12 @@ void LayoutRenderer::renderLayoutHead(const Layout* pLayout, const RenderContext
// the parent is not a panel
// assume that the direct parent is a panel
ostr << "new Ext.Panel({border:false,bodyBorder:false,";
renderParameters(pLayout, context, ostr, layoutId, layoutConfig);
renderParameters(pLayout, context, ostr, layoutId, layoutConfig, cols, htmlPadding);
ostr << "})";
}
else
{
renderParameters(pLayout, context, ostr, layoutId, layoutConfig);
renderParameters(pLayout, context, ostr, layoutId, layoutConfig, cols, htmlPadding);
}
}
@ -83,7 +84,7 @@ void LayoutRenderer::renderBody(const Renderable* pRenderable, const RenderConte
}
void LayoutRenderer::renderParameters(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig)
void LayoutRenderer::renderParameters(const Layout* pLayout, const RenderContext& context, std::ostream& ostr, const std::string& layoutId, const std::string& layoutConfig, int cols, const std::string& htmlPadding)
{
poco_assert_dbg(pLayout != 0);
bool writeComma = false;
@ -106,24 +107,36 @@ void LayoutRenderer::renderParameters(const Layout* pLayout, const RenderContext
ostr << ",items:[";
else
ostr << "items:[";
visitChildren(pLayout, context, ostr);
visitChildren(pLayout, cols, htmlPadding, context, ostr);
ostr << "]";
}
void LayoutRenderer::visitChildren(const Layout* pLayout, const RenderContext& context, std::ostream& ostr)
void LayoutRenderer::visitChildren(const Layout* pLayout, int cols, const std::string& htmlPadding, const RenderContext& context, std::ostream& ostr)
{
ContainerView::ConstIterator it = pLayout->begin();
for (; it != pLayout->end(); ++it)
int cnt(0);
const VerticalLayout* pVert = dynamic_cast<const VerticalLayout*>(pLayout);
for (; it != pLayout->end(); ++it, ++cnt)
{
if (it != pLayout->begin())
{
ostr << ",";
if (!htmlPadding.empty() && (cnt % cols != 0 || pVert))
{
// if padding, insert an additional empty html element that defines padding
ostr << "new Ext.Panel({border:false,bodyBorder:false,";
ostr << "html:'" << htmlPadding << "'}),";
++cnt;
}
}
if (*it)
{
//horizontallayout works only when children are panels
ostr << "{xtype:'panel',items:";
ostr << "new Ext.Panel({border:false,bodyBorder:false,items:";
(*it)->renderHead(context, ostr);
ostr << "}";
ostr << "})";
}
else
ostr << "{}";

View File

@ -189,6 +189,7 @@ void PageRenderer::renderHead(const Renderable* pRenderable, const RenderContext
ostr << STRC_HEAD;
WebApplication::instance().registerAjaxProcessor(Poco::NumberFormatter::format(pPage->id()), pPage);
pPage->afterPageRequested.notify(this, pPage);
}

View File

@ -60,10 +60,21 @@ void VerticalLayoutRenderer::renderHead(const Renderable* pRenderable, const Ren
poco_assert_dbg (pRenderable != 0);
poco_assert_dbg (pRenderable->type() == typeid(Poco::WebWidgets::VerticalLayout));
const VerticalLayout* pLayout = static_cast<const Poco::WebWidgets::VerticalLayout*>(pRenderable);
int padVal = pLayout->getPadding();
std::string padding;
if (padVal > 0)
{
std::ostringstream pad;
// or style=\"background:#deecfd;padding-left:10px\"
// or transparent gif: <img src=\"resources/images/default/s.gif\" width=\"10\" height=\"1\" alt=\"\">'
pad << "<p style=\"padding-top:" << padVal << "px\">&nbsp;</p>";
padding = pad.str();
}
// a vertical layout is a table with one column
std::string layoutConfig("{columns:1}");
std::string layout("table");
LayoutRenderer::renderLayoutHead(pLayout, context, ostr, layout, layoutConfig);
LayoutRenderer::renderLayoutHead(pLayout, context, ostr, layout, layoutConfig, 1, padding);
}

View File

@ -1462,6 +1462,86 @@ void ExtJSTest::testPanelShowHide()
}
void ExtJSTest::testHorizontalLayout()
{
ResourceManager::Ptr pRM(new ResourceManager());Utility::initialize(pRM, Poco::Path());WebApplication webApp(Poco::URI("/"), pRM);
LookAndFeel::Ptr laf(new LookAndFeel());
webApp.setLookAndFeel(laf);
RenderContext context(*laf, webApp);
Utility::initialize(laf);
Page::Ptr ptr = new Page("test");
webApp.setCurrentPage(ptr);
HorizontalLayout::Ptr pHor(new HorizontalLayout(10));
pHor->add(new Button("b1", "But1"));
pHor->add(new Button("b2", "But2"));
pHor->add(new Button("b3", "But3"));
pHor->add(new Button("b4", "But4"));
ptr->add(pHor);
std::ostringstream ostr;
std::ofstream fstr("testHorizontalLayout.html");
TeeOutputStream out(ostr);
out.addStream(fstr);
ptr->renderHead(context, out);
ptr->renderBody(context, out);
std::string result = ostr.str();
}
void ExtJSTest::testVerticalLayout()
{
ResourceManager::Ptr pRM(new ResourceManager());Utility::initialize(pRM, Poco::Path());WebApplication webApp(Poco::URI("/"), pRM);
LookAndFeel::Ptr laf(new LookAndFeel());
webApp.setLookAndFeel(laf);
RenderContext context(*laf, webApp);
Utility::initialize(laf);
Page::Ptr ptr = new Page("test");
webApp.setCurrentPage(ptr);
VerticalLayout::Ptr pHor(new VerticalLayout(10));
pHor->add(new Button("b1", "But1"));
pHor->add(new Button("b2", "But2"));
pHor->add(new Button("b3", "But3"));
pHor->add(new Button("b4", "But4"));
ptr->add(pHor);
std::ostringstream ostr;
std::ofstream fstr("testVerticalLayout.html");
TeeOutputStream out(ostr);
out.addStream(fstr);
ptr->renderHead(context, out);
ptr->renderBody(context, out);
std::string result = ostr.str();
}
void ExtJSTest::testGridLayout()
{
ResourceManager::Ptr pRM(new ResourceManager());Utility::initialize(pRM, Poco::Path());WebApplication webApp(Poco::URI("/"), pRM);
LookAndFeel::Ptr laf(new LookAndFeel());
webApp.setLookAndFeel(laf);
RenderContext context(*laf, webApp);
Utility::initialize(laf);
Page::Ptr ptr = new Page("test");
webApp.setCurrentPage(ptr);
GridLayout::Ptr pHor(new GridLayout(2));
pHor->setHorizontalPadding(30);
pHor->setVerticalPadding(15);
pHor->add(new Button("b1", "But1"));
pHor->add(new Button("b2", "But2"));
pHor->add(new Button("b3", "But3"));
pHor->add(new Button("b4", "But4"));
ptr->add(pHor);
std::ostringstream ostr;
std::ofstream fstr("testGridLayout.html");
TeeOutputStream out(ostr);
out.addStream(fstr);
ptr->renderHead(context, out);
ptr->renderBody(context, out);
std::string result = ostr.str();
}
void ExtJSTest::setUp()
{
}
@ -1516,6 +1596,9 @@ CppUnit::Test* ExtJSTest::suite()
CppUnit_addTest(pSuite, ExtJSTest, testJSEvent2);
CppUnit_addTest(pSuite, ExtJSTest, testButtonRename);
CppUnit_addTest(pSuite, ExtJSTest, testPanelShowHide);
CppUnit_addTest(pSuite, ExtJSTest, testHorizontalLayout);
CppUnit_addTest(pSuite, ExtJSTest, testGridLayout);
CppUnit_addTest(pSuite, ExtJSTest, testVerticalLayout);
return pSuite;
}

View File

@ -85,6 +85,9 @@ public:
void testJSEvent2();
void testButtonRename();
void testPanelShowHide();
void testHorizontalLayout();
void testVerticalLayout();
void testGridLayout();
void setUp();
void tearDown();

View File

@ -77,6 +77,18 @@ public:
void minimize();
/// Minimizes the rowCount (i.e. empty lines at the end of the grid are truncated
void setHorizontalPadding(int hor);
/// Sets horizontal padding. A value <= 0 means no padding which is also the default.
void setVerticalPadding(int vert);
/// Sets vertical padding. A value <= 0 means no padding which is also the default.
int getHorizontalPadding() const;
/// Gets horizontal padding. A value <= 0 means no padding which is also the default.
int getVerticalPadding() const;
/// Gets vertical padding. A value <= 0 means no padding which is also the default.
protected:
~GridLayout();
@ -91,6 +103,8 @@ protected:
private:
const std::size_t _colCnt;
const std::size_t _rowCnt;
int _horPadding;
int _vertPadding;
std::size_t _lastIdx;
};
@ -130,6 +144,30 @@ inline void GridLayout::add(View::Ptr pView)
}
inline void GridLayout::setHorizontalPadding(int hor)
{
_horPadding = hor;
}
inline void GridLayout::setVerticalPadding(int vert)
{
_vertPadding = vert;
}
inline int GridLayout::getHorizontalPadding() const
{
return _horPadding;
}
inline int GridLayout::getVerticalPadding() const
{
return _vertPadding;
}
} } // namespace Poco::WebWidgets

View File

@ -53,15 +53,36 @@ class WebWidgets_API HorizontalLayout: public Layout
public:
typedef Poco::AutoPtr<HorizontalLayout> Ptr;
HorizontalLayout();
HorizontalLayout(int pad=0);
/// Creates the HorizontalLayout.
int getPadding() const;
/// Returns the padding value, a value <= 0 means no padding which is the default.
void setPadding(int pad);
/// Sets the padding value, a value <= 0 means no padding which is the default.
protected:
~HorizontalLayout();
/// Destroys the HorizontalLayout.
private:
int _padding;
};
inline int HorizontalLayout::getPadding() const
{
return _padding;
}
inline void HorizontalLayout::setPadding(int pad)
{
_padding = pad;
}
} } // namespace Poco::WebWidgets

View File

@ -71,6 +71,9 @@ public:
Poco::BasicEvent<Page*> pageRequested;
/// Thrown whenever the page is requested, before code is generated
Poco::BasicEvent<Page*> afterPageRequested;
/// Thrown whenever the page is requested, after code is generated
Page();
/// Creates an anonymous Page.

View File

@ -53,15 +53,36 @@ class WebWidgets_API VerticalLayout: public Layout
public:
typedef Poco::AutoPtr<VerticalLayout> Ptr;
VerticalLayout();
VerticalLayout(int pad = 0);
/// Creates the VerticalLayout.
int getPadding() const;
/// Returns the padding value, a value <= 0 means no padding which is the default.
void setPadding(int pad);
/// Sets the padding value, a value <= 0 means no padding which is the default.
protected:
~VerticalLayout();
/// Destroys the VerticalLayout.
private:
int _padding;
};
inline int VerticalLayout::getPadding() const
{
return _padding;
}
inline void VerticalLayout::setPadding(int pad)
{
_padding = pad;
}
} } // namespace Poco::WebWidgets

View File

@ -45,6 +45,8 @@ GridLayout::GridLayout(std::size_t colCnt, std::size_t rowCnt):
Layout(typeid(GridLayout)),
_colCnt(colCnt),
_rowCnt(rowCnt),
_horPadding(0),
_vertPadding(0),
_lastIdx(0)
{
poco_assert (_colCnt > 0);

View File

@ -41,8 +41,9 @@ namespace Poco {
namespace WebWidgets {
HorizontalLayout::HorizontalLayout():
Layout(typeid(HorizontalLayout))
HorizontalLayout::HorizontalLayout(int pad):
Layout(typeid(HorizontalLayout)),
_padding(pad)
{
}

View File

@ -41,8 +41,9 @@ namespace Poco {
namespace WebWidgets {
VerticalLayout::VerticalLayout():
Layout(typeid(VerticalLayout))
VerticalLayout::VerticalLayout(int pad):
Layout(typeid(VerticalLayout)),
_padding(pad)
{
}