diff options
Diffstat (limited to 'test/file_chooser.cxx')
| -rw-r--r-- | test/file_chooser.cxx | 290 |
1 files changed, 262 insertions, 28 deletions
diff --git a/test/file_chooser.cxx b/test/file_chooser.cxx index 3637250b8..2da5133f0 100644 --- a/test/file_chooser.cxx +++ b/test/file_chooser.cxx @@ -1,9 +1,9 @@ // -// "$Id: file_chooser.cxx,v 1.4.2.3.2.4 2002/01/01 15:11:32 easysw Exp $" +// "$Id: file_chooser.cxx,v 1.4.2.3.2.5 2002/06/07 15:06:32 easysw Exp $" // -// File chooser test program for the Fast Light Tool Kit (FLTK). +// File chooser test program. // -// Copyright 1998-2002 by Bill Spitzak and others. +// Copyright 1999-2002 by Michael Sweet. // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public @@ -22,39 +22,273 @@ // // Please report all bugs and problems to "fltk-bugs@fltk.org". // +// Contents: +// +// main() - Create a file chooser and wait for a selection to +// be made. +// close_callback() - Close the main window... +// fc_callback() - Handle choices in the file chooser... +// pdf_check() - Check for and load the first page of a PDF file. +// ps_check() - Check for and load the first page of a PostScript +// file. +// show_callback() - Show the file chooser... +// + +// +// Include necessary headers... +// -#include <FL/Fl.H> -#include <FL/Fl_Button.H> -#include <FL/Fl_Window.H> -#include <FL/Fl_Input.H> +#include <stdio.h> #include <FL/Fl_File_Chooser.H> #include <FL/Fl_File_Icon.H> -#include <stdio.h> -#include <string.h> -#include <errno.h> -#include <stdlib.h> +#include <FL/Fl_Shared_Image.H> +#include <FL/Fl_PNM_Image.H> +#include "../src/flstring.h" -Fl_Input *pattern, *current; -void pickfile(Fl_Widget *) { - const char *p; - p = fl_file_chooser("Pick a file",pattern->value(),current->value()); - if (p) current->value(p); -} +// +// Globals... +// + +Fl_Input *filter; +Fl_File_Browser *files; +Fl_File_Chooser *fc; +Fl_Shared_Image *image = 0; + -int main(int argc, char **argv) { +// +// Functions... +// + +void close_callback(void); +void fc_callback(Fl_File_Chooser *, void *); +Fl_Image *pdf_check(const char *, uchar *, int); +Fl_Image *ps_check(const char *, uchar *, int); +void show_callback(void); + + +// +// 'main()' - Create a file chooser and wait for a selection to be made. +// + +int // O - Exit status +main(int argc, // I - Number of command-line arguments + char *argv[]) // I - Command-line arguments +{ + Fl_Window *window;// Main window + Fl_Button *button;// Buttons + Fl_File_Icon *icon; // New file icon + + + // Make the file chooser... + Fl::scheme(NULL); Fl_File_Icon::load_system_icons(); - Fl_Window window(310,110); - pattern = new Fl_Input(100,10,200,25,"Pattern:"); - pattern->static_value("*"); - current = new Fl_Input(100,40,200,25,"Current:"); - Fl_Button button(200,75,100,25,"&Choose file"); - button.callback(pickfile); - window.end(); - window.show(argc, argv); - return Fl::run(); + + fc = new Fl_File_Chooser(".", "*", Fl_File_Chooser::MULTI, "Fl_File_Chooser Test"); + fc->callback(fc_callback); +// fc->type(Fl_File_Chooser::MULTI); +// fc->color((Fl_Color)196); + + // Register the PS and PDF image types... + Fl_Shared_Image::add_handler(pdf_check); + Fl_Shared_Image::add_handler(ps_check); + + // Make the main window... + window = new Fl_Window(400, 200, "File Chooser Test"); + + filter = new Fl_Input(50, 10, 315, 25, "Filter:"); + if (argc > 1) + filter->value(argv[1]); + else + filter->value("PDF Files (*.pdf)\t" + "PostScript Files (*.ps)\t" + "Image Files (*.{bmp,gif,jpg,png})\t" + "C/C++ Source Files (*.{c,C,cc,cpp,cxx})"); + + button = new Fl_Button(365, 10, 25, 25); + button->labelcolor(FL_YELLOW); + button->callback((Fl_Callback *)show_callback); + + icon = Fl_File_Icon::find(".", Fl_File_Icon::DIRECTORY); + icon->label(button); + + files = new Fl_File_Browser(50, 45, 340, 110, "Files:"); + files->align(FL_ALIGN_LEFT); + + button = new Fl_Button(340, 165, 50, 25, "Close"); + button->callback((Fl_Callback *)close_callback); + + window->end(); + window->show(); + + Fl::run(); + + return (0); } + +// +// 'close_callback()' - Close the main window... +// + +void +close_callback(void) +{ + exit(0); +} + + +// +// 'fc_callback()' - Handle choices in the file chooser... +// + +void +fc_callback(Fl_File_Chooser *fc, // I - File chooser + void *data) // I - Data +{ + const char *filename; // Current filename + + + printf("fc_callback(fc = %p, data = %p)\n", fc, data); + + filename = fc->value(); + + printf(" filename = \"%s\"\n", filename ? filename : "(null)"); +} + + +// +// 'pdf_check()' - Check for and load the first page of a PDF file. +// + +Fl_Image * // O - Page image or NULL +pdf_check(const char *name, // I - Name of file + uchar *header, // I - Header data + int headerlen) // I - Length of header data +{ + const char *home; // Home directory + char preview[1024], // Preview filename + command[1024]; // Command + + + if (memcmp(header, "%PDF", 4) != 0) + return 0; + + home = getenv("HOME"); + snprintf(preview, sizeof(preview), "%s/.preview.ppm", home ? home : ""); + + snprintf(command, sizeof(command), + "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH " + "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' " + "-dFirstPage=1 -dLastPage=1 \'%s\' 2>/dev/null", preview, name); + + if (system(command)) return 0; + + return new Fl_PNM_Image(preview); +} + + +// +// 'ps_check()' - Check for and load the first page of a PostScript file. +// + +Fl_Image * // O - Page image or NULL +ps_check(const char *name, // I - Name of file + uchar *header, // I - Header data + int headerlen) // I - Length of header data +{ + const char *home; // Home directory + char preview[1024], // Preview filename + outname[1024], // Preview PS file + command[1024]; // Command + FILE *in, // Input file + *out; // Output file + int page; // Current page + char line[256]; // Line from file + + + if (memcmp(header, "%!", 2) != 0) + return 0; + + home = getenv("HOME"); + snprintf(preview, sizeof(preview), "%s/.preview.ppm", home ? home : ""); + + if (memcmp(header, "%!PS", 4) == 0) { + // PS file has DSC comments; extract the first page... + snprintf(outname, sizeof(outname), "%s/.preview.ps", home ? home : ""); + + if (strcmp(name, outname) != 0) { + in = fopen(name, "rb"); + out = fopen(outname, "wb"); + page = 0; + + while (fgets(line, sizeof(line), in) != NULL) { + if (strncmp(line, "%%Page:", 7) == 0) { + page ++; + if (page > 1) break; + } + + fputs(line, out); + } + + fclose(in); + fclose(out); + } + } else { + // PS file doesn't have DSC comments; do the whole file... + strlcpy(outname, name, sizeof(outname)); + } + + snprintf(command, sizeof(command), + "gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH " + "-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' \'%s\' 2>/dev/null", + preview, outname); + + if (system(command)) return 0; + + return new Fl_PNM_Image(preview); +} + + +// +// 'show_callback()' - Show the file chooser... +// + +void +show_callback(void) +{ + int i; // Looping var + int count; // Number of files selected + char relative[1024]; // Relative filename + + + fc->show(); + if (filter->value()[0]) + fc->filter(filter->value()); + + fc->show(); + + while (fc->visible()) + Fl::wait(); + + count = fc->count(); + if (count > 0) + { + files->clear(); + + for (i = 1; i <= count; i ++) + { + fl_filename_relative(relative, sizeof(relative), fc->value(i)); + + files->add(relative, + Fl_File_Icon::find(fc->value(i), Fl_File_Icon::PLAIN)); + } + + files->redraw(); + } +} + + // -// End of "$Id: file_chooser.cxx,v 1.4.2.3.2.4 2002/01/01 15:11:32 easysw Exp $". +// End of "$Id: file_chooser.cxx,v 1.4.2.3.2.5 2002/06/07 15:06:32 easysw Exp $". // |
