Blame platform-demos/C/filechooserdialog.py.page

Packit 1470ea
Packit 1470ea
Packit 1470ea
      xmlns:its="http://www.w3.org/2005/11/its"
Packit 1470ea
      xmlns:xi="http://www.w3.org/2001/XInclude"
Packit 1470ea
      type="guide" style="task"
Packit 1470ea
      id="filechooserdialog.py">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">FileChooserDialog (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#file-selectors"/>
Packit 1470ea
    <link type="seealso" xref="gmenu.py"/>
Packit 1470ea
    <link type="seealso" xref="toolbar_builder.py"/>
Packit 1470ea
    <link type="seealso" xref="textview.py"/>
Packit 1470ea
    <link type="next" xref="combobox.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-08-14" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A dialog suitable for "Open" and "Save" commands</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>FileChooserDialog</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/filechooserdialog_save.png"/>
Packit 1470ea
  

This FileChooserDialog saves a text document, which can be opened or written from scratch in a TextView (see below).

Packit 1470ea
  <media type="image" mime="image/png" src="media/filechooserdialog_menu.png"/>
Packit 1470ea
  

It is also possible to call a FileChooserDialog to open a new document.

Packit 1470ea
Packit 1470ea
  <links type="sections" />
Packit 1470ea
  
Packit 1470ea
  <section id="overview">
Packit 1470ea
  <title>Steps to recreate the example</title>
Packit 1470ea
  <steps>
Packit 1470ea
    <item>

Create a file .ui to describe an app-menu with items "New", "Open", "Save", "Save as", and "Quit". This can be done with Glade or in a text editor. See <link xref="#xml" />

</item>
Packit 1470ea
    <item>

Create a Python program for a Gtk.TextView with a Gtk.Buffer self.buffer, and a self.file which will be a Gio.File and we set initially as None.

</item>
Packit 1470ea
    <item>

In this program, create also the actions corresponding to the items in the app-menu, connect them to callback functions, and import the menu in the do_startup() method with a Gtk.Builder.

</item>
Packit 1470ea
    <item>

"New" and "Quit" actions and callback functions are quite straightforward, see <link xref="#code" />. See <link xref="signals-callbacks.py"/> for a more detailed explanation of signals and callback functions.

</item>
Packit 1470ea
    <item>

"Open" callback should create and open a Gtk.FileChooserDialog for "Open", connected with another callback function for each of the two "Open" and "Cancel" buttons of the FileChooserDialog.

</item>
Packit 1470ea
    <item>

"Save as" works basically as "Open", but the callback function of the "Save" button depends on a more complex method save_to_file().

</item>
Packit 1470ea
    <item>

"Save" can be reduced to the case where the file is None, that is the case where self.file is a new file, which in turn is the case "Save as"; and to the case where the file is not None, which in turn is reduced to save_to_file().

</item>
Packit 1470ea
    <item>

Finally, the method save_to_file(): see <link xref="#code" />, lines 146 - 175.

</item>
Packit 1470ea
  </steps>
Packit 1470ea
  </section>
Packit 1470ea
  
Packit 1470ea
  <section id="xml">
Packit 1470ea
  <title>XML file which creates the app-menu</title>
Packit 1470ea
  <xi:include href="samples/filechooserdialog.ui" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  </section>
Packit 1470ea
  
Packit 1470ea
  <section id="code">
Packit 1470ea
  <title>Code used to generate this example</title>
Packit 1470ea
  <xi:include href="samples/filechooserdialog.py" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  </section>
Packit 1470ea
  
Packit 1470ea
  <section id="methods">
Packit 1470ea
  <title>Useful methods for a FileChooserDialog</title>
Packit 1470ea
    

Note that the action of the FileChooserDialog can be one of the following: Gtk.FileChooserAction.OPEN (the file chooser will only let the user pick an existing file) Gtk.FileChooserAction.SAVE (the file chooser will let the user pick an existing file, or type in a new filename), Gtk.FileChooserAction.SELECT_FOLDER (the file chooser will let the user pick an existing folder), Gtk.FileChooserAction.CREATE_FOLDER (the file chooser will let the user name an existing or new folder).

Packit 1470ea
    

Besides the methods used in the <link xref="#code" />, we have:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

set_show_hidden(True) is used to display hidden files and folders.

</item>
Packit 1470ea
      <item>

set_select_multiple(True) sets that multiple files can be selected. This is only relevant if the mode is Gtk.FileChooserAction.OPEN or Gtk.FileChooserAction.SELECT_FOLDER.

</item>
Packit 1470ea
      <item>

In a "Save as" dialog, set_current_name(current_name) sets current_name in the file selector, as if entered by the user; current_name can be something like Untitled.txt. This method should not be used except in a "Save as" dialog.

</item>
Packit 1470ea
      <item>

The default current folder is "recent items". To set another folder use set_current_folder_uri(uri); but note you should use this method and cause the file chooser to show a specific folder only when you are doing a "Save as" command and you already have a file saved somewhere.

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
  <title>API References</title>
Packit 1470ea
  

In this sample we used the following:

Packit 1470ea
  <list>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkFileChooserDialog.html">GtkFileChooserDialog</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkFileChooser.html">GtkFileChooser</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkWindow.html">GtkWindow</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkTextView.html">GtkTextView</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkTextBuffer.html">GtkTextBuffer</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkScrolledWindow.html">GtkScrolledWindow</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gio/stable/GFile.html">GFile</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkBuilder.html">GtkBuilder</link>

</item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>