TweetFollow Us on Twitter

Using Controls
Volume Number:1
Issue Number:4
Column Tag:Forth Forum

Using Controls

By Jörg Langowski

In this month’s column I want to introduce to you some concepts of control handling on the Macintosh under MacForth. Controls, as you might have guessed by now, are a very basic concept of the Macintosh user interface: a major part of mouse input to an application is done through controls.

The four main types of controls that show up to you when you work with an application are buttons, check boxes, radio buttons and dials, and these are the ones that we will be talking about this time.

All control handling routines are built into the toolbox ROM. Therefore, one can call them - in principle - from any programming environment as toolbox traps. MacForth, of course, makes this job a little easier by providing a large number of predefined routines. Interestingly enough, most of these routines are already present in Version 1.1, even though the manual says that 1.1 does not support controls. I suppose this was a typo. They wanted to say that it does not document controls. Well, the Inside Macintosh manual does that very extensively and as you will see, it is not hard to transfer those concepts into Forth.

Let’s first look at the definitions of the four control types, as they are given in IM:

Buttons are the little rounded rectangles that have a text inside. When you click a button with the mouse, you tell the application to perform some action as shown in the example below.

“Yes” and “No” are buttons and you may choose either one.

Check boxes show the state of a parameter that can be on or off, and can be clicked on/off with the mouse:

Radio buttons are sets of buttons, only one of which may be on. They are used to set and display parameters that can assume discrete values or states, such as integer numbers:

Dials, the scroll bar being the most familiar example, are used to set and display a parameter that can change continuously (or quasi-continuously, such as the position in a text file).

Generating a control in your active window is very simple, it requires only one toolbox call. Just putting a control into a window like that, however, is useless. You can click and point at it as long as you wish, it won’t do anything unless you tell it what to do. Clicking a control only generates an event that your program has to recognize. For instance, if a radio button is clicked you have to make sure that the corresponding parameter is set on and all others that belong to the same group of radio buttons are turned off. If the page region in a scroll bar is clicked you have to tell the system what to do, change the parameter value and show the scroll bar with its new value.

But then, there is not much more to do. The Control Manager will auto- matically determine if you clicked a control and which one, and will also ‘track’ the control, that is, display it changing while you perform some action on it with the mouse. It will also, if appropriate, inactivate a control, which is then displayed in a different way and any actions on it are ignored (like the scroll bars in the Finder windows if all the icons fit into the window).

Let’s assume we want to generate a window that contains the following controls:

• a scroll bar that can be changed through a range from 0 to 300

• a check box that is used to turn the scroll bar on and off.

An example program that does these functions is given below, and most of it should be self-explanatory. The Toolbox function NewControl that creates a new control in a window, however, has to be explained in a little more detail.

It expects 10 parameters on the stack, from bottom to top:

- 4 bytes for the function result ControlHandle

- WindowPtr, the pointer to the window that the new control belongs to

- boundsRect, the address of a rectangle that gives the control’s size and location in the window’s local coordinates

- title, a pointer to the control’s title

- visible, a boolean value (16 bit); if TRUE, the control is actually drawn inside the window, otherwise, it’s invisible

- value, a 16-bit integer, the control’s initial value

- min, 16-bit integer, the control’s minimum value

- max, 16-bit integer, the control’s maximum value

- procID, 16-bit integer, a number that defines the type of control:

- 0 - simple button, 1 - check box, 2 - radio button, 16 - scroll bar.

- refCon, 32-bit integer, a number that you may use as a unique reference number; it is not used by the Control Manager at all.

This set of parameters is rather impressive, and you can immediately see that none of the standard toolbox trap defining words in MacForth can be used to call this function. First, only function calls with at most one parameter are supported, for the others you have to include space for the result on the stack and use the MT defining word. Second, there are five 16-bit parameters between 32-bit ones, and Forth normally pushes 32-bit items on the stack.

MacForth 2.0 has the word ADD.CONTROL built in, which is called with the stack set up like above, only the integers are all 32-bit. ADD.CONTROL then converts visible, value, min, max, and procID to 16 bits and calls NewControl, which creates the control. However, if you have MacForth 1.1, you can define your own ADD.CONTROL by using the 16-bit push PUSHW that was defined in the last MacTech. The example below contains the definitions of PUSHW and ADD.CONTROL.

The scroll bar that we define is horizontal. The up button and page-up regions of a horizontal bar are on the left side, but the bar is incremented to the right (an inconsistency in the Control Manager). Therefore the program decrements the control’s value when the up button is clicked and increments it on the down button.

The only other control in the example is a check box that turns the scroll bar on and off. I have not included simple buttons or radio buttons; you may want to try them out on your own by setting procID to different values. Remember, if you define a set of radio buttons, your program has to keep track of which one is on and turn the others off. There is no way how the Control Manager could know which buttons belong to the same group.

( Demo program for using the Control Manager from Mac Forth )
( © 1985  MacTutor by J. Langowski  )

( pushw, new.control ) hex
: pushw s0 @ >R sp@ 2- s0 ! sp! r> s0 ! drop ;
A954 mt (new.control)
: add.control
  >r ( refCon )   >r ( procID )  >r ( max )   >r ( min )   >r ( value 
)
  pushw  ( visible ) r> pushw  ( value )
  r> pushw  ( min )  r> pushw ( max ) r> pushw  ( procID )  r> ( refCon 
)
 (new.control)  ;

( test window for new.control )              ( JL Jan 1985 )
decimal  new.window my.window  50 100 200 450 my.window w.bounds
close.box size.box + my.window w.attributes   my.window add.window
50 50 75 300 rect my.scroll
( set up scroll bar )
0 my.window my.scroll 
“  “ ( no title )
 -1  ( visible=true )
100  ( value )
0    ( min )
300  ( max )
16   ( procID for scroll bar )
here ( to get a unique ref # )
add.control 
constant my.control in.heap ( so that handle is released when FORGETting 
)

( on/off box for test window )
25 50 40 200 rect button.a.rect ( check box )
0 my.window button.a.rect “  Scroll Bar Off” -1 0 0 1 1 ( check box ) 

here add.control
constant my.button in.heap   my.window show.controls
80 50 95 150 rect text.rect  ( for output )

( control tracking )
( track control, given a control handle and mouse position, tracks a 
control )
: my.track this.control @ @mouse track.control ;

( get.control, given a control handle, gets the value of that control 
)
: show.value my.control get.control text.rect erase.rect
             0 textfont  50 90 move.to .” Value:” . .”    “  ;

( set.control, given a control handle and an integer, sets the control 
to 
  that integer value )

( scroll bar )
: do.thumb  my.track drop get.window show.controls show.value ;
: inc.scroll this.control @ dup get.control 1+ set.control show.value 
;
: dec.scroll this.control @ dup get.control 1- set.control show.value 
;
: inc10.scroll this.control @ dup get.control 10 + set.control show.value 
;
: dec10.scroll this.control @ dup get.control 10 - set.control show.value 
;

( scroll bar )
: do.scroll.bar  case in.thumb of  do.thumb   endof
           up.button of dec.scroll endof  down.button of inc.scroll endof
           page.up of dec10.scroll endof  page.down of inc10.scroll endof
                 endcase  ;


( check box )
( hilite.control, given control handle and integer value, sets the control
 active if value=0 and inactive if value=255 ) 
: do.button drop my.track if this.control @ dup get.control 1 swap -
                         my.control over 255 * hilite.control set.control 
then
     get.window show.controls ;

( control actions )
: do.controls swap  case my.control of  do.scroll.bar endof
                         my.button  of  do.button endof
                    endcase ;

( event loop for test window )
: my.action show.value if begin do.events
                          case mouse.down of  @mouse.dn get.window
                             find.control ?dup if do.controls then endof
                          endcase again
  else .” My Window Deactivated” then ;

my.window on.activate my.action

 
AAPL
$95.60
Apple Inc.
-2.55
MSFT
$43.16
Microsoft Corpora
-0.42
GOOG
$571.60
Google Inc.
-15.82

MacTech Search:
Community Search:

Software Updates via MacUpdate

OneNote 15.2 - Free digital notebook fro...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that’s too important to forget. Whether you’re at... Read more
iStat Menus 4.22 - Monitor your system r...
iStat Menus lets you monitor your system right from the menubar. Included are 8 menu extras that let you monitor every aspect of your system. Some features: CPU -- Monitor cpu usage. 7 display... Read more
Ember 1.8 - Versatile digital scrapbook....
Ember (formerly LittleSnapper) is your digital scrapbook of things that inspire you: websites, photos, apps or other things. Just drag in images that you want to keep, organize them into relevant... Read more
OmniPlan 2.3.6 - Robust project manageme...
With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more
Command-C 1.1.1 - Clipboard sharing tool...
Command-C is a revolutionary app which makes easy to share your clipboard between iOS and OS X using your local WiFi network, even if the app is not currently opened. Copy anything (text, pictures,... Read more
Knock 1.1.7 - Unlock your Mac by knockin...
Knock is a faster, safer way to sign in. You keep your iPhone with you all the time. Now you can use it as a password. You never have to open the app -- just knock on your phone twice, even when it's... Read more
Mellel 3.3.6 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
LibreOffice 4.3.0.4 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
Freeway Pro 7.0 - Drag-and-drop Web desi...
Freeway Pro lets you build websites with speed and precision... without writing a line of code! With it's user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of... Read more
Drive Genius 3.2.4 - Powerful system uti...
Drive Genius is an OS X utility designed to provide unsurpassed storage management. Featuring an easy-to-use interface, Drive Genius is packed with powerful tools such as a drive optimizer, a... Read more

Latest Forum Discussions

See All

Dawn of the Immortals Review
Dawn of the Immortals Review By Jennifer Allen on July 31st, 2014 Our Rating: :: RESPECTABLE EXPLORATIONUniversal App - Designed for iPhone and iPad Dawn of the Immortals might not re-invent the wheel, but it does tweak it a little... | Read more »
80 Days Review
80 Days Review By Jennifer Allen on July 31st, 2014 Our Rating: :: EPIC ADVENTUREUniversal App - Designed for iPhone and iPad A fantastic and fascinating re-envisioning of the classic novel by Jules Verne, 80 Days is a delightful... | Read more »
Battleheart Legacy Guide
The world of Battleheart Legacy is fun and deep; full of wizards, warriors, and witches. Here are some tips and tactics to help you get the most enjoyment out of this great game. | Read more »
Puzzle Roo Review
Puzzle Roo Review By Jennifer Allen on July 31st, 2014 Our Rating: :: PUZZLE-BASED TWISTUniversal App - Designed for iPhone and iPad A different take on the usual block dropping puzzle game, Puzzle Roo is quite pleasant.   | Read more »
Super Crossfire Re-Release Super Crossfi...
Super Crossfire Re-Release Super Crossfighter Coming Soon, Other Radiangames Titles Go 50% Off Posted by Ellis Spice on July 31st, 2014 [ | Read more »
Hexiled Review
Hexiled Review By Rob Thomas on July 31st, 2014 Our Rating: :: HEX SELLSUniversal App - Designed for iPhone and iPad In space, no one can hear you… spell? Hexiled is a neat concept for a word scramble puzzle, but it doesn’t go too... | Read more »
Summoners War: Sky Arena Passes 10 Milli...
Summoners War: Sky Arena Passes 10 Million Installs! Posted by Jessica Fisher on July 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Deep Loot Review
Deep Loot Review By Jennifer Allen on July 31st, 2014 Our Rating: :: DIVE DEEPUniversal App - Designed for iPhone and iPad Dive deep in this fun explore-em-up that’s a little grind heavy but ultimately quite entertaining.   | Read more »
Despicable Me: Minion Rush is One Year O...
Despicable Me: Minion Rush is One Year Old, Gets its Biggest Update Yet Posted by Jennifer Allen on July 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Fish & Shark Review
Fish & Shark Review By Jordan Minor on July 31st, 2014 Our Rating: :: FLAPPY FISHUniversal App - Designed for iPhone and iPad Fish & Shark’s beauty is only scale deep.   | Read more »

Price Scanner via MacPrices.net

Save up to $130 on an iPad mini with Apple re...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays available for up to $130 off the cost of new models, starting at $339. Apple’s one-year warranty is included... Read more
iPad Cannibalization Threat “Overblown”
Seeking Alpha’s Kevin Greenhalgh observes that while many commentators think Apple’s forthcoming 5.5-inch panel iPhone 6 will cannibalize iPad sales, in his estimation, these concerns are being... Read more
Primate Labs Releases July 2014 MacBook Pro P...
Primate Labs’ John Poole has posted Geekbench 3 results for most of the new MacBook Pro models that Apple released on Tuesday. Poole observes that overall performance improvements for the new MacBook... Read more
Apple Re-Releases Bugfixed MacBook Air EFI Fi...
Apple has posted a bugfixed version EFI Firmware Update 2.9 a for MacBook Air (Mid 2011) models. The update addresses an issue where systems may take longer to wake from sleep than expected, and... Read more
Save $50 on the 2.5GHz Mac mini, plus free sh...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $140 on an iPad Air with Apple ref...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
$250 price drop on leftover 15-inch Retina Ma...
B&H Photo has dropped prices on 2013 15″ Retina MacBook Pros by $250 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2249, $250 off... Read more
More iPad Upgrade Musings – The ‘Book Mystiqu...
Much discussed recently, what with Apple reporting iPad sales shrinkage over two consecutive quarters, is that it had apparently been widely assumed that tablet users would follow a two-year hardware... Read more
13-inch 2.5GHz MacBook Pro on sale for $999,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $999.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Save up to $300 on an iMac with Apple refurbi...
The Apple Store has Apple Certified Refurbished iMacs available for up to $300 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. These are the best prices on... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.