private void AddStyleSetting(Grid grid, string name, string text, MessageSetting setting)
        {
            int row = grid.RowDefinitions.Count;

            grid.RowDefinitions.Add(new RowDefinition()
            {
                Height = System.Windows.GridLength.Auto
            });
            grid.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(10)
            });

            // <Label Grid.Column="0" Grid.Row="1" Content="Auto login at startup:"></Label>
            TextBlock tb = new TextBlock();

            tb.Text = text;
            Grid.SetRow(tb, row);
            Grid.SetColumn(tb, 0);
            grid.Children.Add(tb);

            Run       run, run2 = null;
            TextBlock tb2 = new TextBlock();

            tb2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            if (setting.OneColorOnly)
            {
                run = new Run("Example");
                MessageSettings.LoadSettingsFor(run, setting);
                run.Foreground = setting.NickColor;
                tb2.Inlines.Add(run);
            }
            else
            {
                run = new Run("Player: ");
                MessageSettings.LoadSettingsFor(run, setting);
                run.FontWeight = FontWeights.Bold;
                run.Foreground = setting.NickColor;
                tb2.Inlines.Add(run);
                run2 = new Run("message");
                MessageSettings.LoadSettingsFor(run2, setting);
                run2.Foreground = setting.MessageColor;
                tb2.Inlines.Add(run2);
            }
            Grid.SetRow(tb2, row);
            Grid.SetColumn(tb2, 1);
            grid.Children.Add(tb2);

            // <Button Grid.Row="1" Grid.Column="1" Content="Change" Click="FontChange" Name="UserMessage"></Button>
            Button b = new Button();

            b.Focusable = false;
            b.Tag       = new object[] { name, text, setting, run, run2 };
            b.Content   = "Change";
            b.Click    += StyleHandler;
            Grid.SetRow(b, row);
            Grid.SetColumn(b, 2);
            grid.Children.Add(b);
        }
        void window_SaveSetting(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                object[] tag           = (object[])helper.Tag;
                MessageSetting setting = (MessageSetting)tag[2];
                Run run = (Run)tag[3];
                MessageSettings.LoadSettingsFor(run, setting);
                run.Foreground = setting.NickColor;

                if (setting.OneColorOnly == false)
                {
                    Run run2 = (Run)tag[4];
                    MessageSettings.LoadSettingsFor(run2, setting);
                    run2.Foreground = setting.MessageColor;
                }

                if (SettingChanged != null)
                {
                    SettingChanged(this, new SettingChangedEventArgs((string)tag[0], SettingChangedType.Style));
                }
            }
                                              ));
        }
Beispiel #3
0
        public bool AddNewMessage(Channel ch, MessageClass message, bool insert = false)
        {
            if (Properties.Settings.Default.ChatMode && (
                    message.Style.Type == MessageTypes.Part ||
                    message.Style.Type == MessageTypes.Join ||
                    message.Style.Type == MessageTypes.Quit)
                )
            {
                return(false);
            }

            try
            {
                Paragraph p = new Paragraph();
                MessageSettings.LoadSettingsFor(p, message.Style);
                p.Foreground            = message.Style.MessageColor;
                p.Margin                = new Thickness(0, 2, 0, 2);
                p.Tag                   = message;
                p.MouseRightButtonDown += InstantColorMenu;

                // Time when the message arrived
                if (Properties.Settings.Default.MessageTime)
                {
                    Run word = new Run(message.Time.ToString("T") + " ");
                    MessageSettings.LoadSettingsFor(word, MessageSettings.MessageTimeStyle);
                    word.Foreground = MessageSettings.MessageTimeStyle.NickColor;
                    p.Inlines.Add(word);
                }

                // Sender of the message
                Run nick = (message.Style.Type == MessageTypes.Action) ? new Run(message.Sender.Name + " ") : new Run(message.Sender.Name + ": ");

                SolidColorBrush b;
                // Instant color
                if (InstantColors.TryGetValue(message.Sender.LowerName, out b))
                {
                    nick.Foreground = b;
                }
                // Group color
                else if (message.Sender.Group.ID != UserGroups.SystemGroupID)
                {
                    nick.Foreground = message.Sender.Group.TextColor;
                    nick.FontStyle  = FontStyles.Italic;
                }
                else
                {
                    nick.Foreground = message.Style.NickColor;
                }
                nick.FontWeight = FontWeights.Bold;
                p.Inlines.Add(nick);

                // Message content
                if (message.Style.IsFixedText)
                {
                    p.Inlines.Add(new Run(message.Message));
                }
                else
                {
                    string[] words;
                    if (message.Words != null)
                    {
                        words = message.Words;
                    }
                    else
                    {
                        words = message.Message.Split(' ');
                    }
                    Uri             uri = null;
                    HightLightTypes highlightType;
                    sb.Clear(); // this StringBuilder is for minimizing the number on Runs in a paragraph
                    for (int i = 0; i < words.Length; i++)
                    {
                        if (message.HighlightWords != null && message.HighlightWords.TryGetValue(i, out highlightType))
                        {
                            // Flush the sb content
                            if (sb.Length > 0)
                            {
                                p.Inlines.Add(new Run(sb.ToString()));
                                sb.Clear();
                            }

                            Run word = new Run(words[i]);
                            if (highlightType == HightLightTypes.Highlight)
                            {
                                word.FontStyle = FontStyles.Italic;
                            }
                            else
                            {
                                MessageSettings.LoadSettingsFor(word, MessageSettings.LeagueFoundMessage);
                                word.Foreground = MessageSettings.LeagueFoundMessage.NickColor;
                            }
                            p.Inlines.Add(word);
                        }
                        // Links
                        else if (
                            (
                                words[i].StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) ||
                                words[i].StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
                                words[i].StartsWith("https://", StringComparison.OrdinalIgnoreCase)
                            ) && Uri.TryCreate(words[i], UriKind.RelativeOrAbsolute, out uri)
                            )
                        {
                            // Flush the sb content
                            if (sb.Length > 0)
                            {
                                p.Inlines.Add(new Run(sb.ToString()));
                                sb.Clear();
                            }

                            Hyperlink word = new Hyperlink(new Run(words[i]));
                            MessageSettings.LoadSettingsFor(word, MessageSettings.HyperLinkStyle);
                            word.Foreground       = MessageSettings.HyperLinkStyle.NickColor;
                            word.NavigateUri      = new Uri(words[i]);
                            word.RequestNavigate += OpenURLInBrowser;
                            p.Inlines.Add(word);
                        }
                        else
                        {
                            sb.Append(words[i]);
                        }
                        if (i + 1 < words.Length)
                        {
                            sb.Append(' ');
                        }
                    }

                    // Flush the sb content
                    if (sb.Length > 0)
                    {
                        p.Inlines.Add(new Run(sb.ToString()));
                    }
                }

                // Insert the new paragraph
                if (insert)
                {
                    ch.TheFlowDocument.Blocks.InsertBefore(ch.TheFlowDocument.Blocks.FirstBlock, p);
                }
                else
                {
                    ch.TheFlowDocument.Blocks.Add(p);
                }

                while (ch.TheFlowDocument.Blocks.Count > GlobalManager.MaxMessagesInMemory)
                {
                    ch.TheFlowDocument.Blocks.Remove(ch.TheFlowDocument.Blocks.FirstBlock);
                }

                return(true);
            }
            catch (Exception e)
            {
                ErrorLog.Log(e);
            }
            return(false);
        }