OpenSubMenu

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

OpenSubMenu

Post by spacebuddy »

I am using OpenSubMenu in my program, in the SubMenu I have 10 entries, I need a way to delete the entries and
be able to insert new entires. :P

I looked at all the menu commands but could not find anything to delete a submenu. :shock:


I am trying to create a recent files for my program, similar to the way TextEditors recent files work. :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OpenSubMenu

Post by Danilo »

Re-building the menu should be cross-platform.

Code: Select all

Global NewList RecentFiles.s()
#RecentFiles = 1000

Procedure BuildMenu()
    If CreateMenu(0, WindowID(0))
        MenuTitle("File")
        OpenSubMenu("Recent files")
            ; Add recent files list
            i=0
            ForEach RecentFiles()
                MenuItem(#RecentFiles+i, RecentFiles()) : i+1
            Next
        CloseSubMenu()
    EndIf
EndProcedure
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OpenSubMenu

Post by spacebuddy »

Danilo,

I tried this, but it not work when application is code signed.

Not sure what entitlement you need to make this work.

Without code sign it work good. :D
Wolfram
Enthusiast
Enthusiast
Posts: 610
Joined: Thu May 30, 2013 4:39 pm

Re: OpenSubMenu

Post by Wolfram »

try this...

Code: Select all

Global Window_0

Global Button_Clear, Button_fill

Global NewList RecentFiles.s()
#RecentFiles = 10

Procedure BuildMenu() ;Builds the Menu
    If CreateMenu(0, WindowID(Window_0))
        MenuTitle("File")
        OpenSubMenu("Recent files")
            ; Add recent files list
            i=0
            ForEach RecentFiles()
                MenuItem(#RecentFiles+i, RecentFiles()) : i+1
            Next
        CloseSubMenu()
    EndIf
EndProcedure
  
Procedure FillSubMenu() ;Put a new Item in the Menu
  
  AddElement(RecentFiles.s())
  RecentFiles.s() = "Item " + Str(ListSize(RecentFiles.s() ))
  
  
  BuildMenu()
EndProcedure

Procedure ClearSubMenu() ;Delets all Items
  ClearList(RecentFiles.s())
  
  BuildMenu()
EndProcedure

    
    
  

Procedure OpenWindow_0(x = 0, y = 0, width = 520, height = 190)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  Button_Clear = ButtonGadget(#PB_Any, 130, 70, 70, 25, "Clear")
  Button_fill = ButtonGadget(#PB_Any, 250, 70, 70, 25, "fill")
  BuildMenu()
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_Clear
          ClearSubMenu()
        Case Button_fill
          FillSubMenu()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False

End
macOS Catalina 10.15.7
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OpenSubMenu

Post by spacebuddy »

Okay, it is working now, thank you all :D :D :D
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OpenSubMenu

Post by spacebuddy »

Found another problem. If you create a "Open Recent" file and the application is SandBox it does not work if
you try to load a file from the drive.

Even if you use the proper entitlements, I think you need extra code for this to work :oops:
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OpenSubMenu

Post by spacebuddy »

I found more information and what is needed to create bookmarks. Here is a good URL that explains what needs to be
done incase anyone else needs this.

http://objcolumnist.com/2012/05/21/secu ... bookmarks/
Wolfram
Enthusiast
Enthusiast
Posts: 610
Joined: Thu May 30, 2013 4:39 pm

Re: OpenSubMenu

Post by Wolfram »

Hi spacebuddy,

do you want a RecentFiles list of the files which do you open in your program,
or do you want a mirror of the System RecentFiles (in the apple menu)?
macOS Catalina 10.15.7
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OpenSubMenu

Post by spacebuddy »

Wolfram , thank you. But I got the open recent working in my program.

My only problem is with SandBox and loading a file without using an OpenDialog. This means I have to use BookMark with SandBoxing
and my brain hurt trying to make this work in my program :D

Trying to translate this to PB

NSError *error = nil;
NSData *bookmarkData = nil;

bookmarkData = [openPanelFileURL
bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
You can now store this bookmark (which is just a NSData object) in any way you choose, as long as you can retrieve it later.

When you want to access this file you need to convert the bookmarkData into a file URL:

NSError *error = nil;
BOOL bookmarkDataIsStale;
NSURL *bookmarkFileURL = nil;

bookmarkFileURL = [NSURL
URLByResolvingBookmarkData:bookmarkData
options:NSURLBookmarkResolutionWithSecurityScope
relativeToURL:nil
bookmarkDataIsStale:&bookmarkDataIsStale
error:&error];
The URL returned includes a “security scope” appended to it (although this cannot be assumed):

file://localhost/Users/ObjColumnist/Desktop/File?applesecurityscope=373861333331353430643963323736623939346438646161643134663339363061396361306534303b30303030303030303b303030303030303030303030303032303b636f6d2e6170706c652e6170702d73616e64626f782e726561642d77726974653b30303030303030313b30653030303030323b303030303030303030306535363566373b2f75736572732f7370656e6365722f6465736b746f702f77617463686564

You then need to tell the OS that you are going access this file URL:

[bookmarkFileURL startAccessingSecurityScopedResource];
Post Reply