Article
Difficulty Rating
4
Groups and Backgrounds

.

.

 

An introduction to Groups

A group is a type of object that contains other controls. It can contain any controls - buttons, fields, graphics, scrollbars, and eps objects, in any combination and number. As a result, groups are powerful and flexible objects. Here are some examples of what you can do with them:

  • Hide and show multiple objects with a single command
  • Move multiple objects with a single command
  • Creating "panes" in a window to allow scrolling sets of objects
  • Display the same object or set of objects on multiple cards (it is possible to group a single object)
  • Create a common script that can be used by multiple objects
  • Automatically track hiliting in a set of radio buttons
  • Create tabbed menus
  • Place menus into the Mac system menubar

When you group objects, they get placed in another object, which comes complete with selection handles, the ability to show a border, an option to show a scrollbar etc. Just like other objects you can assign groups scripts and custom properties, send messages to them, move them etc. To edit the properties of a group, double click it with the pointer tool like any other object.

However, groups are really a unique cross between a card and an object. Like cards, they are attached to the stack. Unlike cards, they can be smaller than the window frame, can be moved around as objects on cards, can scroll and do most of the things an object can do. Like a card, their scripts get inserted into the mesage hierachy, just above all the objects inside them, but before the card.

Groups and Backgrounds, why and whats the difference?

Once created, a group can be displayed on any card in the current stack. Try it now: create a new stack with some objects and group them (select them and choose "Group" from the Edit menu), then choose "Backgrounds..." from the "Edit" menu. Select the group you have created and press "Remove". The group will dissapear from the card (move the dialog out of the way if you can't see the card). You will note that the group has appeared in the lower half of the box. Select it, and press "Place". It will reappear on the current card. Now, create a few more cards and go through each one. You will be able to place and remove the group on any card in that stack using the Backgrounds dialog. Note you can only place a group once on any one card. That is why the group dissapears from the bottom half of the dialog when you place it: the group is still available for placement on other cards, but can't be placed again on the current card.

As you will already have noted, in MetaCard there are two names for groups: groups and backgrounds. Both are used in the Metatalk language. Confusing? The two terms are useful. Group refers to group objects counted relative to a card, background refers to group objects relative to the stack.

Groups are displayed as objects on the card. Asking for the number of groups will give you the total number of groups on the current card.

Backgrounds refers to the total number of groups in the stack. Asking for the number of backgrounds will return the total number of unique groups in the stack, regardless of whether or not they are on the current (or any other) card.

Here are two examples. Firstly, the backgroundNames property returns a list of all the groups anywhere in the stack, whereas the groupNames returns a list of all the groups that are on a particular card. Secondly, referring to backgrounds and groups by number works differently. Referring to backgrounds by number works in order of creation of any group anywhere in the stack, but referring to groups by number works by using the layer of the group on a card. Thus background 1 is the first group created anywhere in the stack, whereas group 1 is the group with the lowest layer on the card.

You need the two terms when using scripts to place groups on a card. To determine which group to place, you need to be able to count it relative to all the other groups in the stack.

Manipulating Groups

When creating groups, it is important to remember that the layer of controls in each group is determined by their order of selection before they are grouped, so if you want to make it possible for the user to tab between controls in a group simply select them in the appropriate order. Of course, you can change this order at any point by editing the layer of the individual objects within the group. Remember that the layer of grouped objects only refers to their layer within the group, not in relation to other objects in the card. However, the number of a grouped object refers to its number relative to all the objects on the card.

TIP: The function the selectedobject returns the name of the currently selected object. If multiple objects are selected that are not grouped it returns the names of all the selected objects with one on each line, in order of selection.

To edit a group, open its properties palette, click the Properties tab, and press Edit. You will go into edit background mode, and only the contents of the group will be displayed - all other objects will dissapear. To stop editing a group, choose "Stop editing BG" from the edit menu.

To do this by script:

start editing group 1
start editing background "example"
stop editing this bg
                                    

To create a group and objects in a group by script:

create group "My Group" -- creates a new group called "My Group"
create button "My Button" in group "My Group" -- creates a button
create fld "My Field" in group "My Group"
                                    

If you have a nested group (a group within a group) , it will not appear in the Edit Background dialogue. Edit the first level group and then select and edit the nested group.

You can also add or remove groups to cards by script:

place group "My Group" onto card 1 
place background "My Group" onto card 2
remove background "My Group" from card 1
                                    

Remember that a group is one object. So be wary about deleting groups by selecting and pressing the delete key because this permanently deletes them from every card in the stack.

TIP: Setting the selectGroupedControls global property to true

set the selectGroupedControls to true
                                             

allows you select individual controls within a group without having to ungroup or go into Edit Background mode. (This requires MetaCard 2.2.1B1 or above.)

Individual controls within a group can be deleted by script, just as if they were separate objects.

One particularly useful function of groups is to automatically make radio buttons hilite correctly. If you create some radio buttons and group them, they will automatically enforce radio button behavior without any scripts. It is possible to navigate between radio buttons in the same group using the arrow keys on Windows and UNIX. To do this set the tabGroupBehavior of the group to true. This will also makes the tab key skip over the whole group.

Tabbed menus

Groups are important for creating tab menus and menu bars. To create a tabbed menu create a button and using the Button Properties dialogue, set the style to tabbed. Set the menu contents of the button to the name of the different tabs you require, and create groups named the same as each tab. It is easy to hide and show the groups corresponding to each tab. Place the following script in the tabbed button:

on menuPick newTab, oldTab 
  hide group oldTab 
  show group newTab
end menuPick
                                    

In the above example, the parameters passed with the menuPick message (containing the name of the tab clicked on and the name of the tab that was previously in front) are used to hide and show groups with the same name as the tabs.

Displaying text on multiple cards

When placing a group on multiple cards, you will often find you want to fields such as labels to display the same text on each card in the stack, while others fields (usually for user entry) should be editable and display different text on each card. Fields which display the same text on each card have their sharedText property set to true, and those that display different text have their sharedText property set to false.

Similarly, if you want to display the same state of hiliting in buttons and checkboxes on each card, set their sharedHilite property to true. (You will find that if you create labels using the Show as Label button in the Button Properties dialogue their sharedText property is set to true, but the default setting for fields is false.)

You can sort cards by the contents of their fields using the sort command. Sorting can be alphabetical or numeric, and ascending or descending. The sort command can be complimented with the identically named mark command and mark property. For example if you want only to sort certain cards, they can be marked and you can specify that only the marked cards are sorted.

Did you find this article useful? Have any ideas for future topics? Email Us!