Example #1
0
        public WpfPresentationSource(UIElement rootElement, WpfRenderElementFactory wpfRenderElementFactory, WpfValueConverter converter)
        {
            this.RootElement = rootElement;
            this.converter   = converter;

            RootElement.IsRootElement = true;

            MouseDevice    = new MouseDevice(this);
            KeyboardDevice = new KeyboardDevice(this);

            container = new wpf::System.Windows.Controls.Canvas {
                Background = wpf::System.Windows.Media.Brushes.Transparent
            };
            container.PreviewMouseMove  += OnContainerMouseMove;
            container.PreviewMouseDown  += OnContainerMouseDown;
            container.PreviewMouseUp    += OnContainerMouseUp;
            container.PreviewMouseWheel += (sender, e) => e.Handled = ProcessMouseEvent(new RawMouseWheelEventArgs(e.Delta, converter.ConvertBack(e.GetPosition(container)), GetTimestamp()));

            MouseDevice.CursorChanged += (sender, e) => container.Cursor = converter.Convert(MouseDevice.Cursor);
            container.Cursor           = converter.Convert(MouseDevice.Cursor);

            window = new wpf::System.Windows.Window {
                UseLayoutRounding = true, Content = container
            };
            window.Activated      += (sender, e) => MouseDevice.Activate();
            window.Deactivated    += (sender, e) => MouseDevice.Deactivate();
            window.SizeChanged    += (sender, e) => SetRootElementSize();
            window.PreviewKeyDown += (sender, e) => e.Handled = ProcessKeyboardEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.PreviewKeyUp   += (sender, e) => e.Handled = ProcessKeyboardEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.Show();

            container.Children.Add(((IWpfRenderElement)rootElement.GetRenderElement(wpfRenderElementFactory)).WpfElement);
            SetRootElementSize();
            ((FrameworkElement)RootElement).Arrange(new Rect(container.ActualWidth, container.ActualHeight));

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
        public Size Measure(string text, double fontSize, Typeface typeface, double maxWidth)
        {
            if (!text.IsNullOrEmpty() && text.EndsWith(Environment.NewLine))
            {
                text += " ";
            }

            wpf::System.Windows.Media.FormattedText formattedText = new wpf::System.Windows.Media.FormattedText(
                text.DefaultIfNullOrEmpty("A"),
                System.Globalization.CultureInfo.CurrentCulture,
                wpf::System.Windows.FlowDirection.LeftToRight,
                converter.Convert(typeface),
                fontSize,
                wpf::System.Windows.Media.Brushes.Black);

            formattedText.Trimming     = wpf::System.Windows.TextTrimming.None;
            formattedText.MaxTextWidth = Double.IsInfinity(maxWidth) ? 0 : maxWidth;

            return(new Size(text.IsNullOrEmpty() ? 0 : Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace + 4), Math.Ceiling(formattedText.Height)));
        }