Page 1 of 1
Changing a menu
Posted: Thu Dec 04, 2003 1:19 pm
by Dr_Pixel
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.
Re: Changing a menu
Posted: Thu Dec 04, 2003 5:50 pm
by Doobrey
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?
Posted: Fri Dec 05, 2003 1:24 am
by Dr_Pixel
Well, flickering was not really the right term - the window resizes itself when I free the original menu (to eliminate the menu area at the top) and then resizes again to make space at the top for the new menu
Posted: Fri Dec 05, 2003 11:36 am
by GPI
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
GPI
Posted: Thu Jan 01, 2004 11:02 am
by PolyVector
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...
It will act properly for some time and then suddenly the menu will fail to create...
I'm running the latest ver. of PB on XP
Posted: Fri Jan 02, 2004 4:10 am
by kenmo
As long as your recent folders list always stays the same length, cant you just change each slot's text?
Posted: Fri Jan 02, 2004 5:29 am
by fsw
The search function in this forum is your friend:
viewtopic.php?t=4986&highlight=files+menu
Hope it's what you need.
Posted: Fri Jan 02, 2004 2:00 pm
by Dr_Pixel
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.
Posted: Mon Dec 06, 2004 6:30 am
by wayne1
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.
Use SendMessage_() at the beginning and the end of the menu creation.
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
Posted: Mon Dec 06, 2004 6:14 pm
by blueznl
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...
poly, if that is the case you may want to add it with a little snippet in the bug section...
Re: Changing a menu
Posted: Mon Aug 05, 2013 4:00 am
by skywalk
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?
Re: Changing a menu
Posted: Mon Aug 05, 2013 12:12 pm
by TI-994A
Ten years on, and still relevant!
