fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

template_panel.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id$"
00003 //
00004 // FLUID template support for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 // generated by Fast Light User Interface Designer (fluid) version 1.0300
00029 
00030 #include "template_panel.h"
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include "../src/flstring.h"
00034 #include <errno.h>
00035 #include <FL/filename.H>
00036 #include <FL/fl_ask.H>
00037 #include <FL/Fl_Shared_Image.H>
00038 #include <FL/Fl_Preferences.H>
00039 #if defined(WIN32) && !defined(__CYGWIN__)
00040 #include <io.h>
00041 #else
00042 #include <unistd.h>
00043 #endif // WIN32 && !__CYGWIN__
00044 extern Fl_Preferences fluid_prefs;
00045 
00046 Fl_Double_Window *template_panel=(Fl_Double_Window *)0;
00047 
00048 static void cb_template_panel(Fl_Double_Window*, void*) {
00049   Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
00050 if (img) img->release();
00051 template_preview->image(0);
00052 
00053 template_browser->deselect();
00054 template_name->value("");
00055 template_instance->value("");
00056 template_panel->hide();
00057 }
00058 
00059 Fl_Browser *template_browser=(Fl_Browser *)0;
00060 
00061 static void cb_template_browser(Fl_Browser*, void*) {
00062   if (Fl::event_clicks()) {
00063   template_panel->hide();
00064   return;
00065 }
00066 Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
00067 if (img) img->release();
00068 template_preview->image(0);
00069 template_preview->redraw();
00070 
00071 int item = template_browser->value();
00072 
00073 if (item <= 1) template_instance->deactivate();
00074 else template_instance->activate();
00075 
00076 if (item < 1) {
00077   template_submit->deactivate();
00078   template_delete->deactivate();
00079   return;
00080 }
00081 
00082 template_submit->activate();
00083 
00084 const char *flfile = (const char *)template_browser->data(item);
00085 if (!flfile) {
00086   template_delete->deactivate();
00087   return;
00088 }
00089 
00090 template_name->value(template_browser->text(item));
00091 
00092 template_delete->activate();
00093 
00094 char pngfile[1024], *ext;
00095 
00096 strlcpy(pngfile, flfile, sizeof(pngfile));
00097 if ((ext = strrchr(pngfile, '.')) == NULL) return;
00098 strcpy(ext, ".png");
00099 
00100 img = Fl_Shared_Image::get(pngfile);
00101 
00102 if (img) {
00103   template_preview->image(img);
00104   template_preview->redraw();
00105 };
00106 }
00107 
00108 Fl_Box *template_preview=(Fl_Box *)0;
00109 
00110 Fl_Input *template_name=(Fl_Input *)0;
00111 
00112 static void cb_template_name(Fl_Input*, void*) {
00113   if (strlen(template_name->value())) {
00114   template_submit->activate();
00115   if (Fl::event_key() == FL_Enter) template_panel->hide();
00116 } else template_submit->deactivate();
00117 }
00118 
00119 Fl_Input *template_instance=(Fl_Input *)0;
00120 
00121 Fl_Button *template_delete=(Fl_Button *)0;
00122 
00123 static void cb_Cancel(Fl_Button*, void*) {
00124   Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
00125 if (img) img->release();
00126 template_preview->image(0);
00127 
00128 template_browser->deselect();
00129 template_name->value("");
00130 template_instance->value("");
00131 template_panel->hide();
00132 }
00133 
00134 Fl_Return_Button *template_submit=(Fl_Return_Button *)0;
00135 
00136 static void cb_template_submit(Fl_Return_Button*, void*) {
00137   Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
00138 if (img) img->release();
00139 template_preview->image(0);
00140 
00141 template_panel->hide();
00142 }
00143 
00144 Fl_Double_Window* make_template_panel() {
00145   { template_panel = new Fl_Double_Window(460, 355, "New/Save Template");
00146     template_panel->callback((Fl_Callback*)cb_template_panel);
00147     { template_browser = new Fl_Browser(10, 28, 180, 250, "Available Templates:");
00148       template_browser->type(2);
00149       template_browser->labelfont(1);
00150       template_browser->callback((Fl_Callback*)cb_template_browser);
00151       template_browser->align(Fl_Align(FL_ALIGN_TOP_LEFT));
00152       template_browser->when(3);
00153     } // Fl_Browser* template_browser
00154     { template_preview = new Fl_Box(200, 28, 250, 250);
00155       template_preview->box(FL_THIN_DOWN_BOX);
00156       template_preview->align(Fl_Align(69|FL_ALIGN_INSIDE));
00157       Fl_Group::current()->resizable(template_preview);
00158     } // Fl_Box* template_preview
00159     { template_name = new Fl_Input(124, 288, 326, 25, "Template Name:");
00160       template_name->labelfont(1);
00161       template_name->textfont(4);
00162       template_name->callback((Fl_Callback*)cb_template_name);
00163       template_name->when(3);
00164     } // Fl_Input* template_name
00165     { template_instance = new Fl_Input(124, 288, 326, 25, "Instance Name:");
00166       template_instance->labelfont(1);
00167       template_instance->textfont(4);
00168       template_instance->hide();
00169     } // Fl_Input* template_instance
00170     { Fl_Group* o = new Fl_Group(10, 323, 440, 25);
00171       { template_delete = new Fl_Button(10, 323, 133, 25, "Delete Template");
00172         template_delete->callback((Fl_Callback*)template_delete_cb);
00173       } // Fl_Button* template_delete
00174       { Fl_Box* o = new Fl_Box(153, 323, 126, 25);
00175         Fl_Group::current()->resizable(o);
00176       } // Fl_Box* o
00177       { Fl_Button* o = new Fl_Button(289, 323, 72, 25, "Cancel");
00178         o->callback((Fl_Callback*)cb_Cancel);
00179       } // Fl_Button* o
00180       { template_submit = new Fl_Return_Button(371, 323, 79, 25, "Save");
00181         template_submit->callback((Fl_Callback*)cb_template_submit);
00182       } // Fl_Return_Button* template_submit
00183       o->end();
00184     } // Fl_Group* o
00185     template_panel->set_modal();
00186     template_panel->end();
00187   } // Fl_Double_Window* template_panel
00188   return template_panel;
00189 }
00190 
00191 void template_clear() {
00192   int i;
00193   void *filename;
00194   
00195   for (i = 1; i <= template_browser->size(); i ++) {
00196     if ((filename = template_browser->data(i)) != NULL) free(filename);
00197   }
00198   
00199   template_browser->deselect();
00200   template_browser->clear();
00201 }
00202 
00203 void template_delete_cb(Fl_Button *, void *) {
00204   int item = template_browser->value();
00205   if (item < 1) return;
00206   
00207   const char *name = template_browser->text(item);
00208   const char *flfile = (const char *)template_browser->data(item);
00209   if (!flfile) return;
00210   
00211   if (!fl_choice("Are you sure you want to delete the template \"%s\"?",
00212                  "Cancel", "Delete", 0, name)) return;
00213   
00214   if (unlink(flfile)) {
00215     fl_alert("Unable to delete template \"%s\":\n%s", name, strerror(errno));
00216     return;
00217   }
00218   
00219   template_browser->remove(item);
00220   template_browser->do_callback();
00221 }
00222 
00223 void template_load() {
00224   int i;
00225   char name[1024], filename[1024], path[1024], *ptr;
00226   struct dirent **files;
00227   int num_files;
00228   
00229   fluid_prefs.getUserdataPath(path, sizeof(path));
00230   strlcat(path, "templates", sizeof(path));
00231   
00232   num_files = fl_filename_list(path, &files);
00233   
00234   for (i = 0; i < num_files; i ++) {
00235     if (fl_filename_match(files[i]->d_name, "*.fl")) {
00236       // Format the name as the filename with "_" replaced with " "
00237       // and without the trailing ".fl"...
00238       strlcpy(name, files[i]->d_name, sizeof(name));
00239       *strstr(name, ".fl") = '\0';
00240   
00241       for (ptr = name; *ptr; ptr ++) {
00242         if (*ptr == '_') *ptr = ' ';
00243       }
00244   
00245       // Add the template to the browser...
00246       snprintf(filename, sizeof(filename), "%s/%s", path, files[i]->d_name);
00247       template_browser->add(name, strdup(filename));
00248     }
00249   
00250     free(files[i]);
00251   }
00252   
00253   if (num_files > 0) free(files);
00254 }
00255 
00256 //
00257 // End of "$Id$".
00258 //