1 // -*- mode: c++ -*-
2 //
3 // This file is part of libyacurs.
4 // Copyright (C) 2013  Rafael Ostertag
5 //
6 // This program is free software: you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation, either version 3 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see
18 // <http://www.gnu.org/licenses/>.
19 //
20 //
21 // $Id$
22 
23 #ifndef FILEDIALOG_H
24 #define FILEDIALOG_H 1
25 
26 #include <string>
27 
28 #include <unistd.h>
29 
30 #include "dialog.h"
31 #include "dynlabel.h"
32 #include "input.h"
33 #include "listbox.h"
34 #include "messagebox2.h"
35 
36 namespace YACURS {
37 class FileDialog : public Dialog {
38    private:
39     MessageBox2* _errmsgbox;
40     MessageBox2* _filetypemismatch;
41     DynLabel _path;
42     ListBox<> _directories;
43     ListBox<> _files;
44     Input<> _filename;
45     HPack _hpack;
46     VPack _vpack;
47     bool _do_chdir;
48     FILEDIALOG_SELECTION_TYPE _sel_type;
49     std::string _suffix;
50 
51    std::string cleanup_path(const std::string& p) const;
52 
53    protected:
54     std::string dir_up(const std::string& dir);
55 
56     void read_dir();
57 
58     void listbox_enter_handler(Event& e);
59 
60     void filename_readonly(bool ro);
61 
62     std::string getcwd() const;
63 
64     virtual void window_close_handler(Event& e);
65 
66     virtual void button_press_handler(Event& e);
67 
68     /**
69      * Test if the selected file path matches the requested
70      * selection type.
71      *
72      * Be advised, if the method returns @c false, it has
73      * created a dialog.
74      *
75      * @return @c true if the selected file matches the
76      * requested selection type. @c false otherwise.
77      */
78     bool selection_type_match();
79 
80    public:
81     FileDialog(const std::string& title, std::string path = std::string(),
82                bool do_chdir = false, DIALOG_TYPE _dt = OKCANCEL);
83 
84     FileDialog& operator=(const FileDialog&) = delete;
85     FileDialog& operator=(FileDialog&&) = delete;
86     FileDialog(const FileDialog&) = delete;
87     FileDialog(FileDialog&&) = delete;
88     virtual ~FileDialog();
89 
90     std::string filepath() const;
91 
92     const std::string& directory() const;
93 
94     std::string filename() const;
95 
96     void do_chdir(bool v);
97 
98     bool do_chdir() const;
99 
100     void selection_type(FILEDIALOG_SELECTION_TYPE t);
101 
102     FILEDIALOG_SELECTION_TYPE selection_type() const;
103 
104     /**
105      * File name suffix.
106      *
107      * String that is appended to file name and file path when
108      * selection type is FILE.
109      */
110     void suffix(const std::string& s);
111 
112     void refresh(bool immediate);
113 };
114 }  // namespace YACURS
115 
116 #endif  // FILEDIALOG_H
117