From 911ab3948abb7c6cb3229ff3bc30ff6756bb0c93 Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Mon, 16 Dec 2013 10:26:44 +0000 Subject: Added Fl_Tree sort example. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10035 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- examples/Makefile | 1 + examples/tree-custom-sort.cxx | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 examples/tree-custom-sort.cxx (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile index a9a4f6175..1320c3755 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -25,6 +25,7 @@ ALL = howto-add_fd-and-popen$(EXEEXT) \ tree-simple$(EXEEXT) \ tree-as-container$(EXEEXT) \ tree-custom-draw-items$(EXEEXT) \ + tree-custom-sort$(EXEEXT) \ tree-of-tables$(EXEEXT) \ wizard-simple$(EXEEXT) diff --git a/examples/tree-custom-sort.cxx b/examples/tree-custom-sort.cxx new file mode 100644 index 000000000..e7bc056dd --- /dev/null +++ b/examples/tree-custom-sort.cxx @@ -0,0 +1,82 @@ +// +// "$Id$" +// +// Simple Fl_Tree custom (numeric) sort example. - erco 12/16/2013 +// Demonstrates custom sorting of Fl_Tree items. +// +// Copyright 2013 Greg Ercolano. +// Copyright 1998-2010 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: +// +// http://www.fltk.org/COPYING.php +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// +#include +#include /* qsort(3), srand(3).. */ +#include /* time(2) */ +#include +#include +#include +#include + +Fl_Tree *G_tree = 0; + +// Resort the tree +void MySortCallback(Fl_Widget*, void *data) { + int dir = int(long(data)); // forward or reverse + Fl_Tree_Item *i = G_tree->root(); + // Bubble sort + for ( int ax=0; axchildren(); ax++ ) { + for ( int bx=ax+1; bxchildren(); bx++ ) { + long a; sscanf(i->child(ax)->label(), "%ld", &a); + long b; sscanf(i->child(bx)->label(), "%ld", &b); + switch ( dir ) { + case 1: if ( a > b ) { i->swap_children(ax, bx); } break; // fwd + case -1: if ( a < b ) { i->swap_children(ax, bx); } break; // rev + } + } + } + G_tree->redraw(); +} + +int main(int argc, char *argv[]) { + // Randomize the random number generator + time_t tval; time(&tval); + srand((unsigned)tval); + + // Create window with tree + Fl::scheme("gtk+"); + Fl_Double_Window *win = new Fl_Double_Window(250, 600, "Numeric Sort Tree"); + win->begin(); + { + G_tree = new Fl_Tree(10, 10, win->w()-20, win->h()-60); + G_tree->showroot(0); + + // Add 200 random numbers to the tree + Fl_Tree_Item *item; + char word[50]; + for ( int t=0; t<200; t++ ) { + sprintf(word, "%ld", long((float(rand()) / RAND_MAX) * 1000000)); + item = G_tree->add(word); + } + + // Add some sort buttons + Fl_Button *but; + but = new Fl_Button(10, win->h()-40,80,20,"Fwd"); but->callback(MySortCallback, (void*) 1); + but = new Fl_Button(20+80,win->h()-40,80,20,"Rev"); but->callback(MySortCallback, (void*)-1); + } + win->end(); + win->resizable(win); + win->show(argc, argv); + return(Fl::run()); +} + +// +// End of "$Id$". +// -- cgit v1.2.3