package TkPlotLauncherWindow; use MyTk::MyMainWindow; use MyTk::TkCommon; @ISA = qw(MyMainWindow TkCommon); use strict; use JFile; use Tk; use MyTk::MyNoteBook; use MyTk::MyFrame; use MyTk::MyLabel; use MyTk::MyEntry; use MyTk::MyButton; use MyTk::MyListbox; use MyTk::MyText; use MyTk::MyDragDrop; BEGIN { } sub new { my $class = shift; my $self = MyMainWindow->new(@_); return bless $self, $class; } sub DESTROY { my $this = shift; $this->SUPER::DESTROY(@_); } sub CreateMenu { my ($this) = @_; my $menu = $this->CreateMainMenu(); return $this->MainMenu(); } sub ShowPopupMenu { my ($widget, $this, $x, $y) = @_; } sub CreateWidgets { my ($this) = @_; my $App = $this->GetApplication(); my $IniFile = $App->IniFile(); my $mw = $this; $this->{Notebook} = $mw->MyNoteBook()->pack(-fill => 'both', -expand => 'yes'); # $this->{Notebook} = $mw->Scrolled( # 'MyNoteBook', # -scrollbars => 'e', # -takefocus => 1, # )->pack(-fill => 'both', -expand => 'yes'); my $Frame = $this->MyFrame()->pack(-fill => 'x');#-expand => 'yes', -fill => 'x'); $this->{ExecuteButton} = $Frame->MyButton( -text => '&Execute', -command => [ \&Execute, $this, undef, 'ButtonPressed', 'Execute' ], )->pack(-side => 'left'); $this->{ExecuteButton} = $Frame->MyButton( -text => 'Execute(&Debug)', -command => [ \&Execute, $this, undef, 'ButtonPressed', 'Execute(Debug)' ], )->pack(-side => 'left'); $this->{CloseButton} = $Frame->MyButton( -text => '&Close', -command => [ \&Close, $this, 'ButtonPressed', 'Close' ], )->pack(-side => 'right'); $this->{EditModuleFileButton} = $Frame->MyButton( -text => 'Edit &Module', -command => [ \&Edit, $this, 'ButtonPressed', 'EditModule' ], )->pack(-side => 'right'); $this->{EditIniFileButton} = $Frame->MyButton( -text => 'Edit &IniFile', -command => [ \&Edit, $this, 'ButtonPressed', 'EditIniFile' ], )->pack(-side => 'right'); $this->{ReloadButton} = $Frame->MyButton( -text => '&Reload', -command => [ \&Reload, $this, 'ButtonPressed', 'Reload' ], )->pack(-side => 'right'); $Frame = $this->MyFrame()->pack(-fill => 'x');#-expand => 'yes', -fill => 'x'); $this->{ExplanationEntry} = $Frame->MyText( -width => 50, -height => 5, )->pack(-fill => 'x'); $this->{ExplanationEntry}->configure(-font => ["MS UI Cothic", 10, "normal"]); $Frame = $this->MyFrame()->pack(-fill => 'x');#-expand => 'yes', -fill => 'x'); # $this->{ExplanationEntryFrame} = $this->MakeLabelEntry($Frame, 'Expl:', ''); # $this->{ExplanationEntry} = $this->{ExplanationEntryFrame}->{Entry}; $this->{CommandEntryFrame} = $this->MakeLabelEntry($Frame, 'Command:', ''); $this->{CommandEntry} = $this->{CommandEntryFrame}->{Entry}; $this->{DirEntryFrame} = $this->MakeLabelEntry($Frame, 'Dir:', ''); $this->{DirEntry} = $this->{DirEntryFrame}->{Entry}; $this->{FileNameEntryFrame} = $this->MakeLabelEntry($Frame, 'FileName:', ''); $this->{FileNameEntry} = $this->{FileNameEntryFrame}->{Entry}; $this->ReloadModules(); $this->SetGeometry($IniFile->GetString("Window", "geometry", '')); # $this->OnDestroy( sub { $this->Close(); } ); # $this->{FileNameEntry}->bind('' => sub { $this->Close(); } ); $this->bind('' => sub { $this->App()->IniFile()->WriteString("Window", "geometry", $this->geometry()); } ); return $this; } sub Close { my ($this, $event, $action, $DoConfirm) = @_; $DoConfirm = 0 if(!defined $DoConfirm); my $geometry = $this->geometry(); $this->App()->IniFile()->WriteString("Window", "geometry", $geometry); if($DoConfirm) { my $button = $this->messageBox( -icon => 'warning', -type => 'YesNo', -default => 'No', -title => 'Confirm', -message => 'Really quit?'); return if($button eq 'No'); } $this->destroy(); } sub Edit { my ($this, $event, $action) = @_; my $App = $this->App(); my $IniFilePath = $App->IniFile()->IniFile(); my $EditorPath = $App->IniFile()->GetString("Programs", "Editor", "notepad.exe"); $EditorPath = 'notepad.exe' if(!-e $EditorPath); my $listbox = $this->{CurrentListBox}; if($action eq 'EditIniFile') { system("\"$EditorPath\" \"$IniFilePath\""); # Deps::ExecBackground("notepad.exe \"$IniFilePath\""); } elsif($action eq 'EditModule') { my $title = $listbox->get('active'); my $pInf = $this->{FileInfo}->{$title}; my $path = $pInf->{Path}; system("\"$EditorPath\" \"$path\""); } } sub ReloadModules { my ($this) = @_; my $App = $this->App(); my $IniFile = $App->IniFile(); my $pPageList = $this->{pPageListHash}; if($pPageList) { foreach my $PageName (keys %$pPageList) { $this->{"${PageName}Listbox"}->DeleteAllItem(); } } else { $this->{pPageListHash} = $pPageList = {}; } my $MyDir = $IniFile->MyDir(); $this->{FileInfo} = {}; for(my $i = 1 ; ; $i++) { my %info; my $ProgramName = $IniFile->GetString("ExternalPrograms", "Program$i", ''); last if($ProgramName eq ''); $info{Dir} = 'External'; $info{Path} = ''; $info{FileName} = ''; $info{ModuleName} = 'External'; $info{Title} = $IniFile->GetString($ProgramName, 'Title', ''); $info{Command} = $IniFile->GetString($ProgramName, 'Command', ''); $info{Args} = $IniFile->GetString($ProgramName, 'Args', ''); my $Explanation = $IniFile->GetString($ProgramName, 'Explanation', ''); if($Explanation eq '') { $Explanation = $info{Title}; $Explanation =~ s/^.*?:\s*//; } $info{Explanation} = $Explanation; #print "Add: $info{Title} [$info{ModuleName}]\n"; $this->{FileInfo}->{$info{Title}} = \%info; } my ($drive, $directory, $filename, $ext1, $lastdir, $filebody) = Deps::SplitFilePath($App->SpeculateProgramPath($0, undef)); my $MyDir2 = "$drive$directory"; $MyDir2 =~ s/TkPlot/TkPlot2/i; my @array = qw(dlib); foreach my $dir (@array) { my $fmask = Deps::MakePath($MyDir2, $dir, 0); $fmask = Deps::MakePath($fmask, "*.pm", 0); print "search $fmask ...\n"; my @files = glob($fmask); foreach my $path (@files) { my %info; my $fname = Deps::ExtractFileName($path); my $ModuleName = $fname; $ModuleName =~ s/^Tk//i; $ModuleName =~ s/\.pm//i; next if(!$IniFile->GetString($ModuleName, 'Visible', 1)); $info{Dir} = $dir; $info{Path} = $path; $info{FileName} = $fname; $info{ModuleName} = $ModuleName; $info{PerlScript} = Deps::MakePath($MyDir2, "TkPlot2.pl", 0); my $title = $info{Title} = $IniFile->GetString($ModuleName, 'Title', ''); $title = $ModuleName if($title eq ''); $info{Command} = $IniFile->GetString($ModuleName, 'Command', ''); $info{Args} = $IniFile->GetString($ModuleName, 'Args', ''); my $Explanation = $IniFile->GetString($ModuleName, 'Explanation', ''); if($Explanation eq '') { $Explanation = $title; $Explanation =~ s/^.*?:\s*//; } $info{Explanation} = $Explanation; print "Add: $title [$ModuleName]\n"; $this->{FileInfo}->{$title} = \%info; } } @array = qw(clib dlib); foreach my $dir (@array) { #print "search ./$dir ...\n"; my $fmask = Deps::MakePath($MyDir, $dir, 0); $fmask = Deps::MakePath($fmask, "Tk*.pm", 0); my @files = glob($fmask); foreach my $path (@files) { my %info; my $fname = Deps::ExtractFileName($path); my $ModuleName = $fname; $ModuleName =~ s/^Tk//i; $ModuleName =~ s/\.pm//i; next if(!$IniFile->GetString($ModuleName, 'Visible', 1)); next if(defined $info{ModuleName}); $info{Dir} = $dir; $info{Path} = $path; $info{FileName} = $fname; $info{ModuleName} = $ModuleName; my $title = $info{Title} = $IniFile->GetString($ModuleName, 'Title', ''); $title = $ModuleName if($title eq ''); $info{Command} = $IniFile->GetString($ModuleName, 'Command', ''); $info{Args} = $IniFile->GetString($ModuleName, 'Args', ''); my $Explanation = $IniFile->GetString($ModuleName, 'Explanation', ''); if($Explanation eq '') { $Explanation = $title; $Explanation =~ s/^.*?:\s*//; } $info{Explanation} = $Explanation; #print "Add: $title [$ModuleName]\n"; $this->{FileInfo}->{$title} = \%info; } } # my $h = $IniFile->GetString("ModuleListBox", "height", 25); # my $w = $IniFile->GetString("ModuleListBox", "width", 50); foreach my $title (sort keys %{$this->{FileInfo}}) { my ($PageName) = ($title =~ /^\s*(.*?)\s*:/); $PageName = 'Others' if(!defined $PageName or $PageName eq ''); if(!$pPageList->{$PageName}) { $pPageList->{$PageName}++; $this->{"${PageName}Page"} = $this->{Notebook}->add($PageName, -label => $PageName); my $Frame = $this->{"${PageName}Page"}->MyFrame()->pack(-expand => 'yes', -fill => 'both'); $this->{"${PageName}Listbox"} = $Frame->Scrolled( 'MyListbox', -setgrid => 1, # -height => $h, # -width => $w, -scrollbars => 'e', -takefocus => 1, )->pack(-side => 'left', -expand => 'yes', -fill => 'both'); $this->{"${PageName}Listbox"}->focus; $this->{"${PageName}Listbox"}->bind('' => [ \&SelChangeListBox, $this, 'Key', Ev('K') ] ); $this->{"${PageName}Listbox"}->bind('' => [ \&SelChangeListBox, $this, 'ButtonRelease-1' ] ); $this->{"${PageName}Listbox"}->bind('' => [ \&SelChangeListBox, $this, 'Double-1' ] ); $this->{"${PageName}Listbox"}->SetCurSel(0); $this->{"${PageName}Listbox"}->configure(-font => ["MS UI Cothic", 10, "normal"]); my $Drop = new MyDragDrop(); $Drop->SetDropSite( $this->{"${PageName}Listbox"}, -dropcommand => sub { $this->AccetDrop($this->{"${PageName}Listbox"}, @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); } $this->{"${PageName}Listbox"}->AddItem($title); } } sub AccetDrop { my ($this, $widget, $selection, $a, $b, $c) = @_; #print "this=$this, w=$widget, s=$selection, a=$a, b=$b, c=$c\n"; my $y = $widget->pointery() - $widget->rooty(); my $nearest = $widget->nearest($y); #print "y=$y, $nearest\n"; my $filename; eval { if ( $^O eq 'MSWin32' ) { $filename = $widget->SelectionGet( -selection => $selection, 'STRING' ); } else { $filename = $widget->SelectionGet( -selection => $selection, 'FILE_NAME' ); } }; #print "f=$filename\n"; $this->Execute($widget, 'Drop', $nearest, $filename); } sub Reload { my ($this, $event) = @_; $this->ReloadModules(); } sub SelChangeListBox { my ($listbox, $this, $event, $PressedKey) = @_; # my $lbox = $this->{ModuleListbox}; my $title = $listbox->get('active'); my $IniFile = $this->App()->IniFile(); my $MyDir = $IniFile->MyDir(); #print "event=$event, $ModuleName\n"; if($listbox) { $this->{CurrentListBox} = $listbox; } else { $listbox = $this->{CurrentListBox}; } my $pInf = $this->{FileInfo}->{$title}; return if(!$pInf); #print "title=$title - pInf=$pInf\n"; my $ModuleName = $pInf->{ModuleName}; #print "event=$event key=$PressedKey Module=[$ModuleName]\n"; if($event eq 'ButtonRelease-1' or ($event eq 'Key' and $PressedKey ne 'Return')) { $this->{DirEntry}->SetText($pInf->{Dir}); $this->{FileNameEntry}->SetText($pInf->{FileName}); my $Explanation = $pInf->{Explanation}; $Explanation =~ s/\\n/\n/g; # Jcode::convert(\$Explanation, 'sjis', "sjis"); $this->{ExplanationEntry}->SetText($Explanation); my $cmd; my $PerlScript = (defined $pInf->{PerlScript})? $pInf->{PerlScript} : 'TkPlot.pl'; if($pInf->{Dir} eq 'clib') { $cmd = "perl -w -x -S $PerlScript --style=$ModuleName --LoadModule=clib/Tk$ModuleName --ReadPrev --NoAutoLoadModules=1"; } elsif($pInf->{Dir} eq 'dlib') { $cmd = "perl -w -x -S $PerlScript --style=$ModuleName --ReadPrev"; } $cmd = $pInf->{Command} if($pInf->{Command} ne ''); $cmd = "$cmd $pInf->{Args}" if($pInf->{Args} ne ''); $this->{CommandEntry}->SetText($cmd); } elsif($event eq 'Double-1') { return &Execute($this, $listbox, $event); } elsif($event eq 'Key' and $PressedKey eq 'Return') { return &Execute($this, $listbox, $event); } } sub GetCommandFromTitle { my ($this, $title) = @_; my $pInf = $this->{FileInfo}->{$title}; return if(!$pInf); my $ModuleName = $pInf->{ModuleName}; $this->{DirEntry}->SetText($pInf->{Dir}); $this->{FileNameEntry}->SetText($pInf->{FileName}); my $Explanation = $pInf->{Explanation}; $Explanation =~ s/\\n/\n/g; $this->{ExplanationEntry}->SetText($Explanation); my $cmd; if($pInf->{Dir} eq 'clib') { $cmd = "perl -w -x -S TkPlot.pl --style=$ModuleName --LoadModule=clib/Tk$ModuleName --ReadPrev --NoAutoLoadModules=1"; } elsif($pInf->{Dir} eq 'dlib') { $cmd = "perl -w -x -S TkPlot.pl --style=$ModuleName --ReadPrev"; } $cmd = $pInf->{Command} if($pInf->{Command} ne ''); $cmd = "$cmd $pInf->{Args}" if($pInf->{Args} ne ''); return $cmd; } sub Execute { my ($this, $listbox, $event, $action, $action2) = @_; #print "Execute($this, $listbox, $event, $action, $action2)\n"; $action = '' if(!defined $action); my $IniFile = $this->App()->IniFile(); my $MyDir = $IniFile->MyDir(); print "\n===\n"; # my $lbox = $this->{ModuleListbox}; if($listbox) { $this->{CurrentListBox} = $listbox; } else { $listbox = $this->{CurrentListBox}; } my $title = $listbox ->get('active'); if($event eq 'Drop') { $title = $listbox ->get($action); #print "t=$title: filename=$action2\n"; } print "Execute [event=$event][title=$title]\n"; if(!defined $action2) { $action2 = '' } else { $action2 = " $action2"; } # my $pInf = $this->{FileInfo}->{$title}; my $SetupPath = Deps::MakePath($MyDir, "setup.bat", 0); if($action =~ /debug/i) { my $CMD = 'cmd.exe /K '; my $cmd = $this->{CommandEntry}->GetText(); print " debug: Fork [$CMD][$cmd]\n"; Deps::ExecBackground($CMD . $cmd); } elsif($event =~ /Drop/i) { my $cmd = $this->GetCommandFromTitle($title); print " Drop: cmd=$cmd\n"; Deps::ExecBackground("$cmd$action2"); } else { my $cmd = "perl -x -S $SetupPath TkPlot.pl"; if($cmd =~ /\.exe$/i) { $cmd = $this->{CommandEntry}->GetText(); # system($cmd); print " Execute .exefile: cmd=$cmd\n"; open(OUT132041235, "|$cmd$action2"); close(OUT132041235); } else { #print " Execute [$cmd]\n"; # system($cmd); $cmd = $this->{CommandEntry}->GetText(); #print " Fork [$cmd]\n"; # Deps::ExecBackground("$cmd$action2"); my $CMD = 'cmd.exe /C '; print " Fork [$CMD $cmd]\n"; Deps::ExecBackground("$CMD $cmd$action2"); } } } sub MakeLabelEntry { my ($this, $Frame, $Label, $EntryVal) = @_; my $ChildFrame = $Frame->MyFrame()->pack(-fill => 'x');#-expand => 'yes', -fill => 'x'); $ChildFrame->{Label} = $ChildFrame->MyLabel( -text => $Label )->pack(-side => 'left'); $ChildFrame->{Entry} = $ChildFrame->MyEntry( -text => $EntryVal, -relief => 'ridge', -takefocus => 1, )->pack(-side => 'left', -expand => 'yes', -fill => 'x'); return $ChildFrame; } 1;