Page 1 of 2

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

Posted: Sun Jul 09, 2023 8:10 pm
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




Re: SaveFileRequester()

Posted: Sun Jul 09, 2023 8:20 pm
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 

Re: SaveFileRequester()

Posted: Sun Jul 09, 2023 8:22 pm
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.

Re: SaveFileRequester()

Posted: Sun Jul 09, 2023 8:38 pm
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

Re: SaveFileRequester()

Posted: Sun Jul 09, 2023 8:43 pm
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

Re: SaveFileRequester()

Posted: Mon Jul 10, 2023 9:26 am
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?

Re: SaveFileRequester()

Posted: Mon Jul 10, 2023 9:40 am
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.

Re: SaveFileRequester()

Posted: Mon Jul 10, 2023 3:13 pm
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

Re: SaveFileRequester()

Posted: Mon Jul 10, 2023 7:29 pm
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

Re: SaveFileRequester()

Posted: Mon Jul 10, 2023 8:01 pm
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

Re: SaveFileRequester()

Posted: Tue Jul 11, 2023 4:19 am
by Ktim07
thanks @jassing & @infratec for the codes

html

Posted: Tue Jul 11, 2023 4:27 am
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


Re: html

Posted: Tue Jul 11, 2023 5:03 am
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.

Re: SaveFileRequester()

Posted: Tue Jul 11, 2023 10:58 am
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.

Re: SaveFileRequester()

Posted: Tue Jul 11, 2023 11:38 am
by Ktim07
Olli wrote: Tue Jul 11, 2023 10:58 am
I am not sure that I understand your explanation.