Tweak themes for more color consistency.
[ntk.git] / test / file_chooser.cxx
blob95b1e634116a8c9d7a10c6f872df6e111a7f2cfe
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.
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
27 // Contents:
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...
38 // extra_callback() - circle extra groups (none,group1,check_button);
42 // Include necessary headers...
45 #include <stdio.h>
46 #include <FL/Fl_File_Chooser.H>
47 #include <FL/Fl_File_Icon.H>
48 #include <FL/Fl_Shared_Image.H>
49 #include <FL/Fl_PNM_Image.H>
50 #include <FL/Fl_Light_Button.H>
51 #include <FL/Fl_Double_Window.H>
52 #include <string.h>
56 // Globals...
59 Fl_Input *filter;
60 Fl_File_Browser *files;
61 Fl_File_Chooser *fc;
62 Fl_Shared_Image *image = 0;
64 // for choosing extra groups
65 Fl_Choice *ch_extra;
66 // first extra group
67 Fl_Group *encodings = (Fl_Group*)0;
68 Fl_Choice *ch_enc;
69 // second extra widget
70 Fl_Check_Button *version = (Fl_Check_Button*)0;
73 // Functions...
76 void close_callback(void);
77 void create_callback(void);
78 void dir_callback(void);
79 void fc_callback(Fl_File_Chooser *, void *);
80 void multi_callback(void);
81 Fl_Image *pdf_check(const char *, uchar *, int);
82 Fl_Image *ps_check(const char *, uchar *, int);
83 void show_callback(void);
85 void extra_callback(Fl_Choice*,void*);
88 // 'main()' - Create a file chooser and wait for a selection to be made.
91 int // O - Exit status
92 main(int argc, // I - Number of command-line arguments
93 char *argv[]) // I - Command-line arguments
95 Fl_Double_Window *window;// Main window
96 Fl_Button *button;// Buttons
97 Fl_File_Icon *icon; // New file icon
100 // Make the file chooser...
101 Fl::scheme(NULL);
102 Fl_File_Icon::load_system_icons();
104 fc = new Fl_File_Chooser(".", "*", Fl_File_Chooser::SINGLE, "Fl_File_Chooser Test");
105 fc->callback(fc_callback);
107 // Register the PS and PDF image types...
108 Fl_Shared_Image::add_handler(pdf_check);
109 Fl_Shared_Image::add_handler(ps_check);
111 // Make the main window...
112 window = new Fl_Double_Window(400, 215, "File Chooser Test");
114 filter = new Fl_Input(50, 10, 315, 25, "Filter:");
115 int argn = 1;
116 #ifdef __APPLE__
117 // OS X may add the process number as the first argument - ignore
118 if (argc>argn && strncmp(argv[1], "-psn_", 5)==0)
119 argn++;
120 #endif
121 if (argc > argn)
122 filter->value(argv[argn]);
123 else
124 filter->value("PDF Files (*.pdf)\t"
125 "PostScript Files (*.ps)\t"
126 "Image Files (*.{bmp,gif,jpg,png})\t"
127 "C/C++ Source Files (*.{c,C,cc,cpp,cxx})");
129 button = new Fl_Button(365, 10, 25, 25);
130 button->labelcolor(FL_YELLOW);
131 button->callback((Fl_Callback *)show_callback);
133 icon = Fl_File_Icon::find(".", Fl_File_Icon::DIRECTORY);
134 icon->label(button);
136 button = new Fl_Light_Button(50, 45, 80, 25, "MULTI");
137 button->callback((Fl_Callback *)multi_callback);
139 button = new Fl_Light_Button(140, 45, 90, 25, "CREATE");
140 button->callback((Fl_Callback *)create_callback);
142 button = new Fl_Light_Button(240, 45, 115, 25, "DIRECTORY");
143 button->callback((Fl_Callback *)dir_callback);
146 ch_extra = new Fl_Choice(150, 75, 150, 25, "Extra Group:");
147 ch_extra->add("none|encodings group|check button");
148 ch_extra->value(0);
149 ch_extra->callback((Fl_Callback *)extra_callback);
151 files = new Fl_File_Browser(50, 105, 340, 75, "Files:");
152 files->align(FL_ALIGN_LEFT);
154 button = new Fl_Button(340, 185, 50, 25, "Close");
155 button->callback((Fl_Callback *)close_callback);
157 window->resizable(files);
158 window->end();
159 window->show(1, argv);
161 Fl::run();
163 return (0);
167 void
168 extra_callback(Fl_Choice*w,void*)
170 int val=w->value();
171 if (0 == val) fc->add_extra(NULL);
172 else if (1 == val) {
173 if(!encodings){
174 encodings=new Fl_Group(0,0,254,30);
175 ch_enc=new Fl_Choice(152,2,100,25,"Choose Encoding:");
176 ch_enc->add("ASCII|Koi8-r|win1251|Utf-8");
177 encodings->end();
179 fc->add_extra(encodings);
180 } else {
181 if (!version) {
182 version = new Fl_Check_Button(5,0,200,25,"Save binary 1.0 version");
184 fc->add_extra(version);
190 // 'close_callback()' - Close the main window...
193 void
194 close_callback(void)
196 exit(0);
201 // 'create_callback()' - Handle clicks on the create button.
204 void
205 create_callback(void)
207 fc->type(fc->type() ^ Fl_File_Chooser::CREATE);
212 // 'dir_callback()' - Handle clicks on the directory button.
215 void
216 dir_callback(void)
218 fc->type(fc->type() ^ Fl_File_Chooser::DIRECTORY);
223 // 'fc_callback()' - Handle choices in the file chooser...
226 void
227 fc_callback(Fl_File_Chooser *fc, // I - File chooser
228 void *data) // I - Data
230 const char *filename; // Current filename
233 printf("fc_callback(fc = %p, data = %p)\n", fc, data);
235 filename = fc->value();
237 printf(" filename = \"%s\"\n", filename ? filename : "(null)");
242 // 'multi_callback()' - Handle clicks on the multi button.
245 void
246 multi_callback(void)
248 fc->type(fc->type() ^ Fl_File_Chooser::MULTI);
253 // 'pdf_check()' - Check for and load the first page of a PDF file.
256 Fl_Image * // O - Page image or NULL
257 pdf_check(const char *name, // I - Name of file
258 uchar *header, // I - Header data
259 int) // I - Length of header data (unused)
261 const char *home; // Home directory
262 char preview[FL_PATH_MAX], // Preview filename
263 command[FL_PATH_MAX]; // Command
266 if (memcmp(header, "%PDF", 4) != 0)
267 return 0;
269 home = getenv("HOME");
270 sprintf(preview, "%s/.preview.ppm", home ? home : "");
272 sprintf(command,
273 "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
274 "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' "
275 "-dFirstPage=1 -dLastPage=1 \'%s\' 2>/dev/null", preview, name);
277 if (system(command)) return 0;
279 return new Fl_PNM_Image(preview);
284 // 'ps_check()' - Check for and load the first page of a PostScript file.
287 Fl_Image * // O - Page image or NULL
288 ps_check(const char *name, // I - Name of file
289 uchar *header, // I - Header data
290 int) // I - Length of header data (unused)
292 const char *home; // Home directory
293 char preview[FL_PATH_MAX], // Preview filename
294 outname[FL_PATH_MAX], // Preview PS file
295 command[FL_PATH_MAX]; // Command
296 FILE *in, // Input file
297 *out; // Output file
298 int page; // Current page
299 char line[256]; // Line from file
302 if (memcmp(header, "%!", 2) != 0)
303 return 0;
305 home = getenv("HOME");
306 sprintf(preview, "%s/.preview.ppm", home ? home : "");
308 if (memcmp(header, "%!PS", 4) == 0) {
309 // PS file has DSC comments; extract the first page...
310 sprintf(outname, "%s/.preview.ps", home ? home : "");
312 if (strcmp(name, outname) != 0) {
313 in = fl_fopen(name, "rb");
314 out = fl_fopen(outname, "wb");
315 page = 0;
317 while (fgets(line, sizeof(line), in) != NULL) {
318 if (strncmp(line, "%%Page:", 7) == 0) {
319 page ++;
320 if (page > 1) break;
323 fputs(line, out);
326 fclose(in);
327 fclose(out);
329 } else {
330 // PS file doesn't have DSC comments; do the whole file...
331 strncpy(outname, name, sizeof(outname) - 1);
332 outname[sizeof(outname) - 1] = '\0';
335 sprintf(command,
336 "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
337 "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' \'%s\' 2>/dev/null",
338 preview, outname);
340 if (system(command)) return 0;
342 return new Fl_PNM_Image(preview);
347 // 'show_callback()' - Show the file chooser...
350 void
351 show_callback(void)
353 int i; // Looping var
354 int count; // Number of files selected
355 char relative[FL_PATH_MAX]; // Relative filename
358 if (filter->value()[0])
359 fc->filter(filter->value());
361 fc->show();
363 while (fc->visible()) {
364 Fl::wait();
367 count = fc->count();
368 if (count > 0)
370 files->clear();
372 for (i = 1; i <= count; i ++)
374 if (!fc->value(i))
375 break;
377 fl_filename_relative(relative, sizeof(relative), fc->value(i));
379 files->add(relative,
380 Fl_File_Icon::find(fc->value(i), Fl_File_Icon::PLAIN));
383 files->redraw();
389 // End of "$Id: file_chooser.cxx 8164 2011-01-01 20:17:58Z matt $".