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  if( fname.length()>255)
38  throw std::runtime_error(
39  "wex::filebox fname too long");
40  memcpy(
41  ofn.lpstrFile,
42  fname.c_str(),
43  fname.length() + 1);
44  }
45 
54  void filter(
55  const char *fbuf)
56  {
57  ofn.lpstrFilter = fbuf;
58  }
59 
61  void defaultExtension(const std::string &ext)
62  {
63  ofn.lpstrDefExt = ext.c_str();
64  }
65 
66  // title for dialog window
67  void title(const std::string &fname)
68  {
69  ofn.lpstrTitle = fname.c_str();
70  }
71 
76  std::string open()
77  {
78  if (GetOpenFileNameA(&ofn) == TRUE)
79  {
80  myfname = ofn.lpstrFile;
81  if (ofn.lpstrFile[ofn.nFileOffset - 1] == 0)
82  {
83  // multiple files selected
84  // return the first
85  myfname += "\\";
86  myfname += &ofn.lpstrFile[ofn.nFileOffset];
87  }
88  }
89  else
90  {
91  auto err = CommDlgExtendedError();
92  myfname = "";
93  }
94  return myfname;
95  }
101  std::vector<std::string> openMulti()
102  {
103  std::vector<std::string> ret;
104  if (GetOpenFileNameA(&ofn) == TRUE)
105  {
106  if (ofn.lpstrFile[ofn.nFileOffset - 1] != 0)
107  {
108  // single file selected
109  ret.push_back(ofn.lpstrFile);
110  }
111  else
112  {
113  std::string dir = ofn.lpstrFile;
114  int p = ofn.nFileOffset;
115  while (1)
116  {
117  myfname = &ofn.lpstrFile[p];
118  if (myfname.empty())
119  break;
120  ret.push_back(dir + "\\" + myfname);
121  p += myfname.length() + 1;
122  }
123  }
124  }
125  myfname = "";
126  return ret;
127  }
131  std::string save()
132  {
133  if (GetSaveFileNameA(&ofn) == TRUE)
134  myfname = ofn.lpstrFile;
135  else
136  myfname = "";
137  return myfname;
138  }
140  std::string path() const
141  {
142  return myfname;
143  }
144 
145  private:
146  OPENFILENAME ofn; // common dialog box structure
147  char szFile[260]; // buffer for file name
148  std::string myfname;
149  };
150 }
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:140
std::vector< std::string > openMulti()
prompt user for one or multiple files
Definition: filebox.h:101
void filter(const char *fbuf)
Set file filters.
Definition: filebox.h:54
std::string open()
prompt user for one file to open
Definition: filebox.h:76
void defaultExtension(const std::string &ext)
default extension for saved file, appended to whatever user enters
Definition: filebox.h:61
std::string save()
prompt user for folder and filename to save
Definition: filebox.h:131
The base class for all windex gui elements.
Definition: wex.h:900
HWND handle()
get window handle
Definition: wex.h:1748