﻿<?xml version="1.0" encoding="utf-8"?>
<assembly name="Notepad" startlayout="MainForm" startoptions="default">

<!-- components -->
	<components>
		<component classname="TTextEditor" factory="editor.dll" factoryid="text_editor_component" />
		<component classname="TMyCustomPainter" factory="painter.dll" /> <!-- uses classname as factoryid -->
	</components>

<!-- global variables -->
	<variables>
		<var name="filename">Untitled</var>
		<var name="carretpos">0</var>
    <var name="doclength" />
		<var name="insertmode" value="Insert" />
		<const name="my_constant">This is my constant</const>
	</variables>

<!-- locales -->
	<locales>
		<locale code="fi" name="Finnish" icon="fi.png">
			<format type="time_short">h:nn</format>
      <format type="time_long">h:nn:ss</format>
			<format type="currency">?€</format>

      <strings fromfile="finnish.po" />
			<strings><![CDATA[
				# By default, PO format is used to describe translations.
				msgid "My Notepad"
				msgstr "Minun Notepad"

				msgid "This string is translated"
				msgstr "Tämä teksti on käännetty"
			]]></strings>
		</locale>
	</locales>

<!-- options -->
	<options name="default"> <!-- get's loaded on assembly startup -->
		<opt id="global.locale">fi</opt>
		<opt id="wnd-mainform.pos">[0,0,640,480]</opt>
	</options>

	<options name="my_session"> <!-- can be loaded manually -->
		<opt id="wnd-mainform.pos">[100,50,320,240]</opt>
		<opt id="wnd-mainform.editor.wordwrap">true</opt>
		<opt id="wnd-mainform.editor.specialcharacters">false</opt>
	</options>

<!-- main layout -->
	<layout name="MainForm">
		<site name="wnd-mainform" type="window" kind="sizable" text="$filename$" remember="pos,state,locale">
			<split_y>
				<!-- main menu -->
				<toolbars>
					<toolbar name="toolbar-mainmenu" class="mainmenu" resize="fill" painter="toolbar-menu" floating="false">
						<button name="menu-tools" posbefore="menu-help"> <!-- add button before Help menu -->
							<button text="Do something" />
						</button>
					</toolbar>
				</toolbars>
				<!-- text area -->
				<component name="editor" class="TTextEditor" resize="fill" remember="wordwrap,specialcharacters" />
				<!-- statusbar -->
				<toolbars>
					<toolbar name="toolbar-statusbar" class="statusbar" resize="fill" painter="toolbar-status-painter" remember="visibility" />
				</toolbars>
			</split_y>
		</site>
	</layout>

<!-- about dialog layout -->
	<layout name="AboutDlg">
		<site name="wnd-about" type="window" kind="dialog" text="About" height="240" width="320">
			<split_y>
				<text text="My Notepad" font="Arial,16pt" align="center" /> <!-- text is translated automatically -->
				<text rawtext="Credits goes here. %This string is translated%" font="Arial,9pt" align="left" /> <!-- text is not translated by default -->
				<button class="bitbutton" text="close" image="close.png" align="right" margin="5">
					<script event="execute"><![CDATA[
						begin
							Self.Site.Close;
						end.
					]]></script>
				</button>
			</split_y>
		</site>
	</layout>

<!-- bitmap button class -->
	<button classname="bitbutton" painter="mypainter">
		<split_x part="mybutton-background">
			<image name="button-image" part="mybutton-image"/>
			<text name="button-text" part="mybutton-text" align="center" resize="fill" />
		</split_x>
    <param param="image" link="button-image.image" /> <!-- link image parameter to button-image -->
		<param param="text" link="button-text.text"> <!-- link text parameter to button-text -->
			<!-- when setting the parameter, change it to camel case.  -->
			<script event="set"><![CDATA[
				begin
					// override the default function and manually set the parameter.
					//Self.SetParam('button-text.text', CamelCase(ParamValue));
					//Result:= true;

					// change the parameter value and use the default function to set it.
					ParamValue:= CamelCase(ParamValue);
					Result:= false;
				end.
			]]></script>
		</param>
	</button>

<!-- toolbar classes -->
	<toolbar classname="mainmenu">
		<!-- File menu -->
		<button name="menu-file" text="File">
			<button action="act_new" />
      <button action="act_open" />
			<separator />
      <button action="act_save" />
      <button action="act_save_as" />
			<separator />
      <button action="act_exit" />
		</button>
		<!-- Edit menu -->
		<button name="menu-edit" text="Edit">
      <button action="act_copy" />
      <button action="act_cut" />
      <button action="act_paste" />
			<separator />
      <button action="act_delete" />
			<separator />
      <button action="act_select_all" />
		</button>
		<!-- View menu -->
		<button name="menu-view" text="View">
      <button action="act_wordwrap" />
      <button action="act_special_chars" />
			<separator />
      <button action="act_statusbar" />
		</button>
		<!-- Help menu -->
		<button name="menu-help" text="Help">
      <button action="act_about" />
		</button>
	</toolbar>

	<toolbar classname="statusbar">
		<label text="$filename$" resize="fill" />
		<label text="$carretpos$" />
    <label text="$doclength$" />
		<label text="$insertmode$" />
	</toolbar>

<!-- actions -->
	<!-- New -->
	<action name="act_new" text="New" shortcut="Ctrl+N">
		<script event="execute"><![CDATA[
			begin
        // Global.Execute('editor.Clear', '');
        ExecuteProc('editor.Clear'); // same as above

				Global.Variable['filename']:= 'Untitled';
			end.
		]]></script>
	</action>

	<!-- Open -->
	<action name="act_open" text="Open" shortcut="Ctrl+O">
		<script event="execute"><![CDATA[
			procedure OnExecute(Sender: ICEControl; Action: ICEAction);
			var
				s: String;
			begin
				if ShowOpenDialog(s) then
				begin
					s:= MakeParam('path', s); // creates qualified parameter like so: path="c:\this is the s\"
					Execute('editor.LoadFromFile', s);
				end;
			end;
		]]></script>
	</action>

	<!-- Save -->
	<action name="act_save" text="Save" shortcut="Ctrl+S" enabled="$filepath$">
		<script event="execute" cache="true"><![CDATA[
			// Handle action execute
			procedure OnExecute(Sender: ICEControl; Action: ICEAction);
			var
				s: String;
				editor: ICEControl;
			begin
				s:= Global.Variable['filepath'];
				if s <> '' then
				begin
					editor:= Global.FindControl('editor');
					if assigned(editor) then
          editor.Execute('SaveToFile', MakeParam('path', s));
				end;
			end;
		]]></script>
	</action>

	<!-- Save As -->
	<action name="act_save_as" text="Save As..." shortcut="Ctrl+Alt+S">
		<script event="execute"><![CDATA[
			// Handle action execute
			procedure OnExecute(Sender: ICEControl; Action: ICEAction);
			var
				s: String;
			begin
				if ShowSaveDialog(s) then
				begin
					Execute('editor.SaveToFile', MakeParam('path', s));
          Global.Variable['filepath']:= s;
          Global.Variable['filename']:= ExtractFileName(s);
				end;
			end;
		]]></script>
	</action>

	<!-- Exit -->
	<action name="act_exit" text="Exit">
		<script event="execute"><![CDATA[
   		begin
				Application.Terminate;
			end.
		]]></script>
	</action>

	<!-- Copy (uses reusable script class to do the update) -->
	<script classname="enable_when_text_selected" cache="true"><![CDATA[
		begin
			Self.Enabled:= Global.GetIntParam('editor.SelLength') <> 0;
      //Self.Enabled:= Sender.GetIntParam('SelLength') <> 0; // this can be used also if the sender is known.
		end.
	]]></script>

	<action name="act_copy" text="Copy">
    <script event="execute"><![CDATA[
			begin
				ExecuteProc('editor.CopyToClipboard');
			end.
    ]]></script>
		<script class="enable_when_text_selected" event="notify" notify="ParamChanged" param="selection" sender="editor" />
	</action>

	<!-- Cut -->
	<action name="act_cut" text="Cut">
    <script event="execute,notify" notify="ParamChanged" param="selection" sender="editor"><![CDATA[
			procedure OnExecute(Sender: ICEControl; Action: ICEAction);
			begin
				ExecuteProc('editor.CutToClipboard');
			end;

			procedure OnNotify(Sender: ICEControl; Action: ICEAction; Notify: String; Param: String);
			begin
				Action.Enabled:= Sender.GetBoolParam('SelLength'); // will return true if SelLength <> 0
			end;
    ]]></script>
	</action>

	<!-- Paste -->
	<action name="act_paste" text="Paste">
    <script event="execute"><![CDATA[
			begin
    		ExecuteProc('editor.PasteFromClipboard');
			end.
    ]]></script>
	</action>

	<!-- Delete -->
	<action name="act_delete" text="Delete">
    <script event="execute"><![CDATA[
			begin
				ExecuteProc('editor.DeleteSelection');
			end.
    ]]></script>
    <script class="enable_when_text_selected" event="notify" notify="ParamChanged" param="selection" sender="editor" />
	</action>

	<!-- Select All -->
	<action name="act_select_all" text="Select All" type="execute_proc" proc="editor.SelectAll" />

	<!-- Word wrap -->
	<action name="act_wordwrap" text="Word Wrap">
    <script event="execute"><![CDATA[
			begin
				// SetBoolParam('editor.WordWrap', not GetBoolParam('editor.WordWrap'));
				ToggleParam('editor.WordWrap'); // same as above
			end.
    ]]></script>
		<script event="notify" notify="ParamChanged" param="wordwrap" sender="editor">
    <![CDATA[
			begin
        Action.Checked:= Sender.GetBoolParam('WordWrap');
			end.
		]]></script>
	</action>

	<!-- Special characters -->
	<action name="act_special_chars" text="Special Characters">
    <script event="execute"><![CDATA[
   		begin
        ToggleParam('editor.SpecialCharacters');
			end.
    ]]></script>
		<script event="notify" notify="ParamChanged" param="specialcharacters" sender="editor">
    <![CDATA[
			begin
				Action.Checked:= Sender.GetBoolParam('SpecialCharacters');
			end.
		]]></script>
	</action>

	<!-- Statusbar -->
	<action name="act_statusbar" text="Status Bar">
    <script event="execute"><![CDATA[
			var
				ctrl: ICEControl;
   		begin
				// ctrl:= Global.FindControl('toolbar-statusbar');
				// if assigned(ctrl) then
				// ctrl.Visibile:= not ctrl.Visible;

        // SetVisibility('toolbar-statusbar', not GetVisibility('toolbar-statusbar')); // same as above

				ToggleVisibility('toolbar-statusbar'); // same as above
			end.
    ]]></script>
		<script event="notify" notify="VisibilityChanged" sender="toolbar-statusbar">
    <![CDATA[
			begin
        Action.Checked:= Sender.Visible;
			end.
		]]></script>
	</action>

    <!-- same as above -->
		<action name="act_statusbar" text="Status Bar" type="toggle_visibility" control="toolbar-statusbar" />

	<!-- About -->
	<action name="act_about" text="About">
    <script event="execute"><![CDATA[
			var
				dlg: ICESite;
			begin
				dlg:= Global.FindSite('wnd-about');
				if not assigned(dlg) then
				begin
					Global.LoadLayout('AboutDlg');
          dlg:= Global.FindSite('wnd-about');
					dlg.CenterToScreen;
				end;
				dlg.BringToFront;
			end.
    ]]></script>
	</action>

<!-- Scripts -->

	<!-- carret position changed -->
	<script event="notify" notify="ParamChanged" param="selection" sender="editor" cache="true"><![CDATA[
		var
			s: String;
		begin
			s:= 'Line: ' + Sender.GetParam('Carret_Line') +
					' Char: ' + Sender.GetParam('Carret_Char') +
          ' Char: ' + Sender.GetParam('SelLength');

			Global.Variable['carretpos']:= s;
		end.
  ]]></script>

  <!-- document length changed -->
	<script event="notify" notify="ParamChanged" param="length" sender="editor" cache="true"><![CDATA[
  	var
			s: String;
		begin
			s:= 'Length: ' + Sender.GetParam('Length') +
					' Lines: ' + Sender.GetParam('Lines_Count');

			Global.Variable['doclength]:= s;
		end.
  ]]></script>

  <!-- insert mode changed -->
	<script event="notify" notify="ParamChanged" param="insertmode" sender="editor" cache="true"><![CDATA[
  	begin
			if Sender.GetBoolParam('InsertMode') then
			Global.Variable['insertmode]:= 'Insert'
			else
      Global.Variable['insertmode]:= 'Override'
		end.
  ]]></script>

<!-- painters -->
	<painter name="toolbar-menu-painter">
		<part id="toolbar-background">
			<fill color="[#ccccccff,0];[#bbbbbbff,255]" /> <!-- gradient from #ccccccff to #bbbbbbff with 255 steps -->
		</part>
		<part id="toolbar-item" state="normal">
      <textout text="$TEXT$" color="#000000ff" bounds="fill" align="center" valign="center" />
		</part>
		<part id="toolbar-item" state="highlight" dodefault="false">
			<fill color="#ffffffff" />
      <textout text="$TEXT$" color="#000000ff" bounds="fill" align="center" valign="center" />
		</part>
    <part id="toolbar-submenu-background" painter="popupmenu-painter" part="popup-background" />
		<part id="toolbar-submenu-item" painter="popupmenu-painter" part="popup-item" />
	</painter>

	<painter name="popupmenu-painter">
		<part id="popup-background">
			<script state="normal" cache="true"><![CDATA[
				begin
					Canvas.Fill(Sender, Sender.ClientRect, $ccccccff);
				end.
			]]></script>
		</part>
		<part id="popup-item" dodefault="true">
			<state state="normal">
        <fill color="#ccccccff" />
				<font color="#000000ff" style="normal" />
				<default /> <!-- this is where the default painting is done -->
        <fill color="#33333322" />
			</state>

			<state state="highlight">
				<script state="normal" cache="true"><![CDATA[
					begin
						Canvas.Fill(Sender, Sender.ClientRect, $ccccccff);
						Canvas.Font.Color:= $000000ff;
						Canvas.Font.Style:= [fsBold];
						Canvas.PaintDefault(Sender);
					end.
				]]></script>
			</state>
		</part>
	</painter>

	<painter name="mypainter">
		<part id="mybutton-background">
      <fill color="[#ccccccff,0];[#bbbbbbff,255]" />
		</part>
		<part id="mybutton-text" dodefault="false">
      <textout text="$TEXT$" color="#000000ff" bounds="fill" align="center" valign="center" />
		</part>
	</painter>

	<painter name="toolbar-status-painter" class="TMyCustomPainter" />

</assembly>