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
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>FileChooser and FileDialog</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-recent-documents.html" title="Chapter 22. Recently Used Documents">
<link rel="prev" href="chapter-recent-documents.html" title="Chapter 22. Recently Used Documents">
<link rel="next" href="chapter-keyboardevents.html" title="Chapter 23. Keyboard and Mouse Events">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">FileChooser and FileDialog</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="chapter-recent-documents.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 22. Recently Used Documents</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-keyboardevents.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-filechooser-filedialog"></a>FileChooser and FileDialog</h2></div></div></div>
<p>
<code class="classname">FileChooser</code> is an interface that can be
implemented by widgets displaying a list of files.
<span class="application">gtkmm</span> provides three built-in implementations for choosing recent files
or other files:
<code class="classname">FileChooserWidget</code>,
<code class="classname">FileChooserDialog</code>, and
<code class="classname">FileChooserNative</code>.
</p>
<p>
<code class="classname">FileChooserWidget</code> is a simple widget for
displaying a list of recently used files or other files.
<code class="classname">FileChooserWidget</code> is the basic building block for
<code class="classname">FileChooserDialog</code>, but you can embed it into your
user interface if you want to.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="icons/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p><code class="classname">FileChooser</code> and the classes that
implement it are deprecated since <span class="application">gtkmm</span> 4.10. They have been replaced
by <code class="classname">FileDialog</code>, which is available since <span class="application">gtkmm</span> 4.10.
</p></td></tr>
</table></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="recentfiles-example"></a>Simple FileDialog example</h3></div></div></div>
<p>
Shown below is a simple example of how to use the
<code class="classname">FileDialog</code> class in a program.
This simple program has a menubar with a
<span class="guimenuitem">File Dialog</span> menu item.
When you select this menu item, a dialog pops up showing a list of files.
If you select <span class="guimenuitem">Recent</span> in the sidebar,
the list of recently used files is shown.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="icons/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p>
If this is the first time you're using a program that uses the Recent
Files framework, the dialog may be empty at first. Otherwise it
should show the list of recently used documents registered by other
applications.
</p>
</td></tr>
</table></div>
<p>
After selecting the <span class="guimenuitem">File Dialog</span> menu
item and the <span class="guimenuitem">Recent</span> sidebar item, you should
see something similar to the following window.
</p>
<div class="screenshot">
<div class="mediaobject"><img src="figures/recentfiles.png"></div>
</div>
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/recent_files" target="_top">Source Code</a></p>
<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow(const Glib::RefPtr<Gtk::Application>& app);
~ExampleWindow() override;
protected:
//Signal handlers:
void on_menu_file_files_dialog();
void on_menu_file_quit();
void on_menu_file_new();
void on_dialog_finish(Glib::RefPtr<Gio::AsyncResult>& result);
//Child widgets:
Gtk::Box m_Box;
Glib::RefPtr<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gio::SimpleActionGroup> m_refActionGroup;
Glib::RefPtr<Gtk::RecentManager> m_refRecentManager;
Glib::RefPtr<Gtk::FileDialog> m_refFileDialog;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <iostream>
ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
: m_Box(Gtk::Orientation::VERTICAL),
m_refRecentManager(Gtk::RecentManager::get_default())
{
set_title("Recent files example");
set_default_size(300, 150);
//We can put a PopoverMenuBar at the top of the box and other stuff below it.
set_child(m_Box);
//Create actions for menus and toolbars:
m_refActionGroup = Gio::SimpleActionGroup::create();
//File menu:
m_refActionGroup->add_action("new",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new));
//A menu item to open the file dialog:
m_refActionGroup->add_action("files-dialog",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_files_dialog));
m_refActionGroup->add_action("quit",
sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit) );
insert_action_group("example", m_refActionGroup);
m_refBuilder = Gtk::Builder::create();
// When the menubar is a child of a Gtk::Window, keyboard accelerators are not
// automatically fetched from the Gio::Menu.
// See the examples/book/menus/main_menu example for an alternative way of
// adding the menubar when using Gtk::ApplicationWindow.
app->set_accel_for_action("example.new", "<Primary>n");
app->set_accel_for_action("example.files-dialog", "<Primary>o");
app->set_accel_for_action("example.quit", "<Primary>q");
//Layout the actions in a menubar and a toolbar:
const char* ui_info =
"<interface>"
" <menu id='menubar'>"
" <submenu>"
" <attribute name='label' translatable='yes'>_File</attribute>"
" <item>"
" <attribute name='label' translatable='yes'>_New</attribute>"
" <attribute name='action'>example.new</attribute>"
" <attribute name='accel'>&lt;Primary&gt;n</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>File _Dialog</attribute>"
" <attribute name='action'>example.files-dialog</attribute>"
" <attribute name='accel'>&lt;Primary&gt;o</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Quit</attribute>"
" <attribute name='action'>example.quit</attribute>"
" <attribute name='accel'>&lt;Primary&gt;q</attribute>"
" </item>"
" </submenu>"
" </menu>"
" <object class='GtkBox' id='toolbar'>"
" <property name='can_focus'>False</property>"
" <child>"
" <object class='GtkButton' id='toolbutton_new'>"
" <property name='can_focus'>False</property>"
" <property name='tooltip_text' translatable='yes'>New</property>"
" <property name='action_name'>example.new</property>"
" <property name='icon_name'>document-new</property>"
" <property name='hexpand'>False</property>"
" <property name='vexpand'>False</property>"
" </object>"
" </child>"
" <child>"
" <object class='GtkButton' id='toolbutton_quit'>"
" <property name='can_focus'>False</property>"
" <property name='tooltip_text' translatable='yes'>Quit</property>"
" <property name='action_name'>example.quit</property>"
" <property name='icon_name'>application-exit</property>"
" <property name='hexpand'>False</property>"
" <property name='vexpand'>False</property>"
" </object>"
" </child>"
" </object>"
"</interface>";
try
{
m_refBuilder->add_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
std::cerr << "building menubar and toolbar failed: " << ex.what();
}
//Get the menubar and toolbar widgets, and add them to a container widget:
auto object = m_refBuilder->get_object("menubar");
auto gmenu = std::dynamic_pointer_cast<Gio::Menu>(object);
if (gmenu)
{
//Menubar:
auto pMenubar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
m_Box.append(*pMenubar);
}
else
g_warning("GMenu not found");
auto pToolbar = m_refBuilder->get_widget<Gtk::Box>("toolbar");
if (pToolbar)
//Toolbar:
m_Box.append(*pToolbar);
else
g_warning("toolbar not found");
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_menu_file_new()
{
std::cout << " New File" << std::endl;
}
void ExampleWindow::on_menu_file_quit()
{
set_visible(false); //Closes the main window to stop the app->make_window_and_run().
}
void ExampleWindow::on_menu_file_files_dialog()
{
if (!m_refFileDialog)
{
m_refFileDialog = Gtk::FileDialog::create();
m_refFileDialog->set_modal(true);
}
m_refFileDialog->open(*this, sigc::mem_fun(*this, &ExampleWindow::on_dialog_finish));
}
void ExampleWindow::on_dialog_finish(Glib::RefPtr<Gio::AsyncResult>& result)
{
Glib::RefPtr<Gio::File> file;
try
{
file = m_refFileDialog->open_finish(result);
}
catch (const Gtk::DialogError& err)
{
std::cout << "No file selected, " << err.what() << std::endl;
return;
}
auto selected_uri = file->get_uri();
std::cout << "URI selected = " << selected_uri << std::endl;
std::cout << (m_refRecentManager->has_item(selected_uri) ? "A" : "Not a")
<< " recently used file" << std::endl;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
//Shows the window and returns when it is closed.
return app->make_window_and_run<ExampleWindow>(argc, argv, app);
}
</code></pre>
<p>
The constructor for <code class="classname">ExampleWindow</code> creates the
menu and the toolbar using <code class="classname">Builder</code> (see <a class="xref" href="chapter-menus-and-toolbars.html" title="Chapter 15. Menus and Toolbars">Chapter 15, <i>Menus and Toolbars</i></a> for more information). It then adds
the menu and the toolbar to the window.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="recent-files-filtering"></a>Filtering Files</h3></div></div></div>
<p>
For any of the <code class="classname">FileChooser</code> classes, if you
don't wish to display all of the items in the list of files, you
can filter the list to show only those that you want. You can filter
the list with the help of the <code class="classname">FileFilter</code> class.
This class allows you to filter files by their name
(<code class="methodname">add_pattern()</code>), or their mime type
(<code class="methodname">add_mime_type()</code>).
</p>
<p>
After you've created and set up the filter to match only the items you
want, you can apply a filter to a chooser widget with the
<code class="methodname">FileChooser::add_filter()</code> function.
</p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="chapter-recent-documents.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-recent-documents.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-keyboardevents.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 22. Recently Used Documents </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 23. Keyboard and Mouse Events</td>
</tr>
</table>
</div>
</body>
</html>
|