Esempio n. 1
0
        /// <summary>
        /// Wraps the specified framework element.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="wrapOptions">The wrap options.</param>
        /// <param name="buttons">The buttons to add.</param>
        /// <param name="parentContentControl">The parent content control.</param>
        /// <returns><see cref="Grid"/> that contains the wrapped content.</returns>
        /// <remarks>
        /// The framework element that is passed must be disconnected from the parent first. It is recommended to first check whether a
        /// framework element can be wrapped by using the <see cref="CanBeWrapped"/> method.
        /// This method will automatically handle the disconnecting of the framework element from the parent is the <paramref name="parentContentControl"/>
        /// is passed.
        /// </remarks>
        public Grid Wrap(FrameworkElement frameworkElement, WrapControlServiceWrapOptions wrapOptions, DataWindowButton[] buttons, ContentControl parentContentControl)
        {
            Argument.IsNotNull("frameworkElement", frameworkElement);
            Argument.IsNotNull("buttons", buttons);

            if (!string.IsNullOrWhiteSpace(frameworkElement.Name))
            {
                if (frameworkElement.Name.StartsWith(WrapControlServiceControlNames.MainContentHolderName))
                {
                    return((Grid)frameworkElement);
                }
            }

            if (parentContentControl != null)
            {
                SetControlContent(parentContentControl, null);
            }

            var mainContent = frameworkElement;

            // Create the outside grid, so the inner grid is never the same as the main content holder
            var outsideGrid = new Grid();

            outsideGrid.Name = WrapControlServiceControlNames.MainContentHolderName.GetUniqueControlName();

            if (Application.Current != null)
            {
                outsideGrid.Resources.MergedDictionaries.Add(Application.Current.Resources);
            }

            #region Generate buttons
#if !NETFX_CORE
            if (buttons.Length > 0)
            {
                // Add wrappanel containing the buttons
                var buttonsWrapPanel = new WrapPanel();
                buttonsWrapPanel.Name = WrapControlServiceControlNames.ButtonsWrapPanelName;
#if SILVERLIGHT
                buttonsWrapPanel.Style = Application.Current.Resources["DataWindowButtonContainerStyle"] as Style;
#else
                buttonsWrapPanel.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonContainerStyle");
#endif

                foreach (var dataWindowButton in buttons)
                {
                    var button = new Button();
                    if (dataWindowButton.CommandBindingPath != null)
                    {
                        button.SetBinding(ButtonBase.CommandProperty, new Binding(dataWindowButton.CommandBindingPath));
                    }
                    else
                    {
                        button.Command = dataWindowButton.Command;
                    }

                    if (dataWindowButton.ContentBindingPath != null)
                    {
                        Binding contentBinding = new Binding(dataWindowButton.ContentBindingPath);
                        if (dataWindowButton.ContentValueConverter != null)
                        {
                            contentBinding.Converter = dataWindowButton.ContentValueConverter;
                        }
                        button.SetBinding(ButtonBase.ContentProperty, contentBinding);
                    }
                    else
                    {
                        button.Content = dataWindowButton.Text;
                    }

                    if (dataWindowButton.VisibilityBindingPath != null)
                    {
                        Binding visibilityBinding = new Binding(dataWindowButton.VisibilityBindingPath);
                        if (dataWindowButton.VisibilityValueConverter != null)
                        {
                            visibilityBinding.Converter = dataWindowButton.VisibilityValueConverter;
                        }
                        button.SetBinding(ButtonBase.VisibilityProperty, visibilityBinding);
                    }
#if NET
                    button.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonStyle");
                    button.IsDefault = dataWindowButton.IsDefault;
                    button.IsCancel  = dataWindowButton.IsCancel;
#else
                    button.Style = Application.Current.Resources["DataWindowButtonStyle"] as Style;
#endif

                    if (dataWindowButton.IsDefault)
                    {
                        button.Name = WrapControlServiceControlNames.DefaultOkButtonName;
                    }
                    else if (dataWindowButton.IsCancel)
                    {
                        button.Name = WrapControlServiceControlNames.DefaultCancelButtonName;
                    }

                    buttonsWrapPanel.Children.Add(button);
                }

                // Create dockpanel that will dock the buttons underneath the content
                var subDockPanel = new DockPanel();
                subDockPanel.LastChildFill = true;
                DockPanel.SetDock(buttonsWrapPanel, Dock.Bottom);
                subDockPanel.Children.Add(buttonsWrapPanel);

                // Add actual content
                subDockPanel.Children.Add(frameworkElement);

                // The dockpanel is now the main content
                mainContent = subDockPanel;
            }
#endif
            #endregion

            #region Generate internal grid
            // Create grid
            var internalGrid = new Grid();
            internalGrid.Name = WrapControlServiceControlNames.InternalGridName;
            internalGrid.Children.Add(mainContent);

            // Grid is now the main content
            mainContent = internalGrid;
            #endregion

            #region Generate WarningAndErrorValidator
            if (Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateWarningAndErrorValidatorForDataContext))
            {
                // Create warning and error validator
                var warningAndErrorValidator = new WarningAndErrorValidator();
                warningAndErrorValidator.Name = WrapControlServiceControlNames.WarningAndErrorValidatorName;
                warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding());

                // Add to grid
                internalGrid.Children.Add(warningAndErrorValidator);
            }
            #endregion

            #region Generate InfoBarMessageControl
#if !NETFX_CORE
            if (Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateInlineInfoBarMessageControl) ||
                Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateOverlayInfoBarMessageControl))
            {
                // Create info bar message control
                var infoBarMessageControl = new InfoBarMessageControl();
                infoBarMessageControl.Name    = WrapControlServiceControlNames.InfoBarMessageControlName;
                infoBarMessageControl.Content = mainContent;

                if (Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateOverlayInfoBarMessageControl))
                {
                    infoBarMessageControl.Mode = InfoBarMessageControlMode.Overlay;
                }

                // This is now the main content
                mainContent = infoBarMessageControl;
            }
#endif
            #endregion

            // Set content of the outside grid
            outsideGrid.Children.Add(mainContent);

            if (parentContentControl != null)
            {
                SetControlContent(parentContentControl, outsideGrid);
            }

            return(outsideGrid);
        }
Esempio n. 2
0
        /// <summary>
        /// Wraps the specified framework element.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="wrapOptions">The wrap options.</param>
        /// <param name="buttons">The buttons to add.</param>
        /// <param name="parentContentControl">The parent content control.</param>
        /// <returns><see cref="Grid"/> that contains the wrapped content.</returns>
        /// <remarks>
        /// The framework element that is passed must be disconnected from the parent first. It is recommended to first check whether a
        /// framework element can be wrapped by using the <see cref="CanBeWrapped"/> method.
        /// This method will automatically handle the disconnecting of the framework element from the parent is the <paramref name="parentContentControl"/>
        /// is passed.
        /// </remarks>
        private static Grid Wrap(FrameworkElement frameworkElement, WrapOptions wrapOptions, DataWindowButton[] buttons, object parentContentControl)
        {
            Argument.IsNotNull("frameworkElement", frameworkElement);
            Argument.IsNotNull("buttons", buttons);

            if (frameworkElement.Name == MainContentHolderName)
            {
                return((Grid)frameworkElement);
            }

#if SILVERLIGHT
            // According to the documentation, no visual tree is garantueed in the Loaded event of the user control.
            // However, as a solution the documentation says you need to manually call ApplyTemplate, so let's do that.
            // For more info, see http://msdn.microsoft.com/en-us/library/ms596558(vs.95)
            var frameworkElementAsControl = frameworkElement as Control;
            if (frameworkElementAsControl != null)
            {
                frameworkElementAsControl.ApplyTemplate();
            }
#endif

            if (parentContentControl != null)
            {
                SetControlContent(parentContentControl, null);
            }

            FrameworkElement mainContent = frameworkElement;

            // Create the outside grid, so the inner grid is never the same as the main content holder
            var outsideGrid = new Grid();
            outsideGrid.Name = MainContentHolderName;

            if (Application.Current != null)
            {
#if SILVERLIGHT
                // TODO: Fix styles for silverlight
#else
                outsideGrid.Resources.MergedDictionaries.Add(Application.Current.Resources);
#endif
            }

            #region Generate buttons
#if !NETFX_CORE
            if (buttons.Length > 0)
            {
                // Add wrappanel containing the buttons
                var buttonsWrapPanel = new WrapPanel();
                buttonsWrapPanel.Name = ButtonsWrapPanelName;
#if SILVERLIGHT
                buttonsWrapPanel.Style = Application.Current.Resources["DataWindowButtonContainerStyle"] as Style;
#else
                buttonsWrapPanel.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonContainerStyle");
#endif

                foreach (var dataWindowButton in buttons)
                {
                    var button = new Button();
                    if (dataWindowButton.CommandBindingPath != null)
                    {
                        button.SetBinding(ButtonBase.CommandProperty, new Binding(dataWindowButton.CommandBindingPath));
                    }
                    else
                    {
                        button.Command = dataWindowButton.Command;
                    }

                    button.Content = dataWindowButton.Text;
#if NET
                    button.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonStyle");
                    button.IsDefault = dataWindowButton.IsDefault;
                    button.IsCancel  = dataWindowButton.IsCancel;
#else
                    button.Style = Application.Current.Resources["DataWindowButtonStyle"] as Style;
#endif

                    if (dataWindowButton.IsDefault)
                    {
                        button.Name = DefaultOkButtonName;
                    }
                    else if (dataWindowButton.IsCancel)
                    {
                        button.Name = DefaultCancelButtonName;
                    }

                    buttonsWrapPanel.Children.Add(button);
                }

                // Create dockpanel that will dock the buttons underneath the content
                var subDockPanel = new DockPanel();
                subDockPanel.LastChildFill = true;
                DockPanel.SetDock(buttonsWrapPanel, Dock.Bottom);
                subDockPanel.Children.Add(buttonsWrapPanel);

                // Add actual content
                subDockPanel.Children.Add(frameworkElement);

                // The dockpanel is now the main content
                mainContent = subDockPanel;
            }
#endif
            #endregion

            #region Generate internal grid
            // Create grid
            var internalGrid = new Grid();
            internalGrid.Name = InternalGridName;
            internalGrid.Children.Add(mainContent);

            // Grid is now the main content
            mainContent = internalGrid;
            #endregion

            #region Generate WarningAndErrorValidator
            if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateWarningAndErrorValidatorForDataContext))
            {
                // Create warning and error validator
                var warningAndErrorValidator = new WarningAndErrorValidator();
                warningAndErrorValidator.Name = WarningAndErrorValidatorName;
                warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding());

                // Add to grid
                internalGrid.Children.Add(warningAndErrorValidator);
            }
            #endregion

            #region Generate InfoBarMessageControl
#if !NETFX_CORE
            if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateInlineInfoBarMessageControl) ||
                Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateOverlayInfoBarMessageControl))
            {
                // Create info bar message control
                var infoBarMessageControl = new InfoBarMessageControl();
                infoBarMessageControl.Name    = InfoBarMessageControlName;
                infoBarMessageControl.Content = mainContent;

                if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateOverlayInfoBarMessageControl))
                {
                    infoBarMessageControl.Mode = InfoBarMessageControlMode.Overlay;
                }

                // This is now the main content
                mainContent = infoBarMessageControl;
            }
#endif
            #endregion

            // Set content of the outside grid
            outsideGrid.Children.Add(mainContent);

            if (parentContentControl != null)
            {
                SetControlContent(parentContentControl, outsideGrid);
            }

            return(outsideGrid);
        }
Esempio n. 3
0
        public VisualMessage(IChatSource Source, SmilesDataDase Db, ChatMessage Data)
        {
            this.Data = Data;

            List <Uri> Urls = new List <Uri>();

            string UserText = Data.Text;// HttpUtility.HtmlDecode(Data.Text.Replace(":s:", " :s:").Replace("  ", " "));

            UserText = UriDetector.Replace(
                UserText,
                new MatchEvaluator((m) => {
                Urls.Add(new Uri(m.Value, UriKind.RelativeOrAbsolute));
                return(LinkReplacer + " ");
            })
                );

            // parse text
            WrapPanel wp = new WrapPanel()
            {
                Orientation = System.Windows.Controls.Orientation.Horizontal,
            };
            List <string> ttt = new List <string>();

            // Тоже странный кусок:
            int nxd = UserText.IndexOf("<b>");

            if (nxd >= 0)
            {
                int nxd2 = UserText.IndexOf("</b>");
                TalkTo = UserText.Substring(nxd + 3, nxd2 - nxd - 3);
                if (UserText.Length <= (nxd2 + 6))
                {
                    UserText = "";
                }
                else
                {
                    UserText = UserText.Substring(nxd2 + 6);
                }

                this.ToMe = TalkTo == Source.StreamerNick;

                if (this.ToMe)
                {
                    wp.SetResourceReference(WrapPanel.StyleProperty, "StreamerContainer");
                }
                else
                {
                    wp.SetResourceReference(WrapPanel.StyleProperty, "NormalTextContainer");
                }

                ttt.Add(TalkTo + ",");
            }
            else
            {
                TalkTo = "";
            }

            ttt.AddRange(UserText.Split(' '));

            // if (UseLabel) {

            Label name = new Label()
            {
                Content = Data.Name + ": "
            };

            name.SetResourceReference(Label.StyleProperty, "LabelNameStyle");
            wp.Children.Add(name);
            int linkIndex = 0;

            ISmileCreator creator = Source as ISmileCreator;

            for (int j = 0; j < ttt.Count; ++j)
            {
                if (ttt[j] == LinkReplacer)
                {
                    Label link = new Label()
                    {
                        Content = "link ",
                        Cursor  = Cursors.Hand,
                        ToolTip = Urls[linkIndex],
                        Tag     = Urls[linkIndex]
                    };
                    link.SetResourceReference(Label.StyleProperty, "LabelLinkStyle");
                    link.MouseLeftButtonUp += (sender, b) => {
                        Uri u = ((Label)sender).Tag as Uri;
                        System.Diagnostics.Process.Start(u.ToString());
                    };
                    wp.Children.Add(link);
                    linkIndex++;
                }
                else
                {
                    if (creator != null)
                    {
                        if (!creator.CreateSmile(ttt[j], wp))
                        {
                            ttt[j] += ' ';
                            AddWord(wp, ttt, j);
                        }
                    }
                    else
                    {
                        ttt[j] += ' ';

                        if (!CreateSmile(Db, ttt[j], wp))
                        {
                            AddWord(wp, ttt, j);
                        }
                    }
                }
            }

            //} else {

            //    TextBlock name = new TextBlock() { Text = Data.Name + ": " };
            //    name.SetResourceReference(TextBlock.StyleProperty, "NameStyle");
            //    wp.Children.Add(name);
            //    int linkIndex = 0;

            //    for (int j = 0; j < ttt.Count; ++j) {
            //        if (ttt[j] == LinkReplacer) {
            //            TextBlock link = new TextBlock() {
            //                Text = "link ",
            //                Cursor = Cursors.Hand,
            //                ToolTip = Urls[linkIndex],
            //                Tag = Urls[linkIndex]
            //            };
            //            link.SetResourceReference(TextBlock.StyleProperty, "LinkStyle");
            //            link.MouseLeftButtonUp += ( sender, b ) => {
            //                Uri u = ((TextBlock)sender).Tag as Uri;
            //                System.Diagnostics.Process.Start(u.ToString());
            //            };
            //            wp.Children.Add(link);
            //            linkIndex++;
            //        } else {
            //            //if (j != (ttt.Count - 1))
            //            ttt[j] += ' ';

            //            if (CreateSmile(Db, ttt[j], wp)) {
            //                // Ура смайл ебать есть
            //            } else {
            //                TextBlock txt = new TextBlock() { Text = ttt[j] };
            //                if (TalkTo + ", " == ttt[j]) {
            //                    txt.SetResourceReference(TextBlock.StyleProperty, "NameTextStyle");
            //                } else {
            //                    txt.SetResourceReference(TextBlock.StyleProperty, "TextStyle");
            //                }
            //                wp.Children.Add(txt);
            //            }
            //        }
            //    }
            //}

            Text = wp;
        }