Thanks, Bernie,
It turns out that Alain's code works perfectly for me, and by the time
I got done with it, it wasn't even all that messy. I didn't bother with
turning the click into a new fb event, so my btn handler took this form:
local fn myMenuBtnClick
Dim @ menuSel As SInt16
dim @ menuVal as SInt16
Dim @ ignoreSize As Long
' Check to see whether the user chose from the Parent menu,
' or from one of the submenus.
Def GetButtonData( _cMyBtn, _kControlMenuPart, ¬
_kControlBevelButtonLastMenuTag, ¬
Sizeof(SInt16), ¬
@menuSel, ignoreSize )
Def GetButtonData( _cMyBtn, _kControlMenuPart, ¬
_kControlBevelButtonMenuValueTag, ¬
Sizeof(SInt16), ¬
@menuVal, ignoreSize )
select menuSel' Which menu?
case _mParentMenu
select menuVal' Which item?
case _iPar1
' Items 2 & 3 have subMenus, so won't show up here
case _iPar4
case _iPar5
end select
case _mChildMenu1 ' iPar2 menu
select menuVal' Which item?
case _iChld11
case _iChld12
case _iChld13
end select
case _mChildMenu2 ' iPar3 menu
select menuVal' Which item?
case _iChld21
case _iChld22
case _iChld23
end select
end select
end fn
Your code would be appropriate (even preferable) if the project were
based on Carbon events, but mine is not, so I think this gives an
excellent (and easy) alternative.
e-e
=J= a y
"
On Sunday, April 24, 2005, at 03:05 AM, Bernie wrote:
> Jay wrote:
>
>> That looks like it may work. I, of course, was hoping for something
>> less messy.
>
> Nice workaround, Alain :)
>
> It was only yesterday when I started to look at carbon menu events so
> it's early days for me. However, I think the recommended way is to
> assign command IDs to menu items. It's no less messy than Alain's
> method, but that's the nature of CE I suppose.
>
>
> Here's a carbon menu handler added to some old code:
>
> '----------
> /*
> Notes to the carbon experts:
> -- Please feel free to correct this code (I'm still learning)
> -- Is it possible to detect menu events for items that have not been
> assigned a command ID?
> */
>
> '~'A
> ' Runtime : Rntm Appearance.Incl
> ' CPU : Carbon
> ' CALL Req'd : Off
> '~'B
>
> include "Tlbx CarbonEvents.Incl"
>
> _iPar1Cmd = _"Par1"
> _iPar2Cmd = _"Par2"
> _iChild1Cmd = _"Beep"
> _iChild2Cmd = _"BpBp"
> _iPar4Cmd = _"Par4"
> _iPar5Cmd = _"Par5"
>
> _Untitled1Wnd = 1
> _cPop = 1
>
> _mParent = 101
> begin enum 1
> _iParent1
> _iParent2
> _iParent3
> _iParent4
> _iParent5
> end enum
>
> _mChild = 102
> begin enum 1
> _iChild1
> _iChild2
> end enum
>
> local mode
> local fn AddEventToHandle(eventClass as UInt32, eventKind as UInt32, h
> as Handle)
> dim as EventTypeSpec evnt
> '~'<
> evnt.eventClass = eventClass
> evnt.eventKind = eventKind
> end fn = fn PtrAndHand(evnt, h, SizeOf(EventTypeSpec))
>
> local fn InstallMenuEvents(m as MenuRef)
> dim as Handle eventH
> dim as OSStatus ignore
> begin globals
> dim as Ptr sMenuEventHandlerUPP
> end globals
> long if (sMenuEventHandlerUPP == 0)
> sMenuEventHandlerUPP = fn NewEventHandlerUPP([proc "MenuEventHandler"
> + _FBprocToProcPtrOffset])
> end if
> '~'1
> eventH = fn NewHandle(0)
> long if eventH
> '~'<
> fn AddEventToHandle(_kEventClassCommand, _kEventCommandProcess, eventH)
> '~'<
> ignore = fn InstallEventHandler(fn GetMenuEventTarget(m),
> sMenuEventHandlerUPP, ¬
> fn
> GetHandleSize(eventH)\\(SizeOf(EventTypeSpec)), #[eventH], #0, #0)
> DisposeHandle(eventH)
> end if
> end fn
>
> long if 0
> "MenuEventHandler"
> enterproc fn MenuEventHandler(handler as EventHandlerCallRef, evnt as
> EventRef, userData as Ptr) = OSStatus
> dim as UInt32 eventClass, eventKind
> dim as WindowRef w
> dim as HICommand command
> dim as long result
> dim as OSStatus ignore
> '~'1
> result = _eventNotHandledErr
> eventClass = fn GetEventClass(evnt)
> eventKind = fn GetEventKind(evnt)
> ignore = fn GetEventParameter(evnt, _kEventParamDirectObject,
> _TypeHICommand, #0, SizeOf(HiCommand), #0, @command)
> '~'<
> select eventClass
> case _kEventClassCommand
> select eventKind
> case _kEventCommandProcess
> select command.commandID
> case _iPar1Cmd
> case _iPar2Cmd
> case _iChild1Cmd : beep
> case _iChild2Cmd : beep : delay 100 : beep
> case _iPar4Cmd
> case _iPar5Cmd
> end select
> end select
> end select
> exitproc = result
> end if
>
>
> local fn MakeHierMenu(parentMenuID, parentMenuItem, itemString as
> str255, childMenuID)
> dim as Handle menuH
> dim as OSErr err
> '~'<
> menu parentMenuID, parentMenuItem, _enable, itemString
> err = _zTrue
> menuH = fn GetMenuHandle(parentMenuID)
> long if menuH
> err = fn SetMenuItemHierarchicalID(menuH, parentMenuItem, childMenuID)
> InsertMenu(fn NewMenu(childMenuID, ""), -1)
> end if
> end fn = err
>
> local fn BuildPopMenu
> dim as Handle parH, childH
> dim as OSStatus ignore
> '~'<
> menu _mParent, 0, _enable, ""
> menu _mParent, _iParent1, _enable, "Item 1"
> menu _mParent, _iParent2, _enable, "Item 2"
> '~'<
> fn MakeHierMenu(_mParent, _iParent3, "Item 3", _mChild)
> menu _mChild, _iChild1, _enable, "Child Item 1"
> menu _mChild, _iChild2, _enable, "Child Item 2"
> '~'<
> menu _mParent, _iParent4, _enable, "Item 4"
> menu _mParent, _iParent5, _enable, "Item 5"
> '~'<
> parH = fn GetMenuHandle(_mParent)
> ignore = fn SetMenuItemCommandID(parH, 1, _iPar1Cmd)
> ignore = fn SetMenuItemCommandID(parH, 2, _iPar2Cmd)
> ignore = fn SetMenuItemCommandID(parH, 4, _iPar4Cmd)
> ignore = fn SetMenuItemCommandID(parH, 5, _iPar5Cmd)
>
> childH = fn GetMenuHandle(_mChild)
> ignore = fn SetMenuItemCommandID(childH, 1, _iChild1Cmd)
> ignore = fn SetMenuItemCommandID(childH, 2, _iChild2Cmd)
>
> fn InstallMenuEvents(parH)
> fn InstallMenuEvents(childH)
> '~'<
> DeleteMenu(_mParent)
> InsertMenu(parH, -1)
> end fn
>
> local fn BuildUntitled1Wnd
> dim as Rect r
> '~'<
> SetRect(r, 0, 44, 640, 524)
> appearance window -_Untitled1Wnd, "Untitled 1", @r,
> _kDocumentWindowClass
> def SetWindowBackground(_kThemeActiveDialogBackgroundBrush, _zTrue)
>
> SetRect(r, 235, 174, 385, 194)
> appearance button _cPop, _activeBtn,, _mParent, -1,"Pop:", @r,
> _kControlPopupButtonProc
>
> appearance window _Untitled1Wnd
> end fn
>
> fn BuildPopMenu
> fn BuildUntitled1Wnd
>
> do
> HandleEvents
> until gFBQuit
> '----------
>
> Bernie
> --
>
>