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

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.