/// <summary> /// Creates a chat window and displays it based on the parameter value. /// </summary> /// <param name="key">Unique key of user.</param> /// <param name="text">Title of window.</param> /// <param name="activated">Whether to restore window or not.</param> /// <param name="show">Whether to bring window to foreground or not.</param> /// <returns></returns> private ChatForm CreateChatForm(string key, string text, bool activated, bool show) { ChatForm newChatForm = null; try { newChatForm = new ChatForm(); newChatForm.Name = key; newChatForm.Text = text; // Add window to window list. windowList.Add(newChatForm); // Set correct window look. UpdateTabWindowStatusLook(key); // Show the window if "show" is true. if (show) { if (activated) { newChatForm.ShowWindow(true, true); } else { newChatForm.ShowWindow(bMessageToForeground, false); } } return(newChatForm); } catch { if (newChatForm != null) { if (windowList.Contains(newChatForm)) { windowList.Remove(newChatForm); } newChatForm.Dispose(); } return(null); } }
/// <summary> /// Adds a new tab page to the Tab Control and specifies the text and /// key for the tab. If tab already exists, bring it to top. /// </summary> /// <param name="text"></param> private void StartConversation(string key, string text, bool activated) { bool tabSelected = false; // Check if tab with given key exists. if (tabControlChat.TabPages.ContainsKey(key)) { // Tab found, make it selected. TabPageEx selectedTabPage = (TabPageEx)tabControlChat.TabPages[key]; tabControlChat.SelectedTab = selectedTabPage; tabSelected = true; } else if (tabList.ContainsKey(key)) { TabPageEx tabPage = tabList[key]; tabControlChat.TabPages.Add(tabPage); tabControlChat.SelectedTab = tabPage; tabSelected = true; } else if (windowList.ContainsKey(key)) { if (activated) { ChatForm chatForm = windowList[key]; chatForm.ShowWindow(true, true); } } else { TabPageEx newTabPage = null; ChatForm newChatForm = null; ChatControl newChatControl = null; try { // Create a Chat control and set its properties. newChatControl = new ChatControl(); newChatControl.Name = "ChatControl"; newChatControl.Dock = DockStyle.Fill; newChatControl.LocalUserName = localUserName; newChatControl.LocalAddress = localAddress.ToString(); User remoteUser = GetUser(key); newChatControl.Key = key; newChatControl.RemoteUserName = remoteUser.Name; newChatControl.RemoteAddress = remoteUser.Address; newChatControl.MessageFont = defaultFont; newChatControl.MessageFontColor = defaultFontColor; newChatControl.HotKeyMod = Properties.Settings.Default.MessageHotKeyMod; newChatControl.MessageToForeground = bMessageToForeground; newChatControl.SilentMode = bSilentMode; newChatControl.ShowEmoticons = Properties.Settings.Default.ShowEmoticons; newChatControl.EmotTextToImage = Properties.Settings.Default.EmotTextToImage; newChatControl.ShowTimeStamp = Properties.Settings.Default.ShowTimeStamp; newChatControl.AddDateToTimeStamp = Properties.Settings.Default.AddDateToTimeStamp; newChatControl.Windowed = bChatWindowed; newChatControl.Sending += new ChatControl.SendEventHandler(ChatControl_Sending); newChatControl.WindowModeChange += new ChatControl.WindowModeChangeEventHandler(ChatControl_WindowModeChange); if (Properties.Settings.Default.ChatWindowed == true) { newChatForm = CreateChatForm(key, text, activated); newChatForm.Controls.Add(newChatControl); } else { // Create a new tab page and add the chat control to it. newTabPage = CreateChatTab(key, text); newTabPage.Controls.Add(newChatControl); tabSelected = true; } // Display status message. newChatControl.ReceiveMessage(MessageTypes.Status, string.Empty, remoteUser.Name, remoteUser.Status); // Display a message if remote user's version is older. if (IsVersionOlder(remoteUser.Version, localClientVersion)) { newChatControl.ReceiveMessage(MessageTypes.OldVersion, string.Empty, remoteUser.Name, string.Empty); } } catch { if (newChatControl != null) { newChatControl.Dispose(); } if (newTabPage != null) { newTabPage.Dispose(); } if (newChatForm != null) { newChatForm.Dispose(); } return; } } tabControlChat.Visible = (tabControlChat.TabCount > 0); // Set keyboard focus to the message box inside the chat control. // This is to be done after tab control becomes visible, else focus will not work. if (tabSelected && tabControlChat.SelectedIndex >= 0) { TabPageEx tabPage = (TabPageEx)tabControlChat.SelectedTab; ChatControl chatControl = (ChatControl)tabPage.Controls["ChatControl"]; chatControl.SetFocus(); } }