[gnome-devel-docs] Vala tutorials: FileChooserDialog example
- From: Tiffany Antopolski <antopolski src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-devel-docs] Vala tutorials: FileChooserDialog example
- Date: Mon, 2 Jul 2012 02:34:11 +0000 (UTC)
commit 19d31250e09e799d79f050b166e2242004fea179
Author: Tiffany Antopolski <tiffany antopolski gmail com>
Date: Sun Jul 1 21:22:43 2012 -0400
Vala tutorials: FileChooserDialog example
platform-demos/C/filechooserdialog.vala.page | 44 +++++
platform-demos/C/samples/filechooserdialog.ui | 31 ++++
platform-demos/C/samples/filechooserdialog.vala | 194 +++++++++++++++++++++++
3 files changed, 269 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/filechooserdialog.vala.page b/platform-demos/C/filechooserdialog.vala.page
new file mode 100644
index 0000000..1e0fbda
--- /dev/null
+++ b/platform-demos/C/filechooserdialog.vala.page
@@ -0,0 +1,44 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<page xmlns="http://projectmallard.org/1.0/"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ type="guide" style="task"
+ id="filechooserdialog.vala">
+ <info>
+ <link type="guide" xref="beginner.vala#file-selectors"/>
+ <link type="seealso" xref="textview.vala"/>
+ <link type="seealso" xref="menubar.vala"/>
+ <link type="seealso" xref="scrolledwindow.vala"/>
+ <revision version="0.1" date="2012-07-01" status="draft"/>
+
+ <credit type="author copyright">
+ <name>Tiffany Antopolski</name>
+ <email>tiffany antopolski gmail com</email>
+ <years>2012</years>
+ </credit>
+
+ <desc>A dialog suitable for "Open" and "Save" commands.</desc>
+ </info>
+
+ <title>FileChooserDialog</title>
+ <links type="sections"/>
+ <section id="overview"><title>Overview of the example</title>
+ <p>This example demonstrates how the FileChooserDialog can be used. It is incorporated into a very simple text editor application. All the <link xref="menubar.vala#win-app">actions</link>, including the "open", "save" and "save-as" commands can be found in the <link xref="gmenu.vala">app-menu</link>. Here, the app-menu is created using an XML UI file, which is then imported into the application using Gtk.Builder.</p>
+ </section>
+ <section id="xml"><title>XML UI file which creates the app-menu</title>
+<code mime="text" style="numbered"><xi:include href="samples/filechooserdialog.ui" parse="text"><xi:fallback/></xi:include></code>
+ </section>
+ <section id="vala-code"><title>Vala Code</title>
+ <code mime="text/x-vala" style="numbered"><xi:include href="samples/filechooserdialog.vala" parse="text"><xi:fallback/></xi:include></code>
+ </section>
+ <section id="api"><title>Relevant API documentation</title>
+<p>
+ In this sample we used the following:
+</p>
+<list>
+ <item><p><link href="http://valadoc.org/gtk+-3.0/Gtk.FileChooser.html">FileChooser</link></p></item>
+ <item><p><link href="http://valadoc.org/gtk+-3.0/Gtk.FileChooserDialog.html">FileChooserDialog</link></p></item>
+ <item><p><link href="http://valadoc.org/gtk+-3.0/Gtk.Builder.html">Gtk.Builder</link></p></item>
+ <item><p><link href="http://valadoc.org/gio-2.0/GLib.ActionEntry.html">GLib.ActionEntry</link></p></item>
+</list>
+</section>
+</page>
diff --git a/platform-demos/C/samples/filechooserdialog.ui b/platform-demos/C/samples/filechooserdialog.ui
new file mode 100644
index 0000000..bc79bd1
--- /dev/null
+++ b/platform-demos/C/samples/filechooserdialog.ui
@@ -0,0 +1,31 @@
+<?xml version="1.0"?>
+<interface>
+ <menu id="appmenu">
+ <section>
+ <item>
+ <attribute name="label">New</attribute>
+ <attribute name="action">win.new</attribute>
+ </item>
+ <item>
+ <attribute name="label">Open</attribute>
+ <attribute name="action">win.open</attribute>
+ </item>
+ </section>
+ <section>
+ <item>
+ <attribute name="label">Save</attribute>
+ <attribute name="action">win.save</attribute>
+ </item>
+ <item>
+ <attribute name="label">Save As...</attribute>
+ <attribute name="action">win.save-as</attribute>
+ </item>
+ </section>
+ <section>
+ <item>
+ <attribute name="label">Quit</attribute>
+ <attribute name="action">app.quit</attribute>
+ </item>
+ </section>
+ </menu>
+</interface>
diff --git a/platform-demos/C/samples/filechooserdialog.vala b/platform-demos/C/samples/filechooserdialog.vala
new file mode 100644
index 0000000..278a106
--- /dev/null
+++ b/platform-demos/C/samples/filechooserdialog.vala
@@ -0,0 +1,194 @@
+class MyWindow: Gtk.ApplicationWindow {
+
+ /* MyWindow instance variables. */
+ GLib.File? file;
+ Gtk.TextBuffer buffer;
+ Gtk.TextView textview;
+ Gtk.ScrolledWindow scrolled_window;
+
+ /* Create ActionEntries. */
+ const ActionEntry[] actions = {
+ { "new", new_cb },
+ { "open", open_cb },
+ { "save", save_cb },
+ { "save-as", save_as_cb }
+ };
+
+ /* Constructor creates MyWindow, and add the scrolled_window. */
+ internal MyWindow (MyApplication app) {
+ Object (application: app, title: "FileChooserDialog Example");
+ this.set_default_size (400, 400);
+
+ /* Add the ActionEntries to MyWindow. */
+ this.add_action_entries (actions, this);
+
+ buffer = new Gtk.TextBuffer (null); //stores text to be displayed
+ textview = new Gtk.TextView.with_buffer (buffer); //displays TextBuffer
+ textview.set_wrap_mode (Gtk.WrapMode.WORD); //sets line wrapping
+
+ scrolled_window = new Gtk.ScrolledWindow (null, null);
+ scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC,
+ Gtk.PolicyType.AUTOMATIC);
+
+ scrolled_window.add_with_viewport (textview);
+ scrolled_window.set_border_width (5);
+
+ this.add (scrolled_window);
+ this.show_all ();
+ }
+
+ void new_cb (SimpleAction action, Variant? parameter) {
+ file = null;
+ buffer.set_text ("");
+ print ("New file created\n");
+ }
+
+ /* Create FileChooserDialog in OPEN mode. */
+ void open_cb (SimpleAction action, Variant? parameter) {
+
+ var open_dialog = new Gtk.FileChooserDialog ("Pick a file",
+ this as Gtk.Window,
+ Gtk.FileChooserAction.OPEN,
+ Gtk.Stock.CANCEL,
+ Gtk.ResponseType.CANCEL,
+ Gtk.Stock.OPEN,
+ Gtk.ResponseType.ACCEPT);
+
+ open_dialog.local_only = false; //allow for uri
+ open_dialog.set_modal (true);
+ open_dialog.response.connect (open_response_cb);
+ open_dialog.show ();
+ }
+
+ /* Either open the file and load the file contents or cancel. */
+ void open_response_cb (Gtk.Dialog dialog, int response_id) {
+ var open_dialog = dialog as Gtk.FileChooserDialog;
+
+ switch (response_id) {
+ case Gtk.ResponseType.ACCEPT: //open the file
+ file = open_dialog.get_file();
+
+ uint8[] file_contents;
+
+ try {
+ file.load_contents (null, out file_contents, null);
+ }
+ catch (GLib.Error err) { //handle the exception
+ error ("%s\n", err.message);
+ }
+ /* Set the buffer text to be the contents of the file. */
+ buffer.set_text ((string) file_contents,
+ file_contents.length);
+
+ print ("opened: %s\n", (open_dialog.get_filename ()));
+ break;
+
+ case Gtk.ResponseType.CANCEL:
+ print ("cancelled: FileChooserAction.OPEN\n");
+ break;
+ }
+ dialog.destroy ();
+ }
+
+
+ /* Create FileChooserDialog in SAVE mode. */
+ void save_as_cb (SimpleAction action, Variant? parameter) {
+ var save_dialog = new Gtk.FileChooserDialog ("Pick a file",
+ this as Gtk.Window,
+ Gtk.FileChooserAction.SAVE,
+ Gtk.Stock.CANCEL,
+ Gtk.ResponseType.CANCEL,
+ Gtk.Stock.SAVE,
+ Gtk.ResponseType.ACCEPT);
+
+ save_dialog.set_do_overwrite_confirmation (true);
+ save_dialog.set_modal (true);
+ if (file != null) {
+ try {
+ (save_dialog as Gtk.FileChooser).set_file (file);
+ }
+ catch (GLib.Error error) {
+ print ("%s\n", error.message);
+ }
+ }
+ save_dialog.response.connect (save_as_response_cb);
+ save_dialog.show ();
+ }
+
+ void save_as_response_cb (Gtk.Dialog dialog, int response_id) {
+ var save_dialog = dialog as Gtk.FileChooserDialog;
+
+ switch (response_id) {
+ case Gtk.ResponseType.ACCEPT:
+ file = save_dialog.get_file();
+ this.save_to_file ();
+ break;
+ default:
+ break;
+ }
+ dialog.destroy ();
+ }
+
+ /* Save the existing contents to the file.
+ * If file does not exist, call save_as_cb.
+ */
+ void save_cb (SimpleAction action, Variant? parameter) {
+ if (file != null) {
+ this.save_to_file ();
+ }else {
+ save_as_cb (action, parameter);
+ }
+ }
+
+ void save_to_file (){
+ Gtk.TextIter start;
+ Gtk.TextIter end;
+
+ buffer.get_bounds (out start, out end);
+ string current_contents = buffer.get_text (start, end, false);
+ try {
+ file.replace_contents (current_contents.data, null, false,
+ GLib.FileCreateFlags.NONE, null, null);
+ print ("saved: %s\n", file.get_path ());
+ }
+ catch (GLib.Error err){
+ error ("%s\n", err.message);
+ }
+ }
+}
+
+/* This is the application */
+class MyApplication: Gtk.Application {
+ protected override void activate () {
+ new MyWindow (this).show_all;
+ }
+
+ const ActionEntry[] actions = {
+ { "quit", quit_cb }
+ };
+
+ void quit_cb (SimpleAction action, Variant? parameter) {
+ this.quit ();
+ }
+
+ protected override void startup () {
+ base.startup ();
+
+ /* Setup actions */
+ this.add_action_entries (actions, this);
+
+ /* Setup menus */
+ var builder = new Gtk.Builder ();
+ try {
+ builder.add_from_file ("filechooserdialog.ui");
+ } catch (GLib.Error err) {
+ error ("Unable to load file: %s\n", err.message);
+ }
+ this.app_menu = builder.get_object ("appmenu") as MenuModel;
+ }
+}
+
+/* main creates and runs the application. */
+public int main (string[] args) {
+ return new MyApplication ().run (args);
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]