Changing a menu
Changing a menu
I am changing my program's menu when the user selects a new folder path, so I can update the "recent folders" menu items
I find that I can simply use the Create Menu command, redo the menu, and all seems to work fine.
But, I'm wondering if this is "legal" or if it will cause problems, like memory leaks or something?
I know I can use the Free Menu() command first, but if I do that it makes my program's window flash, as the old menu is released, then the new one is added, so I'd rather avoid doing that if possible.
I find that I can simply use the Create Menu command, redo the menu, and all seems to work fine.
But, I'm wondering if this is "legal" or if it will cause problems, like memory leaks or something?
I know I can use the Free Menu() command first, but if I do that it makes my program's window flash, as the old menu is released, then the new one is added, so I'd rather avoid doing that if possible.
Dr Pixel
-
Doobrey
- Enthusiast

- Posts: 218
- Joined: Sat Apr 26, 2003 4:47 am
- Location: Dullsville..population: me
- Contact:
Re: Changing a menu
AFAIK, using CreateMenu() should free any menu already created with same menu number.. otherwise it`d leak resources.
I`ve always done a FreeMemory() before recreating the menu, but I`ve never noticed the window flicker.
Maybe disabling window drawing before FreeMemory and enabling it again after CreateMenu() would help?
I`ve always done a FreeMemory() before recreating the menu, but I`ve never noticed the window flicker.
Maybe disabling window drawing before FreeMemory and enabling it again after CreateMenu() would help?
Code: Select all
Procedure SetMenuText(menuHandle,nr,text$)
ModifyMenu_(menuHandle,nr,#MF_STRING,nr,text$)
EndProcedure
#win=1
OpenWindow(#win,0,0,300,300,#PB_Window_ScreenCentered|#PB_Window_SystemMenu,"Test")
MenuID=CreateMenu(1,WindowID())
MenuTitle("Test")
#menu_item=10
MenuItem(1,"Hello")
MenuItem(#menu_item,"Rename me")
MenuItem(2,"It's me mario")
a=0
Repeat
EventID=WaitWindowEvent()
If EventID=#PB_Event_Menu
If EventMenuID()=#menu_item
a+1
Select a
Case 1:SetMenuText(MenuID,#menu_item,"i have changed me")
Case 2:SetMenuText(MenuID,#menu_item,"dada")
Case 3:SetMenuText(MenuID,#menu_item,"change the text")
Case 4:SetMenuText(MenuID,#menu_item,"And again")
Case 5:SetMenuText(MenuID,#menu_item,"Rename me"):a=0
EndSelect
EndIf
EndIf
Until EventID=#PB_Event_CloseWindow-
PolyVector
- Enthusiast

- Posts: 499
- Joined: Wed Sep 17, 2003 9:17 pm
- Location: Southern California
- Contact:
The search function in this forum is your friend:
viewtopic.php?t=4986&highlight=files+menu
Hope it's what you need.
viewtopic.php?t=4986&highlight=files+menu
Hope it's what you need.
Well, here is what I am doing -
I'm trying to make the "recent history" menu items work as they do, for example, in PSP
If I set the option to have 4 recent files in PSP, it shows the last 4 files in the menu.
If I change this to 6, it still shows the last 4 on the menu, with no blank spaces. Now I load another file - instantly, 5 items are shown on the menu, but the window contents do NOT jump up for an instant, then down.
If I free the old menu before re-creating it, my window contents DO jump up, then down - very unprofessional looking. If I simply recreate the menu without freeing the old one, there is no jumping, which is what I am looking for.
So, what I really need is a way to add or remove menu items on-the-fly, not simply disable/enable or change the text of the existing menu items.
PSP can do this, so surely it is possible, but I don't know how, other than by not freeing the existing menu.
It's the jumping of the window contents that I want to avoid, I've tried disabling the window, and sticking a temporary "dummy" menu in, but neither of those work.
I'm trying to make the "recent history" menu items work as they do, for example, in PSP
If I set the option to have 4 recent files in PSP, it shows the last 4 files in the menu.
If I change this to 6, it still shows the last 4 on the menu, with no blank spaces. Now I load another file - instantly, 5 items are shown on the menu, but the window contents do NOT jump up for an instant, then down.
If I free the old menu before re-creating it, my window contents DO jump up, then down - very unprofessional looking. If I simply recreate the menu without freeing the old one, there is no jumping, which is what I am looking for.
So, what I really need is a way to add or remove menu items on-the-fly, not simply disable/enable or change the text of the existing menu items.
PSP can do this, so surely it is possible, but I don't know how, other than by not freeing the existing menu.
It's the jumping of the window contents that I want to avoid, I've tried disabling the window, and sticking a temporary "dummy" menu in, but neither of those work.
Dr Pixel
Use SendMessage_() at the beginning and the end of the menu creation.Dr_Pixel wrote:Well, here is what I am doing -
I'm trying to make the "recent history" menu items work as they do, for example, in PSP
If I set the option to have 4 recent files in PSP, it shows the last 4 files in the menu.
If I change this to 6, it still shows the last 4 on the menu, with no blank spaces. Now I load another file - instantly, 5 items are shown on the menu, but the window contents do NOT jump up for an instant, then down.
If I free the old menu before re-creating it, my window contents DO jump up, then down - very unprofessional looking. If I simply recreate the menu without freeing the old one, there is no jumping, which is what I am looking for.
So, what I really need is a way to add or remove menu items on-the-fly, not simply disable/enable or change the text of the existing menu items.
PSP can do this, so surely it is possible, but I don't know how, other than by not freeing the existing menu.
It's the jumping of the window contents that I want to avoid, I've tried disabling the window, and sticking a temporary "dummy" menu in, but neither of those work.
Code: Select all
< -------------------- >
; PureBasic IDE - Main
; < -------------------- >
;
; This program is free software; you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation; either version 2 of the License, or (at your option) any later
; version. See the License.txt file for more details.
;
; (c) 2002 Fantaisie Software
;
Procedure CreateIDEMenu(DisableRedraw)
If DisableRedraw
SendMessage_(*MainWindow, #WM_SETREDRAW, 0, 0) ; Needed to remove any flickering when recreating the menu on runtime
EndIf
*MainMenu = CreateMenu(0, WindowID(#WINDOW_Main))
If *MainMenu
; ---------------------------------- File
MenuTitle(Langage(0))
MenuItem(#MENU_New , Langage(1))
MenuItem(#MENU_Open , Langage(2))
MenuItem(#MENU_Save , Langage(3))
MenuItem(#MENU_SaveAs, Langage(4))
MenuItem(#MENU_Close , Langage(104))
MenuBar();
; MenuItem(#MENU_Print, Langage(5))
MenuItem(#MENU_Preference, Langage(37))
Menu_AddRecentFiles()
MenuBar()
MenuItem(#MENU_Exit, Langage(6))
; ---------------------------------- Edition
MenuTitle(Langage(7));
MenuItem(#MENU_Undo, Langage(67))
MenuItem(#MENU_Redo, Langage(68))
MenuBar()
MenuItem(#MENU_Cut , Langage(8))
MenuItem(#MENU_Copy , Langage(9))
MenuItem(#MENU_Paste, Langage(10))
MenuBar()
MenuItem(#MENU_SelectAll, Langage(105))
MenuBar()
MenuItem(#MENU_Goto, Langage(36))
MenuBar()
MenuItem(#MENU_Find, Langage(11))
MenuItem(#MENU_Next, Langage(12))
; ---------------------------------- Compiler
MenuTitle(Langage(13))
MenuItem(#MENU_CompileRun , Langage(14))
MenuItem(#MENU_Run , Langage(15))
MenuBar()
MenuItem(#MENU_Debugger , Langage(16))
MenuBar()
MenuItem(#MENU_CompilerOption , Langage(17))
MenuItem(#MENU_CreateExecutable, Langage(18))
; ---------------------------------- Tools
MenuTitle(Langage(141))
MenuItem(#MENU_ASCIITable , Langage(106))
MenuItem(#MENU_StructureViewer, Langage(107))
MenuItem(#MENU_PickColor , Langage(108))
MenuBar()
MenuItem(#MENU_AddTools , Langage(109))
AddTools_AddMenuEntries()
; ---------------------------------- Help
MenuTitle(Langage(19))
MenuItem(#MENU_Help , Langage(20))
Menu_AddHelpFiles()
MenuItem(#MENU_About, Langage(21))
EndIf
CompilerIf #Demo = 1
SetMenuItemState(0, #MENU_Debugger, 1)
DisableMenuItem(#MENU_Debugger, 1)
CompilerEndIf
If DisableRedraw
SendMessage_(*MainWindow, #WM_SETREDRAW, 1, 0)
InvalidateRect_(*MainWindow, 0 ,0) EndIf
EndProcedure
poly, if that is the case you may want to add it with a little snippet in the bug section...PolyVector wrote: If any of you are interested... I was doing some work w/ menus and discovered that using CreateMenu() without calling FreeMenu() first WILL LEAK MEMORY...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: Changing a menu
Ha! The forum is deep
I had this same problem recently and my flicker was happening when I used:
FreeMenu(#mymenu) ;BEFORE
SendMessage_(hW, #WM_SETREDRAW, 0, 0) ; Prevent flickering when recreating menu at runtime
Moving FreeMenu() cmd AFTER the SendMessage() fixed the problem.
Is FreeMenu(#mymenu) still required if I use multiple CreateMenu(#mymenu) 's?
Or will I get a memory leak?
I had this same problem recently and my flicker was happening when I used:
FreeMenu(#mymenu) ;BEFORE
SendMessage_(hW, #WM_SETREDRAW, 0, 0) ; Prevent flickering when recreating menu at runtime
Moving FreeMenu() cmd AFTER the SendMessage() fixed the problem.
Is FreeMenu(#mymenu) still required if I use multiple CreateMenu(#mymenu) 's?
Or will I get a memory leak?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Changing a menu
Ten years on, and still relevant! 
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 


