port PB GUI app to Android as .apk SaveFileRequester()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

port PB GUI app to Android as .apk SaveFileRequester()

Post by Ktim07 »

Hallo members, I am new here and trying to learn some coding, I was trying to learn about the actual work done by menu items and how to put those accomplishments in the code, for example the save function, I made a tiny code where I want to save action to save what is put what is on the editor as a .txt file. Connecting what is in the manual into the actual code is not easy, Please help.

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration
  #MainMenu
EndEnumeration

Enumeration Gadgets
  #Editor_1
EndEnumeration

Define iEvent.i

Declare Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)

Procedure Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Window_0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)

    If CreateMenu(#MainMenu, WindowID(#Window_0))
      MenuTitle("File")
        MenuItem(0, "Save")
        MenuBar()
        MenuItem(1, "&Quit")      
    EndIf

    EditorGadget(#Editor_1, 160, 50, 230, 200)
      AddGadgetItem(#Editor_1, -1, "Editor_1")
  EndIf
EndProcedure

Open_Window_0()

Repeat
  iEvent = WaitWindowEvent()
  Select iEvent
    Case #PB_Event_Menu
      Select EventMenu()
        Case 1   ;Quit
          End
        Case 2   ;Save
          
          MessageRequester("Save", "Menu Sample", 0)
        Default
          MessageRequester("Information", "ToolBar Or Menu ID: " + Str(EventMenu()), 0)
          
          
          
          
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect

    Case #PB_Event_CloseWindow
      End
  EndSelect
ForEver



Last edited by Ktim07 on Sun Jul 30, 2023 7:01 pm, edited 2 times in total.
Serious souls !!!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: SaveFileRequester()

Post by jassing »

Code: Select all

  Case #PB_Event_Menu
      Select EventMenu()
        Case 1   ;Quit
          End
        Case 0   ;Save
          Define cFile.s = SaveFileRequester("Save","sample.txt","Text File|*.txt",0)
          If cFile 
            CreateFile(0,cfile)
            WriteString(0,GetGadgetText(#Editor_1))
            CloseFile(0)
          EndIf 
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SaveFileRequester()

Post by infratec »

Try this:

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Menue
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MenuSave
  #MenuQuit
EndEnumeration

Enumeration Gadgets
  #Editor_1
EndEnumeration



Procedure Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Window_0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)

    If CreateMenu(#MainMenu, WindowID(#Window_0))
      MenuTitle("File")
        MenuItem(#MenuSave, "Save")
        MenuBar()
        MenuItem(#MenuQuit, "&Quit")      
    EndIf

    EditorGadget(#Editor_1, 160, 50, 230, 200)
    AddGadgetItem(#Editor_1, -1, "Editor_1")
  EndIf
EndProcedure


Define.i iEvent, Quit, File
Define Filename$

Open_Window_0()

Repeat
  iEvent = WaitWindowEvent()
  Select iEvent
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuQuit
          PostEvent(#PB_Event_CloseWindow)
          
        Case 2   ;Save
          Filename$ = SaveFileRequester("Choose a file", "", "TXT|*.txt", 0)
          If Filename$
            File = CreateFile(#PB_Any, Filename$)
            If File
              WriteString(File, GetGadgetText(#Editor_1))
              CloseFile(File)
            EndIf
          EndIf
          
        Default
          MessageRequester("Information", "ToolBar Or Menu ID: " + Str(EventMenu()), 0)
          
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect

    Case #PB_Event_CloseWindow
      If MessageRequester("Sure", "Really exit?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
        Quit = #True
      EndIf
      
  EndSelect
    
Until Quit
Added also some small improvements.
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

Re: SaveFileRequester()

Post by Ktim07 »

jassing wrote: Sun Jul 09, 2023 8:20 pm

Code: Select all

  Case #PB_Event_Menu
      Select EventMenu()
        Case 1   ;Quit
          End
        Case 0   ;Save
          Define cFile.s = SaveFileRequester("Save","sample.txt","Text File|*.txt",0)
          If cFile 
            CreateFile(0,cfile)
            WriteString(0,GetGadgetText(#Editor_1))
            CloseFile(0)
          EndIf 
:D :D :D thanks
Serious souls !!!
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

Re: SaveFileRequester()

Post by Ktim07 »

infratec wrote: Sun Jul 09, 2023 8:22 pm Try this:

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Menue
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MenuSave
  #MenuQuit
EndEnumeration

Enumeration Gadgets
  #Editor_1
EndEnumeration


Until Quit
Added also some small improvements.
Thanks alot, I tried this, save was still bringing something else, I then pasted that code up, now it works
Serious souls !!!
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

Re: SaveFileRequester()

Post by Ktim07 »

jassing wrote: Sun Jul 09, 2023 8:20 pm

Code: Select all

  Case #PB_Event_Menu
      Select EventMenu()
        Case 1   ;Quit
          End
        Case 0   ;Save
          Define cFile.s = SaveFileRequester("Save","sample.txt","Text File|*.txt",0)
          If cFile 
            CreateFile(0,cfile)
            WriteString(0,GetGadgetText(#Editor_1))
            CloseFile(0)
          EndIf 
How this code edited, so that the program auto saves after some time to void loss if the program closes unexpectedly. then does the code above function the same for "Save As" and "Save as a copy", or something changes?
Serious souls !!!
Olli
Addict
Addict
Posts: 1198
Joined: Wed May 27, 2020 12:26 pm

Re: SaveFileRequester()

Post by Olli »

You can add a timer.
AddWindowTimer().
First, you move the code you want to execute :

Code: Select all

Global cFile.s

Procedure SaveMyFile()
          If cFile 
            CreateFile(0,cfile)
            WriteString(0,GetGadgetText(#Editor_1))
            CloseFile(0)
          EndIf
EndProcedure
Note that you get an address number of this code :

Code: Select all

Debug @SaveMyFile()
This address will be required after the function AddWindowTimer()
After this function you can bind the timer event :BindWindowEvent() in order to link the timing event with the procedure above.
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

Re: SaveFileRequester()

Post by Ktim07 »

Olli wrote: Mon Jul 10, 2023 9:40 am You can add a timer.
AddWindowTimer().
First, you move the code you want to execute :

Code: Select all

Global cFile.s

Procedure SaveMyFile()
          If cFile 
            CreateFile(0,cfile)
            WriteString(0,GetGadgetText(#Editor_1))
            CloseFile(0)
          EndIf
EndProcedure
Note that you get an address number of this code :

Code: Select all

Debug @SaveMyFile()
This address will be required after the function AddWindowTimer()
After this function you can bind the timer event :BindWindowEvent() in order to link the timing event with the procedure above.
Great information. Thanks
Serious souls !!!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: SaveFileRequester()

Post by jassing »

You don't need the address or bindevent

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Menue
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MenuSave
  #MenuQuit
EndEnumeration

Enumeration Gadgets
  #Editor_1
EndEnumeration
Enumeration ; timers
  #timerSave
EndEnumeration

Procedure SaveFile()  
  Static Filename$ 
  
  If Filename$ = "" 
    Filename$= SaveFileRequester("Choose a file", "", "TXT|*.txt", 0)
  EndIf 
  If Filename$
    Protected File = CreateFile(#PB_Any, Filename$)
    If File
      WriteString(File, GetGadgetText(#Editor_1))
      CloseFile(File)
    EndIf
  EndIf
EndProcedure

Procedure Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Window_0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
    AddWindowTimer(#Window_0,#TimerSave, 1000)
    If CreateMenu(#MainMenu, WindowID(#Window_0))
      MenuTitle("File")
      MenuItem(#MenuSave, "Save")
      MenuBar()
      MenuItem(#MenuQuit, "&Quit")      
    EndIf
    
    EditorGadget(#Editor_1, 160, 50, 230, 200)
    AddGadgetItem(#Editor_1, -1, "Editor_1")
  EndIf
EndProcedure


Define.i iEvent, Quit, File
Define Filename$

Open_Window_0()

Repeat
  iEvent = WaitWindowEvent()
  Select iEvent
    Case #PB_Event_Timer
      Select EventTimer()
        Case #timerSave
          SaveFile()
      EndSelect
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuQuit
          PostEvent(#PB_Event_CloseWindow)
          
        Case #MenuSave ;Save
          SaveFile()
        Default
          MessageRequester("Information", "ToolBar Or Menu ID: " + Str(EventMenu()), 0)
          
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
      
    Case #PB_Event_CloseWindow
      If MessageRequester("Sure", "Really exit?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
        Quit = #True
      EndIf
      
  EndSelect
  
Until Quit
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SaveFileRequester()

Post by infratec »

Wih 'Save as ...'

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Menue
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MenuSave
  #MenuSaveAs
  #MenuQuit
EndEnumeration

Enumeration Gadgets
  #Editor_1
EndEnumeration
Enumeration ; timers
  #timerSave
EndEnumeration

Procedure.s SaveFile(Filename$="")
  
  Protected File.i
  
  
  If Filename$ = "" 
    Filename$ = SaveFileRequester("Choose a file", "", "TXT|*.txt", 0)
    If LCase(GetExtensionPart(Filename$)) <> "txt"
      Filename$ + ".txt"
    EndIf
  EndIf
  
  If Filename$
    File = CreateFile(#PB_Any, Filename$)
    If File
      WriteString(File, GetGadgetText(#Editor_1))
      CloseFile(File)
    EndIf
  EndIf
  
  ProcedureReturn Filename$
  
EndProcedure

Procedure Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Window_0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
    AddWindowTimer(#Window_0,#TimerSave, 1000)
    If CreateMenu(#MainMenu, WindowID(#Window_0))
      MenuTitle("File")
      MenuItem(#MenuSave, "Save")
      MenuItem(#MenuSaveAs, "Save as ...")
      MenuBar()
      MenuItem(#MenuQuit, "&Quit")      
    EndIf
    
    EditorGadget(#Editor_1, 160, 50, 230, 200)
    AddGadgetItem(#Editor_1, -1, "Editor_1")
  EndIf
EndProcedure


Define.i iEvent, Quit, File
Define Filename$

Open_Window_0()

Repeat
  iEvent = WaitWindowEvent()
  Select iEvent
    Case #PB_Event_Timer
      Select EventTimer()
        Case #timerSave
          If Filename$ <> ""
            PostEvent(#PB_Event_Menu, #Window_0, #MenuSave)
          EndIf
      EndSelect
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MenuQuit
          PostEvent(#PB_Event_CloseWindow)
          
        Case #MenuSave ;Save
          If Filename$ <> ""
            SaveFile(Filename$)
          Else
            PostEvent(#PB_Event_Menu, #Window_0, #MenuSaveAs)
          EndIf
          
        Case #MenuSaveAs
          Filename$ = SaveFile()
          
        Default
          MessageRequester("Information", "ToolBar Or Menu ID: " + Str(EventMenu()), 0)
          
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
      
    Case #PB_Event_CloseWindow
      If MessageRequester("Sure", "Really exit?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
        Quit = #True
      EndIf
      
  EndSelect
  
Until Quit
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

Re: SaveFileRequester()

Post by Ktim07 »

thanks @jassing & @infratec for the codes
Serious souls !!!
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

html

Post by Ktim07 »

is there a way to edit the link in the code below, to instead open and read a html file eg one named, thanks.html
with in the compiled program.

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Gadgets
  #WebView_1
EndEnumeration

Define iEvent.i

Declare Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)

Procedure Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Window_0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
    WebGadget(#WebView_1, 0, 0, 640, 480, "https://www.purebasic.fr/english/index.php")
  EndIf
EndProcedure

Open_Window_0()

Repeat
  iEvent = WaitWindowEvent()
  Select iEvent
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect

    Case #PB_Event_CloseWindow
      End
  EndSelect
ForEver

Serious souls !!!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: html

Post by jassing »

Ktim07 wrote: Tue Jul 11, 2023 4:27 am is there a way to edit the link in the code below, to instead open and read a html file eg one named, thanks.html

Code: Select all

 WebGadget(#WebView_1, 0, 0, 640, 480, "file://c:\temp\thanks.html")
Ktim07 wrote: Tue Jul 11, 2023 4:27 am with in the compiled program.
I'm not sure what you mean by this...
If you mean text stored in a datasection...

Code: Select all

DataSection
  beginThanksHTML:
  IncludeBinary "c:\temp\thanks.html"
  endThanksHTML:
EndDataSection

Procedure Open_Window_0(X = 0, Y = 0, Width = 640, Height = 480)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Window_0", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
    WebGadget(#WebView_1, 0, 0, 640, 480, "")
    SetGadgetItemText(#WebView_1,#PB_Web_HtmlCode,PeekS(?beginThanksHTML,?endThanksHTML-?beginThanksHTML,#PB_Ascii))
  EndIf
EndProcedure
NOTE: My HTML file was ascii, you may need to adjust the peeks() flag.
Olli
Addict
Addict
Posts: 1198
Joined: Wed May 27, 2020 12:26 pm

Re: SaveFileRequester()

Post by Olli »

You will have to replace the direct string content "file://c:\temp\thanks.html" with a personnal string name myLink$ for example.

And you add first a StringGadget() to allow the client to type and edit a string.

The string content typed by the client is got by the following function :

Code: Select all

myLink$ = GetGadgetText(linkStringGadget)
Note that a [Update] button is better for the design (see ButtonGadget() in the doc) and the process : this button prevents the program to update the web page 100 times per seconds (on every loop).

But other more clever update ways are allowed : for example, after a client string change, wait one second to update, then wait a new string change.
User avatar
Ktim07
User
User
Posts: 21
Joined: Wed Jun 28, 2023 7:55 pm

Re: SaveFileRequester()

Post by Ktim07 »

Olli wrote: Tue Jul 11, 2023 10:58 am
I am not sure that I understand your explanation.
Serious souls !!!
Post Reply