diff options
author | Sebastian Geerken <devnull@localhost> | 2014-07-13 14:31:27 +0200 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2014-07-13 14:31:27 +0200 |
commit | 8ceaccff4ffcfffb81667c58784e49a9f314dc3b (patch) | |
tree | 6ca62c67b6c4059fffebf2ebbbe510433ccd00cb | |
parent | 55559a8ef3d847c97d94c215cb207807b5c665ff (diff) |
Tests.
-rw-r--r-- | .hgignore | 1 | ||||
-rw-r--r-- | test/Makefile.am | 12 | ||||
-rw-r--r-- | test/dw_simple_container.cc | 241 | ||||
-rw-r--r-- | test/dw_simple_container.hh | 56 | ||||
-rw-r--r-- | test/dw_simple_container_test.cc | 119 |
5 files changed, 429 insertions, 0 deletions
@@ -37,6 +37,7 @@ ^test/dw-links2$ ^test/dw-lists$ ^test/dw-resource-test$ +^test/dw-simple-container-test$ ^test/dw-table$ ^test/dw-table-aligned$ ^test/dw-ui-test$ diff --git a/test/Makefile.am b/test/Makefile.am index d9056dfe..59840d3c 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -17,6 +17,7 @@ noinst_PROGRAMS = \ dw-images-scaled \ dw-images-scaled2 \ dw-lists \ + dw-simple-container-test \ dw-table-aligned \ dw-table \ dw-border-test \ @@ -119,6 +120,17 @@ dw_lists_LDADD = \ $(top_builddir)/lout/liblout.a \ @LIBFLTK_LIBS@ +dw_simple_container_test_SOURCES = \ + dw_simple_container.hh \ + dw_simple_container.cc \ + dw_simple_container_test.cc +dw_simple_container_test_LDADD = \ + $(top_builddir)/dw/libDw-widgets.a \ + $(top_builddir)/dw/libDw-fltk.a \ + $(top_builddir)/dw/libDw-core.a \ + $(top_builddir)/lout/liblout.a \ + @LIBFLTK_LIBS@ + dw_table_aligned_SOURCES = dw_table_aligned.cc dw_table_aligned_LDADD = \ $(top_builddir)/dw/libDw-widgets.a \ diff --git a/test/dw_simple_container.cc b/test/dw_simple_container.cc new file mode 100644 index 00000000..c4a1aa2a --- /dev/null +++ b/test/dw_simple_container.cc @@ -0,0 +1,241 @@ +/* + * Dillo Widget + * + * Copyright 2014 Sebastian Geerken <sgeerken@dillo.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <math.h> + +#include "dw_simple_container.hh" + +using namespace dw::core; +using namespace dw::core::style; +using namespace lout::misc; + +namespace dw { + +int SimpleContainer::CLASS_ID = -1; + +// ---------------------------------------------------------------------- + +SimpleContainer::SimpleContainerIterator::SimpleContainerIterator + (SimpleContainer *simpleContainer, Content::Type mask, bool atEnd) : + Iterator (simpleContainer, mask, atEnd) +{ + content.type = atEnd ? Content::END : Content::START; +} + +lout::object::Object *SimpleContainer::SimpleContainerIterator::clone () +{ + SimpleContainerIterator *sci = + new SimpleContainerIterator ((SimpleContainer*)getWidget(), + getMask(), false); + sci->content = content; + return sci; +} + +int SimpleContainer::SimpleContainerIterator::index () +{ + switch (content.type) { + case Content::START: + return 0; + case Content::WIDGET_IN_FLOW: + return 1; + case Content::END: + return 2; + default: + assertNotReached (); + return 0; + } +} + +int SimpleContainer::SimpleContainerIterator::compareTo + (lout::object::Comparable *other) +{ + return index () - ((SimpleContainerIterator*)other)->index (); +} + +bool SimpleContainer::SimpleContainerIterator::next () +{ + SimpleContainer *simpleContainer = (SimpleContainer*)getWidget(); + + if (content.type == Content::END) + return false; + + // simple containers only contain widgets: + if ((getMask() & Content::WIDGET_IN_FLOW) == 0) { + content.type = Content::END; + return false; + } + + if (content.type == Content::START) { + if (simpleContainer->child != NULL) { + content.type = Content::WIDGET_IN_FLOW; + content.widget = simpleContainer->child; + return true; + } else { + content.type = Content::END; + return false; + } + } else /* if (content.type == Content::WIDGET) */ { + content.type = Content::END; + return false; + } +} + +bool SimpleContainer::SimpleContainerIterator::prev () +{ + SimpleContainer *simpleContainer = (SimpleContainer*)getWidget(); + + if (content.type == Content::START) + return false; + + // simple containers only contain widgets: + if ((getMask() & Content::WIDGET_IN_FLOW) == 0) { + content.type = Content::START; + return false; + } + + if (content.type == Content::END) { + if (simpleContainer->child != NULL) { + content.type = Content::WIDGET_IN_FLOW; + content.widget = simpleContainer->child; + return true; + } else { + content.type = Content::START; + return false; + } + } else /* if (content.type == Content::WIDGET) */ { + content.type = Content::START; + return false; + } +} + +void SimpleContainer::SimpleContainerIterator::highlight (int start, + int end, + HighlightLayer layer) +{ + /** todo Needs this an implementation? */ +} + +void SimpleContainer::SimpleContainerIterator::unhighlight (int direction, + HighlightLayer + layer) +{ + /** todo Needs this an implementation? */ +} + +void SimpleContainer::SimpleContainerIterator::getAllocation (int start, + int end, + Allocation + *allocation) +{ + /** \bug Not implemented. */ +} + +// ---------------------------------------------------------------------- + +SimpleContainer::SimpleContainer () +{ + registerName ("dw::SimpleContainer", &CLASS_ID); + child = NULL; +} + +SimpleContainer::~SimpleContainer () +{ + if (child) + delete child; +} + +void SimpleContainer::sizeRequestImpl (Requisition *requisition) +{ + Requisition childReq; + if (child) + child->sizeRequest (&childReq); + else + childReq.width = childReq.ascent = childReq.descent = 0; + + requisition->width = childReq.width + boxDiffWidth (); + requisition->ascent = childReq.ascent + boxOffsetY (); + requisition->descent = childReq.descent + boxRestHeight (); + + correctRequisition (requisition, splitHeightPreserveAscent); +} + + +void SimpleContainer::getExtremesImpl (Extremes *extremes) +{ + Extremes childExtr; + if (child) + child->getExtremes (&childExtr); + else + childExtr.minWidth = childExtr.maxWidth = 0; + + extremes->minWidth = childExtr.minWidth + boxDiffWidth (); + extremes->maxWidth = childExtr.maxWidth + boxDiffWidth (); + + correctExtremes (extremes); +} + + +void SimpleContainer::sizeAllocateImpl (Allocation *allocation) +{ + Allocation childAlloc; + + if (child) { + childAlloc.x = allocation->x + boxOffsetX (); + childAlloc.y = allocation->y + boxOffsetY (); + childAlloc.width = allocation->width - boxDiffWidth (); + childAlloc.ascent = allocation->ascent - boxOffsetY (); + childAlloc.descent = allocation->descent - boxRestHeight (); + child->sizeAllocate (&childAlloc); + } +} + +void SimpleContainer::draw (View *view, Rectangle *area) +{ + drawWidgetBox (view, area, false); + Rectangle childArea; + if (child && child->intersects (area, &childArea)) + child->draw (view, &childArea); +} + +Iterator *SimpleContainer::iterator (Content::Type mask, bool atEnd) +{ + return new SimpleContainerIterator (this, mask, atEnd); +} + +void SimpleContainer::removeChild (Widget *child) +{ + assert (child == this->child); + this->child = NULL; + + queueResize (0, true); +} + +void SimpleContainer::setChild (Widget *child) +{ + if (this->child) + delete this->child; + + this->child = child; + if (this->child) + this->child->setParent (this); + + queueResize (0, true); +} + +} // namespace dw diff --git a/test/dw_simple_container.hh b/test/dw_simple_container.hh new file mode 100644 index 00000000..c8a629db --- /dev/null +++ b/test/dw_simple_container.hh @@ -0,0 +1,56 @@ +#ifndef __DW_SIMPLE_CONTAINER_HH__ +#define __DWR_SIMPLE_CONTAINER_HH__ + +#include "dw/core.hh" + +namespace dw { + +/** + * Simple widget used for testing concepts. + */ +class SimpleContainer: public core::Widget +{ +private: + class SimpleContainerIterator: public core::Iterator + { + private: + int index (); + + public: + SimpleContainerIterator (SimpleContainer *simpleContainer, + core::Content::Type mask, + bool atEnd); + + lout::object::Object *clone (); + int compareTo (lout::object::Comparable *other); + + bool next (); + bool prev (); + void highlight (int start, int end, core::HighlightLayer layer); + void unhighlight (int direction, core::HighlightLayer layer); + void getAllocation (int start, int end, core::Allocation *allocation); + }; + + Widget *child; + +protected: + void sizeRequestImpl (core::Requisition *requisition); + void getExtremesImpl (core::Extremes *extremes); + void sizeAllocateImpl (core::Allocation *allocation); + +public: + static int CLASS_ID; + + SimpleContainer (); + ~SimpleContainer (); + + void draw (core::View *view, core::Rectangle *area); + core::Iterator *iterator (core::Content::Type mask, bool atEnd); + void removeChild (Widget *child); + + void setChild (core::Widget *child); +}; + +} // namespace dw + +#endif // __DW_SIMPLE_CONTAINER_HH__ diff --git a/test/dw_simple_container_test.cc b/test/dw_simple_container_test.cc new file mode 100644 index 00000000..371a52b1 --- /dev/null +++ b/test/dw_simple_container_test.cc @@ -0,0 +1,119 @@ +/* + * Dillo Widget + * + * Copyright 2014 Sebastian Geerken <sgeerken@dillo.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + + +#include <FL/Fl_Window.H> +#include <FL/Fl.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "dw_simple_container.hh" +#include "../dw/textblock.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +int main(int argc, char **argv) +{ + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(200, 300, "Dw Example"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 200, 300); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + styleAttrs.borderWidth.setVal (5); + styleAttrs.setBorderColor (Color::create (layout, 0x800080)); + styleAttrs.setBorderStyle (BORDER_DASHED); + styleAttrs.padding.setVal (5); + + FontAttrs fontAttrs; + fontAttrs.name = "Bitstream Charter"; + fontAttrs.size = 14; + fontAttrs.weight = 400; + fontAttrs.style = FONT_STYLE_NORMAL; + fontAttrs.letterSpacing = 0; + fontAttrs.fontVariant = FONT_VARIANT_NORMAL; + styleAttrs.font = style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *containerStyle = Style::create (&styleAttrs); + + SimpleContainer *simpleContainer = new SimpleContainer (); + simpleContainer->setStyle (containerStyle); + layout->setWidget (simpleContainer); + + styleAttrs.margin.setVal (0); + styleAttrs.borderWidth.setVal (0); + styleAttrs.padding.setVal (0); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + simpleContainer->setChild (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + + Style *wordStyle = Style::create (&styleAttrs); + + for(int i = 1; i <= 10; i++) { + char buf[4]; + sprintf(buf, "%d.", i); + + const char *words[] = { "This", "is", "the", buf, "paragraph.", + "Here", "comes", "some", "more", "text", + "to", "demonstrate", "word", "wrapping.", + NULL }; + + for(int j = 0; words[j]; j++) { + textblock->addText(words[j], wordStyle); + textblock->addSpace(wordStyle); + } + + textblock->addParbreak(10, wordStyle); + } + + wordStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} |