//
//  The Pimp      The Pathetic Image Manipulation Program
//  Copyright (c) 2008 Sam Hocevar <sam@zoy.org>
//                All Rights Reserved
//
//  $Id$
//
//  This library is free software. It comes without any warranty, to
//  the extent permitted by applicable law. You can redistribute it
//  and/or modify it under the terms of the Do What The Fuck You Want
//  To Public License, Version 2, as published by Sam Hocevar. See
//  http://sam.zoy.org/wtfpl/COPYING for more details.
//

using System;
using Gtk;
using Pipi;
using ThePimp;

namespace ThePimp
{
    [Gtk.Binding(Gdk.Key.F11, "ToggleFullScreen")]

    public partial class MainWindow: Gtk.Window
    {
        public MainWindow (): base (Gtk.WindowType.Toplevel)
        {
            Build ();
            vpaned1.Add1(new ToolBox());
        }

        protected void OnDeleteEvent (object sender, DeleteEventArgs a)
        {
            Application.Quit ();
            a.RetVal = true;
        }

        protected virtual void OnOpenActionActivated (object sender, System.EventArgs e)
        {
            string s = OpenFile.GetChoice();
            if(s == null)
                return;

            Pipi.Picture p = Pipi.Picture.Load(s);
            if(p == null)
            {
                new ErrorWindow("Could not open \"" + s + "\". Check the file format.");
                return;
            }

            while(notebook1.NPages > 0)
                notebook1.RemovePage(0);
            int n = notebook1.AppendPage(new PictureView(p),
                                         new Label(p.FileName));
            notebook1.Page = n;
        }

        protected virtual void OnNewActionActivated (object sender, System.EventArgs e)
        {
            NewFile dialog = new NewFile();

            string s = dialog.GetChoice();
            dialog.Destroy();
            if(s == null)
                return;

            Pipi.Picture p = Pipi.Picture.Load(s);
            if(p == null)
            {
                new ErrorWindow("Could not create image.");
                return;
            }

            while(notebook1.NPages > 0)
                notebook1.RemovePage(0);
            int n = notebook1.AppendPage(new PictureView(p),
                                         new Label(p.FileName));
            notebook1.Page = n;
        }

        protected virtual void OnQuitActionActivated (object sender, System.EventArgs e)
        {
            Application.Quit ();
        }

        protected virtual void OnSaveAsActionActivated (object sender, System.EventArgs e)
        {
            if(notebook1.NPages <= 0)
                return;

            PictureView view = notebook1.CurrentPageWidget as PictureView;

            string s = OpenFile.GetChoice();
            if(s == null)
                return;

            view.Picture.Save(s);
        }

        protected virtual void OnAboutActionActivated (object sender, System.EventArgs e)
        {
            new AboutWindow();
        }

        protected virtual void OnSaveActionActivated (object sender, System.EventArgs e)
        {
        }

        private bool _fullscreen = false;
        protected virtual void ToggleFullScreen()
        {
            _fullscreen = !_fullscreen;

            if(_fullscreen)
                Fullscreen();
            else
                Unfullscreen();
        }

        protected virtual void OnFullscreenActionActivated (object sender, System.EventArgs e)
        {
            ToggleFullScreen();
        }
    }
}