SimpleGUI
filebrowser.h
1 #ifndef FILEBROWSER
2 #define FILEBROWSER
3 
4 #include "simplegui.h"
5 #include <dirent.h>
6 #include <cstdio>
7 
8 char fullname[256];
9 
10 //===================================================================================
11 
12 class FileBrowser* fileBrowser = NULL;
13 
21 class FileBrowser :public Frame {
22 private:
23  Button* back;
24  Label* path;
25  List* folders;
26  List* files;
27  //-------------------------------------------------------------------------------
28  static void backFolder() {
29  char fullpath[256];
30  strcpy(fullpath,fileBrowser->path->getText());
31  for (int i=strlen(fullpath)-2; i>=0; i--) {
32  if (fullpath[i]=='/') {
33  fullpath[i+1] = 0;
34  break;
35  }
36  }
37  fileBrowser->path->setText(fullpath);
38  fileBrowser->loadFolders();
39  fileBrowser->loadFiles();
40  }
41  //-------------------------------------------------------------------------------
42  static void forwardFolder(const char item[]) {
43  char fullpath[256];
44  sprintf(fullpath,"%s%s/",fileBrowser->path->getText(),item);
45  fileBrowser->path->setText(fullpath);
46  fileBrowser->loadFolders();
47  fileBrowser->loadFiles();
48  }
49  //-------------------------------------------------------------------------------
50  static void selectFile(const char item[]) {
51  sprintf(fullname,"%s%s",fileBrowser->path->getText(),item);
52  fileBrowser->dispose();
53  }
54 
55 public:
56  FileBrowser() : Frame(100,100,400,200,"Select a File") {
57  fileBrowser = this;
58  back = new Button(10,13,13,14,"<");
59  path = new Label(25,10,365,20,"/home/");
60  folders = new List(10,40,185,145);
61  files = new List(205,40,185,145);
62  add(back);
63  add(path);
64  add(folders);
65  add(files);
66 
67  loadFolders();
68  loadFiles();
69 
70  back->action = &backFolder;
71  folders->doubleClicked = &forwardFolder;
72  files->doubleClicked = &selectFile;
73 
74  strcpy(fullname,"");
75  }
76  //-------------------------------------------------------------------------------
77  void loadFolders() {
78  folders->removeAll();
79  DIR *dir;
80  struct dirent *ent;
81  if ((dir = opendir (path->getText())) != NULL) {
82  while ((ent = readdir (dir)) != NULL) {
83  if (ent->d_name[0]=='.') continue;
84  if (ent->d_type == 4) {
85  folders->add(ent->d_name);
86  }
87  }
88  closedir (dir);
89  }
90  folders->sort();
91  }
92  //-------------------------------------------------------------------------------
93  void loadFiles() {
94  files->removeAll();
95  DIR *dir;
96  struct dirent *ent;
97  if ((dir = opendir (path->getText())) != NULL) {
98  while ((ent = readdir (dir)) != NULL) {
99  if (ent->d_name[0]=='.') continue;
100  if (ent->d_type != 4) {
101  files->add(ent->d_name);
102  }
103  }
104  closedir (dir);
105  }
106  files->sort();
107  }
108  //-------------------------------------------------------------------------------
109  static const char* searchFile() {
110  FileBrowser fb;
111  fb.run();
112  return fullname;
113  }
114 
115 };
116 
117 
118 #endif // FILEBROWSER
119 
The Button class is the prefered object usted to execute actions in common applications. This object is actionated by means of a click with the mouse device. This class can program the two actions inherited of Widget class.
Definition: simplegui.h:228
The FileBrowser class show a frame useful to search a file. This interface is created using the Simpl...
Definition: filebrowser.h:21
The Label class is the most simple visual class, useful show information to an application final user...
Definition: simplegui.h:106
The List class is a containter of textual items, it object can be stored more items as it can to show...
Definition: simplegui.h:292
The Frame class manage all visual objects: Labels, InputText, Buttons, Lists, so on. One frame works like a window, and send all actions to each object after use them.
Definition: simplegui.h:454