Changing a menu

Just starting out? Need help? Post your questions and find answers here.
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Changing a menu

Post 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.
Dr Pixel
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Re: Changing a menu

Post 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?
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Post 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
Dr Pixel
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post 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
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post 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
User avatar
kenmo
Addict
Addict
Posts: 2051
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

As long as your recent folders list always stays the same length, cant you just change each slot's text?
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

The search function in this forum is your friend:
viewtopic.php?t=4986&highlight=files+menu

Hope it's what you need.
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Post 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.
Dr Pixel
wayne1
User
User
Posts: 13
Joined: Sat Apr 26, 2003 4:23 am
Location: United States of America

Post 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
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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...
( 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... )
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Changing a menu

Post by skywalk »

Ha! The forum is deep :shock:
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
User avatar
TI-994A
Addict
Addict
Posts: 2749
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Changing a menu

Post by TI-994A »

Ten years on, and still relevant! :lol:
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 :D
Post Reply