ContentControlsHelpers.cs
//
// 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.
    // The code is the same as in the ContentControls example, but
    // uses the (new in v6.2) content creation helper methods,
    // which in most cases yield more robust and compact code.
    // Lines of code replaced by the new helper method calls are preserved
    // in the code of this example for reference, commented out with '//old:' comment.
    public class ContentControlsHelpers
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();
            // Heading:
            //old: var p = doc.Body.Paragraphs.Add("Content Control Examples");
            //old: p.Style = doc.Styles[BuiltInStyleId.Heading1];
            var p = doc.Body.AddParagraph("Content Control Examples", doc.Styles[BuiltInStyleId.Heading1]);
            //old: p = doc.Body.Paragraphs.Add("Below are some examples of content controls. Open the document in MS Word to see the controls in action.");
            //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
            p = doc.Body.AddParagraph(
                "Below are some examples of content controls. Open the document in MS Word to see the controls in action.",
                doc.Styles[BuiltInStyleId.Subtitle]);

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

            //old: var dropDownListCtrl = p.GetRange().ContentControls.Add(ContentControlType.DropdownList, false);
            var dropDownListCtrl = p.AddContentControl(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
            //old: Paragraph pp = dropDownPlaceholder.Body.Paragraphs.Add("Click to pick");
            var pp = dropDownPlaceholder.Body.AddParagraph("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;

            //old: 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.");
            p.AddRun(" 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
            //
            //old: p = doc.Body.Paragraphs.Add("ContentControlType.Text");
            //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
            doc.Body.AddParagraph("ContentControlType.Text", doc.Styles[BuiltInStyleId.Heading2]);
            //old: p = doc.Body.Paragraphs.Add("ContentControlType.Text allows entering a single run of text in the control. " +
            //old:     "All text in a Text control has the same formatting. In this case we use 'Block Text' paragraph style.");
            //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
            doc.Body.AddParagraph("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.",
                doc.Styles[BuiltInStyleId.Subtitle]);

            //old: var textCtrl = doc.Body.ContentControls.Add(ContentControlType.Text, false);
            var textCtrl = doc.Body.AddContentControl(ContentControlType.Text, false);
            //old: p = textCtrl.Content.GetRange().Paragraphs.Add(
            //old:     "This is the default content of the only run in the only paragraph in a Text content control.",
            //old:     doc.Styles[BuiltInStyleId.BlockText]);
            p = textCtrl.Content.AddParagraph(
                "This is the default content of the only run in the only paragraph in a Text content control.",
                doc.Styles[BuiltInStyleId.BlockText]);

            // 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:
            try
            {
                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.");
            }
            try
            {
                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
            //
            //old: p = doc.Body.Paragraphs.Add("ContentControlType.RichText");
            //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
            doc.Body.AddParagraph("ContentControlType.RichText", doc.Styles[BuiltInStyleId.Heading2]);
            //old: p = doc.Body.Paragraphs.Add("ContentControlType.RichText allows having multiple paragraphs, with multiple runs, " +
            //old:     "in a single RichText content control.");
            //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
            doc.Body.AddParagraph("ContentControlType.RichText allows having multiple paragraphs, with multiple runs, " +
                "in a single RichText content control.", doc.Styles[BuiltInStyleId.Subtitle]);

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

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

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

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

            //
            // ContentControlType.Group
            //
            //old: p = doc.Body.Paragraphs.Add("ContentControlType.Group");
            //old: p.Style = doc.Styles[BuiltInStyleId.Heading2];
            doc.Body.AddParagraph("ContentControlType.Group", doc.Styles[BuiltInStyleId.Heading2]);
            //old: p = doc.Body.Paragraphs.Add("ContentControlType.Group allows createing groups with modifiable " +
            //old:     "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.");
            //old: p.Style = doc.Styles[BuiltInStyleId.Subtitle];
            p = doc.Body.AddParagraph("ContentControlType.Group allows createing groups with modifiable " +
                "and constant (non-modifiable) content. Here we create a checkbox with a non-modifiable label on its right.",
                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:
            //old: var groupControl = doc.Body.ContentControls.Add(ContentControlType.Group, false);
            var groupControl = doc.Body.AddContentControl(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(...):
            //old: p = groupControl.Content.GetRange().Paragraphs.Add();
            p = groupControl.Content.AddParagraph();
            //old: var checkBox = p.GetRange().ContentControls.Add(ContentControlType.CheckBox, true);
            var checkBox = p.AddContentControl(ContentControlType.CheckBox, true);

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

            //old: p.GetRange().Runs.Add(" Text to the right of the checkbox");
            p.AddRun(" Text to the right of the checkbox");

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

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

            // 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;
        }
    }
}