I know some folk think GOTO's are the devil's work, but they can be useful if used carefully.
Normally, you would use:
GOSUB somewhere
...
LABEL somewhere
...
But the instruction is capable of much more than that. Here is a note and test program that Paul did on the subject:
' Everybody has heard of and probably used GOSUB in their programs.
' What you may not know is that GOSUB can also use a string variable
' for a parameter and will execute the subroutine named by the string.
' What?? I hear many of you saying. Here is a short snippet:
' DEF mysub:STRING
' mysub = "DoSomething"
' ...
' GOSUB $mysub
' ...
' ...
' SUB DoSomething
' ...
' RETURN
' Now what is happening here is the '$' tells the GOSUB command that the name
' of the subroutine is contained in a string variable.
' So instead of looking for a subroutine named 'mysub',
' GOSUB retrieves the name of the subroutine from the string.
' Obviously by itself its not very useful. It becomes very powerful
' when you use an array of strings, all containing the name of a subroutine
' and you use it to map menu options or buttons in a dialog.
' Here is a complete example that you can cut and paste to try it out for yourself:
DEF win:WINDOW
DEF mysubs[4]:STRING
mysubs[1] = "DoOpen"
mysubs[2] = "DoSave"
mysubs[3] = "DoQuit"
WINDOW win,0,0,640,480,@SIZE|@MINBOX|@MAXBOX,0,"Advanced GOSUB",main
MENU win,"T,File,0,0","I,Open,0,1","I,Save,0,2","I,Quit,0,3"
run = 1
WAITUNTIL run = 0
CLOSEWINDOW win
END
main:
SELECT @CLASS
CASE @IDMENUPICK
GOSUB $MySubs[@MENUNUM]
CASE @IDCLOSEWINDOW
run = 0
ENDSELECT
RETURN
SUB DoOpen
MessageBox win, "You Selected Open!","Cool"
RETURN
SUB DoSave
MessageBox win, "You Selected Save!","Great"
RETURN
SUB DoQuit
run = 0
RETURN
' Now instead of using long SELECT, CASE, ENDSELECT sections to test for the menu ID
' we used the advanced GOSUB and used the menu ID as an index into the string array.
' Notice we did not use array index '0'. This is because menu ID's start at 1.
' Paul 2002
all the best, :)
Graham
You must have read my mind Graham, thank you!
I was just sitting here, strugling with learning how to think as a programmer again, and was trying to do exactly the same thing.
Maybe you would be kind enough to explain how to use the T|I|S - directives as well? What I am trying to do is to produce several submenuitems under each other in the same coloumn, or am I reaching for the moon?
Regards
Egil
Hi Egil,
An example using menus is the 'editor.cba' in the samples programs.
Also, you could have a read of the following exchanges many moons ago .. :)
Quote
Paul
Posted: Thu Aug 15, 2002 01:25:34 PM Post subject: Menu Question
I have a dialogbox with a rather extensive menu. Now I can't figure out how to write the menu item selection process. When I use @MENUNUM = 1 I get a hit on the first item of each category. Here is my code. I think you can tell better from it what my problem is. Watch out for line wrap, especially on the menu line.
Code:
def dlgMainPermits:Dialog
def bitmap:INT
def w,h:INT
def myPath,myPic:string
DIALOG dlgMainPermits,0,0,785,460,0x80CB0080,0,"Lenoir County Permits Charge Accounts",dlgMainPermitsHandler
CONTROL dlgMainPermits,"M,ComboBox1,11,53,190,375,@TABSTOP|0x50A00641,1"
CONTROL dlgMainPermits,"E,Acct Num,327,167,70,20,0x50800800,2"
CONTROL dlgMainPermits,"E,Company,327,193,418,20,0x50810000,3"
CONTROL dlgMainPermits,"E,Address 1,327,223,424,20,0x50810000,4"
CONTROL dlgMainPermits,"E,Address 2,327,253,424,20,0x50810000,5"
CONTROL dlgMainPermits,"E,Phone,327,279,130,20,0x50810000,6"
CONTROL dlgMainPermits,"E,Fax,327,307,126,20,0x50810000,7"
CONTROL dlgMainPermits,"E,Zip Code,327,333,82,20,0x50810000,8"
CONTROL dlgMainPermits,"T,,327,97,366,38,@CTLSTCBITMAP,10"
CONTROL dlgMainPermits,"C,,215,48,554,350,0x50000007,12"
CONTROL dlgMainPermits,"T,ACCT NUMBER,229,168,90,20,0x5000010B,13"
CONTROL dlgMainPermits,"T,COMPANY NAME,229,196,90,20,0x5000010B,14"
CONTROL dlgMainPermits,"T,ADDRESS 1,229,224,90,20,0x5000010B,15"
CONTROL dlgMainPermits,"T,ADDRESS 2,229,254,90,20,0x5000010B,16"
CONTROL dlgMainPermits,"T,PHONE,229,280,90,20,0x5000010B,17"
CONTROL dlgMainPermits,"T,FAX,229,308,90,20,0x5000010B,18"
CONTROL dlgMainPermits,"T,ZIP CODE,229,334,90,20,0x5000010B,19"
'CONTROL dlgMainPermits,"B,Cancel,625,415,70,20,0x50010000,20"
'CONTROL dlgMainPermits,"B,Modify,521,415,70,20,0x50010000,21"
'CONTROL dlgMainPermits,"B,Add,450,415,70,20,0x50010000,22"
CONTROL dlgMainPermits,"B,Close,698,405,70,20,0x50010001,11"
'CONTROL dlgMainPermits,"B,Current Transactions,13,435,76,31,0x50002000,23"
'CONTROL dlgMainPermits,"B,Charge History,91,435,76,31,0x50002000,24"
'CONTROL dlgMainPermits,"B,Payment History,169,435,76,31,0x50002000,25"
domodal dlgMainPermits
end
sub dlgMainPermitshandler
select @class
case @IDINITDIALOG
MENU dlgMainPermits,"T,File,0,0","I,Quit,0,1","T,Clients,0,1","I,Add,0,1","I,Modify,0,2","T,Transactions,0,2","I,Charges,0,1","I,Payments,0,2","S,Current,0,3","I,View,0,2","I,Add,0,3","I,Modify,0,4","T,Zip Codes,0,3","I,View,0,1","I,Add,0,2","T,Reports,0,4"
' myPath=getstartpath
' myPic=myPath + "ClntData.bmp"
' setcontroltext dlgMainPermits,10,myPic
centerwindow dlgMainPermits
case @idcontrol
select @controlid
case 11
closedialog dlgMainPermits,@idok
endselect
'===========================================
'= THIS IS WHERE MY PROBLEM BEGINS!! =
'===========================================
case @IDMENUPICK
SELECT @MENUNUM
CASE 1
MESSAGEBOX dlgMainPermits,"You Clicked MenuNum 1","Menu Test"
CASE 2
MESSAGEBOX dlgMainPermits,"You Clicked MenuNum 2","Menu Test"
ENDSELECT
endselect
return
Any help on how to get my menu to work will be greatly appreciated. Oh there is a BMP label that of course won't work because you don't have the image.
Thanx;
Paul
Paul
Posted: Thu Aug 15, 2002 02:00:08 PM Post subject: Re: Menu Question
The problem is that if you click on "File>Quit" the message "You clicked MenuNum 1" comes up. If you click on "Clients>Add" you get the same message box. If you click on "Transactions>Charges" you get the same message box, and so on. This is not right. I need to get a different response for each one. For example if I have "File>Quit" activated it will close the program. I don't want that to happen if I click on "Clients>Add" or "Transactions>Charges". Does that make sense? I should only get "You clicked MenuNum 1" on "File>Quit" and nothing else.
Hope I've explained my problem better here.
Paul
Jerry Muelver
Posted: Thu Aug 15, 2002 03:14:55 PM Post subject: Re: Menu Question
You have to synchornize the last field of the menu item descirptor, the menupick's ID, with the CASE statements. You have:
dlgMainPermits,"T,File,0,0","I,Quit,0,1",
"T,Clients,0,1","I,Add,0,1","I,Modify,0,2",
"T,Transactions,0,2","I,Charges,0,1","I,Payments,0,2","S,Current,0,3","I,View,0,2","I,Add,0,3","I,Modify,0,4",
"T,Zip Codes,0,3","I,View,0,1","I,Add,0,2",
"T,Reports,0,4"
The numbers 1, 1, 1, 2, 2, 1, 2, 3, 2, 3, 4, 2, 1, 2, 4
should be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
Then your case statements would be:
Code:
CASE 1
MESSAGEBOX dlgMainPermits,"You Clicked MenuNum 1","Menu Test"
CASE 2
MESSAGEBOX dlgMainPermits,"You Clicked MenuNum 2","Menu Test"
CASE 3
MESSAGEBOX dlgMainPermits,"You Clicked MenuNum 3","Menu Test"
CASE 4
MESSAGEBOX dlgMainPermits,"You Clicked MenuNum 4","Menu Test"
Of course, menu headers ("T") won't get any action, since only menu items return the @IDMENUPICK signal. See Cor's reply for a detailed and correct implementation.
Cor
Posted: Thu Aug 15, 2002 03:37:32 PM Post subject: Re: Menu Question
The following is a working version:
I use INSERTMENU, not needed, but find it very handy
Also I use constants see at the top.
It's easier for menuchecking, don't have to remember numbers.
Also you will automatically comment the source
Code:
def dlgMainPermits:Dialog
def bitmap:INT
def w,h:INT
def myPath,myPic:string
' Menu definitions
CONST FILE_MENU=100
CONST QUIT_MENU=105
CONST CLIENTS_MENU=200
CONST CLIENTS_ADD_MENU=205
CONST CLIENTS_MODIFY_MENU=210
CONST TRANSACTIONS_MENU=300
CONST TRANSACTIONS_CURRENT_ADD_MENU=305
CONST ZIP_CODES_MENU=400
CONST REPORTS_MENU=500
;
DIALOG dlgMainPermits,0,0,785,460,0x80CB0080,0,"Lenoir County Permits Charge Accounts",dlgMainPermitsHandler
;
CONTROL dlgMainPermits,"M,ComboBox1,11,53,190,375,@TABSTOP|0x50A00641,1"
CONTROL dlgMainPermits,"E,Acct Num,327,167,70,20,0x50800800,2"
CONTROL dlgMainPermits,"E,Company,327,193,418,20,0x50810000,3"
CONTROL dlgMainPermits,"E,Address 1,327,223,424,20,0x50810000,4"
CONTROL dlgMainPermits,"E,Address 2,327,253,424,20,0x50810000,5"
CONTROL dlgMainPermits,"E,Phone,327,279,130,20,0x50810000,6"
CONTROL dlgMainPermits,"E,Fax,327,307,126,20,0x50810000,7"
CONTROL dlgMainPermits,"E,Zip Code,327,333,82,20,0x50810000,8"
CONTROL dlgMainPermits,"T,,327,97,366,38,@CTLSTCBITMAP,10"
CONTROL dlgMainPermits,"C,,215,48,554,350,0x50000007,12"
CONTROL dlgMainPermits,"T,ACCT NUMBER,229,168,90,20,0x5000010B,13"
CONTROL dlgMainPermits,"T,COMPANY NAME,229,196,90,20,0x5000010B,14"
CONTROL dlgMainPermits,"T,ADDRESS 1,229,224,90,20,0x5000010B,15"
CONTROL dlgMainPermits,"T,ADDRESS 2,229,254,90,20,0x5000010B,16"
CONTROL dlgMainPermits,"T,PHONE,229,280,90,20,0x5000010B,17"
CONTROL dlgMainPermits,"T,FAX,229,308,90,20,0x5000010B,18"
CONTROL dlgMainPermits,"T,ZIP CODE,229,334,90,20,0x5000010B,19"
'CONTROL dlgMainPermits,"B,Cancel,625,415,70,20,0x50010000,20"
'CONTROL dlgMainPermits,"B,Modify,521,415,70,20,0x50010000,21"
'CONTROL dlgMainPermits,"B,Add,450,415,70,20,0x50010000,22"
CONTROL dlgMainPermits,"B,Close,698,405,70,20,0x50010001,11"
'CONTROL dlgMainPermits,"B,Current Transactions,13,435,76,31,0x50002000,23"
'CONTROL dlgMainPermits,"B,Charge History,91,435,76,31,0x50002000,24"
'CONTROL dlgMainPermits,"B,Payment History,169,435,76,31,0x50002000,25"
domodal dlgMainPermits
end
sub dlgMainPermitshandler
select @class
case @IDINITDIALOG
' MENU dlgMainPermits,"T,File,0,0","I,Quit,0,1","T,Clients,0,2","I,Add,0,3","I, Modify,0,4","T,Transactions,0,5","I,Charges,0,1","I,Payments,0,2","S,Cur rent,0,3","I,View,0,2","I,Add,0,3","I,Modify,0,4","T,Zip Codes,0,3","I,View,0,1","I,Add,0,2","T,Reports,0,4"
MENU dlgMainPermits,"T,File,0,FILE_MENU","I,Quit,0,QUIT_MENU"
INSERTMENU dlgMainPermits,1,"T,Clients,0,CLIENTS_MENU","I,Add,0,CLIENTS_ADD_MENU","I, Modify,0,CLIENTS_MODIFY_MENU"
INSERTMENU dlgMainPermits,2,"T,Transactions,0,TRANSACTIONS_MENU","I,Charges,0,21","I,Payments,0,22","S,Current,0,23","I,View,0,24","I,Add,0,TRANSACTIONS_CURRENT_ADD_MENU","I,Modify,0,26"
INSERTMENU dlgMainPermits,3,"T,Zip Codes,0,ZIP_CODES_MENU","I,View,0,31","I,Add,0,32"
INSERTMENU dlgMainPermits,4,"T,Reports,0,REPORTS_MENU"
' myPath=getstartpath
' myPic=myPath + "ClntData.bmp"
' setcontroltext dlgMainPermits,10,myPic
centerwindow dlgMainPermits
case @idcontrol
select @controlid
case 11
closedialog dlgMainPermits,@idok
endselect
'===========================================
'= THIS IS WHERE MY PROBLEM BEGINS!! =
'===========================================
case @IDMENUPICK
SELECT @MENUNUM
CASE QUIT_MENU
MESSAGEBOX dlgMainPermits,"File->Quit","Info"
CASE REPORTS_MENU
MESSAGEBOX dlgMainPermits,"Reports","Info"
case CLIENTS_ADD_MENU
MESSAGEBOX dlgMainPermits,"Clients->add","Info"
CASE TRANSACTIONS_CURRENT_ADD_MENU
MESSAGEBOX dlgMainPermits,"Transactions->Current->add","Info"
ENDSELECT
endselect
return
Paul
Posted: Thu Aug 15, 2002 04:15:49 PM Post subject: Re: Menu Question
OH! I see said the blind man :D
I was thinking that it was a sequential thing 1,2,3,4,5 but it's not it's based on the Title. I didn't know that one could do a select on title name, I thought it had to be some magic number that I wasn't finding.
Just one more question. In the constents, where did those numbers come from? the 100, 105, 200, 205... that are assigned to File_Menu, Quit_Menu, and so on. How would I know to use these exact numbers or doesn't the number matter as long as it's unique?
Also where do I go to learn this? I read the manual about menus, searched the forums, and checked the demos no where did I find anything like this.
Thank you Cor and Jerry that makes sense now. I really appreciate your help.
Thanx; :)
Paul
Cor
Posted: Thu Aug 15, 2002 04:25:20 PM Post subject: Re: Menu Question
The numbers doesn't matter, they must be unique ofcourse for the menu.
File menu =100
so you can have 100 items for File->
Next top Menu number 200
Quote:
Just one more question. In the constents, where did those numbers come from? the 100, 105, 200, 205... that are assigned to File_Menu, Quit_Menu, and so on. How would I know to use these exact numbers or doesn't the number matter as long as it's unique?
Paul
Posted: Thu Aug 15, 2002 04:53:24 PM Post subject: Re: Menu Question
OH OK, and by using the 100, 200, so on, the sequence is maintained by Title selection. Yea that makes sense. Thank you Cor I appreciate you taking the time to not only show me how to do it but also taking the time to explain what you did. That really helps.
Thanx;
Paul
(Paul by the way, is another Paul .. ;D)
Hope that helps, :)
Graham
ThankYou Graham!
Just the information I needed.
Egil
PS
tonight I had a lot of QSO's to finland and the Baltic states via ..... AURORA! :)
Saw this thread long ago, but now I am I need of something related to it: if I create dynamic menuitems, it will be useful to me if I can get text from that menuitem. I mean, I would like to get the text from menu where @menunum=1 or 2 or 3, etc. How can I do that?
Thanks in advance.
Btw, I post here becase it seems where it best fit considering the content, but I am asking this for EB :)
Joao
Two things in the API you need to look at:
QuoteThe GetMenuItemInfo function retrieves information about a menu item.
BOOL WINAPI GetMenuItemInfo(
HMENU hMenu,
UINT uItem,
BOOL fByPosition,
LPMENUITEMINFO lpmii
);
Parameters
hMenu
Handle to the menu that contains the menu item.
uItem
Identifier or position of the menu item to get information about. The meaning of this parameter depends on the value of fByPosition.
fByPosition
Value specifying the meaning of uItem. If this parameter is FALSE, uItem is a menu item identifier. Otherwise, it is a menu item position.
lpmii
Pointer to a MENUITEMINFO structure that specifies the information to retrieve and receives information about the menu item.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, use the GetLastError function.
and the structure:
QuoteThe MENUITEMINFO structure contains information about a menu item.
typedef struct tagMENUITEMINFO {
UINT cbSize;
UINT fMask;
UINT fType;
UINT fState;
UINT wID;
HMENU hSubMenu;
HBITMAP hbmpChecked;
HBITMAP hbmpUnchecked;
DWORD dwItemData;
LPTSTR dwTypeData;
UINT cch;
} MENUITEMINFO, FAR *LPMENUITEMINFO;
Members
cbsize
Size of structure, in bytes.
fMask
Members to retrieve or set. This member can be one or more of these values:
Value Meaning
MIIM_CHECKMARKS Retrieves or sets the hbmpChecked and hbmpUnchecked members.
MIIM_DATA Retrieves or sets the dwItemData member.
MIIM_ID Retrieves or sets the wID member.
MIIM_STATE Retrieves or sets the fState member.
MIIM_SUBMENU Retrieves or sets the hSubMenu member.
MIIM_TYPE Retrieves or sets the fType and dwTypeData members.
fType
Menu item type. This member can be one or more of these values:
Value Meaning
MFT_BITMAP Displays the menu item using a bitmap. The low-order word of the dwTypeData member is the bitmap handle, and the cch member is ignored.
MFT_MENUBARBREAK Places the menu item on a new line (for a menu bar) or in a new column (for a drop-down menu, submenu, or shortcut menu). For a drop-down menu, submenu, or shortcut menu, a vertical line separates the new column from the old.
MFT_MENUBREAK Places the menu item on a new line (for a menu bar) or in a new column (for a drop-down menu, submenu, or shortcut menu). For a drop-down menu, submenu, or shortcut menu, the columns are not separated by a vertical line.
MFT_OWNERDRAW Assigns responsibility for drawing the menu item to the window that owns the menu. The window receives a WM_MEASUREITEM message before the menu is displayed for the first time, and a WM_DRAWITEM message whenever the appearance of the menu item must be updated. If this value is specified, the dwTypeData member contains an application-defined 32-bit value.
MFT_RADIOCHECK Displays checked menu items using a radio-button mark instead of a check mark if the hbmpChecked member is NULL.
MFT_RIGHTJUSTIFY Right-justifies the menu item and any subsequent items. This value is valid only if the menu item is in a menu bar.
MFT_SEPARATOR Specifies that the menu item is a separator. A menu item separator appears as a horizontal dividing line. The dwTypeData and cch members are ignored. This value is valid only in a drop-down menu, submenu, or shortcut menu.
MFT_STRING Displays the menu item using a text string. The dwTypeData member is the pointer to a null-terminated string, and the cch member is the length of the string.
The MFT_BITMAP, MFT_SEPARATOR, and MFT_STRING values cannot be combined with one another.
fState
Menu item state. This member can be one or more of these values:
Value Meaning
MFS_CHECKED Checks the menu item. For more information about checked menu items, see the hbmpChecked member.
MFS_DEFAULT Specifies that the menu item is the default. A menu can contain only one default menu item, which is displayed in bold.
MFS_DISABLED Disables the menu item so that it cannot be selected, but does not gray it.
MFS_ENABLED Enables the menu item so that it can be selected. This is the default state.
MFS_GRAYED Disables the menu item and grays it so that it cannot be selected.
MFS_HILITE Highlights the menu item.
MFS_UNCHECKED Unchecks the menu item. For more information about unchecked menu items, see the hbmpUnchecked member.
MFS_UNHILITE Removes the highlight from the menu item. This is the default state.
wID
Application-defined 16-bit value that identifies the menu item.
hSubMenu
Handle to the drop-down menu or submenu associated with the menu item. If the menu item is not an item that opens a drop-down menu or submenu, this member is NULL.
hbmpChecked
Handle to the bitmap to display next to the item if it is checked. If this member is NULL, a default bitmap is used. If the MFT_RADIOCHECK type value is specified, the default bitmap is a bullet. Otherwise, it is a check mark.
hbmpUnchecked
Handle to the bitmap to display next to the item if it is not checked. If this member is NULL, no bitmap is used.
dwItemData
Application-defined value associated with the menu item.
dwTypeData
Content of the menu item. The meaning of this member depends on the menu item type: the MFT_BITMAP, MFT_SEPARATOR, or MFT_STRING values.
cch
Length of the menu item text, when information is received about a menu item of the MFT_STRING type. This value is zero for other menu item types. This member is ignored when the content of a menu item is set.
Hope that gets you started.
Larry
Asked this at Coding Monkeys too, and besides receive a wonderful advice (check windows.inc before exclude all chances of figure out a problem's solution), peterpuk told me to use getmenustring and getmenu in order to get a menu item string.
Tried also the getmenuiteminfo, but found it difficult to translate the menuitem to a string.
Thanks for all the advices!
DECLARE IMPORT, _GetMenuString ALIAS GetMenuStringA(hMenu AS INT,wIDItem AS INT,lpString AS STRING,nMaxCount AS INT,wFlag AS INT),INT
DECLARE IMPORT, _GetMenu ALIAS GetMenu(hwnd AS INT),INT
_GetMenuString(_GetMenu(w1.hwnd),@MENUNUM,menutext,150,0x0)
with "menutext" being a string with the text of @menunum
When passing @MENUNUM, set the last parameter to MF_BYCOMMAND (also 0x0), otherwise use MF_BYPOSITION, so the @MENUNUM will be treated as index.
MenuID = @MENUNUM
_GetMenuString(_GetMenu(w1.hwnd), MenuID, menutext,150, MF_BYCOMMAND)
MenuIndex = 0
_GetMenuString(_GetMenu(w1.hwnd), MenuIndex, menutext,150, MF_BYPOSITION)
When using MF_BYCOMMAND, the GetMenuString function will scan the entire menu (including all popups) for the item with given ID.
Yes, GetMenuItemInfo is a bit a harder to use, but it is worth knowing what it does and how you use this function.
Any function where you need a structure can be confusing you if you don't get good examples. Generally I can work through some of the more tricky API functions by using the old VB/API examples and converting to EB.
So I guess you will use the menu text with Advanced Gosub somehow?
If the question was to me... no :) Just asked this in this thread because was somewhat related to it. Inclusively this is CB forum and I just currently code in EB. Was somewhat hard to change, but once I learned enough EB, I just code with it. And at the moment I have 2 big projects in hands, both started in IB STD, shaped when I knew CB existed, and both translated to EB. These last questions I am making in forum are due to the translation of the second program. Decided, due to many reasons, that was better to start from 0 both programs in EB, which is making code much less heavy, more clear and easy to read, and with better results/less bugs.