2 // "$Id: file_chooser.cxx 8164 2011-01-01 20:17:58Z matt $"
4 // File chooser test program.
6 // Copyright 1999-2010 by Michael Sweet.
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
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 // main() - Create a file chooser and wait for a selection to
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
36 // show_callback() - Show the file chooser...
38 // extra_callback() - circle extra groups (none,group1,check_button);
42 // Include necessary headers...
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>
60 Fl_File_Browser
*files
;
62 Fl_Shared_Image
*image
= 0;
64 // for choosing extra groups
67 Fl_Group
*encodings
= (Fl_Group
*)0;
69 // second extra widget
70 Fl_Check_Button
*version
= (Fl_Check_Button
*)0;
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...
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:");
117 // OS X may add the process number as the first argument - ignore
118 if (argc
>argn
&& strncmp(argv
[1], "-psn_", 5)==0)
122 filter
->value(argv
[argn
]);
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
);
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");
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
);
159 window
->show(1, argv
);
168 extra_callback(Fl_Choice
*w
,void*)
171 if (0 == val
) fc
->add_extra(NULL
);
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");
179 fc
->add_extra(encodings
);
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...
201 // 'create_callback()' - Handle clicks on the create button.
205 create_callback(void)
207 fc
->type(fc
->type() ^ Fl_File_Chooser::CREATE
);
212 // 'dir_callback()' - Handle clicks on the directory button.
218 fc
->type(fc
->type() ^ Fl_File_Chooser::DIRECTORY
);
223 // 'fc_callback()' - Handle choices in the file chooser...
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.
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)
269 home
= getenv("HOME");
270 sprintf(preview
, "%s/.preview.ppm", home
? home
: "");
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
298 int page
; // Current page
299 char line
[256]; // Line from file
302 if (memcmp(header
, "%!", 2) != 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");
317 while (fgets(line
, sizeof(line
), in
) != NULL
) {
318 if (strncmp(line
, "%%Page:", 7) == 0) {
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';
336 "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
337 "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' \'%s\' 2>/dev/null",
340 if (system(command
)) return 0;
342 return new Fl_PNM_Image(preview
);
347 // 'show_callback()' - Show the file chooser...
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());
363 while (fc
->visible()) {
372 for (i
= 1; i
<= count
; i
++)
377 fl_filename_relative(relative
, sizeof(relative
), fc
->value(i
));
380 Fl_File_Icon::find(fc
->value(i
), Fl_File_Icon::PLAIN
));
389 // End of "$Id: file_chooser.cxx 8164 2011-01-01 20:17:58Z matt $".