I use this functions to create an automated Window menu for every opened window.
Project:
WindowMenuItem (as MenuItem)
Function Action() As Boolean
// Show the window associated with this MenuItem
mWindow.Show
End Function
Sub Constructor(w as Window)
// Save the window associated with the MenuItem
mWindow = w
End Sub
Function IsWindow(w as Window) As Boolean
// Return true if this is the window they are
// looking for
Return ( mWindow = w )
End Function
Private mWindow As Window
App:
Function HandleAppleEvent(theEvent As AppleEvent, eventClass As String, eventID As String) As Boolean
dim w As Window1 'your new window
if eventClass=“aevt” and eventID=“rapp” then
w = Window1 'Creates a new Win1
'Do things with Win1, if you want
return True 'The event has been handled.
end if
End Function
Main Window: Window1
Sub Open()
Dim winMenu As WindowMenuItem
winMenu = New WindowMenuItem(self)
winMenu.Text = self.Title // Be sure to set the text of the menu item
MainMenuBar.Child(“WindowMenu”).Append(winMenu)
End Sub
Sub EnableMenuItems()
// Set Checked = True for the current window
// Only the top-most window calls this event, so
// we first uncheck all the MenuItems and then just
// check the one that matches this window.
Dim count As Integer
Dim w As WindowMenuItem
count = WindowMenu.Count - 1
For i As Integer = 0 To count
w = WindowMenuItem( WindowMenu.Item( i ) )
WindowMenu.Item( i ).KeyboardShortcut = str(i)
w.Checked = False
// Check if the window in WindowMenu is this window (Self)
If w.IsWindow( Self ) Then
w.Checked = True
End If
Next
End Sub
Sub Close()
// Close ourself
App.CloseThisWindow( Self )
End Sub
All other window have the same “EnableMenuItems” and “Close” event (except the “Open” event). This works very well. All windows will be added to the menu item “Window” and get his own short cut (#0 to #5). When I close a window it will be removed from the window menu.
The problem is now, when a user open one or more other windows, close the main window (Window1) and then click on the menu, this error occurs on the window “wWine”:
0 MenuItem cannot be cast to WindowMenuItem
XojoFramework$7082
RuntimeCheckCast
wWine.wWine.Event_EnableMenuItems%%o<wWine.wWine>
XojoFramework$9600
enableMenuItems
XojoFramework$886
XojoFramework$892
-[NSMenu _populateFromDelegateWithEventRef:]
-[NSMenu _populateWithEventRef:]
-[NSCarbonMenuImpl _carbonPopulateEvent:handlerCallRef:]
NSSLMMenuEventHandler
_Z22_InvokeEventHandlerUPPP25OpaqueEventHandlerCallRefP14OpaqueEventRefPvPFlS0_S2_S3_E
_ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec
_ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec
SendEventToEventTargetWithOptions
_Z16SendMenuPopulateP8MenuDataP20OpaqueEventTargetRefmdmP14OpaqueEventRefhPh
_ZL15SendMenuOpeningP14MenuSelectDataP8MenuDatadmmP14__CFDictionaryhPh
_ZL11DrawTheMenuP14MenuSelectDataPP9__CFArrayhPh
_ZL11MenuChangedP14MenuSelectDatahh
ZL15TrackMenuCommonR14MenuSelectDataPhP13SelectionDataP10MenuResultS5
_ZL14MenuSelectCoreP8MenuData5PointdmPP13OpaqueMenuRefPt
_HandleMenuSelection2
_HandleMenuSelection
_NSHandleCarbonMenuEvent
_DPSNextEvent
-[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:]
-[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
XojoFramework$1253
XojoFramework$1254
Delegate.Invoke%%
Application._CallFunctionWithExceptionHandling%%op
XojoFramework$9668
XojoFramework$1253
-[NSApplication run]
XojoFramework$9670
RuntimeRun
REALbasic._RuntimeRun
_Main
main
start
I hope anyone has a resolution for this problem.