using System; using Gtk; using Glade; class SimpleViewer2 { [Widget] Window Window1; [Widget] AboutDialog aboutdialog1; [Widget] TextView textview1; [Widget] CheckMenuItem menuWordWrap; public static void Main(string[] args) { new SimpleViewer2(args); } public SimpleViewer2(string[] args) { try { Application.Init(); Glade.XML guiXml = new Glade.XML("SimpleViewer2.glade", "Window1", null); guiXml.Autoconnect(this); if( args.Length > 0 ) { loadFile(args[0]); } Application.Run(); } catch( Exception e ) { Console.WriteLine("Error running application:\n\n" + e); } } public void OnWindow1DeleteEvent(object o, DeleteEventArgs args) { Console.WriteLine("OnWindow1DeleteEvent start"); args.RetVal = doCloseApplication(); Console.WriteLine("OnWindow1DeleteEvent done: {0}", args.RetVal); } private bool doCloseApplication() { Console.WriteLine("Application Quitting"); // TODO: Any checks to determine whether or not the application // should stay active.. //if( test-for-shutdown fails ) // return false; // By default, quit application and signal that we should // close the main window. Application.Quit(); return true; } public void OnFileOpenEvent(object o, EventArgs args) { Console.WriteLine("OnFileOpenEvent start"); FileChooserDialog fc = new FileChooserDialog("Select file to open", Window1, FileChooserAction.Open, "Cancel",ResponseType.Cancel, "Open",ResponseType.Accept); int resp = fc.Run(); fc.Hide(); if( resp == (int)ResponseType.Accept ) { loadFile(fc.Filename); } fc.Destroy(); Console.WriteLine("OnFileOpenEvent done"); } private bool loadFile( string filename ) { int err = 0; try { System.IO.StreamReader sr = new System.IO.StreamReader(filename); textview1.Buffer.Text = sr.ReadToEnd(); sr.Close(); } catch( UnauthorizedAccessException ) { showError("You are not authorized to access this file."); err++; } catch( Exception e ) { showError("Unexpected error while loading the file, please contact the author with the following information:\n\n" + e); err++; } return err == 0; } private void showError( string s ) { MessageDialog md = new MessageDialog(Window1, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close, s); md.Run(); md.Destroy(); } public void OnQuitEvent(object o, EventArgs args) { Console.WriteLine("OnQuitEvent start"); doCloseApplication(); Console.WriteLine("OnQuitEvent done"); } public void OnWordWrapActivatedEvent(object o, EventArgs args) { textview1.WrapMode = menuWordWrap.Active ? WrapMode.WordChar : WrapMode.None; } public void OnMenuHelpAboutActivatedEvent(object o, EventArgs args) { try { Console.WriteLine("OnMenuHelpAboutActivatedEvent start"); XML aboutXml = new XML("SimpleViewer2.glade", "aboutdialog1", null); aboutXml.Autoconnect(this); if( aboutdialog1 == null ) Console.WriteLine("aboutdialog1 is null (?)"); else { aboutdialog1.TransientFor = Window1; aboutdialog1.Run(); aboutdialog1.Destroy(); } Console.WriteLine("OnMenuHelpAboutActivatedEvent done"); } catch( Exception e ) { Console.WriteLine("Could not display the About dialog: {0}", e); } } }