### ListViewGadget Usage Example Source: https://www.purebasic.com/documentation/gadget/listviewgadget.html A complete example demonstrating how to create a window, populate a ListViewGadget, and set the initial selection. ```PureBasic If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) ListViewGadget(0, 10, 10, 250, 120) For a = 1 To 12 AddGadgetItem (0, -1, "Item " + Str(a) + " of the Listview") ; define listview content Next SetGadgetState(0, 9) ; set (beginning with 0) the tenth item as the active one Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### PureBasic Window Creation and Initial Plot Source: https://www.purebasic.com/documentation/reference/ug_pb_any1.html This code initializes the application by creating the first window, resizing it, and plotting its initial content. This setup is usually performed once at the beginning of the application before the main event loop starts. ```purebasic ; Create the first window. EventWindow = CreatePolygonWindow() ResizePolygonWindow(EventWindow) PlotPolygon(EventWindow) ``` -------------------------------- ### Subsystem Syntax and Example Source: https://www.purebasic.com/documentation/reference/compilerfunctions.html Syntax and usage example for checking active compiler subsystems. ```PureBasic Result = Subsystem() ``` ```PureBasic CompilerIf Subsystem("OpenGL") Debug "Compiling with the OpenGL subsystem" CompilerEndIf ``` -------------------------------- ### SpinGadget Usage Example Source: https://www.purebasic.com/documentation/gadget/spingadget.html A complete example demonstrating the creation and event handling of a SpinGadget. ```PureBasic If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) SpinGadget (0, 20, 20, 100, 25, 0, 1000) SetGadgetState (0, 5) : SetGadgetText(0, "5") ; set initial value Repeat Event = WaitWindowEvent() If Event = #PB_Event_Gadget If EventGadget() = 0 SetGadgetText(0, Str(GetGadgetState(0))) EndIf EndIf Until Event = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### ProgressBarGadget Example Source: https://www.purebasic.com/documentation/gadget/progressbargadget.html A complete example demonstrating the creation of standard, smooth, and vertical progress bars. ```PureBasic If OpenWindow(0, 0, 0, 320, 160, "ProgressBarGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) TextGadget (3, 10, 10, 250, 20, "ProgressBar Standard (50/100)", #PB_Text_Center) ProgressBarGadget(0, 10, 30, 250, 30, 0, 100) SetGadgetState (0, 50) ; set 1st progressbar (ID = 0) to 50 of 100 TextGadget (4, 10, 70, 250, 20, "ProgressBar Smooth (50/200)", #PB_Text_Center) ProgressBarGadget(1, 10, 90, 250, 30, 0, 200, #PB_ProgressBar_Smooth) SetGadgetState (1, 50) ; set 2nd progressbar (ID = 1) to 50 of 200 TextGadget (5, 100,135, 200, 20, "ProgressBar Vertical (100/300)", #PB_Text_Right) ProgressBarGadget(2, 270, 10, 30, 120, 0, 300, #PB_ProgressBar_Vertical) SetGadgetState (2, 100) ; set 3rd progressbar (ID = 2) to 100 of 300 Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow EndIf ``` -------------------------------- ### Basic PureBasic Window and Gadget Setup Source: https://www.purebasic.com/documentation/Examples/GadgetSplitter.pb.html Initializes a PureBasic window with specific dimensions and title, and includes basic gadget setup like TextGadgets. ```purebasic **If** OpenWindow(0, 100, 120, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) TextGadget( 9, 350, 5, 50, 30, "Pos:") TextGadget(10, 400, 5, 50, 30, "-") TextGadget(11, 450, 5, 50, 30, "-") TextGadget(12, 500, 5, 50, 30, "-") **EndIf** **End** ``` -------------------------------- ### Get Array Size with ArraySize() Source: https://www.purebasic.com/documentation/array/arraysize.html Demonstrates how to get the size of a 1D and a 3D array using ArraySize(). For multidimensional arrays, specify the dimension to get its size. The first dimension starts from 1. ```purebasic Dim MyArray.l(10) Debug ArraySize(MyArray()) ; will print '10' Dim MultiArray.l(10, 20, 30) Debug ArraySize(MultiArray(), 2) ; will print '20' Dim MultiArray2.l(2,2,2) For n = 0 To ArraySize(MultiArray2(),2) MultiArray2(0,n,0) = n+1 Next n Debug MultiArray2(0,0,0) ; will print '1' Debug MultiArray2(0,1,0) ; will print '2' Debug MultiArray2(0,2,0) ; will print '3' Debug ArraySize(MultiArray2(),2) ; will print '2' ``` -------------------------------- ### Load and Play Sound Example Source: https://www.purebasic.com/documentation/sound/loadsound.html Demonstrates initializing the sound system, registering a decoder, loading an OGG file, and playing it in a loop. ```PureBasic **If** InitSound() ; Initialize Sound system UseOGGSoundDecoder() ; Use ogg files ; Loads a sound from a file **If** LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg") PlaySound(0, #PB_Sound_Loop) ; Start playing the sound in a loop MessageRequester("Info", "Ok to stop.") FreeSound(0) ; The sound is freed **EndIf** **End** **Else** **Debug** "Warning! The sound environment couldn't be initialized. So no sound commands can be used..." **EndIf** ``` -------------------------------- ### Get Primary Desktop Name - PureBasic Source: https://www.purebasic.com/documentation/desktop/desktopname.html Call ExamineDesktops() before using DesktopName() to get desktop information. This example retrieves and displays the name of the primary desktop. ```purebasic ExamineDesktops() MessageRequester("Display Information", "Primary desktop name = "+DesktopName(0)) ``` -------------------------------- ### ResumeSound() Example Source: https://www.purebasic.com/documentation/sound/resumesound.html Demonstrates how to initialize the sound system, load a sound, play it, pause it, and then resume it using ResumeSound(). Ensure the sound environment is initialized before using sound commands. Sounds loaded with #PB_Sound_Streaming are not supported. ```purebasic **If** InitSound() ; Initialize Sound system UseOGGSoundDecoder() ; Use ogg files ; Loads a sound from a file **If** LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg") ; The sound is playing PlaySound(0, #PB_Sound_Loop) MessageRequester("Info", "Ok to pause.") PauseSound(0) ; Pause MessageRequester("Info", "Ok to resume.") ResumeSound(0) ; Resume MessageRequester("Info", "Ok to stop.") FreeSound(0) ; The sound is freed **End** **EndIf** **Else** **Debug** "Warning! The sound environment couldn't be initialized. So no sound commands can be used..." **EndIf** ``` -------------------------------- ### Main Window Creation and Menu Setup Source: https://www.purebasic.com/documentation/Examples/MDI_ImageViewer.pb.html Creates the main application window and sets up its menu bar with file and window management options. ```purebasic #WindowFlags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget **If** OpenWindow(#WINDOW, 0, 0, 800, 600, "MDI ImageViewer", #WindowFlags) **If** CreateMenu(#MENU, WindowID(#WINDOW)) MenuTitle("File") MenuItem(#MENU_Open, "Open") MenuItem(#MENU_Close, "Close") MenuItem(#MENU_CloseAll, "Close All") MenuBar() MenuItem(#MENU_QUit, "Quit") MenuTitle("Windows") MenuItem(#MENU_TileVertically, "Tile vertically") MenuItem(#MENU_TileHorizontally, "Tile horizontally") MenuItem(#MENU_Cascade, "Cascade") MenuItem(#MENU_Previous, "Previous") MenuItem(#MENU_Next, "Next") ; MDI subwindows will get added here **EndIf** ``` -------------------------------- ### Get Canvas Gadget Output ID Source: https://www.purebasic.com/documentation/gadget/canvasoutput.html Use CanvasOutput() to get the OutputID of a CanvasGadget before starting a drawing operation. The returned ID is only valid for one drawing session. ```purebasic StartDrawing(CanvasOutput(#Gadget)) ; do some drawing stuff here... StopDrawing() ``` -------------------------------- ### Start Vector Drawing on CanvasGadget Source: https://www.purebasic.com/documentation/gadget/canvasvectoroutput.html Use CanvasVectorOutput() to get the OutputID for a CanvasGadget before starting vector drawing operations. The returned ID is valid for a single drawing session. ```purebasic StartVectorDrawing(CanvasVectorOutput(#Gadget)) ; do some drawing stuff here... StopVectorDrawing() ``` -------------------------------- ### Basic Menu Creation Example Source: https://www.purebasic.com/documentation/menu/menuitem.html This example shows how to open a window, create a menu bar, add a menu title, and then add several menu items with different configurations. ```PureBasic **If** OpenWindow(0, 200, 200, 200, 100, "MenuItem Example") **If** CreateMenu(0, WindowID(0)) MenuTitle("Project") MenuItem(1, "Open") ; normal item MenuItem(2, "&Save") ; item with underlined character, the underline will only ; be displayed, if menu is called with F10 + arrow keys MenuItem(3, "Quit"+Chr(9)+"Esc") ; item with separate shortcut text **EndIf** **Repeat** : **Until** WaitWindowEvent() = #PB_Event_CloseWindow **EndIf** ``` -------------------------------- ### Create and Uncompress Pack File Example Source: https://www.purebasic.com/documentation/packer/uncompresspackfile.html This example demonstrates creating a zip pack file, adding multiple files to it, opening the pack file, and then uncompressing its contents into a specified directory. It includes error handling for pack creation, directory creation, and file unpacking. ```purebasic UseZipPacker() Path$ = #PB_Compiler_Home + "examples/sources/Data/" ; path to the PureBasic examples data/media files PackFile$ = GetTemporaryDirectory() + "PureBasicTest.zip" ; path to the pack file which should be created and opened later **If** CreatePack(0, PackFile$) AddPackFile(0, Path$ + "world.png", "world.png") AddPackFile(0, Path$ + "test.pref", "test.pref") AddPackFile(0, Path$ + "CdPlayer.ico", "CdPlayer.ico") AddPackFile(0, Path$ + "Background.bmp", "Background.bmp") ClosePack(0) **Debug** "PackFile successfully created: " + PackFile$ **Else** **Debug** "Error creating the pack file!" **EndIf** Path$ = GetTemporaryDirectory() + "PureBasicTest/" ; path to a directory into which the pack files should be extracted **If** OpenPack(0, PackFile$) **If** CreateDirectory(Path$) **Or** FileSize(Path$) = -2 **If** ExaminePack(0) **Debug** "Extracting archive into: " + Path$ **While** NextPackEntry(0) **Debug** " - name: " + PackEntryName(0) + ", size: " + PackEntrySize(0) **If** UncompressPackFile(0, Path$ + PackEntryName(0), PackEntryName(0)) = -1 **Debug** "Error: unsuccessful unpacking of file: " + PackEntryName(0) **EndIf** **Wend** **EndIf** ClosePack(0) RunProgram(Path$) ; open the directory with the unpacked files **Else** **Debug** "Error while creating the folder '" + Path$ + "' to unpack the archive contents!" **EndIf** **Else** **Debug** "Error opening the pack file!" **EndIf** ``` -------------------------------- ### Send HTTP GET request with custom headers Source: https://www.purebasic.com/documentation/http/httprequest.html This example demonstrates sending a GET request with custom headers like 'Content-Type' and 'User-Agent'. Remember to call FinishHTTP() after processing the response. ```purebasic **NewMap** Header$() Header$("Content-Type") = "plaintext" Header$("User-Agent") = "Firefox 54.0" HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://www.google.com", "", 0, Header$()) **If** HttpRequest **Debug** "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode) **Debug** "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response) FinishHTTP(HTTPRequest) **Else** **Debug** "Request creation failed" **EndIf** ``` -------------------------------- ### Get Joystick Name Source: https://www.purebasic.com/documentation/joystick/index.html Returns the name of the specified joystick. The joystick index starts from 0. ```purebasic JoystickName(0) as String ``` -------------------------------- ### Main Program Initialization Source: https://www.purebasic.com/documentation/reference/ug_procedures.html Initializes the file list, defines necessary variables, creates the main window, and populates the file list with the contents of the user's home directory. This sets up the initial state of the application. ```PureBasic NewList Files.FILEITEM() Define.s Folder Define.l Event, EventWindow, EventGadget, EventType, EventMenu Folder = GetHomeDirectory() WindowCreate() WindowResize() LabelUpdate(Folder) FilesExamine(Folder, Files()) ListLoad(#FilesList, Files()) ``` -------------------------------- ### Fill Area with specified color Source: https://www.purebasic.com/documentation/2ddrawing/fillarea.html Fills an area starting from (150, 155) until a color different from the starting pixel's color is found, using blue as the fill color. This example demonstrates filling a circular shape. ```PureBasic If OpenWindow(0, 0, 0, 300, 300, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) If CreateImage(0, 300, 300) And StartDrawing(ImageOutput(0)) Box(0, 0, 300, 300, RGB(255, 255, 255)) Circle(150, 150, 125 ,$00FF00) Circle(150, 150, 120 ,$FF0000) LineXY(30, 150, 270, 150, $FFFFFF) FillArea(150, 155, -1, $0000FF) ; Replace -1 by $00FF00, and compare the result StopDrawing() ImageGadget(0, 0, 0, 300, 300, ImageID(0)) EndIf Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### Start Vector Drawing on an Image Source: https://www.purebasic.com/documentation/image/imagevectoroutput.html Example showing how to initialize a vector drawing session on an image using millimeter units. ```PureBasic StartVectorDrawing(ImageVectorOutput(#Image, #PB_Unit_Millimeter)) ; do some drawing stuff here... StopVectorDrawing() ``` -------------------------------- ### Implement File Selection Dialog Source: https://www.purebasic.com/documentation/requester/openfilerequester.html Example showing how to initialize a file requester with specific patterns and handle the user's selection or cancellation. ```PureBasic StandardFile$ = "C:\autoexec.bat" ; set initial file+path to display ; With next string we will set the search patterns ("|" as separator) for file displaying: ; 1st: "Text (*.txt)" as name, ".txt" and ".bat" as allowed extension ; 2nd: "PureBasic (*.pb)" as name, ".pb" as allowed extension ; 3rd: "All files (*.*) as name, "*.*" as allowed extension, valid for all files Pattern$ = "Text (*.txt;*.bat)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*" Pattern = 0 ; use the first of the three possible patterns as standard File$ = OpenFileRequester("Please choose file to load", StandardFile$, Pattern$, Pattern) **If** File$ MessageRequester("Information", "You have selected following file:" + Chr(10) + File$, 0) **Else** MessageRequester("Information", "The requester was canceled.", 0) **EndIf** ``` -------------------------------- ### Implement DragText() in a Gadget Source: https://www.purebasic.com/documentation/dragdrop/dragtext.html Example demonstrating how to bind a drag start event to a ListViewGadget and initiate a text drag operation. ```PureBasic Procedure DragStartHandler() ExamineDraggedItems() While NextDraggedItem() Text$ + GetGadgetItemText(0, DraggedItemIndex()) + Chr(10) Wend Debug "Dragging text: " + Text$ DragText(Text$) EndProcedure ; Select some files or folders and drag them to another application ; If OpenWindow(0, 200, 200, 400, 400, "Drag & Drop") ListViewGadget(0, 10, 10, 380, 380, #PB_ListView_MultiSelect) AddGadgetItem(0, -1, "Item 1") AddGadgetItem(0, -1, "Item 2") AddGadgetItem(0, -1, "Item 3") ; BindGadgetEvent() is required to handle the drag start event BindGadgetEvent(0, @DragStartHandler(), #PB_EventType_DragStart) Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### Initialize and Setup 3D Environment Source: https://www.purebasic.com/documentation/Examples/NodeAnimation.pb.html Initializes the 3D engine, sprite, keyboard, and mouse. Opens a window and sets up the screen. Loads necessary 3D archives and parses scripts. ```purebasic InitEngine3D() InitSprite() InitKeyboard() InitMouse() ExamineDesktops():dx=DesktopWidth(0)*0.8:dy=DesktopHeight(0)*0.8 OpenWindow(0, 0,0, DesktopUnscaledX(dx),DesktopUnscaledY(dy), " NodeAnimation - [Esc] quit",#PB_Window_ScreenCentered) OpenWindowedScreen(WindowID(0), 0, 0, dx, dy, 0, 0, 0) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data" , #PB_3DArchive_FileSystem) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures" , #PB_3DArchive_FileSystem) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models" , #PB_3DArchive_FileSystem) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip) Parse3DScripts() ``` -------------------------------- ### Initialize and Manage Gadgets in PureBasic Source: https://www.purebasic.com/documentation/Examples/GadgetSplitterAdvanced.pb.html Demonstrates creating a window with multiple gadgets and handling real-time resizing using BindEvent. ```PureBasic ; ; ------------------------------------------------------------ ; ; PureBasic - Gadget example file ; ; (c) Fantaisie Software ; ; ------------------------------------------------------------ ; #WindowWidth = 640 #WindowHeight = 480 **Procedure** OnSizeWindow() ResizeGadget(5, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-25) ; Our 'master' splitter gadget **EndProcedure** **If** OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) TextGadget(7, 10, 5, 700, 15, "PureBasic splitter demonstation with Editor, ScrollArea, ExplorerTree and Web gadgets. Feel the power...") WebGadget(0, 10, 10, 300, 20, "http://www.google.com") EditorGadget(1, 115, 10, 100, 190) **For** k=1 **To** 10 AddGadgetItem(1, k-1, "Line "+Str(k)) **Next** ExplorerTreeGadget(3, 115, 10, 100, 190, GetHomeDirectory(), #PB_Explorer_AlwaysShowSelection|#PB_Explorer_FullRowSelect|#PB_Explorer_MultiSelect) ScrollAreaGadget(6, 0, 0, 400, 400, 1000, 1000, 1) ButtonGadget(20, 20, 20, 200, 200, "Scroll Area !") CloseGadgetList() SplitterGadget(2, 0, 0, #WindowWidth/2, #WindowHeight/2, 1, 0) SplitterGadget(4, 0, 0, #WindowWidth, #WindowHeight, 3, 2, #PB_Splitter_Vertical) SplitterGadget(5, 0, 25, #WindowWidth, #WindowHeight-25, 4, 6, #PB_Splitter_Vertical) SetGadgetState(5, 500) ; Use BindEvent() to have a realtime window sizing BindEvent(#PB_Event_SizeWindow, @OnSizeWindow()) **Repeat** Event = WaitWindowEvent() **If** Event = #PB_Event_Gadget **Select** EventGadget() **Case** 8 SetGadgetState(5, 333) SetGadgetState(2, 333) SetGadgetState(11, 5) **Case** 20 **Debug** "OK" **EndSelect **EndIf** **Until** Event = #PB_Event_CloseWindow **EndIf** **End** ``` -------------------------------- ### Get Current User Name in PureBasic Source: https://www.purebasic.com/documentation/system/username.html Use this function to retrieve the name of the currently logged-in user. No setup or imports are required. ```purebasic Debug "Currently logged user: " + UserName() ``` -------------------------------- ### PureBasic MiniBrowser Window Setup Source: https://www.purebasic.com/documentation/Examples/WebBrowser.pb.html Initializes the main window, status bar, and navigation controls for the MiniBrowser. Includes setup for buttons, URL input, and the WebGadget itself. Handles potential errors if the WebGadget cannot be created. ```purebasic ; ; ------------------------------------------------------------ ; ; PureBasic - MiniBrowser ; ; (c) Fantaisie Software ; ; ------------------------------------------------------------ ; **Procedure** ResizeWebWindow() ResizeGadget(10, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-52) ResizeGadget(4, #PB_Ignore, #PB_Ignore, WindowWidth(0)-185, #PB_Ignore) ResizeGadget(5, WindowWidth(0)-25, #PB_Ignore, #PB_Ignore, #PB_Ignore) ResizeGadget(6, #PB_Ignore, #PB_Ignore, WindowWidth(0), #PB_Ignore) **EndProcedure** **If** OpenWindow(0, 100, 200, 500, 300, "PureBasic MiniBrowser v1.0", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget) CreateStatusBar(0, WindowID(0)) AddStatusBarField(#PB_Ignore) StatusBarText(0, 0, "Welcome to the world's smallest Browser !", 0) ButtonGadget(1, 0, 3, 50, 25, "Back") ButtonGadget(2, 50, 3, 50, 25, "Next") ButtonGadget(3, 100, 3, 50, 25, "Stop") StringGadget(4, 155, 5, 0, 20, "http://www.google.com") ButtonGadget(5, 0, 3, 25, 25, "Go") FrameGadget(6, 0, 30, 0, 2, "", 2) ; Nice little separator **If** WebGadget(10, 0, 31, 0, 0, "http://www.google.com") = 0 **CompilerIf** #PB_Compiler_OS <> #PB_OS_Windows ; Linux and OX uses Webkit MessageRequester("Error", "Webkit library not found", 0) **CompilerEndIf** **End** ; Quit **EndIf** AddKeyboardShortcut(0, #PB_Shortcut_Return, 0) ; Use bindevent() to have a realtime window resize ; BindEvent(#PB_Event_SizeWindow, @ResizeWebWindow()) ResizeWebWindow() ; Adjust the gadget to the current window size **Repeat** Event = WaitWindowEvent() **Select** Event **Case** #PB_Event_Gadget **Select** EventGadget() **Case** 1 SetGadgetState(10, #PB_Web_Back) **Case** 2 SetGadgetState(10, #PB_Web_Forward) **Case** 3 SetGadgetState(10, #PB_Web_Stop) **Case** 5 SetGadgetText(10, GetGadgetText(4)) **EndSelect** **Case** #PB_Event_Menu ; We only have one shortcut SetGadgetText(10, GetGadgetText(4)) **EndSelect** **Until** Event = #PB_Event_CloseWindow **EndIf** ``` -------------------------------- ### AddPathCurve() Example Source: https://www.purebasic.com/documentation/vectordrawing/addpathcurve.html Demonstrates how to use AddPathCurve() to draw a cubic Bezier curve on a canvas. Ensure the vector drawing is started before calling this function. ```PureBasic If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) CanvasGadget(0, 0, 0, 400, 200) If StartVectorDrawing(CanvasVectorOutput(0)) MovePathCursor(50, 100) AddPathCurve(90, 30, 250, 180, 350, 100) VectorSourceColor(RGBA(255, 0, 0, 255)) StrokePath(10) StopVectorDrawing() EndIf Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### ReadCGI Usage Example Source: https://www.purebasic.com/documentation/cgi/readcgi.html Demonstrates initializing the CGI environment and reading the request input before writing a response. ```PureBasic If Not InitCGI() Or Not ReadCGI() End EndIf WriteCGIHeader(#PB_CGI_HeaderContentType, "text/html", #PB_CGI_LastHeader) ; Write the headers to inform the browser of the content format WriteCGIString("PureBasic - variables" + "Hello from PureBasic CGI !" + "") ``` -------------------------------- ### WriteProgramString() Example Source: https://www.purebasic.com/documentation/process/writeprogramstring.html Demonstrates writing a string to a program's input. Ensure the program was started with RunProgram() and #PB_Program_Write. No newline is automatically added. ```purebasic WriteProgramString(Program, "Hello World") ``` -------------------------------- ### PureBasic 3D Initialization and Setup Source: https://www.purebasic.com/documentation/Examples/Entity.pb.html Initializes the 3D engine, sprite, keyboard, and mouse. Opens a window and sets up the screen for 3D rendering. Loads necessary 3D archives and assets like meshes and textures. ```purebasic ; ------------------------------------------------------------ ; ; PureBasic - Entity ; ; (c) Fantaisie Software ; ; ------------------------------------------------------------ ; #CameraSpeed = 1 InitEngine3D() InitSprite() InitKeyboard() InitMouse() ExamineDesktops():dx=DesktopWidth(0)*0.8:dy=DesktopHeight(0)*0.8 OpenWindow(0, 0,0, DesktopUnscaledX(dx),DesktopUnscaledY(dy), " Entity - [Esc] quit",#PB_Window_ScreenCentered) OpenWindowedScreen(WindowID(0), 0, 0, dx, dy, 0, 0, 0) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem) Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/skybox.zip", #PB_3DArchive_Zip) LoadMesh(0, "robot.mesh") CreateMaterial(0, LoadTexture(0, "clouds.jpg")) CopyMaterial(0, 1) CreateMaterial(2, LoadTexture(2, "r2skin.jpg")) MaterialShadingMode(0, #PB_Material_Wireframe) CreateEntity(0, MeshID(0), MaterialID(0)) CreateEntity(1, MeshID(0), MaterialID(1), -60, 0, 0) CreateEntity(2, MeshID(0), MaterialID(2), 60, 0, 0) StartEntityAnimation(0, "Walk") EntityRenderMode(0, #PB_Entity_DisplaySkeleton) SkyBox("stevecube.jpg") CreateCamera(0, 0, 0, 100, 100) MoveCamera(0, 0, 40, 150, #PB_Absolute) ``` -------------------------------- ### ExamineDraggedItems Usage Example Source: https://www.purebasic.com/documentation/dragdrop/examinedraggeditems.html Demonstrates how to initialize item examination within a drag start event handler and iterate through selected items in an ExplorerListGadget. ```PureBasic Procedure DragStartHandler() ; Start to examine the dragged items. Have to be after a #PB_EventType_DragStart event ; ExamineDraggedItems() ; Iterate over the dragged items ; While NextDraggedItem() Debug "Dragged item: " + GetGadgetText(0) + GetGadgetItemText(0, DraggedItemIndex()) Wend EndProcedure ; Select some files or folders and drag them to another application ; If OpenWindow(0, 200, 200, 400, 400, "Drag & Drop") ExplorerListGadget(0, 10, 10, 380, 380, GetHomeDirectory(), #PB_Explorer_MultiSelect) ; BindGadgetEvent() is required to handle the drag start event BindGadgetEvent(0, @DragStartHandler(), #PB_EventType_DragStart) Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### GetSoundFrequency usage example Source: https://www.purebasic.com/documentation/sound/getsoundfrequency.html Demonstrates initializing the sound system, loading an OGG file, playing it, and retrieving its frequency. ```PureBasic If InitSound() ; Initialize Sound system UseOGGSoundDecoder() ; Use ogg files ; Loads a sound from a file If LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg") ; The sound is playing PlaySound(0, #PB_Sound_Loop, 20) MessageRequester("Info", "The average frequency is " + Str(GetSoundFrequency(0))+" Hz") MessageRequester("Info", "Ok to stop.") FreeSound(0) ; The sound is freed End EndIf Else Debug "Warning! The sound environment couldn't be initialized. So no sound commands can be used..." EndIf ``` -------------------------------- ### Create Mesh LOD Levels Source: https://www.purebasic.com/documentation/mesh/buildmeshlod.html Example demonstrating the creation of three LOD levels for a mesh with a 75% reduction factor starting at 100 units. ```PureBasic CreateMeshLodLevels(#Mesh, 3, 100, 0.75) ``` -------------------------------- ### Get Relative Z Direction of Light Source: https://www.purebasic.com/documentation/light/lightdirectionz.html Retrieves the 'z' direction vector of the light relative to its parent. Use this when dealing with hierarchical lighting setups. ```purebasic Result = LightDirectionZ(#Light, #PB_Relative) ``` -------------------------------- ### Connect to PostgreSQL Database Source: https://www.purebasic.com/documentation/database/usepostgresqldatabase.html Example showing initialization of the PostgreSQL environment followed by an OpenDatabase call. ```purebasic UsePostgreSQLDatabase() ; You should have a server running on localhost ; If OpenDatabase(0, "host=localhost port=5432", "user", "password") Debug "Connected to PostgreSQL" Else Debug "Connection failed: "+DatabaseError() EndIf ``` -------------------------------- ### Basic WebGadget Implementation Source: https://www.purebasic.com/documentation/gadget/webgadget.html Initializes a window and displays a website using the WebGadget. ```PureBasic If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) WebGadget(0, 10, 10, 580, 280, "https://www.purebasic.com") ; Note: if you want to use a local file, change last parameter to "file://" + path + filename Repeat Until WaitWindowEvent() = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### Get Active Gadget Example Source: https://www.purebasic.com/documentation/gadget/getactivegadget.html Demonstrates how to use GetActiveGadget() to identify the gadget that received an escape key press. Ensure the window and gadgets are opened before calling this function. ```PureBasic **If** OpenWindow(0, 0, 0, 270, 70, "GetActiveGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) StringGadget (0, 10, 10, 250, 20, "Press escape...") StringGadget (1, 10, 40, 250, 20, "Press escape...") AddKeyboardShortcut(0, #PB_Shortcut_Escape, 1) SetActiveGadget(0) **Repeat** Event = WaitWindowEvent() **If** Event = #PB_Event_Menu **And** EventMenu() = 1 MessageRequester("Test", "Escape pressed in Gadget " + Str(GetActiveGadget())) **EndIf** **Until** Event = #PB_Event_CloseWindow **EndIf** ``` -------------------------------- ### Start Drawing on Printer using PrinterOutput() Source: https://www.purebasic.com/documentation/printer/printeroutput.html Use PrinterOutput() to get the OutputID for pixel-based drawing on the printer. Vector-based drawing with PrinterVectorOutput() is generally preferred for better quality and resolution independence. ```purebasic StartDrawing(PrinterOutput()) ; do some drawing stuff here... StopDrawing() ``` -------------------------------- ### Adjusting Sound Volume in PureBasic Source: https://www.purebasic.com/documentation/sound/soundvolume.html This example demonstrates how to initialize the sound system, load a sound file, play it with a specific volume, change the volume in real-time, and then free the sound. It requires the sound system to be initialized and a valid sound file to be available. ```PureBasic **If** InitSound() ; Initialize Sound system UseOGGSoundDecoder() ; Use ogg files ; Loads a sound from a file **If** LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg") ; The sound is playing PlaySound(0, #PB_Sound_Loop, 20) MessageRequester("Info", "The sound volume is at 20%") SoundVolume(0, 80) MessageRequester("Info", "The sound volume is at 80%") FreeSound(0) ; The sound is freed **End** **EndIf** **Else** **Debug** "Warning! The sound environment couldn't be initialized. So no sound commands can be used..." **EndIf** ``` -------------------------------- ### Start Drawing on an Image with ImageOutput() Source: https://www.purebasic.com/documentation/image/imageoutput.html Use ImageOutput() to get the drawing ID for an image, then pass it to StartDrawing(). Ensure StopDrawing() is called afterward. This function cannot be used with loaded icon files. ```purebasic StartDrawing(ImageOutput(#Image)) ; do some drawing stuff here... StopDrawing() ``` -------------------------------- ### PlaySound() Usage Example Source: https://www.purebasic.com/documentation/sound/playsound.html Demonstrates initializing the sound system, loading an OGG file, and playing it in a loop with a specified volume. ```PureBasic If InitSound() ; Initialize Sound system UseOGGSoundDecoder() ; Use ogg files ; Loads a sound from a file If LoadSound(0, #PB_Compiler_Home +"Examples/3D/Data/Siren.ogg") ; The sound is playing PlaySound(0, #PB_Sound_Loop, 20) MessageRequester("Info", "Ok to stop.") FreeSound(0) ; The sound is freed EndIf End Else Debug "Warning! The sound environment couldn't be initialized. So no sound commands can be used..." EndIf ``` -------------------------------- ### Main File Implementation Source: https://www.purebasic.com/documentation/reference/ide_form.html Example of a main file integrating multiple .pbf form files and handling event loops. ```purebasic XIncludeFile "MainWindow.pbf" ; Include the first window definition XIncludeFile "DateWindow.pbf" ; Include the second window definition OpenMainWindow() ; Open the first window. This procedure name is always 'Open' followed by the window name OpenDateWindow() ; Open the second window ; The event procedures, as specified in the 'event procedure' property of each gadget Procedure OkButtonEvent(EventType) Debug "OkButton event" EndProcedure Procedure CancelButtonEvent(EventType) Debug "CancelButton event" EndProcedure Procedure TrainCalendarEvent(EventType) Debug "TrainCalendar event" EndProcedure ; The main event loop as usual, the only change is to call the automatically ; generated event procedure for each window. Repeat Event = WaitWindowEvent() Select EventWindow() Case MainWindow MainWindow_Events(Event) ; This procedure name is always window name followed by '_Events' Case DateWindow DateWindow_Events(Event) EndSelect Until Event = #PB_Event_CloseWindow ; Quit on any window close ``` -------------------------------- ### Displaying Path Segments Source: https://www.purebasic.com/documentation/vectordrawing/pathsegments.html This example demonstrates how to use PathSegments() to get and debug the string representation of a vector drawing path. Ensure the vector drawing library is initialized before calling this function. ```PureBasic If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) CanvasGadget(0, 0, 0, 400, 200) If StartVectorDrawing(CanvasVectorOutput(0)) MovePathCursor(40, 20) For i = 1 To 4 AddPathLine(80, 0, #PB_Path_Relative) AddPathLine(0, 40, #PB_Path_Relative) Next i ; Show the path segments Debug PathSegments() VectorSourceColor(RGBA(255, 0, 0, 255)) StrokePath(10, #PB_Path_RoundCorner) StopVectorDrawing() EndIf Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### Get Desktop Color Depth - PureBasic Source: https://www.purebasic.com/documentation/desktop/desktopdepth.html Call ExamineDesktops() before using DesktopDepth() to retrieve information about available desktops. This example displays the current resolution and color depth of the primary monitor. ```purebasic ExamineDesktops() MessageRequester("Display Information", "Current resolution = "+Str(DesktopWidth(0))+"x"+Str(DesktopHeight(0))+"x"+Str(DesktopDepth(0))) ``` -------------------------------- ### PureBasic FileSystem Example Source: https://www.purebasic.com/documentation/Examples/FileSystem.pb.html This code creates a GUI application that lists the contents of a specified directory. It requires a window, string gadget, button gadget, and list view gadget. The listing is triggered by the button click event. ```PureBasic ; ; ------------------------------------------------------------ ; ; PureBasic - FileSystem example file ; ; (c) Fantaisie Software ; ; ------------------------------------------------------------ ; If OpenWindow(0, 100, 200, 290, 200, "PureBasic - FileSystem Example") StringGadget (0, 10, 10, 202, 24, GetHomeDirectory()) ButtonGadget (1, 220, 10, 60 , 24, "List") ListViewGadget(2, 10, 40, 270, 150) Repeat Event = WaitWindowEvent() If Event = #PB_Event_Gadget If EventGadget() = 1 ; Read ClearGadgetItems(2) ; Clear all the items found in the ListView If ExamineDirectory(0, GetGadgetText(0), "*.*") While NextDirectoryEntry(0) FileName$ = DirectoryEntryName(0) If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory FileName$ = "[DIR] "+FileName$ EndIf AddGadgetItem(2, -1, FileName$) Wend Else MessageRequester("Error","Can't examine this directory: "+GetGadgetText(0),0) EndIf EndIf EndIf Until Event = #PB_Event_CloseWindow EndIf End ``` -------------------------------- ### ListViewGadget() Example Source: https://www.purebasic.com/documentation/gadget/mdigadget.html This snippet shows how to use ListViewGadget() to create a list view. It includes basic event handling for closing the window. Ensure you have a main window and relevant event handling setup. ```purebasic UseGadgetList(WindowID(#Main)) ; go back to the main window gadgetlist **EndIf** **Repeat** : **Until** WaitWindowEvent()=#PB_Event_CloseWindow **EndIf** ``` -------------------------------- ### Create and manage OptionGadgets Source: https://www.purebasic.com/documentation/gadget/optiongadget.html Demonstrates initializing a window, creating a group of three option gadgets, and setting the initial selection state. ```PureBasic If OpenWindow(0, 0, 0, 140, 110, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) OptionGadget(0, 30, 20, 60, 20, "Option 1") OptionGadget(1, 30, 45, 60, 20, "Option 2") OptionGadget(2, 30, 70, 60, 20, "Option 3") SetGadgetState(1, 1) ; set second option as active one Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow EndIf ``` -------------------------------- ### Iterative String Search with FindString() Source: https://www.purebasic.com/documentation/string/findstring.html This example repeatedly uses FindString() to find all occurrences of a character within a string. It starts searching from the position after the last found occurrence. The loop continues until no more occurrences are found. ```PureBasic String$ = "This is a simple line...." Repeat Last = Position Position = FindString(String$ , "i", Position + 1) If Position > 0 Debug "'i' found at position: " + Position EndIf Until Not Position Debug "Last position 'i' was found: " + Last ```