File: lf_filechooser.h

package info (click to toggle)
mysql-workbench 5.2.40%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 53,880 kB
  • sloc: cpp: 419,850; yacc: 74,784; xml: 54,510; python: 31,455; sh: 9,423; ansic: 4,736; makefile: 2,442; php: 529; java: 237
file content (260 lines) | stat: -rw-r--r-- 8,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* 
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */
#ifndef _LF_FILECHOOSER_H_
#define _LF_FILECHOOSER_H_

#include <mforms/mforms.h>

#include "lf_mforms.h"
#include "lf_view.h"
#include "base/file_utilities.h"

#define RESPONSE_OK 1
#define RESPONSE_CANCEL 0



namespace mforms {
namespace gtk {

class FileChooserImpl : public ViewImpl
{
  Gtk::FileChooserDialog *_dlg;
  Gtk::Widget *get_outer() const { return _dlg; }

  Gtk::Table *_options_table;
  std::map<std::string, Gtk::ComboBoxText*> _combos;
 
  static std::vector<std::string> split_string(const std::string &s, const std::string &sep)
  {
    std::vector<std::string> parts;
    std::string ss= s;

    std::string::size_type p;

    if (s.empty())
      return parts;

    p= ss.find(sep);
    while (!ss.empty() && p != std::string::npos)
    {
      parts.push_back(ss.substr(0, p));
      ss= ss.substr(p+sep.size());

      p= ss.find(sep);
    }
    parts.push_back(ss);

    return parts;
  
  }
  
  static bool create(::mforms::FileChooser *self, ::mforms::Form *owner, ::mforms::FileChooserType type, const bool show_hidden)
  {
    return new FileChooserImpl(self, owner, type, show_hidden) != 0;
  }

  static void set_title(::mforms::FileChooser *self, const std::string &title)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if ( dlg )
      dlg->_dlg->set_title(title);
  }

  static bool show_modal(::mforms::FileChooser *self)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    bool res;
    res= dlg->_dlg->run() == RESPONSE_OK;
    dlg->_dlg->hide();
    return res;
  }

  static void set_directory(FileChooser *self, const std::string &path)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if ( dlg )
    {
      dlg->_dlg->set_current_folder(path);
    }
  }

  static std::string get_directory(FileChooser *self)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if ( dlg )
    {
      return dlg->_dlg->get_current_folder();
    }
    return "";
  }


  static std::string get_path(FileChooser *self)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    return dlg ? dlg->_dlg->get_filename().raw() : "";
  }

  static void set_path(FileChooser *self, const std::string &path)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    dlg->_dlg->set_filename(path);

    std::string ext = base::extension(path);
    Gtk::ComboBoxText *combo = dlg->_combos["format"];
    if (combo)
    {
      std::vector<std::string> &extensions(self->_selector_options["format"]);
      std::vector<std::string>::const_iterator it = std::find(extensions.begin(), extensions.end(), ext.substr(1));
      if (it != extensions.end())
        combo->set_active(it - extensions.begin());
    }
  }

  static void add_selector_option(FileChooser *self, const std::string &name, const std::string &label, const std::vector<std::pair<std::string,std::string> > &values)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    int row;
    if (!dlg->_options_table)
    {
      dlg->_options_table = Gtk::manage(new Gtk::Table(1, 2));
      dlg->_options_table->set_col_spacings(4);
      dlg->_dlg->set_extra_widget(*dlg->_options_table);
      row = 0;
    }
    else
    {
      row = dlg->_options_table->property_n_rows().get_value();
      dlg->_options_table->property_n_rows().set_value(row + 1);
    }
    Gtk::ComboBoxText *combo = 0; 
    if (!(combo = dlg->_combos[name]))
    {
      combo = dlg->_combos[name] = Gtk::manage(new Gtk::ComboBoxText());
      dlg->_options_table->attach(*Gtk::manage(new Gtk::Label(label)), 0, 1, row, row+1, Gtk::FILL, Gtk::FILL);
      dlg->_options_table->attach(*dlg->_combos[name], 1, 2, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL);
      dlg->_options_table->show_all();
    }
    //combo->remove_all();
    combo->clear_items();
    for (std::vector<std::pair<std::string,std::string> >::const_iterator iter = values.begin(); iter != values.end(); ++iter)
      combo->append_text(iter->first);
    combo->set_active(0);
  }

  static std::string get_selector_option_value(FileChooser *self, const std::string &name)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if (dlg->_combos[name])
    {
      int i = dlg->_combos[name]->get_active_row_number();
      if (i >= 0)
        return self->_selector_options[name][i];
    }
    return "";
  }

  static void set_extensions(FileChooser *self, const std::string &extensions, const std::string &default_extension)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if (dlg)
    {      
      // extensions format:
      // AAA Files (*.aaa)|*.aaa,BBB Files (*.bbb)
      std::vector<std::pair<std::string, std::string> > exts(self->split_extensions(extensions));
      
      for (std::vector<std::pair<std::string, std::string> >::const_iterator iter= exts.begin();
           iter != exts.end(); ++iter)
      {
        Gtk::FileFilter filter;
        
        filter.add_pattern(iter->second);
        filter.set_name(iter->first);
        
        dlg->_dlg->add_filter(filter);
      }
      
      Gtk::FileFilter filter;
      filter.add_pattern("*");
      filter.set_name("All Files");

      dlg->_dlg->add_filter(filter);
    }
  }

  FileChooserImpl(::mforms::FileChooser *form, ::mforms::Form *owner, ::mforms::FileChooserType type, bool show_hidden)
    : ViewImpl(form), _options_table(0)
  {
    // TODO: enable showing hidden files/folders.
    //FileChooserDialog *dialog = 0;
    switch (type)
    {
    case ::mforms::OpenFile:
      _dlg = new Gtk::FileChooserDialog("Open File...", Gtk::FILE_CHOOSER_ACTION_OPEN);
      _dlg->add_button(Gtk::Stock::CANCEL, RESPONSE_CANCEL);
      _dlg->add_button(Gtk::Stock::OPEN, RESPONSE_OK);
      break;
    case ::mforms::SaveFile:
      _dlg = new Gtk::FileChooserDialog("Save File...", Gtk::FILE_CHOOSER_ACTION_SAVE);
      _dlg->add_button(Gtk::Stock::CANCEL, RESPONSE_CANCEL);
      _dlg->add_button(Gtk::Stock::SAVE, RESPONSE_OK);
      _dlg->set_do_overwrite_confirmation(true);
      break;
    case ::mforms::OpenDirectory:
      _dlg = new Gtk::FileChooserDialog("Open Directory...", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
      _dlg->add_button(Gtk::Stock::CANCEL, RESPONSE_CANCEL);
      _dlg->add_button(Gtk::Stock::OPEN, RESPONSE_OK);
      break;      
    }
    if (owner)
    {
      FormImpl *fi = owner->get_data<FormImpl>();
      if (fi && fi->get_window())
        _dlg->set_transient_for(*fi->get_window());
    }
  }

  virtual ~FileChooserImpl()
  {
    delete _dlg;
  }

 public:
  static void init()
  {
    ::mforms::ControlFactory *f = ::mforms::ControlFactory::get_instance();

    f->_filechooser_impl.create = &FileChooserImpl::create;
    f->_filechooser_impl.set_title = &FileChooserImpl::set_title;
    f->_filechooser_impl.run_modal = &FileChooserImpl::show_modal;
    f->_filechooser_impl.set_extensions = &FileChooserImpl::set_extensions;
    f->_filechooser_impl.set_directory = &FileChooserImpl::set_directory;
    f->_filechooser_impl.get_directory = &FileChooserImpl::get_directory;
    f->_filechooser_impl.get_path = &FileChooserImpl::get_path;
    f->_filechooser_impl.set_path = &FileChooserImpl::set_path;
    f->_filechooser_impl.add_selector_option = &FileChooserImpl::add_selector_option;
    f->_filechooser_impl.get_selector_option_value = &FileChooserImpl::get_selector_option_value;
  }
};

};
};

#endif