# Customization - Creating a theme

## Creating your own Themes

Creating you own themes for the TinyMCE application is fairly easy if you know the basics of HTML, CSS and Javascript. The most easy way is to copy the "default" or the "advanced" template and rename it as your own name to for example "mytheme". After you copy the template you need to change the red sections marked below to "mytheme" this is needed so that themes don't overlap in other words it gives the theme a unique name. Then just alter the HTML code as you see fit but notice some elements needs to be there so check the docs below on each function also remember that your custom themes needs to be located in tiny_mce's "themes" directory. If you want you may add theme specific options/settings but remember to namespace them in the following format "theme_<your theme>_<option>".

The example below has three functions, these are explained in greater detail below.

    function TinyMCE_default_getEditorTemplate(settings) {
       var template = new Array();

       template['html'] = '<Some HTML>';
       template['delta_width'] = 0;
       template['delta_height'] = -40;

       return template;
    }

    function TinyMCE_default_getInsertLinkTemplate(settings) {
       var template = new Array();

       template['file'] = 'link.htm';
       template['width'] = 320;
       template['height'] = 130;

       return template;
    }

    function TinyMCE_default_getInsertImageTemplate(settings) {
       var template = new Array();

       template['file'] = 'image.htm';
       template['width'] = 320;
       template['height'] = 130;

       return template;
    }

    function TinyMCE_default_handleNodeChange(editor_id, node) {
       // Check what happend
    }

    function TinyMCE_default_execCommand(editor_id, element, command, user_interface, value) {
       // Your custom command logic
       return false;
    }

### Creating popup HTML files

When creating a popup you need to include the "tiny_mce_popup.js" this enables you to retrive the tinyMCE global instance in all popup windows. All variables and language definitions gets replaced in the page when it loads. So language variables such as {$lang_something} can be places in the HTML code, if you need to get a language string in JavaScript simply use the tinyMCE.getLang function.

### Example of simple popup file:

    <html>
    <head>
    <title>{$lang_plugin_sample_title}</title>
    <script language="javascript" src="../../tiny_mce_popup.js"></script>
    <script language="javascript">
         // getWindowArg returns any arguments passed to the window
         alert(tinyMCE.getWindowArg('some_arg'));
    </script>
    <body>
         <strong>{$lang_plugin_sample_desc}</strong>
    </body>

### The TinyMCE_<theme>_getEditorTemplate(settings) function

This function is responsible for the layout of the editor instance within the page it should return a name/value based array with some specific names. These are explained below, notice names included in [] characters are optional. The settings parameter to this function is a name/value array containing tinyMCE:s settings/options.

Template data
html | HTML template data, this value should contain all the HTML code needed to render the editor. Notice: {$<variable>} are replaces with values when used. More about these specific values later on.
[delta_width] | Delta width, this value should contain the relative width needed by the UI. For example if a toolbar takes 20 pixels this value should be -20. This is so the editor gets the same size as the element that are replaced.
[delta_height] | Delta height, this value should contain the relative width needed by the UI. For example if a toolbar takes 40 pixels this value should be -40. This is so the editor gets the same size as the element that are replaced.

Variables within the "html" value above are replaced with internal TinyMCE values. There are two types of variables one is the ones starting with the "lang_" prefix, these are replaced with the matching names in the language packs. So for example <b>{$lang_test}</b> gets replaces with the "tinyMCELang['lang_test']" variable and the output is then <b>Test</b>. The other variables are passed expicilty for the template used and these are listed below. Notice: Variables within [] characters are optional.

Variables
editor_id | This is the editor instance id and it should be placed in ID attribute of the IFRAME element that must be included in the template.
[default_document] | This will be replaced with a blank html page, this is added for MSIE security issues and should be placed in the SRC attribute of the IFRAME within the template.
[area_width] | Width of IFRAME area in pixels.
[area_height] | Height of IFRAME area in pixels.
[width] | Width of the whole editor area in pixels.
[height] | Height of the whole editor area in pixels.
[themeurl] | URL to theme location.

Notice: There are two javascript function that can be called from this template these are tinyMCE.execCommand that executes commans on the currenly selected editor area and the tinyMCE.switchClass that switches the CSS class of the element specified. For more details of commands available by execCommand check the Mozilla midas specification and the TinyMCE specific commands.

### The TinyMCE_<theme>_getInsertLinkTemplate(settings) function

This function is responsible for the layout of the insert link popup window and it should return a name/value based array with some specific names. These are explained below, notice names included in [] characters are optional. The settings parameter to this function is a name/value array containing tinyMCE:s settings/options.

Template data
html | HTML template data, this value should contain all the HTML code needed to render the link dialog. Notice: {$<variable>} are replaces with values when used. More about these specific values later on. This parameter is not needed if the "file" param is assigned.
file | Name of external template file to use, this may even be logic pages like .php,.asp,.jsp etc.
[width] | Width of popup window in pixels. Default is 320.
[height] | Height of popup window in pixels. Default is 200.

Variables within the "html" value above are replaced with internal TinyMCE values. There are two types of variables one is the ones starting with the "lang_" prefix, these are replaced with the matching names in the language packs. So for example <b>{$lang_test}</b> gets replaces with the "tinyMCELang['lang_test']" variable and the output is then <b>Test</b>. The other variables are passed expicilty for the template used and these are listed below. Notice: Variables within [] characters are optional.

Variables/Window arguments:
[href] | This variable gets replaced with the "href" attribute value of the selected link if one is selected.
[target] | This variable gets replaced with the "target" attribute value of the selected link if one is selected.
[css] | Theme popup css location.

Notice: There are a javascript function that can be called from this template "window.opener.tinyMCE.insertLink(href, target)" this inserts the link into the currently selected editor and should be called when for example a insert button is pressed.

### The TinyMCE_<theme>_getInsertImageTemplate(settings) function

This function is responsible for the layout of the insert image dialog, it should return a name/value based array with some specific names. These are explained below, notice names included in [] characters are optional. The settings parameter to this function is a name/value array containing tinyMCE:s settings/options.

Template data
html | HTML template data, this value should contain all the HTML code needed to render the image dialog. Notice: {$<variable>} are replaces with values when used. More about these specific values later on. This parameter is not needed if the "file" param is assigned.
[width] | Width of popup window in pixels. Default is 320.
[height] | Height of popup window in pixels. Default is 200.

Variables within the "html" value above are replaced with internal TinyMCE values. There are two types of variables one is the ones starting with the "lang_" prefix, these are replaced with the matchin names in the language packs. So for example <b>{$lang_test}</b> gets replaces with the "tinyMCELang['lang_test']" variable and the output is then <b>Test</b>. The other variables are passed expicilty for the template used and these are listed below. Notice: Variables within [] characters are optional.

Variables/Window arguments
[src] | This variable gets replaced with the "src" attribute value of the selected link if one is selected.
[alt] | This variable gets replaced with the "alt" attribute value of the selected link if one is selected.
[border] | This variable gets replaced with the "border" attribute value of the selected link if one is selected.
[hspace] | This variable gets replaced with the "hspace" attribute value of the selected link if one is selected.
[vspace] | This variable gets replaced with the "vspace" attribute value of the selected link if one is selected.
[width] | This variable gets replaced with the "width" attribute value of the selected link if one is selected.
[height] | This variable gets replaced with the "height" attribute value of the selected link if one is selected.
[align] | This variable gets replaced with the "align" attribute value of the selected link if one is selected.
[css] | Theme popup css location.

Notice: There are a javascript function that can be called from this template "window.opener.tinyMCE.insertImage(src, alt, border, hspace, vspace, width, height, align)" this inserts the image into the currently selected editor and should be called when for example a insert button is pressed.

### The TinyMCE_<theme>_handleNodeChange function (Optional)

This function is called when the cursor/selection of a editor instance changes. Then the currenly selected/focused node is passed to this function. This can be useful when you want to change the UI depending on what the user has selected.

Parameters
editor_id | Unique editor id, this is the same as the $editor_id variable in getEditorTemplate.
node | Reference to the Node element where the cursor is currenly located.
undo_index | Current undo index, this value is -1 if the custom undo/redo support is disabled.
undo_levels | Current number of undo levels, this value is -1 if the custom undo/redo support is disabled.
visual_aid | True/false state of visual aid/guidelines mode.
any_selection | Is any text or image selected.

### The TinyMCE_<theme>_execCommand function (Optional)

This function is called when a command is executed for example "bold" or "createlink" this callback/theme function may then intercept theme specific commands and do custom logic. If this command returns true the command handling is terminated and the default tinyMCE command handeling never gets executed.

Parameters
editor_id | Unique editor id, this is the same as the $editor_id variable in getEditorTemplate.
element | Reference to the document DOM root element of the editor instance.
command | Command that is to be executed for example "myCommand".
user_interface | true/false option if a user insterace is to be used or not.
value | Custom data value passed with command, may be any data type.

Returns:
true - Command intercepted and handled do not continue with command handling.
false - Continue with execCommand handling, bubble.

### The TinyMCE_<theme>_getControlHTML(control_name) function (Optional)

This function is called when a editor needs to render a specific control/button. This function should return the HTML template of that control or a empty string if the control_name wasn't recognized. Notice the variable {$pluginurl} gets replaced with the URL prefix for the current plugin directory.

Parameters
control_name | Control name to match against.

Returns: return the HTML template of that control or a empty string if the control_name wasn't recognized.