diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2022-07-28 18:26:07 +0200 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2022-08-01 15:33:20 +0200 |
| commit | f37aca15e9786a7192e36ea4b0295ab4dc12819f (patch) | |
| tree | 549680fc1da3b72de9a38ed24d9ce1521b97cf71 /test/flex_demo.cxx | |
| parent | de8e6de25b33a3bf6c00da603d1332042ae51cf2 (diff) | |
Add Fl_Flex widget from Karsten Pedersen (issue #255)
This work is based on the repository and latest commit:
https://github.com/osen/FL_Flex.git
commit 36e4ed75a00daac825b87e81295818b4650991f5
Author: Karsten Pedersen <...>
Date: Fri Apr 23 12:06:16 2021 +0000
Added Fltk (LGPL) license.
This widget is similar to Fl_Pack and supports either one row or one
column of widgets but has some more features. Test and demo programs
are included:
test/flex_login.cxx: simple "login window" demo program
test/flex_demo.cxx: slightly more complex demo program
The original demo programs can still be compiled and built with
the new widget provided you '#include <FL/Fl_Flex.H>'.
Backwards compatible methods are included (except debug()).
The original widget has been modified to match FLTK standards and
enhanced in several ways, including:
- support box frames
- add HORIZONTAL and VERTICAL enum values (as in Fl_Pack)
- add horizontal() method (as in Fl_Pack)
- use type() rather than internal 'direction' variable
- add standard widget constructor (x, y, w, h, label)
- add margin and gap accessors rather than hard coding constants
- improve test and demo programs
- add documentation
- replace <vector> with array as required by FLTK CMP
- rename camelCase method names, keeping old names for compatibility:
- change 'setSize(Fl_Widget*, int)' to 'set_size(Fl_Widget*, int)'
- change 'bool isSetSize(Fl_Widget*)' to 'int set_size(Fl_Widget*)'
- remove debug() method
- add a way to "unset" fixed size: set_size(Fl_Widget *, 0)
- add layout() method to force recalculation of children
- unify resizeRow() and resizeCol() methods to avoid code duplication
- improve widget size calculation.
Diffstat (limited to 'test/flex_demo.cxx')
| -rw-r--r-- | test/flex_demo.cxx | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/test/flex_demo.cxx b/test/flex_demo.cxx new file mode 100644 index 000000000..fb2b73cef --- /dev/null +++ b/test/flex_demo.cxx @@ -0,0 +1,168 @@ +// +// Fl_Flex demo program for the Fast Light Tool Kit (FLTK). +// +// Copyright 2020 by Karsten Pedersen +// Copyright 2022 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#include <FL/Fl.H> +#include <FL/Fl_Double_Window.H> +#include <FL/Fl_Flex.H> +#include <FL/Fl_Box.H> +#include <FL/Fl_Button.H> +#include <FL/Fl_Input.H> + +#define DEBUG_GROUP (0) + +void debug_group(Fl_Group *g) { +#if (DEBUG_GROUP) + printf("\nFl_Group (%p) has %d children:\n", g, g->children()); + for (int i = 0; i < g->children(); i++) { + Fl_Widget *c = g->child(i); + printf(" child %2d: hidden = %-5s, (x,y,w,h) = (%3d, %3d, %3d, %3d), label = '%s'\n", + i, c->visible() ? "false" : "true", c->x(), c->y(), c->w(), c->h(), + c->label() ? c->label() : "(null)"); + } +#endif +} // debug_group + +Fl_Button *createButton(const char *caption) { + Fl_Button *rtn = new Fl_Button(0, 0, 120, 30, caption); + rtn->color(fl_rgb_color(225, 225, 225)); + return rtn; +} + +void toggle_cb(Fl_Widget *w, void *v) { + static Fl_Box *b = 0; + Fl_Widget *o = (Fl_Widget *)v; + Fl_Flex *flex = (Fl_Flex *)o->parent(); + if (o->visible()) { + o->hide(); + w->label("show OK button"); + flex->child(1)->hide(); // hide Box + } else { + o->show(); + w->label("hide OK button"); + flex->child(1)->show(); // show Box + } + flex->layout(); + + debug_group(flex); + + // Yet another test: modify the first (top) Fl_Flex widget + + flex = (Fl_Flex *)(flex->parent()->child(0)); + Fl_Group::current(0); + if (!b) { + b = new Fl_Box(0, 0, 0, 0, "Box3"); + flex->insert(*b, flex->children() - 1); + } else { + delete b; + b = 0; + } + flex->layout(); + debug_group(flex); +} + +Fl_Flex *createRow() { + Fl_Flex *row = new Fl_Flex(Fl_Flex::ROW); + { + Fl_Button *toggle = createButton("hide OK button"); + toggle->tooltip("hide() or show() OK button"); + Fl_Box *box2 = new Fl_Box(0, 0, 120, 10, "Box2"); + Fl_Button * okay = createButton("OK"); + new Fl_Input(0, 0, 120, 10, ""); + + toggle->callback(toggle_cb, okay); + + Fl_Flex *col2 = new Fl_Flex(Fl_Flex::COLUMN); + { + createButton("Top2"); + createButton("Bottom2"); + col2->end(); + col2->margin(0, 5); + col2->box(FL_FLAT_BOX); + col2->color(fl_rgb_color(255, 128, 128)); + } + + row->set_size(box2, 50); + row->set_size(col2, 100); + row->end(); + + // TEST + row->box(FL_DOWN_BOX); + row->color(FL_GREEN); + } + + return row; +} + +int main(int argc, char **argv) { + + Fl_Window *window = new Fl_Double_Window(100, 100, "Simple GUI Example"); + { + Fl_Flex *col = new Fl_Flex(5, 5, 90, 90, Fl_Flex::COLUMN); + { + Fl_Flex *row = new Fl_Flex(Fl_Flex::ROW); + row->color(FL_YELLOW); + row->box(FL_FLAT_BOX); + { + createButton("Cancel"); + new Fl_Box(0, 0, 120, 10, "Box1"); + createButton("OK"); + new Fl_Input(0, 0, 120, 10, ""); + + Fl_Flex *col1 = new Fl_Flex(Fl_Flex::COLUMN); + { + createButton("Top1"); + createButton("Bottom1"); + col1->end(); + col1->box(FL_FLAT_BOX); + col1->color(fl_rgb_color(255, 128, 128)); + col1->margin(5, 5); + } + + row->end(); + } + col->set_size(createRow(), 90); + createButton("Something1"); + row = new Fl_Flex(Fl_Flex::ROW); + { + Fl_Button *cancel = createButton("Cancel"); + Fl_Button *ok = createButton("OK"); + new Fl_Input(0, 0, 120, 10, ""); + + row->set_size(cancel, 100); + row->set_size(ok, 100); + row->end(); + } + createButton("Something2"); + + col->set_size(row, 30); + col->margin(0, 6); + col->end(); + } + window->resizable(col); + window->color(fl_rgb_color(160, 180, 240)); + window->box(FL_FLAT_BOX); + window->end(); + } + + window->size_range(550, 330); + window->resize(0, 0, 640, 480); + window->show(argc, argv); + + int ret = Fl::run(); + delete window; + return ret; +} |
