6.12 Controlling the interface

nip2 looks at the scraps of program you type in and execute and tries to show them on the screen in a graphical way. The sorts of display you get depend on where in nip2 you define the expression, and what sort of value it has.

6.12.1 Tools and toolkits

Definitions in toolkits are turned into menus off the Toolkits menu in the main window, and added to the toolkit browser. Toolkits are loaded from files at startup or can be made in the program window. Toolkit or a definition names which start with an underscore character are hidden and not displayed. The toolkits are always displayed in alphabetical order, but you can order the items within a toolkit in any way you like.

There are two ways to write toolkit definitions. Function definitions and zero-argument classes simply appear as menu items, built from static analysis of their source code. However, if a definition evaluates to an instance of the class Menu, a menu item is built from dynamic analysis of the value of the definition.

Static menu items

Zero-argument classes within toolkits are displayed as pull-right menus. You can nest classes to any depth.

nip2 uses the first line of the comment before a definition as help text for that function, so it’s a good idea to put a simple one-line description of the function at the start of a comment.

For example, if the following text is placed in a file called Fred.def on nip2’s start path, you’ll get a menu in the tookits called Fred with a pull-right and a tooltip. See Figure 6.1.

Banana a = a ⋆ 3;  
 
Subfred = class {  
  // add two things  
  Jim a b = a + b;  
  Apple e = e ⋆ 12;  
  Harry z = 12 + z;  
}


PIC


Figure 6.1: How Fred.def will look


Dynamic menu items

Dynamic menus give you much more control over the way menus are drawn and make it easy to reuse menus. A dynamic menu item is a class instance that is a sub-class of Menuitem. It needs to have three members: label, the text that should appear in the menu (with an underscore character to indicate the mnenonic); tooltip, a short hint that appears as a tooltip or in the toolkit browser; icon, an optional image file to be displayed in the menu next to the text; and action, the function that is called when the menu item is activated. label and tooltip are constructor arguments for Menu.

So for example:

Wombat_find_item = class Menuitem  
  "_Find Wombat"  
  "analyse image and locate wombat" {  
  icon = "nip-slider-16.png";  
  action x = im_wombat_locate x;  
}

will appear as shown in Figure 6.2.


PIC


Figure 6.2: How Wombat_find_item will look


A dynamic pullright menu is a subclass of Menupullright. It’s just like Menuitem, but without the need for an action member. Any members which are subclasses of Menu are displayed as items in the submenu. So again:

Wombat_item = class Menupullright  
  "_Wombat"  
  "wombat-related operations" {  
  icon = "nip-slider-16.png";  
  item1 = Wombat_find_item;  
  sep = Menuseparator;  
  boink = Wombat_find_item;  
}

will appear as shown in Figure 6.3.


PIC


Figure 6.3: How Wombat_item will look


6.12.2 Workspaces

Definitions in workspaces are displayed with nip2’s class browser. Each row is displayed in four main parts: a button for the row name, a line of text, a set of sub-rows for the members of the row’s class, and a graphic display representing the row’s value. See Figure 6.4.


PIC


Figure 6.4: Components of a workspace row


The text part of the right-hand-side of each row is always displayed, but the sub-rows are only displayed if the row represents a class, and the graphic is only displayed if the class is an instance of one of the classes in Table 6.5. You can subclass these if you want to use the graphic display in your own widgets.

There are three separate ways to set the value for a row. You can edit the line of program text, you can edit one of the members, or you can manipulate the graphic representation (dragging a slider, or moving a region). These can be contradictory, so nip2 resolves conflicts by always applying changes in the order text, then graphic, then member.

When it applies a graphic change, nip2 rebuilds the class using a class member called class-name_edit, or if that is not defined, the class’s constructor member. For example, the Colour class can be defined as:

Colour colour_space value = class {}  
A1 = Colour "sRGB" [255,0,0];

There are two ways to change A1. You can open A1 and change colour_space to ”Lab”, or you can double-click on the swatch and drag the disc. When you click OK on the colour edit dialog, nip2 searches for a member called Colour_edit, fails to find it, and so picks the Colour member instead (the default constructor generated by nip2). It then replaces the value of A1 with

[needs finishing]




Class Description


Clock interval value A clock widget, handy for animations
Expression caption expr Displays an editable expression
Group value A group of objects for iteration
List value A list of related objects
Pathname caption value Displays a file browser
Fontname caption value Displays a font browser
Toggle caption value A toggle switch
Scale caption from to value A slider
Option caption labels value Select one item from a list
Colour colour_space value A patch of colour
Matrix_vips value scale offset filename displayA matrix
Arrow image left top width height Two points joined by a line on an image
Region image left top width height A sub-area of an image
Plot options value Displays a plot widget
Image value An image
Number caption value Displays an editable number
Real value Displays a real number
Vector value Displays a list of reals
String caption value Displays an editable string
Mark image left top A point on an image
HGuide image top A horizontal line on an image
VGuide image left A vertical line on an image
Area image left top width height A sub-area of an image, fixed in size



Table 6.5: nip2 built in graphic classes


6.12.3 The Image class

.

say supports mioxed ops with real, vector and complex constants

6.12.4 The Colour class

This class displays a swatch of colour. If you double-click on the

Pathname caption value = class {}