Next: Writing Methods Up: Widget Class Declaration Previous: Widget Class Declaration

 

Using the tixWidgetClass Command 

We can use the tixWidgetClass command to declare a new class. The syntax is:

tixWidgetClass classCommandName {
-switch value
-switch value
....
}

For example, the following is the declaration section of TixArrowButton:

tixWidgetClass tixArrowButton {
-classname TixArrowButton
-superclass tixPrimitive
-method {
flash invoke invert
}
-flag {
-direction -state
}
-configspec {
{-direction direction Direction e}
{-state state State normal}
}
-alias {
{-dir -direction}
}
-default {
{*Button.anchor c}
{*Button.padX 5}
}
}

(Figure 6-2) declaration of the TixArrowButton Class 

We'll look at what each option means as I describe the command in the following sections.

The first argument for tixWidgetClass is the command name for the widget class (tixArrowButton). Command names are used to create widgets of this class. For example, the code

tixArrowButton .arrow

creates a widget instance .arrow of the class TixArrowButton. Also, the command name is used as a prefix of all the methods of this class. For example, the Foo and Bar methods of the class TixArrowButton will be written as tixArrowButton:Foo and tixArrowButton:Bar.

The class name of the class (TixArrowButton)is specified by the -classname switch inside the main body of the declaration. The class name is used only to specify options in the TK option database. For example, the following commands specifies the TixArrowButton widget instances should have the default value up for their -direction option and the default value normal for their -state option.

option add *TixArrowButton.direction up
option add *TixArrowButton.state normal

Notice the difference in the capitalization of the class name and the command name of the TixArrowButton class: both of them has the individual words capitalized, but the command name (tixArrowButton)starts with a lower case letter while the class name (TixArrowButton) starts with an upper case letter. When you create your own classes, you should follow this naming convention.

The -superclass switch specifies the superclass of the new widget. In our example, we have set it to tixPrimitive. Again, pay attention to the capitalization: we should use the command name of the superclass, not its class name.


http://tix.sourceforge.net