<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:o="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                    xmlns:m="http://metro.mahapps.com/winfx/xaml/controls"
                    xmlns:mi="http://metro.mahapps.com/winfx/xaml/iconpacks"
                    xmlns:qc="http://QuickConverter.CodePlex.com/"
                    xmlns:ge="clr-namespace:SourceChord.GridExtra;assembly=GridExtra.Wpf"
                    xmlns:ae="http://icsharpcode.net/sharpdevelop/avalonedit"
                    xmlns:aec="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion;assembly=ICSharpCode.AvalonEdit"
                    xmlns:aef="clr-namespace:ICSharpCode.AvalonEdit.Folding;assembly=ICSharpCode.AvalonEdit"
                    xmlns:controls="clr-namespace:MyPad.Views.Controls"
                    xmlns:completion="clr-namespace:MyPad.Views.Controls.Completion">

    <Style x:Key="Common.TextView" TargetType="{x:Type ae:TextView}">
        <!-- WARN: ColumnRulerPen はオンコードで指定する -->
        <Setter Property="LinkTextForegroundBrush" Value="{DynamicResource MahApps.Brushes.AccentBase}"/>
        <Setter Property="NonPrintableCharacterBrush" Value="{DynamicResource MahApps.Brushes.Gray2}"/>
        <Setter Property="CurrentLineBackground" Value="{DynamicResource MahApps.Brushes.Accent4}"/>
        <Setter Property="CurrentLineBorder">
            <Setter.Value>
                <Pen Brush="{DynamicResource MahApps.Brushes.Accent2}" o:Freeze="True"/>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="Common.TextArea" TargetType="{x:Type ae:TextArea}" BasedOn="{StaticResource {x:Type ae:TextArea}}">
        <Setter Property="SelectionBrush" Value="{DynamicResource MahApps.Brushes.Accent3}"/>
        <Setter Property="SelectionBorder" Value="{x:Null}"/>
        <Setter Property="SelectionForeground" Value="{x:Null}"/>
        <Setter Property="SelectionCornerRadius" Value="0"/>
    </Style>

    <Style x:Key="Common.TextEditor" TargetType="{x:Type ae:TextEditor}" BasedOn="{StaticResource {x:Type ae:TextEditor}}">
        <Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Text}"/>
        <Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Control.Background}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource MahApps.Brushes.Control.Background}"/>
        <Setter Property="LineNumbersForeground" Value="{DynamicResource MahApps.Brushes.Gray2}"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
        <Setter Property="HorizontalScrollBarVisibility" Value="Visible"/>
    </Style>

    <Style x:Key="Common.CompletionWindow" TargetType="{x:Type aec:CompletionWindow}">
        <Setter Property="ResizeMode" Value="NoResize"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="Opacity" Value="0.9"/>
        <Style.Resources>
            <Style TargetType="{x:Type aec:CompletionList}">
                <!-- WARN: 入力補完ウィンドウのフォントはテキストエディタに追従させる -->
                <Setter Property="FontFamily" Value="{Binding TextArea.FontFamily, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
                <Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Text}"/>
                <Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Control.Background}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type aec:CompletionList}">
                            <!-- ItemContainerStyle, ImageSize は外から変えられないので再定義する -->
                            <aec:CompletionListBox x:Name="PART_ListBox"
                                                   ItemContainerStyle="{StaticResource Common.ListBoxItem}"
                                                   Background="{TemplateBinding Background}"
                                                   TextBlock.Foreground="{TemplateBinding Foreground}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="{Binding Image, Mode=OneWay}"
                                                   Width="{DynamicResource MahApps.Font.Size.Button.ClearText}"
                                                   Height="{DynamicResource MahApps.Font.Size.Button.ClearText}"
                                                   Margin="0 0 2 0"/>
                                            <ContentPresenter Content="{Binding Content, Mode=OneWay}"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </aec:CompletionListBox>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style TargetType="{x:Type aec:CompletionListBox}" BasedOn="{StaticResource Common.ListBox}">
                <Setter Property="BorderBrush" Value="{DynamicResource MahApps.Brushes.ComboBox.PopupBorder}"/>
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </Style.Resources>
    </Style>

    <Style x:Key="Common.FoldingMargin" TargetType="{x:Type aef:FoldingMargin}">
        <Setter Property="FoldingMarkerBrush"                   Value="{DynamicResource MahApps.Brushes.CheckBox}"/>
        <Setter Property="SelectedFoldingMarkerBrush"           Value="{DynamicResource MahApps.Brushes.CheckBox.MouseOver}"/>
        <Setter Property="FoldingMarkerBackgroundBrush"         Value="{DynamicResource MahApps.Brushes.Control.Background}"/>
        <Setter Property="SelectedFoldingMarkerBackgroundBrush" Value="{DynamicResource MahApps.Brushes.Control.Background}"/>
    </Style>

    <Style x:Key="Common.SearchPanel" TargetType="{x:Type ae:SearchPanel}">
        <!-- WARN: MarkerBrush, MarkerCornerRadius はオンコードで指定する -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Background="{DynamicResource MahApps.Brushes.Gray10}"
                            BorderBrush="{DynamicResource MahApps.Brushes.Accent}"
                            BorderThickness="0 0 0 2"
                            Padding="3"
                            Opacity="0.9"
                            VerticalAlignment="Top"
                            HorizontalAlignment="Right"
                            KeyboardNavigation.TabNavigation="Cycle">
                        <Grid ge:GridEx.ColumnDefinition="Auto, 3, *, 3, Auto, Auto, 3, Auto, Auto, 3, Auto">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="{qc:Binding '$P ? double.NaN : 0', P={Binding (controls:TextArea.ReplaceAreaExpanded), Mode=OneWay}}"/>
                                <RowDefinition Height="{qc:Binding '$P ? double.NaN : 0', P={Binding (controls:TextArea.ReplaceAreaExpanded), Mode=OneWay}}"/>
                            </Grid.RowDefinitions>

                            <Grid.Resources>
                                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource Common.TextBox}">
                                    <Setter Property="Background"  Value="{DynamicResource MahApps.Brushes.Gray8}"/>
                                    <Setter Property="BorderBrush" Value="{DynamicResource MahApps.Brushes.Gray}"/>
                                    <Setter Property="MinWidth"    Value="150"/>
                                </Style>
                                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource Common.Button}">
                                    <Setter Property="Background"  Value="{x:Null}"/>
                                    <Setter Property="BorderBrush" Value="{x:Null}"/>
                                    <Setter Property="Padding"     Value="2"/>
                                    <Setter Property="Width"       Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
                                </Style>
                                <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource Common.ToggleButton}">
                                    <Setter Property="Background"  Value="{x:Null}"/>
                                    <Setter Property="BorderBrush" Value="{x:Null}"/>
                                    <Setter Property="Padding"     Value="2"/>
                                    <Setter Property="Width"       Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
                                    <Style.Resources>
                                        <!-- チェック状態での BorderBrush を上書きする -->
                                        <SolidColorBrush x:Key="MahApps.Brushes.Gray5" Color="{DynamicResource MahApps.Colors.Gray2}" o:Freeze="True"/>
                                    </Style.Resources>
                                </Style>
                            </Grid.Resources>

                            <ToggleButton Grid.Row="0" Grid.Column="0" x:Name="ExpandButton"
                                          IsChecked="{Binding (controls:TextArea.ReplaceAreaExpanded), Mode=TwoWay}"
                                          ToolTip="{Binding Localization.SwitchFindModeText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
                                <mi:Codicons Kind="ChevronDown"/>
                            </ToggleButton>
                            
                            <TextBox Grid.Row="0" Grid.Column="2" x:Name="PART_searchTextBox"
                                     Text="{Binding SearchPattern, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                     ToolTip="{Binding Localization.FindText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                     m:TextBoxHelper.Watermark="{Binding Localization.FindText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                     m:TextBoxHelper.ClearTextButton="True"/>
                            <Button Grid.Row="0" Grid.Column="4"
                                    Command="ae:SearchCommands.FindPrevious"
                                    ToolTip="{Binding Localization.FindPreviousText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
                                <mi:Codicons Kind="ArrowUp"/>
                            </Button>
                            <Button Grid.Row="0" Grid.Column="5"
                                    Command="ae:SearchCommands.FindNext"
                                    ToolTip="{Binding Localization.FindNextText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
                                <mi:Codicons Kind="ArrowDown"/>
                            </Button>
                            <ToggleButton Grid.Row="0" Grid.Column="7" 
                                          IsChecked="{Binding MatchCase, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                          ToolTip="{Binding Localization.MatchCaseText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
                                <mi:Codicons Kind="CaseSensitive"/>
                            </ToggleButton>
                            <ToggleButton Grid.Row="0" Grid.Column="8"
                                          IsChecked="{Binding UseRegex, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                          ToolTip="{Binding Localization.UseRegexText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
                                <mi:Codicons Kind="Regex"/>
                            </ToggleButton>
                            <Button Grid.Row="0" Grid.Column="10"
                                    Command="ae:SearchCommands.CloseSearchPanel"
                                    ToolTip="{Binding Localization.CloseText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                    IsTabStop="False">
                                <mi:Codicons Kind="Close"/>
                            </Button>

                            <Border Grid.Row="1" Grid.Column="0" Height="3"/>

                            <TextBox Grid.Row="2" Grid.Column="2"
                                     Text="{Binding (controls:TextArea.ReplacePattern), UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                     ToolTip="{Binding Localization.ReplaceText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                     m:TextBoxHelper.Watermark="{Binding Localization.ReplaceText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                     m:TextBoxHelper.ClearTextButton="True"
                                     Focusable="{Binding (controls:TextArea.ReplaceAreaExpanded), Mode=OneWay}"/>
                            <Button Grid.Row="2" Grid.Column="4"
                                    Command="controls:TextArea+Commands.ReplaceNext"
                                    ToolTip="{Binding Localization.ReplaceNextText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                    Focusable="{Binding (controls:TextArea.ReplaceAreaExpanded), Mode=OneWay}">
                                <mi:Codicons Kind="Replace"/>
                            </Button>
                            <Button Grid.Row="2" Grid.Column="5"
                                    Command="controls:TextArea+Commands.ReplaceAll"
                                    ToolTip="{Binding Localization.ReplaceAllText, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                    Focusable="{Binding (controls:TextArea.ReplaceAreaExpanded), Mode=OneWay}">
                                <mi:Codicons Kind="ReplaceAll"/>
                            </Button>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="controls:TextArea.ReplaceAreaExpanded" Value="True">
                            <Setter TargetName="ExpandButton" Property="Content">
                                <Setter.Value>
                                    <mi:Codicons Kind="ChevronUp"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type controls:TextView}"           BasedOn="{StaticResource Common.TextView}"/>
    <Style TargetType="{x:Type controls:TextArea}"           BasedOn="{StaticResource Common.TextArea}"/>
    <Style TargetType="{x:Type controls:TextEditor}"         BasedOn="{StaticResource Common.TextEditor}"/>
    <Style TargetType="{x:Type completion:CompletionWindow}" BasedOn="{StaticResource Common.CompletionWindow}"/>
    <Style TargetType="{x:Type aef:FoldingMargin}"           BasedOn="{StaticResource Common.FoldingMargin}"/>
    <Style TargetType="{x:Type ae:SearchPanel}"              BasedOn="{StaticResource Common.SearchPanel}"/>

</ResourceDictionary>