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.

391 lines
9.1KB

  1. //
  2. // "$Id: file_chooser.cxx 8164 2011-01-01 20:17:58Z matt $"
  3. //
  4. // File chooser test program.
  5. //
  6. // Copyright 1999-2010 by Michael Sweet.
  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. // Contents:
  28. //
  29. // main() - Create a file chooser and wait for a selection to
  30. // be made.
  31. // close_callback() - Close the main window...
  32. // fc_callback() - Handle choices in the file chooser...
  33. // pdf_check() - Check for and load the first page of a PDF file.
  34. // ps_check() - Check for and load the first page of a PostScript
  35. // file.
  36. // show_callback() - Show the file chooser...
  37. //
  38. // extra_callback() - circle extra groups (none,group1,check_button);
  39. //
  40. //
  41. // Include necessary headers...
  42. //
  43. #include <stdio.h>
  44. #include <FL/Fl_File_Chooser.H>
  45. #include <FL/Fl_File_Icon.H>
  46. #include <FL/Fl_Shared_Image.H>
  47. #include <FL/Fl_PNM_Image.H>
  48. #include <FL/Fl_Light_Button.H>
  49. #include <FL/Fl_Double_Window.H>
  50. #include <string.h>
  51. //
  52. // Globals...
  53. //
  54. Fl_Input *filter;
  55. Fl_File_Browser *files;
  56. Fl_File_Chooser *fc;
  57. Fl_Shared_Image *image = 0;
  58. // for choosing extra groups
  59. Fl_Choice *ch_extra;
  60. // first extra group
  61. Fl_Group *encodings = (Fl_Group*)0;
  62. Fl_Choice *ch_enc;
  63. // second extra widget
  64. Fl_Check_Button *version = (Fl_Check_Button*)0;
  65. //
  66. // Functions...
  67. //
  68. void close_callback(void);
  69. void create_callback(void);
  70. void dir_callback(void);
  71. void fc_callback(Fl_File_Chooser *, void *);
  72. void multi_callback(void);
  73. Fl_Image *pdf_check(const char *, uchar *, int);
  74. Fl_Image *ps_check(const char *, uchar *, int);
  75. void show_callback(void);
  76. void extra_callback(Fl_Choice*,void*);
  77. //
  78. // 'main()' - Create a file chooser and wait for a selection to be made.
  79. //
  80. int // O - Exit status
  81. main(int argc, // I - Number of command-line arguments
  82. char *argv[]) // I - Command-line arguments
  83. {
  84. Fl_Double_Window *window;// Main window
  85. Fl_Button *button;// Buttons
  86. Fl_File_Icon *icon; // New file icon
  87. // Make the file chooser...
  88. Fl::scheme(NULL);
  89. Fl_File_Icon::load_system_icons();
  90. fc = new Fl_File_Chooser(".", "*", Fl_File_Chooser::SINGLE, "Fl_File_Chooser Test");
  91. fc->callback(fc_callback);
  92. // Register the PS and PDF image types...
  93. Fl_Shared_Image::add_handler(pdf_check);
  94. Fl_Shared_Image::add_handler(ps_check);
  95. // Make the main window...
  96. window = new Fl_Double_Window(400, 215, "File Chooser Test");
  97. filter = new Fl_Input(50, 10, 315, 25, "Filter:");
  98. int argn = 1;
  99. #ifdef __APPLE__
  100. // OS X may add the process number as the first argument - ignore
  101. if (argc>argn && strncmp(argv[1], "-psn_", 5)==0)
  102. argn++;
  103. #endif
  104. if (argc > argn)
  105. filter->value(argv[argn]);
  106. else
  107. filter->value("PDF Files (*.pdf)\t"
  108. "PostScript Files (*.ps)\t"
  109. "Image Files (*.{bmp,gif,jpg,png})\t"
  110. "C/C++ Source Files (*.{c,C,cc,cpp,cxx})");
  111. button = new Fl_Button(365, 10, 25, 25);
  112. button->labelcolor(FL_YELLOW);
  113. button->callback((Fl_Callback *)show_callback);
  114. icon = Fl_File_Icon::find(".", Fl_File_Icon::DIRECTORY);
  115. icon->label(button);
  116. button = new Fl_Light_Button(50, 45, 80, 25, "MULTI");
  117. button->callback((Fl_Callback *)multi_callback);
  118. button = new Fl_Light_Button(140, 45, 90, 25, "CREATE");
  119. button->callback((Fl_Callback *)create_callback);
  120. button = new Fl_Light_Button(240, 45, 115, 25, "DIRECTORY");
  121. button->callback((Fl_Callback *)dir_callback);
  122. //
  123. ch_extra = new Fl_Choice(150, 75, 150, 25, "Extra Group:");
  124. ch_extra->add("none|encodings group|check button");
  125. ch_extra->value(0);
  126. ch_extra->callback((Fl_Callback *)extra_callback);
  127. //
  128. files = new Fl_File_Browser(50, 105, 340, 75, "Files:");
  129. files->align(FL_ALIGN_LEFT);
  130. button = new Fl_Button(340, 185, 50, 25, "Close");
  131. button->callback((Fl_Callback *)close_callback);
  132. window->resizable(files);
  133. window->end();
  134. window->show(1, argv);
  135. Fl::run();
  136. return (0);
  137. }
  138. void
  139. extra_callback(Fl_Choice*w,void*)
  140. {
  141. int val=w->value();
  142. if (0 == val) fc->add_extra(NULL);
  143. else if (1 == val) {
  144. if(!encodings){
  145. encodings=new Fl_Group(0,0,254,30);
  146. ch_enc=new Fl_Choice(152,2,100,25,"Choose Encoding:");
  147. ch_enc->add("ASCII|Koi8-r|win1251|Utf-8");
  148. encodings->end();
  149. }
  150. fc->add_extra(encodings);
  151. } else {
  152. if (!version) {
  153. version = new Fl_Check_Button(5,0,200,25,"Save binary 1.0 version");
  154. }
  155. fc->add_extra(version);
  156. }
  157. }
  158. //
  159. // 'close_callback()' - Close the main window...
  160. //
  161. void
  162. close_callback(void)
  163. {
  164. exit(0);
  165. }
  166. //
  167. // 'create_callback()' - Handle clicks on the create button.
  168. //
  169. void
  170. create_callback(void)
  171. {
  172. fc->type(fc->type() ^ Fl_File_Chooser::CREATE);
  173. }
  174. //
  175. // 'dir_callback()' - Handle clicks on the directory button.
  176. //
  177. void
  178. dir_callback(void)
  179. {
  180. fc->type(fc->type() ^ Fl_File_Chooser::DIRECTORY);
  181. }
  182. //
  183. // 'fc_callback()' - Handle choices in the file chooser...
  184. //
  185. void
  186. fc_callback(Fl_File_Chooser *fc, // I - File chooser
  187. void *data) // I - Data
  188. {
  189. const char *filename; // Current filename
  190. printf("fc_callback(fc = %p, data = %p)\n", fc, data);
  191. filename = fc->value();
  192. printf(" filename = \"%s\"\n", filename ? filename : "(null)");
  193. }
  194. //
  195. // 'multi_callback()' - Handle clicks on the multi button.
  196. //
  197. void
  198. multi_callback(void)
  199. {
  200. fc->type(fc->type() ^ Fl_File_Chooser::MULTI);
  201. }
  202. //
  203. // 'pdf_check()' - Check for and load the first page of a PDF file.
  204. //
  205. Fl_Image * // O - Page image or NULL
  206. pdf_check(const char *name, // I - Name of file
  207. uchar *header, // I - Header data
  208. int) // I - Length of header data (unused)
  209. {
  210. const char *home; // Home directory
  211. char preview[FL_PATH_MAX], // Preview filename
  212. command[FL_PATH_MAX]; // Command
  213. if (memcmp(header, "%PDF", 4) != 0)
  214. return 0;
  215. home = getenv("HOME");
  216. sprintf(preview, "%s/.preview.ppm", home ? home : "");
  217. sprintf(command,
  218. "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
  219. "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' "
  220. "-dFirstPage=1 -dLastPage=1 \'%s\' 2>/dev/null", preview, name);
  221. if (system(command)) return 0;
  222. return new Fl_PNM_Image(preview);
  223. }
  224. //
  225. // 'ps_check()' - Check for and load the first page of a PostScript file.
  226. //
  227. Fl_Image * // O - Page image or NULL
  228. ps_check(const char *name, // I - Name of file
  229. uchar *header, // I - Header data
  230. int) // I - Length of header data (unused)
  231. {
  232. const char *home; // Home directory
  233. char preview[FL_PATH_MAX], // Preview filename
  234. outname[FL_PATH_MAX], // Preview PS file
  235. command[FL_PATH_MAX]; // Command
  236. FILE *in, // Input file
  237. *out; // Output file
  238. int page; // Current page
  239. char line[256]; // Line from file
  240. if (memcmp(header, "%!", 2) != 0)
  241. return 0;
  242. home = getenv("HOME");
  243. sprintf(preview, "%s/.preview.ppm", home ? home : "");
  244. if (memcmp(header, "%!PS", 4) == 0) {
  245. // PS file has DSC comments; extract the first page...
  246. sprintf(outname, "%s/.preview.ps", home ? home : "");
  247. if (strcmp(name, outname) != 0) {
  248. in = fl_fopen(name, "rb");
  249. out = fl_fopen(outname, "wb");
  250. page = 0;
  251. while (fgets(line, sizeof(line), in) != NULL) {
  252. if (strncmp(line, "%%Page:", 7) == 0) {
  253. page ++;
  254. if (page > 1) break;
  255. }
  256. fputs(line, out);
  257. }
  258. fclose(in);
  259. fclose(out);
  260. }
  261. } else {
  262. // PS file doesn't have DSC comments; do the whole file...
  263. strncpy(outname, name, sizeof(outname) - 1);
  264. outname[sizeof(outname) - 1] = '\0';
  265. }
  266. sprintf(command,
  267. "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
  268. "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' \'%s\' 2>/dev/null",
  269. preview, outname);
  270. if (system(command)) return 0;
  271. return new Fl_PNM_Image(preview);
  272. }
  273. //
  274. // 'show_callback()' - Show the file chooser...
  275. //
  276. void
  277. show_callback(void)
  278. {
  279. int i; // Looping var
  280. int count; // Number of files selected
  281. char relative[FL_PATH_MAX]; // Relative filename
  282. if (filter->value()[0])
  283. fc->filter(filter->value());
  284. fc->show();
  285. while (fc->visible()) {
  286. Fl::wait();
  287. }
  288. count = fc->count();
  289. if (count > 0)
  290. {
  291. files->clear();
  292. for (i = 1; i <= count; i ++)
  293. {
  294. if (!fc->value(i))
  295. break;
  296. fl_filename_relative(relative, sizeof(relative), fc->value(i));
  297. files->add(relative,
  298. Fl_File_Icon::find(fc->value(i), Fl_File_Icon::PLAIN));
  299. }
  300. files->redraw();
  301. }
  302. }
  303. //
  304. // End of "$Id: file_chooser.cxx 8164 2011-01-01 20:17:58Z matt $".
  305. //