<?xml version="1.0" encoding="utf-8"?>
<Folder>
  <Instance name="MainWindowResource">
    <Instance ref.CodeBehind="true" ref.Class="demo::MainWindow">
      <ref.Members>
        <![CDATA[
          @cpp:UserImpl
          func CanCloseFile() : bool {}
          
          @cpp:UserImpl
          func LoadFile(path : string) : bool {}
      
          @cpp:UserImpl
          func OpenFile(filterIndex : int) : bool {}
      
          @cpp:UserImpl
          func SaveFile(saveAs : bool) : bool {}
      
          @cpp:UserImpl
          func SetupTextConfig() : void {}
      
          @cpp:UserImpl
          func SetupXmlConfig() : void {}
      
          @cpp:Protected
          var fileName : string = "";
      
          @cpp:Protected
          var isXml : bool = false;
      
          @cpp:Protected
          var findWindow : GuiWindow^ = null;
      
          @cpp:Protected
          var recentFiles : observe ToolstripCommand*[] = {};
          
          @cpp:Protected
          func AddRecentFile(path : string) : void
          {
            for (i in range [0, recentFiles.Count))
            {
              if (recentFiles[i].Text == path)
              {
                var existingCommand = recentFiles[i];
                recentFiles.RemoveAt(i);
                recentFiles.Insert(0, existingCommand);
                return;
              }
            }
          
            if (recentFiles.Count == 10)
            {
              recentFiles.RemoveAt(9);
            }
            
            var command = new ToolstripCommand*();
            command.Text = path;
            attach(command.Executed, func(sender : GuiGraphicsComposition*, arguments : GuiEventArgs*) : void
            {
              LoadFile(path);
            });
            
            recentFiles.Insert(0, command);
          }
        ]]>
      </ref.Members>
      <Window ref.Name="self" Text="TextEditor" ClientSize="x:480 y:320" ev.WindowClosing="window_Closing">
        <att.BoundsComposition-set PreferredMinSize="x:480 y:320"/>

        <ToolstripCommand ref.Name="commandFileNewText" Text="Text File" Image-uri="res://Images/NewText.png">
          <ev.Executed>commandFileNewText_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileNewXml" Text="Xml File" Image-uri="res://Images/NewXml.png">
          <ev.Executed>commandFileNewXml_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileOpen" Text="Open ..." Image-uri="res://Images/Open.png" ShortcutBuilder="Ctrl+O">
          <ev.Executed>commandFileOpen_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileOpenText" Text="Text File ..." Image-uri="res://Images/NewText.png">
          <ev.Executed>commandFileOpenText_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileOpenXml" Text="Xml File ..." Image-uri="res://Images/NewXml.png">
          <ev.Executed>commandFileOpenXml_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileSave" Text="Save" Image-uri="res://Images/Save.png" ShortcutBuilder="Ctrl+S">
          <ev.Executed>commandFileSave_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileSaveAs" Text="Save As ..." Image-uri="res://Images/SaveAs.png">
          <ev.Executed>commandFileSaveAs_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandFileExit" Text="Exit">
          <ev.Executed>commandFileExit_Executed</ev.Executed>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandEditUndo" Text="Undo" Image-uri="res://Images/EditUndo.png" ShortcutBuilder="Ctrl+Z">
          <att.Enabled-bind>textBox.observe as _(_.CanUndo() on _.UndoRedoChanged)</att.Enabled-bind>
          <ev.Executed>commandEditUndo_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditRedo" Text="Redo" Image-uri="res://Images/EditRedo.png" ShortcutBuilder="Ctrl+Y">
          <att.Enabled-bind>textBox.observe as _(_.CanRedo() on _.UndoRedoChanged)</att.Enabled-bind>
          <ev.Executed>commandEditRedo_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditCut" Text="Cut" Image-uri="res://Images/EditCut.png" ShortcutBuilder="Ctrl+X">
          <att.Enabled-bind>textBox.observe as _(_.CanCut() on _.SelectionChanged)</att.Enabled-bind>
          <ev.Executed>commandEditCut_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditCopy" Text="Copy" Image-uri="res://Images/EditCopy.png" ShortcutBuilder="Ctrl+C">
          <att.Enabled-bind>textBox.observe as _(_.CanCopy() on _.SelectionChanged)</att.Enabled-bind>
          <ev.Executed>commandEditCopy_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditPaste" Text="Paste" Image-uri="res://Images/EditPaste.png" ShortcutBuilder="Ctrl+V">
          <att.Enabled-bind>textBox.observe as _(_.CanPaste() on self.ClipboardUpdated)</att.Enabled-bind>
          <ev.Executed>commandEditPaste_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditDelete" Text="Delete" Image-uri="res://Images/EditDelete.png">
          <att.Enabled-bind>textBox.observe as _(_.CanCut() on _.SelectionChanged)</att.Enabled-bind>
          <ev.Executed>commandEditDelete_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditSelect" Text="Select All" ShortcutBuilder="Ctrl+A">
          <ev.Executed>commandEditSelect_Executed</ev.Executed>
        </ToolstripCommand>
        <ToolstripCommand ref.Name="commandEditFind" Text="Find ..." Image-uri="res://Images/EditFind.png" ShortcutBuilder="Ctrl+F">
          <ev.Executed>commandEditFind_Executed</ev.Executed>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandAbout" Text="About TextEditor ...">
          <ev.Executed>commandAbout_Executed</ev.Executed>
        </ToolstripCommand>

        <MessageDialog ref.Name="dialogQueryClose"
                       Input="DisplayYesNoCancel"
                       Icon="IconQuestion"
                       Title-eval="self.Text"
                       Text="Do you want to save this file?"
              />

        <MessageDialog ref.Name="dialogCannotOpen"
                       Input="DisplayOK"
                       Icon="IconError"
                       Title-eval="self.Text"
                       Text="Cannot open the selected file."
              />

        <MessageDialog ref.Name="dialogCannotSave"
                       Input="DisplayOK"
                       Icon="IconError"
                       Title-eval="self.Text"
                       Text="Cannot save the selected file."
              />

        <OpenFileDialog ref.Name="dialogOpen"
                        Filter="Text Files (*.txt)|*.txt|XML Files (*.xml)|*.xml|All Files (*.*)|*.*"
                        Title-eval="self.Text"
                        DefaultExtension="txt"
                        Options="FileDialogFileMustExist|FileDialogDereferenceLinks"
              />

        <SaveFileDialog ref.Name="dialogSave"
                        Filter="Text Files (*.txt)|*.txt|XML Files (*.xml)|*.xml|All Files (*.*)|*.*"
                        Title-eval="self.Text"
                        DefaultExtension="txt"
                        Options="FileDialogPromptCreateFile|FileDialogPromptOverwriteFile|FileDialogDirectoryMustExist|FileDialogAddToRecent"
              />

        <Table AlignmentToParent="left:0 top:0 right:0 bottom:0" MinSizeLimitation="LimitToElementAndChildren">
          <att.Rows>
            <CellOption>composeType:MinSize</CellOption>
            <CellOption>composeType:MinSize</CellOption>
            <CellOption>composeType:Percentage percentage:1.0</CellOption>
          </att.Rows>
          <att.Columns>
            <CellOption>composeType:Percentage percentage:1.0</CellOption>
          </att.Columns>

          <Cell Site="row:0 column:0">
            <ToolstripMenuBar>
              <att.BoundsComposition-set AlignmentToParent="left:0 top:0 right:0 bottom:0"/>
              <MenuBarButton Text="File" Alt="F">
                <att.SubMenu-set>
                  <ToolstripGroupContainer>
                    <ToolstripGroup>
                      <MenuItemButton Text="New" Alt="N" Image-uri="res://Images/New.png">
                        <att.SubMenu-set>
                          <MenuItemButton Command-ref="commandFileNewText" Alt="T"/>
                          <MenuItemButton Command-ref="commandFileNewXml" Alt="X"/>
                        </att.SubMenu-set>
                      </MenuItemButton>
                      <MenuItemButton Command-ref="commandFileOpen" Alt="O"/>
                      <MenuItemButton Command-ref="commandFileSave" Alt="S"/>
                      <MenuItemButton Command-ref="commandFileSaveAs" Alt="A"/>
                    </ToolstripGroup>
                    <MenuItemButton Text="Recent Files">
                      <att.SubMenu-set>
                        <MenuItemButton Text="(Empty)" Enabled="false" Visible-bind="self.recentFiles.observe as _(_.Count == 0 on _.ItemChanged)" />
                        <CustomControl Visible-bind="self.recentFiles.observe as _(_.Count != 0 on _.ItemChanged)">
                          <RepeatStack AlignmentToParent="left:0 top:0 right:0 bottom:0" MinSizeLimitation="LimitToElementAndChildren" Direction="Vertical">
                            <att.ItemSource-eval>self.recentFiles</att.ItemSource-eval>
                            <att.ItemTemplate>demo::RecentFileItemTemplate</att.ItemTemplate>
                          </RepeatStack>
                        </CustomControl>
                      </att.SubMenu-set>
                    </MenuItemButton>
                    <MenuItemButton Command-ref="commandFileExit" Alt="X"/>
                  </ToolstripGroupContainer>
                </att.SubMenu-set>
              </MenuBarButton>
              <MenuBarButton Text="Edit" Alt="E">
                <att.SubMenu-set>
                  <ToolstripGroupContainer>
                    <ToolstripGroup>
                      <MenuItemButton Command-ref="commandEditUndo" Alt="U"/>
                      <MenuItemButton Command-ref="commandEditRedo" Alt="R"/>
                    </ToolstripGroup>
                    <ToolstripGroup>
                      <MenuItemButton Command-ref="commandEditCut" Alt="X"/>
                      <MenuItemButton Command-ref="commandEditCopy" Alt="C"/>
                      <MenuItemButton Command-ref="commandEditPaste" Alt="V"/>
                    </ToolstripGroup>
                    <ToolstripGroup>
                      <MenuItemButton Command-ref="commandEditDelete" Alt="D"/>
                      <MenuItemButton Command-ref="commandEditSelect" Alt="A"/>
                    </ToolstripGroup>
                    <MenuItemButton Command-ref="commandEditFind" Alt="F"/>
                  </ToolstripGroupContainer>
                </att.SubMenu-set>

              </MenuBarButton>
              <MenuBarButton Text="Help" Alt="H">
                <att.SubMenu-set>
                  <MenuItemButton Command-ref="commandAbout" Alt="A"/>
                </att.SubMenu-set>
              </MenuBarButton>
            </ToolstripMenuBar>
          </Cell>

          <Cell Site="row:1 column:0">
            <ToolstripToolBar>
              <att.BoundsComposition-set AlignmentToParent="left:0 top:0 right:0 bottom:0"/>
              <ToolstripGroupContainer>
                <ToolstripGroup>
                  <ToolstripDropdownButton Image-uri="res://Images/New.png" Alt="N">
                    <att.SubMenu-set>
                      <MenuItemButton Command-ref="commandFileNewText" Alt="T"/>
                      <MenuItemButton Command-ref="commandFileNewXml" Alt="X"/>
                    </att.SubMenu-set>
                  </ToolstripDropdownButton>
                  <ToolstripSplitButton Command-ref="commandFileOpen" Alt="O">
                    <att.SubMenu-set>
                      <MenuItemButton Command-ref="commandFileOpenText" Alt="T"/>
                      <MenuItemButton Command-ref="commandFileOpenXml" Alt="X"/>
                    </att.SubMenu-set>
                  </ToolstripSplitButton>
                </ToolstripGroup>
                <ToolstripGroup>
                  <ToolstripButton Command-ref="commandFileSave" Alt="S"/>
                  <ToolstripButton Command-ref="commandFileSaveAs" Alt="A"/>
                </ToolstripGroup>
                <ToolstripGroup>
                  <ToolstripButton Command-ref="commandEditUndo" Alt="U"/>
                  <ToolstripButton Command-ref="commandEditRedo" Alt="R"/>
                </ToolstripGroup>
                <ToolstripGroup>
                  <ToolstripButton Command-ref="commandEditCut" Alt="X"/>
                  <ToolstripButton Command-ref="commandEditCopy" Alt="C"/>
                  <ToolstripButton Command-ref="commandEditPaste" Alt="V"/>
                </ToolstripGroup>
                <ToolstripButton Command-ref="commandEditDelete" Alt="D"/>
                <ToolstripButton Command-ref="commandEditFind" Alt="I"/>
              </ToolstripGroupContainer>
            </ToolstripToolBar>
          </Cell>

          <Cell Site="row:2 column:0">
            <MultilineTextBox ref.Name="textBox" HorizontalAlwaysVisible="false" VerticalAlwaysVisible="false">
              <att.BoundsComposition-set AlignmentToParent="left:0 top:0 right:0 bottom:0"/>
            </MultilineTextBox>
          </Cell>
        </Table>
      </Window>
    </Instance>
  </Instance>

  <Instance name="RecentFileItemTemplateResource">
    <Instance ref.CodeBehind="false" ref.Class="demo::RecentFileItemTemplate">
      <ref.Parameter Name="ViewModel" Class="presentation::controls::GuiToolstripCommand"/>
      <ControlTemplate MinSizeLimitation="LimitToElementAndChildren">
        <MenuItemButton Command-ref="ViewModel">
          <att.BoundsComposition-set AlignmentToParent="left:0 top:0 right:0 bottom:0"/>
        </MenuItemButton>
      </ControlTemplate>
    </Instance>
  </Instance>
</Folder>