using Gtk;
using System;
using TextEditorWidget;
using VPKSoftUtils;

namespace TextEditorWidgetSample
{
    /// <summary>
    /// Description of MainWindow.
    /// </summary>
    public class EditorApp : Window
    {
        EditorWidget Ew = new EditorWidget(false);
        public EditorApp() : base("EditorWidget v.0.0.2 sample application   (c) VPKSoft 2007")
        {
            DeleteEvent += new DeleteEventHandler(MainWindowDeleteEvent);
            Ew.SaveRequest = SaveFile;
            Ew.LoadRequest = LoadFile;
            Add(Ew);
            ShowAll();
            SetSizeRequest(600, 500);
        }
        
        [STAThread]
        public static void Main(string[] arg)
        {
            Application.Init();
            new EditorApp();
            Application.Run();
        }
        
        void MainWindowDeleteEvent(object o, DeleteEventArgs args)
        {
            Application.Quit();
            args.RetVal = true;
        }

        void SaveFile()
        {
            FileChooserDialog Fcd = new FileChooserDialog ("Save file", null, FileChooserAction.Save);
            Fcd.AddButton(Stock.Cancel, ResponseType.Cancel);
            Fcd.AddButton(Stock.Open, ResponseType.Ok);
            int RetVal = Fcd.Run();
            if ((ResponseType)RetVal == ResponseType.Ok) {
                Ew.Editor.SaveTextToFile(Fcd.Filename, Ew.DefaultAttr);
            }
            Fcd.Destroy();            
        }
        
        void LoadFile()
        {
            try {
                FileChooserDialog Fcd = new FileChooserDialog ("Open file", null, FileChooserAction.Open);
                Fcd.AddButton(Stock.Cancel, ResponseType.Cancel);
                Fcd.AddButton(Stock.Open, ResponseType.Ok);
                int RetVal = Fcd.Run();
                if ((ResponseType)RetVal == ResponseType.Ok) {
                    Ew.Editor.LoadTextFromFile(Fcd.Filename);
                }
                Fcd.Destroy();            
            } catch (Exception E) {
                Utils.ShowMessage(E.Message);
            }
        }
    }
}