// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
    // This sample demonstrates how to add content controls to a document.
    public class ContentControls
        public GcWordDocument CreateDocx()
            var doc = new GcWordDocument();
            // Heading:
            var p = doc.Body.Paragraphs.Add("Content Control Examples");
            p.Style = doc.Styles[BuiltInStyleId.Heading1];
            p = doc.Body.Paragraphs.Add(
                "Below are some examples of content controls. Open the document in MS Word to see the controls in action.");
            p.Style = doc.Styles[BuiltInStyleId.Subtitle];

            // ContentControlType.DropdownList:
            p = doc.Body.Paragraphs.Add("ContentControlType.DropdownList");
            p.Style = doc.Styles[BuiltInStyleId.Heading2];
            p = doc.Body.Paragraphs.Add("Select a fruit from the dropdown list control: ");
            p.Style = doc.Styles[BuiltInStyleId.Subtitle];

            var dropDownListCtrl = p.GetRange().ContentControls.Add(ContentControlType.DropdownList, false);
            dropDownListCtrl.DropDownItems.Add(new DropDownItem("apple", "Apples"));
            dropDownListCtrl.DropDownItems.Add(new DropDownItem("orange", "Oranges"));
            dropDownListCtrl.DropDownItems.Add(new DropDownItem("banana", "Bananas"));
            dropDownListCtrl.DropDownItems.Add(new DropDownItem("pear", "Pears"));

            // Add placeholder to the building blocks gallery:
            BuildingBlock dropDownPlaceholder = doc.GlossaryDocument.BuildingBlocks.Add("dropdownlist-placeholder", "General",
                type: BuildingBlockType.ContentControlPlaceholder);

            //set placeholder text
            Paragraph pp = dropDownPlaceholder.Body.Paragraphs.Add("Click to pick");
            //apply style to placeholder element
            pp.GetRange().Runs.First.Style = p.ParentBody.Document.Styles[BuiltInStyleId.Strong];
            //...and change its color
            pp.GetRange().Runs.First.Font.Color.RGB = Color.DarkSeaGreen;

            p.GetRange().Runs.Add(" The green 'Click to pick' text on the left is a placeholder, replaced with the picked fruit when you've selected one.");

            // Set control border color:
            dropDownListCtrl.Color.RGB = Color.OrangeRed;
            // Set control text color:
            dropDownListCtrl.Font.Color.RGB = Color.DarkOrange;

            // Use building blocks placeholder on the DropDownList contentControl.           
            dropDownListCtrl.ShowingPlaceholderText = true;
            dropDownListCtrl.PlaceholderText = dropDownPlaceholder;

            //  ContentControlType.Text
            p = doc.Body.Paragraphs.Add("ContentControlType.Text");
            p.Style = doc.Styles[BuiltInStyleId.Heading2];
            p = doc.Body.Paragraphs.Add("ContentControlType.Text allows entering a single run of text in the control. " +
                "All text in a Text control has the same formatting. In this case we use 'Block Text' paragraph style.");
            p.Style = doc.Styles[BuiltInStyleId.Subtitle];

            var textCtrl = doc.Body.ContentControls.Add(ContentControlType.Text, false);
            p = textCtrl.Content.GetRange().Paragraphs.Add(
                "This is the default content of the only run in the only paragraph in a Text content control.",

            // This code demonstrates that only one paragraph, and only one run in it,
            // can exist in a Text control. Both statements under 'try' clauses will fail:
                textCtrl.Content.GetRange().Paragraphs.Add("Another paragraph (cannot be added).");
            catch (InvalidContentControlSingleChildException)
                Console.WriteLine("Cannot have more than one paragraph to a Text content control.");
                p.GetRange().Runs.Add("Another run (cannot be added).");
            catch (InvalidContentControlSingleChildException)
                Console.WriteLine("Cannot have more than one run to a Text content control.");

            // ContentControlType.RichText
            p = doc.Body.Paragraphs.Add("ContentControlType.RichText");
            p.Style = doc.Styles[BuiltInStyleId.Heading2];
            p = doc.Body.Paragraphs.Add("ContentControlType.RichText allows having multiple paragraphs, with multiple runs, " +
                "in a single RichText content control.");
            p.Style = doc.Styles[BuiltInStyleId.Subtitle];

            var richtext = doc.Body.ContentControls.Add(ContentControlType.RichText, false);
            var p1 = richtext.Content.GetRange().Paragraphs.Add("First paragraphs in the ");
            p1.GetRange().Runs.Add("RichText ", doc.Styles[BuiltInStyleId.Strong]);
            p1.GetRange().Runs.Add("content control.");
            var p2 = richtext.Content.GetRange().Paragraphs.Add("Second paragraphs in the ");
            p2.GetRange().Runs.Add("RichText ", doc.Styles[BuiltInStyleId.Strong]);
            p2.GetRange().Runs.Add("content control.");

            p2.Style = doc.Styles[BuiltInStyleId.IntenseQuote];

            // ContentControlType.BuildingBlockGallery
            p = doc.Body.Paragraphs.Add("ContentControlType.BuildingBlockGallery");
            p.Style = doc.Styles[BuiltInStyleId.Heading2];
            p = doc.Body.Paragraphs.Add("ContentControlType.BuildingBlockGallery allows selecting items from " +
                "building block galleries. Here we allow to select an equation from the built-in Word equations gallery.");
            p.Style = doc.Styles[BuiltInStyleId.Subtitle];

            // We create a content control that allows selecting equations from 
            // the built-in Word equations gallery:
            var galleryControl = doc.Body.ContentControls.Add(ContentControlType.BuildingBlockGallery);
            galleryControl.BuildingBlockCategory = "Built-In";
            galleryControl.BuildingBlockGallery = "Equations";

            // ContentControlType.Group
            p = doc.Body.Paragraphs.Add("ContentControlType.Group");
            p.Style = doc.Styles[BuiltInStyleId.Heading2];
            p = doc.Body.Paragraphs.Add("ContentControlType.Group allows creating groups with modifiable " +
                "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.");
            p.Style = doc.Styles[BuiltInStyleId.Subtitle];

            // Group content control prevent some kinds of modifications of children controls.
            // Add a group content control that will be used as parent container for
            // child controls:
            var groupControl = doc.Body.ContentControls.Add(ContentControlType.Group, false);

            // Note how to add ContentControls inside another ContentControl:
            // Do not use groupControl.GetRange().ContentControls.Add(...),
            // instead use groupControl.Content.GetRange().ContentControls.Add(...):
            p = groupControl.Content.GetRange().Paragraphs.Add();
            var checkBox = p.GetRange().ContentControls.Add(ContentControlType.CheckBox, true);

            checkBox.Title = "Grouped checkbox";
            checkBox.Checked = true;

            p.GetRange().Runs.Add(" Text to the right of the checkbox");

            // Date:
            groupControl.Content.GetRange().Paragraphs.Add("Finally, we add a ContentControlType.Date control that shows the current date.");

            // Add a Date control
            var date = groupControl.Content.GetRange().ContentControls.Add(ContentControlType.Date, false);
            date.DateFormat = "u";
            date.Date = Util.TimeNow();

            //create representation of date time
            var dateParagraph = date.Content.GetRange().Paragraphs.Add();

            // In the Group contentControl, we have:
            // - a checkbox content control
            // - a text to its left
            // - a paragraph
            // - a date content control
            // Due to being inside Group content control, text cannot be changed unless
            // you disable grouping (Word->Developer->Group and select Ungroup).

            // Done:
            return doc;