2 // "$Id: nativefilechooser-simple-app.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
4 // An example of how to use Fl_Native_File_Chooser to open & save files.
6 // Copyright 2010 Greg Ercolano.
7 // Copyright 1998-2010 by Bill Spitzak and others.
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Library General Public License for more details.
19 // You should have received a copy of the GNU Library General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 // Please report all bugs and problems on the following page:
26 // http://www.fltk.org/str.php
28 #include <stdio.h> // printf
29 #include <stdlib.h> // exit,malloc
30 #include <string.h> // strerror
31 #include <errno.h> // errno
33 #include <FL/Fl_Window.H>
34 #include <FL/Fl_Menu_Bar.H>
35 #include <FL/Fl_Native_File_Chooser.H>
36 #include <FL/Fl_Box.H>
37 #include <FL/fl_ask.H>
39 class Application
: public Fl_Window
{
40 Fl_Native_File_Chooser
*fc
;
42 int exist(const char *filename
) {
43 FILE *fp
= fopen(filename
, "r");
44 if (fp
) { fclose(fp
); return(1); }
48 void open(const char *filename
) {
49 printf("Open '%s'\n", filename
);
52 // Create the file if it doesn't exist
53 // and save something in it.
55 void save(const char *filename
) {
56 printf("Saving '%s'\n", filename
);
57 if ( !exist(filename
) ) {
58 FILE *fp
= fopen(filename
, "w"); // create file if it doesn't exist
60 // A real app would do something useful here.
61 fprintf(fp
, "Hello world.\n");
64 fl_message("Error: %s: %s", filename
, strerror(errno
));
67 // A real app would do something useful here.
70 // Handle an 'Open' request from the menu
71 static void open_cb(Fl_Widget
*w
, void *v
) {
72 Application
*app
= (Application
*)v
;
73 app
->fc
->title("Open");
74 app
->fc
->type(Fl_Native_File_Chooser::BROWSE_FILE
); // only picks files that exist
75 switch ( app
->fc
->show() ) {
76 case -1: break; // Error
77 case 1: break; // Cancel
79 app
->fc
->preset_file(app
->fc
->filename());
80 app
->open(app
->fc
->filename());
84 // Handle a 'Save as' request from the menu
85 static void saveas_cb(Fl_Widget
*w
, void *v
) {
86 Application
*app
= (Application
*)v
;
87 app
->fc
->title("Save As");
88 app
->fc
->type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE
); // need this if file doesn't exist yet
89 switch ( app
->fc
->show() ) {
90 case -1: break; // Error
91 case 1: break; // Cancel
93 app
->fc
->preset_file(app
->fc
->filename());
94 app
->save(app
->fc
->filename());
98 // Handle a 'Save' request from the menu
99 static void save_cb(Fl_Widget
*w
, void *v
) {
100 Application
*app
= (Application
*)v
;
101 if ( strlen(app
->fc
->filename()) == 0 ) {
104 app
->save(app
->fc
->filename());
107 static void quit_cb(Fl_Widget
*w
, void *v
) {
110 // Return an 'untitled' default pathname
111 const char* untitled_default() {
112 static char *filename
= 0;
115 getenv("HOME") ? getenv("HOME") : // unix
116 getenv("HOME_PATH") ? getenv("HOME_PATH") : // windows
118 filename
= (char*)malloc(strlen(home
)+20);
119 sprintf(filename
, "%s/untitled.txt", home
);
125 Application() : Fl_Window(400,200,"Native File Chooser Example") {
126 Fl_Menu_Bar
*menu
= new Fl_Menu_Bar(0,0,400,25);
127 menu
->add("&File/&Open", FL_COMMAND
+'o', open_cb
, (void*)this);
128 menu
->add("&File/&Save", FL_COMMAND
+'s', save_cb
, (void*)this);
129 menu
->add("&File/&Save As", 0, saveas_cb
, (void*)this);
130 menu
->add("&File/&Quit", FL_COMMAND
+'q', quit_cb
);
131 // Describe the demo..
132 Fl_Box
*box
= new Fl_Box(20,25+20,w()-40,h()-40-25);
134 box
->box(FL_FLAT_BOX
);
135 box
->align(FL_ALIGN_CENTER
|FL_ALIGN_INSIDE
|FL_ALIGN_WRAP
);
136 box
->label("This demo shows an example of implementing "
137 "common 'File' menu operations like:\n"
138 " File/Open, File/Save, File/Save As\n"
139 "..using the Fl_Native_File_Chooser widget.\n\n"
140 "Note 'Save' and 'Save As' really *does* create files! "
141 "This is to show how behavior differs when "
142 "files exist vs. do not.");
144 // Initialize the file chooser
145 fc
= new Fl_Native_File_Chooser();
146 fc
->filter("Text\t*.txt\n");
147 fc
->preset_file(untitled_default());
152 int main(int argc
, char *argv
[]) {
154 Application
*app
= new Application();
155 app
->show(argc
,argv
);
160 // End of "$Id: nativefilechooser-simple-app.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".