package LauncherWindow; use MyTk::MyMainWindow; use MyTk::TkCommon; @ISA = qw(MyMainWindow TkCommon); use strict; use JFile; use MyScript; use MyTk::Dialog; use MyTk::MyFrame; use MyTk::MyLabel; use MyTk::MyEntry; use MyTk::MyButton; use MyTk::MyListbox; use MyTk::MyDragDrop; BEGIN { } sub new { my $class = shift; my $self = MyMainWindow->new(@_); $self->{'OnErrorAction'} = ''; return bless $self, $class; } sub DESTROY { my $this = shift; $this->SUPER::DESTROY(@_); } sub CreateMenu { my ($this) = @_; my $menu = $this->CreateMainMenu(); $this->bind("", sub { $this->Close(1) } ); # メニュー作成 my $FileMenu = $menu->cascade( -label => 'File', -underline => 0, -menuitems => [ [command => '~Quit', -command => [\&Close, $this, 0]], ]); my $HelpMenu = $menu->cascade( -label => 'Help', -underline => 0, -menuitems => [ [command => '~About', -command => [ \&About, $this ]], ]); return $this->MainMenu(); } sub ShowPopupMenu { my ($widget, $this, $x, $y) = @_; my $menu = $this->MainMenu(); #print "menu: $menu\n"; #print "x=$x, $y\n"; $menu->Post($x, $y); } sub CreateWidgets { my ($this) = @_; my $App = $this->GetApplication(); my $nButtonRow = $App->nButtonRow(); my $nButtonColumn = $App->nButtonColumn(); my $ButtonWidth = $App->ButtonWidth(); my $IniFile = $App->IniFile(); # 1列目作成 $this->{'TopFrame1'} = $this->MyFrame()->pack(-expand => 'yes', -fill => 'both'); $this->bind($this->{'TopFrame1'}, '', [ \&ShowPopupMenu, $this, Tk::Ev('X'), Tk::Ev('Y') ]); $this->{'InfoButton'} = $this->{'TopFrame1'}->MyButton( -text => "Info", -command => [\&ShowInfoDialog, $this], )->pack(-side => 'left'); $this->{'EnvVarButton'} = $this->{'TopFrame1'}->MyButton( -text => "EnvVar", -command => [\&ShowEnvVar, $this], )->pack(-side => 'left'); $this->{'EditScriptButton'} = $this->{'TopFrame1'}->MyButton( -text => "Edit Script", -command => [\&EditScript, $this], )->pack(-side => 'left'); $this->{'LauncherButton'} = $this->{'TopFrame1'}->MyButton( -text => '&Launcher', -command => [\&ExecButton, "", $this, "Launcher"], )->pack(-side => 'left'); $this->{'ExitButton'} = $this->{'TopFrame1'}->MyButton( -text => '&Close', -command => [\&ExecButton, "", $this, "Exit"], )->pack(-side => 'right'); $this->{'ExitButton'}->bind('' => [\&RButtonDownExecButton, $this, "Exit", ""]); # 2列目作成 $this->{'TopFrame'} = $this->MyFrame()->pack(-expand => 'yes', -fill => 'both'); $this->bind($this->{'TopFrame'}, '', [ \&ShowPopupMenu, $this, Tk::Ev('X'), Tk::Ev('Y') ]); $this->{'SectionListbox'} = $this->{'TopFrame'}->Scrolled( 'MyListbox', -setgrid => 1, -height => 8, -scrollbars => 'e', -takefocus => 1, )->pack(-side => 'left', -expand => 'yes', -fill => 'both'); $this->{SectionListbox}->focus; $this->{SectionListbox}->bind('' => [\&SelChangeListBox, $this]); $this->{SectionListbox}->bind('' => [\&SelChangeListBox, $this]); $this->{SectionListbox}->bind('' => [\&DBLClickSectionListbox, $this]); $this->{SectionListbox}->SetCurSel(0); my $Drop = new MyDragDrop(); $Drop->SetDropSite( $this->{SectionListbox}, -dropcommand => sub { $this->AcceptDrop($this->{SectionListbox},'SectionListbox', @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); $this->{ExecuteButton} = $this->{'TopFrame'}->MyButton( -text => '&Execute', -command => [\&ExecuteOne, $this], )->pack(); $Drop->SetDropSite( $this->{ExecuteButton}, -dropcommand => sub { $this->AcceptDrop($this->{ExecuteButton},'Execute', @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); $this->{EditorButton} = $this->{'TopFrame'}->MyButton( -text => 'E&ditor', -command => [\&ExecButton, "", $this, "Editor"], )->pack(); $this->{EditorButton}->bind('' => [\&RButtonDownExecButton, $this, "Editor"]); $Drop->SetDropSite( $this->{EditorButton}, -dropcommand => sub { $this->AcceltDrop($this->{EditorButton}, 'Editor', @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); $this->{ShellButton} = $this->{'TopFrame'}->MyButton( -text => '&Shell', -command => [\&ExecButton, "", $this, "Shell"], )->pack(); $this->{ShellButton}->bind('' => [\&RButtonDownExecButton, $this, "Shell"]); $Drop->SetDropSite( $this->{ShellButton}, -dropcommand => sub { $this->AcceptDrop($this->{ShellButton}, 'Shell', @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); $this->{ExplorerButton} = $this->{'TopFrame'}->MyButton( -text => 'Ex&plorer', -command => [\&ExecButton, "", $this, "Explorer"], )->pack(); $this->{ExplorerButton}->bind('' => [\&RButtonDownExecButton, $this, "Explorer"]); $Drop->SetDropSite( $this->{ExplorerButton}, -dropcommand => sub { $this->AcceptDrop($this->{ExplorerButton}, 'Explorer', @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); # 3列目作成 $this->{FileFrame} = $this->MyFrame()->pack(-expand => 'yes', -fill => 'both'); $this->bind($this->{'FileFrame'}, '', [ \&ShowPopupMenu, $this, Tk::Ev('X'), Tk::Ev('Y') ]); $this->{FileLabel} = $this->{FileFrame}->MyLabel( -text => 'File:' )->pack(-side => 'left'); $this->{FileEntry} = $this->{FileFrame}->MyEntry( -text => '', -takefocus => 1, )->pack(-side => 'left', -expand => 'yes', -fill => 'both'); $this->{EditFileButton} = $this->{FileFrame}->MyButton( -text => 'Ed', -takefocus => 1, -command => sub { $this->ExecuteScript("EditFile"); } )->pack(-side => 'left'); $this->{ArgFrame} = $this->MyFrame()->pack(-expand => 'yes', -fill => 'both'); $this->{ArgLabel} = $this->{ArgFrame}->MyLabel( -text => 'Arg:', )->pack(-side => 'left'); $this->{ArgumentEntry} = $this->{ArgFrame}->MyEntry( -takefocus => 1, )->pack(-expand => 'yes', -fill => 'both'); # ボタン群作製 my $count = 0; for(my $i = 0 ; $i < $nButtonRow ; $i++) { $this->{"ButtonFrame$i"} = $this->MyFrame(); for(my $j = 0 ; $j < $nButtonColumn ; $j++) { $count++; my $cm1 = $count-1; $this->{"Button$cm1"} = $this->{"ButtonFrame$i"}->MyButton( -text => "Button$count", -width => $ButtonWidth, -takefocus => 1, )->pack(-side => 'left'); $this->{"Button$cm1"}->bind('' => [\&ExecButton, $this, "$count"] ); $this->{"Button$cm1"}->bind('' => [\&ExecButton, $this, "$count"] ); $this->{"Button$cm1"}->bind('' => [\&RButtonDownExecButton, $this, "$count"]); $Drop->SetDropSite( $this->{"Button$cm1"}, -dropcommand => sub { $this->AcceptDrop($this->mw(), $cm1, @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); } $this->{"ButtonFrame$i"}->pack(); } # メッセージボックス作製 for(my $i = 0 ; $i < 2 ; $i++) { $this->{"MessageEntry$i"} = $this->MyEntry( -relief => 'ridge', -takefocus => 0 )->pack(-expand => 'yes', -fill => 'x'); } # セクションリストの作製 $this->UpdateSectionList(); $Drop->SetDropSite( $this->mw(), -dropcommand => sub { $this->AccetpDrop($this->mw(), 'MainWindow', @_); }, -droptypes => ( $^O eq 'MSWin32' ? 'Win32' : [ 'XDND', 'Sun' ] ) ); # ウィンドウ位置を設定 my $geometry = $this->{'Application'} ->IniFile()->GetString("Window", "geometry", ""); $this->SetGeometry($geometry); $this->Resizable(0); $this->ExecuteScript("Boot"); return $this; } sub AcceptDrop { my ($this, $widget, $label, $selection, $a, $b, $c) = @_; #print "this=$this, w=$widget, l=$label, s=$selection, a=$a, b=$b, c=$c\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->{FileEntry}->SetText($filename); if($label eq 'MainWindow') { return; } elsif($label eq 'SectionListbox') { my $y = $widget->pointery() - $widget->rooty(); my $nearest = $widget->nearest($y); &ExecButton($widget, $this, $label, $nearest); } elsif($label eq 'Execute') { &ExecuteOne($widget, $this); } elsif($label =~ /^\d+$/) { &ExecButton($widget, $this, $label, 'Drop'); } else { &ExecButton($widget, $this, "DragDrop$label"); } } sub SetButtonCaption { my ($this, $section, $Button, $pButtonText) = @_; Jcode::convert(\$section, $this->{'Application'}->{'OSCharCode'}) if($this->{'Application'}->{'OSCharCode'}); my $Caption = $this->GetButtonCaption($section); return $Button->SetTitle($Caption); } sub GetButtonCaption { my ($this, $section) = @_; my $App = $this->{'Application'}; my @ScriptPath = $App->ScriptPaths(); Jcode::convert(\$section, $this->{'Application'}->{'PerlCharCode'}) if($this->{'Application'}->{'PerlCharCode'}); $section = Utils::RegExpQuote($section); my $caption = ''; for(my $i = 0 ; $i < @ScriptPath ; $i++) { my $fname = $ScriptPath[$i]; my $infile = new IniFile($fname); $caption = $infile->GetString($section, "Caption", ""); last if($caption); } return $caption; } sub UpdateSectionList { my ($this) = @_; my $App = $this->{'Application'}; my @ScriptPath = $App->ScriptPaths(); my $FirstSection = ''; for(my $i = 0 ; $i < @ScriptPath ; $i++) { my $fname = $ScriptPath[$i]; #$App->print("Script$i: [$fname]\n"); unless(open(IN, "<$fname")) { $App->print("Error: Can not open [$fname].\n"); next; } while(!eof(IN)) { while() { #$App->print("line1: $_"); if($_ =~ /^\[DBLClick\.(.*?)\]/i) { my $section = $1; Utils::DelSpace($section); Jcode::convert(\$section, $this->{'Application'}->{'OSCharCode'}) if($this->{'Application'}->{'OSCharCode'}); $this->{'SectionListbox'}->insert("end", $section); $FirstSection = $section if($FirstSection eq ''); last; } } } close(IN); } $this->{'SectionListbox'}->SetCurSel(0); $this->SetSection($FirstSection); } sub SetSection { my ($this, $section) = @_; my $App = $this->{'Application'}; my @ScriptPath = $App->ScriptPaths(); for(my $i = 0 ; $i < $App->{'nTotalButtons'} ; $i++) { $this->{"Button$i"}->SetTitle(''); } my $fname = $this->FindScriptIniFile("DBLClick.$section"); unless($fname) { $App->print("Can not find [DBLClick.$section] in Script Files.\n"); return -1; } my $PerlCharCode = $this->{'Application'}->{'PerlCharCode'}; Jcode::convert(\$section, $PerlCharCode) if($PerlCharCode); $section = Utils::RegExpQuote($section); my $infile = new JFile; next unless($infile->Open($fname, "r", $this->{'Application'}->{'PerlCharCode'})); while(!$infile->eof()) { my $iButton = 0; my $line; while($line = $infile->ReadLine()) { last if($infile->eof()); Utils::DelSpace($line); next unless($line); #$App->print("line1: $line\n"); if($line =~ /^\[Button(\d+)\.$section\]/i) { $iButton = $1; last; } } while($line = $infile->ReadLine()) { last if($infile->eof()); Utils::DelSpace($line); next unless($line); last if($line =~ /^\[/); #$App->print("line2: $line\n"); if($line =~ /^\s*Caption\s*=\s*(.*)$/i) { my $caption = $1; $caption = '' unless($caption); Utils::DelSpace($caption); my $cm1 = $iButton - 1; $this->{"Button$cm1"}->SetText($caption); last; } } } $infile->Close(); } sub FindScriptIniFile { my ($this, $section) = @_; my $App = $this->{'Application'}; my @ScriptPath = $App->ScriptPaths(); my $PerlCharCode = $App->{'PerlCharCode'}; $section = "[$section]" unless($section =~ /^\[.*\]$/); my $RegSec = Utils::RegExpQuote("$section"); Jcode::convert(\$RegSec, $PerlCharCode) if($PerlCharCode); #$App->print("section: $RegSec\n"); for(my $i = 0 ; $i < @ScriptPath ; $i++) { my $fname = $ScriptPath[$i]; my $infile = new JFile; unless( $infile->Open($fname, "r", $PerlCharCode) ) { $App->print("Can not open [$fname].\n"); return undef; } if($infile->SkipTo("^$RegSec", 0)) { $infile->Close(); return $fname; } } return undef; } sub ExecuteACommand { my ($this, $line, $infile) = @_; my $App = $this->{'Application'}; my $IniFile = $App->{'IniFile'}; my $list_box = $App->{'SectionListbox'}; $App->print(" :$line\n"); $this->ShowMessage(0, "$line"); my $orgline = $line; $line = MyScript::ConvertCommand($line); $App->DebugPrint(" Conv: $line\n") if($orgline ne $line); $this->ShowMessage(0, "$line"); if($App->{'DoConfirm'}) { my $ret = Dialog::MessageBox($this, "Confirmation", "Execute [$line]?", "OKCancel", ""); return -1 if($ret =~ /cancel/i); } my ($head, $args) = ($line =~ /^([^\s]+)\s+(.*)$/); $head = $line unless($head); $args = '' unless($args); my $OriginalHead = $head; $head = lc $head; if($head eq 'settitle') { $this->SetTitle($args); } elsif($head eq 'getcurdir') { $ENV{$args} = Deps::GetWorkingDirectory(); } elsif($head eq 'set') { my ($varname, $var) = ($args =~ /^([^\s]+?)\s*=\s*(.*)$/); if($var) { Utils::DelSpace($var); $ENV{$varname} = $var; $App->DebugPrint("ENV{$varname}=$ENV{$varname}\n"); } } elsif($head eq 'addpath') { Deps::AddPath($args); } elsif($head eq 'chdir' or $head eq 'cd') { chdir($args) if($args); $App->DebugPrint("cwd: " . Deps::GetWorkingDirectory() . "\n"); } elsif($head eq 'mkdir' or $head eq 'md') { mkdir($args); } elsif($head eq 'delete' or $head eq 'del') { unlink($args); } elsif($head =~ /^getopen(dos)?filename$/) { my ($fmask, $defstr, $message) = ($args =~ /^([^\s]+)\s+([^\s]+)\s+(.*)$/); $fmask = Utils::DelQuote($fmask); $defstr = Utils::DelQuote($defstr); $message = Utils::DelQuote($message); #$App->print(" $head: [$fmask] [$defstr] [$message]\n"); my $sel = $this->{'SectionListbox'}->get('active'); my $dir = $IniFile->GetString("Preferences", "${sel}WorkDir", ""); #$App->print("Change dir to [$dir] (${sel}WorkDir)\n"); chdir($dir); my $filepath = Dialog::OpenFileDialog($this, 'open', $fmask, $defstr, $message, $dir); return -1 unless($filepath); $ENV{'o'} = $this->{'FileEntry'}->SetText($filepath); my ($drive, $directory, $filename, $ext, $lastdir, $filebody) = Deps::SplitFilePath($filepath); $IniFile->WriteString("Preferences", "${sel}WorkDir", "$drive$directory"); } elsif($head =~ /^getsave(dos)?filename$/) { my ($fmask, $defstr, $message) = ($args =~ /^([^\s]+)\s+([^\s]+)\s+(.*)$/); $fmask = Utils::DelQuote($fmask); $defstr = Utils::DelQuote($defstr); $message = Utils::DelQuote($message); #$App->print(" $head: [$fmask] [$defstr] [$message]\n"); my $sel = $this->{'SectionListbox'}->get('active'); my $wdir = $IniFile->GetString("Preferences", "${sel}WorkDir", ""); chdir($wdir); my $filepath = Dialog::OpenFileDialog($this, 'save', $fmask, $defstr, $message, $wdir); return -1 unless($filepath); $ENV{'o'} = $this->{'FileEntry'}->SetText($filepath); my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = Deps::SplitFilePath($filepath); $IniFile->WriteString("Preferences", "${sel}WorkDir", "$drive$dir"); } elsif($head =~ /^getdrive$/) { my ($path1, $envvar) = ($args =~ /^(.*)\s+(.*?)$/); #$App->print(" GetDrive: [$path1] [$envvar]\n"); unless(defined $path1) { $App->print("Invalid option for GetDrive: $args.\n"); return -2; } my ($drive) = ($path1 =~ /^([a-zA-Z]:)/); $ENV{$envvar} = $drive if($drive); } elsif($head =~ /^getdrive$/ or $head =~ /^getdirectory$/ or $head =~ /^getlastdirectory$/ or $head =~ /^getfilename$/ or $head =~ /^getfilebody$/ or $head =~ /^getext$/) { my ($path1, $envvar) = ($args =~ /^(.*)\s+(.*?)$/); #$App->print(" GetFiles: [$path1] [$envvar]\n"); unless(defined $path1) { $App->print("Invalid option for $head: $args.\n"); return -2; } my ($drive, $dir, $filename, $ext, $lastdir, $filebody) = Deps::SplitFilePath($path1); if($head =~ /^getdrive$/) { $ENV{$envvar} = $drive; } if($head =~ /^getdirectory$/) { $ENV{$envvar} = $dir; } elsif($head =~ /^getlastdirectory$/) { $ENV{$envvar} = $lastdir if(defined $lastdir); #$App->print("dir: $dir lastdir: $lastdir\n"); } elsif($head =~ /^getfilename$/) { $ENV{$envvar} = $filename; } elsif($head =~ /^getfilebody$/) { $ENV{$envvar} = $filebody; } elsif($head =~ /^getext$/) { $ENV{$envvar} = $ext; } } elsif($head =~ /^dtounixpath$/ or $head =~ /^utodospath$/) { my ($path1, $envvar) = ($args =~ /^(.*)\s+(.*?)$/); unless(defined $path1) { $App->print("Invalid option for $head: $args.\n"); return -2; } $path1 =~ s/\\/\//g if($head =~ /^dtounixpath$/); $path1 =~ s/\//\\/g if($head =~ /^utodospath$/); $ENV{$envvar} = $path1; } elsif($head =~ /^dtounixpath$/) { my ($path1, $envvar) = ($args =~ /^(.*)\s+(.*?)$/); unless(defined $path1) { $App->print("Invalid option for $head: $args.\n"); return -2; } $path1 =~ s/\\/\//g; $ENV{$envvar} = $path1; } elsif($head =~ /^showmessage$/) { $this->ShowMessage(0, $args); } elsif($head =~ /^delquote$/) { my ($envvar, $str) = ($args =~ /^([^\s]+)\s+(.*)$/); unless(defined $envvar) { $App->print("Invalid argument: DelQuote [$args]\n"); return -2; } $ENV{$envvar} = Utils::DelQuote($str); } elsif($head =~ /^exit$/) { return -1; } elsif($head =~ /^exitifnotdefined$/) { return -1 unless($ENV{$args}); } elsif($head =~ /^exitifdefined$/) { return -1 if($ENV{$args}); } elsif($head =~ /^exitifexist$/) { return -1 if(-e $args); } elsif($head =~ /^exitifnotexist$/) { return -1 unless(-e $args); } elsif($head =~ /^iflinux/) { return $this->ExecuteACommand($args, $infile) if(Deps::OS() eq 'linux'); } elsif($head =~ /^ifwindows/) { return $this->ExecuteACommand($args, $infile) if(Deps::OS() eq 'MSWin32'); } elsif($head =~ /^ifexist/) { my ($path, $script) = ($args =~ /^([^\s]+)\s+(.*)$/); return $this->ExecuteACommand($script, $infile) if(-e $path); } elsif($head =~ /^ifnotexist/) { my ($path, $script) = ($args =~ /^([^\s]+)\s+(.*)$/); return $this->ExecuteACommand($script, $infile) unless(-e $path); } elsif($head =~ /^iffileexist/) { my ($path, $script) = ($args =~ /^([^\s]+)\s+(.*)$/); return $this->ExecuteACommand($script, $infile) if(-f $path); } elsif($head =~ /^ifnotfileexist/) { my ($path, $script) = ($args =~ /^([^\s]+)\s+(.*)$/); return $this->ExecuteACommand($script, $infile) unless(-f $path); } elsif($head =~ /^ifdirectoryexist/) { my ($path, $script) = ($args =~ /^([^\s]+)\s+(.*)$/); return $this->ExecuteACommand($script, $infile) if(-d $path); } elsif($head =~ /^ifnotdirectoryexist/) { my ($path, $script) = ($args =~ /^([^\s]+)\s+(.*)$/); return $this->ExecuteACommand($script, $infile) unless(-d $path); } elsif($head =~ /^input$/) { my ($envvar, $message) = ($args =~ /^([^\s]+)\s+(.*)$/); unless(defined $envvar) { $App->print("Invalid argument: $head [$args]\n"); return -2; } $message = "Input" unless($message); my $str = $ENV{$envvar}; $str =~ s/\\n/\n/g; my $ret = Dialog::InputDialog($this, $message, $str, 50, 5); if(defined $ret) { $ret =~ s/\n/ /g; $ret =~ s/\s*$//; $ENV{$envvar} = $ret; } else { return -1; } } elsif($head =~ /^loadmenu$/) { $this->SetSection($ENV{'ExecutingSection'}); } elsif($head =~ /^updatecaption$/) { # &SetButtonCaption($ENV{'ExecutingSection'}, # $LauncherButton, \$LauncherButtonText); } elsif($head =~ /^copy/) { my ($source, $target) = ($args =~ /^([^\s]+)\s+(.*)$/); #print "copy $source $target\n"; unless(defined $source) { $App->print("Invalid argument: $head [$args]\n"); return -2; } if($head =~ /^copyifexist$/ and ! -e $source) { $App->print(" CopyIfExist: Source [$target] does not exist.\n"); return -3; } if($head =~ /^copyifnotexist$/ and -e $target) { $App->print(" CopyIfNotExist: Target [$target] exists.\n"); return -3; } if(Utils::CopyFile($source, $target) <= 0) { Dialog::MessageBox($this, "Copy Error", "Can not copy [$source] to [$target].\n" . "Script terminated.\n"); return -4; } } elsif($head =~ /^readini$/) { my ($inifile, $section, $key, $env, $defval) = ($args =~ /^(.*)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)?$/); if(!defined $inifile) { ($inifile, $section, $key, $env) = ($args =~ /^(.*)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)$/); } unless(defined $inifile) { $App->print("Invalid argument: $head [$args]\n"); return -2; } my $ifl = new IniFile($inifile); my $str = $ifl->GetString($section, $key, ""); if($str) { $ENV{$env} = $str; } else { if(!defined $defval) { $App->print("Error: ReadIni: Value for $section/$key is not found [$inifile].\n"); } else { $ENV{$env} = $defval; } } } elsif($head =~ /^writeini$/) { my ($inifile, $section, $key, $str) = ($args =~ /^(.*)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)$/); unless(defined $inifile) { $App->print("Invalid argument: $head [$args]\n"); return -2; } my $ifl = new IniFile($inifile); my $ret = $ifl->WriteString($section, $key, $str); unless($ret) { $App->print("Error: WriteIni: Can not write to [$inifile].\n"); } } elsif($head =~ /^decomposeparameters$/) { my @a = Utils::Split("\\s+", $args); my $FileCount = 1; foreach my $s (@a) { my ($key, $val) = ($s =~ /^(.*?)=(.*)$/); if(!defined $key) { $key = "ArgFile$FileCount"; $val = $s; $FileCount++; } $ENV{$key} = $val; # print "$val: $key\n"; } } elsif($head =~ /^goto$/) { $args =~ s/^\s*\[(.*)\]\s*?$/$1/; $this->ExecuteScript($args); return 1; } elsif($head =~ /^gosub$/) { $args =~ s/^\s*\[(.*)\]\s*?$/$1/; $this->ExecuteScript($args); } elsif($head =~ /^debug$/) { if($args =~ /(0|off)/i) { $App->SetDebug(0); } else { $App->SetDebug(1); } } elsif($head =~ /^confirm$/) { if($args =~ /(0|off)/i) { $App->SetDoConfirm(0); } else { $App->SetDoConfirm(1); } } elsif($head =~ /^message$/) { Jcode::convert(\$args, $App->{'OSCharCode'}) if($App->{'OSCharCode'}); my $ret = Dialog::MessageBox($this, "Confirm", $args, "YESNO"); return 1 if($ret =~ /yes/i); return -1; } elsif($head =~ /^waitprocess$/) { #何もしない } elsif($head =~ /^showwindow$/) { $this->ShowWindow($args); } elsif($head =~ /^onerror$/) { $App->print(" OnError: Do [$args] on error.\n"); $this->{'OnErrorAction'} = $args; } elsif($head =~ /^getheader$/ or $head =~ /^showwindow$/ or $head =~ /^clearerrormessage$/ or $head =~ /^call$/ or $head =~ /^getshortpathname$/ or $head =~ /^setargs$/ or $head =~ /^getappfile$/ or $head =~ /^getappdosfile$/ ) { $App->print("$head is not implemented.\n"); return 1; } else { my $cmdline = "$OriginalHead $args"; #$App->print("Launch [$cmdline].\n"); $this->ShowMessage(0, "Running $cmdline..."); unless($infile->PreReadLine() =~ /^WaitProcess/i) { if($App->OS() eq 'MSWin32') { $cmdline = "\"$ENV{'StartApp'}\" $cmdline"; } else { $cmdline = "$cmdline&"; } } Jcode::convert(\$cmdline, $App->{'OSCharCode'}) if($App->{'OSCharCode'}); my $ret = system($cmdline); unless($ret / 256 <= 32) { $App->DebugPrint(" ***Invalid command: [$head]: " ."[$args] [ret: ", $ret / 256, "]\n"); } $this->ShowMessage(0, "$cmdline terminated."); } # $App->print(" Finished: $line\n"); return 1; } sub ExecuteScript { my ($this, $section) = @_; my $App = $this->{'Application'}; my @ScriptPath = $App->ScriptPaths(); $App->print("***Execute [$section]\n"); $this->{'OnErrorAction'} = 'stop'; $this->UpdateEnvVars(); $ENV{'ExecutingSection'} = $section; Jcode::convert(\$section, $App->{'PerlCharCode'}) if($App->{'PerlCharCode'}); my $fname = $this->FindScriptIniFile($section); unless($fname) { $App->print("Can not find $section.\n"); return -1; } my $RegSection = Utils::RegExpQuote($section); #$App->DebugPrint("Search in $fname...\n"); my $infile = new JFile; unless($infile->Open($fname, "r", $App->{'PerlCharCode'})) { $App->print("Can not read [$fname].\n"); next; } my $IsFound = 0; my $line; while($line = $infile->ReadLine()) { if($line =~ /^\[$RegSection\]/i) { $IsFound = 1; last; } } unless($IsFound) { $App->print("Script [$section] was not found.\n"); $infile->Close(); return -1; } while($line = $infile->ReadLine()) { Utils::DelSpace($line); $App->DebugPrint(" line: [$line]\n"); next if($line eq ''); next if($line =~ /^#/); next if($line =~ /^;/); next if($line =~ /^rem\s/); next if($line =~ /^\s*Caption\s*=/i); last if($line =~ /^\[/); last if($line =~ /^End(\s|$)/); if($line =~ /^Bye\s/i or $line =~ /^Bye$/i) { $infile->Close(); return $this->Close(); } my $ret = $this->ExecuteACommand($line, $infile); #print "OnError: [", $this->{'OnErrorAction'}, "] [ret=$ret] \n"; if($ret < 0) { if($this->{'OnErrorAction'} =~ /^continue$/i) { #print " Continue\n"; next; } $this->ShowMessage(1, 'Error occurs. Script terminated.'); last; } } $infile->Close(); $App->print(" +Finished: [$section]\n"); return 1; } sub UpdateEnvVars { my ($this) = @_; my $IniFile = $this->{'Application'}->{'IniFile'}; $ENV{'p'} = $IniFile->ProgramFile(); $ENV{'i'} = $IniFile->IniFile() if($IniFile->IniFile()); $ENV{'s'} = $IniFile->MyDir() if($IniFile->MyDir()); $ENV{'w'} = $IniFile->WinDir() if($IniFile->WinDir()); $ENV{'o'} = $this->{'FileEntry'}->GetText(); my $s = ''; for(my $i = 0 ; $i < @ARGV ; $i++) { $ENV{$i} = $ARGV[$i]; $s .= "$ARGV[$i] "; } $s =~ s/ $//; $ENV{'a'} = $s; return 1; } sub ShowMessage { my ($this, $iMessageEntry, $message) = @_; $this->{"MessageEntry$iMessageEntry"}->delete(0, 'end'); $this->{"MessageEntry$iMessageEntry"}->insert('end', $message); } sub SelChangeListBox { my ($listbox, $this) = @_; my $lbox = $this->{'SectionListbox'}; my $sel = $lbox->get('active'); $this->SetSection($sel); } sub DBLClickSectionListbox { my ($listbox, $this) = @_; my $lbox = $this->{'SectionListbox'}; my $sel = $lbox->get('active'); $this->ExecuteScript("DBLClick.$sel"); } sub ExecButton { my ($button, $this, $iButton, $Option) = @_; my $lbox = $this->{'SectionListbox'}; my $sel = ''; $sel = $lbox->get('active') if(defined $lbox); if($iButton =~ /^\d+$/) { if(defined $Option and $Option eq 'Drop') { $this->ExecuteScript("Button$iButton.${sel}.DragDrop"); } else { $this->ExecuteScript("Button$iButton.$sel"); } } elsif($iButton eq 'SectionListbox' and defined $lbox) { my $sel = $lbox->get($Option); #print "Exec [DragDrop.$sel]\n"; $this->ExecuteScript("DragDrop.$sel"); } else { $this->ExecuteScript("$iButton"); } } sub RButtonDownExecButton { my ($button, $this, $iButton) = @_; my $lbox = $this->{'SectionListbox'}; my $sel = ''; $sel = $lbox->get('active') if(defined $lbox and $lbox ne ''); if($iButton =~ /^\d+$/) { $ENV{'alsect'} = "Button$iButton.$sel"; } else { $ENV{'alsect'} = "$iButton"; } $ENV{"alfname"} = $this->FindScriptIniFile($ENV{'alsect'}); $this->ExecuteScript("ProgButtonEdit"); } sub EditScript { my ($this) = @_; } sub ExecuteOne { my ($this) = @_; my $App = $this->{'Application'}; $App->DebugPrint("ExecuteOne\n"); my $command = Dialog::InputDialog($this, "Input command:"); return unless($command); $command = MyScript::ConvertCommand($command); system($command); $this->ShowMessage(0, "$command terminated."); } sub ShowEnvVar { my ($this) = @_; $this->UpdateEnvVars(); Dialog::ShowEnvDialog($this); } sub ShowInfoDialog { my ($this) = @_; $this->UpdateEnvVars(); $this->GetApplication()->ShowInfoDialog($this); } sub About { my ($this) = @_; Dialog::ShowAboutDialog($this, ucfirst $this->{'Program'} . " 2005/pl"); } sub Close { my ($this, $DoConfirm) = @_; my $geometry = $this->geometry(); $this->{'Application'}->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(); } 1;