fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_file_dir.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // File chooser widget for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2015 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include "flstring.h"
20 #include <FL/filename.H>
21 #include <FL/Fl_File_Chooser.H>
22 #include <FL/fl_ask.H>
23 
24 
26 static void (*current_callback)(const char*) = 0;
27 static const char *current_label = fl_ok;
28 
29 
30 // Do a file chooser callback...
31 static void callback(Fl_File_Chooser *, void*) {
32  if (current_callback && fc->value())
33  (*current_callback)(fc->value());
34 }
35 
36 // Pop up a file chooser dialog window and wait until it is closed...
37 static void popup(Fl_File_Chooser *fc) {
38  fc->show();
39 
40  // deactivate Fl::grab(), because it is incompatible with modal windows
41  Fl_Window* g = Fl::grab();
42  if (g) Fl::grab(0);
43 
44  while (fc->shown())
45  Fl::wait();
46 
47  if (g) // regrab the previous popup menu, if there was one
48  Fl::grab(g);
49 }
50 
51 
60 void fl_file_chooser_callback(void (*cb)(const char*)) {
62 }
63 
64 
70 void fl_file_chooser_ok_label(const char *l) {
71  if (l) current_label = l;
72  else current_label = fl_ok;
73 }
74 
88 char * // O - Filename or NULL
89 fl_file_chooser(const char *message, // I - Message in titlebar
90  const char *pat, // I - Filename pattern
91  const char *fname, // I - Initial filename selection
92  int relative) { // I - 0 for absolute path
93  static char retname[FL_PATH_MAX]; // Returned filename
94 
95  if (!fc) {
96  if (!fname || !*fname) fname = ".";
97 
99  fc->callback(callback, 0);
100  } else {
102  // see, if we use the same pattern between calls
103  char same_pattern = 0;
104  const char *fcf = fc->filter();
105  if ( fcf && pat && strcmp(fcf, pat)==0)
106  same_pattern = 1;
107  else if ( (fcf==0L || *fcf==0) && (pat==0L || *pat==0) )
108  same_pattern = 1;
109  // now set the pattern to the new pattern (even if they are the same)
110  fc->filter(pat);
111  fc->label(message);
112 
113  if (!fname) { // null pointer reuses same filename if pattern didn't change
114  if (!same_pattern && fc->value()) {
115  // if pattern is different, remove name but leave old directory:
116  strlcpy(retname, fc->value(), sizeof(retname));
117 
118  char *p = strrchr(retname, '/');
119 
120  if (p) {
121  // If the filename is "/foo", then the directory will be "/", not
122  // ""...
123  if (p == retname)
124  retname[1] = '\0';
125  else
126  *p = '\0';
127  }
128  // Set the directory...
129  fc->value(retname);
130  } else {
131  // re-use the previously selected name
132  }
133  } else if (!*fname) { // empty filename reuses directory with empty name
134  const char *fcv = fc->value();
135  if (fcv)
136  strlcpy(retname, fc->value(), sizeof(retname));
137  else
138  *retname = 0;
139  const char *n = fl_filename_name(retname);
140  if (n) *((char*)n) = 0;
141  fc->value("");
142  fc->directory(retname);
143  } else {
144  fc->value(fname);
145  }
146  }
147 
149  popup(fc);
150  if (fc->value() && relative) {
151  fl_filename_relative(retname, sizeof(retname), fc->value());
152 
153  return retname;
154  } else if (fc->value()) return (char *)fc->value();
155  else return 0;
156 }
157 
166 char * // O - Directory or NULL
167 fl_dir_chooser(const char *message, // I - Message for titlebar
168  const char *fname, // I - Initial directory name
169  int relative) // I - 0 for absolute
170 {
171  static char retname[FL_PATH_MAX]; // Returned directory name
172 
173  if (!fc) {
174  if (!fname || !*fname) fname = ".";
175 
178  fc->callback(callback, 0);
179  } else {
181  fc->filter("*");
182  if (fname && *fname) fc->value(fname);
183  fc->label(message);
184  }
185 
186  popup(fc);
187 
188  if (fc->value() && relative) {
189  fl_filename_relative(retname, sizeof(retname), fc->value());
190 
191  return retname;
192  } else if (fc->value()) return (char *)fc->value();
193  else return 0;
194 }
198 //
199 // End of "$Id$".
200 //
fl_filename_relative
int fl_filename_relative(char *to, int tolen, const char *from)
Definition: filename_absolute.cxx:128
Fl_File_Chooser::fl_file_chooser_ok_label
void fl_file_chooser_ok_label(const char *l)
Definition: fl_file_dir.cxx:70
Fl_File_Chooser::fl_file_chooser
char * fl_file_chooser(const char *message, const char *pat, const char *fname, int relative)
Definition: fl_file_dir.cxx:89
Fl_File_Chooser::CREATE
Definition: Fl_File_Chooser.H:50
fl_ask.H
relative
static Fl_Button * relative(Fl_Widget *o, int i)
Definition: Fl_Widget_Type.cxx:1129
message
static Fl_Box * message
Definition: fl_ask.cxx:47
callback
static void callback(Fl_File_Chooser *, void *)
Definition: fl_file_dir.cxx:31
fc
static Fl_File_Chooser * fc
Definition: fl_file_dir.cxx:25
filename.H
Fl_File_Chooser::show
void show()
Definition: Fl_File_Chooser2.cxx:1597
Fl_File_Chooser::label
void label(const char *l)
Definition: Fl_File_Chooser.cxx:369
Fl_File_Chooser::value
const char * value(int f=1)
Definition: Fl_File_Chooser2.cxx:1477
Fl_File_Chooser::callback
void callback(void(*cb)(Fl_File_Chooser *, void *), void *d=0)
Definition: Fl_File_Chooser.cxx:327
Fl_File_Chooser::filter
void filter(const char *p)
Definition: Fl_File_Chooser2.cxx:1010
Fl_File_Chooser::fl_dir_chooser
char * fl_dir_chooser(const char *message, const char *fname, int relative)
Definition: fl_file_dir.cxx:167
p
static menustate * p
Definition: Fl_Menu.cxx:606
current_label
static const char * current_label
Definition: fl_file_dir.cxx:27
FL_PATH_MAX
#define FL_PATH_MAX
Definition: filename.H:38
fname
static const char * fname
Definition: file.cxx:128
fl_filename_name
const char * fl_filename_name(const char *filename)
Definition: Fl_win32.cxx:2179
strlcpy
#define strlcpy
Definition: flstring.h:84
Fl_File_Chooser::shown
int shown()
Definition: Fl_File_Chooser.cxx:390
Fl_Window
Definition: Fl_Window.H:57
fl_ok
const char * fl_ok
string pointer used in common dialogs, you can change it to another language
Definition: fl_ask.cxx:273
Fl_File_Chooser.H
cb
static void cb(Fl_Widget *, void *v)
Definition: factory.cxx:937
Fl_File_Chooser::DIRECTORY
Definition: Fl_File_Chooser.H:50
Fl_File_Chooser::fl_file_chooser_callback
void fl_file_chooser_callback(void(*cb)(const char *))
Definition: fl_file_dir.cxx:60
Fl_File_Chooser
Definition: Fl_File_Chooser.H:48
Fl_File_Chooser::ok_label
void ok_label(const char *l)
Definition: Fl_File_Chooser.cxx:377
Fl_File_Chooser::directory
void directory(const char *d)
Definition: Fl_File_Chooser2.cxx:458
Fl::wait
static int wait()
Definition: Fl.cxx:666
popup
static void popup(Fl_File_Chooser *fc)
Definition: fl_file_dir.cxx:37
flstring.h
current_callback
static void(* current_callback)(const char *)=0
Definition: fl_file_dir.cxx:26
Fl_File_Chooser::type
void type(int t)
Definition: Fl_File_Chooser.cxx:418
Fl::grab
static Fl_Window * grab()
Definition: Fl.H:555