// Оригинальная версия написана Cristian Forsberg
// и находится по адресу 
//http://www.businessanyplace.net/?p=spfileman


// Переделано и переведено на русский для Windows Mobile 6
// Visual Studio 2008
// Александр Климов

//------------------------------------------------------------------------------
/// <copyright from='2004' to='2005' company='Christian Forsberg'>
///		Copyright (c) Christian Forsberg, chris@businessanyplace.net.
///
///   This program is distributed in the hope that it will be useful,
///   but WITHOUT ANY WARRANTY; without even the implied warranty of
///   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
/// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace FileManager
{
	/// <summary>
	/// This is the rename form.
	/// </summary>
  public class PropertiesForm : System.Windows.Forms.Form
  {
    private Form parentForm;
    private FileInfo fileInfo;
    private SetNameDelegate setNameCallback;
    private SetAttributesDelegate setAttributesCallback;

    #region Windows Form Designer declarations

    private System.Windows.Forms.MainMenu mainMenu;
    private System.Windows.Forms.MenuItem doneMenuItem;
    private System.Windows.Forms.Panel pnlForm;
    private System.Windows.Forms.VScrollBar vsbForm;
    private System.Windows.Forms.TextBox nameTextBox;
    private System.Windows.Forms.Label attributesLabel;
    private System.Windows.Forms.Label createdLabel;
    private System.Windows.Forms.Label createdLabelLabel;
    private System.Windows.Forms.Label sizeLabel;
    private System.Windows.Forms.Label sizeLabelLabel;
    private System.Windows.Forms.Label nameLabel;
    private System.Windows.Forms.CheckBox archiveCheckBox;
    private System.Windows.Forms.CheckBox readOnlyCheckBox;
    private System.Windows.Forms.CheckBox hiddenCheckBox;
    private System.Windows.Forms.TextBox locationTextBox;
    private System.Windows.Forms.Label locationLabel;
    private System.Windows.Forms.MenuItem cancelMenuItem;
    #endregion

    public PropertiesForm(Form parentForm, FileInfo fileInfo, SetNameDelegate setNameCallback, SetAttributesDelegate setAttributesCallback)
    {
      this.parentForm = parentForm;
      this.fileInfo = fileInfo;
      this.setNameCallback = setNameCallback;
      this.setAttributesCallback = setAttributesCallback;

      InitializeComponent();
    }

    #region Dispose
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      base.Dispose( disposing );
    }
    #endregion

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.mainMenu = new System.Windows.Forms.MainMenu();
        this.doneMenuItem = new System.Windows.Forms.MenuItem();
        this.cancelMenuItem = new System.Windows.Forms.MenuItem();
        this.pnlForm = new System.Windows.Forms.Panel();
        this.nameLabel = new System.Windows.Forms.Label();
        this.nameTextBox = new System.Windows.Forms.TextBox();
        this.locationLabel = new System.Windows.Forms.Label();
        this.locationTextBox = new System.Windows.Forms.TextBox();
        this.attributesLabel = new System.Windows.Forms.Label();
        this.sizeLabelLabel = new System.Windows.Forms.Label();
        this.sizeLabel = new System.Windows.Forms.Label();
        this.createdLabelLabel = new System.Windows.Forms.Label();
        this.createdLabel = new System.Windows.Forms.Label();
        this.archiveCheckBox = new System.Windows.Forms.CheckBox();
        this.readOnlyCheckBox = new System.Windows.Forms.CheckBox();
        this.hiddenCheckBox = new System.Windows.Forms.CheckBox();
        this.vsbForm = new System.Windows.Forms.VScrollBar();
        // 
        // mainMenu
        // 
        this.mainMenu.MenuItems.Add(this.doneMenuItem);
        this.mainMenu.MenuItems.Add(this.cancelMenuItem);
        // 
        // doneMenuItem
        // 
        this.doneMenuItem.Text = "Готово";
        this.doneMenuItem.Click += new System.EventHandler(this.doneMenuItem_Click);
        // 
        // cancelMenuItem
        // 
        this.cancelMenuItem.Text = "Отмена";
        this.cancelMenuItem.Click += new System.EventHandler(this.cancelMenuItem_Click);
        // 
        // pnlForm
        // 
        this.pnlForm.Controls.Add(this.hiddenCheckBox);
        this.pnlForm.Controls.Add(this.readOnlyCheckBox);
        this.pnlForm.Controls.Add(this.archiveCheckBox);
        this.pnlForm.Controls.Add(this.createdLabel);
        this.pnlForm.Controls.Add(this.createdLabelLabel);
        this.pnlForm.Controls.Add(this.sizeLabel);
        this.pnlForm.Controls.Add(this.sizeLabelLabel);
        this.pnlForm.Controls.Add(this.attributesLabel);
        this.pnlForm.Controls.Add(this.locationTextBox);
        this.pnlForm.Controls.Add(this.locationLabel);
        this.pnlForm.Controls.Add(this.nameTextBox);
        this.pnlForm.Controls.Add(this.nameLabel);
        this.pnlForm.Location = new System.Drawing.Point(0, 0);
        this.pnlForm.Size = new System.Drawing.Size(171, 272);
        // 
        // nameLabel
        // 
        this.nameLabel.Location = new System.Drawing.Point(3, 2);
        this.nameLabel.Size = new System.Drawing.Size(152, 22);
        this.nameLabel.Text = "Name:";
        // 
        // nameTextBox
        // 
        this.nameTextBox.Location = new System.Drawing.Point(4, 24);
        this.nameTextBox.Size = new System.Drawing.Size(152, 28);
        this.nameTextBox.GotFocus += new System.EventHandler(this.textBox_GotFocus);
        // 
        // locationLabel
        // 
        this.locationLabel.Location = new System.Drawing.Point(3, 46);
        this.locationLabel.Size = new System.Drawing.Size(152, 22);
        this.locationLabel.Text = "Location";
        // 
        // locationTextBox
        // 
        this.locationTextBox.Location = new System.Drawing.Point(4, 68);
        this.locationTextBox.Size = new System.Drawing.Size(152, 28);
        this.locationTextBox.GotFocus += new System.EventHandler(this.textBox_GotFocus);
        // 
        // attributesLabel
        // 
        this.attributesLabel.Location = new System.Drawing.Point(3, 178);
        this.attributesLabel.Size = new System.Drawing.Size(152, 22);
        this.attributesLabel.Text = "Attributes:";
        // 
        // sizeLabelLabel
        // 
        this.sizeLabelLabel.Location = new System.Drawing.Point(3, 90);
        this.sizeLabelLabel.Size = new System.Drawing.Size(152, 22);
        this.sizeLabelLabel.Text = "Size:";
        // 
        // sizeLabel
        // 
        this.sizeLabel.Location = new System.Drawing.Point(3, 112);
        this.sizeLabel.Size = new System.Drawing.Size(152, 22);
        this.sizeLabel.Text = "<Size>";
        // 
        // createdLabelLabel
        // 
        this.createdLabelLabel.Location = new System.Drawing.Point(3, 134);
        this.createdLabelLabel.Size = new System.Drawing.Size(152, 22);
        this.createdLabelLabel.Text = "Created:";
        // 
        // createdLabel
        // 
        this.createdLabel.Location = new System.Drawing.Point(3, 156);
        this.createdLabel.Size = new System.Drawing.Size(152, 22);
        this.createdLabel.Text = "<Created>";
        // 
        // archiveCheckBox
        // 
        this.archiveCheckBox.Location = new System.Drawing.Point(4, 200);
        this.archiveCheckBox.Size = new System.Drawing.Size(163, 22);
        this.archiveCheckBox.Text = "Archive";
        this.archiveCheckBox.GotFocus += new System.EventHandler(this.checkBox_GotFocus);
        // 
        // readOnlyCheckBox
        // 
        this.readOnlyCheckBox.Location = new System.Drawing.Point(4, 224);
        this.readOnlyCheckBox.Size = new System.Drawing.Size(163, 22);
        this.readOnlyCheckBox.Text = "Read-only";
        this.readOnlyCheckBox.GotFocus += new System.EventHandler(this.checkBox_GotFocus);
        // 
        // hiddenCheckBox
        // 
        this.hiddenCheckBox.Location = new System.Drawing.Point(4, 248);
        this.hiddenCheckBox.Size = new System.Drawing.Size(163, 22);
        this.hiddenCheckBox.Text = "Hidden";
        this.hiddenCheckBox.GotFocus += new System.EventHandler(this.checkBox_GotFocus);
        // 
        // vsbForm
        // 
        this.vsbForm.LargeChange = 1;
        this.vsbForm.Location = new System.Drawing.Point(171, 0);
        this.vsbForm.Maximum = 2;
        this.vsbForm.Size = new System.Drawing.Size(6, 180);
        this.vsbForm.ValueChanged += new System.EventHandler(this.vsbForm_ValueChanged);
        // 
        // PropertiesForm
        // 
        this.ClientSize = new System.Drawing.Size(176, 180);
        this.Controls.Add(this.vsbForm);
        this.Controls.Add(this.pnlForm);
        this.Menu = this.mainMenu;
        this.Text = "Properties";
        this.Closing += new System.ComponentModel.CancelEventHandler(this.PropertiesForm_Closing);
        this.Load += new System.EventHandler(this.PropertiesForm_Load);

    }
    #endregion

    private void PropertiesForm_Load(object sender, System.EventArgs e)
    {
      // Correct for designer flaw (not according to UI standard)
      nameTextBox.Height = 22;
      locationTextBox.Height = 22;

      // Get file length
      long fileLength = 0;
      if(!((fileInfo.Attributes & FileAttributes.Directory) > 0))
        fileLength = fileInfo.Length;

      nameTextBox.Text = fileInfo.Name;
      locationTextBox.Text = Path.GetDirectoryName(fileInfo.FullName);
      sizeLabel.Text = string.Format("{0:0}", fileLength / 1024L) + " KB";
      createdLabel.Text = fileInfo.CreationTime.ToString();

      if((fileInfo.Attributes & FileAttributes.Archive) > 0)
        archiveCheckBox.Checked = true;
      if((fileInfo.Attributes & FileAttributes.ReadOnly) > 0)
        readOnlyCheckBox.Checked = true;
      if((fileInfo.Attributes & FileAttributes.Hidden) > 0)
        hiddenCheckBox.Checked = true;
//      if((fileInfo.Attributes & FileAttributes.System) > 0)
//        systemCheckBox.Checked = true;

      if((fileInfo.Attributes & FileAttributes.System) > 0)
      {
        nameTextBox.Enabled = false;
        locationTextBox.Enabled = false;
        archiveCheckBox.Enabled = false;
        readOnlyCheckBox.Enabled = false;
        hiddenCheckBox.Enabled = false;
        doneMenuItem.Enabled = false;
        vsbForm.Focus();
      }

      this.parentForm.Hide();
      Cursor.Current = Cursors.Default;
    }

    #region Menu options
    private void doneMenuItem_Click(object sender, System.EventArgs e)
    {
      setNameCallback(nameTextBox.Text);

      FileAttributes fa = fileInfo.Attributes;
      if(archiveCheckBox.Checked)
        fa |= FileAttributes.Archive;
      else
        if((fa & FileAttributes.Archive) > 0)
          fa -= FileAttributes.Archive;
      if(readOnlyCheckBox.Checked)
        fa |= FileAttributes.ReadOnly;
      else
        if((fa & FileAttributes.ReadOnly) > 0)
          fa -= FileAttributes.ReadOnly;
      if(hiddenCheckBox.Checked)
        fa |= FileAttributes.Hidden;
      else
        if((fa & FileAttributes.Hidden) > 0)
          fa -= FileAttributes.Hidden;
//      if(systemCheckBox.Checked)
//        fa |= FileAttributes.System;
//      else
//        if((fa & FileAttributes.System) > 0)
//          fa -= FileAttributes.System;
      setAttributesCallback(fa);

      this.DialogResult = DialogResult.OK;
      this.Close();
    }

    private void cancelMenuItem_Click(object sender, System.EventArgs e)
    {
      this.DialogResult = DialogResult.Cancel;
      this.Close();
    }
    #endregion

    #region Scrolling
    private void checkBox_GotFocus(object sender, System.EventArgs e)
    {
      if(nameTextBox.Enabled)
        vsbForm.Value = 2;
    }

    private void textBox_GotFocus(object sender, System.EventArgs e)
    {
      if(nameTextBox.Enabled)
        vsbForm.Value = 0;
    }
    private void vsbForm_ValueChanged(object sender, System.EventArgs e)
    {
      pnlForm.Top = -vsbForm.Value * 44;
    }
    #endregion

    private void PropertiesForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      parentForm.Show();
    }
  }
}
