From 21f2a6c92c0d2e5a9e8608fd69fbf8708a7e4750 Mon Sep 17 00:00:00 2001 From: Latif Khalifa Date: Mon, 18 Apr 2011 13:20:19 +0000 Subject: [PATCH] Rewrite friends console to work around mono 2.10 bugs. git-svn-id: https://radegast.googlecode.com/svn/trunk@853 f7a694da-4d33-11de-9ad6-1127a62b9fcd --- Radegast/GUI/Consoles/FriendsConsole.Designer.cs | 45 +++--- Radegast/GUI/Consoles/FriendsConsole.cs | 186 ++++++++++++----------- Radegast/GUI/Consoles/FriendsConsole.resx | 2 +- 3 files changed, 118 insertions(+), 115 deletions(-) diff --git a/Radegast/GUI/Consoles/FriendsConsole.Designer.cs b/Radegast/GUI/Consoles/FriendsConsole.Designer.cs index 9e6d334..fedf616 100644 --- a/Radegast/GUI/Consoles/FriendsConsole.Designer.cs +++ b/Radegast/GUI/Consoles/FriendsConsole.Designer.cs @@ -74,8 +74,7 @@ namespace Radegast this.chkModifyMyObjects = new System.Windows.Forms.CheckBox(); this.pnlFriendsRights = new System.Windows.Forms.GroupBox(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); - this.lvwFriends = new Radegast.ListViewNoFlicker(); - this.chName = new System.Windows.Forms.ColumnHeader(); + this.listFriends = new System.Windows.Forms.ListBox(); this.pnlActions.SuspendLayout(); this.pnlFriendsRights.SuspendLayout(); this.SuspendLayout(); @@ -210,36 +209,27 @@ namespace Radegast this.imageList1.Images.SetKeyName(0, "GreenOrbFaded_16.png"); this.imageList1.Images.SetKeyName(1, "GreenOrb_16.png"); // - // lvwFriends + // listFriends // - this.lvwFriends.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + this.listFriends.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); - this.lvwFriends.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { - this.chName}); - this.lvwFriends.FullRowSelect = true; - this.lvwFriends.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; - this.lvwFriends.HideSelection = false; - this.lvwFriends.LargeImageList = this.imageList1; - this.lvwFriends.Location = new System.Drawing.Point(0, 0); - this.lvwFriends.Name = "lvwFriends"; - this.lvwFriends.Size = new System.Drawing.Size(203, 466); - this.lvwFriends.SmallImageList = this.imageList1; - this.lvwFriends.TabIndex = 0; - this.lvwFriends.UseCompatibleStateImageBehavior = false; - this.lvwFriends.View = System.Windows.Forms.View.Details; - this.lvwFriends.MouseClick += new System.Windows.Forms.MouseEventHandler(this.lvwFriends_MouseClick); - this.lvwFriends.SelectedIndexChanged += new System.EventHandler(this.lvwFriends_SelectedIndexChanged); - this.lvwFriends.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lvwFriends_KeyDown); - // - // chName - // - this.chName.Text = ""; - this.chName.Width = 195; + this.listFriends.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; + this.listFriends.FormattingEnabled = true; + this.listFriends.ItemHeight = 18; + this.listFriends.Location = new System.Drawing.Point(3, 9); + this.listFriends.Name = "listFriends"; + this.listFriends.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; + this.listFriends.Size = new System.Drawing.Size(200, 454); + this.listFriends.TabIndex = 0; + this.listFriends.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listFriends_DrawItem); + this.listFriends.SelectedIndexChanged += new System.EventHandler(this.listFriends_SelectedIndexChanged); + this.listFriends.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listFriends_MouseDown); + this.listFriends.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listFriends_KeyDown); // // FriendsConsole // + this.Controls.Add(this.listFriends); this.Controls.Add(this.pnlFriendsRights); - this.Controls.Add(this.lvwFriends); this.Controls.Add(this.pnlActions); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "FriendsConsole"; @@ -265,8 +255,7 @@ namespace Radegast public Button btnPay; public Button btnRemove; public GroupBox pnlFriendsRights; - public ListViewNoFlicker lvwFriends; - public ColumnHeader chName; public ImageList imageList1; + private ListBox listFriends; } } diff --git a/Radegast/GUI/Consoles/FriendsConsole.cs b/Radegast/GUI/Consoles/FriendsConsole.cs index 5b491d8..55960e9 100644 --- a/Radegast/GUI/Consoles/FriendsConsole.cs +++ b/Radegast/GUI/Consoles/FriendsConsole.cs @@ -38,7 +38,7 @@ using OpenMetaverse.StructuredData; namespace Radegast { - public partial class FriendsConsole : UserControl, IContextMenuProvider + public partial class FriendsConsole : UserControl { private RadegastInstance instance; private GridClient client { get { return instance.Client; } } @@ -70,7 +70,29 @@ namespace Radegast client.Friends.FriendshipResponse += new EventHandler(Friends_FriendshipResponse); client.Friends.FriendNames += new EventHandler(Friends_FriendNames); Load += new EventHandler(FriendsConsole_Load); - lvwFriends.ListViewItemSorter = new FriendsSorter(); + instance.Names.NameUpdated += new EventHandler(Names_NameUpdated); + } + + void Names_NameUpdated(object sender, UUIDNameReplyEventArgs e) + { + bool moded = false; + + foreach (var id in e.Names.Keys) + { + if (client.Friends.FriendList.ContainsKey(id)) + { + moded = true; + break; + } + } + + if (moded) + { + if (InvokeRequired) + BeginInvoke(new MethodInvoker(() => listFriends.Invalidate())); + else + listFriends.Invalidate(); + } } void FriendsConsole_Disposed(object sender, EventArgs e) @@ -85,51 +107,34 @@ namespace Radegast void FriendsConsole_Load(object sender, EventArgs e) { InitializeFriendsList(); - lvwFriends.Select(); + listFriends.Select(); } private void InitializeFriendsList() { if (!Monitor.TryEnter(lockOneAtaTime)) return; List friends = client.Friends.FriendList.FindAll(delegate(FriendInfo f) { return true; }); - - lvwFriends.BeginUpdate(); - foreach (FriendInfo friend in friends) - { - ListViewItem item; - if (lvwFriends.Items.ContainsKey(friend.UUID.ToString())) - { - item = lvwFriends.Items[friend.UUID.ToString()]; - if (friend.Name != null) item.Text = friend.Name; - item.ImageIndex = friend.IsOnline ? 1 : 0; - item.Tag = friend; - } - else + + friends.Sort((fi1, fi2) => { - item = new ListViewItem(); - item.Name = friend.UUID.ToString(); - if (friend.Name != null) item.Text = friend.Name; - item.ImageIndex = friend.IsOnline ? 1 : 0; - item.Tag = friend; - lvwFriends.Items.Add(item); + if (fi1.IsOnline && !fi2.IsOnline) + return -1; + else if (!fi1.IsOnline && fi2.IsOnline) + return 1; + else + return string.Compare(fi1.Name, fi2.Name); } - } + ); + + listFriends.BeginUpdate(); - List removed = new List(); - foreach (ListViewItem item in lvwFriends.Items) + listFriends.Items.Clear(); + foreach (FriendInfo friend in friends) { - UUID id = ((FriendInfo)item.Tag).UUID; - if (null == friends.Find((FriendInfo f) => { return f.UUID == id; })) - removed.Add(id); + listFriends.Items.Add(friend); } - foreach (UUID id in removed) - { - lvwFriends.Items.RemoveByKey(id.ToString()); - } - - lvwFriends.Sort(); - lvwFriends.EndUpdate(); + listFriends.EndUpdate(); Monitor.Exit(lockOneAtaTime); } @@ -209,18 +214,18 @@ namespace Radegast private void SetControls() { - if (lvwFriends.SelectedItems.Count == 0) + if (listFriends.SelectedItems.Count == 0) { pnlActions.Enabled = pnlFriendsRights.Enabled = false; } - else if (lvwFriends.SelectedItems.Count == 1) + else if (listFriends.SelectedItems.Count == 1) { pnlActions.Enabled = pnlFriendsRights.Enabled = true; btnProfile.Enabled = btnIM.Enabled = btnPay.Enabled = btnRemove.Enabled = true; - FriendInfo friend = (FriendInfo)lvwFriends.SelectedItems[0].Tag; + FriendInfo friend = (FriendInfo)listFriends.SelectedItems[0]; lblFriendName.Text = friend.Name + (friend.IsOnline ? " (online)" : " (offline)"); - + settingFriend = true; chkSeeMeOnline.Checked = friend.CanSeeMeOnline; chkSeeMeOnMap.Checked = friend.CanSeeMeOnMap; @@ -236,20 +241,11 @@ namespace Radegast } } - private void lvwFriends_SelectedIndexChanged(object sender, EventArgs e) - { - if (lvwFriends.SelectedItems.Count == 1) - selectedFriend = (FriendInfo)lvwFriends.SelectedItems[0].Tag; - else - selectedFriend = null; - SetControls(); - } - private void btnIM_Click(object sender, EventArgs e) { - if (lvwFriends.SelectedItems.Count == 1) + if (listFriends.SelectedItems.Count == 1) { - selectedFriend = (FriendInfo)lvwFriends.SelectedItems[0].Tag; + selectedFriend = (FriendInfo)listFriends.SelectedItems[0]; if (instance.TabConsole.TabExists((client.Self.AgentID ^ selectedFriend.UUID).ToString())) { @@ -262,11 +258,11 @@ namespace Radegast instance.TabConsole.AddIMTab(selectedFriend.UUID, client.Self.AgentID ^ selectedFriend.UUID, selectedFriend.Name); instance.TabConsole.SelectTab((client.Self.AgentID ^ selectedFriend.UUID).ToString()); } - else if (lvwFriends.SelectedItems.Count > 1) + else if (listFriends.SelectedItems.Count > 1) { List participants = new List(); - foreach(ListViewItem item in lvwFriends.SelectedItems) - participants.Add(((FriendInfo)item.Tag).UUID); + foreach (var item in listFriends.SelectedItems) + participants.Add(((FriendInfo)item).UUID); UUID tmpID = UUID.Random(); lblFriendName.Text = "Startings friends conference..."; instance.TabConsole.DisplayNotificationInChat(lblFriendName.Text, ChatBufferTextStyle.Invisible); @@ -339,9 +335,9 @@ namespace Radegast private void btnOfferTeleport_Click(object sender, EventArgs e) { - foreach (ListViewItem item in lvwFriends.SelectedItems) + foreach (var item in listFriends.SelectedItems) { - FriendInfo friend = (FriendInfo)item.Tag; + FriendInfo friend = (FriendInfo)item; client.Self.SendTeleportLure(friend.UUID, "Join me in " + client.Network.CurrentSim.Name + "!"); } } @@ -357,41 +353,25 @@ namespace Radegast RefreshFriendsList(); } - private void lvwFriends_MouseClick(object sender, MouseEventArgs e) - { - if (e.Button == MouseButtons.Right) - ShowContextMenu(); - } - - private void lvwFriends_KeyDown(object sender, KeyEventArgs e) - { - if ((e.KeyCode == Keys.Apps) || - (e.Control && e.KeyCode == RadegastContextMenuStrip.ContexMenuKeyCode)) - { - ShowContextMenu(); - e.SuppressKeyPress = e.Handled = true; - } - } - public void ShowContextMenu() { RadegastContextMenuStrip menu = GetContextMenu(); - if (menu.HasSelection) menu.Show(lvwFriends, lvwFriends.PointToClient(System.Windows.Forms.Control.MousePosition)); + if (menu.HasSelection) menu.Show(listFriends, listFriends.PointToClient(System.Windows.Forms.Control.MousePosition)); } public RadegastContextMenuStrip GetContextMenu() { RadegastContextMenuStrip friendsContextMenuStrip = new RadegastContextMenuStrip(); - if (lvwFriends.SelectedItems.Count == 1) + if (listFriends.SelectedItems.Count == 1) { - FriendInfo item = (FriendInfo)lvwFriends.SelectedItems[0].Tag; + FriendInfo item = (FriendInfo)listFriends.SelectedItems[0]; instance.ContextActionManager.AddContributions(friendsContextMenuStrip, typeof(Avatar), item, btnPay.Parent); friendsContextMenuStrip.Selection = item.Name; friendsContextMenuStrip.HasSelection = true; } - else if (lvwFriends.SelectedItems.Count > 1) + else if (listFriends.SelectedItems.Count > 1) { - instance.ContextActionManager.AddContributions(friendsContextMenuStrip, typeof(ListView), lvwFriends, btnPay.Parent); + instance.ContextActionManager.AddContributions(friendsContextMenuStrip, typeof(ListView), listFriends, btnPay.Parent); friendsContextMenuStrip.Selection = "Multiple friends"; friendsContextMenuStrip.HasSelection = true; } @@ -403,20 +383,54 @@ namespace Radegast return friendsContextMenuStrip; } - public class FriendsSorter : System.Collections.IComparer + private void listFriends_DrawItem(object sender, DrawItemEventArgs e) { - public int Compare(object o1, object o2) + e.DrawBackground(); + + try { - FriendInfo f1 = (FriendInfo)((ListViewItem)o1).Tag; - FriendInfo f2 = (FriendInfo)((ListViewItem)o2).Tag; + if (e.Index >= 0) + { + var item = ((ListBox)sender).Items[e.Index]; + if (item is FriendInfo) + { + var friend = (FriendInfo)((ListBox)sender).Items[e.Index]; + string title = instance.Names.Get(friend.UUID); - if (f1.IsOnline && !f2.IsOnline) - return -1; - else if (!f1.IsOnline && f2.IsOnline) - return 1; - else - return string.Compare(f1.Name, f2.Name, true); + using (var brush = new SolidBrush(e.ForeColor)) + { + e.Graphics.DrawImageUnscaled(imageList1.Images[friend.IsOnline ? 1 : 0], e.Bounds.X, e.Bounds.Y); + e.Graphics.DrawString(title, e.Font, brush, e.Bounds.X + 20, e.Bounds.Y + 2); + } + } + } + } + catch { } + + e.DrawFocusRectangle(); + } + + private void listFriends_SelectedIndexChanged(object sender, EventArgs e) + { + selectedFriend = (FriendInfo)listFriends.SelectedItem; + SetControls(); + } + + private void listFriends_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Apps || (e.Control && e.KeyCode == RadegastContextMenuStrip.ContexMenuKeyCode)) + { + ShowContextMenu(); } } + + private void listFriends_MouseDown(object sender, MouseEventArgs e) + { + if (e.Button == MouseButtons.Right) + { + ShowContextMenu(); + } + } + } } diff --git a/Radegast/GUI/Consoles/FriendsConsole.resx b/Radegast/GUI/Consoles/FriendsConsole.resx index 2dc4b15..347a2e3 100644 --- a/Radegast/GUI/Consoles/FriendsConsole.resx +++ b/Radegast/GUI/Consoles/FriendsConsole.resx @@ -125,7 +125,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAACI - CAAAAk1TRnQBSQFMAgEBAgEAAagBAAGoAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo + CAAAAk1TRnQBSQFMAgEBAgEAARABAQEQAQEBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA -- 2.11.0