/******************************************************************************
 * FileDialogExtender.cs 
 * 
 * <Copyright and License> 
 * 
 * This file is part of GODS, a package for annotating medical images. 
 * 
 * Copyright (C) 2004 
 * Xuedong Chen 
 * Heri Mariwa 
 * Kimmy (Hsiang-ling) Lin 
 * Rui Yu 
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version. 
 * 
 * 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. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 *  
 * For further questions about this file, please send email to 
 * gods@cs.umb.edu or visit us at http://gods.sourceforge.net.  
 ******************************************************************************/

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace GODS.MarkConfig
{
	public class FileDialogExtender
	{
		#region Enum DialogViewTypes

		/// <summary>
		/// Ansichtentypen der Listenanzeige von Dateidialogen
		/// </summary>
		public enum DialogViewTypes
		{
			/// <summary>
			/// Symbole
			/// </summary>
			Icons = 0x7029,
			/// <summary>
			/// Liste
			/// </summary>
			List  = 0x702b,
			/// <summary>
			/// Details
			/// </summary>
			Details= 0x702c,
			/// <summary>
			/// Minitauransicht
			/// </summary>
			Thumbnails= 0x702d,
			/// <summary>
			/// Kacheln
			/// </summary>
			Tiles = 0x702e,
		}

		#endregion

		#region Fields

		private const uint WM_COMMAND = 0x0111;
		private uint _lastDialogHandle = 0;
		private DialogViewTypes _viewType;
		private bool _enabled;

		#endregion

		#region DllImports

		[DllImport("user32.dll", EntryPoint="SendMessageA", CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Ansi)] 
		private static extern uint SendMessage(uint Hdc, uint Msg_Const, uint wParam, uint lParam);

		[DllImport("user32.dll", EntryPoint="FindWindowExA", CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Ansi)] 
		private static extern uint FindWindowEx(uint hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

		#endregion

		#region Constructors & Destructors

		public FileDialogExtender() : this(DialogViewTypes.List, false) {}

		public FileDialogExtender(DialogViewTypes viewType) : this(viewType, false) {}

		public FileDialogExtender(DialogViewTypes viewType, bool enabled)
		{
			_viewType = viewType;
			Enabled = enabled;
		}

		#endregion

		#region Properties

		public DialogViewTypes DialogViewType 
		{
			get { return _viewType; }
			set { _viewType = value; }
		}

		public bool Enabled 
		{
			get { return _enabled; }
			set { _enabled = value; }
		}

		#endregion

		#region Methods

		/// <summary>
		/// Hash to be called from the overriden WndProc in the parent window of the dialog
		/// </summary>
		/// <param name="m"></param>
		public void WndProc(ref Message m)
		{
			if (!_enabled)
				return;

			if (m.Msg == 289) //Notify of message loop
			{
				uint dialogHandle = (uint)m.LParam;	//handle of the file dialog

				if (dialogHandle != _lastDialogHandle) //only when not already changed
				{			
					//get handle of the listview
					uint listviewHandle = FindWindowEx(dialogHandle, 0, "SHELLDLL_DefView", "");

					//send message to listview
					SendMessage(listviewHandle, WM_COMMAND, (uint)_viewType, 0);

					//remember last handle
					_lastDialogHandle = dialogHandle;
				}
			}
		}

		#endregion
	}
}
