Recent Used

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 1298
Joined: Sun May 14, 2017 1:48 am

Recent Used

Post by AZJIO »

An example of how to control the history of menu item selection.

Code: Select all

EnableExplicit

#Window = 0
#Menu = 0

Enumeration Gadget
	#Field
	#btnMenu
	#btnAdd
EndEnumeration

Global NewList RecentUsed.s()
Global MenuMax = -1
Define i, eMenu

Procedure AddLast()
	Protected tmp$
	tmp$ = GetGadgetText(#Field)
	ForEach RecentUsed()
		If tmp$ = RecentUsed()
			MoveElement(RecentUsed(), #PB_List_First)
			ProcedureReturn
		EndIf
	Next
	SelectElement(RecentUsed(), 0)
	If InsertElement(RecentUsed())
		RecentUsed() = tmp$
	EndIf
	While ListSize(RecentUsed()) > 10 ; the maximum number of items in the menu
		LastElement(RecentUsed())
		DeleteElement(RecentUsed()) 
	Wend
	MenuMax = ListSize(RecentUsed()) - 1
	
EndProcedure


If OpenWindow(#Window, 0, 0, 470, 100, "Enter text and click use", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	StringGadget(#Field, 10, 10, 400, 28 , "")
	ButtonGadget(#btnMenu, 415, 10, 28, 28, Chr($25BC))
	ButtonGadget(#btnAdd, 185, 45, 70, 28, "Use")
	
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Menu
				eMenu = EventMenu()
				Select eMenu
					Case 0 To MenuMax
						SetGadgetText(#Field, GetMenuItemText(#Menu , eMenu))
				EndSelect
			Case #PB_Event_Gadget
				Select EventGadget()
					Case #btnAdd
						AddLast()
					Case #btnMenu
						If IsMenu(#Menu)
							FreeMenu(#Menu)
						EndIf
						If CreatePopupMenu(#Menu)
							i = 0
							ForEach RecentUsed()
								MenuItem(i, RecentUsed())
								i + 1
							Next
						EndIf
						DisplayPopupMenu(#Menu, WindowID(#Window), WindowX(#Window, #PB_Window_InnerCoordinate) + 10, WindowY(#Window, #PB_Window_InnerCoordinate) + 38)
				EndSelect
			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Recent Used

Post by Kwai chang caine »

Works here :D
A piece of code i not need to write :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply