WINDEX
filebox.h
1 #include <commdlg.h>
2 
3 namespace wex
4 {
5 
7 
8  class filebox
9  {
10  public:
11  filebox(gui &parent)
12  {
13 
14  // Initialize OPENFILENAME
15  ZeroMemory(&ofn, sizeof(ofn));
16  ofn.lStructSize = sizeof(ofn);
17  ofn.hwndOwner = parent.handle();
18  ofn.lpstrFile = szFile;
19  // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
20  // use the contents of szFile to initialize itself.
21  ofn.lpstrFile[0] = '\0';
22  ofn.nMaxFile = sizeof(szFile);
23  ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
24  ofn.nFilterIndex = 1;
25  ofn.lpstrFileTitle = NULL;
26  ofn.nMaxFileTitle = 0;
27  ofn.lpstrInitialDir = NULL;
28  ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
29  }
30 
31  void initDir(const std::string &dir)
32  {
33  ofn.lpstrInitialDir = dir.c_str();
34  }
35  void initFile(const std::string &fname)
36  {
37  ofn.lpstrFileTitle = (LPSTR)fname.c_str();
38  }
39 
48  void filter(
49  const char *fbuf)
50  {
51  ofn.lpstrFilter = fbuf;
52  }
53 
55  void defaultExtension(const std::string &ext)
56  {
57  ofn.lpstrDefExt = ext.c_str();
58  }
59 
60  // title for dialog window
61  void title(const std::string &fname)
62  {
63  ofn.lpstrTitle = fname.c_str();
64  }
65 
70  std::string open()
71  {
72  if (GetOpenFileNameA(&ofn) == TRUE)
73  {
74  myfname = ofn.lpstrFile;
75  if (ofn.lpstrFile[ofn.nFileOffset - 1] == 0)
76  {
77  // multiple files selected
78  // return the first
79  myfname += "\\";
80  myfname += &ofn.lpstrFile[ofn.nFileOffset];
81  }
82  }
83  else
84  {
85  auto err = CommDlgExtendedError();
86  myfname = "";
87  }
88  return myfname;
89  }
95  std::vector<std::string> openMulti()
96  {
97  std::vector<std::string> ret;
98  if (GetOpenFileNameA(&ofn) == TRUE)
99  {
100  if (ofn.lpstrFile[ofn.nFileOffset - 1] != 0)
101  {
102  // single file selected
103  ret.push_back(ofn.lpstrFile);
104  }
105  else
106  {
107  std::string dir = ofn.lpstrFile;
108  int p = ofn.nFileOffset;
109  while (1)
110  {
111  myfname = &ofn.lpstrFile[p];
112  if( myfname.empty())
113  break;
114  ret.push_back(dir + "\\" + myfname);
115  p += myfname.length() + 1;
116  }
117  }
118  }
119  myfname = "";
120  return ret;
121  }
125  std::string save()
126  {
127  if (GetSaveFileNameA(&ofn) == TRUE)
128  myfname = ofn.lpstrFile;
129  else
130  myfname = "";
131  return myfname;
132  }
134  std::string path() const
135  {
136  return myfname;
137  }
138 
139  private:
140  OPENFILENAME ofn; // common dialog box structure
141  char szFile[260]; // buffer for file name
142  std::string myfname;
143  };
144 }
A popup window where used can browse folders and select a file.
Definition: filebox.h:9
std::string path() const
get filename entered by user
Definition: filebox.h:134
std::vector< std::string > openMulti()
prompt user for one or multiple files
Definition: filebox.h:95
void filter(const char *fbuf)
Set file filters.
Definition: filebox.h:48
std::string open()
prompt user for one file to open
Definition: filebox.h:70
void defaultExtension(const std::string &ext)
default extension for saved file, appended to whatever user enters
Definition: filebox.h:55
std::string save()
prompt user for folder and filename to save
Definition: filebox.h:125
The base class for all windex gui elements.
Definition: wex.h:824
HWND handle()
get window handle
Definition: wex.h:1655