Scripting:How to create a new toolbar
From TSWiki
WebCoder comes with a bunch of toolbars which you may customize to your likings. However, creating a brand new toolbar, with your own functionality, can be very useful for adding custom functionality to WebCoder. Fortunately, it's pretty easy. Just follow this guide, or if you're not the patient type, have a look at the complete & finished example.
Contents |
[edit] Importing the toolbar assembly
WebCoder uses a 3rd party control set for toolbars and tool windows. Therefore, you need to import an assembly that comes with WebCoder, to start using the functionality. Do it like this:
clr.AddReferenceByPartialName("ActiproSoftware.UIStudio.Bar.Net20")
from ActiproSoftware.UIStudio.Bar import *
You will now have access to all the functionality of the toolbar library. Let's start using it.
[edit] Creating the toolbar
You create a new toolbar like this:
# Create a new DockableToolbar, with the name/title "My toolbar"
bar = DockableToolBar("My toolbar")
# Add it to the collection of WebCoder toolbars
MainForm.BarManager.DockableToolBars.Add(bar)
# Activate it, to make sure it's visible
bar.Active = True
# We create it as a temporary toolbar, which means it will be
# destroyed if the user closes the toolbar
bar.CreationStyle = DockableToolBarCreationStyle.Temporary
Try running the script, and you will see a new, empty toolbar. So far, so good.
[edit] Adding buttons
Let's add a button to the toolbar. It will simply insert a simple set of tags into the active window, and before we create the button, we will define a function for the button to use:
def Button1Click(sender, eventargs):
ScriptUtils.InsertTags("<center>", "</center>")
The def keyword is used to define a function, in this case a click event for the button. The function is very simply, it just uses the InsertTags function of the ScriptUtils library, which just inserts two pieces of text and places the cursor between them.
Now, let's add a button to use this function:
# Create a new BarButtonCommand, that is, a simple toolbar button
# Parameters: Category, Name, Text and ImageIndex
cmd = BarButtonCommand("", "Button1", "Center tags", -1)
# Add our eventhandler to the Click event of the button
cmd.Click += Button1Click
# Add the commandlink to our toolbar
bar.CommandLinks.Add(cmd.CreateCommandLink())
Okay, two problems here: The button does not seem visible when the script is executed, and when it's clicked, WebCoder tells you something about the command not being implemented.
[edit] Invalidating the toolbar
After adding buttons to the toolbar, you need to ask WebCoder to re-draw it, to compensate for the new content. To do so, you need access to another assembly, and import a couple of types. Add these lines to the top of your script:
clr.AddReferenceByPartialName("ActiproSoftware.WinUICore.Net20")
from ActiproSoftware.WinUICore import InvalidationLevels, InvalidationTypes
Now, at the end of the script, or at least after you're done adding buttons, add the following line:
bar.Invalidate(InvalidationLevels.All, InvalidationTypes.All)
[edit] Getting rid of the "not implemented" message
WebCoder will try to handle the click event of any toolbar button, even the ones you create. However, usually you will handle the click event with your own code, so we need a way to make WebCoder ignore a click from one of your buttons. It's a bit of a hack, but you simply need to prefix the name of your buttons with ScriptedControl_. So, make a modification in the script:
#cmd = BarButtonCommand("", "Button1", "Center tags", -1)
cmd = BarButtonCommand("", "ScriptedControl_Button1", "Center tags", -1)
That's it! You now have a toolbar with a single button. To add more, simply follow the same steps. I will now show you a couple of extra tips, and at last, the final example.
[edit] Adding icons
There are two options if you wish to use icons on your toolbar buttons: You may either use one of the icons already used by WebCoder, or you may load your own icon. If you wish to use a WebCoder icon, you may specify an ImageIndex when creating the button. For instance, to use the Save icon of WebCoder, create the button like this:
cmd = BarButtonCommand("", "ScriptedControl_Button1", "Center tags", 2)
Alternatively, you may load an icon of your own, by adding the following line, after creating the button:
cmd.Image = Image.FromFile("C:\\Icons\\icon.bmp")
Most BMP, JPEG, PNG and GIF icons should work. Don't forget to use doubleslashes in the path.
[edit] Complete toolbar example
clr.AddReferenceByPartialName("ActiproSoftware.UIStudio.Bar.Net20")
clr.AddReferenceByPartialName("ActiproSoftware.WinUICore.Net20")
from ActiproSoftware.UIStudio.Bar import *
from ActiproSoftware.WinUICore import InvalidationLevels, InvalidationTypes
# Create a new DockableToolbar, with the name/title "My toolbar"
bar = DockableToolBar("My toolbar")
# Add it to the collection of WebCoder toolbars
MainForm.BarManager.DockableToolBars.Add(bar)
# Activate it, to make sure it's visible
bar.Active = True
# We create it as a temporary toolbar, which means it will be
# destroyed if the user closes the toolbar
bar.CreationStyle = DockableToolBarCreationStyle.Temporary
# Click eventhandler for the first button
def Button1Click(sender, eventargs):
ScriptUtils.InsertTags("<center>", "</center>")
# Create a new BarButtonCommand, that is, a simple toolbar button
# Parameters: Category, Name, Text and ImageIndex
cmd = BarButtonCommand("", "ScriptedControl_Button1", "Center tags", -1)
# Add our eventhandler to the Click event of the button
cmd.Click += Button1Click
# Add the commandlink to our toolbar
bar.CommandLinks.Add(cmd.CreateCommandLink())
# Make sure the toolbar is repainted
bar.Invalidate(InvalidationLevels.All, InvalidationTypes.All)
