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

Fl_File_Input.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_File_Input.cxx 8063 2010-12-19 21:20:10Z matt $"
00003 //
00004 // File_Input header file for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 // Original version Copyright 1998 by Curtis Edwards.
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Library General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Library General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022 // USA.
00023 //
00024 // Please report all bugs and problems on the following page:
00025 //
00026 //     http://www.fltk.org/str.php
00027 //
00028 
00029 #include <FL/Fl.H>
00030 #include <FL/Fl_File_Input.H>
00031 #include <FL/Fl_Window.H>
00032 #include <FL/fl_draw.H>
00033 #include <FL/filename.H>
00034 #include <stdio.h>
00035 #include "flstring.h"
00036 
00037 
00038 //
00039 // Height of directory buttons...
00040 //
00041 
00042 #define DIR_HEIGHT      10
00043 
00044 
00045 //
00046 // Redraw bit for directory bar...
00047 //
00048 
00049 #define FL_DAMAGE_BAR   0x10
00050 
00051 
00058 Fl_File_Input::Fl_File_Input(int X, int Y, int W, int H, const char *L)
00059   : Fl_Input(X, Y, W, H, L) {
00060   buttons_[0] = 0;
00061   errorcolor_ = FL_RED;
00062   ok_entry_   = 1;
00063   pressed_    = -1;
00064 
00065   down_box(FL_UP_BOX);
00066 }
00067 
00071 void Fl_File_Input::draw_buttons() {
00072   int   i,                                      // Looping var
00073         X;                                      // Current X position
00074 
00075 
00076   if (damage() & (FL_DAMAGE_BAR | FL_DAMAGE_ALL)) {
00077     update_buttons();
00078   }
00079 
00080   for (X = 0, i = 0; buttons_[i]; i ++)
00081   {
00082     if ((X + buttons_[i]) > xscroll()) {
00083       if (X < xscroll()) {
00084         draw_box(pressed_ == i ? fl_down(down_box()) : down_box(),
00085                  x(), y(), X + buttons_[i] - xscroll(), DIR_HEIGHT, FL_GRAY);
00086       } else if ((X + buttons_[i] - xscroll()) > w()) {
00087         draw_box(pressed_ == i ? fl_down(down_box()) : down_box(),
00088                  x() + X - xscroll(), y(), w() - X + xscroll(), DIR_HEIGHT,
00089                  FL_GRAY);
00090       } else {
00091         draw_box(pressed_ == i ? fl_down(down_box()) : down_box(),
00092                  x() + X - xscroll(), y(), buttons_[i], DIR_HEIGHT, FL_GRAY);
00093       }
00094     }
00095 
00096     X += buttons_[i];
00097   }
00098 
00099   if (X < w()) {
00100     draw_box(pressed_ == i ? fl_down(down_box()) : down_box(),
00101              x() + X - xscroll(), y(), w() - X + xscroll(), DIR_HEIGHT, FL_GRAY);
00102   }
00103 }
00104 
00108 void Fl_File_Input::update_buttons() {
00109   int           i;                              // Looping var
00110   const char    *start,                         // Start of path component
00111                 *end;                           // End of path component
00112 
00113 
00114 //  puts("update_buttons()");
00115 
00116   // Set the current font & size...
00117   fl_font(textfont(), textsize());
00118 
00119   // Loop through the value string, setting widths...
00120   for (i = 0, start = value();
00121        start && i < (int)(sizeof(buttons_) / sizeof(buttons_[0]) - 1);
00122        start = end, i ++) {
00123 //    printf("    start = \"%s\"\n", start);
00124     if ((end = strchr(start, '/')) == NULL)
00125 #if defined(WIN32) || defined(__EMX__)
00126       if ((end = strchr(start, '\\')) == NULL)
00127 #endif // WIN32 || __EMX__
00128       break;
00129 
00130     end ++;
00131 
00132     buttons_[i] = (short)fl_width(start, end - start);
00133     if (!i) buttons_[i] += Fl::box_dx(box()) + 6;
00134   }
00135 
00136 //  printf("    found %d components/buttons...\n", i);
00137 
00138   buttons_[i] = 0;
00139 }
00140 
00141 
00148 int                                             // O - TRUE on success
00149 Fl_File_Input::value(const char *str,           // I - New string value
00150                      int        len) {          // I - Length of value
00151   damage(FL_DAMAGE_BAR);
00152   return Fl_Input::value(str,len);
00153 }
00154 
00155 
00161 int                                             // O - TRUE on success
00162 Fl_File_Input::value(const char *str) {         // I - New string value
00163   damage(FL_DAMAGE_BAR);
00164   return Fl_Input::value(str);
00165 }
00166 
00167 
00171 void Fl_File_Input::draw() {
00172   Fl_Boxtype b = box();
00173   if (damage() & (FL_DAMAGE_BAR | FL_DAMAGE_ALL)) draw_buttons();
00174   // this flag keeps Fl_Input_::drawtext from drawing a bogus box!
00175   char must_trick_fl_input_ = 
00176     Fl::focus()!=this && !size() && !(damage()&FL_DAMAGE_ALL);
00177   if ((damage() & FL_DAMAGE_ALL) || must_trick_fl_input_) 
00178     draw_box(b,x(),y()+DIR_HEIGHT,w(),h()-DIR_HEIGHT,color());
00179   if (!must_trick_fl_input_) 
00180     Fl_Input_::drawtext(x()+Fl::box_dx(b)+3, y()+Fl::box_dy(b)+DIR_HEIGHT,
00181                         w()-Fl::box_dw(b)-6, h()-Fl::box_dh(b)-DIR_HEIGHT);
00182 }
00183 
00184 
00185 
00191 int                                             // O - TRUE if we handled event
00192 Fl_File_Input::handle(int event)                // I - Event
00193 {
00194 //  printf("handle(event = %d)\n", event);
00195   static char inButtonBar = 0;
00196 
00197   switch (event) {
00198     case FL_MOVE :
00199     case FL_ENTER :
00200       if (active_r()) {
00201         if (Fl::event_y() < (y() + DIR_HEIGHT)) 
00202           window()->cursor(FL_CURSOR_DEFAULT);
00203         else 
00204           window()->cursor(FL_CURSOR_INSERT);
00205       }
00206 
00207       return 1;
00208 
00209     case FL_PUSH :
00210       inButtonBar = (Fl::event_y() < (y() + DIR_HEIGHT));
00211     case FL_RELEASE :
00212     case FL_DRAG :
00213       if (inButtonBar) 
00214         return handle_button(event);
00215       else
00216         return Fl_Input::handle(event);
00217 
00218     default :
00219       { Fl_Widget_Tracker wp(this);
00220         if (Fl_Input::handle(event)) {
00221           if (wp.exists())
00222             damage(FL_DAMAGE_BAR);
00223           return 1;
00224         }
00225       }
00226       return 0;
00227   }
00228 }
00229 
00230 
00231 
00237 int                                             // O - TRUE if we handled event
00238 Fl_File_Input::handle_button(int event)         // I - Event
00239 {
00240   int           i,                              // Looping var
00241                 X;                              // Current X position
00242   char          *start,                         // Start of path component
00243                 *end;                           // End of path component
00244   char          newvalue[FL_PATH_MAX];          // New value
00245 
00246 
00247   // Figure out which button is being pressed...
00248   for (X = 0, i = 0; buttons_[i]; i ++)
00249   {
00250     X += buttons_[i];
00251 
00252     if (X > xscroll() && Fl::event_x() < (x() + X - xscroll())) break;
00253   }
00254 
00255 //  printf("handle_button(event = %d), button = %d\n", event, i);
00256 
00257   // Redraw the directory bar...
00258   if (event == FL_RELEASE) pressed_ = -1;
00259   else pressed_ = (short)i;
00260 
00261   window()->make_current();
00262   draw_buttons();
00263 
00264   // Return immediately if the user is clicking on the last button or
00265   // has not released the mouse button...
00266   if (!buttons_[i] || event != FL_RELEASE) return 1;
00267 
00268   // Figure out where to truncate the path...
00269   strlcpy(newvalue, value(), sizeof(newvalue));
00270 
00271   for (start = newvalue, end = start; start && i >= 0; start = end, i --) {
00272 //    printf("    start = \"%s\"\n", start);
00273     if ((end = strchr(start, '/')) == NULL)
00274 #if defined(WIN32) || defined(__EMX__)
00275       if ((end = strchr(start, '\\')) == NULL)
00276 #endif // WIN32 || __EMX__
00277       break;
00278 
00279     end ++;
00280   }
00281 
00282   if (i < 0) {
00283     // Found the end; truncate the value and update the buttons...
00284     *start = '\0';
00285     value(newvalue, start - newvalue);
00286 
00287     // Then do the callbacks, if necessary...
00288     set_changed();
00289     if (when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE) ) do_callback();
00290   }
00291 
00292   return 1;
00293 }
00294 
00295 
00296 //
00297 // End of "$Id: Fl_File_Input.cxx 8063 2010-12-19 21:20:10Z matt $".
00298 //