summaryrefslogtreecommitdiff
path: root/fluid/template_panel.cxx
blob: 01b1a35dd7c4614714f3c59609be854482f91a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// "$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>
#if defined(WIN32) && !defined(__CYGWIN__)
#include <io.h>
#else
#include <unistd.h>
#endif // WIN32 && !__CYGWIN__
extern Fl_Preferences fluid_prefs;

Fl_Double_Window *template_panel=(Fl_Double_Window *)0;

static void cb_template_panel(Fl_Double_Window*, 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_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");

img = Fl_Shared_Image::get(pngfile);

if (img) {
  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;
    o->callback((Fl_Callback*)cb_template_panel);
    { 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 (num_files > 0) free(files);
}

//
// End of "$Id$".
//