Menu "resource" generator

BlitzMax Forums/BlitzMax GUI Programming/Menu "resource" generator

I was playing around with my parser generator and tried to come up with something other than a programming language.

I ended up trying to make a menu resource for making it easier to change the menus in MaxGUI.

It takes something like this:
{
Import BRL.MaxGUI
}
MENU Edit {
		"Cu&t"			MENUCUT			[CTRL+X] .
		"&Copy"			MENUCOPY		[CTRL+C] .
		"&Paste"		MENUPASTE		[CTRL+V] .
		-
		"Select &All"	MENUSELECTALL	[CTRL+A] .
}

MAINMENU main {
	"&File" <file> {
		"&New"		MENUNEW			[CTRL+N] .
		"&Open"		MENUOPEN		[CTRL+O] .
		"&Recent"	<recentmenu> .
		!"&Close"	MENUCLOSE		[CTRL+F4] .
		-
		!"&Save"	MENUSAVE		[CTRL+S] .
		!"Save &As"	MENUSAVEAS		[SHIFT+CTRL+S] .
		-
		"E&xit"		MENUEXIT .
	}
	@Edit "&Edit" <edit>
}

And generates a file with this:
SuperStrict

Import BRL.MaxGUI

Const MENUCUT:Int = 1
Const MENUCOPY:Int = 2
Const MENUPASTE:Int = 3
Const MENUSELECTALL:Int = 4
Const MENUNEW:Int = 5
Const MENUOPEN:Int = 6
Const MENUCLOSE:Int = 7
Const MENUSAVE:Int = 8
Const MENUSAVEAS:Int = 9
Const MENUEXIT:Int = 10

Function GetMenuActionName:String( id:int)
	Global names:String[] = [ "", ..
		"MENUCUT", "MENUCOPY", "MENUPASTE", "MENUSELECTALL", "MENUNEW", "MENUOPEN", ..
		"MENUCLOSE", "MENUSAVE", "MENUSAVEAS", "MENUEXIT" ..
	]
	If (id >= 0) And (id < names.Length) Then Return names[id]
	Return "Unknown"
EndFunction

Type TMenus
	Field file:TGadget
	Field recentmenu:TGadget
	Field edit:TGadget

	Method CreateEditMenu:TGadget( parent:TGadget=Null, title:String=Null)
		If parent And title Then
			parent = CreateMenu( title, 0, parent)
		ElseIf Not parent Then
			parent = CreateMenu( title, 0, Null)
		EndIf
		CreateMenu( "Cu&t", MENUCUT, parent, KEY_X, MODIFIER_CONTROL)
		CreateMenu( "&Copy", MENUCOPY, parent, KEY_C, MODIFIER_CONTROL)
		CreateMenu( "&Paste", MENUPASTE, parent, KEY_V, MODIFIER_CONTROL)
		CreateMenu( "", 0, parent)
		CreateMenu( "Select &All", MENUSELECTALL, parent, KEY_A, MODIFIER_CONTROL)
		Return parent
	EndMethod

	Method CreateMainMenu( parent:TGadget)
		file = CreateMenu( "&File", 0, parent)
		CreateMenu( "&New", MENUNEW, file, KEY_N, MODIFIER_CONTROL)
		CreateMenu( "&Open", MENUOPEN, file, KEY_O, MODIFIER_CONTROL)
		recentmenu = CreateMenu( "&Recent", 0, file)
		DisableGadget CreateMenu( "&Close", MENUCLOSE, file, KEY_F4, MODIFIER_CONTROL)
		CreateMenu( "", 0, file)
		DisableGadget CreateMenu( "&Save", MENUSAVE, file, KEY_S, MODIFIER_CONTROL)
		DisableGadget CreateMenu( "Save &As", MENUSAVEAS, file, KEY_S, MODIFIER_SHIFT | MODIFIER_CONTROL)
		CreateMenu( "", 0, file)
		CreateMenu( "E&xit", MENUEXIT, file)
		edit = CreateEditMenu(parent, "&Edit")
	EndMethod
EndType


Does anyone find this sort of thing useful?

Heres an example to see it in action:
SuperStrict

Global window:TGadget = CreateWindow("My Window", 64,64,512,512, Null, WINDOW_RESIZABLE | WINDOW_TITLEBAR | WINDOW_MENU)
Global panel1:TGadget = CreatePanel( 0,0, ClientWidth(window)/2,ClientHeight(window), window, PANEL_ACTIVE)
Global panel2:TGadget = CreatePanel( ClientWidth(window)/2,0, ClientWidth(window)/2,ClientHeight(window), window, PANEL_ACTIVE)

SetPanelColor panel1, 255,0,0
SetPanelColor panel2, 0,255,0

' create window menu
Global menus:TMenus = New TMenus
menus.CreateMainMenu( WindowMenu( window))
UpdateWindowMenu( window)

' create another edit popup menu
Global editmenu:TGadget = menus.CreateEditMenu()

While WaitEvent()
	Select CurrentEvent.ID
		Case EVENT_MENUACTION
			DebugLog "MenuAction : " + GetMenuActionName( CurrentEvent.Data)
		Case EVENT_MOUSEUP
			If CurrentEvent.Data = 2 Then 
				Select CurrentEvent.Source
					Case panel1	PopupWindowMenu panel1, editmenu	' popup the one we created on the side
					Case panel2	PopupWindowMenu panel2, menus.edit	' popup the one attached to the window
				EndSelect
			EndIf				
		Case EVENT_WINDOWCLOSE
			If CurrentEvent.Source = window Then Exit
	End Select
Wend
End

Const MENUCUT:Int = 1
Const MENUCOPY:Int = 2
Const MENUPASTE:Int = 3
Const MENUSELECTALL:Int = 4
Const MENUNEW:Int = 5
Const MENUOPEN:Int = 6
Const MENUCLOSE:Int = 7
Const MENUSAVE:Int = 8
Const MENUSAVEAS:Int = 9
Const MENUEXIT:Int = 10

Function GetMenuActionName:String( id:Int)
	Global names:String[] = [ "", ..
		"MENUCUT", "MENUCOPY", "MENUPASTE", "MENUSELECTALL", "MENUNEW", "MENUOPEN", ..
		"MENUCLOSE", "MENUSAVE", "MENUSAVEAS", "MENUEXIT" ..
	]
	If (id >= 0) And (id < names.Length) Then Return names[id]
	Return "Unknown"
EndFunction

Type TMenus
	Field file:TGadget
	Field recentmenu:TGadget
	Field edit:TGadget

	Method CreateEditMenu:TGadget( parent:TGadget=Null, title:String=Null)
		If parent And title Then
			parent = CreateMenu( title, 0, parent)
		ElseIf Not parent Then
			parent = CreateMenu( title, 0, Null)
		EndIf
		CreateMenu( "Cu&t", MENUCUT, parent, KEY_X, MODIFIER_CONTROL)
		CreateMenu( "&Copy", MENUCOPY, parent, KEY_C, MODIFIER_CONTROL)
		CreateMenu( "&Paste", MENUPASTE, parent, KEY_V, MODIFIER_CONTROL)
		CreateMenu( "", 0, parent)
		CreateMenu( "Select &All", MENUSELECTALL, parent, KEY_A, MODIFIER_CONTROL)
		Return parent
	EndMethod

	Method CreateMainMenu( parent:TGadget)
		file = CreateMenu( "&File", 0, parent)
		CreateMenu( "&New", MENUNEW, file, KEY_N, MODIFIER_CONTROL)
		CreateMenu( "&Open", MENUOPEN, file, KEY_O, MODIFIER_CONTROL)
		recentmenu = CreateMenu( "&Recent", 0, file)
		DisableGadget CreateMenu( "&Close", MENUCLOSE, file, KEY_F4, MODIFIER_CONTROL)
		CreateMenu( "", 0, file)
		DisableGadget CreateMenu( "&Save", MENUSAVE, file, KEY_S, MODIFIER_CONTROL)
		DisableGadget CreateMenu( "Save &As", MENUSAVEAS, file, KEY_S, MODIFIER_SHIFT | MODIFIER_CONTROL)
		CreateMenu( "", 0, file)
		CreateMenu( "E&xit", MENUEXIT, file)

		edit = CreateEditMenu(parent, "&Edit")
	EndMethod
EndType