sub
filebox {
my
$demo
=
shift
;
$TOP
=
$MW
->WidgetDemo
(
-name
=>
$demo
,
-text
=>
"Enter a file name in the entry box or click on the \"Browse\" buttons to select a file name using the file selection dialog."
,
-title
=>
'File box Demonstration'
,
-iconname
=>
'filebox'
,
);
foreach
my
$i
(
qw(open save)
) {
my
$f
=
$TOP
->Frame;
my
$lab
=
$f
->Label(
-text
=>
"Select a file to $i: "
,
-anchor
=>
'e'
);
my
$ent
=
$f
->Entry(
-width
=> 20);
my
$but
=
$f
->Button(
-text
=>
"Browse ..."
,
-command
=>
sub
{ fileDialog(
$TOP
,
$ent
,
$i
)});
$lab
->
pack
(
-side
=>
'left'
);
$ent
->
pack
(
-side
=>
'left'
,
-expand
=>
'yes'
,
-fill
=>
'x'
);
$but
->
pack
(
-side
=>
'left'
);
$f
->
pack
(
-fill
=>
'x'
,
-padx
=>
'1c'
,
-pady
=> 3);
}
my
$cbf
=
$TOP
->Frame->
pack
(
-fill
=>
'x'
,
-padx
=>
'1c'
,
-pady
=> 3);
my
$fd
;
$cbf
->Radiobutton
(
-text
=>
'FileSelect'
,
-variable
=> \
$fd
,
-value
=>
'FileSelect'
,
-command
=>
sub
{
local
($^W) = 0;
Tk::FileSelect->
import
(
'as_default'
);
my
$mw
=
$TOP
->MainWindow;
delete
$mw
->{
'tk_getOpenFile'
};
delete
$mw
->{
'tk_getSaveFile'
};
})->
pack
(
-side
=>
'left'
);
my
$fdb
=
$cbf
->Radiobutton
(
-text
=>
'FBox'
,
-variable
=> \
$fd
,
-value
=>
'FBox'
,
-command
=>
sub
{
local
($^W) = 0;
Tk::FBox->
import
(
'as_default'
);
my
$mw
=
$TOP
->MainWindow;
delete
$mw
->{
'tk_getOpenFile'
};
delete
$mw
->{
'tk_getSaveFile'
};
})->
pack
(
-side
=>
'left'
);
$fdb
->invoke;
}
sub
fileDialog {
my
$w
=
shift
;
my
$ent
=
shift
;
my
$operation
=
shift
;
my
$types
;
my
$file
;
@types
=
([
"Text files"
, [
qw/.txt .doc/
]],
[
"Text files"
,
''
,
'TEXT'
],
[
"Perl Scripts"
,
'.pl'
,
'TEXT'
],
[
"C Source Files"
, [
'.c'
,
'.h'
]],
[
"All Source Files"
, [
qw/.tcl .c .h/
]],
[
"Image Files"
,
'.gif'
],
[
"Image Files"
, [
'.jpeg'
,
'.jpg'
]],
[
"Image Files"
,
''
, [
qw/GIFF JPEG/
]],
[
"All files"
,
'*'
]
);
if
(
$operation
eq
'open'
) {
$file
=
$w
->getOpenFile(
-filetypes
=> \
@types
);
}
else
{
$file
=
$w
->getSaveFile(
-filetypes
=> \
@types
,
-initialfile
=>
'Untitled'
,
-defaultextension
=>
'.txt'
);
}
if
(
defined
$file
and
$file
ne
''
) {
$ent
->
delete
(0,
'end'
);
$ent
->insert(0,
$file
);
$ent
->xview(
'end'
);
}
}