1998-10-20 17:25:25 +04:00
|
|
|
//
|
2002-06-07 19:06:32 +04:00
|
|
|
// File chooser test program.
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1999-2010 by Michael Sweet.
|
2019-08-29 18:58:24 +03:00
|
|
|
// Copyright 2011-2019 by Bill Spitzak and others.
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2002-06-07 19:06:32 +04:00
|
|
|
// Contents:
|
|
|
|
//
|
2017-11-17 20:25:33 +03:00
|
|
|
// main() - Create a file chooser and wait for a selection to be made.
|
2002-06-07 19:06:32 +04:00
|
|
|
// 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.
|
2017-11-17 20:25:33 +03:00
|
|
|
// ps_check() - Check for and load the first page of a PostScript file.
|
2002-06-07 19:06:32 +04:00
|
|
|
// show_callback() - Show the file chooser...
|
|
|
|
//
|
2008-05-01 22:04:41 +04:00
|
|
|
// extra_callback() - circle extra groups (none,group1,check_button);
|
|
|
|
//
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Include necessary headers...
|
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
#include <stdio.h>
|
2001-09-30 21:37:06 +04:00
|
|
|
#include <FL/Fl_File_Chooser.H>
|
2001-09-29 18:38:59 +04:00
|
|
|
#include <FL/Fl_File_Icon.H>
|
2002-06-07 19:06:32 +04:00
|
|
|
#include <FL/Fl_Shared_Image.H>
|
|
|
|
#include <FL/Fl_PNM_Image.H>
|
2002-07-14 22:19:00 +04:00
|
|
|
#include <FL/Fl_Light_Button.H>
|
2009-04-21 13:09:37 +04:00
|
|
|
#include <FL/Fl_Double_Window.H>
|
2017-10-17 03:28:56 +03:00
|
|
|
#include <FL/Fl_Simple_Terminal.H>
|
2005-04-16 04:01:49 +04:00
|
|
|
#include <string.h>
|
2020-07-12 00:46:34 +03:00
|
|
|
#include <locale.h> // setlocale()..
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2017-10-17 03:28:56 +03:00
|
|
|
#define TERMINAL_HEIGHT 120
|
|
|
|
#define TERMINAL_GREEN "\033[32m"
|
|
|
|
#define TERMINAL_NORMAL "\033[0m"
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
//
|
|
|
|
// Globals...
|
|
|
|
//
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Input *filter;
|
|
|
|
Fl_File_Browser *files;
|
|
|
|
Fl_File_Chooser *fc;
|
|
|
|
Fl_Shared_Image *image = 0;
|
|
|
|
Fl_Simple_Terminal *tty = 0;
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2008-05-01 22:04:41 +04:00
|
|
|
// for choosing extra groups
|
|
|
|
Fl_Choice *ch_extra;
|
|
|
|
// first extra group
|
|
|
|
Fl_Group *encodings = (Fl_Group*)0;
|
|
|
|
Fl_Choice *ch_enc;
|
|
|
|
// second extra widget
|
|
|
|
Fl_Check_Button *version = (Fl_Check_Button*)0;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
//
|
|
|
|
// Functions...
|
|
|
|
//
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
void close_callback(void);
|
|
|
|
void create_callback(void);
|
|
|
|
void dir_callback(void);
|
|
|
|
void fc_callback(Fl_File_Chooser *, void *);
|
|
|
|
void multi_callback(void);
|
|
|
|
Fl_Image *pdf_check(const char *, uchar *, int);
|
|
|
|
Fl_Image *ps_check(const char *, uchar *, int);
|
|
|
|
void show_callback(void);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
void extra_callback(Fl_Choice*,void*);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// 'main()' - Create a file chooser and wait for a selection to be made.
|
|
|
|
//
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
int // O - Exit status
|
|
|
|
main(int argc, // I - Number of command-line arguments
|
|
|
|
char *argv[]) // I - Command-line arguments
|
2002-06-07 19:06:32 +04:00
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Double_Window *window;// Main window
|
|
|
|
Fl_Button *button;// Buttons
|
2018-10-29 22:59:57 +03:00
|
|
|
Fl_Group *grp; // Groups
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_File_Icon *icon; // New file icon
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Make the file chooser...
|
2020-07-12 00:46:34 +03:00
|
|
|
setlocale(LC_ALL, ""); // enable multilanguage errors in file chooser
|
2002-06-07 19:06:32 +04:00
|
|
|
Fl::scheme(NULL);
|
2001-09-29 18:38:59 +04:00
|
|
|
Fl_File_Icon::load_system_icons();
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2002-07-14 22:19:00 +04:00
|
|
|
fc = new Fl_File_Chooser(".", "*", Fl_File_Chooser::SINGLE, "Fl_File_Chooser Test");
|
2002-06-07 19:06:32 +04:00
|
|
|
fc->callback(fc_callback);
|
|
|
|
|
|
|
|
// 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...
|
2017-10-17 03:28:56 +03:00
|
|
|
window = new Fl_Double_Window(400, 215+TERMINAL_HEIGHT, "File Chooser Test");
|
|
|
|
|
|
|
|
tty = new Fl_Simple_Terminal(0,215,window->w(),TERMINAL_HEIGHT);
|
|
|
|
tty->ansi(true);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2018-10-29 22:59:57 +03:00
|
|
|
// Group: limit resizing to filter input (not browse button)
|
|
|
|
grp = new Fl_Group(0,10,400,25);
|
|
|
|
grp->begin();
|
|
|
|
{
|
|
|
|
filter = new Fl_Input(50, 10, 315, 25, "Filter:");
|
|
|
|
// Process standard arguments and find filter argument if present
|
|
|
|
int argn = 1;
|
|
|
|
while (argn < argc) {
|
|
|
|
if (Fl::arg(argc, argv, argn) == 0) break;
|
|
|
|
}
|
|
|
|
if (argc > argn)
|
|
|
|
filter->value(argv[argn]);
|
|
|
|
else
|
|
|
|
filter->value("PDF Files (*.pdf)\t"
|
|
|
|
"PostScript Files (*.ps)\t"
|
2020-07-01 19:03:10 +03:00
|
|
|
"Image Files (*.{bmp,gif,jpg,png})\t"
|
|
|
|
"C/C++ Source Files (*.{c,C,cc,cpp,cxx})");
|
2018-10-29 22:59:57 +03:00
|
|
|
|
|
|
|
button = new Fl_Button(365, 10, 25, 25);
|
|
|
|
button->tooltip("Click to open file browser..");
|
|
|
|
button->callback((Fl_Callback *)show_callback);
|
|
|
|
if ( (icon = Fl_File_Icon::find(".", Fl_File_Icon::DIRECTORY)) ) {
|
|
|
|
// Icon found; assign it..
|
|
|
|
button->labelcolor(FL_YELLOW);
|
|
|
|
icon->label(button);
|
|
|
|
} else {
|
|
|
|
// Fallback if no icon found
|
|
|
|
button->label("..");
|
|
|
|
}
|
2016-10-06 11:55:22 +03:00
|
|
|
}
|
2018-10-29 22:59:57 +03:00
|
|
|
grp->end();
|
|
|
|
grp->resizable(filter);
|
|
|
|
|
|
|
|
// Group: prevent resizing of the light buttons
|
|
|
|
grp = new Fl_Group(0,45,400,55);
|
|
|
|
grp->begin();
|
|
|
|
{
|
|
|
|
button = new Fl_Light_Button(50, 45, 80, 25, "MULTI");
|
|
|
|
button->callback((Fl_Callback *)multi_callback);
|
|
|
|
|
|
|
|
button = new Fl_Light_Button(140, 45, 90, 25, "CREATE");
|
|
|
|
button->callback((Fl_Callback *)create_callback);
|
|
|
|
|
|
|
|
button = new Fl_Light_Button(240, 45, 115, 25, "DIRECTORY");
|
|
|
|
button->callback((Fl_Callback *)dir_callback);
|
|
|
|
|
|
|
|
//
|
|
|
|
ch_extra = new Fl_Choice(150, 75, 150, 25, "Extra Group:");
|
|
|
|
ch_extra->add("none|encodings group|check button");
|
|
|
|
ch_extra->value(0);
|
|
|
|
ch_extra->callback((Fl_Callback *)extra_callback);
|
2018-10-29 22:33:11 +03:00
|
|
|
}
|
2018-10-29 22:59:57 +03:00
|
|
|
grp->end();
|
|
|
|
grp->resizable(0);
|
2008-05-01 22:04:41 +04:00
|
|
|
//
|
|
|
|
files = new Fl_File_Browser(50, 105, 340, 75, "Files:");
|
2002-06-07 19:06:32 +04:00
|
|
|
files->align(FL_ALIGN_LEFT);
|
|
|
|
|
2018-10-29 22:59:57 +03:00
|
|
|
// Prevent resizing close button, but keep at right edge of scrn
|
|
|
|
grp = new Fl_Group(0,185,400,25);
|
|
|
|
grp->begin();
|
|
|
|
{
|
|
|
|
Fl_Box *invis = new Fl_Box(100,185,1,1);
|
|
|
|
invis->box(FL_NO_BOX);
|
|
|
|
button = new Fl_Button(310, 185, 80, 25, "Close");
|
|
|
|
button->callback((Fl_Callback *)close_callback);
|
|
|
|
grp->resizable(invis);
|
|
|
|
}
|
|
|
|
grp->end();
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2002-07-14 22:19:00 +04:00
|
|
|
window->resizable(files);
|
2002-06-07 19:06:32 +04:00
|
|
|
window->end();
|
2005-07-12 00:31:59 +04:00
|
|
|
window->show(1, argv);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
Fl::run();
|
|
|
|
|
|
|
|
return (0);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1998-10-20 17:25:25 +04:00
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2008-05-01 22:04:41 +04:00
|
|
|
void
|
|
|
|
extra_callback(Fl_Choice*w,void*)
|
|
|
|
{
|
|
|
|
int val=w->value();
|
|
|
|
if (0 == val) fc->add_extra(NULL);
|
|
|
|
else if (1 == val) {
|
|
|
|
if(!encodings){
|
|
|
|
encodings=new Fl_Group(0,0,254,30);
|
|
|
|
ch_enc=new Fl_Choice(152,2,100,25,"Choose Encoding:");
|
|
|
|
ch_enc->add("ASCII|Koi8-r|win1251|Utf-8");
|
|
|
|
encodings->end();
|
|
|
|
}
|
|
|
|
fc->add_extra(encodings);
|
|
|
|
} else {
|
|
|
|
if (!version) {
|
|
|
|
version = new Fl_Check_Button(5,0,200,25,"Save binary 1.0 version");
|
|
|
|
}
|
|
|
|
fc->add_extra(version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
//
|
|
|
|
// 'close_callback()' - Close the main window...
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
close_callback(void)
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-14 22:19:00 +04:00
|
|
|
//
|
|
|
|
// 'create_callback()' - Handle clicks on the create button.
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
create_callback(void)
|
|
|
|
{
|
|
|
|
fc->type(fc->type() ^ Fl_File_Chooser::CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 'dir_callback()' - Handle clicks on the directory button.
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
dir_callback(void)
|
|
|
|
{
|
|
|
|
fc->type(fc->type() ^ Fl_File_Chooser::DIRECTORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
//
|
|
|
|
// 'fc_callback()' - Handle choices in the file chooser...
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2020-07-01 19:03:10 +03:00
|
|
|
fc_callback(Fl_File_Chooser *fc, // I - File chooser
|
|
|
|
void *data) // I - Data
|
2002-06-07 19:06:32 +04:00
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
const char *filename; // Current filename
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
|
2017-10-17 03:28:56 +03:00
|
|
|
tty->printf("fc_callback(fc = %p, data = %p)\n", fc, data);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
filename = fc->value();
|
|
|
|
|
2017-10-17 03:28:56 +03:00
|
|
|
tty->printf(" filename = \"%s\"\n", filename ? filename : "(null)");
|
2002-06-07 19:06:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-14 22:19:00 +04:00
|
|
|
//
|
|
|
|
// 'multi_callback()' - Handle clicks on the multi button.
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
multi_callback(void)
|
|
|
|
{
|
|
|
|
fc->type(fc->type() ^ Fl_File_Chooser::MULTI);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-07 19:06:32 +04:00
|
|
|
//
|
|
|
|
// 'pdf_check()' - Check for and load the first page of a PDF file.
|
|
|
|
//
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Image * // O - Page image or NULL
|
|
|
|
pdf_check(const char *name, // I - Name of file
|
|
|
|
uchar *header, // I - Header data
|
|
|
|
int) // I - Length of header data (unused)
|
2002-06-07 19:06:32 +04:00
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
const char *home; // Home directory
|
|
|
|
char preview[FL_PATH_MAX], // Preview filename
|
|
|
|
command[3 * FL_PATH_MAX]; // Command
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
if (memcmp(header, "%PDF", 4) != 0)
|
|
|
|
return 0;
|
|
|
|
|
2017-10-13 17:58:30 +03:00
|
|
|
home = fl_getenv("HOME");
|
2005-04-16 04:01:49 +04:00
|
|
|
sprintf(preview, "%s/.preview.ppm", home ? home : "");
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2005-04-16 04:01:49 +04:00
|
|
|
sprintf(command,
|
|
|
|
"gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
|
2020-07-01 19:03:10 +03:00
|
|
|
"-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' "
|
|
|
|
"-dFirstPage=1 -dLastPage=1 \'%s\' 2>/dev/null", preview, name);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
if (system(command)) return 0;
|
|
|
|
|
|
|
|
return new Fl_PNM_Image(preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 'ps_check()' - Check for and load the first page of a PostScript file.
|
|
|
|
//
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Image * // O - Page image or NULL
|
|
|
|
ps_check(const char *name, // I - Name of file
|
|
|
|
uchar *header, // I - Header data
|
|
|
|
int) // I - Length of header data (unused)
|
2002-06-07 19:06:32 +04:00
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
const char *home; // Home directory
|
|
|
|
char preview[FL_PATH_MAX], // Preview filename
|
|
|
|
outname[FL_PATH_MAX], // Preview PS file
|
|
|
|
command[3 * FL_PATH_MAX]; // Command
|
|
|
|
FILE *in, // Input file
|
|
|
|
*out; // Output file
|
|
|
|
int page; // Current page
|
|
|
|
char line[256]; // Line from file
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
if (memcmp(header, "%!", 2) != 0)
|
|
|
|
return 0;
|
|
|
|
|
2017-11-17 20:25:33 +03:00
|
|
|
home = fl_getenv("HOME");
|
2005-04-16 04:01:49 +04:00
|
|
|
sprintf(preview, "%s/.preview.ppm", home ? home : "");
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
if (memcmp(header, "%!PS", 4) == 0) {
|
|
|
|
// PS file has DSC comments; extract the first page...
|
2005-04-16 04:01:49 +04:00
|
|
|
sprintf(outname, "%s/.preview.ps", home ? home : "");
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
if (strcmp(name, outname) != 0) {
|
2010-11-18 23:00:01 +03:00
|
|
|
in = fl_fopen(name, "rb");
|
|
|
|
out = fl_fopen(outname, "wb");
|
2002-06-07 19:06:32 +04:00
|
|
|
page = 0;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), in) != NULL) {
|
2020-07-01 19:03:10 +03:00
|
|
|
if (strncmp(line, "%%Page:", 7) == 0) {
|
2002-06-07 19:06:32 +04:00
|
|
|
page ++;
|
2020-07-01 19:03:10 +03:00
|
|
|
if (page > 1) break;
|
|
|
|
}
|
2002-06-07 19:06:32 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
fputs(line, out);
|
2002-06-07 19:06:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// PS file doesn't have DSC comments; do the whole file...
|
2005-04-16 04:01:49 +04:00
|
|
|
strncpy(outname, name, sizeof(outname) - 1);
|
|
|
|
outname[sizeof(outname) - 1] = '\0';
|
2002-06-07 19:06:32 +04:00
|
|
|
}
|
|
|
|
|
2005-04-16 04:01:49 +04:00
|
|
|
sprintf(command,
|
|
|
|
"gs -r100 -dFIXED -sDEVICE=ppmraw -dQUIET -dNOPAUSE -dBATCH "
|
2020-07-01 19:03:10 +03:00
|
|
|
"-sstdout=\"%%stderr\" -sOUTPUTFILE=\'%s\' \'%s\' 2>/dev/null",
|
|
|
|
preview, outname);
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
if (system(command)) return 0;
|
|
|
|
|
|
|
|
return new Fl_PNM_Image(preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 'show_callback()' - Show the file chooser...
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
show_callback(void)
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
int i; // Looping var
|
|
|
|
int count; // Number of files selected
|
|
|
|
char relative[FL_PATH_MAX]; // Relative filename
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
if (filter->value()[0])
|
|
|
|
fc->filter(filter->value());
|
|
|
|
|
|
|
|
fc->show();
|
|
|
|
|
2008-04-12 23:16:19 +04:00
|
|
|
while (fc->visible()) {
|
2002-06-07 19:06:32 +04:00
|
|
|
Fl::wait();
|
2008-04-12 23:16:19 +04:00
|
|
|
}
|
2002-06-07 19:06:32 +04:00
|
|
|
|
|
|
|
count = fc->count();
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
files->clear();
|
|
|
|
|
|
|
|
for (i = 1; i <= count; i ++)
|
|
|
|
{
|
2018-10-29 21:33:37 +03:00
|
|
|
if (!fc->value(i)) break;
|
2002-06-07 19:06:32 +04:00
|
|
|
fl_filename_relative(relative, sizeof(relative), fc->value(i));
|
2020-07-01 19:03:10 +03:00
|
|
|
tty->printf("%d/%d) %sPicked: '%s'\n Relative: '%s'%s\n", i, count,
|
2018-10-29 21:33:37 +03:00
|
|
|
TERMINAL_GREEN, fc->value(i), relative, TERMINAL_NORMAL);
|
|
|
|
files->add(relative, Fl_File_Icon::find(fc->value(i), Fl_File_Icon::PLAIN));
|
2002-06-07 19:06:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
files->redraw();
|
|
|
|
}
|
|
|
|
}
|