#include <gtk/gtk.h>
 #include <libchimara/chimara-glk.h>
 #include <libchimara/chimara-if.h>
+#include <config.h>
 #include "error.h"
 
-void 
-on_open_activate(GtkAction *action, ChimaraGlk *glk) 
+/* If a game is running in @glk, warn the user that they will quit the currently
+running game if they open a new one. Returns TRUE if no game was running.
+Returns FALSE if the user cancelled. Returns TRUE and shuts down the running
+game if the user wishes to continue. */
+static gboolean
+confirm_open_new_game(ChimaraGlk *glk)
 {
+       g_return_val_if_fail(glk && CHIMARA_IS_GLK(glk), FALSE);
+       
        GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
        
        if(chimara_glk_get_running(glk)) {
                gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_OPEN, GTK_RESPONSE_OK);
                gint response = gtk_dialog_run(GTK_DIALOG(dialog));
                gtk_widget_destroy(dialog);
-
+               
                if(response != GTK_RESPONSE_OK)
-                       return;
+                       return FALSE;
 
                chimara_glk_stop(glk);
                chimara_glk_wait(glk);
        }
+       return TRUE;
+}
+
+void 
+on_open_activate(GtkAction *action, ChimaraGlk *glk) 
+{
+       GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
+       
+       if(!confirm_open_new_game(glk))
+               return;
 
        GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
            window,
        if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
                GError *error = NULL;
                gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
-               if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error))
-                       error_dialog(window, error, _("Could not open game file."));
+               if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
+                       error_dialog(window, error, _("Could not open game file '%s': "), filename);
+                       g_free(filename);
+                       gtk_widget_destroy(dialog);
+                       return;
+               }
+               
+               /* Add file to recent files list */
+               GtkRecentManager *manager = gtk_recent_manager_get_default();
+               gchar *uri;
+               
+               if(!(uri = g_filename_to_uri(filename, NULL, &error)))
+                       g_warning(_("Could not convert filename '%s' to URI: %s"), filename, error->message);
+               else {
+                       if(!gtk_recent_manager_add_item(manager, uri))
+                               g_warning(_("Could not add URI '%s' to recent files list."), uri);
+                       g_free(uri);
+               }
                g_free(filename);
        }
        gtk_widget_destroy(dialog);
 void
 on_recent_item_activated(GtkRecentChooser *chooser, ChimaraGlk *glk)
 {
-
+       GError *error = NULL;
+       GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
+       gchar *uri = gtk_recent_chooser_get_current_uri(chooser);
+       gchar *filename;
+       if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
+               error_dialog(window, error, _("Could not open game file '%s': "), uri);
+               g_free(uri);
+               return;
+       }
+       
+       if(!confirm_open_new_game(glk)) {
+               g_free(filename);
+               g_free(uri);
+               return;
+       }
+       
+       if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
+               error_dialog(window, error, _("Could not open game file '%s': "), filename);
+               g_free(filename);
+               g_free(uri);
+               return;
+       }
+       g_free(filename);
+       
+       /* Add file to recent files list again, this updates it to most recently used */
+       GtkRecentManager *manager = gtk_recent_manager_get_default();
+       if(!gtk_recent_manager_add_item(manager, uri))
+               g_warning(_("Could not add URI '%s' to recent files list."), uri);
+       
+       g_free(uri);
 }
 
 void
        gtk_main_quit();
 }
 
-void
-on_cut_activate(GtkAction *action, ChimaraGlk *glk)
-{
-
-}
-
 void
 on_copy_activate(GtkAction *action, ChimaraGlk *glk)
 {
-
+       GtkWindow *toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
+       GtkWidget *focus = gtk_window_get_focus(toplevel);
+       /* Call "copy clipboard" on any widget that defines it */
+       if(GTK_IS_LABEL(focus) || GTK_IS_ENTRY(focus) || GTK_IS_TEXT_VIEW(focus))
+               g_signal_emit_by_name(focus, "copy-clipboard");
 }
 
 void
 on_paste_activate(GtkAction *action, ChimaraGlk *glk)
 {
-
+       GtkWindow *toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
+       GtkWidget *focus = gtk_window_get_focus(toplevel);
+       /* Call "paste clipboard" on any widget that defines it */
+       if(GTK_IS_ENTRY(focus) || GTK_IS_TEXT_VIEW(focus))
+               g_signal_emit_by_name(focus, "paste-clipboard");
 }
 
 void
 on_preferences_activate(GtkAction *action, ChimaraGlk *glk)
 {
-
+       extern GtkWidget *prefswindow;
+       gtk_window_present(GTK_WINDOW(prefswindow));
 }
 
 void
 void
 on_about_activate(GtkAction *action, ChimaraGlk *glk)
 {
-
+       extern GtkWidget *aboutwindow;
+       gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(aboutwindow), PACKAGE_VERSION);
+       gtk_window_present(GTK_WINDOW(aboutwindow));
 }
 
 gboolean 
 
     <property name="stock_id">gtk-media-stop</property>
     <signal name="activate" handler="on_quit_activate"/>
   </object>
-  <object class="GtkAction" id="cut">
-    <property name="label">Cu_t</property>
-    <property name="tooltip">Cut the selection</property>
-    <property name="stock_id">gtk-cut</property>
-    <signal name="activate" handler="on_cut_activate"/>
-  </object>
   <object class="GtkAction" id="copy">
     <property name="label">_Copy</property>
     <property name="tooltip">Copy the selection</property>
     <property name="stock_id">gtk-about</property>
     <signal name="activate" handler="on_about_activate"/>
   </object>
+  <object class="GtkAboutDialog" id="aboutwindow">
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">About Chimara</property>
+    <property name="window_position">center</property>
+    <property name="type_hint">dialog</property>
+    <property name="has_separator">False</property>
+    <property name="program_name">Chimara</property>
+    <property name="copyright" translatable="yes">© 2008–2009 Marijn van Vliet and Philip Chimento</property>
+    <property name="comments" translatable="yes">Interactive Fiction Player</property>
+    <property name="license" translatable="yes">Copyright (C) 2008, Philip Chimento and Marijn van Vliet.
+All rights reserved.
+
+Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+   list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation 
+   and/or other materials provided with the distribution.
+3. Neither of the names Philip Chimento or Marijn van Vliet, nor the name of any 
+   other contributor may be used to endorse or promote products derived from 
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+</property>
+    <property name="authors">Marijn van Vliet
+Philip Chimento</property>
+    <signal name="response" handler="gtk_widget_hide"/>
+    <signal name="delete_event" handler="gtk_widget_hide_on_delete"/>
+    <child internal-child="vbox">
+      <object class="GtkVBox" id="dialog-vbox1">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child>
+          <placeholder/>
+        </child>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area1">
+            <property name="visible">True</property>
+            <property name="layout_style">end</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+  <object class="GtkDialog" id="prefswindow">
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Chimara Preferences</property>
+    <property name="window_position">center</property>
+    <property name="type_hint">dialog</property>
+    <signal name="response" handler="gtk_widget_hide"/>
+    <signal name="delete_event" handler="gtk_widget_hide_on_delete"/>
+    <child internal-child="vbox">
+      <object class="GtkVBox" id="dialog-vbox2">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">9</property>
+        <child>
+          <object class="GtkVBox" id="vbox1">
+            <property name="visible">True</property>
+            <property name="orientation">vertical</property>
+            <property name="spacing">12</property>
+            <child>
+              <object class="GtkFrame" id="frame1">
+                <property name="visible">True</property>
+                <property name="label_xalign">0</property>
+                <property name="shadow_type">none</property>
+                <child>
+                  <object class="GtkAlignment" id="alignment1">
+                    <property name="visible">True</property>
+                    <property name="left_padding">12</property>
+                    <child>
+                      <object class="GtkVBox" id="vbox2">
+                        <property name="visible">True</property>
+                        <property name="orientation">vertical</property>
+                        <property name="spacing">8</property>
+                        <child>
+                          <object class="GtkCheckButton" id="checkbutton1">
+                            <property name="label" translatable="yes">_Awesome mode</property>
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="receives_default">False</property>
+                            <property name="use_underline">True</property>
+                            <property name="draw_indicator">True</property>
+                          </object>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">False</property>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkCheckButton" id="checkbutton2">
+                            <property name="label" translatable="yes">_Super-awesome mode</property>
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="receives_default">False</property>
+                            <property name="use_underline">True</property>
+                            <property name="draw_indicator">True</property>
+                          </object>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">False</property>
+                            <property name="position">1</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkCheckButton" id="checkbutton3">
+                            <property name="label" translatable="yes">_Ludicrous mode</property>
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="receives_default">False</property>
+                            <property name="use_underline">True</property>
+                            <property name="draw_indicator">True</property>
+                          </object>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">False</property>
+                            <property name="position">2</property>
+                          </packing>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child type="label">
+                  <object class="GtkLabel" id="label1">
+                    <property name="visible">True</property>
+                    <property name="label" translatable="yes"><b>Miscellaneous options</b></property>
+                    <property name="use_markup">True</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkFrame" id="frame2">
+                <property name="visible">True</property>
+                <property name="label_xalign">0</property>
+                <property name="shadow_type">none</property>
+                <child>
+                  <object class="GtkAlignment" id="alignment2">
+                    <property name="visible">True</property>
+                    <property name="left_padding">12</property>
+                    <child>
+                      <object class="GtkVBox" id="vbox3">
+                        <property name="visible">True</property>
+                        <property name="orientation">vertical</property>
+                        <child>
+                          <object class="GtkScrolledWindow" id="scrolledwindow1">
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="hscrollbar_policy">automatic</property>
+                            <property name="vscrollbar_policy">automatic</property>
+                            <child>
+                              <object class="GtkTreeView" id="treeview1">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                              </object>
+                            </child>
+                          </object>
+                          <packing>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkHButtonBox" id="hbuttonbox1">
+                            <property name="visible">True</property>
+                            <property name="spacing">7</property>
+                            <property name="layout_style">end</property>
+                            <child>
+                              <object class="GtkButton" id="button2">
+                                <property name="label">gtk-add</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="receives_default">True</property>
+                                <property name="use_stock">True</property>
+                              </object>
+                              <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">False</property>
+                                <property name="position">0</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkButton" id="button3">
+                                <property name="label">gtk-remove</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="receives_default">True</property>
+                                <property name="use_stock">True</property>
+                              </object>
+                              <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">False</property>
+                                <property name="position">1</property>
+                              </packing>
+                            </child>
+                          </object>
+                          <packing>
+                            <property name="position">1</property>
+                          </packing>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child type="label">
+                  <object class="GtkLabel" id="label2">
+                    <property name="visible">True</property>
+                    <property name="label" translatable="yes"><b>Styles</b></property>
+                    <property name="use_markup">True</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area2">
+            <property name="visible">True</property>
+            <property name="layout_style">end</property>
+            <child>
+              <object class="GtkButton" id="button1">
+                <property name="label">gtk-close</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_stock">True</property>
+                <signal name="clicked" handler="gtk_widget_hide" object="prefswindow"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="0">button1</action-widget>
+    </action-widgets>
+  </object>
 </interface>