File: lf_filechooser.h

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (354 lines) | stat: -rw-r--r-- 12,370 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* 
 * Copyright (c) 2008, 2015, 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;
  std::map<std::string, std::map<std::string, std::string> > _option_values;
  std::map<std::string, std::string> _ext_list;
  std::string _default_extension;
  
  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;
    int dialog_result = dlg->_dlg->run();
    res = dialog_result == RESPONSE_OK || dialog_result == GTK_RESPONSE_ACCEPT;
    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"]);
      if (!ext.empty())
      {
        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);
      dlg->_option_values[name][iter->first] = iter->second;
      dlg->_ext_list.insert(std::make_pair(iter->first, iter->second));
    }
    combo->set_active(0);
  }

  static std::string get_selector_option_value(FileChooser *self, const std::string &name)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();

    if (name != "format")
      return dlg->_option_values["format"][name];
    
    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, bool allow_all_file_types = true)
  {
    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);
        if (iter->second.substr(2) == default_extension)
          dlg->_dlg->set_filter(filter);

        dlg->_ext_list.insert(std::make_pair(iter->first, iter->second));
        dlg->_ext_list[iter->first].erase(0, 2); //Remove the *.

        if (dlg->_default_extension.empty()) //First extension set default ext
        {
          dlg->_default_extension = iter->second;
          if (dlg->_default_extension.size() > 0)
            dlg->_default_extension.erase(0, 2);
        }
      }
      
      if (allow_all_file_types)
      {
        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)
  {
    Gtk::Button *ok_button = NULL;
    // 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);
      _dlg->set_default_response(RESPONSE_OK);
      break;
    case ::mforms::SaveFile:
      _dlg = new Gtk::FileChooserDialog("Save File...", Gtk::FILE_CHOOSER_ACTION_SAVE);
      _dlg->add_button(Gtk::Stock::CANCEL, GTK_RESPONSE_CANCEL);            // GTK_RESPONSE_CANCEL - Changing this will alter the behavior of the dialog 
      ok_button = _dlg->add_button(Gtk::Stock::SAVE, GTK_RESPONSE_ACCEPT);  // GTK_RESPONSE_ACCEPT - Changing this will alter the behavior of the dialog
      _dlg->set_do_overwrite_confirmation(true);
      _dlg->set_default_response(GTK_RESPONSE_ACCEPT);

      //  The save dialog has a strange behavior regarding the overwrite of files. For the stock dialog
      //  to be displayed as expected we need to add the file extension if required. To do so, we need
      //  to do it before the dialog checks if the file exists. The signal sequence seems different
      //  depending on the widget that has the focus. So, to add the file extension for the text box
      //  we need the signal_activate from the ok button (taking advantage of the default action).
      //  To add the extension when clicking the ok button, we need the signal_pressed from the ok button.
      //  The signal_clicked is triggered too late in the sequence, making it unusable.
      ok_button->signal_activate().connect(sigc::bind(&FileChooserImpl::on_ok_button_clicked, this), false);
      ok_button->signal_pressed().connect(sigc::bind(&FileChooserImpl::on_ok_button_clicked, this), false);
      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);
      _dlg->set_default_response(RESPONSE_OK);
      break;      
    }
    if (owner)
    {
      FormImpl *fi = owner->get_data<FormImpl>();
      if (fi && fi->get_window())
        _dlg->set_transient_for(*fi->get_window());
    }
  }
  
  //  Add the file extension related to the selected file type in the dialog
  void on_ok_button_clicked()
  {
    FileChooser *chooser = dynamic_cast<FileChooser*>(owner);


    //  If the dialog has no file format options, there's nothing to do here...
    std::string format_ext;
    if (chooser->_selector_options.find("format") == chooser->_selector_options.end())
    {
      // We need to check if there are maybe extensions specified if so we've pick up currently selected.
      GtkFileFilter *filter = gtk_file_chooser_get_filter(((Gtk::FileChooser*)_dlg)->gobj()); // Because of bug in gtkmm we can't use get_filter, we fallback to c api
      if (filter)
      {
        std::map<std::string, std::string>::iterator it =  _ext_list.find(gtk_file_filter_get_name(filter));
        if (it != _ext_list.end())
          format_ext = it->second;
      }
      else
        format_ext = _default_extension;
    }
    else
    {
      std::string format_human = get_selector_option_value(chooser, "format");
      format_ext = get_selector_option_value(chooser, format_human);
    }

    if(!format_ext.empty())
    {
      std::string path = _dlg->get_filename();
      std::string ext = base::extension(path);
      if (!ext.empty() && ext[0] == '.')
        ext = ext.substr(1);

      if (ext != format_ext)
      {
        std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
        std::map<std::string, std::string>::iterator it;
        for (it = _ext_list.begin(); it != _ext_list.end(); ++it)
        {
          if (it->second == ext) //If we support extension provided by user - don't change anything
            return;
        }

        path.append(".").append(format_ext);
      }

      _dlg->set_current_name(base::basename(path.c_str()));
      _dlg->set_filename(path);
    }
  }
    
  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