Template panel for "new" and "save template" actions, complete with

preview images that are generated when you save the template.

Use @INSTANCE@ anywhere you want to substitute an instance name.


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4148 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Michael R Sweet 2005-03-20 21:27:20 +00:00
parent 0bc98fd29e
commit 0919d57c33
10 changed files with 761 additions and 48 deletions

View File

@ -2,6 +2,7 @@ CHANGES IN FLTK 1.1.7
- Documentation fixes (STR #648, STR #692, STR #730, STR
#744, STR #745)
- FLUID now provides support for UI templates.
- fl_not_clipped() incorrectly used the current window
dimensions for gross clipping, which interfered with
off-screen rendering.

View File

@ -561,6 +561,8 @@ public:
Fl_Widget_Type::ideal_size(w, h);
w += 2 * ((o->labelsize() - 3) & ~1) + o->labelsize() - 4;
h = (h / 5) * 5;
if (h < 15) h = 15;
if (w < (15 + h)) w = 15 + h;
}
virtual const char *type_name() {return "Fl_Menu_Button";}
Fl_Widget *widget(int X,int Y,int W,int H) {
@ -582,6 +584,8 @@ public:
if (w1 < 1) w1 = 1;
w += 2 * w1 + o->labelsize() - 4;
h = (h / 5) * 5;
if (h < 15) h = 15;
if (w < (15 + h)) w = 15 + h;
}
virtual const char *type_name() {return "Fl_Choice";}
Fl_Widget *widget(int X,int Y,int W,int H) {
@ -614,6 +618,8 @@ public:
w = o->w() - 20 - Fl::box_dw(o->box());
int ww = (int)fl_width('m');
w = ((w + ww - 1) / ww) * ww + 20 + Fl::box_dw(o->box());
if (h < 15) h = 15;
if (w < (15 + h)) w = 15 + h;
}
virtual const char *type_name() {return "Fl_Input_Choice";}
Fl_Widget *widget(int X,int Y,int W,int H) {

View File

@ -663,13 +663,15 @@ void Fl_Window_Type::draw_overlay() {
newposition(myo,x,y,r,t);
if (!show_guides || !drag || numselected != 1) fl_rect(x,y,r-x,t-y);
if (!(myo->o->align() & FL_ALIGN_INSIDE)) {
// Adjust top/bottom for top/bottom labels...
// Adjust left/right/top/bottom for top/bottom labels...
int ww, hh;
ww = myo->o->w();
ww = (myo->o->align() & FL_ALIGN_WRAP) ? myo->o->w() : 0;
hh = myo->o->labelsize();
myo->o->measure_label(ww, hh);
if (myo->o->align() & FL_ALIGN_TOP) y -= hh;
if (myo->o->align() & FL_ALIGN_BOTTOM) t += hh;
else if (myo->o->align() & FL_ALIGN_BOTTOM) t += hh;
else if (myo->o->align() & FL_ALIGN_LEFT) x -= ww + 4;
else if (myo->o->align() & FL_ALIGN_RIGHT) r += ww + 4;
}
if (x < mybx) mybx = x;
if (y < myby) myby = y;
@ -749,8 +751,8 @@ void Fl_Window_Type::draw_overlay() {
}
// Draw height guide
draw_height(mybx < 50 ? mybx+10 : mybx-10, y, t,
mybx < 50 ? FL_ALIGN_RIGHT : FL_ALIGN_LEFT);
draw_height(x < 50 ? x+10 : x-10, y, t,
x < 50 ? FL_ALIGN_RIGHT : FL_ALIGN_LEFT);
}
if (drag & (LEFT | RIGHT)) {
@ -759,15 +761,17 @@ void Fl_Window_Type::draw_overlay() {
// Resize width
if (drag & LEFT) {
mybx -= d;
x -= d;
dx -= d;
} else {
mybr += d;
r += d;
dx += d;
}
}
// Draw width guide
draw_width(mybx, myby < 50 ? y+10 : y-10, mybr,
draw_width(x, y < 50 ? y+10 : y-10, r,
y < 50 ? FL_ALIGN_BOTTOM : FL_ALIGN_TOP);
}
}

View File

@ -39,6 +39,7 @@ CPPFILES = \
file.cxx \
fluid.cxx \
function_panel.cxx \
template_panel.cxx \
undo.cxx \
widget_panel.cxx

View File

@ -217,6 +217,8 @@ public:
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
h = ((h + fl_height() - 1) / fl_height()) * fl_height() +
Fl::box_dh(o->box());
if (h < 30) h = 30;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_Browser";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -262,6 +264,8 @@ public:
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
h = ((h + fl_height() - 1) / fl_height()) * fl_height() +
Fl::box_dh(o->box());
if (h < 30) h = 30;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_Check_Browser";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -307,6 +311,8 @@ public:
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
h = ((h + fl_height() - 1) / fl_height()) * fl_height() +
Fl::box_dh(o->box());
if (h < 30) h = 30;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_File_Browser";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -388,6 +394,8 @@ public:
w -= Fl::box_dw(o->box());
int ww = (int)fl_width('m');
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
if (h < 15) h = 15;
if (w < 15) w = 15;
}
virtual const char *type_name() {return "Fl_Input";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -426,6 +434,8 @@ public:
w -= Fl::box_dw(o->box());
int ww = (int)fl_width('m');
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
if (h < 20) h = 20;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_File_Input";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -465,6 +475,8 @@ public:
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
h = ((h + fl_height() - 1) / fl_height()) * fl_height() +
Fl::box_dh(o->box());
if (h < 30) h = 30;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_Text_Display";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -503,6 +515,8 @@ public:
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
h = ((h + fl_height() - 1) / fl_height()) * fl_height() +
Fl::box_dh(o->box());
if (h < 30) h = 30;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_Text_Editor";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -553,6 +567,8 @@ public:
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
h = ((h + fl_height() - 1) / fl_height()) * fl_height() +
Fl::box_dh(o->box());
if (h < 30) h = 30;
if (w < 50) w = 50;
}
virtual const char *type_name() {return "Fl_Help_View";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -688,6 +704,8 @@ public:
w -= Fl::box_dw(o->box());
int ww = (int)fl_width('m');
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
if (h < 15) h = 15;
if (w < 15) w = 15;
}
virtual const char *type_name() {return "Fl_Output";}
Fl_Widget *widget(int x,int y,int w,int h) {
@ -712,6 +730,8 @@ public:
w -= Fl::box_dw(o->box());
int ww = (int)fl_width('m');
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
if (h < 15) h = 15;
if (w < 15) w = 15;
}
virtual const char *type_name() {return "Fl_Value_Input";}
int textstuff(int w, Fl_Font& f, int& s, Fl_Color& c);
@ -749,6 +769,8 @@ public:
w = o->w() - Fl::box_dw(o->box());
int ww = (int)fl_width('m');
w = ((w + ww - 1) / ww) * ww + Fl::box_dw(o->box());
if (h < 15) h = 15;
if (w < 15) w = 15;
}
virtual const char *type_name() {return "Fl_Value_Output";}
int textstuff(int w, Fl_Font& f, int& s, Fl_Color& c);

View File

@ -45,6 +45,7 @@
#include "../src/flstring.h"
#include "alignment_panel.h"
#include "function_panel.h"
#include "template_panel.h"
#if defined(WIN32) && !defined(__CYGWIN__)
# include <direct.h>
@ -154,6 +155,21 @@ void save_position(Fl_Window *w, const char *prefsName) {
Fl_Window *main_window;
Fl_Menu_Bar *main_menubar;
static char* cutfname(int which = 0) {
static char name[2][1024];
static char beenhere = 0;
if (!beenhere) {
beenhere = 1;
fluid_prefs.getUserdataPath(name[0], sizeof(name[0]));
strlcat(name[0], "cut_buffer", sizeof(name[0]));
fluid_prefs.getUserdataPath(name[1], sizeof(name[1]));
strlcat(name[1], "dup_buffer", sizeof(name[1]));
}
return name[which];
}
void save_cb(Fl_Widget *, void *v) {
const char *c = filename;
if (v || !c || !*c) {
@ -189,9 +205,42 @@ void save_cb(Fl_Widget *, void *v) {
}
void save_template_cb(Fl_Widget *, void *) {
const char *c = fl_input("Template Name:");
if (!c) return;
// Setup the template panel...
if (!template_panel) make_template_panel();
template_clear();
template_browser->add("New Template");
template_load();
template_name->show();
template_name->value("");
template_instance->hide();
template_delete->show();
template_delete->deactivate();
template_submit->label("Save");
template_submit->deactivate();
template_panel->label("Save Template");
// Show the panel and wait for the user to do something...
template_panel->show();
while (template_panel->shown()) Fl::wait();
// Get the template name, return if it is empty...
const char *c = template_name->value();
if (!c || !*c) return;
// Convert template name to filename_with_underscores
char safename[1024], *safeptr;
strlcpy(safename, c, sizeof(safename));
for (safeptr = safename; *safeptr; safeptr ++) {
if (isspace(*safeptr)) *safeptr = '_';
}
// Find the templates directory...
char filename[1024];
fluid_prefs.getUserdataPath(filename, sizeof(filename));
@ -199,7 +248,7 @@ void save_template_cb(Fl_Widget *, void *) {
if (access(filename, 0)) mkdir(filename, 0777);
strlcat(filename, "/", sizeof(filename));
strlcat(filename, c, sizeof(filename));
strlcat(filename, safename, sizeof(filename));
char *ext = filename + strlen(filename);
if (ext >= (filename + sizeof(filename) - 5)) {
@ -207,10 +256,9 @@ void save_template_cb(Fl_Widget *, void *) {
return;
}
// Save the .fl file...
strcpy(ext, ".fl");
// printf("save_template_cb: template filename=\"%s\"\n", filename);
if (!access(filename, 0)) {
if (fl_choice("The template \"%s\" already exists.\n"
"Do you want to replace it?", "Cancel",
@ -240,13 +288,9 @@ void save_template_cb(Fl_Widget *, void *) {
if ((pixels = wt->read_image(w, h)) == NULL) return;
// printf("save_template_cb: pixels=%p, w=%d, h=%d...\n", pixels, w, h);
// Save to a PNG file...
strcpy(ext, ".png");
// printf("save_template_cb: screenshot filename=\"%s\"\n", filename);
FILE *fp;
if ((fp = fopen(filename, "wb")) == NULL) {
@ -290,10 +334,10 @@ void save_template_cb(Fl_Widget *, void *) {
void exit_cb(Fl_Widget *,void *) {
if (modflag)
switch (fl_choice("Do you want to save changes to this user\n"
"interface before exiting?", "Don't Save",
"Save", "Cancel"))
"interface before exiting?", "Cancel",
"Save", "Don't Save"))
{
case 2 : /* Cancel */
case 0 : /* Cancel */
return;
case 1 : /* Save */
save_cb(NULL, NULL);
@ -327,7 +371,7 @@ apple_open_cb(const char *c) {
"interface before opening another one?", "Don't Save",
"Save", "Cancel"))
{
case 2 : /* Cancel */
case 0 : /* Cancel */
return;
case 1 : /* Save */
save_cb(NULL, NULL);
@ -360,10 +404,10 @@ void open_cb(Fl_Widget *, void *v) {
if (!v && modflag)
{
switch (fl_choice("Do you want to save changes to this user\n"
"interface before opening another one?", "Don't Save",
"Save", "Cancel"))
"interface before opening another one?", "Cancel",
"Save", "Don't Save"))
{
case 2 : /* Cancel */
case 0 : /* Cancel */
return;
case 1 : /* Save */
save_cb(NULL, NULL);
@ -408,10 +452,10 @@ void open_history_cb(Fl_Widget *, void *v) {
if (modflag)
{
switch (fl_choice("Do you want to save changes to this user\n"
"interface before opening another one?", "Don't Save",
"Save", "Cancel"))
"interface before opening another one?", "Cancel",
"Save", "Don't Save"))
{
case 2 : /* Cancel */
case 0 : /* Cancel */
return;
case 1 : /* Save */
save_cb(NULL, NULL);
@ -438,21 +482,109 @@ void open_history_cb(Fl_Widget *, void *v) {
}
void new_cb(Fl_Widget *, void *v) {
// Check if the current file has been modified...
if (!v && modflag)
{
// Yes, ask the user what to do...
switch (fl_choice("Do you want to save changes to this user\n"
"interface before creating a new one?", "Don't Save",
"Save", "Cancel"))
"interface before creating a new one?", "Cancel",
"Save", "Don't Save"))
{
case 2 : /* Cancel */
case 0 : /* Cancel */
return;
case 1 : /* Save */
save_cb(NULL, NULL);
if (modflag) return; // Didn't save!
}
}
// Setup the template panel...
if (!template_panel) make_template_panel();
template_clear();
template_browser->add("Blank");
template_load();
template_name->hide();
template_name->value("");
template_instance->show();
template_instance->deactivate();
template_instance->value("");
template_delete->hide();
template_submit->label("New");
template_submit->deactivate();
template_panel->label("New");
// Show the panel and wait for the user to do something...
template_panel->show();
while (template_panel->shown()) Fl::wait();
// See if the user chose anything...
int item = template_browser->value();
if (item < 1) return;
// Clear the current data...
delete_all();
set_filename(NULL);
// Load the template, if any...
const char *tname = (const char *)template_browser->data(item);
if (tname) {
// Grab the instance name...
const char *iname = template_instance->value();
if (iname && *iname) {
// Copy the template to a temp file, then read it in...
char line[1024], *ptr, *next;
FILE *infile, *outfile;
if ((infile = fopen(tname, "r")) == NULL) {
fl_alert("Error reading template file \"%s\":\n%s", tname,
strerror(errno));
set_modflag(0);
undo_clear();
return;
}
if ((outfile = fopen(cutfname(1), "w")) == NULL) {
fl_alert("Error writing buffer file \"%s\":\n%s", cutfname(1),
strerror(errno));
fclose(infile);
set_modflag(0);
undo_clear();
return;
}
while (fgets(line, sizeof(line), infile)) {
// Replace @INSTANCE@ with the instance name...
for (ptr = line; (next = strstr(ptr, "@INSTANCE@")) != NULL; ptr = next + 10) {
fwrite(ptr, next - ptr, 1, outfile);
fputs(iname, outfile);
}
fputs(ptr, outfile);
}
fclose(infile);
fclose(outfile);
undo_suspend();
read_file(cutfname(1), 0);
unlink(cutfname(1));
undo_resume();
} else {
// No instance name, so read the template without replacements...
undo_suspend();
read_file(tname, 0);
undo_resume();
}
}
set_modflag(0);
undo_clear();
}
@ -550,21 +682,6 @@ void ungroup_cb(Fl_Widget *, void *);
extern int pasteoffset;
static int ipasteoffset;
static char* cutfname(int which = 0) {
static char name[2][1024];
static char beenhere = 0;
if (!beenhere) {
beenhere = 1;
fluid_prefs.getUserdataPath(name[0], sizeof(name[0]));
strlcat(name[0], "cut_buffer", sizeof(name[0]));
fluid_prefs.getUserdataPath(name[1], sizeof(name[1]));
strlcat(name[1], "dup_buffer", sizeof(name[1]));
}
return name[which];
}
void copy_cb(Fl_Widget*, void*) {
if (!Fl_Type::current) {
fl_beep();
@ -648,6 +765,7 @@ void duplicate_cb(Fl_Widget*, void*) {
if (!read_file(cutfname(1), 1)) {
fl_message("Can't read %s: %s", cutfname(1), strerror(errno));
}
unlink(cutfname(1));
undo_resume();
force_parent = 0;
@ -716,7 +834,7 @@ void toggle_widgetbin_cb(Fl_Widget *, void *);
Fl_Menu_Item Main_Menu[] = {
{"&File",0,0,0,FL_SUBMENU},
{"&New", FL_CTRL+'n', new_cb, 0},
{"&New...", FL_CTRL+'n', new_cb, 0},
{"&Open...", FL_CTRL+'o', open_cb, 0},
{"Open Pre&vious",0,0,0,FL_SUBMENU},
{relative_history[0], FL_CTRL+'0', open_history_cb, absolute_history[0]},

View File

@ -110,7 +110,7 @@ Fl_Widget_Type.o: ../FL/Fl_Text_Display.H ../FL/fl_show_colormap.H
Fl_Window_Type.o: ../FL/Fl.H ../FL/Enumerations.H ../FL/Fl_Export.H
Fl_Window_Type.o: ../FL/Fl_Overlay_Window.H ../FL/Fl_Double_Window.H
Fl_Window_Type.o: ../FL/Fl_Window.H ../FL/Fl_Group.H ../FL/Fl_Widget.H
Fl_Window_Type.o: ../FL/fl_message.H ../FL/fl_ask.H ../FL/fl_draw.H
Fl_Window_Type.o: ../FL/fl_message.H ../FL/fl_ask.H ../FL/fl_draw.H ../FL/x.H
Fl_Window_Type.o: ../FL/Fl_Menu_Item.H ../FL/Fl_Image.H Fl_Widget_Type.h
Fl_Window_Type.o: Fl_Type.h ../FL/Fl_Widget.H ../FL/Fl_Menu.H
Fl_Window_Type.o: ../FL/Fl_Menu_Item.H Fluid_Image.h ../FL/Fl_Shared_Image.H
@ -253,9 +253,10 @@ fluid.o: alignment_panel.h ../FL/Fl_Text_Buffer.H ../FL/Fl_Text_Display.H
fluid.o: ../FL/Fl_Text_Buffer.H ../FL/Fl_Tooltip.H ../FL/Fl_Widget.H
fluid.o: ../FL/Fl_Tabs.H ../FL/Fl_Light_Button.H function_panel.h
fluid.o: ../FL/Fl_Window.H ../FL/Fl_Text_Editor.H ../FL/Fl_Text_Display.H
fluid.o: about_panel.h undo.h Fl_Type.h ../FL/Fl_Menu.H Fluid_Image.h
fluid.o: ../FL/Fl_Shared_Image.H ../FL/Fl_Pack.H ../FL/Fl_Wizard.H
fluid.o: ../FL/Fl_Menu_.H ../FL/Fl_Input_Choice.H
fluid.o: template_panel.h ../FL/Fl_Browser.H about_panel.h undo.h Fl_Type.h
fluid.o: ../FL/Fl_Menu.H Fluid_Image.h ../FL/Fl_Shared_Image.H
fluid.o: ../FL/Fl_Pack.H ../FL/Fl_Wizard.H ../FL/Fl_Menu_.H
fluid.o: ../FL/Fl_Input_Choice.H
function_panel.o: function_panel.h ../FL/Fl.H ../FL/Enumerations.H
function_panel.o: ../FL/Fl_Export.H ../FL/Fl_Window.H ../FL/Fl_Light_Button.H
function_panel.o: ../FL/Fl_Input.H ../FL/Fl_Input_.H ../FL/Fl_Return_Button.H
@ -270,6 +271,16 @@ function_panel.o: ../FL/Fl_Shared_Image.H ../FL/fl_draw.H ../FL/Fl_Tabs.H
function_panel.o: ../FL/Fl_Pack.H ../FL/Fl_Wizard.H ../FL/Fl_Menu_.H
function_panel.o: ../FL/Fl_Choice.H ../FL/Fl_Input_Choice.H
function_panel.o: ../FL/Fl_Menu_Bar.H undo.h
template_panel.o: template_panel.h ../FL/Fl.H ../FL/Enumerations.H
template_panel.o: ../FL/Fl_Export.H ../FL/Fl_Double_Window.H
template_panel.o: ../FL/Fl_Window.H ../FL/Fl_Group.H ../FL/Fl_Widget.H
template_panel.o: ../FL/Fl_Browser.H ../FL/Fl_Browser_.H ../FL/Fl_Scrollbar.H
template_panel.o: ../FL/Fl_Slider.H ../FL/Fl_Valuator.H ../FL/Fl_Box.H
template_panel.o: ../FL/Fl_Input.H ../FL/Fl_Input_.H ../FL/Fl_Button.H
template_panel.o: ../FL/Fl_Return_Button.H ../FL/Fl_Button.H
template_panel.o: ../src/flstring.h ../FL/Fl_Export.H ../config.h
template_panel.o: ../FL/filename.H ../FL/fl_ask.H ../FL/Fl_Shared_Image.H
template_panel.o: ../FL/Fl_Image.H ../FL/Fl_Preferences.H
undo.o: ../FL/Fl.H ../FL/Enumerations.H ../FL/Fl_Export.H Fl_Type.h
undo.o: ../FL/Fl_Widget.H ../FL/Fl_Menu.H ../FL/Fl_Menu_Item.H
undo.o: ../FL/Fl_Widget.H ../FL/Fl_Image.H Fluid_Image.h

253
fluid/template_panel.cxx Normal file
View File

@ -0,0 +1,253 @@
//
// "$Id$"
//
// FLUID template support for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2005 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
//
// generated by Fast Light User Interface Designer (fluid) version 1.0107
#include "template_panel.h"
#include <stdio.h>
#include <stdlib.h>
#include "../src/flstring.h"
#include <errno.h>
#include <FL/filename.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Preferences.H>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif // WIN32
extern Fl_Preferences fluid_prefs;
Fl_Double_Window *template_panel=(Fl_Double_Window *)0;
Fl_Browser *template_browser=(Fl_Browser *)0;
static void cb_template_browser(Fl_Browser*, void*) {
if (Fl::event_clicks()) {
template_panel->hide();
return;
}
Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
if (img) img->release();
template_preview->image(0);
template_preview->redraw();
int item = template_browser->value();
if (item <= 1) template_instance->deactivate();
else template_instance->activate();
if (item < 1) {
template_submit->deactivate();
template_delete->deactivate();
return;
}
template_submit->activate();
const char *flfile = (const char *)template_browser->data(item);
if (!flfile) {
template_delete->deactivate();
return;
}
template_name->value(template_browser->text(item));
template_delete->activate();
char pngfile[1024], *ext;
strlcpy(pngfile, flfile, sizeof(pngfile));
if ((ext = strrchr(pngfile, '.')) == NULL) return;
strcpy(ext, ".png");
printf("updating preview to \"%s\"...\n", pngfile);
img = Fl_Shared_Image::get(pngfile);
if (img) {
printf("img=%p, img->w()=%d, img->h()=%d\n",
img, img->w(), img->h());
template_preview->image(img);
template_preview->redraw();
};
}
Fl_Box *template_preview=(Fl_Box *)0;
Fl_Input *template_name=(Fl_Input *)0;
static void cb_template_name(Fl_Input*, void*) {
if (strlen(template_name->value())) {
template_submit->activate();
if (Fl::event_key() == FL_Enter) template_panel->hide();
} else template_submit->deactivate();
}
Fl_Input *template_instance=(Fl_Input *)0;
Fl_Button *template_delete=(Fl_Button *)0;
static void cb_Cancel(Fl_Button*, void*) {
Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
if (img) img->release();
template_preview->image(0);
template_browser->deselect();
template_name->value("");
template_instance->value("");
template_panel->hide();
}
Fl_Return_Button *template_submit=(Fl_Return_Button *)0;
static void cb_template_submit(Fl_Return_Button*, void*) {
Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
if (img) img->release();
template_preview->image(0);
template_panel->hide();
}
Fl_Double_Window* make_template_panel() {
Fl_Double_Window* w;
{ Fl_Double_Window* o = template_panel = new Fl_Double_Window(460, 355, "New/Save Template");
w = o;
{ Fl_Browser* o = template_browser = new Fl_Browser(10, 28, 180, 250, "Available Templates:");
o->type(2);
o->labelfont(1);
o->callback((Fl_Callback*)cb_template_browser);
o->align(FL_ALIGN_TOP_LEFT);
o->when(3);
}
{ Fl_Box* o = template_preview = new Fl_Box(200, 28, 250, 250);
o->box(FL_THIN_DOWN_BOX);
o->align(69|FL_ALIGN_INSIDE);
Fl_Group::current()->resizable(o);
}
{ Fl_Input* o = template_name = new Fl_Input(124, 288, 326, 25, "Template Name:");
o->labelfont(1);
o->textfont(4);
o->callback((Fl_Callback*)cb_template_name);
o->when(3);
}
{ Fl_Input* o = template_instance = new Fl_Input(124, 288, 326, 25, "Instance Name:");
o->labelfont(1);
o->textfont(4);
o->hide();
}
{ Fl_Group* o = new Fl_Group(10, 323, 440, 25);
{ Fl_Button* o = template_delete = new Fl_Button(10, 323, 133, 25, "Delete Template");
o->callback((Fl_Callback*)template_delete_cb);
}
{ Fl_Box* o = new Fl_Box(153, 323, 126, 25);
Fl_Group::current()->resizable(o);
}
{ Fl_Button* o = new Fl_Button(289, 323, 72, 25, "Cancel");
o->callback((Fl_Callback*)cb_Cancel);
}
{ Fl_Return_Button* o = template_submit = new Fl_Return_Button(371, 323, 79, 25, "Save");
o->callback((Fl_Callback*)cb_template_submit);
}
o->end();
}
o->set_modal();
o->end();
}
return w;
}
void template_clear() {
int i;
void *filename;
for (i = 1; i <= template_browser->size(); i ++) {
if ((filename = template_browser->data(i)) != NULL) free(filename);
}
template_browser->deselect();
template_browser->clear();
}
void template_delete_cb(Fl_Button *, void *) {
int item = template_browser->value();
if (item < 1) return;
const char *name = template_browser->text(item);
const char *flfile = (const char *)template_browser->data(item);
if (!flfile) return;
if (!fl_choice("Are you sure you want to delete the template \"%s\"?",
"Cancel", "Delete", 0, name)) return;
if (unlink(flfile)) {
fl_alert("Unable to delete template \"%s\":\n%s", name, strerror(errno));
return;
}
template_browser->remove(item);
template_browser->do_callback();
}
void template_load() {
int i;
char name[1024], filename[1024], path[1024], *ptr;
struct dirent **files;
int num_files;
fluid_prefs.getUserdataPath(path, sizeof(path));
strlcat(path, "templates", sizeof(path));
num_files = fl_filename_list(path, &files);
for (i = 0; i < num_files; i ++) {
if (fl_filename_match(files[i]->d_name, "*.fl")) {
// Format the name as the filename with "_" replaced with " "
// and without the trailing ".fl"...
strlcpy(name, files[i]->d_name, sizeof(name));
*strstr(name, ".fl") = '\0';
for (ptr = name; *ptr; ptr ++) {
if (*ptr == '_') *ptr = ' ';
}
// Add the template to the browser...
snprintf(filename, sizeof(filename), "%s/%s", path, files[i]->d_name);
template_browser->add(name, strdup(filename));
}
free(files[i]);
}
if (files) free(files);
}
//
// End of "$Id$".
//

241
fluid/template_panel.fl Normal file
View File

@ -0,0 +1,241 @@
# data file for the Fltk User Interface Designer (fluid)
version 1.0107
header_name {.h}
code_name {.cxx}
comment {//
// "$Id$"
//
// FLUID template support for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2005 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
//
} {in_source in_header
}
decl {\#include <stdio.h>} {}
decl {\#include <stdlib.h>} {}
decl {\#include "../src/flstring.h"} {}
decl {\#include <errno.h>} {}
decl {\#include <FL/filename.H>} {}
decl {\#include <FL/fl_ask.H>} {}
decl {\#include <FL/Fl_Shared_Image.H>} {}
decl {\#include <FL/Fl_Preferences.H>} {}
declblock {\#ifdef WIN32} {after {\#endif // WIN32}
} {
decl {\#include <io.h>} {}
decl {\#else} {}
decl {\#include <unistd.h>} {}
}
decl {extern Fl_Preferences fluid_prefs;} {}
Function {make_template_panel()} {open
} {
Fl_Window template_panel {
label {New/Save Template} open
xywh {340 237 460 355} type Double resizable modal visible
} {
Fl_Browser template_browser {
label {Available Templates:}
callback {if (Fl::event_clicks()) {
template_panel->hide();
return;
}
Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
if (img) img->release();
template_preview->image(0);
template_preview->redraw();
int item = template_browser->value();
if (item <= 1) template_instance->deactivate();
else template_instance->activate();
if (item < 1) {
template_submit->deactivate();
template_delete->deactivate();
return;
}
template_submit->activate();
const char *flfile = (const char *)template_browser->data(item);
if (!flfile) {
template_delete->deactivate();
return;
}
template_name->value(template_browser->text(item));
template_delete->activate();
char pngfile[1024], *ext;
strlcpy(pngfile, flfile, sizeof(pngfile));
if ((ext = strrchr(pngfile, '.')) == NULL) return;
strcpy(ext, ".png");
printf("updating preview to \\"%s\\"...\\n", pngfile);
img = Fl_Shared_Image::get(pngfile);
if (img) {
printf("img=%p, img->w()=%d, img->h()=%d\\n",
img, img->w(), img->h());
template_preview->image(img);
template_preview->redraw();
}} selected
xywh {10 28 180 250} type Hold labelfont 1 align 5 when 3
}
Fl_Box template_preview {
xywh {200 28 250 250} box THIN_DOWN_BOX align 85 resizable
}
Fl_Input template_name {
label {Template Name:}
callback {if (strlen(template_name->value())) {
template_submit->activate();
if (Fl::event_key() == FL_Enter) template_panel->hide();
} else template_submit->deactivate();}
xywh {124 288 326 25} labelfont 1 when 3 textfont 4
}
Fl_Input template_instance {
label {Instance Name:}
xywh {124 288 326 25} labelfont 1 textfont 4 hide
}
Fl_Group {} {
xywh {10 323 440 25}
} {
Fl_Button template_delete {
label {Delete Template}
callback template_delete_cb
xywh {10 323 133 25}
}
Fl_Box {} {
xywh {153 323 126 25} resizable
}
Fl_Button {} {
label Cancel
callback {Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
if (img) img->release();
template_preview->image(0);
template_browser->deselect();
template_name->value("");
template_instance->value("");
template_panel->hide();}
xywh {289 323 72 25}
}
Fl_Return_Button template_submit {
label Save
callback {Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
if (img) img->release();
template_preview->image(0);
template_panel->hide();}
xywh {371 323 79 25}
}
}
}
}
Function {template_clear()} {return_type void
} {
code {int i;
void *filename;
for (i = 1; i <= template_browser->size(); i ++) {
if ((filename = template_browser->data(i)) != NULL) free(filename);
}
template_browser->deselect();
template_browser->clear();} {}
}
Function {template_delete_cb(Fl_Button *, void *)} {return_type void
} {
code {int item = template_browser->value();
if (item < 1) return;
const char *name = template_browser->text(item);
const char *flfile = (const char *)template_browser->data(item);
if (!flfile) return;
if (!fl_choice("Are you sure you want to delete the template \\"%s\\"?",
"Cancel", "Delete", 0, name)) return;
if (unlink(flfile)) {
fl_alert("Unable to delete template \\"%s\\":\\n%s", name, strerror(errno));
return;
}
template_browser->remove(item);
template_browser->do_callback();} {}
}
Function {template_load()} {return_type void
} {
code {int i;
char name[1024], filename[1024], path[1024], *ptr;
struct dirent **files;
int num_files;
fluid_prefs.getUserdataPath(path, sizeof(path));
strlcat(path, "templates", sizeof(path));
num_files = fl_filename_list(path, &files);
for (i = 0; i < num_files; i ++) {
if (fl_filename_match(files[i]->d_name, "*.fl")) {
// Format the name as the filename with "_" replaced with " "
// and without the trailing ".fl"...
strlcpy(name, files[i]->d_name, sizeof(name));
*strstr(name, ".fl") = '\\0';
for (ptr = name; *ptr; ptr ++) {
if (*ptr == '_') *ptr = ' ';
}
// Add the template to the browser...
snprintf(filename, sizeof(filename), "%s/%s", path, files[i]->d_name);
template_browser->add(name, strdup(filename));
}
free(files[i]);
}
if (files) free(files);} {}
}
comment {
//
// End of "$Id$".
//} {in_source in_header
}

56
fluid/template_panel.h Normal file
View File

@ -0,0 +1,56 @@
//
// "$Id$"
//
// FLUID template support for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2005 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
//
// generated by Fast Light User Interface Designer (fluid) version 1.0107
#ifndef template_panel_h
#define template_panel_h
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
extern Fl_Double_Window *template_panel;
#include <FL/Fl_Browser.H>
extern Fl_Browser *template_browser;
#include <FL/Fl_Box.H>
extern Fl_Box *template_preview;
#include <FL/Fl_Input.H>
extern Fl_Input *template_name;
extern Fl_Input *template_instance;
#include <FL/Fl_Group.H>
#include <FL/Fl_Button.H>
extern void template_delete_cb(Fl_Button*, void*);
extern Fl_Button *template_delete;
#include <FL/Fl_Return_Button.H>
extern Fl_Return_Button *template_submit;
Fl_Double_Window* make_template_panel();
void template_clear();
void template_delete_cb(Fl_Button *, void *);
void template_load();
#endif
//
// End of "$Id$".
//