diff options
author | Rodrigo Arias Mallo <rodarima@gmail.com> | 2023-12-12 21:27:08 +0100 |
---|---|---|
committer | Rodrigo Arias Mallo <rodrigo.arias@bsc.es> | 2023-12-21 01:05:58 +0100 |
commit | 1da1260af72b20126176e2b8f73f7b7fd5952ce1 (patch) | |
tree | 0fcdb276d30814ce4075f7cc205e357b2b7c1be5 /test/dw | |
parent | 78ad5bfe9644d1217f9d9ad0bf2fcdc388551113 (diff) |
Split tests into unit and dw (graphical)
Graphical tests for the dw (Dillo Widget) are moved to test/dw, while
unit tests are placed into test/unit.
All tests are compiled with "make check" but only the tests that can run
without intervention and without a graphic display are executed.
Diffstat (limited to 'test/dw')
-rw-r--r-- | test/dw/Makefile.am | 63 | ||||
-rw-r--r-- | test/dw/dw_anchors_test.cc | 164 | ||||
-rw-r--r-- | test/dw/dw_border_test.cc | 127 | ||||
-rw-r--r-- | test/dw/dw_example.cc | 105 | ||||
-rw-r--r-- | test/dw/dw_find_test.cc | 157 | ||||
-rw-r--r-- | test/dw/dw_float_test.cc | 145 | ||||
-rw-r--r-- | test/dw/dw_image_background.cc | 196 | ||||
-rw-r--r-- | test/dw/dw_images_scaled.cc | 155 | ||||
-rw-r--r-- | test/dw/dw_images_scaled2.cc | 150 | ||||
-rw-r--r-- | test/dw/dw_images_simple.cc | 154 | ||||
-rw-r--r-- | test/dw/dw_imgbuf_mem_test.cc | 108 | ||||
-rw-r--r-- | test/dw/dw_links.cc | 163 | ||||
-rw-r--r-- | test/dw/dw_links2.cc | 192 | ||||
-rw-r--r-- | test/dw/dw_lists.cc | 136 | ||||
-rw-r--r-- | test/dw/dw_resource_test.cc | 102 | ||||
-rw-r--r-- | test/dw/dw_simple_container.cc | 245 | ||||
-rw-r--r-- | test/dw/dw_simple_container.hh | 57 | ||||
-rw-r--r-- | test/dw/dw_simple_container_test.cc | 114 | ||||
-rw-r--r-- | test/dw/dw_table.cc | 116 | ||||
-rw-r--r-- | test/dw/dw_table_aligned.cc | 121 | ||||
-rw-r--r-- | test/dw/dw_ui_test.cc | 245 | ||||
-rw-r--r-- | test/dw/form.cc | 263 | ||||
-rw-r--r-- | test/dw/form.hh | 164 |
23 files changed, 3442 insertions, 0 deletions
diff --git a/test/dw/Makefile.am b/test/dw/Makefile.am new file mode 100644 index 00000000..39bb8440 --- /dev/null +++ b/test/dw/Makefile.am @@ -0,0 +1,63 @@ +AM_CPPFLAGS = \ + -I$(top_srcdir) \ + -DCUR_WORKING_DIR='"@BASE_CUR_WORKING_DIR@/test"' + +AM_CFLAGS = @LIBFLTK_CFLAGS@ +AM_CXXFLAGS = @LIBFLTK_CXXFLAGS@ + +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@ @LIBX11_LIBS@ + +check_PROGRAMS = \ + dw-anchors-test \ + dw-border-test \ + dw-example \ + dw-find-test \ + dw-float-test \ + dw-image-background \ + dw-images-scaled \ + dw-images-scaled2 \ + dw-images-simple \ + dw-imgbuf-mem-test \ + dw-links \ + dw-links2 \ + dw-lists \ + dw-resource-test \ + dw-simple-container-test \ + dw-table \ + dw-table-aligned \ + dw-ui-test + +# Don't run most tests yet, as they require graphical display and human +# intervention. +TESTS = \ + dw-imgbuf-mem-test + +dw_anchors_test_SOURCES = dw_anchors_test.cc +dw_border_test_SOURCES = dw_border_test.cc +dw_example_SOURCES = dw_example.cc +dw_find_test_SOURCES = dw_find_test.cc +dw_float_test_SOURCES = dw_float_test.cc +dw_links_SOURCES = dw_links.cc +dw_links2_SOURCES = dw_links2.cc +dw_image_background_SOURCES = dw_image_background.cc +dw_images_simple_SOURCES = dw_images_simple.cc +dw_images_scaled_SOURCES = dw_images_scaled.cc +dw_images_scaled2_SOURCES = dw_images_scaled2.cc +dw_lists_SOURCES = dw_lists.cc +dw_simple_container_test_SOURCES = \ + dw_simple_container.hh \ + dw_simple_container.cc \ + dw_simple_container_test.cc +dw_table_aligned_SOURCES = dw_table_aligned.cc +dw_table_SOURCES = dw_table.cc +dw_imgbuf_mem_test_SOURCES = dw_imgbuf_mem_test.cc +dw_resource_test_SOURCES = dw_resource_test.cc +dw_ui_test_SOURCES = \ + dw_ui_test.cc \ + form.cc \ + form.hh diff --git a/test/dw/dw_anchors_test.cc b/test/dw/dw_anchors_test.cc new file mode 100644 index 00000000..d7201053 --- /dev/null +++ b/test/dw/dw_anchors_test.cc @@ -0,0 +1,164 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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 <ctype.h> +#include <FL/Fl_Window.H> +#include <FL/Fl.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" + +using namespace lout::container::typed; +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +static FltkPlatform *platform; +static Layout *layout; +static Fl_Window *window; +static FltkViewport *viewport; +static Style *topWidgetStyle, *widgetStyle, *wordStyle, *headingStyle; +static Textblock *topTextblock = NULL; +static int textblockNo = 0; + +static const char *numbers[10] = { + "one", "two", "three", "four", "five", + "six", "seven", "eight", "nine", "ten" +}; + +static void anchorCallback (Fl_Widget *widget, void *data) +{ + layout->setAnchor (numbers[(long)data]); +} + +static void textTimeout (void *data) +{ + Textblock *oldTop = topTextblock; + topTextblock = new Textblock (false); + + if (oldTop) { + oldTop->addLinebreak (wordStyle); + oldTop->addWidget (topTextblock, widgetStyle); + } else { + topTextblock->setStyle (topWidgetStyle); + layout->setWidget (topTextblock); + } + + topTextblock->addAnchor (numbers[textblockNo], headingStyle); + + char buf[16]; + strcpy (buf, numbers[textblockNo]); + buf[0] = lout::misc::AsciiToupper (buf[0]); + topTextblock->addText (buf, headingStyle); + topTextblock->addParbreak (5, headingStyle); + + for (int i = 0; i < 30; i++) { + strcpy (buf, numbers[textblockNo]); + if (i == 0) + buf[0] = lout::misc::AsciiToupper (buf[0]); + strcat (buf, i == 29 ? "." : ","); + + topTextblock->addText (buf, wordStyle); + topTextblock->addSpace (wordStyle); + } + + topTextblock->flush (); + + textblockNo++; + if (textblockNo < 10) + Fl::repeat_timeout (1, textTimeout, NULL); + +} + +int main(int argc, char **argv) +{ + char *buttonLabel[10]; + + platform = new FltkPlatform (); + layout = new Layout (platform); + + window = new Fl_Window(250, 200, "Dw Anchors Test"); + window->box(FL_NO_BOX); + window->begin(); + + viewport = new FltkViewport (50, 0, 200, 200); + viewport->end(); + layout->attachView (viewport); + + for (int i = 0; i < 10; i++) { + char buf[16]; + strcpy (buf, numbers[i]); + buf[0] = lout::misc::AsciiToupper (buf[0]); + buttonLabel[i] = strdup(buf); + Fl_Button *button = new Fl_Button(0, 20 * i, 50, 20, buttonLabel[i]); + button->callback (anchorCallback, (void*)(long)i); + button->when (FL_WHEN_RELEASE); + } + + 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 styleAttrs; + styleAttrs.initValues (); + styleAttrs.font = dw::core::style::Font::create (layout, &fontAttrs); + styleAttrs.margin.setVal (5); + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + topWidgetStyle = Style::create (&styleAttrs); + + styleAttrs.margin.left = 20; + styleAttrs.margin.right = 0; + styleAttrs.backgroundColor = NULL; + widgetStyle = Style::create (&styleAttrs); + + styleAttrs.margin.left = 0; + wordStyle = Style::create (&styleAttrs); + + fontAttrs.size = 28; + fontAttrs.weight = 700; + styleAttrs.font = dw::core::style::Font::create (layout, &fontAttrs); + headingStyle = Style::create (&styleAttrs); + + Fl::add_timeout (0, textTimeout, NULL); + + window->resizable(viewport); + window->show(); + + int errorCode = Fl::run(); + + topWidgetStyle->unref (); + widgetStyle->unref (); + wordStyle->unref (); + headingStyle->unref (); + for (int i = 0; i < 10; i++) + free(buttonLabel[i]); + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_border_test.cc b/test/dw/dw_border_test.cc new file mode 100644 index 00000000..ec5a09a5 --- /dev/null +++ b/test/dw/dw_border_test.cc @@ -0,0 +1,127 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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/textblock.hh" +#include "../dw/listitem.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 Border Test"); + 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 (2); + styleAttrs.setBorderColor (Color::create (layout, 0xffffff)); + styleAttrs.setBorderStyle (BORDER_INSET); + 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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle1 = Style::create (&styleAttrs); + + styleAttrs.backgroundColor = Color::create (layout, 0xffff80); + styleAttrs.margin.setVal (0); + styleAttrs.borderWidth.setVal (1); + styleAttrs.setBorderColor (Color::create (layout, 0x4040ff)); + styleAttrs.setBorderStyle (BORDER_SOLID); + styleAttrs.padding.setVal (1); + + Style *widgetStyle2 = Style::create (&styleAttrs); + + Textblock *textblock1 = new Textblock (false); + textblock1->setStyle (widgetStyle1); + layout->setWidget (textblock1); + + widgetStyle1->unref(); + + styleAttrs.borderWidth.setVal (0); + styleAttrs.padding.setVal (0); + styleAttrs.backgroundColor = NULL; + styleAttrs.cursor = CURSOR_TEXT; + + Style *wordStyle = Style::create (&styleAttrs); + + const char *words1[] = { "Some", "random", "text.", NULL }; + const char *words2[] = { "A", "nested", "paragraph.", NULL }; + + for(int i = 0; words1[i]; i++) { + if(i != 0) + textblock1->addSpace (wordStyle); + textblock1->addText (words1[i], wordStyle); + } + + for(int i = 0; i < 1; i++) { + textblock1->addParbreak(0, wordStyle); + + Textblock *textblock2 = new Textblock (false); + textblock1->addWidget (textblock2, widgetStyle2); + + for(int j = 0; words2[j]; j++) { + if(j != 0) + textblock2->addSpace (wordStyle); + textblock2->addText (words2[j], wordStyle); + } + + textblock2->flush (); + } + + textblock1->flush (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + widgetStyle2->unref(); + wordStyle->unref(); + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_example.cc b/test/dw/dw_example.cc new file mode 100644 index 00000000..259650cf --- /dev/null +++ b/test/dw/dw_example.cc @@ -0,0 +1,105 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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/textblock.hh" + + +int main(int argc, char **argv) +{ + dw::fltk::FltkPlatform *platform = new dw::fltk::FltkPlatform (); + dw::core::Layout *layout = new dw::core::Layout (platform); + + Fl_Window *window = new Fl_Window(200, 300, "Dw Example"); + window->box(FL_NO_BOX); + window->begin(); + + dw::fltk::FltkViewport *viewport = + new dw::fltk::FltkViewport (0, 0, 200, 300); + layout->attachView (viewport); + + dw::core::style::StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + + dw::core::style::FontAttrs fontAttrs; + fontAttrs.name = "Bitstream Charter"; + fontAttrs.size = 14; + fontAttrs.weight = 400; + fontAttrs.style = dw::core::style::FONT_STYLE_NORMAL; + fontAttrs.letterSpacing = 0; + fontAttrs.fontVariant = dw::core::style::FONT_VARIANT_NORMAL; + styleAttrs.font = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = + dw::core::style::Color::create (layout, 0x000000); + styleAttrs.backgroundColor = + dw::core::style::Color::create (layout, 0xffffff); + + dw::core::style::Style *widgetStyle = + dw::core::style::Style::create (&styleAttrs); + + dw::Textblock *textblock = new dw::Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + + dw::core::style::Style *wordStyle = + dw::core::style::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; +} diff --git a/test/dw/dw_find_test.cc b/test/dw/dw_find_test.cc new file mode 100644 index 00000000..5bddda10 --- /dev/null +++ b/test/dw/dw_find_test.cc @@ -0,0 +1,157 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_Box.H> +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" + +using namespace lout::container::typed; +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +static FltkPlatform *platform; +static Layout *layout; +static Fl_Window *window; +static FltkViewport *viewport; +static Fl_Button *findButton, *resetButton; +static Fl_Widget *resultLabel; + +static void findCallback (Fl_Widget *widget, void *data) +{ + //switch(layout->search ("worm", true)) { + switch(layout->search ("WORM", false, false)) { + case FindtextState::SUCCESS: + resultLabel->label("SUCCESS"); + break; + + case FindtextState::RESTART: + resultLabel->label("RESTART"); + break; + + case FindtextState::NOT_FOUND: + resultLabel->label("NOT_FOUND"); + break; + } + + resultLabel->redraw (); +} + +static void resetCallback (Fl_Widget *widget, void *data) +{ + layout->resetSearch (); + resultLabel->label("---"); + resultLabel->redraw (); +} + +int main(int argc, char **argv) +{ + platform = new FltkPlatform (); + layout = new Layout (platform); + + window = new Fl_Window(200, 300, "Dw Find Test"); + window->box(FL_NO_BOX); + window->begin(); + + viewport = new FltkViewport (0, 0, 200, 280); + viewport->end(); + layout->attachView (viewport); + + findButton = new Fl_Button(0, 280, 50, 20, "Find"); + findButton->callback (findCallback, NULL); + findButton->when (FL_WHEN_RELEASE); + findButton->clear_visible_focus (); + + resetButton = new Fl_Button(50, 280, 50, 20, "Reset"); + resetButton->callback (resetCallback, NULL); + resetButton->when (FL_WHEN_RELEASE); + resetButton->clear_visible_focus (); + + resultLabel = new Fl_Box(100, 280, 100, 20, "---"); + resultLabel->box(FL_FLAT_BOX); + + 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 styleAttrs; + styleAttrs.initValues (); + styleAttrs.font = dw::core::style::Font::create (layout, &fontAttrs); + styleAttrs.margin.setVal (10); + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + Style *topWidgetStyle = Style::create (&styleAttrs); + + styleAttrs.margin.setVal (0); + styleAttrs.margin.left = 30; + styleAttrs.backgroundColor = NULL; + Style *widgetStyle = Style::create (&styleAttrs); + + styleAttrs.margin.left = 0; + Style *wordStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (topWidgetStyle); + layout->setWidget (textblock); + + Stack <Textblock> *stack = new Stack <Textblock> (false); + stack->push (textblock); + + for(int i = 0; i < 10; i++) + for(int j = 0; j < 10; j++) { + Textblock *current; + if(j < 5) { + current = new Textblock (false); + stack->getTop()->addWidget (current, widgetStyle); + stack->push (current); + } else { + stack->getTop()->flush (); + stack->pop (); + current = stack->getTop (); + } + + current->addText ((i == j ? "worm" : "apple"), wordStyle); + current->addLinebreak (wordStyle); + } + + stack->getTop()->flush (); + + topWidgetStyle->unref (); + widgetStyle->unref (); + wordStyle->unref (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_float_test.cc b/test/dw/dw_float_test.cc new file mode 100644 index 00000000..807002b2 --- /dev/null +++ b/test/dw/dw_float_test.cc @@ -0,0 +1,145 @@ +#include <FL/Fl.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +static Textblock *firstFloat; +static Style *wordStyle; + +static void addTextToFloatTimeout (void *data) +{ + printf("addTextToFloatTimeout\n"); + + const char *fWords[] = { "This", "is", "a", "float,", "which", "is", + "set", "aside", "from", "the", "main", + "text.", NULL }; + + for(int k = 0; fWords[k]; k++) { + firstFloat->addText(fWords[k], wordStyle); + firstFloat->addSpace(wordStyle); + } + + firstFloat->flush(); + + Fl::repeat_timeout (2, addTextToFloatTimeout, NULL); +} + +int main(int argc, char **argv) +{ + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(400, 600, "Dw Floats Example"); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 400, 600); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + + FontAttrs fontAttrs; + fontAttrs.name = "Bitstream Charter"; + fontAttrs.size = 14; + fontAttrs.weight = 400; + fontAttrs.style = FONT_STYLE_NORMAL; + fontAttrs.letterSpacing = 0; + styleAttrs.font = core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + styleAttrs.borderWidth.setVal (1); + styleAttrs.setBorderColor (Color::create (layout, 0x808080)); + styleAttrs.setBorderStyle (BORDER_DASHED); + styleAttrs.width = createAbsLength(100); + styleAttrs.vloat = FLOAT_LEFT; + Style *leftFloatStyle = Style::create (&styleAttrs); + + styleAttrs.width = createAbsLength(80); + styleAttrs.vloat = FLOAT_RIGHT; + Style *rightFloatStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.borderWidth.setVal (0); + styleAttrs.width = LENGTH_AUTO; + styleAttrs.vloat = FLOAT_NONE; + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + + wordStyle = Style::create (&styleAttrs); + + for(int i = 1; i <= 10; i++) { + char buf[16]; + snprintf(buf, sizeof(buf), "%d%s", + i, (i == 1 ? "st" : (i == 2 ? "nd" : (i == 3 ? "rd" : "th")))); + + 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); + + if ((i == 3 || i == 5) && j == 8) { + textblock->addText("[float]", wordStyle); + textblock->addSpace(wordStyle); + + Textblock *vloat = new Textblock (false); + textblock->addWidget(vloat, i == 3 ? leftFloatStyle : rightFloatStyle); + + const char *fWords[] = { "This", "is", "a", "float,", "which", "is", + "set", "aside", "from", "the", "main", + "text.", NULL }; + + vloat->addText(i == 3 ? "Left:" : "Right:", wordStyle); + vloat->addSpace(wordStyle); + + for(int k = 0; fWords[k]; k++) { + vloat->addText(fWords[k], wordStyle); + vloat->addSpace(wordStyle); + } + + vloat->flush (); + + if(i == 3) + firstFloat = vloat; + } + } + + textblock->addParbreak(10, wordStyle); + } + + leftFloatStyle->unref(); + rightFloatStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + Fl::add_timeout (2, addTextToFloatTimeout, NULL); + int errorCode = Fl::run(); + + wordStyle->unref(); + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_image_background.cc b/test/dw/dw_image_background.cc new file mode 100644 index 00000000..14cb7b97 --- /dev/null +++ b/test/dw/dw_image_background.cc @@ -0,0 +1,196 @@ +/* + * Dillo Widget + * + * Copyright 2013 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 <stdlib.h> + +#include <FL/Fl.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" +#include "../dw/image.hh" + +using namespace lout::signal; +using namespace lout::misc; +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +class ImageStyleDeletionReceiver: public ObservedObject::DeletionReceiver +{ +public: + void deleted (ObservedObject *object); +}; + +static StyleImage *image1 = NULL, *image2 = NULL; +static Layout *layout; +static int imgRow1 = 0, imgRow2 = 0; +static ImageStyleDeletionReceiver isdr; + +void ImageStyleDeletionReceiver::deleted (ObservedObject *object) +{ + if ((StyleImage*)object == image1) + image1 = NULL; + else if ((StyleImage*)object == image2) + image2 = NULL; + else + assertNotReached (); +} + +static void imageInitTimeout (void *data) +{ + if (image1) { + Imgbuf *imgbuf1 = layout->createImgbuf (Imgbuf::RGB, 400, 200, 1); + image1->getMainImgRenderer()->setBuffer (imgbuf1, false); + } + + if (image2) { + Imgbuf *imgbuf2 = layout->createImgbuf (Imgbuf::RGB, 100, 100, 1); + image2->getMainImgRenderer()->setBuffer (imgbuf2, false); + } +} + +static void imageDrawTimeout (void *data) +{ + Imgbuf *imgbuf1 = image1 ? image1->getImgbufSrc () : NULL; + Imgbuf *imgbuf2 = image2 ? image2->getImgbufSrc () : NULL; + + if (imgbuf1 && imgRow1 < 200) { + byte buf[3 * 400]; + for(int x = 0; x < 400; x++) { + buf[3 * x + 0] = 128 + x * 127 / 399; + buf[3 * x + 1] = 128 + (399 - x) * 127 / 399; + buf[3 * x + 2] = 128 + imgRow1 * 127 / 199; + } + + imgbuf1->copyRow (imgRow1, buf); + image1->getMainImgRenderer()->drawRow (imgRow1); + imgRow1++; + } + + if (imgbuf2 && imgRow2 < 100) { + byte buf[3 * 100]; + for(int x = 0; x < 100; x++) { + int r = 128 + rand () % 127; + buf[3 * x + 0] = buf[3 * x + 1] = buf[3 * x + 2] = r; + } + + imgbuf2->copyRow (imgRow2, buf); + image2->getMainImgRenderer()->drawRow (imgRow2); + imgRow2++; + } + + if(imgRow1 < 200 || imgRow2 < 100) + Fl::repeat_timeout (0.5, imageDrawTimeout, NULL); +} + +int main(int argc, char **argv) +{ + FltkPlatform *platform = new FltkPlatform (); + 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); + + image1 = StyleImage::create (); + image1->connectDeletion (&isdr); + layout->setBgImage (image1, BACKGROUND_REPEAT_Y, + BACKGROUND_ATTACHMENT_SCROLL, createPerLength (0.5), + createAbsLength (30)); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + styleAttrs.x_lang[0] = 'e'; + styleAttrs.x_lang[1] = 'n'; + + 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 *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + styleAttrs.backgroundImage = NULL; + + Style *wordStyle = Style::create (&styleAttrs); + + image2 = styleAttrs.backgroundImage = StyleImage::create (); + image2->connectDeletion (&isdr); + styleAttrs.backgroundRepeat = BACKGROUND_REPEAT; + styleAttrs.backgroundPositionX = createPerLength (0); + styleAttrs.backgroundPositionY = createPerLength (0); + Style *wordStyleBg = Style::create (&styleAttrs); + + for(int i = 1; i <= 1; 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], j == 11 ? wordStyleBg : wordStyle); + textblock->addSpace(wordStyle); + } + + textblock->addParbreak(10, wordStyle); + } + + wordStyle->unref(); + wordStyleBg->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + + Fl::add_timeout (1.0, imageInitTimeout, NULL); + Fl::add_timeout (0.1, imageDrawTimeout, NULL); + + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_images_scaled.cc b/test/dw/dw_images_scaled.cc new file mode 100644 index 00000000..e31abfaa --- /dev/null +++ b/test/dw/dw_images_scaled.cc @@ -0,0 +1,155 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" +#include "../dw/image.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +static Layout *layout; +static Image *image; +static core::Imgbuf *imgbuf = NULL; +static int imgRow = 0; + +static void imageInitTimeout (void *data) +{ + //imgbuf = layout->createImgbuf (Imgbuf::RGBA, 400, 200); + imgbuf = layout->createImgbuf (Imgbuf::RGB, 400, 200, 1); + image->setBuffer (imgbuf); +} + +/* +static void imageDrawTimeout (void *data) +{ + if (imgbuf) { + for (int i = 0; i < 1; i++) { + byte buf[4 * 400]; + for(int x = 0; x < 400; x++) { + buf[4 * x + 0] = x * 255 / 399; + buf[4 * x + 1] = (399 - x) * 255 / 399; + buf[4 * x + 2] = imgRow * 255 / 199; + buf[4 * x + 3] = (199 - imgRow) * 255 / 199; + } + + imgbuf->copyRow (imgRow, buf); + image->drawRow (imgRow); + imgRow++; + } + } + + if(imgRow < 200) + ::fltk::repeat_timeout (0.5, imageDrawTimeout, NULL); +} +*/ + +static void imageDrawTimeout (void *data) +{ + if (imgbuf) { + for (int i = 0; i < 1; i++) { + byte buf[3 * 400]; + for(int x = 0; x < 400; x++) { + buf[3 * x + 0] = x * 255 / 399; + buf[3 * x + 1] = (399 - x) * 255 / 399; + buf[3 * x + 2] = imgRow * 255 / 199; + } + + imgbuf->copyRow (imgRow, buf); + image->drawRow (imgRow); + imgRow++; + } + } + + if(imgRow < 200) + Fl::repeat_timeout (0.5, imageDrawTimeout, NULL); +} + +int main(int argc, char **argv) +{ + FltkPlatform *platform = new FltkPlatform (); + layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(410, 210, "Dw Scaled Image"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 410, 210); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + styleAttrs.width = createPerLength (1.0); + styleAttrs.height = createPerLength (1.0); + + 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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + + Style *imageStyle = Style::create (&styleAttrs); + + image = new dw::Image (""); + textblock->addWidget (image, imageStyle); + textblock->addSpace (imageStyle); + + imageStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + + Fl::add_timeout (2.0, imageInitTimeout, NULL); + Fl::add_timeout (0.1, imageDrawTimeout, NULL); + + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_images_scaled2.cc b/test/dw/dw_images_scaled2.cc new file mode 100644 index 00000000..01a55bcf --- /dev/null +++ b/test/dw/dw_images_scaled2.cc @@ -0,0 +1,150 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" +#include "../dw/image.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +static Layout *layout; +static Image *image1, *image2; +static core::Imgbuf *imgbuf = NULL; +static int imgRow = 0; + +static void imageInitTimeout (void *data) +{ + imgbuf = layout->createImgbuf (Imgbuf::RGB, 400, 200, 1); + image1->setBuffer (imgbuf); + image2->setBuffer (imgbuf); +} + +static void imageDrawTimeout (void *data) +{ + if (imgbuf) { + for (int i = 0; i < 1; i++) { + byte buf[3 * 400]; + for(int x = 0; x < 400; x++) { + buf[3 * x + 0] = x * 255 / 399; + buf[3 * x + 1] = (399 - x) * 255 / 399; + buf[3 * x + 2] = imgRow * 255 / 199; + } + + imgbuf->copyRow (imgRow, buf); + image1->drawRow (imgRow); + image2->drawRow (imgRow); + imgRow++; + } + } + + if(imgRow < 200) + Fl::repeat_timeout (0.5, imageDrawTimeout, NULL); +} + +int main(int argc, char **argv) +{ + FltkPlatform *platform = new FltkPlatform (); + layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(410, 210, "Dw Scaled Image 2"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 410, 210); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.borderWidth.setVal (0); + styleAttrs.padding.setVal (0); + styleAttrs.backgroundColor = NULL; + + Style *wordStyle = Style::create (&styleAttrs); + + styleAttrs.borderWidth.setVal (1); + styleAttrs.setBorderColor (Color::create (layout, 0x000080)); + styleAttrs.setBorderStyle (BORDER_SOLID); + styleAttrs.padding.setVal (1); + styleAttrs.backgroundColor = NULL; + styleAttrs.width = createPerLength (0.25); + styleAttrs.height = createPerLength (0.25); + + Style *imageStyle1 = Style::create (&styleAttrs); + image1 = new dw::Image ("A longer ALT Text to demonstrate clipping."); + textblock->addWidget (image1, imageStyle1); + imageStyle1->unref(); + + textblock->addParbreak (10, wordStyle); + + styleAttrs.width = LENGTH_AUTO; + styleAttrs.height = LENGTH_AUTO; + + Style *imageStyle2 = Style::create (&styleAttrs); + image2 = new dw::Image ("A longer ALT Text to demonstrate clipping."); + textblock->addWidget (image2, imageStyle2); + imageStyle2->unref(); + + wordStyle->unref (); + textblock->flush (); + + window->resizable(viewport); + window->show(); + + Fl::add_timeout (3.0, imageInitTimeout, NULL); + Fl::add_timeout (0.1, imageDrawTimeout, NULL); + + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_images_simple.cc b/test/dw/dw_images_simple.cc new file mode 100644 index 00000000..361ede68 --- /dev/null +++ b/test/dw/dw_images_simple.cc @@ -0,0 +1,154 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" +#include "../dw/image.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +static Layout *layout; +static Image *image; +static core::Imgbuf *imgbuf = NULL; +static int imgRow = 0; + +static void imageInitTimeout (void *data) +{ + const bool resize = true; + //imgbuf = layout->createImgbuf (Imgbuf::RGBA, 400, 200); + imgbuf = layout->createImgbuf (Imgbuf::RGB, 400, 200, 1); + image->setBuffer (imgbuf, resize); +} + +/* +static void imageDrawTimeout (void *data) +{ + if (imgbuf) { + for (int i = 0; i < 1; i++) { + byte buf[4 * 400]; + for(int x = 0; x < 400; x++) { + buf[4 * x + 0] = x * 255 / 399; + buf[4 * x + 1] = (399 - x) * 255 / 399; + buf[4 * x + 2] = imgRow * 255 / 199; + buf[4 * x + 3] = (199 - imgRow) * 255 / 199; + } + + imgbuf->copyRow (imgRow, buf); + image->drawRow (imgRow); + imgRow++; + } + } + + if(imgRow < 200) + Fl::repeat_timeout (0.5, imageDrawTimeout, NULL); +} +*/ + +static void imageDrawTimeout (void *data) +{ + if (imgbuf) { + for (int i = 0; i < 1; i++) { + byte buf[3 * 400]; + for(int x = 0; x < 400; x++) { + buf[3 * x + 0] = x * 255 / 399; + buf[3 * x + 1] = (399 - x) * 255 / 399; + buf[3 * x + 2] = imgRow * 255 / 199; + } + + imgbuf->copyRow (imgRow, buf); + image->drawRow (imgRow); + imgRow++; + } + } + + if(imgRow < 200) + Fl::repeat_timeout (0.5, imageDrawTimeout, NULL); +} + +int main(int argc, char **argv) +{ + FltkPlatform *platform = new FltkPlatform (); + layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(410, 210, "Dw Simple Image"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 410, 210); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + + Style *imageStyle = Style::create (&styleAttrs); + + image = new dw::Image (""); + textblock->addWidget (image, imageStyle); + textblock->addSpace (imageStyle); + + imageStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + + Fl::add_timeout (2.0, imageInitTimeout, NULL); + Fl::add_timeout (0.1, imageDrawTimeout, NULL); + + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_imgbuf_mem_test.cc b/test/dw/dw_imgbuf_mem_test.cc new file mode 100644 index 00000000..e2532ea7 --- /dev/null +++ b/test/dw/dw_imgbuf_mem_test.cc @@ -0,0 +1,108 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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 "../dw/core.hh" +#include "../dw/fltkcore.hh" + +using namespace lout::signal; +using namespace dw::core; +using namespace dw::fltk; + +void solution1 () +{ + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Imgbuf *rootbuf = layout->createImgbuf (Imgbuf::RGB, 100, 100, 1); + rootbuf->ref (); // Extra reference by the dicache. + printf ("=== Can be deleted? %s.\n", + rootbuf->lastReference () ? "Yes" : "No"); + Imgbuf *scaledbuf = rootbuf->getScaledBuf (50, 50); + printf ("=== Can be deleted? %s.\n", + rootbuf->lastReference () ? "Yes" : "No"); + rootbuf->unref (); + printf ("=== Can be deleted? %s.\n", + rootbuf->lastReference () ? "Yes" : "No"); + scaledbuf->unref (); + printf ("=== Can be deleted? %s.\n", + rootbuf->lastReference () ? "Yes" : "No"); + rootbuf->unref (); // Extra reference by the dicache. + + delete layout; +} + +void solution2 () +{ + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Imgbuf *rootbuf = layout->createImgbuf (Imgbuf::RGB, 100, 100, 1); + rootbuf->setDeleteOnUnref (false); + printf ("=== Can be deleted? %s.\n", + !rootbuf->isReferred () ? "Yes" : "No"); + Imgbuf *scaledbuf = rootbuf->getScaledBuf (50, 50); + printf ("=== Can be deleted? %s.\n", + !rootbuf->isReferred () ? "Yes" : "No"); + rootbuf->unref (); + printf ("=== Can be deleted? %s.\n", + !rootbuf->isReferred () ? "Yes" : "No"); + scaledbuf->unref (); + printf ("=== Can be deleted? %s.\n", + !rootbuf->isReferred () ? "Yes" : "No"); + delete rootbuf; + + delete layout; +} + +class RootbufDeletionReceiver: public ObservedObject::DeletionReceiver +{ + void deleted (ObservedObject *object); +}; + +void RootbufDeletionReceiver::deleted (ObservedObject *object) +{ + printf ("=== Is deleted now.\n"); + delete this; +} + +void solution3 () +{ + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Imgbuf *rootbuf = layout->createImgbuf (Imgbuf::RGB, 100, 100, 1); + rootbuf->connectDeletion (new RootbufDeletionReceiver ()); + Imgbuf *scaledbuf = rootbuf->getScaledBuf (50, 50); + rootbuf->unref (); + scaledbuf->unref (); + + delete layout; +} + +int main (int argc, char **argv) +{ + printf ("========== SOLUTION 1 ==========\n"); + solution1 (); + printf ("========== SOLUTION 2 ==========\n"); + solution2 (); + printf ("========== SOLUTION 3 ==========\n"); + solution3 (); + + return 0; +} diff --git a/test/dw/dw_links.cc b/test/dw/dw_links.cc new file mode 100644 index 00000000..79ccbb54 --- /dev/null +++ b/test/dw/dw_links.cc @@ -0,0 +1,163 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +class LinkTestReceiver: public Layout::LinkReceiver +{ + bool enter (Widget *widget, int link, int img, int x, int y); + bool press (Widget *widget, int link, int img, int x, int y, + EventButton *event); + bool release (Widget *widget, int link, int img, int x, int y, + EventButton *event); + bool click (Widget *widget, int link, int img, + int x, int y, EventButton *event); +}; + +bool LinkTestReceiver::enter (Widget *widget, int link, int img, int x, int y) +{ + printf ("enter: %d\n", link); + return true; +} + +bool LinkTestReceiver::press (Widget *widget, int link, int img, int x, int y, + EventButton *event) +{ + printf ("press: %d\n", link); + return true; +} + +bool LinkTestReceiver::release (Widget *widget, int link, int img, int x,int y, + EventButton *event) +{ + printf ("release: %d\n", link); + return true; +} + +bool LinkTestReceiver::click (Widget *widget, int link, int img, int x, int y, + EventButton *event) +{ + printf ("click: %d\n", link); + return true; +} + +int main(int argc, char **argv) +{ + LinkTestReceiver linkTestReceiver; + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(200, 300, "Dw Links"); + 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); + + 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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + layout->connectLink (&linkTestReceiver); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + styleAttrs.cursor = CURSOR_TEXT; + + Style *wordStyle = Style::create (&styleAttrs); + + styleAttrs.color = Color::create (layout, 0x0000ff); + styleAttrs.textDecoration = TEXT_DECORATION_UNDERLINE; + styleAttrs.cursor = CURSOR_POINTER; + + for(int i = 1; i <= 10; i++) { + char buf[4]; + sprintf(buf, "%d.", i); + + const char *words1[] = { + "This", "is", "the", buf, "paragraph.", + "Here", "comes", "some", "more", "text", + "to", "demonstrate", "word", "wrapping.", + NULL }; + const char *words2[] = { + "Click", "here", "for", "more..", NULL }; + + for(int j = 0; words1[j]; j++) { + textblock->addText(words1[j], wordStyle); + textblock->addSpace(wordStyle); + } + + styleAttrs.x_link = i; + Style *linkStyle = Style::create (&styleAttrs); + + for(int j = 0; words2[j]; j++) { + textblock->addText(words2[j], linkStyle); + textblock->addSpace(wordStyle); + } + + linkStyle->unref (); + + textblock->addParbreak(10, wordStyle); + } + + wordStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_links2.cc b/test/dw/dw_links2.cc new file mode 100644 index 00000000..acb095f3 --- /dev/null +++ b/test/dw/dw_links2.cc @@ -0,0 +1,192 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_Box.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::fltk; + +class LinkTestReceiver: public Layout::LinkReceiver +{ + bool enter (Widget *widget, int link, int img, int x, int y); + bool press (Widget *widget, int link, int img, int x, int y, + EventButton *event); + bool release (Widget *widget, int link, int img, int x, int y, + EventButton *event); + bool click (Widget *widget, int link, int img, int x, int y, + EventButton *event); +}; + +bool LinkTestReceiver::enter (Widget *widget, int link, int img, int x, int y) +{ + printf ("enter: %d\n", link); + return true; +} + +bool LinkTestReceiver::press (Widget *widget, int link, int img, int x, int y, + EventButton *event) +{ + printf ("press: %d\n", link); + return true; +} + +bool LinkTestReceiver::release (Widget *widget, int link, int img, int x,int y, + EventButton *event) +{ + printf ("release: %d\n", link); + return true; +} + +bool LinkTestReceiver::click (Widget *widget, int link, int img, int x, int y, + EventButton *event) +{ + printf ("click: %d\n", link); + return true; +} + +int main(int argc, char **argv) +{ + int MainIdx; + int ww = 200, wh = 300, lh = 24; + + FltkPlatform *platform = new FltkPlatform (); + Layout *layout = new Layout (platform); + + Fl_Window *window = new Fl_Window(200, 300, "Dw Links2"); + window->box(FL_NO_BOX); + window->begin(); + Fl_Widget *Panel = new Fl_Box(0, 0, ww, lh, "CONTROL PANEL"); + + Panel->color(FL_GRAY_RAMP + 3); + Panel->labelcolor(FL_WHITE); + Panel->box(FL_FLAT_BOX); + Fl_Widget *Main = new Fl_Box(0, lh, ww, wh - 2*lh, "MAIN RENDERING AREA"); + Main->color(FL_GRAY_RAMP + 4); + Main->labelcolor(FL_WHITE); + MainIdx = window->find(Main); + /* status bar */ + Fl_Widget *Bar = new Fl_Box(0, wh - lh, 200, lh, "STATUS BAR..."); + Bar->color(FL_GRAY_RAMP + 3); + Bar->labelcolor(FL_WHITE); + Bar->box(FL_FLAT_BOX); + + window->resizable(Main); + window->end(); + + // + // Create the main Dw and add some text there. + // + window->remove(MainIdx); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, lh, ww, wh - 2*lh); + layout->attachView (viewport); + + window->end(); + + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + layout->connectLink (new LinkTestReceiver ()); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + styleAttrs.cursor = CURSOR_TEXT; + + Style *wordStyle = Style::create (&styleAttrs); + + styleAttrs.color = Color::create (layout, 0x0000ff); + styleAttrs.textDecoration = TEXT_DECORATION_UNDERLINE; + styleAttrs.cursor = CURSOR_POINTER; + + for(int i = 1; i <= 30; i++) { + char buf[4]; + sprintf(buf, "%d.", i); + + const char *words1[] = { + "This", "is", "the", buf, "paragraph.", + "Here", "comes", "some", "more", "text", + "to", "demonstrate", "word", "wrapping.", + NULL }; + const char *words2[] = { + "Click", "here", "for", "more..", NULL }; + + for(int j = 0; words1[j]; j++) { + textblock->addText (words1[j], wordStyle); + textblock->addSpace(wordStyle); + } + + styleAttrs.x_link = i; + Style *linkStyle = Style::create (&styleAttrs); + + for(int j = 0; words2[j]; j++) { + textblock->addText (words2[j], linkStyle); + textblock->addSpace(wordStyle); + } + + linkStyle->unref (); + + textblock->addParbreak(10, wordStyle); + } + + wordStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_lists.cc b/test/dw/dw_lists.cc new file mode 100644 index 00000000..2aa0abb7 --- /dev/null +++ b/test/dw/dw_lists.cc @@ -0,0 +1,136 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" +#include "../dw/listitem.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 Lists"); + 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); + + 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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + styleAttrs.cursor = CURSOR_TEXT; + + Style *wordStyle = Style::create (&styleAttrs); + + styleAttrs.margin.setVal (5); + styleAttrs.padding.setVal (5); + styleAttrs.backgroundColor = Color::create (layout, 0xffff40); + styleAttrs.setBorderColor (Color::create (layout, 0x000000)); + styleAttrs.setBorderStyle (BORDER_SOLID); + styleAttrs.borderWidth.setVal (1); + + Style *itemStyle = Style::create (&styleAttrs); + + const char *wordsPar[] = { + "This", "is", "a", "normal", "paragraph.", "And", + "some", "list", "items", "follow:", NULL }; + const char *wordsItem[] = { + "This", "is", "a", "list", "item.", "Here", + "comes", "some", "more", "text", "to", + "demonstrate", "word", "wrapping.", NULL }; + + + for(int i = 0; wordsPar[i]; i++) { + if(i != 0) + textblock->addSpace (wordStyle); + textblock->addText (wordsPar[i], wordStyle); + } + textblock->addParbreak (5, wordStyle); + + ListItem *refItem = NULL; + + for(int i = 1; i <= 100; i++) { + ListItem *listItem = new ListItem (refItem, false); + refItem = listItem; + + textblock->addWidget (listItem, itemStyle); + textblock->addParbreak (2, wordStyle); + + char buf[16]; + sprintf (buf, "%d.", i); + listItem->initWithText (buf, wordStyle); + + for(int j = 0; wordsItem[j]; j++) { + if(j != 0) + listItem->addSpace (wordStyle); + listItem->addText (wordsItem[j], wordStyle); + } + + listItem->flush (); + } + + wordStyle->unref(); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_resource_test.cc b/test/dw/dw_resource_test.cc new file mode 100644 index 00000000..e8a8b227 --- /dev/null +++ b/test/dw/dw_resource_test.cc @@ -0,0 +1,102 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/textblock.hh" +#include "../dw/ui.hh" + +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::core::ui; +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(410, 210, "Dw Resource test"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 410, 210); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + Style *widgetStyle = Style::create (&styleAttrs); + + Textblock *textblock = new Textblock (false); + textblock->setStyle (widgetStyle); + layout->setWidget (textblock); + + widgetStyle->unref(); + + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + + widgetStyle = Style::create (&styleAttrs); + + SelectionResource *res = layout->getResourceFactory()->createListResource + (ListResource::SELECTION_AT_MOST_ONE, 4); + //SelectionResource *res = + // layout->getResourceFactory()->createOptionMenuResource (); + + Embed *embed = new Embed (res); + textblock->addWidget (embed, widgetStyle); + textblock->addSpace (widgetStyle); + + widgetStyle->unref(); + + for(int i = 0; i < 50; i++) + res->addItem ("Hello, world!", true, i == 0 ? true : false); + + textblock->flush (); + + window->resizable(viewport); + window->show(); + + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_simple_container.cc b/test/dw/dw_simple_container.cc new file mode 100644 index 00000000..e7fa563c --- /dev/null +++ b/test/dw/dw_simple_container.cc @@ -0,0 +1,245 @@ +/* + * 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::sizeRequestSimpl (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, true, true); +} + + +void SimpleContainer::getExtremesSimpl (Extremes *extremes) +{ + Extremes childExtr; + if (child) + child->getExtremes (&childExtr); + else + childExtr.minWidth = childExtr.minWidthIntrinsic = childExtr.maxWidth = + childExtr.maxWidthIntrinsic = extremes->adjustmentWidth = 0; + + extremes->minWidth = childExtr.minWidth + boxDiffWidth (); + extremes->minWidthIntrinsic = childExtr.minWidthIntrinsic + boxDiffWidth (); + extremes->maxWidth = childExtr.maxWidth + boxDiffWidth (); + extremes->maxWidthIntrinsic = childExtr.maxWidthIntrinsic + boxDiffWidth (); + extremes->adjustmentWidth = childExtr.adjustmentWidth + boxDiffWidth (); + + correctExtremes (extremes, true); +} + +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, + DrawingContext *context) +{ + drawWidgetBox (view, area, false); + Rectangle childArea; + if (child && child->intersects (this, area, &childArea)) + child->draw (view, &childArea, context); +} + +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/dw_simple_container.hh b/test/dw/dw_simple_container.hh new file mode 100644 index 00000000..bd40b41e --- /dev/null +++ b/test/dw/dw_simple_container.hh @@ -0,0 +1,57 @@ +#ifndef __DW_SIMPLE_CONTAINER_HH__ +#define __DW_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 sizeRequestSimpl (core::Requisition *requisition); + void getExtremesSimpl (core::Extremes *extremes); + void sizeAllocateImpl (core::Allocation *allocation); + +public: + static int CLASS_ID; + + SimpleContainer (); + ~SimpleContainer (); + + void draw (core::View *view, core::Rectangle *area, + core::DrawingContext *context); + 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/dw_simple_container_test.cc b/test/dw/dw_simple_container_test.cc new file mode 100644 index 00000000..83d0a77f --- /dev/null +++ b/test/dw/dw_simple_container_test.cc @@ -0,0 +1,114 @@ +/* + * 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); + + 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 *textblockStyle1 = Style::create (&styleAttrs); + + styleAttrs.backgroundColor = NULL; + styleAttrs.margin.setVal (0); + + Style *textblockStyle2 = Style::create (&styleAttrs); + Style *wordStyle = Style::create (&styleAttrs); + + styleAttrs.borderWidth.setVal (5); + styleAttrs.setBorderColor (Color::create (layout, 0x800080)); + styleAttrs.setBorderStyle (BORDER_DASHED); + styleAttrs.padding.setVal (5); + + Style *containerStyle = Style::create (&styleAttrs); + + Textblock *textblock1 = new Textblock (false); + textblock1->setStyle (textblockStyle1); + layout->setWidget (textblock1); + + SimpleContainer *simpleContainer = new SimpleContainer (); + simpleContainer->setStyle (containerStyle); + textblock1->addWidget (simpleContainer, containerStyle); + + Textblock *textblock2 = new Textblock (false); + textblock2->setStyle (textblockStyle2); + simpleContainer->setChild (textblock2); + + const char *words[] = { "This", "is", "only", "a", "short", "paragraph.", + NULL }; + + for(int j = 0; words[j]; j++) { + textblock2->addText(words[j], wordStyle); + textblock2->addSpace(wordStyle); + } + + textblockStyle1->unref(); + textblockStyle2->unref(); + containerStyle->unref(); + wordStyle->unref(); + + textblock1->flush (); + textblock2->flush (); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_table.cc b/test/dw/dw_table.cc new file mode 100644 index 00000000..9bec1a09 --- /dev/null +++ b/test/dw/dw_table.cc @@ -0,0 +1,116 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/table.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(300, 300, "Dw Table"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 300, 300); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + styleAttrs.padding.setVal (0); + styleAttrs.borderWidth.setVal (1); + styleAttrs.setBorderStyle (BORDER_OUTSET); + styleAttrs.setBorderColor (Color::create (layout, 0xffffff)); + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + styleAttrs.hBorderSpacing = 5; + styleAttrs.vBorderSpacing = 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 = dw::core::style::Font::create (layout, &fontAttrs); + + Style *tableStyle = Style::create (&styleAttrs); + + Table *table = new Table (false); + table->setStyle (tableStyle); + layout->setWidget (table); + + tableStyle->unref(); + + styleAttrs.setBorderStyle (BORDER_INSET); + styleAttrs.backgroundColor = NULL; + styleAttrs.margin.setVal (0); + styleAttrs.padding.setVal (5); + + Style *cellStyle = Style::create (&styleAttrs); + + styleAttrs.borderWidth.setVal (0); + styleAttrs.margin.setVal (0); + styleAttrs.cursor = CURSOR_TEXT; + styleAttrs.textAlignChar = '.'; + + Style *wordStyle = Style::create (&styleAttrs); + + for (int i = 0; i < 4; i++) { + table->addRow (wordStyle); + + for (int j = 0; j < 4; j++) { + Textblock *cell = new Textblock (false); + cell->setStyle (cellStyle); + table->addCell (cell, 1, 1); + + char buf[10]; + sprintf (buf, "cell %c", 'A' + 4 * i + j); + + cell->addText (buf, wordStyle); + cell->flush (); + } + } + + wordStyle->unref(); + cellStyle->unref(); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_table_aligned.cc b/test/dw/dw_table_aligned.cc new file mode 100644 index 00000000..bef3d521 --- /dev/null +++ b/test/dw/dw_table_aligned.cc @@ -0,0 +1,121 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/table.hh" +#include "../dw/alignedtablecell.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 Table Aligned"); + 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 (1); + styleAttrs.setBorderStyle (BORDER_OUTSET); + styleAttrs.setBorderColor (Color::create (layout, 0x808080)); + + 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 = dw::core::style::Font::create (layout, &fontAttrs); + + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xa0a0a0); + styleAttrs.hBorderSpacing = 5; + styleAttrs.vBorderSpacing = 5; + + Style *tableStyle = Style::create (&styleAttrs); + + Table *table = new Table (false); + table->setStyle (tableStyle); + layout->setWidget (table); + + tableStyle->unref(); + + styleAttrs.borderWidth.setVal (1); + styleAttrs.setBorderStyle (BORDER_INSET); + + Style *cellStyle = Style::create (&styleAttrs); + + styleAttrs.borderWidth.setVal (0); + styleAttrs.margin.setVal (0); + styleAttrs.backgroundColor = NULL; + styleAttrs.cursor = CURSOR_TEXT; + styleAttrs.textAlignChar = '.'; + + Style *wordStyle = Style::create (&styleAttrs); + + AlignedTableCell *ref = NULL; + for(int i = 0; i < 10; i++) { + //for(int i = 0; i < 1; i++) { + AlignedTableCell *cell = new AlignedTableCell (ref, false); + cell->setStyle (cellStyle); + ref = cell; + table->addRow (wordStyle); + table->addCell (cell, 1, 1); + + char buf[16]; + for(int j = 0; j < i; j++) + buf[j] = '0' + j; + buf[i] = '.'; + for(int j = i + 1; j < 11; j++) + buf[j] = '0' + (j - 1); + buf[11] = 0; + + cell->addText (buf, wordStyle); + cell->flush (); + } + + wordStyle->unref(); + cellStyle->unref(); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete layout; + + return errorCode; +} diff --git a/test/dw/dw_ui_test.cc b/test/dw/dw_ui_test.cc new file mode 100644 index 00000000..f24cc9b9 --- /dev/null +++ b/test/dw/dw_ui_test.cc @@ -0,0 +1,245 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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.H> +#include <FL/Fl_Window.H> + +#include "../dw/core.hh" +#include "../dw/fltkcore.hh" +#include "../dw/fltkviewport.hh" +#include "../dw/table.hh" +#include "../dw/textblock.hh" +#include "../dw/ui.hh" +#include "form.hh" + +using namespace lout::object; +using namespace lout::container::typed; +using namespace dw; +using namespace dw::core; +using namespace dw::core::style; +using namespace dw::core::ui; +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(400, 400, "Dw UI Test"); + window->box(FL_NO_BOX); + window->begin(); + + FltkViewport *viewport = new FltkViewport (0, 0, 400, 400); + layout->attachView (viewport); + + StyleAttrs styleAttrs; + styleAttrs.initValues (); + styleAttrs.margin.setVal (5); + styleAttrs.color = Color::create (layout, 0x000000); + styleAttrs.backgroundColor = Color::create (layout, 0xffffff); + + FontAttrs fontAttrs; + fontAttrs.name = "Helvetica"; + fontAttrs.size = 14; + fontAttrs.weight = 400; + fontAttrs.style = FONT_STYLE_NORMAL; + fontAttrs.letterSpacing = 0; + fontAttrs.fontVariant = FONT_VARIANT_NORMAL; + styleAttrs.font = dw::core::style::Font::create (layout, &fontAttrs); + + Style *tableStyle = Style::create (&styleAttrs); + + Table *table = new Table (false); + table->setStyle (tableStyle); + layout->setWidget (table); + + tableStyle->unref(); + + styleAttrs.backgroundColor = NULL; + styleAttrs.margin.setVal (0); + + Style *cellStyle = Style::create (&styleAttrs); + + // First of all, the resources. Later, they are embedded into the + // widget tree. + EntryResource *entryres1 = + layout->getResourceFactory()->createEntryResource (10, false, NULL,NULL); + entryres1->setText ("Hi!"); + EntryResource *entryres2 = + layout->getResourceFactory()->createEntryResource (10, true, NULL, + "password field!"); + MultiLineTextResource *textres = + layout->getResourceFactory()->createMultiLineTextResource (15,3, + "textarea placeholder!"); + RadioButtonResource *radiores1 = + layout->getResourceFactory()->createRadioButtonResource (NULL, false); + RadioButtonResource *radiores2 = + layout->getResourceFactory()->createRadioButtonResource (radiores1, + false); + CheckButtonResource *checkres = + layout->getResourceFactory()->createCheckButtonResource (true); + SelectionResource *selres[2]; + selres[0] = layout->getResourceFactory()->createOptionMenuResource (); + selres[1] = layout->getResourceFactory()->createListResource + (ListResource::SELECTION_AT_MOST_ONE, 4); + LabelButtonResource *buttonres = + layout->getResourceFactory()->createLabelButtonResource ("Run!"); + + // Note on complex buttons: before any operations on the widget, which + // need a layout, the complex button resource should be created, since + // then, a layout and a platform are instantiated. + Textblock *cbuttontext = new Textblock(false); + ComplexButtonResource *cbuttonres = + layout->getResourceFactory()->createComplexButtonResource (cbuttontext, + true); + cbuttontext->setStyle (cellStyle); + cbuttontext->addText ("Run (complex)!", cellStyle); + cbuttontext->flush (); + + // The entry resources are put into a special handler, which is + // also a receiver for the button resources. + form::Form *form = new form::Form(); + form->addTextResource ("val1", entryres1); + form->addTextResource ("val2", entryres2); + form->addTextResource ("text", textres); + const char *radiovalues[] = { "radio1", "radio2", NULL }; + form->addRadioButtonResource ("val3", radiores1, radiovalues); + form->addCheckButtonResource ("check", checkres); + const char *selvalues[] = { "i1", "g1", "i11", "i12", "i13", "(pop)", "i2", + "g2", "i21", "i22", "i23", "(pop)", "i3", NULL}; + form->addSelectionResource ("val4", selres[0], selvalues); + form->addSelectionResource ("val5", selres[1], selvalues); + form->addButtonResource ("button", buttonres, "Run!"); + form->addButtonResource ("cbutton", cbuttonres, "cbuttonval"); + + // Create the widgets. + table->addRow (cellStyle); + + Textblock *label1 = new Textblock(false); + label1->setStyle (cellStyle); + table->addCell (label1, 1, 1); + label1->addText ("val1 = ", cellStyle); + label1->flush (); + + Embed *input1 = new Embed (entryres1); + input1->setStyle (cellStyle); + table->addCell (input1, 1, 1); + + table->addRow (cellStyle); + + Textblock *label2 = new Textblock(false); + label2->setStyle (cellStyle); + table->addCell (label2, 1, 1); + label2->addText ("val2 = ", cellStyle); + label2->flush (); + + Embed *input2 = new Embed (entryres2); + input2->setStyle (cellStyle); + table->addCell (input2, 1, 1); + + table->addRow (cellStyle); + + Textblock *label = new Textblock(false); + label->setStyle (cellStyle); + table->addCell (label, 1, 1); + label->addText ("text = ", cellStyle); + label->flush (); + + Embed *text = new Embed (textres); + textres->setText("Hi textarea"); + text->setStyle (cellStyle); + table->addCell (text, 1, 1); + + table->addRow (cellStyle); + + Textblock *radiolabel1 = new Textblock(false); + radiolabel1->setStyle (cellStyle); + table->addCell (radiolabel1, 2, 1); + Embed *radio1 = new Embed (radiores1); + radiolabel1->addWidget (radio1, cellStyle); + radiolabel1->addText (" radio1", cellStyle); + radiolabel1->flush (); + + table->addRow (cellStyle); + Textblock *radiolabel2 = new Textblock(false); + radiolabel2->setStyle (cellStyle); + table->addCell (radiolabel2, 2, 1); + Embed *radio2 = new Embed (radiores2); + radiolabel2->addWidget (radio2, cellStyle); + radiolabel2->addText (" radio2", cellStyle); + radiolabel2->flush (); + + table->addRow (cellStyle); + Textblock *checklabel = new Textblock(false); + checklabel->setStyle (cellStyle); + table->addCell (checklabel, 2, 1); + Embed *check = new Embed (checkres); + checklabel->addWidget (check, cellStyle); + checklabel->addText (" check", cellStyle); + checklabel->flush (); + + for(int i = 0; i < 2; i++) { + table->addRow (cellStyle); + + Embed *sel = new Embed (selres[i]); + sel->setStyle (cellStyle); + table->addCell (sel, 2, 1); + + selres[i]->addItem("item 1", true, false); + + selres[i]->pushGroup("group 1", true); + selres[i]->addItem("item 1/1", true, false); + selres[i]->addItem("item 1/2", true, true); + selres[i]->addItem("item 1/3", false, false); + selres[i]->popGroup(); + + selres[i]->addItem("item 2", false, i == 1); + + selres[i]->pushGroup("group 2", true); + selres[i]->addItem("item 2/1", true, false); + selres[i]->addItem("item 2/2", true, false); + selres[i]->addItem("item 2/3", false, false); + selres[i]->popGroup(); + + selres[i]->addItem("item 3", false, false); + } + + table->addRow (cellStyle); + Embed *button = new Embed (buttonres); + button->setStyle (cellStyle); + table->addCell (button, 2, 1); + + table->addRow (cellStyle); + Embed *cbutton = new Embed (cbuttonres); + cbutton->setStyle (cellStyle); + table->addCell (cbutton, 2, 1); + + cellStyle->unref(); + + window->resizable(viewport); + window->show(); + int errorCode = Fl::run(); + + delete form; + delete layout; + + return errorCode; +} diff --git a/test/dw/form.cc b/test/dw/form.cc new file mode 100644 index 00000000..82938a16 --- /dev/null +++ b/test/dw/form.cc @@ -0,0 +1,263 @@ +/* + * Dillo Widget + * + * Copyright 2005-2007 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 "form.hh" + +namespace form { + +using namespace dw::core::ui; + +Form::ResourceDecorator::ResourceDecorator (const char *name) +{ + this->name = strdup (name); +} + +Form::ResourceDecorator::~ResourceDecorator () +{ + free((char *)name); +} + +Form::TextResourceDecorator::TextResourceDecorator (const char *name, + TextResource *resource): + Form::ResourceDecorator (name) +{ + this->resource = resource; +} + +const char *Form::TextResourceDecorator::getValue () +{ + return resource->getText (); +} + +Form::RadioButtonResourceDecorator::RadioButtonResourceDecorator + (const char *name, RadioButtonResource *resource, const char **values): + Form::ResourceDecorator (name) +{ + this->resource = resource; + + int n = 0; + while (values[n]) + n++; + this->values = new const char*[n + 1]; + for (int i = 0; i < n; i++) + this->values[i] = strdup (values[i]); + this->values[n] = 0; +} + +Form::RadioButtonResourceDecorator::~RadioButtonResourceDecorator () +{ + for (int i = 0; values[i]; i++) + free((char *)values[i]); + delete[] values; +} + +const char *Form::RadioButtonResourceDecorator::getValue () +{ + RadioButtonResource::GroupIterator *it; + int i; + for (it = resource->groupIterator (), i = 0; it->hasNext (); i++) { + RadioButtonResource *resource = it->getNext (); + if(resource->isActivated ()) { + it->unref (); + return values[i]; + } + } + + it->unref (); + return NULL; +} + +Form::CheckButtonResourceDecorator::CheckButtonResourceDecorator + (const char *name, CheckButtonResource *resource): + Form::ResourceDecorator (name) +{ + this->resource = resource; +} + +const char *Form::CheckButtonResourceDecorator::getValue () +{ + return resource->isActivated () ? "true" : NULL; +} + +Form::SelectionResourceDecorator::SelectionResourceDecorator + (const char *name, SelectionResource *resource, const char **values): + Form::ResourceDecorator (name) +{ + this->resource = resource; + + int n = 0; + while (values[n]) + n++; + this->values = new const char*[n + 1]; + for(int i = 0; i < n; i++) + this->values[i] = strdup (values[i]); + this->values[n] = 0; +} + +Form::SelectionResourceDecorator::~SelectionResourceDecorator () +{ + for(int i = 0; values[i]; i++) + free((char *)values[i]); + delete[] values; +} + +const char *Form::SelectionResourceDecorator::getValue () +{ + valueBuf.clear(); + int n = resource->getNumberOfItems (); + bool first = true; + for (int i = 0; i < n; i++) { + if (resource->isSelected (i)) { + if (!first) + valueBuf.append (", "); + valueBuf.append (values[i]); + first = false; + } + } + + return valueBuf.getChars (); +} + +void Form::FormActivateReceiver::activate (Resource *resource) +{ + form->send (NULL, NULL, -1, -1); +} + +void Form::FormActivateReceiver::enter (Resource *resource) +{ +} + +void Form::FormActivateReceiver::leave (Resource *resource) +{ +} + +Form::FormClickedReceiver::FormClickedReceiver (Form *form, const char *name, + const char *value) +{ + this->form = form; + this->name = strdup (name); + this->value = strdup (value); +} + +Form::FormClickedReceiver::~FormClickedReceiver () +{ + free((char *)name); + free((char *)value); +} + +void Form::FormClickedReceiver::clicked (Resource *resource, + dw::core::EventButton *event) +{ + form->send (name, value, event->xCanvas, event->yCanvas); +} + +Form::Form () +{ + resources = new lout::container::typed::List <ResourceDecorator> (true); + activateReceiver = new FormActivateReceiver (this); + clickedReceivers = + new lout::container::typed::List <FormClickedReceiver> (true); +} + +Form::~Form () +{ + delete resources; + delete activateReceiver; + delete clickedReceivers; +} + +/** + * \brief Adds an instance of dw::core::ui::TextResource. + */ +void Form::addTextResource (const char *name, + dw::core::ui::TextResource *resource) +{ + resources->append (new TextResourceDecorator (name, resource)); + resource->connectActivate (activateReceiver); +} + +/** + * \brief Adds an instance of dw::core::ui::RadioButtonResource. + * + * This method has to be called only once for a group of radio buttons. + */ +void Form::addRadioButtonResource (const char *name, + dw::core::ui::RadioButtonResource *resource, + const char **values) +{ + resources->append (new RadioButtonResourceDecorator (name, resource, + values)); + resource->connectActivate (activateReceiver); +} + +/** + * \brief Adds an instance of dw::core::ui::CheckButtonResource. + */ +void Form::addCheckButtonResource (const char *name, + dw::core::ui::CheckButtonResource *resource) +{ + resources->append (new CheckButtonResourceDecorator (name, resource)); + resource->connectActivate (activateReceiver); +} + +/** + * \brief Adds an instance of dw::core::ui::SelectionResource. + */ +void Form::addSelectionResource (const char *name, + dw::core::ui::SelectionResource *resource, + const char **values) +{ + resources->append (new SelectionResourceDecorator (name, resource, values)); + resource->connectActivate (activateReceiver); +} + +/** + * \todo Comment this; + */ +void Form::addButtonResource (const char *name, + dw::core::ui::ButtonResource *resource, + const char *value) +{ + FormClickedReceiver *receiver = + new FormClickedReceiver (this, name, value); + resource->connectClicked (receiver); + clickedReceivers->append (receiver); +} + +/** + * \todo Comment this; + */ +void Form::send (const char *buttonName, const char *buttonValue, int x, int y) +{ + for (lout::container::typed::Iterator <ResourceDecorator> it = + resources->iterator (); + it.hasNext (); ) { + ResourceDecorator *resource = it.getNext (); + const char *value = resource->getValue (); + if (value) + printf ("%s = %s; x=%d y=%d\n", resource->getName (), value, x, y); + } + + if(buttonName && buttonValue) + printf ("%s = %s\n", buttonName, buttonValue); +} + +} // namespace form diff --git a/test/dw/form.hh b/test/dw/form.hh new file mode 100644 index 00000000..6235c6cc --- /dev/null +++ b/test/dw/form.hh @@ -0,0 +1,164 @@ +#ifndef __TEST_FORM_HH__ +#define __TEST_FORM_HH__ + +#include "../dw/core.hh" +#include "../dw/ui.hh" + +namespace form { + +/** + * \brief Handles HTML form data. + * + * Add resources by calling the respective add...Resource method. Furtermore, + * this class impelements dw::core::ui::ButtonResource::ClickedReceiver, the + * form data is printed to stdout, when the "clicked" signal is received. + * + * \todo wrong comment + */ +class Form +{ +private: + /** + * \brief Decorates instances of dw::core::ui::Resource. + * + * This is the abstract base class, sub classes have to be defined to + * decorate specific sub interfaces of dw::core::ui::Resource. + */ + class ResourceDecorator: public lout::object::Object + { + private: + const char *name; + + protected: + ResourceDecorator (const char *name); + ~ResourceDecorator (); + + public: + inline const char *getName () { return name; } + virtual const char *getValue () = 0; + }; + + /** + * \brief Decorates instances of dw::core::ui::TextResource. + */ + class TextResourceDecorator: public ResourceDecorator + { + private: + dw::core::ui::TextResource *resource; + + public: + TextResourceDecorator (const char *name, + dw::core::ui::TextResource *resource); + const char *getValue (); + }; + + /** + * \brief Decorates instances of dw::core::ui::RadioButtonResource. + * + * This class has to be instantiated only once for a group of radio + * buttons. + */ + class RadioButtonResourceDecorator: public ResourceDecorator + { + private: + dw::core::ui::RadioButtonResource *resource; + const char **values; + + public: + RadioButtonResourceDecorator (const char *name, + dw::core::ui::RadioButtonResource + *resource, + const char **values); + ~RadioButtonResourceDecorator (); + const char *getValue (); + }; + + /** + * \brief Decorates instances of dw::core::ui::CheckButtonResource. + */ + class CheckButtonResourceDecorator: public ResourceDecorator + { + private: + dw::core::ui::CheckButtonResource *resource; + + public: + CheckButtonResourceDecorator (const char *name, + dw::core::ui::CheckButtonResource + *resource); + const char *getValue (); + }; + + /** + * \brief Decorates instances of dw::core::ui::SelectionResource. + */ + class SelectionResourceDecorator: public ResourceDecorator + { + private: + dw::core::ui::SelectionResource *resource; + const char **values; + lout::misc::StringBuffer valueBuf; + + public: + SelectionResourceDecorator (const char *name, + dw::core::ui::SelectionResource *resource, + const char **values); + ~SelectionResourceDecorator (); + const char *getValue (); + }; + + class FormActivateReceiver: public dw::core::ui::Resource::ActivateReceiver + { + private: + Form *form; + + public: + inline FormActivateReceiver (Form *form) { this->form = form; } + + void activate (dw::core::ui::Resource *resource); + void enter (dw::core::ui::Resource *resource); + void leave (dw::core::ui::Resource *resource); + }; + + class FormClickedReceiver: + public dw::core::ui::Resource::ClickedReceiver + { + private: + Form *form; + const char *name, *value; + + public: + FormClickedReceiver (Form *form, const char *name, const char *value); + ~FormClickedReceiver (); + + void clicked(dw::core::ui::Resource *resource, + dw::core::EventButton *event); + }; + + lout::container::typed::List <ResourceDecorator> *resources; + FormActivateReceiver *activateReceiver; + lout::container::typed::List <FormClickedReceiver> *clickedReceivers; + +public: + Form (); + ~Form (); + + void addTextResource (const char *name, + dw::core::ui::TextResource *resource); + void addRadioButtonResource (const char *name, + dw::core::ui::RadioButtonResource *resource, + const char **values); + void addCheckButtonResource (const char *name, + dw::core::ui::CheckButtonResource *resource); + void addSelectionResource (const char *name, + dw::core::ui::SelectionResource *resource, + const char **values); + void addButtonResource (const char *name, + dw::core::ui::ButtonResource *resource, + const char *value); + + void send (const char *buttonName, const char *buttonValue, int x, int y); +}; + +} // namespace form + +#endif // __TEST_FORM_HH__ |