X11: Add support for horizontal mouse scroll axis input.
[ntk.git] / fluid / template_panel.cxx
blob5abe9adc8f716e184f1ae4b283ea911fd24f2fce
1 //
2 // "$Id$"
3 //
4 // FLUID template support for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // generated by Fast Light User Interface Designer (fluid) version 1.0300
30 #include "template_panel.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "../src/flstring.h"
34 #include <errno.h>
35 #include <FL/filename.H>
36 #include <FL/fl_ask.H>
37 #include <FL/Fl_Shared_Image.H>
38 #include <FL/Fl_Preferences.H>
39 #if defined(WIN32) && !defined(__CYGWIN__)
40 #include <io.h>
41 #else
42 #include <unistd.h>
43 #endif // WIN32 && !__CYGWIN__
44 extern Fl_Preferences fluid_prefs;
46 Fl_Double_Window *template_panel=(Fl_Double_Window *)0;
48 static void cb_template_panel(Fl_Double_Window*, void*) {
49 Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
50 if (img) img->release();
51 template_preview->image(0);
53 template_browser->deselect();
54 template_name->value("");
55 template_instance->value("");
56 template_panel->hide();
59 Fl_Browser *template_browser=(Fl_Browser *)0;
61 static void cb_template_browser(Fl_Browser*, void*) {
62 if (Fl::event_clicks()) {
63 template_panel->hide();
64 return;
66 Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
67 if (img) img->release();
68 template_preview->image(0);
69 template_preview->redraw();
71 int item = template_browser->value();
73 if (item <= 1) template_instance->deactivate();
74 else template_instance->activate();
76 if (item < 1) {
77 template_submit->deactivate();
78 template_delete->deactivate();
79 return;
82 template_submit->activate();
84 const char *flfile = (const char *)template_browser->data(item);
85 if (!flfile) {
86 template_delete->deactivate();
87 return;
90 template_name->value(template_browser->text(item));
92 template_delete->activate();
94 char pngfile[1024], *ext;
96 strlcpy(pngfile, flfile, sizeof(pngfile));
97 if ((ext = strrchr(pngfile, '.')) == NULL) return;
98 strcpy(ext, ".png");
100 img = Fl_Shared_Image::get(pngfile);
102 if (img) {
103 template_preview->image(img);
104 template_preview->redraw();
108 Fl_Box *template_preview=(Fl_Box *)0;
110 Fl_Input *template_name=(Fl_Input *)0;
112 static void cb_template_name(Fl_Input*, void*) {
113 if (strlen(template_name->value())) {
114 template_submit->activate();
115 if (Fl::event_key() == FL_Enter) template_panel->hide();
116 } else template_submit->deactivate();
119 Fl_Input *template_instance=(Fl_Input *)0;
121 Fl_Button *template_delete=(Fl_Button *)0;
123 static void cb_Cancel(Fl_Button*, void*) {
124 Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
125 if (img) img->release();
126 template_preview->image(0);
128 template_browser->deselect();
129 template_name->value("");
130 template_instance->value("");
131 template_panel->hide();
134 Fl_Return_Button *template_submit=(Fl_Return_Button *)0;
136 static void cb_template_submit(Fl_Return_Button*, void*) {
137 Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
138 if (img) img->release();
139 template_preview->image(0);
141 template_panel->hide();
144 Fl_Double_Window* make_template_panel() {
145 { template_panel = new Fl_Double_Window(460, 355, "New/Save Template");
146 template_panel->callback((Fl_Callback*)cb_template_panel);
147 { template_browser = new Fl_Browser(10, 28, 180, 250, "Available Templates:");
148 template_browser->type(2);
149 template_browser->labelfont(1);
150 template_browser->callback((Fl_Callback*)cb_template_browser);
151 template_browser->align(Fl_Align(FL_ALIGN_TOP_LEFT));
152 template_browser->when(3);
153 } // Fl_Browser* template_browser
154 { template_preview = new Fl_Box(200, 28, 250, 250);
155 template_preview->box(FL_THIN_DOWN_BOX);
156 template_preview->align(Fl_Align(69|FL_ALIGN_INSIDE));
157 Fl_Group::current()->resizable(template_preview);
158 } // Fl_Box* template_preview
159 { template_name = new Fl_Input(124, 288, 326, 25, "Template Name:");
160 template_name->labelfont(1);
161 template_name->textfont(4);
162 template_name->callback((Fl_Callback*)cb_template_name);
163 template_name->when(3);
164 } // Fl_Input* template_name
165 { template_instance = new Fl_Input(124, 288, 326, 25, "Instance Name:");
166 template_instance->labelfont(1);
167 template_instance->textfont(4);
168 template_instance->hide();
169 } // Fl_Input* template_instance
170 { Fl_Group* o = new Fl_Group(10, 323, 440, 25);
171 { template_delete = new Fl_Button(10, 323, 133, 25, "Delete Template");
172 template_delete->callback((Fl_Callback*)template_delete_cb);
173 } // Fl_Button* template_delete
174 { Fl_Box* o = new Fl_Box(153, 323, 126, 25);
175 Fl_Group::current()->resizable(o);
176 } // Fl_Box* o
177 { Fl_Button* o = new Fl_Button(289, 323, 72, 25, "Cancel");
178 o->callback((Fl_Callback*)cb_Cancel);
179 } // Fl_Button* o
180 { template_submit = new Fl_Return_Button(371, 323, 79, 25, "Save");
181 template_submit->callback((Fl_Callback*)cb_template_submit);
182 } // Fl_Return_Button* template_submit
183 o->end();
184 } // Fl_Group* o
185 template_panel->set_modal();
186 template_panel->end();
187 } // Fl_Double_Window* template_panel
188 return template_panel;
191 void template_clear() {
192 int i;
193 void *filename;
195 for (i = 1; i <= template_browser->size(); i ++) {
196 if ((filename = template_browser->data(i)) != NULL) free(filename);
199 template_browser->deselect();
200 template_browser->clear();
203 void template_delete_cb(Fl_Button *, void *) {
204 int item = template_browser->value();
205 if (item < 1) return;
207 const char *name = template_browser->text(item);
208 const char *flfile = (const char *)template_browser->data(item);
209 if (!flfile) return;
211 if (!fl_choice("Are you sure you want to delete the template \"%s\"?",
212 "Cancel", "Delete", 0, name)) return;
214 if (unlink(flfile)) {
215 fl_alert("Unable to delete template \"%s\":\n%s", name, strerror(errno));
216 return;
219 template_browser->remove(item);
220 template_browser->do_callback();
223 void template_load() {
224 int i;
225 char name[1024], filename[1024], path[1024], *ptr;
226 struct dirent **files;
227 int num_files;
229 fluid_prefs.getUserdataPath(path, sizeof(path));
230 strlcat(path, "templates", sizeof(path));
232 num_files = fl_filename_list(path, &files);
234 for (i = 0; i < num_files; i ++) {
235 if (fl_filename_match(files[i]->d_name, "*.fl")) {
236 // Format the name as the filename with "_" replaced with " "
237 // and without the trailing ".fl"...
238 strlcpy(name, files[i]->d_name, sizeof(name));
239 *strstr(name, ".fl") = '\0';
241 for (ptr = name; *ptr; ptr ++) {
242 if (*ptr == '_') *ptr = ' ';
245 // Add the template to the browser...
246 snprintf(filename, sizeof(filename), "%s/%s", path, files[i]->d_name);
247 template_browser->add(name, strdup(filename));
250 free(files[i]);
253 if (num_files > 0) free(files);
257 // End of "$Id$".