template_panel.cxx 7.2 KB

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