You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.5KB

  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.
  12. //
  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.
  17. //
  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.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. // generated by Fast Light User Interface Designer (fluid) version 1.0300
  28. #include "template_panel.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "../src/flstring.h"
  32. #include <errno.h>
  33. #include <FL/filename.H>
  34. #include <FL/fl_ask.H>
  35. #include <FL/Fl_Shared_Image.H>
  36. #include <FL/Fl_Preferences.H>
  37. #if defined(WIN32) && !defined(__CYGWIN__)
  38. #include <io.h>
  39. #else
  40. #include <unistd.h>
  41. #endif // WIN32 && !__CYGWIN__
  42. extern Fl_Preferences fluid_prefs;
  43. Fl_Double_Window *template_panel=(Fl_Double_Window *)0;
  44. static void cb_template_panel(Fl_Double_Window*, void*) {
  45. Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
  46. if (img) img->release();
  47. template_preview->image(0);
  48. template_browser->deselect();
  49. template_name->value("");
  50. template_instance->value("");
  51. template_panel->hide();
  52. }
  53. Fl_Browser *template_browser=(Fl_Browser *)0;
  54. static void cb_template_browser(Fl_Browser*, void*) {
  55. if (Fl::event_clicks()) {
  56. template_panel->hide();
  57. return;
  58. }
  59. Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
  60. if (img) img->release();
  61. template_preview->image(0);
  62. template_preview->redraw();
  63. int item = template_browser->value();
  64. if (item <= 1) template_instance->deactivate();
  65. else template_instance->activate();
  66. if (item < 1) {
  67. template_submit->deactivate();
  68. template_delete->deactivate();
  69. return;
  70. }
  71. template_submit->activate();
  72. const char *flfile = (const char *)template_browser->data(item);
  73. if (!flfile) {
  74. template_delete->deactivate();
  75. return;
  76. }
  77. template_name->value(template_browser->text(item));
  78. template_delete->activate();
  79. char pngfile[1024], *ext;
  80. strlcpy(pngfile, flfile, sizeof(pngfile));
  81. if ((ext = strrchr(pngfile, '.')) == NULL) return;
  82. strcpy(ext, ".png");
  83. img = Fl_Shared_Image::get(pngfile);
  84. if (img) {
  85. template_preview->image(img);
  86. template_preview->redraw();
  87. };
  88. }
  89. Fl_Box *template_preview=(Fl_Box *)0;
  90. Fl_Input *template_name=(Fl_Input *)0;
  91. static void cb_template_name(Fl_Input*, void*) {
  92. if (strlen(template_name->value())) {
  93. template_submit->activate();
  94. if (Fl::event_key() == FL_Enter) template_panel->hide();
  95. } else template_submit->deactivate();
  96. }
  97. Fl_Input *template_instance=(Fl_Input *)0;
  98. Fl_Button *template_delete=(Fl_Button *)0;
  99. static void cb_Cancel(Fl_Button*, void*) {
  100. Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
  101. if (img) img->release();
  102. template_preview->image(0);
  103. template_browser->deselect();
  104. template_name->value("");
  105. template_instance->value("");
  106. template_panel->hide();
  107. }
  108. Fl_Return_Button *template_submit=(Fl_Return_Button *)0;
  109. static void cb_template_submit(Fl_Return_Button*, void*) {
  110. Fl_Shared_Image *img = (Fl_Shared_Image *)template_preview->image();
  111. if (img) img->release();
  112. template_preview->image(0);
  113. template_panel->hide();
  114. }
  115. Fl_Double_Window* make_template_panel() {
  116. { template_panel = new Fl_Double_Window(460, 355, "New/Save Template");
  117. template_panel->callback((Fl_Callback*)cb_template_panel);
  118. { template_browser = new Fl_Browser(10, 28, 180, 250, "Available Templates:");
  119. template_browser->type(2);
  120. template_browser->labelfont(1);
  121. template_browser->callback((Fl_Callback*)cb_template_browser);
  122. template_browser->align(Fl_Align(FL_ALIGN_TOP_LEFT));
  123. template_browser->when(3);
  124. } // Fl_Browser* template_browser
  125. { template_preview = new Fl_Box(200, 28, 250, 250);
  126. template_preview->box(FL_THIN_DOWN_BOX);
  127. template_preview->align(Fl_Align(69|FL_ALIGN_INSIDE));
  128. Fl_Group::current()->resizable(template_preview);
  129. } // Fl_Box* template_preview
  130. { template_name = new Fl_Input(124, 288, 326, 25, "Template Name:");
  131. template_name->labelfont(1);
  132. template_name->textfont(4);
  133. template_name->callback((Fl_Callback*)cb_template_name);
  134. template_name->when(3);
  135. } // Fl_Input* template_name
  136. { template_instance = new Fl_Input(124, 288, 326, 25, "Instance Name:");
  137. template_instance->labelfont(1);
  138. template_instance->textfont(4);
  139. template_instance->hide();
  140. } // Fl_Input* template_instance
  141. { Fl_Group* o = new Fl_Group(10, 323, 440, 25);
  142. { template_delete = new Fl_Button(10, 323, 133, 25, "Delete Template");
  143. template_delete->callback((Fl_Callback*)template_delete_cb);
  144. } // Fl_Button* template_delete
  145. { Fl_Box* o = new Fl_Box(153, 323, 126, 25);
  146. Fl_Group::current()->resizable(o);
  147. } // Fl_Box* o
  148. { Fl_Button* o = new Fl_Button(289, 323, 72, 25, "Cancel");
  149. o->callback((Fl_Callback*)cb_Cancel);
  150. } // Fl_Button* o
  151. { template_submit = new Fl_Return_Button(371, 323, 79, 25, "Save");
  152. template_submit->callback((Fl_Callback*)cb_template_submit);
  153. } // Fl_Return_Button* template_submit
  154. o->end();
  155. } // Fl_Group* o
  156. template_panel->set_modal();
  157. template_panel->end();
  158. } // Fl_Double_Window* template_panel
  159. return template_panel;
  160. }
  161. void template_clear() {
  162. int i;
  163. void *filename;
  164. for (i = 1; i <= template_browser->size(); i ++) {
  165. if ((filename = template_browser->data(i)) != NULL) free(filename);
  166. }
  167. template_browser->deselect();
  168. template_browser->clear();
  169. }
  170. void template_delete_cb(Fl_Button *, void *) {
  171. int item = template_browser->value();
  172. if (item < 1) return;
  173. const char *name = template_browser->text(item);
  174. const char *flfile = (const char *)template_browser->data(item);
  175. if (!flfile) return;
  176. if (!fl_choice("Are you sure you want to delete the template \"%s\"?",
  177. "Cancel", "Delete", 0, name)) return;
  178. if (unlink(flfile)) {
  179. fl_alert("Unable to delete template \"%s\":\n%s", name, strerror(errno));
  180. return;
  181. }
  182. template_browser->remove(item);
  183. template_browser->do_callback();
  184. }
  185. void template_load() {
  186. int i;
  187. char name[1024], filename[1024], path[1024], *ptr;
  188. struct dirent **files;
  189. int num_files;
  190. fluid_prefs.getUserdataPath(path, sizeof(path));
  191. strlcat(path, "templates", sizeof(path));
  192. num_files = fl_filename_list(path, &files);
  193. for (i = 0; i < num_files; i ++) {
  194. if (fl_filename_match(files[i]->d_name, "*.fl")) {
  195. // Format the name as the filename with "_" replaced with " "
  196. // and without the trailing ".fl"...
  197. strlcpy(name, files[i]->d_name, sizeof(name));
  198. *strstr(name, ".fl") = '\0';
  199. for (ptr = name; *ptr; ptr ++) {
  200. if (*ptr == '_') *ptr = ' ';
  201. }
  202. // Add the template to the browser...
  203. snprintf(filename, sizeof(filename), "%s/%s", path, files[i]->d_name);
  204. template_browser->add(name, strdup(filename));
  205. }
  206. free(files[i]);
  207. }
  208. if (num_files > 0) free(files);
  209. }
  210. //
  211. // End of "$Id$".
  212. //