The Hildon::FileChooserDialog is the Maemo equivalent for the Gtk::FileChooserDialog. As it is derived from Gtk::FileChooser you can use the same API.
Unlike the Gtk::FileChooserDialog, this dialog already has OK and Cancel buttons, so you should not use add_button() to add buttons with explicit action names, such as Open or Save.
The documentation for the standard gtkmm Gtk::FileChooser API can be found in the
gtkmm tutorial.
void show_file_chooser()
{
Hildon::FileChooserDialog dialog(*this, Gtk::FILE_CHOOSER_ACTION_OPEN);
int response = dialog.run();
dialog.hide();
//Handle the response:
switch(response)
{
case(Gtk::RESPONSE_OK):
{
std::string filename = dialog.get_filename();
// Do something with filename
break;
}
case(Gtk::RESPONSE_CANCEL):
{
// Cancel clicked
break;
}
default:
{
break;
}
}
}
Some parts of the example will be explained in detail:
Hildon::FileChooserDialog dialog(*this, Gtk::FILE_CHOOSER_ACTION_OPEN);
This creates a new file chooser dialog for opening files, and specifies the window for which it should be transient. The following options are available for the dialog:
Gtk::FILE_CHOOSER_ACTION_OPEN: The file chooser will only allow the user to pick an existing file.
Gtk::FILE_CHOOSER_ACTION_SAVE: The file chooser will allow the user to pick an existing file, or type in a new filename.
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER: The file chooser will allow the user to pick an existing folder.
Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER: The file chooser will allow the user to name an existing or new folder.
std::string filename = dialog.get_filename();
This is the most important method of the Gtk::FileChooser interface because here
you can receive the file chosen in the dialog.
Here is the full example code for the FileChooserDialog:
File: examplewindow.h
#ifndef _MAEMOMM_EXAMPLEWINDOW_H
#define _MAEMOMM_EXAMPLEWINDOW_H
#include <hildonmm/window.h>
#include <hildonmm/button.h>
#include <gtkmm/buttonbox.h>
class ExampleWindow : public Hildon::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
private:
// Signal handlers:
void on_button_file_clicked();
void on_button_folder_clicked();
// Child widgets:
Gtk::VButtonBox box_;
Hildon::Button button_file_;
Hildon::Button button_folder_;
};
#endif /* _MAEMOMM_EXAMPLEWINDOW_H */
File: examplewindow.cc
#include "examplewindow.h"
#include <hildon-fmmm/file-chooser-dialog.h>
#include <iostream>
ExampleWindow::ExampleWindow() :
button_file_(Gtk::Hildon::SIZE_HALFSCREEN_WIDTH |
Gtk::Hildon::SIZE_FINGER_HEIGHT, Hildon::BUTTON_ARRANGEMENT_VERTICAL,
"Click Me",
"to select a file"),
button_folder_(Gtk::Hildon::SIZE_HALFSCREEN_WIDTH |
Gtk::Hildon::SIZE_FINGER_HEIGHT, Hildon::BUTTON_ARRANGEMENT_VERTICAL,
"Click Me",
"to select a folder")
{
set_title("Hildon::FileChooserDialog Example");
add(box_);
box_.pack_start(button_file_);
button_file_.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_file_clicked));
box_.pack_start(button_folder_);
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()
{
Hildon::FileChooserDialog dialog(*this,
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
int response = dialog.run();
// Handle the response:
switch(response)
{
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()
{
Hildon::FileChooserDialog dialog(*this, Gtk::FILE_CHOOSER_ACTION_OPEN);
// Add filters, so that only certain file types can be selected:
Gtk::FileFilter filter_text;
filter_text.set_name("Text Files");
filter_text.add_mime_type("text/plain");
dialog.add_filter(filter_text);
Gtk::FileFilter filter_cpp;
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);
Gtk::FileFilter filter_any;
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 response = dialog.run();
//Handle the response:
switch(response)
{
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;
}
}
}
File: main.cc
#include <hildonmm.h>
#include <hildon-fmmm.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Hildon::init();
Hildon::fm_init();
ExampleWindow window;
kit.run(window); //Shows the window and returns when it is closed.
return 0;
}