1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) Widget FileSelection
--
-- Author : Manuel M T Chakravarty
--
-- Created: 20 January 1999
--
-- Copyright (C) 1999-2005 Manuel M T Chakravarty, Jens Petersen
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- TODO
--
-- Fix fileSelectionQueryButtons
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- Prompt the user for a file or directory name
--
-- * As of Gtk+ 2.4 this module has been deprecated in favour of 'FileChooser'
--
module Graphics.UI.Gtk.Selectors.FileSelection (
-- * Detail
--
-- | 'FileSelection' should be used to retrieve file or directory names from
-- the user. It will create a new dialog window containing a directory list,
-- and a file list corresponding to the current working directory. The
-- filesystem can be navigated using the directory list or the drop-down
-- history menu. Alternatively, the TAB key can be used to navigate using
-- filename completion - common in text based editors such as emacs and jed.
--
-- File selection dialogs are created with a call to 'fileSelectionNew'.
--
-- The default filename can be set using 'fileSelectionSetFilename' and the
-- selected filename retrieved using 'fileSelectionGetFilename'.
--
-- Use 'fileSelectionComplete' to display files and directories that match a
-- given pattern. This can be used for example, to show only *.txt files, or
-- only files beginning with gtk*.
--
-- Simple file operations; create directory, delete file, and rename file,
-- are available from buttons at the top of the dialog. These can be hidden
-- using 'fileSelectionHideFileopButtons' and shown again using
-- 'fileSelectionShowFileopButtons'.
--
-- * Class Hierarchy
-- |
-- @
-- | 'GObject'
-- | +----'Object'
-- | +----'Widget'
-- | +----'Container'
-- | +----'Bin'
-- | +----'Window'
-- | +----'Dialog'
-- | +----FileSelection
-- @
-- * Types
FileSelection,
FileSelectionClass,
castToFileSelection, gTypeFileSelection,
toFileSelection,
-- * Constructors
fileSelectionNew,
-- * Methods
fileSelectionSetFilename,
fileSelectionGetFilename,
fileSelectionShowFileopButtons,
fileSelectionHideFileopButtons,
fileSelectionGetButtons,
fileSelectionComplete,
fileSelectionGetSelections,
fileSelectionSetSelectMultiple,
fileSelectionGetSelectMultiple,
-- * Attributes
fileSelectionFilename,
fileSelectionShowFileops,
fileSelectionSelectMultiple,
) where
import Control.Monad (liftM)
import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.Attributes
import System.Glib.Properties
{#import Graphics.UI.Gtk.Types#}
import Graphics.UI.Gtk.Abstract.Object (makeNewObject)
import Graphics.UI.Gtk.General.Structs (fileSelectionGetButtons)
{# context lib="libgtk" prefix="gtk" #}
--------------------
-- Constructors
-- | Creates a new file selection dialog box. By default it will contain a
-- 'TreeView' of the application's current working directory, and a file
-- listing. Operation buttons that allow the user to create a directory, delete
-- files and rename files, are also present.
--
fileSelectionNew ::
String -- ^ @title@ - a message that will be placed in the file
-- requestor's titlebar.
-> IO FileSelection
fileSelectionNew title =
makeNewObject mkFileSelection $
liftM (castPtr :: Ptr Widget -> Ptr FileSelection) $
withUTFString title $ \titlePtr ->
{# call unsafe file_selection_new #}
titlePtr
--------------------
-- Methods
-- | Sets a default path for the file requestor. If @filename@ includes a
-- directory path, then the requestor will open with that path as its current
-- working directory.
--
-- This has the consequence that in order to open the requestor with a
-- working directory and an empty filename, @filename@ must have a trailing
-- directory separator.
--
fileSelectionSetFilename :: FileSelectionClass self => self
-> String -- ^ @filename@ - a string to set as the default file name.
-> IO ()
fileSelectionSetFilename self filename =
withUTFString filename $ \filenamePtr ->
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
{# call unsafe gtk_file_selection_set_filename_utf8 #}
#else
{# call unsafe gtk_file_selection_set_filename #}
#endif
(toFileSelection self)
filenamePtr
-- | This function returns the selected filename.
--
-- If no file is selected then the selected directory path is returned.
--
fileSelectionGetFilename :: FileSelectionClass self => self
-> IO String -- ^ returns currently-selected filename
fileSelectionGetFilename self =
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
{# call unsafe gtk_file_selection_get_filename_utf8 #}
#else
{# call unsafe gtk_file_selection_get_filename #}
#endif
(toFileSelection self)
>>= peekUTFString
-- | Shows the file operation buttons, if they have previously been hidden.
-- The rest of the widgets in the dialog will be resized accordingly.
--
fileSelectionShowFileopButtons :: FileSelectionClass self => self -> IO ()
fileSelectionShowFileopButtons self =
{# call file_selection_show_fileop_buttons #}
(toFileSelection self)
-- | Hides the file operation buttons that normally appear at the top of the
-- dialog. Useful if you wish to create a custom file selector, based on
-- 'FileSelection'.
--
fileSelectionHideFileopButtons :: FileSelectionClass self => self -> IO ()
fileSelectionHideFileopButtons self =
{# call file_selection_hide_fileop_buttons #}
(toFileSelection self)
-- currently broken
-- -- query the widgets of the file selectors buttons
-- --
-- -- * this is useful to attach signals handlers to these buttons
-- --
-- -- * the buttons are OK & Cancel (in this order)
-- --
-- fileSelectionQueryButtons :: FileSelectionClass fsel
-- => fsel
-- -> IO (Button, Button)
-- fileSelectionQueryButtons fsel =
-- withForeignPtr (unFileSelection $ toFileSelection fsel) $ \ ptr -> do
-- ok <- {#get FileSelection.ok_button #} ptr
-- cancel <- {#get FileSelection.cancel_button#} ptr
-- return (castToButton ok, castToButton cancel)
-- | Will attempt to match @pattern@ to a valid filenames or subdirectories in
-- the current directory. If a match can be made, the matched filename will
-- appear in the text entry field of the file selection dialog. If a partial
-- match can be made, the \"Files\" list will contain those file names which
-- have been partially matched, and the \"Folders\" list those directories
-- which have been partially matched.
--
fileSelectionComplete :: FileSelectionClass self => self
-> String -- ^ @pattern@ - a string of characters which may or may not match
-- any filenames in the current directory.
-> IO ()
fileSelectionComplete self pattern =
withUTFString pattern $ \patternPtr ->
{# call file_selection_complete #}
(toFileSelection self)
patternPtr
-- | Retrieves the list of file selections the user has made in the dialog
-- box. This function is intended for use when the user can select multiple
-- files in the file list.
--
fileSelectionGetSelections :: FileSelectionClass self => self -> IO [String]
fileSelectionGetSelections self = do
cStrArr <-
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
{# call gtk_file_selection_get_selections_utf8 #}
#else
{# call gtk_file_selection_get_selections #}
#endif
(toFileSelection self)
cStrs <- peekArray0 nullPtr cStrArr
result <- mapM peekUTFString cStrs
{# call unsafe g_strfreev #} cStrArr
return result
-- | Sets whether the user is allowed to select multiple files in the file
-- list. Use 'fileSelectionGetSelections' to get the list of selected files.
--
fileSelectionSetSelectMultiple :: FileSelectionClass self => self
-> Bool -- ^ @selectMultiple@ - whether or not the user is allowed to select
-- multiple files in the file list.
-> IO ()
fileSelectionSetSelectMultiple self selectMultiple =
{# call gtk_file_selection_set_select_multiple #}
(toFileSelection self)
(fromBool selectMultiple)
-- | Determines whether or not the user is allowed to select multiple files in
-- the file list. See 'fileSelectionSetSelectMultiple'.
--
fileSelectionGetSelectMultiple :: FileSelectionClass self => self
-> IO Bool -- ^ returns @True@ if the user is allowed to select multiple
-- files in the file list
fileSelectionGetSelectMultiple self =
liftM toBool $
{# call gtk_file_selection_get_select_multiple #}
(toFileSelection self)
--------------------
-- Attributes
-- | The currently selected filename.
--
--
fileSelectionFilename :: FileSelectionClass self => Attr self String
fileSelectionFilename = newAttr
fileSelectionGetFilename
fileSelectionSetFilename
-- | Whether buttons for creating\/manipulating files should be displayed.
--
-- Default value: @False@
--
fileSelectionShowFileops :: FileSelectionClass self => Attr self Bool
fileSelectionShowFileops = newAttrFromBoolProperty "show-fileops"
-- | Whether to allow multiple files to be selected.
--
-- Default value: @False@
--
fileSelectionSelectMultiple :: FileSelectionClass self => Attr self Bool
fileSelectionSelectMultiple = newAttr
fileSelectionGetSelectMultiple
fileSelectionSetSelectMultiple
|