// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Diagnostics; using System.Windows.Media.TextFormatting; using Tango.Scripting.Editors.Document; using Tango.Scripting.Editors.Utils; namespace Tango.Scripting.Editors.Rendering { /// /// WPF TextSource implementation that creates TextRuns for a VisualLine. /// sealed class VisualLineTextSource : TextSource, ITextRunConstructionContext { public VisualLineTextSource(VisualLine visualLine) { this.VisualLine = visualLine; } public VisualLine VisualLine { get; private set; } public TextView TextView { get; set; } public TextDocument Document { get; set; } public TextRunProperties GlobalTextRunProperties { get; set; } public override TextRun GetTextRun(int textSourceCharacterIndex) { try { foreach (VisualLineElement element in VisualLine.Elements) { if (textSourceCharacterIndex >= element.VisualColumn && textSourceCharacterIndex < element.VisualColumn + element.VisualLength) { int relativeOffset = textSourceCharacterIndex - element.VisualColumn; TextRun run = element.CreateTextRun(textSourceCharacterIndex, this); if (run == null) throw new ArgumentNullException(element.GetType().Name + ".CreateTextRun"); if (run.Length == 0) throw new ArgumentException("The returned TextRun must not have length 0.", element.GetType().Name + ".Length"); if (relativeOffset + run.Length > element.VisualLength) throw new ArgumentException("The returned TextRun is too long.", element.GetType().Name + ".CreateTextRun"); InlineObjectRun inlineRun = run as InlineObjectRun; if (inlineRun != null) { inlineRun.VisualLine = VisualLine; VisualLine.hasInlineObjects = true; TextView.AddInlineObject(inlineRun); } return run; } } if (TextView.Options.ShowEndOfLine && textSourceCharacterIndex == VisualLine.VisualLength) { return CreateTextRunForNewLine(); } return new TextEndOfParagraph(1); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } } TextRun CreateTextRunForNewLine() { string newlineText = ""; DocumentLine lastDocumentLine = VisualLine.LastDocumentLine; if (lastDocumentLine.DelimiterLength == 2) { newlineText = "¶"; } else if (lastDocumentLine.DelimiterLength == 1) { char newlineChar = Document.GetCharAt(lastDocumentLine.Offset + lastDocumentLine.Length); if (newlineChar == '\r') newlineText = "\\r"; else if (newlineChar == '\n') newlineText = "\\n"; else newlineText = "?"; } return new FormattedTextRun(new FormattedTextElement(TextView.cachedElements.GetTextForNonPrintableCharacter(newlineText, this), 0), GlobalTextRunProperties); } public override TextSpan GetPrecedingText(int textSourceCharacterIndexLimit) { try { foreach (VisualLineElement element in VisualLine.Elements) { if (textSourceCharacterIndexLimit > element.VisualColumn && textSourceCharacterIndexLimit <= element.VisualColumn + element.VisualLength) { TextSpan span = element.GetPrecedingText(textSourceCharacterIndexLimit, this); if (span == null) break; int relativeOffset = textSourceCharacterIndexLimit - element.VisualColumn; if (span.Length > relativeOffset) throw new ArgumentException("The returned TextSpan is too long.", element.GetType().Name + ".GetPrecedingText"); return span; } } CharacterBufferRange empty = CharacterBufferRange.Empty; return new TextSpan(empty.Length, new CultureSpecificCharacterBufferRange(null, empty)); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } } public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex(int textSourceCharacterIndex) { throw new NotSupportedException(); } string cachedString; int cachedStringOffset; public StringSegment GetText(int offset, int length) { if (cachedString != null) { if (offset >= cachedStringOffset && offset + length <= cachedStringOffset + cachedString.Length) { return new StringSegment(cachedString, offset - cachedStringOffset, length); } } cachedStringOffset = offset; return new StringSegment(cachedString = this.Document.GetText(offset, length)); } } }
<UserControl x:Class="Tango.MachineStudio.Common.Controls.RealTimeGraphMultiControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:graphEx="clr-namespace:RealTimeGraphEx.FastGraphs;assembly=RealTimeGraphEx"
             xmlns:components="clr-namespace:RealTimeGraphEx.Components;assembly=RealTimeGraphEx"
             xmlns:converters="clr-namespace:Tango.MachineStudio.Common.Converters"
             xmlns:local="clr-namespace:Tango.MachineStudio.Common.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="300">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--RealTimeGraphEx-->
                <ResourceDictionary Source="pack://application:,,,/RealTimeGraphEx;component/Resources/Resources.xaml"></ResourceDictionary>
                <ResourceDictionary Source="../Resources/MaterialDesign.xaml"></ResourceDictionary>

				<ResourceDictionary>
					<Style TargetType="ContentControl" x:Key="graphContent">
						<Style.Setters>
							<Setter Property="ContentTemplate">
								<Setter.Value>
									<DataTemplate>
										<Grid MouseEnter="Graph_MouseEnter" MouseLeave="Graph_MouseLeave" ClipToBounds="True">
											<ContentControl Content="{Binding}"></ContentControl>
											<Grid Opacity="0.8" HorizontalAlignment="Stretch" VerticalAlignment="Top" ClipToBounds="True" Height="35" Margin="0 -35 0 0">
												<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top">
													<Button Margin="0 0 5 0" Click="OnGraphFullScreen" ToolTip="Full Screen" Width="24" Height="24" BorderBrush="Transparent" Background="{StaticResource AccentColorBrush}" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
														<materialDesign:PackIcon Kind="Fullscreen" HorizontalAlignment="Right" Width="20" Height="20" Foreground="{StaticResource WhiteBrush}" />
													</Button>
													<Button Click="OnGraphRemove" ToolTip="Remove" Width="24" Height="24" BorderBrush="Transparent" Background="#FF7777" Style="{StaticResource MaterialDesignFloatingActionAccentButton}" >
														<materialDesign:PackIcon Kind="CloseCircle" HorizontalAlignment="Right" Width="20" Height="20" Foreground="{StaticResource WhiteBrush}" />
													</Button>
												</StackPanel>
											</Grid>
										</Grid>
									</DataTemplate>
								</Setter.Value>
							</Setter>
						</Style.Setters>
					</Style>
				</ResourceDictionary>
			</ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <!--Temperature-->
        <ContentControl Style="{StaticResource graphContent}" Margin="0 0 0 0" MinHeight="5">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="40"/>
                    <ColumnDefinition Width="438*"/>
                </Grid.ColumnDefinitions>

                <Border BorderBrush="{StaticResource AccentColorBrush}" BorderThickness="1 1 0 1" Background="#90FFFFFF">
                    <StackPanel Orientation="Horizontal">
                        <components:YAxisScroll x:Name="yAxis" Interval="6" Graph="{Binding ElementName=Graph}" Width="35" Foreground="{StaticResource MaterialDesignLightForeground}" VerticalOffset="-5" FontSize="8" StringFormat="#0.0"></components:YAxisScroll>
                        <components:YAxisTicks x:Name="yAxisTicks" SmallTickTemplate="{StaticResource graphTicksTemplate}" Width="5" SmallTicks="6" Foreground="{StaticResource MaterialDesignLightForeground}" BigTicks="10" Graph="{Binding ElementName=Graph}"></components:YAxisTicks>
                    </StackPanel>
                </Border>
                <Border Grid.Column="1" BorderThickness="1" BorderBrush="{StaticResource borderBrush}" Background="{DynamicResource graphBackground}" Margin="5 0 0 0">
                    <graphEx:RealTimeGraphExMultiLineErase x:Name="Graph" x:FieldModifier="public" UseAutoRange="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=UseAutoRange}" Controller="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Controller}" Antialiased="True" RefreshRate="30" MarkerColor="{StaticResource graphsMarkerColor}" FillGraph="False" Minimum="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Minimum,Mode=TwoWay}" Maximum="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Maximum,Mode=TwoWay}"  Stroke="DodgerBlue">
                        <graphEx:RealTimeGraphExMultiLineErase.Components>
                            <components:MouseValueToolTip ToolTipTemplate="{StaticResource graphTooltipTemplate}" />
                            <components:GridLines Rows="4" Columns="6" GridBrush="{DynamicResource graphGridLinesBrush}"></components:GridLines>
                        </graphEx:RealTimeGraphExMultiLineErase.Components>
                        <graphEx:RealTimeGraphExMultiLineErase.InnerContent>
                            <Grid>
                                <Label Style="{StaticResource graphLabel}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl,AncestorLevel=2},Path=SensorName,FallbackValue='Dispensers Motors'}"></TextBlock>
                                        <TextBlock Foreground="Gray" Margin="10 0 0 0" FontFamily="Sylfaen Regular" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl,AncestorLevel=2},Path=SensorUnits,FallbackValue='(hz)'}"></TextBlock>
                                    </StackPanel>
                                </Label>
                            </Grid>
                        </graphEx:RealTimeGraphExMultiLineErase.InnerContent>
                    </graphEx:RealTimeGraphExMultiLineErase>
                </Border>

                <Border Grid.Column="2" Margin="5 0 0 0" HorizontalAlignment="Right" Opacity="0.8">
                    <components:YAxisLegends VerticalAlignment="Center" Margin="0 0 5 0" Graph="{Binding ElementName=Graph}" Width="70" FlowDirection="RightToLeft" LegendTemplate="{StaticResource graphLegendTemplate}">
                    </components:YAxisLegends>
                </Border>
            </Grid>
        </ContentControl>
    </Grid>
</UserControl>