/[RomCheater]/trunk/RomCheater/Docking/PIDSelector.cs
ViewVC logotype

Diff of /trunk/RomCheater/Docking/PIDSelector.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater/PIDSelector.cs	2012/05/10 11:16:03	107
+++ trunk/RomCheater/Docking/PIDSelector.cs	2012/05/28 04:14:03	156
@@ -8,15 +8,18 @@
 using System.Diagnostics;
 using RomCheater.PluginFramework.Interfaces;
 using RomCheater.PluginFramework.Core;
+using System.IO;
+using RomCheater.Logging;
+using WeifenLuo.WinFormsUI.Docking;
 
-namespace RomCheater
+namespace RomCheater.Docking
 {
-    public partial class PIDSelector : Form
+    public partial class PIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>
     {
-        private IConfigPlugin plugin = null;
+        //private IConfigPlugin plugin = null;
         //bool isInError = false;
-        public PIDSelector() { InitializeComponent(); this.SelectedPid = -1; }
-        public PIDSelector(IConfigPlugin plugin) : this() { this.plugin = plugin; }
+        public PIDSelector() { InitializeComponent(); this.SelectedPid = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
+        public PIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
         // unsued construtor (it's not implmented, may not be)
         [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
         public PIDSelector(int pid) : this() { this.SelectedPid = pid; }
@@ -26,40 +29,61 @@
         private int _SelectedPid;
         public int SelectedPid { get { return _SelectedPid; } set { _SelectedPid = value; } }
 
+        public IConfigPlugin AcceptedPlugin { get; set; }
+
+        public EventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged { get; set; }
+        
+
 
         public new void Show() { this.Show(null); }
-        public new void Show(IWin32Window owner) { this.PreInitShow(owner); }
-        public new DialogResult ShowDialog() { return this.ShowDialog(null); }
-        public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); }
-        private void PreInitShow(IWin32Window owner)
-        {
-            if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}.  Please start an instance of one.",plugin.ToString())); }
-            else { if (owner == null) { base.Show(); } else { base.Show(owner); } }
-        }
-        private DialogResult PreInitDialog(IWin32Window owner)
-        {
-            if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}.  Please start an instance of one.", plugin.ToString())); return DialogResult.Cancel; }
-            else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } }
-        }
+        //public new void Show(IWin32Window owner) { this.PreInitShow(owner); }
+        public new void Show(DockPanel panel) { this.PreInitShow(panel); }
+        //public new DialogResult ShowDialog() { return this.ShowDialog(null); }
+        //public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); }
+        //private void PreInitShow(IWin32Window owner)
+        //{
+        //    if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}.  Please start an instance of one.",plugin.ToString())); }
+        //    else { if (owner == null) { base.Show(); } else { base.Show(owner); } }
+        //}
+        private void PreInitShow(DockPanel panel)
+        {
+            //if (panel == null) return;
+            if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}.  Please start an instance of one.", AcceptedPlugin.ToString())); }
+            else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
+        }
+        //private DialogResult PreInitDialog(IWin32Window owner)
+        //{
+        //    if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}.  Please start an instance of one.", plugin.ToString())); return DialogResult.Cancel; }
+        //    else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } }
+        //}
         private void btnLargeIcon_Click(object sender, EventArgs e) { this.lstProcessList.View = View.LargeIcon; }
         private void btnDetails_Click(object sender, EventArgs e) { this.lstProcessList.View = View.Details; }
         private void btnSmallIcon_Click(object sender, EventArgs e) { this.lstProcessList.View = View.SmallIcon; }
         private void btnList_Click(object sender, EventArgs e) { this.lstProcessList.View = View.List; }
         private void btnTile_Click(object sender, EventArgs e) { this.lstProcessList.View = View.Tile; }
 
-        private void btnOK_Click(object sender, EventArgs e)
+        private void PerformListViewItemSelect(ListViewItem li)
         {
-            if (!(lstProcessList.SelectedItems.Count > 0)) return;
-            this.SelectedPid = Convert.ToInt32(lstProcessList.SelectedItems[0].SubItems[1].Text);
-            this.Close();
+            this.SelectedPid = Convert.ToInt32(li.SubItems[1].Text);
+            //this.Close();
+            if (this.OnSelectedProcessChanged != null)
+            {
+                this.OnSelectedProcessChanged(this, new ProcessChangedEventArgs(this.SelectedPid));
+            }
+            Process SelectedProcess = Process.GetProcessById(this.SelectedPid);
+            logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
         }
-
-        private void btnCancel_Click(object sender, EventArgs e)
+        private void btnOK_Click(object sender, EventArgs e)
         {
-            this.SelectedPid = -1;
-            this.Close();
+            
         }
 
+        //private void btnCancel_Click(object sender, EventArgs e)
+        //{
+        //    this.SelectedPid = -1;
+        //    //this.Close();
+        //}
+
         private void btnRefresh_Click(object sender, EventArgs e)
         {
             this.RefreshList();
@@ -68,9 +92,9 @@
         private bool RefreshList()
         {
             lstProcessList.Items.Clear();
-            this.plugin.Reload();
+            this.AcceptedPlugin.Reload();
             // create a selection of PCSX2 processes
-            List<ProcContainer> proc_list = plugin.ValidProcessesForPlugin;
+            List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
 
             if (!(proc_list.Count > 0)) { //MessageBox.Show("Could not find any PCSX2 Processes.  Please start an instance of one."); 
                 return false; }
@@ -78,36 +102,114 @@
             List<ListViewItem> items = new List<ListViewItem>();
             ImageList small_image_list = new ImageList();
             ImageList large_image_list = new ImageList();
-            foreach (ProcContainer p in proc_list) { this.AddProcessItem(p, ref items, out small_image_list, out large_image_list); }
+
+            int small_width = 32;
+            int large_width = 48;
+
+            small_image_list.ImageSize = new Size(small_width, small_width);
+            large_image_list.ImageSize = new Size(large_width, large_width);
+            foreach (ProcContainer p in proc_list) 
+            {
+                Bitmap small_image = null;
+                Bitmap large_image = null;
+                string image_key = string.Empty;
+                this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image);
+                if (image_key != string.Empty)
+                {
+                    if (small_image != null) { small_image_list.Images.Add(image_key, small_image); }
+                    if (large_image != null) { large_image_list.Images.Add(image_key, large_image); }
+                    ListViewItem item = new ListViewItem();
+                    // process name
+                    item.Text = p.Name;
+                    // pid
+                    item.SubItems.Add(p.ProcessInfo.Id.ToString());
+                    // FullPath
+                    item.SubItems.Add(p.FileName);
+                    // image key
+                    item.ImageKey = p.FileName;
+                    if (!items.Contains(item)) items.Add(item);
+                }
+                else
+                {
+                    //logger.Error.WriteLine("image_key is an empty string!");
+                    //if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); }
+                    //if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); }
+                }
+            }
             lstProcessList.SmallImageList = small_image_list;
             lstProcessList.LargeImageList = large_image_list;
             this.lstProcessList.Items.AddRange(items.ToArray());
+
+            if (lstProcessList.Items.Count > 0)
+            {
+                lstProcessList.Items[0].Selected = true;
+            }
+
             return true;
         }
 
-        private void AddProcessItem(ProcContainer p, ref  List<ListViewItem> items, out ImageList small_image_list, out ImageList large_image_list)
+        private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
         {
-            small_image_list = new ImageList();
-            large_image_list = new ImageList();
-            ListViewItem item = new ListViewItem();
-            // process
-            item.Text = p.Name;
-            if (p.ProcessIcon != null)
+            image_key = "";
+            small_image = null;
+            large_image = null;
+            ////small_image_list = new ImageList();
+            ////large_image_list = new ImageList();
+            //ListViewItem item = new ListViewItem();
+            //// process
+            //item.Text = p.Name;
+            //if (p.ProcessIcon != null)
+            //{   
+            //    //small_image_list.Images.Add(
+            //    //large_image_list.Images.Add(p.FileName, p.ProcessIcon);                
+            //    item.ImageKey = p.FileName;
+            //}
+            //// pid
+            //item.SubItems.Add(p.ProcessInfo.Id.ToString());
+            //// FullPath
+            //item.SubItems.Add(p.FileName);
+            //if (!items.Contains(item)) items.Add(item);
+            if (p != null)
             {
-                Bitmap small_image = ProcContainer.CreateIconFromProcess(p, 1);
-                Bitmap large_image = ProcContainer.CreateIconFromProcess(p, 2);
-                small_image_list.Images.Add(p.FileName, ProcContainer.CreateIconFromProcess(p, 1));
-                small_image_list.ImageSize = new Size(small_image.Width, small_image.Height);
-                large_image_list.Images.Add(p.FileName, ProcContainer.CreateIconFromProcess(p, 2));
-                large_image_list.ImageSize = new Size(large_image.Width, large_image.Height);
-                item.ImageKey = p.FileName;
+                if (p.ProcessIcon != null)
+                {
+                    image_key = p.FileName;
+                    small_image = new Bitmap(p.ProcessIcon, small);
+                    large_image = new Bitmap(p.ProcessIcon, large);
+                }
             }
-            // pid
-            item.SubItems.Add(p.ProcessInfo.Id.ToString());
-            // FullPath
-            item.SubItems.Add(p.FileName);
-            if (!items.Contains(item)) items.Add(item);
+
+        }
+
+        private void PIDSelector_Load(object sender, EventArgs e)
+        {
+            if (AcceptedPlugin == null || this.DesignMode) return;
+            btnRefresh.PerformClick();
+        }
+
+
+        private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
+        {
+            if (!e.IsSelected) return;
+            PerformListViewItemSelect(e.Item);
         }
+
+        private void lstProcessList_ItemActivate(object sender, EventArgs e)
+        {
+            ListViewItem Item = lstProcessList.SelectedItems[0];
+            PerformListViewItemSelect(Item);
+        }
+
+
+    }
+
+    #region eventargs
+    public class ProcessChangedEventArgs : EventArgs
+    {
+        public ProcessChangedEventArgs() : this(-1) { }
+        public ProcessChangedEventArgs(int pid) { this.ProcessID = pid; }
+        public int ProcessID { get; private set; }
     }
+    #endregion
 }
 

 

  ViewVC Help
Powered by ViewVC 1.1.22