<!-- Copyright © Northwoods Software Corporation, 2008-2026. All Rights Reserved. -->

<UserControl x:Class="Planogram.Planogram" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:go="http://schemas.nwoods.com/GoXam"
    xmlns:local="clr-namespace:Planogram">
  <UserControl.Resources>
    <!-- Our converters; necessary for successful databinding-->
    <go:BooleanBrushConverter x:Key="theStrokeChooser" TrueBrush="Magenta" FalseBrush="Black" />
    <local:NumberSizeConverter x:Key="theNumberSizeConverter" />

    <!-- A class containing all the slider, checkbox and combobox information to directly bind to. -->
    <local:PlanogramOptions x:Key="PlanogramOptions" />

    <!-- A custom adornment template that makes the border red-->
    <DataTemplate x:Key="BoxNodeAdornmentTemplate">
      <go:SelectionHandle Stroke="Red" StrokeThickness="3" go:Part.Selectable="False" />
    </DataTemplate>

    <!-- A custom resize handle template that makes handles red-->
    <DataTemplate x:Key="GroupResizeAdornmentTemplate">
      <go:SpotPanel>
        <go:ToolHandle go:SpotPanel.Spot="0.0 0.0" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />
        <go:ToolHandle go:SpotPanel.Spot="0.5 0.0" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />
        <go:ToolHandle go:SpotPanel.Spot="1.0 0.0" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />

        <go:ToolHandle go:SpotPanel.Spot="0.0 0.5" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />
        <go:ToolHandle go:SpotPanel.Spot="1.0 0.5" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />

        <go:ToolHandle go:SpotPanel.Spot="0.0 1.0" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />
        <go:ToolHandle go:SpotPanel.Spot="0.5 1.0" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />
        <go:ToolHandle go:SpotPanel.Spot="1.0 1.0" go:NodePanel.Figure="Rectangle" Width="6" Height="6" Fill="Red" Stroke="Black" StrokeThickness="1" />
      </go:SpotPanel>
    </DataTemplate>

    <go:DataTemplateDictionary x:Key="GroupTemplateDictionary">
      <!-- the template for racks -->
      <DataTemplate x:Key="Rack">
        <Grid go:Node.LocationElementName="area"
            go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}" 
            go:Part.SelectionElementName="area"
            go:Part.Selectable="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisible}"
            go:Part.Movable="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisible}"
            go:Part.Resizable="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisible}"
            go:Part.ResizeCellSize="{Binding Source={StaticResource PlanogramOptions}, Path=CellSize}" 
            go:Part.ResizeAdornmentTemplate="{StaticResource GroupResizeAdornmentTemplate}"
            go:Part.DragOverSnapEnabled="True"
            go:Part.DragOverSnapCellSpot="BottomRight"
            go:Part.DragOverSnapCellSize="{Binding Source={StaticResource PlanogramOptions}, Path=CellSize}" 
            go:Part.DropOntoBehavior="AddsToGroup">
          <Grid.RowDefinitions>
            <RowDefinition Height="14" />
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <TextBlock Text="{Binding Path=Data.Key}" FontSize="10"
                   Visibility="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisibility}"
                   go:Part.TextEditable="True" />
          <Border Grid.Row="1"
                BorderBrush="{Binding Path=Node.IsDropOntoAccepted, Converter={StaticResource theStrokeChooser}}"
                BorderThickness="1"
                Background="Transparent">
            <go:GridPattern x:Name="area"
                          Width="{Binding Path=Data.Width, Mode=TwoWay}"
                          Height="{Binding Path=Data.Height, Mode=TwoWay}"
                          CellSize="{Binding Source={StaticResource PlanogramOptions}, Path=CellSize}">
              <Path Stroke="{Binding Path=Node.IsDropOntoAccepted, Converter={StaticResource theStrokeChooser}}"
                  StrokeThickness=".5"
                  go:GridPattern.Figure="HorizontalLine" />
              <Path Stroke="{Binding Path=Node.IsDropOntoAccepted, Converter={StaticResource theStrokeChooser}}"
                  StrokeThickness=".5"
                  go:GridPattern.Figure="VerticalLine" />
              <Path Stroke="{Binding Source={StaticResource PlanogramOptions}, Path=MinorColor}"
                  go:GridPattern.Interval="{Binding Source={StaticResource PlanogramOptions}, Path=MinorInterval}"
                  go:GridPattern.Figure="{Binding Source={StaticResource PlanogramOptions}, Path=MinorFigureH}" />
              <Path Stroke="{Binding Source={StaticResource PlanogramOptions}, Path=MinorColor}"
                  go:GridPattern.Interval="{Binding Source={StaticResource PlanogramOptions}, Path=MinorInterval}"
                  go:GridPattern.Figure="{Binding Source={StaticResource PlanogramOptions}, Path=MinorFigureV}" />
              <Path Stroke="{Binding Source={StaticResource PlanogramOptions}, Path=MajorColor}"
                  go:GridPattern.Interval="{Binding Source={StaticResource PlanogramOptions}, Path=MajorInterval}"
                  go:GridPattern.Figure="{Binding Source={StaticResource PlanogramOptions}, Path=MajorFigureH}" />
              <Path Stroke="{Binding Source={StaticResource PlanogramOptions}, Path=MajorColor}"
                  go:GridPattern.Interval="{Binding Source={StaticResource PlanogramOptions}, Path=MajorInterval}"
                  go:GridPattern.Figure="{Binding Source={StaticResource PlanogramOptions}, Path=MajorFigureV}" />
            </go:GridPattern>
          </Border>
        </Grid>
      </DataTemplate>

      <!-- the template for shelves -->
      <DataTemplate x:Key="Shelf">
        <Grid go:Node.LocationElementName="area"
            go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}"
            go:Part.SelectionElementName="area"
            go:Part.Selectable="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisible}"
            go:Part.Movable="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisible}"
            go:Part.Resizable="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisible}"
            go:Part.ResizeAdornmentTemplate="{StaticResource GroupResizeAdornmentTemplate}"
            go:Part.DragOverSnapEnabled="True"
            go:Part.DragOverSnapCellSpot="BottomLeft" 
            go:Part.DragOverSnapCellSize="{Binding Path=Data.Height, Converter={StaticResource theNumberSizeConverter}}" 
            go:Part.DropOntoBehavior="AddsToGroup">
          <StackPanel>
            <Rectangle x:Name="area"
                     Width="{Binding Path=Data.Width, Mode=TwoWay}"
                     Height="{Binding Path=Data.Height, Mode=TwoWay}"
                     Stroke="{Binding Path=Node.IsDropOntoAccepted, Converter={StaticResource theStrokeChooser}}"
                     StrokeThickness="1" Fill="Transparent" />
            <Rectangle Stroke="Black" StrokeThickness="1" Fill="LightBlue" 
                     HorizontalAlignment="Stretch" Height="10" />
          </StackPanel>
          <TextBlock Text="{Binding Path=Data.Key}" FontSize="9"
                   Visibility="{Binding Source={StaticResource PlanogramOptions}, Path=GridResizeVisibility}"
                   VerticalAlignment="Bottom" HorizontalAlignment="Right"
                   go:Part.TextEditable="True" />
        </Grid>
      </DataTemplate>
    </go:DataTemplateDictionary>

    <go:DataTemplateDictionary x:Key="GroupPaletteTemplateDictionary">
      <!-- the palette's template for racks -->
      <DataTemplate x:Key="Rack">
        <Grid go:Node.LocationElementName="area"
            go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}" 
            go:Part.SelectionElementName="area" go:Part.SelectionAdorned="True">
          <Grid.RowDefinitions>
            <RowDefinition Height="14" />
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <TextBlock Text="{Binding Path=Data.Key}" FontSize="10" />
          <Border Grid.Row="1"
                BorderBrush="Black" BorderThickness="1"
                Background="Transparent">
            <go:GridPattern x:Name="area"
                          Width="{Binding Path=Data.Width, Mode=TwoWay}"
                          Height="{Binding Path=Data.Height, Mode=TwoWay}"
                          CellSize="{Binding Source={StaticResource PlanogramOptions}, Path=CellSize}">
              <Path Stroke="Black" StrokeThickness=".5"
                  go:GridPattern.Figure="HorizontalLine" />
              <Path Stroke="Black" StrokeThickness=".5"
                  go:GridPattern.Figure="VerticalLine" />
            </go:GridPattern>
          </Border>
        </Grid>
      </DataTemplate>

      <!-- the palette's template for shelves -->
      <DataTemplate x:Key="Shelf">
        <Grid go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}"
            go:Part.SelectionElementName="area" go:Part.SelectionAdorned="True">
          <StackPanel>
            <Rectangle x:Name="area"
                     Stroke="Black" StrokeThickness="1" Fill="Transparent"
                     Width="{Binding Path=Data.Width, Mode=TwoWay}"
                     Height="{Binding Path=Data.Height, Mode=TwoWay}" />
            <Rectangle Stroke="Black" StrokeThickness="1" Fill="LightBlue" 
                     HorizontalAlignment="Stretch" Height="10" />
          </StackPanel>
          <TextBlock Text="{Binding Path=Data.Key}" FontSize="9"
                   VerticalAlignment="Bottom" HorizontalAlignment="Right" />
        </Grid>
      </DataTemplate>
    </go:DataTemplateDictionary>

    <!-- Holds templates for all the different kinds of nodes-->
    <go:DataTemplateDictionary x:Key="NodeTemplateDictionary">
      <!-- the template for boxes -->
      <DataTemplate x:Key="">
        <go:NodePanel Sizing="Fixed"
                    Width="{Binding Path=Data.Width, Mode=TwoWay}"
                    Height="{Binding Path=Data.Height, Mode=TwoWay}" 
                    go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}" 
                    go:Node.LocationSpot="BottomLeft" 
                    go:Part.SelectionAdorned="{Binding Source={StaticResource PlanogramOptions}, Path=BoxResizeNotVisible}" 
                    go:Part.SelectionAdornmentTemplate="{StaticResource BoxNodeAdornmentTemplate}"
                    go:Part.Resizable="{Binding Source={StaticResource PlanogramOptions}, Path=BoxResizeVisible}" 
                    go:Part.ResizeCellSize="{Binding Source={StaticResource PlanogramOptions}, Path=CellSize}">
          <go:NodeShape go:NodePanel.Figure="Rectangle" Fill="{Binding Path=Data.Color}" Stroke="Black" StrokeThickness="1" />
          <TextBlock Text="{Binding Path=Data.Key}" FontSize="10" HorizontalAlignment="Left" />
        </go:NodePanel>
      </DataTemplate>

      <!-- templates for special nodes -->
      <DataTemplate x:Key="Note">
        <Border Background="LightYellow" BorderBrush="Gray" BorderThickness="1"
              go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}"
              go:Part.SelectionAdorned="True"
              go:Part.SelectionAdornmentTemplate="{StaticResource BoxNodeAdornmentTemplate}">
          <TextBlock Text="{Binding Path=Data.Key}" FontSize="12"
                   Margin="2" TextWrapping="Wrap"
                   go:Part.TextEditable="True" />
        </Border>
      </DataTemplate>

      <DataTemplate x:Key="Label">
        <TextBlock Text="{Binding Path=Data.Key}" FontSize="12"
                 TextWrapping="Wrap"
                 go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}"
                 go:Part.SelectionAdorned="True"
                 go:Part.SelectionAdornmentTemplate="{StaticResource BoxNodeAdornmentTemplate}"
                 go:Part.TextEditable="True" />
      </DataTemplate>

      <DataTemplate x:Key="Title">
        <TextBlock Text="{Binding Path=Data.Key}" FontSize="16" FontWeight="Bold"
                 TextWrapping="Wrap"
                 go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}"
                 go:Part.SelectionAdorned="True"
                 go:Part.SelectionAdornmentTemplate="{StaticResource BoxNodeAdornmentTemplate}"
                 go:Part.TextEditable="True" />
      </DataTemplate>
    </go:DataTemplateDictionary>
  </UserControl.Resources>

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid Background="LightGray">
      <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="4*" />
      </Grid.RowDefinitions>

      <ListBox Padding="5" BorderThickness="1" Margin="2" BorderBrush="Black" SelectionChanged="listBox_SelectionChanged">
        <ListBoxItem Content="Special" />
        <ListBoxItem Content="Small" />
        <ListBoxItem Content="Tall" />
        <ListBoxItem Content="Wide" />
      </ListBox>

      <!--Bottom left palette for selecting nodes-->
      <go:Palette x:Name="nodePalette" Grid.Row="1"
                  Width="125" Padding="5" Margin="2" BorderBrush="Black" BorderThickness="1" 
                  Background="White" MaximumSelectionCount="1"
                  GroupTemplateDictionary="{StaticResource GroupPaletteTemplateDictionary}" 
                  NodeTemplateDictionary="{StaticResource NodeTemplateDictionary}">
        <go:Palette.Layout>
          <go:GridLayout WrappingColumn="1" CellSize="25 25" Sorting="Forward" />
        </go:Palette.Layout>
      </go:Palette>
    </Grid>

    <Grid Background="LightGray" Grid.Column="1">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>

      <!--Top right panel for the controls-->
      <ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Auto">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
          <Border Background="AliceBlue" BorderBrush="Gray" BorderThickness="2" CornerRadius="2" Margin="1" Padding="1">
            <StackPanel>
              <CheckBox Click="allowResizeDisplayCheckBox_Click" x:Name="allowResizeDisplayCheckBox" Margin="1" Content="Allow Moving/Resizing Display" />
              <CheckBox Click="allowResizeDisplayCheckBox_Click" x:Name="allowResizeItemsCheckBox" Margin="1" Content="Allow Resizing Items" />
              <StackPanel Margin="1">
                <StackPanel Orientation="Horizontal">
                  <TextBlock Text="Cell Size: " />
                  <TextBlock Text="{Binding Path=Value, ElementName=cellSlider}" HorizontalAlignment="Right" />
                </StackPanel>
                <Slider x:Name="cellSlider" ValueChanged="gridlineSlider_ValueChanged" Minimum="3" Maximum="200" Value="25" Width="150" />
              </StackPanel>
            </StackPanel>
          </Border>
          <Border Background="AliceBlue" BorderBrush="Gray" BorderThickness="2" CornerRadius="2" Margin="1" Padding="1">
            <StackPanel Margin="1">
              <CheckBox x:Name="enableMinor" Click="allowResizeDisplayCheckBox_Click" Content="Minor Gridlines" />
              <TextBlock Text="Interval: " />
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Value, ElementName=gridlineSlider1}" HorizontalAlignment="Right" />
                <Slider ValueChanged="gridlineSlider_ValueChanged" x:Name="gridlineSlider1" Value="2" Minimum="1" Maximum="5" Width="150" />
              </StackPanel>
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="Color: " />
                <ComboBox SelectionChanged="gridlineComboBox_SelectionChanged"
                          Width="30" Height="25" SelectedIndex="0"
                          Background="{Binding Path=SelectedItem.Background, ElementName=minorGridlineComboBox}" x:Name="minorGridlineComboBox">
                  <ComboBoxItem Background="Blue" Content=" " />
                  <ComboBoxItem Background="Red" Content=" " />
                  <ComboBoxItem Background="Green" Content=" " />
                  <ComboBoxItem Background="Yellow" Content=" " />
                  <ComboBoxItem Background="Orange" Content=" " />
                </ComboBox>
              </StackPanel>
            </StackPanel>
          </Border>
          <Border Background="AliceBlue" BorderBrush="Gray" BorderThickness="2" CornerRadius="2" Margin="1" Padding="1">
            <StackPanel Margin="1">
              <CheckBox x:Name="enableMajor" Click="allowResizeDisplayCheckBox_Click" Content="Major Gridlines" />
              <TextBlock Text="Interval: " />
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Value, ElementName=gridlineSlider2}" HorizontalAlignment="Right" />
                <Slider ValueChanged="gridlineSlider_ValueChanged" x:Name="gridlineSlider2" Value="4" Minimum="1" Maximum="10" Width="150" />
              </StackPanel>
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="Color: " />
                <ComboBox SelectionChanged="gridlineComboBox_SelectionChanged"
                          Width="30" Height="25" SelectedIndex="1"
                          Background="{Binding ElementName=majorGridlineComboBox, Path=SelectedItem.Background}" x:Name="majorGridlineComboBox">
                  <ComboBoxItem Background="Blue" Content=" " />
                  <ComboBoxItem Background="Red" Content=" " />
                  <ComboBoxItem Background="Green" Content=" " />
                  <ComboBoxItem Background="Yellow" Content=" " />
                  <ComboBoxItem Background="Orange" Content=" " />
                </ComboBox>
              </StackPanel>
            </StackPanel>
          </Border>
          <Border x:Name="printPanel" Background="AliceBlue" BorderBrush="Gray" BorderThickness="2" CornerRadius="2" Margin="1" Padding="1">
            <StackPanel Margin="1">
              <TextBlock HorizontalAlignment="Center" Text="Printing Options" />
              <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Text="Scale: " />
                <TextBox x:Name="printScale" Text="{Binding Path=PrintManager.Scale, ElementName=myDiagram, Mode=TwoWay}" />
              </StackPanel>
              <Button x:Name="printButton" HorizontalAlignment="Left" Content="Print"  />
            </StackPanel>
          </Border>
        </StackPanel>
      </ScrollViewer>

      <!--Diagram at bottom right-->
      <go:Diagram x:Name="myDiagram" Grid.Row="1" Margin="2" Padding="5"
                  BorderBrush="Black" BorderThickness="1" Background="White"
                  GroupTemplateDictionary="{StaticResource GroupTemplateDictionary}" 
                  NodeTemplateDictionary="{StaticResource NodeTemplateDictionary}" 
                  ExternalObjectsDropped="myDiagram_ExternalObjectsDropped"
                  HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <go:Diagram.DraggingTool>
          <local:CustomDraggingTool DragOverSnapArea="DiagramAndNodes" DropOntoEnabled="True" />
        </go:Diagram.DraggingTool>
      </go:Diagram>
    </Grid>
  </Grid>
</UserControl>
