Scripting:How to create a new tool window
From TSWiki
Creating a new Tool window is as easy as creating a new toolbar. Read on for a complete tutorial, or check the final, complete example.
Contents |
[edit] Importing the tool window 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.Dock.Net20")
from ActiproSoftware.UIStudio.Dock import ToolWindow
[edit] Creating the tool window
You can create a new tool window like this:
# Create the tool window. Parameters: DockManager, Key, Text, ImageIndex and ChildControl window = ToolWindow(MainForm.DockManager, "mytoolwindow", "Test window", -1, None) # Activate the window to make it visible window.Activate()
Actually, it's as simple as that - running those lines of code, will give you a brand new tool window, which you may start adding content to. However, there is a slight problem.
[edit] Dealing with scripted tool windows
When you create new tool windows from scripting, these will be included as a part of WebCoder. That also means that unless you have turned off the feature that saves your tool window layout on exit (Functions -> Settings -> Interface), the tool window will be saved along with the rest of the windows, and it will automatically be re-created once WebCoder starts again. Is this a problem? Yes, sort of, because the content of the window and the scripted functionality will not be remembered! That means you will end up with an empty window, which is not really what we're looking for. There are two solutions:
1. Hacking into the WebCoder event that fires once the application closes, and make sure that the window is disposed before the layout is saved. It can be done like this:
def webcoder_closing(sender, eventargs): window.Dispose() MainForm.Closing += webcoder_closing
It's very simple. We simply add our own event to the MainForm's Closing event, and once it fires, we dispose of our window. This will prevent WebCoder from ever saving your window, allowing you to create it from your script when you need it.
2. The second solution is probably more ideal in most cases. We simply do a check before we create the window, allowing us to get a reference to it from the DockManager if it already exists:
# See if the window already exists, based on the Key we created it with if(MainForm.DockManager.ToolWindows["mytoolwindow"] != None): # If it does, get a reference to it window = MainForm.DockManager.ToolWindows["mytoolwindow"] else: # Create the tool window. Parameters: DockManager, Key, Text, ImageIndex and ChildControl window = ToolWindow(MainForm.DockManager, "mytoolwindow", "Test window", -1, None) # Activate the window to make it visible window.Activate()
This is the solution we will be using for the rest of this tutorial, mainly because it offers one big advantage over the first solution: Our tool window will be saved just like any other window, which means that if we move it around and position it somewhere else, this will be remembered!
[edit] Adding content to the window
Okay, so we have an empty window, but that's not very useful, is it? Let's add some content to it, which is very easy! As with anything else, this has great potential, but for demonstrational purposes, we will do something very simple: A textbox and a button, where the button will insert text into the textbox. It's not rocketscience, but hopefully you can come up with something more interesting. To do this, we need to import a new assembly. This should go in the top:
clr.AddReferenceByPartialName("System.Windows.Forms")
from System.Windows.Forms import *
Now, let's add some content!
textbox = TextBox() textbox.Multiline = True textbox.Dock = DockStyle.Fill window.Controls.Add(textbox) def button_click(sender, eventargs): textbox.Text += "This is a scripted line!\r\n" button = Button() button.Text = "Add something!" button.Dock = DockStyle.Bottom button.Click += button_click window.Controls.Add(button)
This will create a textbox and a button, add them to the window, and position them properly. When the button is clicked, a simply line of text is appended to the textbox.
[edit] Extras
[edit] Using an icon
There are two ways to get an icon on your tool window: Either you use one from the WebCoder Imagelist, or you load on of your own. You should use the proper constructor of the tool window depending on which way you wish to obtain an icon.
Using an image from the WebCoder imagelist:
window = ToolWindow(MainForm.DockManager, "mytoolwindow", "Test window", 2, None)
This will make the tool window use image number 2 in the WebCoder imagelist, which just happens to be the Save icon.
The other approach is to load your own image. This is quite easy as well:
clr.AddReferenceByPartialName("System.Drawing")
from System.Drawing import Image
img = Image.FromFile("C:\\path\\to\\image.bmp")
window = ToolWindow(MainForm.DockManager, "mytoolwindow", "Test window", img, None)
Most BMP, JPEG, PNG and GIF icons should work. Don't forget to use doubleslashes in the path.
[edit] Complete tool window example
clr.AddReferenceByPartialName("ActiproSoftware.UIStudio.Dock.Net20")
clr.AddReferenceByPartialName("System.Windows.Forms")
from ActiproSoftware.UIStudio.Dock import ToolWindow
from System.Windows.Forms import *
# See if the window already exists, based on the Key we created it with
if(MainForm.DockManager.ToolWindows["mytoolwindow"] != None):
# If it does, get a reference to it
window = MainForm.DockManager.ToolWindows["mytoolwindow"]
else:
# Create the tool window. Parameters: DockManager, Key, Text, ImageIndex and ChildControl
window = ToolWindow(MainForm.DockManager, "mytoolwindow", "Test window", -1, None)
# Activate the window to make it visible
window.Activate()
# Create a new textbox and add it to the window
textbox = TextBox()
textbox.Multiline = True
textbox.Dock = DockStyle.Fill
window.Controls.Add(textbox)
# Define an eventhandler for the button
def button_click(sender, eventargs):
textbox.Text += "This is a scripted line!\r\n"
# Create a new button and add it to the window
button = Button()
button.Text = "Add something!"
button.Dock = DockStyle.Bottom
button.Click += button_click
window.Controls.Add(button)
