Code Box



The primary purpose of a codebox is as it's name implies; a container for placing programming code. They can have other uses of course, limited to the user's imagination, but here we'll explain their uses for program code.
Because programming is a complex task but with a lot of repetition, most programmers keep a ¨library" of one kind or another, functions, routines and other structures that they use often. They may run into problems with how a program they have written works, and scour the web for examples or solutions that others may have for the problem. In short, they collect or have a lot of written code "snippets" that they somehow need to be able to find in their filing system. That's where code boxes come in. These give a programmer a place to put all this disparate code, with proper syntax highlighting, in one place. The containing node give them a place to put notes about the code, what it does, where it's useful, and anything else the programmer may want to add.
When you click on the "Insert a Codebox" icon, or select "Insert Codebox" from the menu (or right-click menu) you're presented with this a dialog:

images/44-1.png

Type: Choose whether the box will be used as a container for Plain Text (no formatting, coloring, etc.) or, by choosing Automatic Syntax Highlighting, as a true code-containing box. If you choose Automatic Syntax Highlighting, you'll be given the option to set what type of programming language the box will contain, and Cherrytree will apply the proper syntax highlighting to it. Cherrytree supports over 70 different program languages, so you're sure to find one that applies.
Size: Here you set, in either absolute pixels or a percentage of the node window, the width for your code box. Note that the pixel size is relative to the screen, so if you set a value of say 700, like the default here, and then reduce the size of the Cherrytree window, you will have to scroll from left to right to see all the box. If you choose percentage, the codebox will resize with the Cherrytree window. The height is only in pixels, and if the box is not big enough to show all the code in it, there will be a scroll bar on the right to allow you to scroll down or up.
Show line numbers: This option will put a sequential number in front of every line of code the box contains, useful if you want to be able to refer to a particular line or lines in your notes or in discussing the code with others.
Highlight Matching Brackets: This option will turn on a highlight to it's mate every time you put the cursor next to or on any "containment" character, be it a brace, bracket or parenthesis. This helps follow where and when things like variables and structures begin and end.

def LoadDataGrid():
global ColHeads
for c in FileList:
    treeview.insert('','end',values=c)
# adjust column's width if necessary to fit each value
for ix, val in enumerate(c):
    col_w = tkFont.Font().measure(val)
if treeview.column(ColHeads[ix],width=None)<col_w:
    treeview.column(ColHeads[ix], width=col_w)
def busyStart(newcursor=None):
global preBusyCursors
if not newcursor:
    newcursor = busyCursor
    newPreBusyCursors = {}
for component in busyWidgets:
    newPreBusyCursors[component] = component['cursor']
    component.configure(cursor=newcursor)
    component.update_idletasks()
    preBusyCursors = (newPreBusyCursors, preBusyCursors)
def busyEnd():
global preBusyCursors
if not preBusyCursors:
return
    oldPreBusyCursors = preBusyCursors[0]
    preBusyCursors = preBusyCursors[1]
for component in busyWidgets:
try:
    component.configure(cursor=oldPreBusyCursors[component])
except KeyError:
pass
    component.update_idletasks()


In the example above we have a snippet of some Python code. Syntax highlighting is set to Python, the box is set to 70% of the window size and 400 pixels high, line numbers is turned on, as well as Highlight Matching Brackets. If you put your cursor in the code box next to any of the brackets, you will see it's mate highlighted.

Code boxes also have their own right-click menus, with some options unique to them. You can change the input method to support various keyboards, insert UNICODE control characters, change the code boxe's properties, load from or save to text files, cut, copy or delete the code box, delete the code box but keep it's contents in the node, or change it's size. It also has options in the menu for undo/redo, cut, copy and paste for the code box contents. In fact most all of Cherrytree's text functions are also available in code boxes, such as moving lines of text up and down, duplicating lines, timestamps, list handling, etc.
NOTE: When searching for text, if the text you are searching for is found in a codebox, Cherrytree will focus the whole codebox but not highlight the internal found pattern. If in the codebox there are 4 instances of the text you search for, Cherrytree will just point to the fact that inside the codebox there is at least one instance of it.

<-Previous /Home/ Next ->

Index