File: sec-filechooser-filedialog.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (350 lines) | stat: -rw-r--r-- 14,353 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
<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 &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; 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&lt;Gio::AsyncResult&gt;&amp; result);

  //Child widgets:
  Gtk::Box m_Box;

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;
  Glib::RefPtr&lt;Gio::SimpleActionGroup&gt; m_refActionGroup;

  Glib::RefPtr&lt;Gtk::RecentManager&gt; m_refRecentManager;
  Glib::RefPtr&lt;Gtk::FileDialog&gt; 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 &lt;iostream&gt;

ExampleWindow::ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; 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-&gt;add_action("new",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_new));

  //A menu item to open the file dialog:
  m_refActionGroup-&gt;add_action("files-dialog",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_files_dialog));

  m_refActionGroup-&gt;add_action("quit",
    sigc::mem_fun(*this, &amp;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-&gt;set_accel_for_action("example.new", "&lt;Primary&gt;n");
  app-&gt;set_accel_for_action("example.files-dialog", "&lt;Primary&gt;o");
  app-&gt;set_accel_for_action("example.quit", "&lt;Primary&gt;q");

  //Layout the actions in a menubar and a toolbar:
  const char* ui_info =
    "&lt;interface&gt;"
    "  &lt;menu id='menubar'&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_File&lt;/attribute&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_New&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.new&lt;/attribute&gt;"
    "        &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;n&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;File _Dialog&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.files-dialog&lt;/attribute&gt;"
    "        &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;o&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_Quit&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.quit&lt;/attribute&gt;"
    "        &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;q&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "    &lt;/submenu&gt;"
    "  &lt;/menu&gt;"
    "  &lt;object class='GtkBox' id='toolbar'&gt;"
    "    &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "    &lt;child&gt;"
    "      &lt;object class='GtkButton' id='toolbutton_new'&gt;"
    "        &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "        &lt;property name='tooltip_text' translatable='yes'&gt;New&lt;/property&gt;"
    "        &lt;property name='action_name'&gt;example.new&lt;/property&gt;"
    "        &lt;property name='icon_name'&gt;document-new&lt;/property&gt;"
    "        &lt;property name='hexpand'&gt;False&lt;/property&gt;"
    "        &lt;property name='vexpand'&gt;False&lt;/property&gt;"
    "      &lt;/object&gt;"
    "    &lt;/child&gt;"
    "    &lt;child&gt;"
    "      &lt;object class='GtkButton' id='toolbutton_quit'&gt;"
    "        &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "        &lt;property name='tooltip_text' translatable='yes'&gt;Quit&lt;/property&gt;"
    "        &lt;property name='action_name'&gt;example.quit&lt;/property&gt;"
    "        &lt;property name='icon_name'&gt;application-exit&lt;/property&gt;"
    "        &lt;property name='hexpand'&gt;False&lt;/property&gt;"
    "        &lt;property name='vexpand'&gt;False&lt;/property&gt;"
    "      &lt;/object&gt;"
    "    &lt;/child&gt;"
    "  &lt;/object&gt;"
    "&lt;/interface&gt;";

  try
  {
    m_refBuilder-&gt;add_from_string(ui_info);
  }
  catch(const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "building menubar and toolbar failed: " &lt;&lt;  ex.what();
  }

  //Get the menubar and toolbar widgets, and add them to a container widget:
  auto object = m_refBuilder-&gt;get_object("menubar");
  auto gmenu = std::dynamic_pointer_cast&lt;Gio::Menu&gt;(object);
  if (gmenu)
  {
    //Menubar:
    auto pMenubar = Gtk::make_managed&lt;Gtk::PopoverMenuBar&gt;(gmenu);
    m_Box.append(*pMenubar);
  }
  else
    g_warning("GMenu not found");

  auto pToolbar = m_refBuilder-&gt;get_widget&lt;Gtk::Box&gt;("toolbar");
  if (pToolbar)
    //Toolbar:
    m_Box.append(*pToolbar);
  else
    g_warning("toolbar not found");
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_menu_file_new()
{
  std::cout &lt;&lt; " New File" &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_file_quit()
{
  set_visible(false); //Closes the main window to stop the app-&gt;make_window_and_run().
}

void ExampleWindow::on_menu_file_files_dialog()
{
  if (!m_refFileDialog)
  {
    m_refFileDialog = Gtk::FileDialog::create();
    m_refFileDialog-&gt;set_modal(true);
  }
  m_refFileDialog-&gt;open(*this, sigc::mem_fun(*this, &amp;ExampleWindow::on_dialog_finish));
}

void ExampleWindow::on_dialog_finish(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result)
{
  Glib::RefPtr&lt;Gio::File&gt; file;
  try
  {
    file = m_refFileDialog-&gt;open_finish(result);
  }
  catch (const Gtk::DialogError&amp; err)
  {
    std::cout &lt;&lt; "No file selected, " &lt;&lt; err.what() &lt;&lt; std::endl;
    return;
  }

  auto selected_uri = file-&gt;get_uri();
  std::cout &lt;&lt; "URI selected = " &lt;&lt; selected_uri &lt;&lt; std::endl;
  std::cout &lt;&lt; (m_refRecentManager-&gt;has_item(selected_uri) ? "A" : "Not a")
    &lt;&lt; " recently used file" &lt;&lt; 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 &lt;gtkmm/application.h&gt;

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-&gt;make_window_and_run&lt;ExampleWindow&gt;(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>