00001
00002
00003
00004
00005
00006
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
00040
00041
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
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
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
00074
00075 }
00076
00077 void CL_FileDialog_Generic::on_cancel()
00078 {
00079 sig_cancel();
00080
00081
00082
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
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