diff options
author | Aaron M. Ucko <ucko@debian.org> | 2010-09-27 16:10:19 -0400 |
---|---|---|
committer | Aaron M. Ucko <ucko@debian.org> | 2010-09-27 16:10:19 -0400 |
commit | 00740ce83c86dad306c2467f0d6e316231da93bc (patch) | |
tree | 9fe7f0437fb7be589a64648a54a0eacca608337c /examples | |
parent | 23e91462dac5ca7feb96f6b22ab4ea85b14d31ea (diff) |
Imported Upstream version 1.3~r7704
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 18 | ||||
-rw-r--r-- | examples/README.examples | 122 | ||||
-rw-r--r-- | examples/howto-add_fd-and-popen.cxx | 72 | ||||
-rw-r--r-- | examples/tabs-simple.cxx | 91 | ||||
-rw-r--r-- | examples/tree-simple.cxx | 71 | ||||
-rw-r--r-- | examples/wizard-simple.cxx | 89 |
6 files changed, 463 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..c55891a --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,18 @@ +include ../makeinclude + +ALL = howto-add_fd-and-popen$(EXEEXT) \ + tabs-simple$(EXEEXT) \ + tree-simple$(EXEEXT) \ + wizard-simple$(EXEEXT) + +# default target -- build everything +default: $(ALL) + +tabs-simple$(EXEEXT): tabs-simple.cxx + fltk-config --compile tabs-simple.cxx + +# clean everything +clean: + $(RM) $(ALL) + $(RM) *.o + $(RM) core diff --git a/examples/README.examples b/examples/README.examples new file mode 100644 index 0000000..6889eaa --- /dev/null +++ b/examples/README.examples @@ -0,0 +1,122 @@ +FLTK EXAMPLE PROGRAMS +--------------------- + + This directory contains example FLTK programs that demonstrate + recommended programming practices and techniques for FLTK application + programmers. The "*-simple.cxx" files are a good starting point for + those new to FLTK. + + The programs in this directory are NOT built automatically + when you build FLTK; you have to manually build them. + + The goals of these example programs: + + o Show good programming style for app programmers to emulate + + o Show simple examples of how to use widgets to new users of FLTK. + + o Show intermediate or advanced examples of techniques often + misused or hard to document + + o Demonstrate code that are FAQs on the newsgroup forum. + (such as how to use threads, callbacks, etc) + + o Example code should be short, but not at the expense of clarity. + + o Where possible, examples should emphasize FLTK's simplicity. + + +NEW SUBMISSIONS: RECOMMENDED PRACTICES + + These programs must follow FLTK coding style as defined in the FLTK + "CMP" (Configuration Management Plan/Coding Standards). + + Example code should have the above goals in mind. The best examples + are those that are as short and clear as possible; terse, but not + at the expense of clarity. + + To avoid cluttering up the top level directory with ancillary files + (such as image files or icons), examples that depend on more than + just a .cxx/.h file pair should have their own subdirectory. + + Data files common to several examples should be located in the + examples/data directory. + + Ancillary data files should be as small as possible, to keep the + distribution tar files small. Avoid high resolution images or + uncompressed images when possible. + + Examples that need large data sets (HD images, etc) should not + be part of the FLTK distribution; they can be provided as separate + packages (eg. as articles or external links). + + Some widgets have multiple capabilities that are best demonstrated + separately. For instance, the table widget can be used as a + custom data table, or as a spreadsheet, or as a widget container. + So separate examples for each would be e.g. "table-custom-data.cxx", + "table-spreadsheet.cxx", "table-widget-container.cxx", etc. + + Example programs should contain comments that help understand the + concepts shown, but not so verbose as to dwarf the code or make + the code hard to read. Within code, it's best to use single line + comments to emphasize code that might be unclear. Let the code + speak as much as possible. + + Examples programs may be referred to from the documentation + as good examples on how to do particular programming techniques. + + +NAMING CONVENTIONS + + Example programs that demonstrate a particular widget should start + with that widget's name in lowercase, eg. "table.cxx" for Fl_Table. + + Demonstrations of a particular technique should start with + "howto-xxx.cxx" to avoid naming conflicts with future widgets + of the same name. + + xxx-simple.cxx -- Simplest possible example of widget xxx + eg. table-simple.cxx + + xxx-<technique>.cxx -- A particular "technique" using widget xxx + eg. "table-spreadsheet.cxx" + + howto-<technique>.cxx -- Demonstrate a particular technique, eg. "howto-threading.cxx" + + Some example programs may depend on multiple files. To avoid + cluttering up the top level examples directory, such examples will + have their own subdirectory with the files they depend on localized + to that directory. + + Example programs should be as small as possible, to keep the + distribution tar files small. + + Very large examples, or examples that depend on large data sets + should be submitted as separate articles on the FLTK site, or as + external links on the FLTK site's 'links' page. + + +HISTORY + + Previous to FLTK 1.3.0, the fltk/test directory served the dual + purpose of containing test suites as well as example code. + + But the fltk/test programs started becoming necessarily complex, + testing for obscure problems, and not necessarily good demos for + applications programmers. + + The fltk/examples directory was created in FLTK 1.3.0 to separate + 'good programming examples' from the test suite code. + + +DISCLAIMER + + The examples in this directory are provided 'as-is', without any express + or implied warranty. In no event will the authors be held liable for + any damages arising from the use of this software. + + +BUGS + + If you find a bug, please report it through the fltk STR form at + http://fltk.org/str.php diff --git a/examples/howto-add_fd-and-popen.cxx b/examples/howto-add_fd-and-popen.cxx new file mode 100644 index 0000000..aa7667d --- /dev/null +++ b/examples/howto-add_fd-and-popen.cxx @@ -0,0 +1,72 @@ +// +// "$Id: howto-add_fd-and-popen.cxx 7629 2010-06-06 21:24:06Z greg.ercolano $" +// +// How to use popen() and Fl::add_fd() - erco 10/04/04 +// Originally from erco's cheat sheet, permission by author. +// +// Shows how the interface can remain "alive" while external +// command is running and outputing occassional data. For instance, +// while the command is running, keyboard navigation works, +// text can be highlighted, and the interface can be resized. +// +// Copyright 1998-2010 by Bill Spitzak and others. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// +#include <stdio.h> +#include <unistd.h> +#include <FL/Fl.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_Multi_Browser.H> + +#define PING_CMD "ping -i 2 -c 10 localhost" // 'slow command' under unix + +// GLOBALS +FILE *G_fp = NULL; + +// Handler for add_fd() -- called whenever the ping command outputs a new line of data +void HandleFD(int fd, void *data) { + Fl_Multi_Browser *brow = (Fl_Multi_Browser*)data; + char s[1024]; + if ( fgets(s, 1023, G_fp) == NULL ) { // read the line of data + Fl::remove_fd(fileno(G_fp)); // command ended? disconnect callback + pclose(G_fp); // close the descriptor + brow->add(""); brow->add("<<DONE>>"); // append msg indicating command finished + return; + } + brow->add(s); // line of data read? append to widget +} + +int main(int argc, char *argv[]) { + Fl_Window win(600,600); + Fl_Multi_Browser brow(10,10,580,580); + if ( ( G_fp = popen(PING_CMD, "r") ) == NULL ) { // start the external unix command + perror("popen failed"); + return(1); + } + Fl::add_fd(fileno(G_fp), HandleFD, (void*)&brow); // setup a callback for the popen()ed descriptor + win.resizable(brow); + win.show(argc, argv); + return(Fl::run()); +} + +// +// End of "$Id: howto-add_fd-and-popen.cxx 7629 2010-06-06 21:24:06Z greg.ercolano $". +// diff --git a/examples/tabs-simple.cxx b/examples/tabs-simple.cxx new file mode 100644 index 0000000..d858440 --- /dev/null +++ b/examples/tabs-simple.cxx @@ -0,0 +1,91 @@ +// +// "$Id: tabs-simple.cxx 7629 2010-06-06 21:24:06Z greg.ercolano $" +// +// Simple Fl_Tabs widget example. +// Originally from erco's cheat sheet 06/05/2010, permission by author. +// +// Copyright 1998-2010 by Bill Spitzak and others. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// +#include <FL/Fl.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_Tabs.H> +#include <FL/Fl_Group.H> +#include <FL/Fl_Button.H> +// +// Simple tabs example +// _____ _____ +// __/ Aaa \/ Bbb \______________________ +// | _______ | +// | |_______| | +// | _______ | +// | |_______| | +// | _______ | +// | |_______| | +// |______________________________________| +// +int main(int argc, char *argv[]) { + Fl::scheme("gtk+"); + Fl_Window *win = new Fl_Window(500,200,"Tabs Example"); + { + // Create the tab widget + Fl_Tabs *tabs = new Fl_Tabs(10,10,500-20,200-20); + { + // ADD THE "Aaa" TAB + // We do this by adding a child group to the tab widget. + // The child group's label defined the label of the tab. + // + Fl_Group *aaa = new Fl_Group(10,35,500-20,200-45,"Aaa"); + { + // Put some different buttons into the group, which will be shown + // when the tab is selected. + Fl_Button *b1 = new Fl_Button(50, 60,90,25,"Button A1"); b1->color(88+1); + Fl_Button *b2 = new Fl_Button(50, 90,90,25,"Button A2"); b2->color(88+2); + Fl_Button *b3 = new Fl_Button(50,120,90,25,"Button A3"); b3->color(88+3); + } + aaa->end(); + + // ADD THE "Bbb" TAB + // Same details as above. + // + Fl_Group *bbb = new Fl_Group(10,35,500-10,200-35,"Bbb"); + { + // Put some different buttons into the group, which will be shown + // when the tab is selected. + Fl_Button *b1 = new Fl_Button( 50,60,90,25,"Button B1"); b1->color(88+1); + Fl_Button *b2 = new Fl_Button(150,60,90,25,"Button B2"); b2->color(88+3); + Fl_Button *b3 = new Fl_Button(250,60,90,25,"Button B3"); b3->color(88+5); + Fl_Button *b4 = new Fl_Button( 50,90,90,25,"Button B4"); b4->color(88+2); + Fl_Button *b5 = new Fl_Button(150,90,90,25,"Button B5"); b5->color(88+4); + Fl_Button *b6 = new Fl_Button(250,90,90,25,"Button B6"); b6->color(88+6); + } + bbb->end(); + } + tabs->end(); + } + win->end(); + win->show(argc, argv); + return(Fl::run()); +} + +// +// End of "$Id: tabs-simple.cxx 7629 2010-06-06 21:24:06Z greg.ercolano $". +// diff --git a/examples/tree-simple.cxx b/examples/tree-simple.cxx new file mode 100644 index 0000000..5dfd2f3 --- /dev/null +++ b/examples/tree-simple.cxx @@ -0,0 +1,71 @@ +// +// "$Id: tree-simple.cxx 7628 2010-06-06 20:28:37Z greg.ercolano $" +// +// Simple Fl_Tree widget example. - erco 06/05/2010 +// +// Copyright 1998-2010 by Bill Spitzak and others. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// +#include <stdio.h> +#include <FL/Fl.H> +#include <FL/Fl_Double_Window.H> +#include <FL/Fl_Tree.H> + +// Tree's callback +// Invoked whenever someone clicks an item. +// +void TreeCallback(Fl_Widget *w, void *data) { + Fl_Tree *tree = (Fl_Tree*)w; + Fl_Tree_Item *item = (Fl_Tree_Item*)tree->item_clicked(); + fprintf(stderr, "TreeCallback: item clicked='%s'\n", (item)?item->label():"???"); +} + +int main(int argc, char *argv[]) { + Fl::scheme("gtk+"); + Fl_Double_Window *win = new Fl_Double_Window(250, 400, "Simple Tree"); + win->begin(); + { + // Create the tree + Fl_Tree *tree = new Fl_Tree(10, 10, win->w()-20, win->h()-20); + tree->showroot(0); // don't show root of tree + tree->callback(TreeCallback); // setup a callback for the tree + + // Add some items + tree->add("Flintstones/Fred"); + tree->add("Flintstones/Wilma"); + tree->add("Flintstones/Pebbles"); + tree->add("Simpsons/Homer"); + tree->add("Simpsons/Marge"); + tree->add("Simpsons/Bart"); + tree->add("Simpsons/Lisa"); + + // Start with one of the items closed + tree->close("Simpsons"); + } + win->end(); + win->resizable(win); + win->show(argc, argv); + return(Fl::run()); +} + +// +// End of "$Id: tree-simple.cxx 7628 2010-06-06 20:28:37Z greg.ercolano $". +// diff --git a/examples/wizard-simple.cxx b/examples/wizard-simple.cxx new file mode 100644 index 0000000..05e2124 --- /dev/null +++ b/examples/wizard-simple.cxx @@ -0,0 +1,89 @@ +// +// "$Id: wizard-simple.cxx 7631 2010-06-06 21:43:59Z greg.ercolano $" +// +// Simple Fl_Wizard widget example. +// Originally from erco's cheat sheet 06/05/2010, permission by author. +// +// Copyright 1998-2010 by Bill Spitzak and others. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// +#include <stdlib.h> +#include <FL/Fl.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_Group.H> +#include <FL/Fl_Wizard.H> +#include <FL/Fl_Button.H> +#include <FL/Fl_Multiline_Output.H> +// +// Simple 'wizard' using fltk's new Fl_Wizard widget +// +Fl_Window *G_win = 0; +Fl_Wizard *G_wiz = 0; + +void back_cb(Fl_Widget*,void*) { G_wiz->prev(); } +void next_cb(Fl_Widget*,void*) { G_wiz->next(); } +void done_cb(Fl_Widget*,void*) { exit(0); } + +int main(int argc, char **argv) { + G_win = new Fl_Window(400,300,"Example Wizard"); + G_wiz = new Fl_Wizard(0,0,400,300); + + // Wizard: page 1 + { + Fl_Group *g = new Fl_Group(0,0,400,300); + Fl_Button *next = new Fl_Button(290,265,100,25,"Next @->"); next->callback(next_cb); + Fl_Multiline_Output *out = new Fl_Multiline_Output(10,30,400-20,300-80,"Welcome"); + out->labelsize(20); + out->align(FL_ALIGN_TOP|FL_ALIGN_LEFT); + out->value("This is First page"); + g->end(); + } + // Wizard: page 2 + { + Fl_Group *g = new Fl_Group(0,0,400,300); + Fl_Button *next = new Fl_Button(290,265,100,25,"Next @->"); next->callback(next_cb); + Fl_Button *back = new Fl_Button(180,265,100,25,"@<- Back"); back->callback(back_cb); + Fl_Multiline_Output *out = new Fl_Multiline_Output(10,30,400-20,300-80,"Terms And Conditions"); + out->labelsize(20); + out->align(FL_ALIGN_TOP|FL_ALIGN_LEFT); + out->value("This is the Second page"); + g->end(); + } + // Wizard: page 3 + { + Fl_Group *g = new Fl_Group(0,0,400,300); + Fl_Button *done = new Fl_Button(290,265,100,25,"Finish"); done->callback(done_cb); + Fl_Button *back = new Fl_Button(180,265,100,25,"@<- Back"); back->callback(back_cb); + Fl_Multiline_Output *out = new Fl_Multiline_Output(10,30,400-20,300-80,"Finish"); + out->labelsize(20); + out->align(FL_ALIGN_TOP|FL_ALIGN_LEFT); + out->value("This is the Last page"); + g->end(); + } + G_wiz->end(); + G_win->end(); + G_win->show(argc, argv); + return Fl::run(); +} + +// +// End of "$Id: wizard-simple.cxx 7631 2010-06-06 21:43:59Z greg.ercolano $". +// |