Page 2 of 2

Re: Examples for Documentation

Posted: Wed Sep 27, 2023 8:23 pm
by Andre
Added the examples for following commands:
- ChangeSysTrayIcon
- AddSysTrayIcon
- AddKeyboardShortcut

All with additional comments and all in a bit adapted / simplified form, e.g. for ChangeSysTrayIcon I've used a randomly sized red dot on the yellow background.

Again thanks a lot, AZJIO :D

Re: Examples for Documentation

Posted: Thu Nov 02, 2023 2:08 pm
by Piero
I would like to see some "quick example" of how to use WriteProgramString(N) and #PB_Program_Eof ... :wink:

Code: Select all

Procedure Shell(ShellCommand$, AddReturns = #True)
   Protected Err$, tmper$, Output$, shell, Exc.w = -1 ; -1 on failed launch
   shell = RunProgram("/bin/sh","","",
      #PB_Program_Open|#PB_Program_Write|#PB_Program_Read|#PB_Program_Error )
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      While ProgramRunning(shell)
         If AvailableProgramOutput(shell)
            Output$ + ReadProgramString(shell)
            If AddReturns : Output$ + Chr(13) : EndIf
         EndIf
         tmper$ = ReadProgramError(shell)
         If tmper$ : Err$ + tmper$ + Chr(13) : EndIf
      Wend
      Exc = ProgramExitCode(shell) : CloseProgram(shell)
   EndIf
   Sh\Out = Output$ : Sh\Err = Err$ : Sh\ExC = Exc
EndProcedure

Re: Examples for Documentation

Posted: Tue Nov 21, 2023 2:47 pm
by AZJIO
SaveImage
It took me a while to figure out that I needed to use UsePNGImageEncoder(). The description does not say about it. (on the SaveSprite page, also specify)

Code: Select all

#Image = 0
Define tmp$
UsePNGImageEncoder()

If CreateImage(#Image, 33, 33)
	If StartDrawing(ImageOutput(#Image))
		DrawingMode(#PB_2DDrawing_Transparent)
		Box(0, 0, 33, 33, $FF0000)
		StopDrawing()
	EndIf
	tmp$ = SaveFileRequester("Select file to save", GetCurrentDirectory(), "*.png|*.png", 0)
	If Asc(tmp$)
		tmp$ + ".png"
		If SaveImage(#Image, tmp$, #PB_ImagePlugin_PNG)
			MessageRequester("File created", tmp$)
		Else
			MessageRequester("Error", "Failed to save file")
		EndIf
	EndIf
	FreeImage(#Image)
EndIf

Re: Examples for Documentation

Posted: Tue Nov 21, 2023 4:30 pm
by juergenkulow

Re: Examples for Documentation

Posted: Tue Nov 21, 2023 5:12 pm
by AZJIO
ProgramParameter() (version 2)
The most practical and elementary example. After all, it is often necessary to open a file in the program.

Code: Select all

Define path$, Text$

If OpenWindow(0, 0, 0, 520, 510, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	EditorGadget(0, 5, 5, 510, 500, #PB_Editor_WordWrap)
	path$ = ProgramParameter()
	If Asc(path$) And FileSize(path$) >= 0
		If ReadFile(0, path$)
			Text$ = ReadString(0, #PB_File_IgnoreEOL)
		EndIf
		SetGadgetText(0, Text$)
	EndIf
	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Examples for Documentation

Posted: Fri Dec 01, 2023 9:44 pm
by juergenkulow
UsePNGImageDecoder

Code: Select all

; Select and show PNG File until ALT-F4 is pressed. 
UsePNGImageDecoder() ;Activates support for PNG images, here for LoadImage in line 9. 
ExamineDesktops()    ;Determine information about the screens the number of pixels, 
                     ;here For DesktopHeight And DesktopWidth.  
#screen=0            ;Select a screen.
File$ = OpenFileRequester("Select PNG", #PB_Compiler_Home+"logo.png",
                          "PNG Picture (*.png)|*.png|Alle Dateien (*.*)|*.*",0) ;Select a file.
If File$<>""         ;If a file has been selected.
  image=LoadImage(#PB_Any,File$)   ;Load the specified image from the file with the name File$.
  Width=ImageWidth(image)          ;Determine the width in pixels of image.
  Height=ImageHeight(image)        ;Determine the width in pixels of image.
  If Width>DesktopWidth(#screen) Or Height>DesktopHeight(#screen) ;If the image is larger than the screen.
    Width=DesktopWidth(#screen)    ;The width becomes the width of the selected screen and 
    Height=DesktopHeight(#screen)  ;the height becomes the height of the selected screen.
  EndIf
  OpenWindow(0,0,0,Width/DesktopResolutionX(),Height/DesktopResolutionY(),"ShowPNG",#PB_Window_BorderLess) 
                                   ;Open a window without a frame in the top left-hand corner.
  ImageGadget(0,0,0,Width,Height,ImageID(image));Create an image gadget in the top left-hand corner of the window.
  Repeat
  Until WaitWindowEvent()=#PB_Event_CloseWindow ;Waits until an event occurs. When ALT-F4 is pressed, 
                                                ;end the loop and exit the program.
EndIf 
Documentation ImagePlugin.txt Line 156

Re: Examples for Documentation

Posted: Mon Feb 12, 2024 12:52 pm
by Andre
AZJIO wrote: Tue Nov 21, 2023 2:47 pm SaveImage
It took me a while to figure out that I needed to use UsePNGImageEncoder(). The description does not say about it. (on the SaveSprite page, also specify)

Code: Select all

#Image = 0
Define tmp$
UsePNGImageEncoder()

If CreateImage(#Image, 33, 33)
	If StartDrawing(ImageOutput(#Image))
		DrawingMode(#PB_2DDrawing_Transparent)
		Box(0, 0, 33, 33, $FF0000)
		StopDrawing()
	EndIf
	tmp$ = SaveFileRequester("Select file to save", GetCurrentDirectory(), "*.png|*.png", 0)
	If Asc(tmp$)
		tmp$ + ".png"
		If SaveImage(#Image, tmp$, #PB_ImagePlugin_PNG)
			MessageRequester("File created", tmp$)
		Else
			MessageRequester("Error", "Failed to save file")
		EndIf
	EndIf
	FreeImage(#Image)
EndIf
I will add a slightly modified example to SaveImage() command:

Code: Select all

  UsePNGImageEncoder()
  
  If CreateImage(0, 33, 33)
    If StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_Transparent)
        Box(0, 0, 33, 33, $FF0000) ; draw a blue box and save it as image file below
      StopDrawing()
    EndIf
    file$ = SaveFileRequester("Select file to save", GetCurrentDirectory(), "*.png|*.png", 0)
    If file$
      If GetExtensionPart(file$) = ""
        file$ + ".png"  ; add the file extension if needed
      EndIf
      If SaveImage(0, file$, #PB_ImagePlugin_PNG)
        MessageRequester("File created", file$)
      Else
        MessageRequester("Error", "Failed to save file")
      EndIf
    EndIf
    FreeImage(0)
  EndIf

Re: Examples for Documentation

Posted: Mon Feb 12, 2024 1:24 pm
by Andre
AZJIO wrote: Tue Nov 21, 2023 5:12 pm ProgramParameter() (version 2)
The most practical and elementary example. After all, it is often necessary to open a file in the program.

Code: Select all

Define path$, Text$

If OpenWindow(0, 0, 0, 520, 510, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	EditorGadget(0, 5, 5, 510, 500, #PB_Editor_WordWrap)
	path$ = ProgramParameter()
	If Asc(path$) And FileSize(path$) >= 0
		If ReadFile(0, path$)
			Text$ = ReadString(0, #PB_File_IgnoreEOL)
		EndIf
		SetGadgetText(0, Text$)
	EndIf
	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Thanks, AZJIO! Added it in a slightly simplified version:

Code: Select all

  ; Call the compiled program from command-line with the file to display as parameter
  If OpenWindow(0, 0, 0, 520, 510, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 5, 5, 510, 500, #PB_Editor_WordWrap)
    path$ = ProgramParameter()
    If FileSize(path$) >= 0
      If ReadFile(0, path$)
        Text$ = ReadString(0, #PB_File_IgnoreEOL)
      EndIf
      SetGadgetText(0, Text$)
    EndIf
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf

Re: Examples for Documentation

Posted: Mon Feb 12, 2024 1:30 pm
by Andre
juergenkulow wrote: Fri Dec 01, 2023 9:44 pm UsePNGImageDecoder

Code: Select all

; Select and show PNG File until ALT-F4 is pressed. 
UsePNGImageDecoder() ;Activates support for PNG images, here for LoadImage in line 9. 
ExamineDesktops()    ;Determine information about the screens the number of pixels, 
                     ;here For DesktopHeight And DesktopWidth.  
#screen=0            ;Select a screen.
File$ = OpenFileRequester("Select PNG", #PB_Compiler_Home+"logo.png",
                          "PNG Picture (*.png)|*.png|Alle Dateien (*.*)|*.*",0) ;Select a file.
If File$<>""         ;If a file has been selected.
  image=LoadImage(#PB_Any,File$)   ;Load the specified image from the file with the name File$.
  Width=ImageWidth(image)          ;Determine the width in pixels of image.
  Height=ImageHeight(image)        ;Determine the width in pixels of image.
  If Width>DesktopWidth(#screen) Or Height>DesktopHeight(#screen) ;If the image is larger than the screen.
    Width=DesktopWidth(#screen)    ;The width becomes the width of the selected screen and 
    Height=DesktopHeight(#screen)  ;the height becomes the height of the selected screen.
  EndIf
  OpenWindow(0,0,0,Width/DesktopResolutionX(),Height/DesktopResolutionY(),"ShowPNG",#PB_Window_BorderLess) 
                                   ;Open a window without a frame in the top left-hand corner.
  ImageGadget(0,0,0,Width,Height,ImageID(image));Create an image gadget in the top left-hand corner of the window.
  Repeat
  Until WaitWindowEvent()=#PB_Event_CloseWindow ;Waits until an event occurs. When ALT-F4 is pressed, 
                                                ;end the loop and exit the program.
EndIf 
Documentation ImagePlugin.txt Line 156
Thank you for the suggestion, Jürgen!
But this one looks a bit "overloaded" for a simple example in the command description. Maybe it could become a more extended example (e.g. with proportional image resize) for the PureBasic Examples drawer. Or it should be a lot shorter for the UsePNGImageDecoder() docs...

Re: Examples for Documentation

Posted: Fri Mar 08, 2024 5:20 am
by AZJIO
FinishDirectory
Another example of searching for files without recursion.

Code: Select all

Procedure FileSearch(List Files.s(), dir.s, mask.s = "*", depth = 130)
	Protected Name.s, c
	Protected Dim hDir(depth)
	Protected Dim SearchPath.s(depth)

	If Right(dir, 1) <> #PS$
		dir + #PS$
	EndIf

	SearchPath(c) = dir
	hDir(c) = ExamineDirectory(#PB_Any, dir, mask)
	If Not hDir(c)
		ProcedureReturn
	EndIf

	Repeat
		While NextDirectoryEntry(hDir(c))
			Name = DirectoryEntryName(hDir(c))
			If Name = "." Or Name = ".."
				Continue
			EndIf
			If DirectoryEntryType(hDir(c)) = #PB_DirectoryEntry_Directory
				If c >= depth
					Continue
				EndIf
				dir = SearchPath(c)
				c + 1
				SearchPath(c) = dir + Name + #PS$
				hDir(c) = ExamineDirectory(#PB_Any, SearchPath(c), mask)
				If Not hDir(c)
					c - 1
				EndIf
			Else
				If AddElement(Files())
					Files() = SearchPath(c) + Name
				EndIf
			EndIf
		Wend
		FinishDirectory(hDir(c))
		c - 1
	Until c < 0
EndProcedure

Define NewList Files.s()
FileSearch(Files(), GetTemporaryDirectory(), "*", 130)
ForEach Files()
	Debug Files()
Next
AddElement/NewList (link)

Code: Select all

Define NewList MyList.s()
AddElement(MyList())
MyList() = "WORD"

; get a pointer to the string of a list element, in functions where a pointer to the string is required
If CompareMemoryString(PeekI(@MyList()), @"Word", #PB_String_NoCase) = #PB_String_Equal
	Debug 1
Else
	Debug 0
EndIf

Define *ptr.string
*ptr = @MyList() ; get pointer and access to string without copying data
Debug *ptr\s