package Dialog; #@ISA = qw(AtomTypeObject); #公開したいサブルーチン @EXPORT = qw(); use strict; use Jcode; use Utils; use Deps; use Tk; use Tk::Dialog; use Tk::DialogBox; use Tk::DirTree; use MyTk::MyTkApplication; use MyTk::MyLabel; use MyTk::MyEntry; use MyTk::MyBrowseEntry; use MyTk::MyText; use MyTk::MyCanvas; #use File::Path; #use File::Basename; #use File::Find; #=============================================== # 定数 #=============================================== #ダイアログアイコンの種類 # qw/error info question warning/ my $iconvar = 'info'; #ダイアログボタンの種類 # qw/AbortRetryIgnore OK OKCancel RetryCancel YesNo YesNoCancel/ my $typevar = 'OKCancel'; #=============================================== # パス(読み込みDB) #=============================================== sub ShowAboutDialog { my ($parent, $message) = @_; $message =~ s/\\n/\n/g; return $parent->messageBox( -icon => "info", # "warning" "question" -type => "OK", # "YesNo" "YesNoCancel" "OkCancel" "RetryCancel" -title => 'About', -message => $message ); } sub ShowScaleDialog { my ($parent, $title, $x0, $x1, $y0, $y1, $xscaleplottype, $yscaleplottype, $PlotXZero, $PlotYZero) = @_; # return Dialog::SetupBoxDialog($parent, $title, 10, # "X: ", $x0, " - " , $x1, "", # "Y: ", $y0, " - " , $y1, "" ); my $width = 20; #print "X,Y: $xscaleplottype $yscaleplottype \n"; my $dialog = $parent->DialogBox( -title => $title, -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); my $Frame = $dialog->add('Frame') ->pack(-expand => 'yes', -fill => 'both'); my $Frame0a = $Frame->Frame()->pack(-expand => 'yes', -fill => 'x'); my $XPlotTypeEntry = $Frame0a->MyBrowseEntry( -label => "XPlotType:", -state => "readonly", )->pack(-side => 'left'); my $Frame0b = $Frame->Frame()->pack(-expand => 'yes', -fill => 'x'); my $YPlotTypeEntry = $Frame0b->MyBrowseEntry( -label => "YPlotType:", -state => "readonly", )->pack(-side => 'left'); my @plottypes = qw(x -x log(x) log(|x|) ln(x) ln(|x|) 1/x); $XPlotTypeEntry->AddItem(@plottypes); $YPlotTypeEntry->AddItem(@plottypes); $XPlotTypeEntry->SetText($xscaleplottype); $YPlotTypeEntry->SetText($yscaleplottype); my $Frame1 = $Frame->Frame()->pack(); $Frame1->Label(-text => 'X: ')->pack(-side => 'left'); my $TextV11 = $Frame1->MyEntry(-relief => 'sunken', -width => $width, -takefocus => 1, )->pack(-side => 'left'); $Frame1->Label(-text => ' - ')->pack(-side => 'left'); my $TextV12 = $Frame1->MyEntry(-relief => 'sunken', -width => $width,, -takefocus => 1, )->pack(-side => 'left'); $TextV11->SetText($x0); $TextV12->SetText($x1); $TextV11->focus; $TextV11->selectionRange(0, 'end'); my $Frame2 = $Frame->Frame()->pack(); $Frame2->Label(-text => 'Y: ')->pack(-side => 'left'); my $TextV21 = $Frame2->MyEntry(-relief => 'sunken', -width => $width, -takefocus => 1, )->pack(-side => 'left'); $Frame2->Label(-text => ' - ')->pack(-side => 'left'); my $TextV22 = $Frame2->MyEntry(-relief => 'sunken', -width => $width, -takefocus => 1, )->pack(-side => 'left'); $TextV21->SetText($y0); $TextV22->SetText($y1); my $Frame3 = $Frame->Frame()->pack(); if(defined $PlotXZero) { $Frame3->Checkbutton(-text => "Plot x=0", -variable => \$PlotXZero, -onvalue => 1, -offvalue => 0, -indicatoron => 1)->pack(); } if(defined $PlotYZero) { $Frame3->Checkbutton(-text => "Plot y=0", -variable => \$PlotYZero, -onvalue => 1, -offvalue => 0, -indicatoron => 1)->pack(); } # $dialog->bind("", sub { $dialog->destroy; return 'Cancel'; } ); $dialog->bind("", sub { return $dialog->Exit(); } ); my $button = $dialog->Show; if($button eq 'OK') { $x0 = Utils::DelSpace($TextV11->GetText()) if($TextV11->GetText() =~ /^[\d\.eEdD+-]+/); $x1 = Utils::DelSpace($TextV12->GetText()) if($TextV12->GetText() =~ /^[\d\.eEdD+-]+/); $y0 = Utils::DelSpace($TextV21->GetText()) if($TextV21->GetText() =~ /^[\d\.eEdD+-]+/); $y1 = Utils::DelSpace($TextV22->GetText()) if($TextV22->GetText() =~ /^[\d\.eEdD+-]+/); $xscaleplottype = $XPlotTypeEntry->GetText(); $yscaleplottype = $YPlotTypeEntry->GetText(); return ($x0, $x1, $y0, $y1, $xscaleplottype, $yscaleplottype, $PlotXZero, $PlotYZero); } return undef; } sub SetupBoxDialog { my ($parent, $title, $width, $t11, $v11, $t12, $v12, $t13, $t21, $v21, $t22, $v22, $t23 ) = @_; my $dialog = $parent->DialogBox( -title => $title, -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); my $Frame = $dialog->add('Frame') ->pack(-expand => 'yes', -fill => 'both'); my $Frame1 = $Frame->Frame()->pack(); $Frame1->Label(-text => $t11)->pack(-side => 'left'); my $TextV11 = $Frame1->MyEntry(-relief => 'sunken', -width => $width, -takefocus => 1, )->pack(-side => 'left'); $Frame1->Label(-text => $t12)->pack(-side => 'left'); my $TextV12 = $Frame1->MyEntry(-relief => 'sunken', -width => $width,, -takefocus => 1, )->pack(-side => 'left'); $Frame1->Label(-text => $t13)->pack(-side => 'left'); $TextV11->SetText($v11); $TextV12->SetText($v12); $TextV11->focus; $TextV11->selectionRange(0, 'end'); my $Frame2 = $Frame->Frame()->pack(); $Frame2->Label(-text => $t21)->pack(-side => 'left'); my $TextV21 = $Frame2->MyEntry(-relief => 'sunken', -width => $width, -takefocus => 1, )->pack(-side => 'left'); $Frame2->Label(-text => $t22)->pack(-side => 'left'); my $TextV22 = $Frame2->MyEntry(-relief => 'sunken', -width => $width, -takefocus => 1, )->pack(-side => 'left'); $Frame2->Label(-text => $t23)->pack(-side => 'left'); $TextV21->SetText($v21); $TextV22->SetText($v22); # $dialog->bind("", sub { $dialog->destroy; return 'Cancel'; } ); $dialog->bind("", sub { return $dialog->Exit(); } ); my $button = $dialog->Show; if($button eq 'OK') { $v11 = Utils::DelSpace($TextV11->GetText()) if($TextV11->GetText() =~ /^[\d\.eEdD+-]+/); $v12 = Utils::DelSpace($TextV12->GetText()) if($TextV12->GetText() =~ /^[\d\.eEdD+-]+/); $v21 = Utils::DelSpace($TextV21->GetText()) if($TextV21->GetText() =~ /^[\d\.eEdD+-]+/); $v22 = Utils::DelSpace($TextV22->GetText()) if($TextV22->GetText() =~ /^[\d\.eEdD+-]+/); return ($v11, $v12, $v21, $v22); } return undef; } sub ShowFontDialog { my ($parent, $font_name, $font_size, $font_style) = @_; my @font_families = sort($parent->fontFamilies); my @font_sizes = ( 8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72); my @font_styles = ("normal", "bold", "italic", "bold italic", "roman", "underline", "overstrike"); my $dialog = $parent->DialogBox( -title => 'Fonts', -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); $dialog->{FontName} = $font_name; $dialog->{FontSize} = $font_size; $dialog->{FontStyle} = $font_style; $dialog->{FontName} = $font_families[0] unless($font_name); $dialog->{FontSize} = "10.5" unless($font_size); $dialog->{FontStyle} = "normal" unless($font_style); my $Frame = $dialog->add('Frame') ->pack(-expand => 'yes', -fill => 'both'); my $UpperFrame = $Frame->Frame()->pack(); $dialog->{'NameEntry'} = $UpperFrame->MyBrowseEntry( -label => "Font:", -state => "readonly", # -variable => \$dialog->{FontName}, -choices => \@font_families, -browsecmd => [\&DrawFontDialogCanvas, $dialog], )->pack(-side => 'left'); #print "NE: ", $dialog->{'NameEntry'}, "\n"; $dialog->{'NameEntry'}->SetText($dialog->{FontName}); $dialog->{'SizeEntry'} = $UpperFrame->MyBrowseEntry( -label => "Size:", -width => 5, # -variable => \$dialog->{FontSize}, -choices => \@font_sizes, -browsecmd => [\&DrawFontDialogCanvas, $dialog], )->pack(-side => 'left'); $dialog->{'SizeEntry'}->SetText($dialog->{FontSize}); $dialog->{'StyleEntry'} = $UpperFrame->MyBrowseEntry( -label => "Style:", -width => 8, -state => "readonly", # -variable => \$dialog->{FontStyle}, -choices => \@font_styles, -browsecmd => [\&DrawFontDialogCanvas, $dialog], )->pack(-side => 'left'); $dialog->{'StyleEntry'}->SetText($dialog->{FontStyle}); my $LowerFrame = $Frame->Frame() ->pack(-side => 'bottom', -expand => 'yes', -fill => 'both'); $dialog->{'Canvas'} = $LowerFrame->MyCanvas( -background => 'white', -width => 500, -height => 80, -relief => 'groove', -borderwidth => 3, -cursor => 'crosshair', )->pack(-expand => 'yes', -expand => 'yes', -fill => 'both'); &DrawFontDialogCanvas($dialog); my $button = $dialog->Show; if($button eq 'OK') { # return ($dialog->{FontName}, # $dialog->{FontSize}, # $dialog->{FontStyle}); my $Name = $dialog->{'NameEntry'}->GetText(); my $Size = $dialog->{'SizeEntry'}->GetText(); my $Style = $dialog->{'StyleEntry'}->GetText(); return ($Name, $Size, $Style); } return undef; } sub DrawFontDialogCanvas { my ($dialog) = @_; my $Name = $dialog->{FontName}; my $Size = $dialog->{FontSize}; my $Style = $dialog->{FontStyle}; my $Canvas = $dialog->{'Canvas'}; $Canvas->ClearAll(); $Canvas->SetFont([$Name, $Size, $Style]); $Canvas->DrawText(10, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" . "abcdefghijklmnopqrstuvwxyz", '', 'nw'); } sub ShowEnvDialog { my ($parent) = @_; my $dialog = $parent->DialogBox( -title => 'Environment Variables', -default_button => 'OK', -buttons => ['OK'], ); my $Text = $dialog->add('Scrolled', 'MyText', -scrollbars => 'osoe', -relief => 'sunken', # -width => $width, # -height => $height, ); $Text->pack(-expand => 'yes', -fill => 'both'); # $Text->tagConfigure('normal', # -font => "-*-Courier-Medium-Normal--*-12-*-*-*-*-*-*"); $Text->tagConfigure('normal', -font => "{Courier New} 10"); # $Text->tagConfigure('bold', # -font => "-*-Courier-Medium-B-Normal--*-12-*-*-*-*-*-*"); $Text->tagConfigure('bold', -font => "{Courier New} 10 {bold}"); foreach my $s (sort keys %ENV) { next if(length($s) <= 1); $Text->insert('end', "$s: ", 'bold'); $Text->insert('end', "$ENV{$s}\n", "normal"); } foreach my $s (sort keys %ENV) { next if(length($s) > 1); $Text->insert('end', "$s: ", 'bold'); $Text->insert('end', "$ENV{$s}\n", "normal"); } # $Text->configure(-state => 'disable'); my $button = $dialog->Show; } sub InputDialog2 { my ($parent, $title, $message, $text, $width, $height) = @_; $text =~ s/\\n/\n/g; my $dialog = $parent->DialogBox( -title => $title, -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); $text = '' unless(defined $text); $width = 50 unless(defined $width); $height = 1 unless(defined $height); if($message) { $dialog->add( 'MyLabel', -text => $message, )->pack(-expand => 'yes', -fill => 'x');; } my $Text; if($height > 2) { $Text = $dialog->add('Scrolled', 'MyText', -scrollbars => 'osoe', -relief => 'sunken', -width => $width, -height => $height, -takefocus => 1, ); } else { $Text = $dialog->add('MyEntry', -relief => 'sunken', -width => $width, # -height => $height, -takefocus => 1, ); } $Text->pack(-expand => 'yes', -fill => 'both'); $Text->SetText($text); my $button = $dialog->Show; chomp($text); return undef if($button eq 'Cancel'); return $Text->GetText(); } sub InputDialog { my ($parent, $title, $text, $width, $height) = @_; $text =~ s/\\n/\n/g; my $dialog = $parent->DialogBox( -title => $title, -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); $text = '' unless(defined $text); $width = 50 unless(defined $width); $height = 1 unless(defined $height); my $Text; if($height > 2) { $Text = $dialog->add('Scrolled', 'MyText', -scrollbars => 'osoe', -relief => 'sunken', -width => $width, -height => $height, -takefocus => 1, ); } else { $Text = $dialog->add('MyEntry', -relief => 'sunken', -width => $width, # -height => $height, -takefocus => 1, ); } $Text->pack(-expand => 'yes', -fill => 'both'); $Text->SetText($text); my $button = $dialog->Show; chomp($text); return undef if($button eq 'Cancel'); return $Text->GetText(); } sub ShowDialog { # $icon = 'info' # $DefButton = 'OK' # $buttons = ['OK', 'Cancel'] my ($parent, $title, $text, $icon, $buttons, $DefButton) = @_; $text =~ s/\\n/\n/g; my $dialog = $parent->Dialog( -title => $title, -text => $text, -bitmap => $icon, -default_button => $DefButton, -buttons => $buttons, ); my $button = $dialog->Show; return $button; } sub MessageBox { my ($parent, $title, $message, $type, $icon) = @_; $icon = 'info' unless(defined $icon and $icon ne ''); $type = 'OK' unless(defined $type); $message =~ s/\\n/\n/g; my $button = $parent->messageBox( -icon => $icon, -type => $type, -title => $title, -message => $message, ); return $button; } sub OpenFileDialog { #$operation = 'open' or 'save' my ($parent, $operation, $fmask, $defstr, $title, $initdir) = @_; my $types; my $file; # Type names Extension(s) Mac File Type(s) # #--------------------------------------------------------- my @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", '*'] # ); #print "fm: $fmask\n"; if($fmask eq '*' or $fmask eq '*.*') { $types[0] = ['User files', [ '*' ] ]; $types[1] = ['All files', [ '*' ] ]; } else { my $Description = 'User files'; if($fmask =~ /^\s*(.*?)\s*\((.*?)\)\s*$/) { #print "aa\n"; $Description = $1; $fmask = $2; } my @fmasks = Utils::Split("\\|", $fmask); $types[0] = [$Description, \@fmasks ]; $fmask =~ s/\*//g; $types[1] = ['All files', [ '*' ] ]; } $title = 'Choose file' unless(defined $title); $initdir = '' unless(defined $initdir); if($^O eq 'MSWin32') { $initdir =~ s/\\/\//g; } #print "InitialDir: $initdir\n"; #print "Operation: $operation\n"; if ($operation eq 'open') { $file = $parent->getOpenFile( -title => $title, -filetypes => \@types, -initialfile => $defstr, -initialdir => $initdir); } else { $file = $parent->getSaveFile(-filetypes => \@types, -title => $title, -filetypes => \@types, -initialfile => $defstr, # -defaultextension => '.txt', -initialdir => $initdir); } $file = '' unless(defined $file); return $file; } sub ChooseDirectoryDialog { my ($parent) = @_; my $dir = $parent->chooseDirectory; return $dir; } sub ChooseColorDialog { my($parent, $button, $name, $options) = @_; my $initialColor = "white"; if($name eq '-color') { $initialColor = $options; } elsif(defined $name and $name ne '') { $initialColor = $button->cget($name); } my $color = $button->chooseColor(-title => "Choose a $name color", -initialcolor => $initialColor); return $color; } sub ShowDirectoryTree { my ($parent) = @_; my $dl = $parent->Scrolled('DirTree')->pack(-expand => 1 , -fill => 'both'); return 1; } BEGIN { } sub new { my ($module, $path) = @_; my $this = {}; bless $this; return $this; } sub DESTROY { my $this = shift; } 1;