<UserControl 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:BuzzGUI.ToolBar;assembly=BuzzGUI.ToolBar"
    xmlns:bgc="clr-namespace:BuzzGUI.Common;assembly=BuzzGUI.Common"
    bgc:CommandSinkBinding.CommandSink="{Binding}"
    UseLayoutRounding="True" TextOptions.TextFormattingMode="Display"
>
 
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        
        <!-- Local Resources -->
        <ResourceDictionary>
          
          <Style TargetType="{x:Type Image}" x:Key="ButtonImageStyle">
            <Style.Triggers>
              <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
                <Setter Property="Opacity" Value="0.25"></Setter>
              </DataTrigger>
            </Style.Triggers>
          </Style>

          <SolidColorBrush x:Key="VUMeterBorderBrush" Color="Gray"/>
          <Color x:Key="VUMeterBackgroundColor">#333</Color>

          <LinearGradientBrush x:Key="VUMeterBrush" StartPoint="0,0" EndPoint="1,0">
            <GradientStop Color="#0F0" Offset="0.0"/>
            <GradientStop Color="#FF0" Offset="0.75"/>
            <GradientStop Color="#F00" Offset="1.0"/>
          </LinearGradientBrush>

          <Style x:Key="LockButtonStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                  <Image Name="image" Source="gfx/button_param_unlocked.png"/>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                      <Setter TargetName="image" Property="Source" Value="gfx/button_param_locked.png"/>
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>

          <Style x:Key="LEDButtonStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                  <Image Name="image" Source="gfx/led_off.png"/>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                      <Setter TargetName="image" Property="Source" Value="gfx/led_active.png"/>
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>

          <Style x:Key="KeyboardButtonStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                  <Border Name="border" Width="24" Height="16" Background="WhiteSmoke" BorderBrush="Black" BorderThickness="1">
                    <TextBlock HorizontalAlignment="Center">KB</TextBlock>
                  </Border>

                  <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                      <Setter TargetName="border" Property="Background" Value="LightSkyBlue"/>
                    </Trigger>
                  </ControlTemplate.Triggers>

                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>

					<Style x:Key="ConfigButtonStyle" TargetType="{x:Type Button}">
						<Setter Property="Template">
							<Setter.Value>
								<ControlTemplate TargetType="{x:Type Button}">
									<Border Name="border" Width="32" Height="16" Background="WhiteSmoke" BorderBrush="Black" BorderThickness="1">
										<TextBlock HorizontalAlignment="Center">CFG</TextBlock>
									</Border>
								</ControlTemplate>
							</Setter.Value>
						</Setter>
					</Style>

				</ResourceDictionary>
        
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </UserControl.Resources>

  <UserControl.CommandBindings>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.NewFileCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.OpenFileCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.SaveFileCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.CutCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.CopyCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.PasteCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.UndoCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.RedoCommand"/>
    <bgc:CommandSinkBinding Command="l:ToolBarVM.StopCommand"/>
		<bgc:CommandSinkBinding Command="l:ToolBarVM.ConfigureAudioDriverCommand"/>
		<bgc:CommandSinkBinding Command="l:ToolBarVM.TapTempoCommand"/>
	</UserControl.CommandBindings>
  
  
  <ToolBarTray Margin="0,1,0,1">

    <!-- Standard -->
    <ToolBar>
      <ToolBar.Resources>
        <Style TargetType="{x:Type bgc:RadioButtonEx}" BasedOn="{StaticResource {x:Static ToolBar.RadioButtonStyleKey}}"/>
      </ToolBar.Resources>
      
      <Button Command="l:ToolBarVM.NewFileCommand">
        <Button.ToolTip>New</Button.ToolTip>
        <Image Source="gfx/button_new.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      <Button Command="l:ToolBarVM.OpenFileCommand">
        <Button.ToolTip>Open</Button.ToolTip>
        <Image Source="gfx/button_open.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      <Button Command="l:ToolBarVM.SaveFileCommand">
        <Button.ToolTip>Save</Button.ToolTip>
        <Image Source="gfx/button_save.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      
      <Separator/>

      <Button Command="l:ToolBarVM.CutCommand">
        <Button.ToolTip>Cut</Button.ToolTip>
        <Image Source="gfx/button_cut.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      <Button Command="l:ToolBarVM.CopyCommand">
        <Button.ToolTip>Copy</Button.ToolTip>
        <Image Source="gfx/button_copy.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      <Button Command="l:ToolBarVM.PasteCommand">
        <Button.ToolTip>Paste</Button.ToolTip>
        <Image Source="gfx/button_paste.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>

      <Separator/>

      <Button Command="l:ToolBarVM.UndoCommand">
        <Button.ToolTip>Undo</Button.ToolTip>
        <Image Source="gfx/button_undo.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      <Button Command="l:ToolBarVM.RedoCommand">
        <Button.ToolTip>Redo</Button.ToolTip>
        <Image Source="gfx/button_redo.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>

      <Separator/>

      <bgc:RadioButtonEx GroupName="BuzzViewGroup" IsCheckedReal="{Binding Path=ShowPatternView, Mode=TwoWay}">
        <RadioButton.ToolTip>Pattern View (F2)</RadioButton.ToolTip>
        <Image Source="gfx/button_pe.png"/>
      </bgc:RadioButtonEx>
      <bgc:RadioButtonEx GroupName="BuzzViewGroup" IsCheckedReal="{Binding Path=ShowMachineView, Mode=TwoWay}">
        <RadioButton.ToolTip>Machine View (F3)</RadioButton.ToolTip>
        <Image Source="gfx/button_mv.png"/>
      </bgc:RadioButtonEx>
      <bgc:RadioButtonEx GroupName="BuzzViewGroup" IsCheckedReal="{Binding Path=ShowSequenceView, Mode=TwoWay}">
        <RadioButton.ToolTip>Sequence View (F4)</RadioButton.ToolTip>
        <Image Source="gfx/button_se.png"/>
      </bgc:RadioButtonEx>

      <Separator/>

      <ToggleButton IsChecked="{Binding Path=Playing, Mode=TwoWay}">
        <ToggleButton.ToolTip>Play (F5)</ToggleButton.ToolTip>
        <Image Source="gfx/button_play.png"/>
      </ToggleButton>
      <ToggleButton IsChecked="{Binding Path=Recording, Mode=TwoWay}">
        <ToggleButton.ToolTip>Record (F7)</ToggleButton.ToolTip>
        <Image Source="gfx/button_rec.png"/>
      </ToggleButton>
      <Button Command="l:ToolBarVM.StopCommand">
        <Button.ToolTip>Stop (F8)</Button.ToolTip>
        <Image Source="gfx/button_stop.png" Style="{StaticResource ButtonImageStyle}"/>
      </Button>
      <ToggleButton IsChecked="{Binding Path=Looping, Mode=TwoWay}">
        <ToggleButton.ToolTip>Loop</ToggleButton.ToolTip>
        <Image Source="gfx/button_loop.png"/>
      </ToggleButton>

      <Separator/>

      <bgc:RadioButtonEx GroupName="BuzzViewGroup" IsCheckedReal="{Binding Path=ShowWaveTableView, Mode=TwoWay}">
        <RadioButton.ToolTip>Wavetable View (F9)</RadioButton.ToolTip>
        <Image Source="gfx/button_wt.png"/>
      </bgc:RadioButtonEx>
      <bgc:RadioButtonEx GroupName="BuzzViewGroup" IsCheckedReal="{Binding Path=ShowSongInfoView, Mode=TwoWay}">
        <RadioButton.ToolTip>Info View (F10)</RadioButton.ToolTip>
        <Image Source="gfx/button_blah.png"/>
      </bgc:RadioButtonEx>

      <Separator/>

      <ToggleButton IsChecked="{Binding Path=AudioDeviceDisabled, Mode=TwoWay}">
        <ToggleButton.ToolTip>Disable Audio Device (F12)</ToggleButton.ToolTip>
        <Image Source="gfx/button_mute.png"/>
      </ToggleButton>

    </ToolBar>
    
    <!-- Master -->
    <ToolBar>
      <Grid Margin="8,0,0,0" Width="140">
        <Border Height="9" BorderBrush="{StaticResource VUMeterBorderBrush}" BorderThickness="1" VerticalAlignment="Top" CornerRadius="0">
          <Grid Name="grid">
            <Rectangle Fill="{StaticResource VUMeterBrush}"/>
            <Rectangle>
              <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                  <GradientStop Color="Transparent" Offset="0"/>
                  <GradientStop Color="Transparent" Offset="{Binding Path=VUMeterLevel.Item1}"/>
                  <GradientStop Color="{StaticResource VUMeterBackgroundColor}" Offset="{Binding Path=VUMeterLevel.Item1}"/>
                </LinearGradientBrush>
              </Rectangle.Fill>
            </Rectangle>
          </Grid>
        </Border>
        <Border Height="9" BorderBrush="{StaticResource VUMeterBorderBrush}" BorderThickness="1" VerticalAlignment="Bottom" CornerRadius="0">
          <Grid>
            <Rectangle Fill="{StaticResource VUMeterBrush}"/>
            <Rectangle>
              <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                  <GradientStop Color="Transparent" Offset="0"/>
                  <GradientStop Color="Transparent" Offset="{Binding Path=VUMeterLevel.Item2}"/>
                  <GradientStop Color="{StaticResource VUMeterBackgroundColor}" Offset="{Binding Path=VUMeterLevel.Item2}"/>
                </LinearGradientBrush>
              </Rectangle.Fill>
            </Rectangle>
          </Grid>
        </Border>

        <Viewbox Height="12" Width="140">
          <Slider Value="{Binding Path=MasterVolume, Mode=TwoWay}" Minimum="0" Maximum="1" SmallChange="0.025" LargeChange="0.1" Width="260" FocusVisualStyle="{x:Null}"/>
        </Viewbox>
      </Grid>
          
      <Button Cursor="Hand" ClickMode="Press" Command="l:ToolBarVM.TapTempoCommand" Margin="8,0,8,0" Content="BPM:"/>
      <bgc:NumericUpDown Value="{Binding Path=BPM, Mode=TwoWay}"  Minimum="16" Maximum="500" Width="50" Background="White"/>
      <TextBlock Margin="8,0,8,0" VerticalAlignment="Center">TPB:</TextBlock>
      <bgc:NumericUpDown Value="{Binding Path=TPB, Mode=TwoWay}" Minimum="1" Maximum="32" Width="50" Background="White"/>
      <TextBlock Margin="8,0,8,0" VerticalAlignment="Center">Speed:</TextBlock>
      <ComboBox Text="{Binding Path=Speed, Mode=TwoWay}" IsEditable="False" IsReadOnly="True" Width="50">
        <ComboBoxItem>0</ComboBoxItem>
        <ComboBoxItem>-1</ComboBoxItem>
        <ComboBoxItem>-2</ComboBoxItem>
        <ComboBoxItem>-3</ComboBoxItem>
        <ComboBoxItem>-4</ComboBoxItem>
        <ComboBoxItem>-5</ComboBoxItem>
        <ComboBoxItem>-6</ComboBoxItem>
        <ComboBoxItem>-7</ComboBoxItem>
        <ComboBoxItem>-8</ComboBoxItem>
        <ComboBoxItem>-9</ComboBoxItem>
        <ComboBoxItem>-10</ComboBoxItem>
        <ComboBoxItem>-11</ComboBoxItem>
        <ComboBoxItem>-12</ComboBoxItem>
      </ComboBox>

    </ToolBar>
    
    <!-- Time -->
    <ToolBar>
      <TextBlock VerticalAlignment="Center" Margin="12,0,0,0">Elapsed</TextBlock>
      <TextBlock VerticalAlignment="Center" Margin="4,0,0,0" Text="{Binding Path=ElapsedTime}"/>
      <TextBlock VerticalAlignment="Center" Margin="12,0,0,0">Current</TextBlock>
      <TextBlock VerticalAlignment="Center" Margin="4,0,0,0" Text="{Binding Path=CurrentTime}"/>
      <TextBlock VerticalAlignment="Center" Margin="12,0,0,0">Loop</TextBlock>
      <TextBlock VerticalAlignment="Center" Margin="4,0,12,0" Text="{Binding Path=LoopTime}"/>
    </ToolBar>
          
    <!-- MIDI -->
    <ToolBar>
      <ToggleButton Margin="2,1,0,0" IsEnabled="False" IsChecked="{Binding MIDIActivity, Mode=OneWay}" Width="16" Height="16" Style="{StaticResource LEDButtonStyle}"/>
      <TextBlock Margin="2,0,8,0" VerticalAlignment="Center" ToolTip="MIDI Focus Machine (Master Keyboard Mode)">MIDI:</TextBlock>
      <ComboBox IsEditable="False" IsReadOnly="True" Width="200" ItemsSource="{Binding Path=MachineList}" SelectedItem="{Binding Path=MIDIFocusMachine}" DisplayMemberPath="Name"/>
      <ToggleButton Margin="8,0,0,0" ClickMode="Press" IsChecked="{Binding MIDIFocusLocked}" Style="{StaticResource LockButtonStyle}" ToolTip="Lock MIDI Focus"/>
      <ToggleButton Margin="8,0,8,0" ClickMode="Press" IsChecked="{Binding IsPianoKeyboardVisible}" Style="{StaticResource KeyboardButtonStyle}" ToolTip="Show Keyboard"/>      
    </ToolBar>

		<!-- Audio Driver -->
		<ToolBar>
			<ComboBox IsEditable="False" Margin="8,0,0,0" IsReadOnly="True" Width="200" ItemsSource="{Binding Path=AudioDrivers}" SelectedItem="{Binding Path=SelectedAudioDriver}" ToolTip="Audio Driver"/>
			<Button Margin="8,0,8,0" Style="{StaticResource ConfigButtonStyle}" ToolTip="Configure" Command="l:ToolBarVM.ConfigureAudioDriverCommand"/>
		</ToolBar>

	</ToolBarTray>

</UserControl>