|
Packit |
1470ea |
class MyWindow: Gtk.ApplicationWindow {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* MyWindow instance variables. */
|
|
Packit |
1470ea |
GLib.File? file;
|
|
Packit |
1470ea |
Gtk.TextBuffer buffer;
|
|
Packit |
1470ea |
Gtk.TextView textview;
|
|
Packit |
1470ea |
Gtk.ScrolledWindow scrolled_window;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Create ActionEntries. */
|
|
Packit |
1470ea |
const ActionEntry[] actions = {
|
|
Packit |
1470ea |
{ "new", new_cb },
|
|
Packit |
1470ea |
{ "open", open_cb },
|
|
Packit |
1470ea |
{ "save", save_cb },
|
|
Packit |
1470ea |
{ "save-as", save_as_cb }
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Constructor creates MyWindow, and add the scrolled_window. */
|
|
Packit |
1470ea |
internal MyWindow (MyApplication app) {
|
|
Packit |
1470ea |
Object (application: app, title: "FileChooserDialog Example");
|
|
Packit |
1470ea |
this.set_default_size (400, 400);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Add the ActionEntries to MyWindow. */
|
|
Packit |
1470ea |
this.add_action_entries (actions, this);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
buffer = new Gtk.TextBuffer (null); //stores text to be displayed
|
|
Packit |
1470ea |
textview = new Gtk.TextView.with_buffer (buffer); //displays TextBuffer
|
|
Packit |
1470ea |
textview.set_wrap_mode (Gtk.WrapMode.WORD); //sets line wrapping
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
scrolled_window = new Gtk.ScrolledWindow (null, null);
|
|
Packit |
1470ea |
scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC,
|
|
Packit |
1470ea |
Gtk.PolicyType.AUTOMATIC);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
scrolled_window.add (textview);
|
|
Packit |
1470ea |
scrolled_window.set_border_width (5);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.add (scrolled_window);
|
|
Packit |
1470ea |
this.show_all ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
void new_cb (SimpleAction action, Variant? parameter) {
|
|
Packit |
1470ea |
file = null;
|
|
Packit |
1470ea |
buffer.set_text ("");
|
|
Packit |
1470ea |
print ("New file created\n");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Create FileChooserDialog in OPEN mode. */
|
|
Packit |
1470ea |
void open_cb (SimpleAction action, Variant? parameter) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var open_dialog = new Gtk.FileChooserDialog ("Pick a file",
|
|
Packit |
1470ea |
this as Gtk.Window,
|
|
Packit |
1470ea |
Gtk.FileChooserAction.OPEN,
|
|
Packit |
1470ea |
Gtk.Stock.CANCEL,
|
|
Packit |
1470ea |
Gtk.ResponseType.CANCEL,
|
|
Packit |
1470ea |
Gtk.Stock.OPEN,
|
|
Packit |
1470ea |
Gtk.ResponseType.ACCEPT);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
open_dialog.local_only = false; //allow for uri
|
|
Packit |
1470ea |
open_dialog.set_modal (true);
|
|
Packit |
1470ea |
open_dialog.response.connect (open_response_cb);
|
|
Packit |
1470ea |
open_dialog.show ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Either open the file and load the file contents or cancel. */
|
|
Packit |
1470ea |
void open_response_cb (Gtk.Dialog dialog, int response_id) {
|
|
Packit |
1470ea |
var open_dialog = dialog as Gtk.FileChooserDialog;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
switch (response_id) {
|
|
Packit |
1470ea |
case Gtk.ResponseType.ACCEPT: //open the file
|
|
Packit |
1470ea |
file = open_dialog.get_file();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
uint8[] file_contents;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
try {
|
|
Packit |
1470ea |
file.load_contents (null, out file_contents, null);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
catch (GLib.Error err) { //handle the exception
|
|
Packit |
1470ea |
error ("%s\n", err.message);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
/* Set the buffer text to be the contents of the file. */
|
|
Packit |
1470ea |
buffer.set_text ((string) file_contents,
|
|
Packit |
1470ea |
file_contents.length);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
print ("opened: %s\n", (open_dialog.get_filename ()));
|
|
Packit |
1470ea |
break;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
case Gtk.ResponseType.CANCEL:
|
|
Packit |
1470ea |
print ("cancelled: FileChooserAction.OPEN\n");
|
|
Packit |
1470ea |
break;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
dialog.destroy ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Create FileChooserDialog in SAVE mode. */
|
|
Packit |
1470ea |
void save_as_cb (SimpleAction action, Variant? parameter) {
|
|
Packit |
1470ea |
var save_dialog = new Gtk.FileChooserDialog ("Pick a file",
|
|
Packit |
1470ea |
this as Gtk.Window,
|
|
Packit |
1470ea |
Gtk.FileChooserAction.SAVE,
|
|
Packit |
1470ea |
Gtk.Stock.CANCEL,
|
|
Packit |
1470ea |
Gtk.ResponseType.CANCEL,
|
|
Packit |
1470ea |
Gtk.Stock.SAVE,
|
|
Packit |
1470ea |
Gtk.ResponseType.ACCEPT);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
save_dialog.set_do_overwrite_confirmation (true);
|
|
Packit |
1470ea |
save_dialog.set_modal (true);
|
|
Packit |
1470ea |
if (file != null) {
|
|
Packit |
1470ea |
try {
|
|
Packit |
1470ea |
(save_dialog as Gtk.FileChooser).set_file (file);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
catch (GLib.Error error) {
|
|
Packit |
1470ea |
print ("%s\n", error.message);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
save_dialog.response.connect (save_as_response_cb);
|
|
Packit |
1470ea |
save_dialog.show ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
void save_as_response_cb (Gtk.Dialog dialog, int response_id) {
|
|
Packit |
1470ea |
var save_dialog = dialog as Gtk.FileChooserDialog;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
switch (response_id) {
|
|
Packit |
1470ea |
case Gtk.ResponseType.ACCEPT:
|
|
Packit |
1470ea |
file = save_dialog.get_file();
|
|
Packit |
1470ea |
this.save_to_file ();
|
|
Packit |
1470ea |
break;
|
|
Packit |
1470ea |
default:
|
|
Packit |
1470ea |
break;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
dialog.destroy ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Save the existing contents to the file.
|
|
Packit |
1470ea |
* If file does not exist, call save_as_cb.
|
|
Packit |
1470ea |
*/
|
|
Packit |
1470ea |
void save_cb (SimpleAction action, Variant? parameter) {
|
|
Packit |
1470ea |
if (file != null) {
|
|
Packit |
1470ea |
this.save_to_file ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
else {
|
|
Packit |
1470ea |
save_as_cb (action, parameter);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
void save_to_file (){
|
|
Packit |
1470ea |
Gtk.TextIter start;
|
|
Packit |
1470ea |
Gtk.TextIter end;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
buffer.get_bounds (out start, out end);
|
|
Packit |
1470ea |
string current_contents = buffer.get_text (start, end, false);
|
|
Packit |
1470ea |
try {
|
|
Packit |
1470ea |
file.replace_contents (current_contents.data, null, false,
|
|
Packit |
1470ea |
GLib.FileCreateFlags.NONE, null, null);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
print ("saved: %s\n", file.get_path ());
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
catch (GLib.Error err) {
|
|
Packit |
1470ea |
error ("%s\n", err.message);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* This is the application */
|
|
Packit |
1470ea |
class MyApplication: Gtk.Application {
|
|
Packit |
1470ea |
protected override void activate () {
|
|
Packit |
1470ea |
new MyWindow (this).show_all;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const ActionEntry[] actions = {
|
|
Packit |
1470ea |
{ "quit", quit_cb }
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
void quit_cb (SimpleAction action, Variant? parameter) {
|
|
Packit |
1470ea |
this.quit ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
protected override void startup () {
|
|
Packit |
1470ea |
base.startup ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Setup actions */
|
|
Packit |
1470ea |
this.add_action_entries (actions, this);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Setup menus */
|
|
Packit |
1470ea |
var builder = new Gtk.Builder ();
|
|
Packit |
1470ea |
try {
|
|
Packit |
1470ea |
builder.add_from_file ("filechooserdialog.ui");
|
|
Packit |
1470ea |
} catch (GLib.Error err) {
|
|
Packit |
1470ea |
error ("Unable to load file: %s\n", err.message);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
this.app_menu = builder.get_object ("appmenu") as MenuModel;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* main creates and runs the application. */
|
|
Packit |
1470ea |
public int main (string[] args) {
|
|
Packit |
1470ea |
return new MyApplication ().run (args);
|
|
Packit |
1470ea |
}
|