2 #include "filedialog.h"
4 #define OPEN_DLG_OK 100
5 #define OPEN_DLG_CANCEL 101
11 #include "directory.xpm"
12 #include "regfile.xpm"
14 bool operator < (const file_entry &fe1, const file_entry &fe2)
16 if (S_ISDIR(fe1.st.st_mode) == S_ISDIR(fe2.st.st_mode))
18 return fe1.name < fe2.name;
22 return S_ISDIR(fe1.st.st_mode);
26 directory_view::directory_view(window &parent) : table_view(parent, 200, 2)
32 dir.create(directory_xpm);
33 reg.create(regfile_xpm);
37 int directory_view::scan()
44 dirstream = opendir(cwd);
45 entry = readdir(dirstream);
51 file.name = entry->d_name;
52 stat(entry->d_name, &file.st);
53 d_list.push_back(file);
55 entry = readdir(dirstream);
59 sort(d_list.begin(), d_list.end());
61 recreate(d_list.size());
63 for (i = 0; i < d_list.size(); i++)
65 cell(i, 1) = d_list[i].name;
71 // Change the view's directory and return the new directory pathname
73 String directory_view::changedir(String dir)
75 char process_dir[PATH_MAX]; // Remember the process cwd
76 char destination_dir[PATH_MAX];
78 getcwd(process_dir, PATH_MAX);
85 perror("File selector");
86 slash = dir.rindex('/');
90 getcwd(destination_dir, PATH_MAX);
91 cwd = destination_dir;
95 chdir(process_dir); // Restore the directory we came from
100 void directory_view::redraw()
105 table_view::redraw();
109 while ((unsigned)i < d_list.size() && (unsigned)y < Size().h)
111 if (S_ISDIR(d_list[i].st.st_mode))
113 DrawPixmap(text_normal_gc, 2, y, dir);
117 DrawPixmap(text_normal_gc, 2, y, reg);
124 file_dialog::file_dialog(int cmd) : managed_window("File Selector")
128 Background(inside_3D);
131 OK = new command_button(*this, 20, 300, "OK", OPEN_DLG_OK);
134 Cancel = new command_button(*this, 300, 300, "Cancel", OPEN_DLG_CANCEL);
135 Command_WhenClosed(OPEN_DLG_CANCEL);
137 diredit = new edit(*this, 20, 20, "");
138 diredit->Resize(360, 20);
140 directory_list = new directory_view(*this);
141 directory_list->Move(20, 44);
142 fileedit = new edit(*this, 20, 270, "");
143 fileedit->Resize(360, 20);
145 getcwd(path, PATH_MAX);
146 *diredit = String(path);
147 directory_list->changedir(path);
150 int file_dialog::DoCommand(int code)
156 XApp->DoCommand(OK_command);
159 case OPEN_DLG_CANCEL:
168 void file_dialog::ChildMessage(win_message &msg)
171 if (msg.msg_id == EDIT_ENTERED)
173 if (msg.from == fileedit)
179 *diredit = directory_list->changedir(*diredit);
180 fileedit->SetFocus();
183 else if (msg.msg_id == TABLE_ROW_SELECT)
185 if (directory_list->selected_a_dir())
189 newdir = directory_list->cell(directory_list->selected(), 1);
190 *diredit = directory_list->changedir(newdir);
194 *fileedit = directory_list->cell(directory_list->selected(), 1);