Garry's Mod Wiki

DFileBrowser

Description

A tree and list-based file browser.

It allows filtering by folder (directory) name and file extension, and can display models as SpawnIcons.

View source

Parent

Derives methods, etc not listed on this page from DPanel.

Events

DFileBrowser:OnDoubleClick( Panel selectedPanel, string filePath )
Called when a file is double-clicked. Double-clicking a file or icon will trigger both this and DFileBrowser:OnSelect.
DFileBrowser:OnRightClick( string filePath, Panel selectedPanel )
Called when a file is right-clicked. When not in model viewer mode, DFileBrowser:OnSelect will also be called if the file is not already selected.
DFileBrowser:OnSelect( Panel selectedPanel, string filePath )
Called when a file is selected.

Methods

DFileBrowser:Clear()
Clears the file tree and list, and resets all values.
string DFileBrowser:GetBaseFolder()
Returns the root directory/folder of the file tree.
string DFileBrowser:GetCurrentFolder()
Returns the current directory/folder being displayed.
string DFileBrowser:GetFileTypes()
Returns the current file type filter on the file list.
Panel DFileBrowser:GetFolderNode()
Returns the DTree Node that the file tree stems from. This is a child of the root node of the DTree.
boolean DFileBrowser:GetModels()
Returns whether or not the model viewer mode is enabled. In this mode, files are displayed as SpawnIcons instead of a list.
string DFileBrowser:GetName()
Returns the name being used for the file tree.
boolean DFileBrowser:GetOpen()
Returns whether or not the file tree is open.
string DFileBrowser:GetPath()
Returns the access path of the file tree. This is GAME unless changed with DFileBrowser:SetPath. See file. Read for how paths work.
string DFileBrowser:GetSearch()
Returns the current search filter on the file tree.
DFileBrowser:SetBaseFolder( string baseDir )
Sets the root directory/folder of the file tree. This needs to be set for the file tree to be displayed.
DFileBrowser:SetCurrentFolder( string currentDir )
Sets the directory/folder from which to display the file list.
DFileBrowser:SetFileTypes( string fileTypes = "." )
Sets the file type filter for the file list. This accepts the same file extension wildcards as file. Find.
DFileBrowser:SetModels( boolean showModels = false )
Enables or disables the model viewer mode. In this mode, files are displayed as SpawnIcons instead of a list. This should only be used for . mdl files; the spawn icons will display error models for others. See DFileBrowser:SetFileTypes
DFileBrowser:SetName( string treeName = "baseFolder" )
Sets the name to use for the file tree.
DFileBrowser:SetOpen( boolean open = false, boolean useAnim = false )
Opens or closes the file tree.
DFileBrowser:SetPath( string path )
Sets the access path for the file tree. This is set to GAME by default. See file. Read for how paths work.
DFileBrowser:SetSearch( string filter = "*" )
Sets the search filter for the file tree. This accepts the same wildcards as file. Find.
boolean DFileBrowser:Setup()
This is used internally - although you're able to use it you probably shouldn't. Called to set up the DTree and file viewer when a base path has been set. Calls DFileBrowser:SetupTree and DFileBrowser:SetupFiles.
boolean DFileBrowser:SetupFiles()
This is used internally - although you're able to use it you probably shouldn't. Called to set up the DListView or DIconBrowser by DFileBrowser:Setup. The icon browser is used when in models mode. See DFileBrowser:SetModels.
boolean DFileBrowser:SetupTree()
This is used internally - although you're able to use it you probably shouldn't. Called to set up the DTree by DFileBrowser:Setup.
DFileBrowser:ShowFolder( string currentDir )
This is used internally - although you're able to use it you probably shouldn't. Builds the file or icon list for the current directory. You should use DFileBrowser:SetCurrentFolder to change the directory.
DFileBrowser:SortFiles( boolean descending = false )
Sorts the file list. This is only functional when not using the model viewer. See DFileBrowser:SetModels

Example

Creates a DFileBrowser and displays the data/persist folder. Any file clicked is printed to the console.

local frame = vgui.Create( "DFrame" ) frame:SetSize( 500, 250 ) frame:SetSizable( true ) frame:Center() frame:MakePopup() frame:SetTitle( "DFileBrowser Example" ) local browser = vgui.Create( "DFileBrowser", frame ) browser:Dock( FILL ) browser:SetPath( "GAME" ) -- The access path i.e. GAME, LUA, DATA etc. browser:SetBaseFolder( "data" ) -- The root folder browser:SetOpen( true ) -- Open the tree to show sub-folders browser:SetCurrentFolder( "persist" ) -- Show files from persist function browser:OnSelect( path, pnl ) -- Called when a file is clicked print( path ) end
Output:

Example

Creates a DFileBrowser that can spawn models from props_ folders.

Uses the same DFrame as above
local frame = vgui.Create( "DFrame" ) frame:SetSize( 500, 250 ) frame:SetSizable( true ) frame:Center() frame:MakePopup() frame:SetTitle( "DFileBrowser Example" ) local browser = vgui.Create( "DFileBrowser", frame ) browser:Dock( FILL ) browser:SetPath( "GAME" ) -- The access path i.e. GAME, LUA, DATA etc. browser:SetBaseFolder( "models" ) -- The root folder browser:SetName( "Props_ Models" ) -- Name to display in tree browser:SetSearch( "props_" ) -- Search folders starting with "props_" browser:SetFileTypes( "*.mdl" ) -- File type filter browser:SetOpen( true ) -- Opens the tree (same as double clicking) browser:SetCurrentFolder( "props_badlands" ) -- Set the folder to use function browser:OnSelect( path, pnl ) -- Called when a file is clicked RunConsoleCommand( "gm_spawn", path ) -- Spawn the model we clicked frame:Close() end
Output: See Preview.

Example

Same as above, but enables model viewing. The following line is added to the above code.

browser:SetModels( true ) -- Use SpawnIcons instead of a list
Output:

Example

Same as above, but enables model viewing. The following line is added to the above code.

browser:SetModels( true ) -- Use SpawnIcons instead of a list
Output:

Example

Creates a DFileBrowser for browsing and selecting sounds.

-- Create a new frame (window) local frame = vgui.Create("DFrame") frame:SetSize( 500, 250 ) -- Set the size of the frame frame:SetSizable( true ) -- Allow the frame to be resizable frame:Center() -- Center the frame on the screen frame:MakePopup() -- Make the frame appear in front of other windows and accept user input frame:SetTitle( "Select a sound:" ) -- Set the title of the frame -- Create a file browser inside the frame local browser = vgui.Create( "DFileBrowser", frame ) browser:Dock( FILL ) -- Make the browser fill the entire frame -- Set the path that the browser should start in. "GAME" is a special path that refers to the game's root directory. browser:SetPath("GAME") -- Set the base folder to start searching to 'sound' (contains every sound from every addon/game) browser:SetBaseFolder( "sound" ) browser:SetOpen( true ) -- Show the folder as already open when the browser is created browser:SetFileTypes(".wav .mp3 .ogg") -- Filter the file types that the browser should only display local s = nil -- Initialize a local var to hold the preview sound -- Define what should happen when a file is double-clicked in the browser browser.OnDoubleClick = function(panel, path) if(s ~= nil) then s:Stop() end -- If a sound is currently playing, stop it -- Select the sound and set it as the value of our ConVar, remove the "sound/" part from the path GetConVar("mysoundconvar"):SetString(string.Replace(path, "sound/","")) frame:Close() -- Close the frame end -- Define what should happen when a file is clicked in the browser browser.OnSelect = function(panel, path) if(s ~= nil) then s:Stop() end -- If a sound is currently playing, stop it s = CreateSound(LocalPlayer(), string.Replace(path, "sound/","")) -- Create and play a new sound from the selected file s:Play() end
Output:
sound_browser.png