HiMenus, Color Notes
Volume Number: | | 4
|
Issue Number: | | 6
|
Column Tag: | | Basic School
|
Hierarchical Menus & Color Notes
By Dave Kelly, MacTutor Editorial Board
Hierarchical Menus in ZBasic
Hierarchical menus (also known as submenus) are possible in ZBasic with a small amount of sweat. Hierarchical menu items have the small filled black triangle pointing to the right in many of the newer application programs that are coming out now. This month I will show you how you can implement them in your own ZBasic source code.
There are a few standard guidelines for using submenus which you can find in The Macintosh User Interface Guidelines found in Vol. 5 of Inside Macintosh. In the pull-down menu structure of the Macintosh, it was originally intended that the user could quickly scan through all of the menus and know the function of all the menu items. Therefore, it is strongly recommended that submenus only be used for lists of related items. You should try to only use one level of hierarchical menus although it is possible to go several levels.
The concepts of submenu programming is fairly simple and easy to implement. However, implementing submenus in ZBasic without some understanding of how things work will probably frustrate some of the average Basic programmers out there. The first thing we have to do is throw away the ZBasic event loop and event handling statements and set up our own GETNEXTEVENT loop. This sounds difficult to some of you, but actually it solves some of the problems inherent in ZBasic that Ive been pulling my hair out over for the past year or so. In this months sample program I even went a step further by defining some of the menus and the main window using MENU and WIND resources. You can refer back to my GETNEXTEVENT ZBasic shell program in September 1987 MacTutor. This will give you the program shell to start with.
First create the window resource. Any window type will do for this example. I chose the rDocProc type window which has a ProcID number 16. To create the WIND resource, open ResEdit and select New from the File menu and type in the WIND resource type. Then select New to create a resource and type the following parameters into your window (as shown in the ResEdit Window in figure 2):
boundsRect5478 280 325
procID 16
visible True
goAway False
refCon 0
title Hierarchical Menu Sample
Change the WIND ID to 29414 (or any other unique number) by selecting Get Info from the File menu. Now the WIND resource is created. This window will be used by the program to output confirmation that everything works.
Fig. 1 Our hierarchical menu example in ZBasic
The menus are just as simple to create with ResEdit as the WIND resource was. The parameters for each of the menus is shown in the window dumped from ResEdit which are included here. Two menus are created with ZBasic Menu statements which are not included as resources. Actually, it would probably be better to use all toolbox calls instead of ZBasic Menu statements because we could be in complete control of all the menus. MENU id 255 is the Apple menu created with the APPLE MENU statement in ZBasic, but it is very automatic and you have to have desk accessories whether you like it or not. If you created your own, you could exclude any part you didnt want, but you would have to control every little thing that happens. MENU ID 2 is created with the ZBasic EDIT MENU statement. This is the easiest way to make an Edit menu. MENU ID 134 is the File menu. It contains only one menu item, Quit.
MENU ID 135 is the Format menu which is the main menu which the submenus will be attached. The Format menu contains two items, Style and Font which will each have submenus. Now comes the fun part. To attach a menu we first set the key equiv for the menu item to $1B. This will put the black filled triangle in the menu. A $1B is ASCII character 27, the ESCAPE key. If you have an Extended keyboard you will have no trouble producing this character by typing esc. Typing CTRL-[ will also produce the ESC character. However, I havent found a good way to produce ESC using the MacPlus or earlier keyboards; anybody know how??. Next the ID of the submenu is put in the mark Char field. Actually, you enter the character which has the ASCII value of the ID of the submenu. In the Style menu the ASCII value of à is 136. This character is entered by using the option-` then a. The Font menu has submenu with ID=137 so the mark Char is â which is entered by typing option-i and the a. You should examine the ResEdit windows shown here if you need more help understanding this.
Fig. 2 ResEdit version of the Window Resource
Fig. 3 Menu Resource
After the MENU and WIND resources are complete, the program can be compiled. You will need to compile the program as an application and install the MENU and WIND resources before running the program or unexpected results will occur. Remember that the program needs to have the resources in order to work properly. Be sure that the resources are installed by running ResEdit immediately after compiling. It would be nice if ZBasic would install a resource file for you as in real development languages. Maybe they will add this capability someday.
The program really doesnt do anything except respond to the menu and print to the window which menu was selected. There are a few left over functions that were originally part of the GETNEXTEVENT shell program. The shell could be expanded to work with other applications you may like to do.
An alternative to using ResEdit is to create your resources with RMaker, as shown in the program listing. Once the resources are compiled, ResEdit can be used to move the resources from the RMaker output file into the ZBasic compiled file.
Sorry State of Mac Basics
Thus far, the Mac II has been out over a year and as of the date of this writing, there is still no Basic released which fully supports all the new ROM capabilities. Even the method Ive shown here is a work around because of the way that ResEdit has to be used. It still holds true, however, that ZBasic has the most features and is faster than any of the competitors. Microsoft still has no new updates of MS Basic. And with all the bugs in the compiler, Absoft doesnt want to add any new features till the old ones are fixed. Somebody is walking around with blinders on and doesnt even know it. From the response Ive seen, most people are dissatisfied with all the Basic products that are currently released. There are still some possibilities coming up True Basic 2.01 was released this week. We will be taking a good hard look at the new True Basic in upcoming issues. The grapevine says that Microsoft is readying a new release for sometime? All I can say is that I hope they are listening to all of us and what we are screaming for. [Note that True Basic 2.0 was found to be defective shortly after shipment, and the company has upgraded immediately to 2.01. It seems that a memory management bug exists in 2.0 that is actually hidden by the presence of the Macsbug debugger, which nearly all their beta testors were using. When they tried the product without Macsbug installed, a problem was found that seems to prevent True Basic from picking up on mouse and keyboard events properly during program execution. We discovered it when we noticed the demos would not stop without powering down the Mac. So be sure to check that your upgrade disk is version 2.01, and a word of warning to other developers, that the presence of a debugger may be altering or hiding potential memory manager bugs. -Ed]
By the way, my daughter helped to discover a undocumented feature of ZBasic. She was quietly invading my Mac II (when I wasnt looking) while I had ZBasic running and happened to press the arrow keys. Well it turns out that the down arrow key lists the next sequential program line. The up arrow key prints the previous line. Right arrow lists the last line and Left arrow lists the first line. Interesting, but I prefer to do all my editing using an editor. Id like to say I like using the built in editor the best, but it still doesnt quite work right.
True Basic 2.01 & Color Models
One of the neat features of the new True Basic 2.01 is that it supports Macintosh II color including palette animation. In fact, the palette animation article we did last month in MacTutor can be done in just a few lines in True Basic! And a neat Mac touch they have added is that the output window for True Basic is saved and refreshed and can be written to disk as a PICT2 resource, unlike MS Basic, which forgets how to update the output window after a program runs. To help us get up to speed on color processing on the Mac, here are some notes on color types.
Along with color and the Mac II there also comes new terminology. Im sure there are many of you that either havent wanted to or have had no opportunity till now to know about the various color models that exist. Most of us have heard of RGB, some for the first time when they bought their Mac II with RGB monitor. RGB is yet only one of several color models being used in the computer graphics industry. Some other color models include: CMY, YIQ, LAB, HSL, HSV, and HSI Color Models.
I will attempt to explain them from the few things that I have read on the subject. Each model has its advantages and disadvantages. In general the color models have been formed to assist the user in selecting one of the 16 million colors.
RGB
RGB is directly related to the hardware involved in color displays. Most of us are familiar with this from using the Color Picker package to select colors on the Mac II. The Red, Green, and Blue values are represented as long integers, which are converted into signals driving each of the color guns in the CRT (one for each color). The RGB model is easier for the programmer to implement because it is directly related to the hardware. But it may not be the easiest for the user to use unless he/she understands the RGB model. The other color models are always converted to RGB then the hardware uses a Color LookUp Table (CLUT) to determine which color to display. Also notable about the RGB model, when the values of RGB are equal ( R = G = B) the CRT phosphor chromaticities are balanced so that gray is the result. The brightness or darkness of grey is determined by the amplitude of the RGB components as they effect the color guns of the CRT.
HSV
HSV stands for Hue, Saturation and Value. The Hue associates the color with a value in the color spectrum, Saturation represents the vividness of the color with pure colors (such as red, green, and yellow) being called fully saturated colors and grays being called desaturated colors, the Value represents the lightness or darkness of a color. The Brightness field of the Macintosh Color Picker dialog is the equivalent of the HSV value. The Color Picker primarily works using the HSV model. The RGB values are shown also, but only users that understand both color systems will understand how to the HSV and RGB values relate. The RGB system is given so that the user will have an alternate system to choose from just in case the user is familiar with another system other than HSV.
YIQ
The YIQ model has been in use as a television broadcast standard since it was adopted in 1953 by the National Television Standards Committee (NTSC). The purpose of the YIQ model was to provide a means to encode color information in such a way as to be compatible with black and white transmission. Three components of the YIQ model are: Y, an approximation of luminance; I, chrominance information along a blue-green to orange vector; and Q, chrominance information along a yellow-green to magenta vector. A conversion formula was defined by the Commission Internationale de LEclairage (CIE):
LAB
The CIELAB model is an international standard for perceptual uniformity. This one is difficult to explain, and probably just as difficult to understand. This model relates the color differences that a human perceives as being equal Euclidean distances in CIELAB space. This model only approaches its objective because of the varying perception of color that humans have. It is said that CIELAB is better for making fine adjustments in the color selection process.
HSI
HSI processing can be less complicated than other models. Hue values can provide the basic for quantizing and interpreting the color of objects. Tinting, retouch, and special effects can be produced by modifying hue values, or saturation values. Using HSI, all methods for processing and analyzing monochrome images can be applied directly to color images.
HSL, HSV
I dont know the difference between HSI, HSL, HSV models but they have been referred to as separate models in some of the things Ive read. HSL is mentioned in Inside Macintosh, Vol 5, but not explained. Ill leave the explanation for a future column. I would appreciate any comments in the form of letters that would explain this.
Conversions are available in the Macintosh Color Picker package described in Inside Macintosh, Vol. 5 for some of the color models. They are:
PROCEDURE CMY2RGB (cColor: CMYColor; VAR rColor: RGBColor);
PROCEDURE RGB2CMY (rColor: RGBColor; VAR cColor: CMYColor);
PROCEDURE HSL2RGB (hColor: HSLColor; VAR rColor: RGBColor);
PROCEDURE RGB2HSL (rColor: RGBColor; VAR hColor: HSLColor);
PROCEDURE CMY2RGB (hColor: HSLColor; VAR rColor: RGBColor);
PROCEDURE CMY2RGB (rColor: RGBColor; VAR hColor: HSLColor);
See Inside Macintosh for more information on the conversions. It is sufficient here to know that the conversions exist.
Other information on this subject can be found in the following sources (I have not seen all of these sources myself):
1. APPLE COMPUTER, INC., Inside Macintosh, Vol 5.
2. SCHWARZ, MICHAEL W., COWAN, WILLIAM B., and BEATTY, JOHN C. An Experimental Comparison of RGB, YIQ, LAB, HSV, and Opponent Color Models, ACM Transactions on Graphics, Vol 6, No.2, April 1987, Pages 123-158.
3. BECKER, R. A. and CHAMBERS, J. M. S-An Interactive Environment for Data Analysis and Graphics. Wadsworth, 1984.
5. BERK, T., BROWNSTON, L., and KAUFMAN, A. A new color-naming system for graphics languages. IEEE Comput. Graphil. Appl. 2,3 (May 1982), 37-44
6. BERK, T., BROWNSTON, L., and KAUFMAN, A. A human factors study of color notation systems for computer graphics. Commun. ACM 25,8 (Aug. 1982), 547-550)
7. BOYNTON, R. M. Human Color Vision. Holt, Rinehart and Winston, New York, 1979.
8. CIE. Recommendations on Uniform Color Spaces, Color-Difference Equations, Psychrometric Color Terms. Bureau Central de la CIE (Supplement 2 of CIE Publication 15 (E-1.3.1) 1971), 1978.
9. FOLEY, J. D., and VAN DAM, A. Fundamentals of Interactive Computer Graphics. Addison-Wesley, Reading, Mass. 1982.
10. HURVICH, L. M. Color Vision. Sinaur, Sunderland, Mass., 1981.
11. WYSZECKI, G., and STILES, W.S. Color Science: Concepts and Methods, quantitative Data and Formulae, 2nd ed. Wiley, New York, 1982.
12. JOBLOVE, G. H., and GREENBERG, D. P. Color spaces for computer graphics. Comput. Graph. 12,3 (Aug. 1978), 20-25.
13. MUNSELL, A. H. A Color Notation. Munsell Color Company, 1939.
REM Hierarchical Menu Demo
REM ©MacTutor 1988
REM By Dave Kelly
WINDOW OFF
COORDINATE WINDOW
DEF MOUSE=-1
everyevent=-1:REM ALL events mask
False=0:True=NOT False
DIM dragRect%(3)
Find out monitor size just in case we need it
CALL GETWMGRPORT(WMgrPort&)
PortTop=PEEK WORD(WMgrPort&+8)
PortLeft=PEEK WORD(WMgrPort&+10)
PortBottom=PEEK WORD(WMgrPort&+12)
PortRight=PEEK WORD(WMgrPort&+14)
CALL SETRECT(dragRect%(0),PortLeft+10,PortTop+10,PortRight-
10,PortBottom-10)
REM Setup the EventRecord
myEvent%=0:REM what is first variable
message&=0
when&=0
where=0:REM y coordinate of point is first
wherex=0
modifiers=0
applemark=&H14
whichwindow&=0
charCodeMask&=VAL(&H000000FF)
keyCodeMask&= VAL(&H0000FF00)
myWindow&=FN GETNEWWINDOW(29414,0,-1)
CALL SETPORT(myWindow&)
TEXT 2,12,0,0
CLS
GOSUB SetUpMenus
FLUSHEVENTS
EventLoop: REM Main Event Loop
DO
CALL SYSTEMTASK
LONG IF FN GETNEXTEVENT(everyevent,myEvent%)
SELECT myEvent%
CASE 0 :REM No Event
CASE 1 :REM mousedown
wResult=FN FINDWINDOW(where,whichwindow&)
SELECT wResult
CASE 0 :REM inDesk (do nothing)
CASE 1 :REM inMenuBar
mResult&=FN MENUSELECT(where)
theMenu=FN HIWORD(mResult&)
theItem=FN LOWORD(mResult&)
SELECT theMenu
CASE 255
GOSUB appleID
CASE 134
GOSUB fileID
CASE 2
GOSUB editID
CASE 136
GOSUB StyleID
CASE 137
GOSUB FontID
END SELECT
CALL HILITEMENU(0)
CASE 2 :REM inSysWindow
CALL SYSTEMCLICK(myEvent%,whichwindow&)
CASE 3 :REM inContent
CASE 4 :REM inDrag
CALL DRAGWINDOW(myWindow&, where, dragRect%(0))
CASE 5 :REM inGrow
CASE 6 :REM inGoAway
Byte=FN TRACKGOAWAY(myWindow&,where)
IF Byte THEN PRINT CLOSEWINDOW(myWindow&)
END SELECT
CASE 2 :REM mouseup
CASE 3 :REM keydown
PRINTkey-down
charCode&=FN BITAND(message&,charCodeMask&)
keyCode&=FN BITAND(message&,keyCodeMask&)
keyCode&=FN BITSHIFT(keyCode&,-8)
PRINT Charcode=;charCode&
PRINT Keycode=;keyCode&
CASE 4 :REM keyup
CASE 5 :REM autokey
CASE 6 :REM updateEvt
CASE 7 :REM diskEvt
CASE 8 :REM activateEvt
CASE 10:REM networkEvt
CASE 11:REM driverEvt
CASE 12:REM app1Evt
CASE 13:REM app2Evt
CASE 14:REM app3Evt
CASE 15:REM app4Evt
CASE ELSE
END SELECT
END IF
UNTIL theEnd
SetUpMenus
APPLE MENU About Sample
AboutHandle&=FN GETMHANDLE(255)
FileHandle&=FN GETMENU(134)
CALL INSERTMENU(FileHandle&,0)
EDIT MENU 2
EditHandle&=FN GETMHANDLE(130)
FormatHandle&=FN GETMENU(135):Get Format Menu Handle
StyleHandle&=FN GETMENU(136):Get Style Menu Handle
FontHandle&=FN GETMENU(137):Get Font Menu Handle
CALL INSERTMENU(FormatHandle&,0)
CALL INSERTMENU(StyleHandle&,-1):Style Menu
CALL INSERTMENU(FontHandle&,-1):Font Menu
CALL ADDRESMENU(FontHandle&,CVI(FONT))
Check default font in Font menu
fontcnt%=FN COUNTMITEMS(FontHandle&)
defaultfont$=Geneva
FOR i=1 TO fontcnt%
CALL GETITEM(FontHandle&,i,item$)
IF item$=defaultfont$ THEN Oldmitem=i:i=fontcnt%+1
NEXT i
CALL CHECKITEM(FontHandle&,Oldmitem,True)
TextStyle=0
Plain=0
Bold=1
Italic=2
Underline=4
Outline=8
Shadow=16
Condense=32
Extend=64
just=0
CALL DRAWMENUBAR
RETURN
appleID
CALL GETITEM(AboutHandle&,theItem,Var$)
PRINT Var$
mrefNum=FN OPENDESKACC(Var$)
CALL SETPORT(myWindow&)
RETURN
fileID
END
RETURN
editID
IF NOT FN SYSTEMEDIT(theItem-1) THEN RETURN
SELECT theItem
CASE 1:REM undo command
CASE 3:REM cut command
CASE 4:REM copy command
CASE 5:REM paste command
CASE 6:REM clear command
END SELECT
RETURN
StyleID
CALL GETITEM(StyleHandle&,theItem,Var$)
CLS: PRINT Var$; selected
RETURN
FontID
CALL GETITEM(FontHandle&,theItem,Var$)
CLS: PRINT Var$; selected
RETURN
* ZBasic.R
*
Basic.RSRC
???????
Type ZBDK = STR
,0
© Demo by Dave Kelly \0Dver 1 JUN 1988
Type FREF
,128
APPL 0
Type BNDL
,128
ZBDK 0
ICN#
0 128
FREF
0 128
* ------ Multifinder events --------
* bit 15 = switcher save screen
* bit 14 = accept suspend resume events
* bit 13 = switcher enable option switch
* bit 12 = can do background on null events
* bit 11 = multifinder aware
* (activates & deactivates topmost
* window at resume, suspend events)
Type SIZE = GNRL
,-1
.H
4000 ;; $4000 = bits 14 set
.L
128000 ;; (for 150K recomended)
.L
80000 ;; (for 80K minimum)
.I
Type ALRT
,1 (0)
80 80 205 400
1
7765
Type ALRT
,2 (0)
80 55 205 425
2
7765
Type ALRT
,3 (0)
80 55 205 425
3
7765
Type ALRT
,4 (0)
80 55 205 425
4
7765
Type ALRT
,5 (0)
30 250 82 502
5
7765
Type ALRT
,129 (0)
80 80 200 400
129
5555
Type ALRT
,130 (0)
80 80 200 400
130
7765
Type ALRT
,150 (0)
80 55 200 425
150
7765
Type ALRT
,152 (0)
80 80 200 400
152
7765
Type DITL
,1 (0)
5
Button
15 245 40 305
Ok
staticText Disabled
55 10 70 290
^0
staticText Disabled
72 10 87 290
^1
staticText Disabled
89 10 104 290
^2
staticText Disabled
106 10 121 290
^3
Type DITL
,2 (0)
6
Button
15 295 40 360
Ok
Button
55 295 80 360
Cancel
staticText Disabled
55 10 70 290
^0
staticText Disabled
72 10 87 290
^1
staticText Disabled
89 10 104 290
^2
staticText Disabled
106 10 121 290
^3
Type DITL
,3 (0)
6
Button
15 295 40 360
Yes
Button
55 295 80 360
No
staticText Disabled
55 10 70 290
^0
staticText Disabled
72 10 87 290
^1
staticText Disabled
89 10 104 290
^2
staticText Disabled
106 10 121 290
^3
Type DITL
,4 (0)
6
Button
55 295 80 360
Cancel
Button
15 295 40 360
Ok
staticText Disabled
55 10 70 290
^0
staticText Disabled
72 10 87 290
^1
staticText Disabled
89 10 104 290
^2
staticText Disabled
106 10 121 290
^3
Type DITL
,5 (0)
4
Button
6 210 45 246
Ok
staticText Disabled
10 65 25 205
^0
staticText Disabled
26 65 41 130
At Line #
staticText Disabled
26 130 41 205
^1
Type DITL
,129 (0)
3
Button
15 245 40 305
OK
staticText Disabled
48 10 64 300
^0
staticText Disabled
66 10 82 320
^1
Type DITL
,130 (0)
2
Button
15 245 40 305
OK
staticText Disabled
48 10 64 300
Not Enough Memory!
Type DITL
,150 (0)
5
Button
55 295 80 360
Stop
Button
15 295 40 360
Continue
staticText Disabled
48 10 62 290
^0
staticText Disabled
66 10 80 290
^1
staticText Disabled
84 10 98 290
^2
Type DITL
,152 (0)
3
Button
15 245 40 305
Ok
staticText Disabled
48 10 62 300
The Printer Driver Could not be Opened.
staticText Disabled
66 10 80 300
You will not be able to print anything.
* Menus
* ,ID (attributes)
* menu title (an Apple symbol is \ 14 in hex)
* menu items, ( means its initially disabled.
* (- means a disabled line of dashes.
* A trailing /Q, etc. means a command-key.
Type MENU
,135 (0)
Format
Style /\1B!\88
Font /\1B!\89
Type MENU
,134 (0)
File
Quit
Type MENU
,136 (0)
Styles
Bold/B
Italic /I
Underline /U
Outline /O
Shadow /S
Type MENU
,137 (0)
Fonts
* A Window template
* ,ID (attributes)
* title
* boundsrect (global Top, Left, Bottom, Right)
* (In)Visible (No)GoAway
* procid: window definition ID
* refcon, for programs use.
Type WIND
,29414 (0)
Hierarchical Menu Sample
54 78 280 325
Visible NoGoAway
16
0
Type ICN# = GNRL
,128 (0)
.H
0001 0000 0002 8000 0004 4000 0009 A000
0013 1000 0026 0800 004C 0400 0098 0200
0133 0100 0267 8080 04CE 4040 09A9 C020
1310 8010 26B8 0008 4C64 3F04 8822 4082
4015 8041 200B 3022 1005 C814 081E 7F8F
0402 3005 0201 0007 0100 8005 0080 6007
0040 1FE5 0020 021F 0010 0407 0008 0800
0004 1000 0002 2000 0001 4000 0000 8000
*
0001 0000 0003 8000 0007 C000 000F E000
001F F000 003F F800 007F FC00 00FF FE00
01FF FF00 03FF FF80 07FF FFC0 0FFF FFE0
1FFF FFF0 3FFF FFF8 7FFF FFFC FFFF FFFE
7FFF FFFF 3FFF FFFE 1FFF FFFC 0FFF FFFF
07FF FFFF 03FF FFFF 01FF FFFF 00FF FFFF
007F FFFF 003F FE1F 001F FC07 000F F800
0007 F000 0003 E000 0001 C000 0000 8000