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
|
#include "examplewindow.h"
#include <iostream>
ExampleWindow::ExampleWindow()
: m_ButtonBox(Gtk::ORIENTATION_VERTICAL),
m_Button_File("Choose File"),
m_Button_Folder("Choose Folder")
{
set_title("Gtk::FileSelection example");
add(m_ButtonBox);
m_ButtonBox.pack_start(m_Button_File);
m_Button_File.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_file_clicked) );
m_ButtonBox.pack_start(m_Button_Folder);
m_Button_Folder.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_folder_clicked) );
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_folder_clicked()
{
Gtk::FileChooserDialog dialog("Please choose a folder",
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
dialog.set_transient_for(*this);
//Add response buttons the the dialog:
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("Select", Gtk::RESPONSE_OK);
int result = dialog.run();
//Handle the response:
switch(result)
{
case(Gtk::RESPONSE_OK):
{
std::cout << "Select clicked." << std::endl;
std::cout << "Folder selected: " << dialog.get_filename()
<< std::endl;
break;
}
case(Gtk::RESPONSE_CANCEL):
{
std::cout << "Cancel clicked." << std::endl;
break;
}
default:
{
std::cout << "Unexpected button clicked." << std::endl;
break;
}
}
}
void ExampleWindow::on_button_file_clicked()
{
Gtk::FileChooserDialog dialog("Please choose a file",
Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.set_transient_for(*this);
//Add response buttons the the dialog:
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Open", Gtk::RESPONSE_OK);
//Add filters, so that only certain file types can be selected:
auto filter_text = Gtk::FileFilter::create();
filter_text->set_name("Text files");
filter_text->add_mime_type("text/plain");
dialog.add_filter(filter_text);
auto filter_cpp = Gtk::FileFilter::create();
filter_cpp->set_name("C/C++ files");
filter_cpp->add_mime_type("text/x-c");
filter_cpp->add_mime_type("text/x-c++");
filter_cpp->add_mime_type("text/x-c-header");
dialog.add_filter(filter_cpp);
auto filter_any = Gtk::FileFilter::create();
filter_any->set_name("Any files");
filter_any->add_pattern("*");
dialog.add_filter(filter_any);
//Show the dialog and wait for a user response:
int result = dialog.run();
//Handle the response:
switch(result)
{
case(Gtk::RESPONSE_OK):
{
std::cout << "Open clicked." << std::endl;
//Notice that this is a std::string, not a Glib::ustring.
std::string filename = dialog.get_filename();
std::cout << "File selected: " << filename << std::endl;
break;
}
case(Gtk::RESPONSE_CANCEL):
{
std::cout << "Cancel clicked." << std::endl;
break;
}
default:
{
std::cout << "Unexpected button clicked." << std::endl;
break;
}
}
}
|