<Folder>
  <Instance name="DocumentEditorBaseResource">
    <Instance ref.CodeBehind="false" ref.Class="demo::DocumentEditorBase">
      <ref.Members>
        <![CDATA[
          @cpp:Protected
          prop EditModeCommand : ToolstripCommand* = null {const}
    
          @cpp:Private
          func SetEditMode(editMode: DocumentCommonInterface::EditMode) : void
          {
            var command =
              editMode == DocumentCommonInterface::EditMode::ViewOnly ? commandViewOnly :
              editMode == DocumentCommonInterface::EditMode::Selectable ? commandSelectable :
              commandEditable;

            document.EditMode = editMode;
            commandViewOnly.Selected = (command == commandViewOnly);
            commandSelectable.Selected = (command == commandSelectable);
            commandEditable.Selected = (command == commandEditable);
            SetEditModeCommand(command);
            self.UpdateSubscriptions();
          }
      
          @cpp:Protected
          func SelectAlignmentCommand() : ToolstripCommand*
          {
            var alignment = document.SummarizeParagraphAlignment(document.CaretBegin, document.CaretEnd);
            return
              alignment == Alignment::Left ? commandAlignLeft :
              alignment == Alignment::Center ? commandAlignCenter :
              alignment == Alignment::Right ? commandAlignRight :
              commandAlignDefault;
          }
      
          @cpp:Private
          func SetAlignment(alignment : Alignment?) : void
          {
            document.SetParagraphAlignment(document.CaretBegin, document.CaretEnd, alignment);
            self.UpdateSubscriptions();
          }
      
          @cpp:Private
          prop HasEditableSelection : bool = false {}
      
          @cpp:Private
          prop HasEditableSelectionInSingleParagraph : bool = false {}
      
          @cpp:Private
          func HasEditableCursor() : bool
          {
            return document.EditMode == DocumentCommonInterface::EditMode::Editable;
          }
      
          @cpp:Private
          func HasEditableHyperlink(forEdit : bool) : bool
          {
            var a = document.CaretBegin;
            var b = document.CaretEnd;
            return a.row == b.row and a.column != b.column;
          }
      
          @cpp:Protected
          func GetMenuContainer() : GraphicsComposition*
          {
            return menuContainer;
          }
      
          @cpp:Protected
          func GetToolstripContainer() : GraphicsComposition*
          {
            return toolstripContainer;
          }
      
          prop MenuContainer : GraphicsComposition* {GetMenuContainer}
          prop ToolstripContainer : GraphicsComposition* {GetToolstripContainer}
        ]]>
      </ref.Members>
      <ref.Ctor>
        <![CDATA[
          {
            SetEditMode(Editable);
          }
        ]]>
      </ref.Ctor>
      <CustomControl ref.Name="self" Text="Document Editor">
        <att.HasEditableSelection-bind>document.observe as _(_.CanCut() on _.SelectionChanged)</att.HasEditableSelection-bind>
        <MessageDialog ref.Name="dialogMessage" Title="You Clicked a Hyperlink!"/>
        <OpenFileDialog ref.Name="dialogOpen" Title="Select an Image" Filter="Image Files (*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp" EnabledPreview="true" Options="FileDialogFileMustExist|FileDialogDereferenceLinks"/>

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

          <Cell ref.Name="menuContainer" Site="row:0 column:0"/>
          <Cell ref.Name="toolstripContainer" Site="row:1 column:0"/>

          <Cell Site="row:2 column:0">
            <DocumentViewer ref.Name="document" EditMode="Editable" Alt="D">
              <att.BoundsComposition-set AlignmentToParent="left:0 top:0 right:0 bottom:0"/>
              <ev.ActiveHyperlinkExecuted-eval>
                <![CDATA[
                  {
                    dialogMessage.Text = document.ActiveHyperlinkReference;
                    dialogMessage.ShowDialog();
                  }
                ]]>
              </ev.ActiveHyperlinkExecuted-eval>
            </DocumentViewer>
          </Cell>
        </Table>

        <ToolstripCommand ref.Name="commandUndo" Text="Undo" Image-uri="res://ToolbarImages/Undo" LargeImage-uri="res://ToolbarImages/UndoLarge" ShortcutBuilder="Ctrl+Z">
          <att.Enabled-bind>document.observe as _(_.CanUndo() on _.UndoRedoChanged)</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[document.Undo();]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandRedo" Text="Redo" Image-uri="res://ToolbarImages/Redo" LargeImage-uri="res://ToolbarImages/RedoLarge" ShortcutBuilder="Ctrl+Y">
          <att.Enabled-bind>document.observe as _(_.CanRedo() on _.UndoRedoChanged)</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[document.Redo();]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandCopy" Text="Copy" Image-uri="res://ToolbarImages/Copy" LargeImage-uri="res://ToolbarImages/CopyLarge" ShortcutBuilder="Ctrl+C">
          <att.Enabled-bind>document.observe as _(_.CanCopy() on _.SelectionChanged)</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[document.Copy();]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandCut" Text="Cut" Image-uri="res://ToolbarImages/Cut" LargeImage-uri="res://ToolbarImages/CutLarge" ShortcutBuilder="Ctrl+X">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[document.Cut();]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandPaste" Text="Paste" Image-uri="res://ToolbarImages/Paste" LargeImage-uri="res://ToolbarImages/PasteLarge" ShortcutBuilder="Ctrl+V">
          <att.Enabled-bind>self.observe as _(document.CanPaste() on _.BoundsComposition.clipboardNotify)</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[document.Paste();]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandDelete" Text="Delete" Image-uri="res://ToolbarImages/Delete" LargeImage-uri="res://ToolbarImages/RemoveLinkLarge">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[document.SetSelectionText("");]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandSelect" Text="Select All" ShortcutBuilder="Ctrl+A">
          <ev.Executed-eval>
            <![CDATA[document.SelectAll();]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandInsertImage" Text="Insert Image ..." Image-uri="res://ToolbarImages/Image" LargeImage-uri="res://ToolbarImages/ImageLarge">
          <att.Enabled-bind>self.HasEditableCursor()</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                if (dialogOpen.ShowDialog())
                {
                  var imageService = INativeController::GetCurrentController().ImageService();
                  var image = imageService.CreateImageFromFile(dialogOpen.FileName);
                  var imageData = new ImageData^(image, 0);
                  document.EditImage(document.CaretBegin, document.CaretEnd, imageData);
                }
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandEditHyperlink" Text="Edit Hyperlink ..." Image-uri="res://ToolbarImages/Link" LargeImage-uri="res://ToolbarImages/LinkLarge">
          <att.Enabled-bind>document.observe as _(self.HasEditableHyperlink(true) on _.SelectionChanged)</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[
              $Async{
                var mainWindow = self.RelatedControlHost as controls::GuiWindow*;
            
                var window = new HyperlinkWindow*();
                window.MoveToScreenCenter(mainWindow.RelatedScreen);
                $Await window.ShowModalAsync(mainWindow);
            
                if (window.Url is not null)
                {
                  var row = document.CaretBegin.row;
                  var begin = document.CaretBegin.column;
                  var end = document.CaretEnd.column;
                  document.EditHyperlink(row, begin, end, cast string window.Url, "#NormalLink", "#ActiveLink");
                }
                delete window;
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandRemoveHyperlink" Text="Remove Hyperlink" Image-uri="res://ToolbarImages/RemoveLink" LargeImage-uri="res://ToolbarImages/RemoveLinkLarge">
          <att.Enabled-bind>document.observe as _(self.HasEditableHyperlink(false) on _.SelectionChanged)</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var row = document.CaretBegin.row;
                var begin = document.CaretBegin.column;
                var end = document.CaretEnd.column;
                document.RemoveHyperlink(row, begin, end);
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandBold" Text="Bold" Image-uri="res://ToolbarImages/Bold">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(_.SummarizeStyle(_.CaretBegin, _.CaretEnd).bold == true on _.SelectionChanged, _.ModifiedChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var style = new DocumentStyleProperties^();
                style.bold = not commandBold.Selected;
                document.EditStyle(document.CaretBegin, document.CaretEnd, style);
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandItalic" Text="Italic" Image-uri="res://ToolbarImages/Italic">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(_.SummarizeStyle(_.CaretBegin, _.CaretEnd).italic == true on _.SelectionChanged, _.ModifiedChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var style = new DocumentStyleProperties^();
                style.italic = not commandItalic.Selected;
                document.EditStyle(document.CaretBegin, document.CaretEnd, style);
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandUnderline" Text="Underline" Image-uri="res://ToolbarImages/Underline">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(_.SummarizeStyle(_.CaretBegin, _.CaretEnd).underline == true on _.SelectionChanged, _.ModifiedChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var style = new DocumentStyleProperties^();
                style.underline = not commandUnderline.Selected;
                document.EditStyle(document.CaretBegin, document.CaretEnd, style);
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandStrike" Text="Strike" Image-uri="res://ToolbarImages/Strike">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(_.SummarizeStyle(_.CaretBegin, _.CaretEnd).strikeline == true on _.SelectionChanged, _.ModifiedChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var style = new DocumentStyleProperties^();
                style.strikeline = not commandStrike.Selected;
                document.EditStyle(document.CaretBegin, document.CaretEnd, style);
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ColorDialog ref.Name="dialogColor" EnabledCustomColor="false"/>
        <FontDialog ref.Name="dialogFont" ShowEffect="false" ShowSelection="true" ForceFontExist="true"/>

        <ToolstripCommand ref.Name="commandFont" Text="Set Font ..." Image-uri="res://ToolbarImages/Font">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var begin = document.CaretBegin;
                var end = document.CaretEnd;
                var style = document.SummarizeStyle(begin, end);
            
                var baselineFont = document.Font;
                dialogFont.SelectedFont = 
                {
                  fontFamily:   ( style.face is null ? baselineFont.fontFamily : cast string style.face   )
                  size:         ( style.size is null ? baselineFont.size       : cast int style.size.size )
                };
            
                if (dialogFont.ShowDialog())
                {
                  style = new DocumentStyleProperties^();
                  var selectedFont = dialogFont.SelectedFont;
              
                  style.face = selectedFont.fontFamily;
                  style.size = {size:selectedFont.size relative:false};
                  document.EditStyle(begin, end, style);
                }
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandColor" Text="Text Color ..." Image-uri="res://ToolbarImages/Color">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var begin = document.CaretBegin;
                var end = document.CaretEnd;
                var selectedColor = document.SummarizeStyle(begin, end).color;
                if (selectedColor is null)
                {
                  dialogColor.SelectedColor = cast Color "#000000";
                }
                else
                {
                  dialogColor.SelectedColor = cast Color selectedColor;
                }
                if (dialogColor.ShowDialog())
                {
                  var style = new DocumentStyleProperties^();
                  style.color = dialogColor.SelectedColor;
                  document.EditStyle(begin, end, style);
                }
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandBackColor" Text="Background Color ..." Image-uri="res://ToolbarImages/BackColor">
          <att.Enabled-bind>self.HasEditableSelection</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[
              {
                var begin = document.CaretBegin;
                var end = document.CaretEnd;
                var selectedColor = document.SummarizeStyle(begin, end).backgroundColor;
                if (selectedColor is null)
                {
                  dialogColor.SelectedColor = cast Color "#000000";
                }
                else
                {
                  dialogColor.SelectedColor = cast Color selectedColor;
                }
                if (dialogColor.ShowDialog())
                {
                  var style = new DocumentStyleProperties^();
                  style.backgroundColor = dialogColor.SelectedColor;
                  document.EditStyle(begin, end, style);
                }
              }
            ]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandViewOnly" Text="Preview" Image-uri="res://ToolbarImages/ViewOnly" LargeImage-uri="res://ToolbarImages/ViewOnlyLarge" ShortcutBuilder="Ctrl+Shift+P">
          <ev.Executed-eval>
            <![CDATA[self.SetEditMode(ViewOnly);]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandSelectable" Text="Selectable" Image-uri="res://ToolbarImages/Selectable" LargeImage-uri="res://ToolbarImages/SelectableLarge" ShortcutBuilder="Ctrl+Shift+S">
          <ev.Executed-eval>
            <![CDATA[self.SetEditMode(Selectable);]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandEditable" Text="Editable" Image-uri="res://ToolbarImages/Editable" LargeImage-uri="res://ToolbarImages/EditableLarge" ShortcutBuilder="Ctrl+Shift+E">
          <ev.Executed-eval>
            <![CDATA[self.SetEditMode(Editable);]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandAlignDefault" Text="Set Alignment to Default (Left)" Image-uri="res://ToolbarImages/TextAlign">
          <att.Enabled-bind>self.HasEditableCursor()</att.Enabled-bind>
          <ev.Executed-eval>
            <![CDATA[self.SetAlignment(null);]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandAlignLeft" Text="Left" Image-uri="res://ToolbarImages/TextAlignLeft" LargeImage-uri="res://ToolbarImages/TextAlignLeftLarge">
          <att.Enabled-bind>self.HasEditableCursor()</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(self.SelectAlignmentCommand() == commandAlignLeft on _.SelectionChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[self.SetAlignment(Left);]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandAlignCenter" Text="Center" Image-uri="res://ToolbarImages/TextAlignCenter" LargeImage-uri="res://ToolbarImages/TextAlignCenterLarge">
          <att.Enabled-bind>self.HasEditableCursor()</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(self.SelectAlignmentCommand() == commandAlignCenter on _.SelectionChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[self.SetAlignment(Center);]]>
          </ev.Executed-eval>
        </ToolstripCommand>

        <ToolstripCommand ref.Name="commandAlignRight" Text="Right" Image-uri="res://ToolbarImages/TextAlignRight" LargeImage-uri="res://ToolbarImages/TextAlignRightLarge">
          <att.Enabled-bind>self.HasEditableCursor()</att.Enabled-bind>
          <att.Selected-bind>document.observe as _(self.SelectAlignmentCommand() == commandAlignRight on _.SelectionChanged)</att.Selected-bind>
          <ev.Executed-eval>
            <![CDATA[self.SetAlignment(Right);]]>
          </ev.Executed-eval>
        </ToolstripCommand>
      </CustomControl>
    </Instance>
  </Instance>
</Folder>