Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

filedialog_generic.cpp

Go to the documentation of this file.
00001 /*
00002         ClanGUI, copyrights by various people. Have a look in the CREDITS file.
00003         
00004         This sourcecode is distributed using the Library GNU Public Licence,
00005         version 2 or (at your option) any later version. Please read LICENSE
00006         for details.
00007 */
00008 
00009 #include "precomp.h"
00010 #include "filedialog_generic.h"
00011 #include "API/GUI/component_options.h"
00012 
00013 #ifndef WIN32
00014 #include <sys/stat.h>
00015 #include <unistd.h>
00016 #include <dirent.h>
00017 #endif
00018 
00019 CL_ComponentOptions CL_FileDialog_Generic::create_options(
00020         const std::string &path,
00021         const std::string &filter)
00022 {
00023         CL_ComponentOptions options;
00024 
00025         options.add_option_as_int("x", 100);
00026         options.add_option_as_int("y", 100);
00027         options.add_option_as_int("width", 400);
00028         options.add_option_as_int("height", 250);
00029         options.add_option("path", path);
00030         options.add_option("filter", filter);
00031 
00032         return options;
00033 }
00034 
00035 CL_FileDialog_Generic::CL_FileDialog_Generic(CL_FileDialog *self, const CL_ComponentOptions &options, CL_StyleManager *style)
00036 :
00037         filedialog(self)
00038 {
00039 //      moveable(true);
00040 
00041         // TODO: Read current directory
00042         path = "/";
00043 
00044         CL_Rect pos = filedialog->get_position();
00045         int width = pos.x2 - pos.x1;
00046         int height = pos.y2 - pos.y1;
00047 
00048 /*      CL_Rect rect(width - 40, height - 30, 30, 20);
00049         button_ok = new CL_Button(rect, "Ok", false, filedialog, style);
00050         filedialog->add_child(button_ok, true);
00051 
00052         rect = CL_Rect(width - 100, height - 30, 50, 20);
00053         button_cancel = new CL_Button(rect, "Cancel", filedialog, style);
00054         filedialog->add_child(button_cancel, true);
00055 
00056         rect = CL_Rect(width - 270, height - 190, 120, 130);
00057         list_dir = new CL_ListBox(rect, this, style);
00058         filedialog->add_child(list_dir, true);
00059 
00060         rect = CL_Rect(width - 270, height - 50, 180, 20);
00061         input_file = new CL_InputBox(rect, this, style);
00062         filedialog->add_child(input_file, true);
00063 */      
00064         slot_ok = button_ok->sig_clicked().connect(CL_CreateSlot(this, &CL_FileDialog_Generic::on_ok));
00065         slot_cancel = button_cancel->sig_clicked().connect(CL_CreateSlot(this, &CL_FileDialog_Generic::on_cancel));
00066         slot_list = list_dir->sig_activated().connect(CL_CreateSlot(this, &CL_FileDialog_Generic::on_list));
00067 }
00068 
00069 void CL_FileDialog_Generic::on_ok()
00070 {
00071         sig_ok(filename);
00072 
00073 //      TODO: How to close a component ?
00074 //      quit();
00075 }
00076 
00077 void CL_FileDialog_Generic::on_cancel()
00078 {
00079         sig_cancel();
00080 
00081 //      TODO: How to close a component ?
00082 //      quit();
00083 }
00084 
00085 void CL_FileDialog_Generic::on_list(int option)
00086 {
00087         CL_String selected_file;
00088         selected_file = list_dir->get_current_text();
00089         if(selected_file == "../")
00090         {
00091                 path = path.mid(0, path.find_last('/'));
00092                 update_files();
00093         }
00094         else
00095         {
00096                 if(selected_file.right(1) == "/" && selected_file != "./")
00097                 {
00098                         path = path + "/" + selected_file.mid(0, selected_file.get_length() - 1);
00099                         update_files();
00100                 }
00101                 else
00102                 {
00103                         filename = path + "/" + selected_file;
00104                         input_file->set_text(filename);
00105                 }
00106         }
00107 }
00108 
00109 int CL_FileDialog_Generic::select_files(const struct dirent *file)
00110 {
00111         //dummy. no extension checking yet
00112         return 1;
00113 }
00114 
00115 void CL_FileDialog_Generic::update_files()
00116 {
00117 #ifndef WIN32
00118         struct dirent **namelist;
00119         int n;
00120         struct stat buf;
00121 
00122         input_file->set_text(path);
00123         list_dir->clear();      
00124 
00125         n=scandir(path,&namelist,0, alphasort);
00126         for(int i = 0; i < n; i++)
00127         {
00128                 stat(path + "/" + namelist[i]->d_name, &buf);
00129 
00130                 if(S_ISDIR(buf.st_mode))
00131                         list_dir->insert_item(CL_String(namelist[i]->d_name) + "/");
00132                 else
00133                         list_dir->insert_item(CL_String(namelist[i]->d_name));  
00134         }
00135 #endif
00136         input_file->update();
00137 }
00138 
00139 void CL_FileDialog_Generic::set_path(const std::string &new_path)
00140 {
00141         path = new_path;
00142         input_file->set_text(path);
00143         update_files();
00144 }
00145 
00146 void CL_FileDialog_Generic::set_filter(const std::string &new_filter)
00147 {
00148         filter = new_filter;
00149         update_files();
00150 }
00151 

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001