Plug ins using Tk - hakakou/optiperl GitHub Wiki

Special considerations have been made when using Tk to create a plugIn. See the example below as a template for creating a window from Tk and docking it into optiperl.

icon_idea For a more complex example, see the PlugInDemo.pl in the \plug-ins folder.

#Name: PlugIn Tk Demo
#Description: PlugIn Tk Demo
#Icon: %opti%Tools.icl,146
#Extensions: NewProcess
use Tk;
use Win32::OLE;
 
sub Initialization {
$plug_id = $_[0];
#The first parameter is a handle to the plug-in. We will need this
#for future commands
 
$optiperl = Win32::OLE->new('OptiPerl.Application');
#Initialize OLE object
 
$main = MainWindow->new;
#Initialize TK toplevel window
 
$main->Label(-text => 'OptiPerl Plug-in Demo')->pack;
#Add a label
 
$main->OnDestroy(sub{Finalization()});
#IMPORTANT: Add an event handler when TK destroys itself to call
#the Finalization subroutine.
 
$win = $optiperl->RequestWindow($plug_id);
#Request a dockable window in OptiPerl
 
if (defined $win) {
$win->Show;
#Show the window
 
$win->{Title} = "Plug-in Demo";
#set the windows title
 
ProcessEvents();
#"Magic" subroutine in plugins to force TK to process
#all windows events to create the window.
 
$optiperl->DockWindow($plug_id, hex $main->winfo("id"), $win);
#Dock TK's toplevel window into OptiPerl
 
# The following code creates our menu system and toolbars
 
my $btn = $optiperl->CreateToolItem($plug_id,"button");
# set the buttons options.
# SetOptions(Caption, Hint, Image, Shortcut, On Click subroutine name)
$btn->SetOptions(
     "Button Test","A Button Test","%opti%tools.icl,117",
     "Alt+F5","ButtonTestEvent");
# Note that setoptions is a fast way to set all the settings of
# a tool item. We could also do:
# $btn->{Enabled}=1;
# $btn->{Hint}="test";
# $btn->{Caption}="caption";
# $btn->{Image}="cool.dll,100";
# $btn->{Visible}=1;
# $btn->{Shortcut}="Ctrl+Shift+F1";
# $btn->{OnClick}="SubName";
$win->MainBarLinks->Add($btn,0);
#add the item to the menu of the window
$win->PopUpLinks->Add($btn,0);
#add the item to the pop-up menu (when the arrow is pressed)
$optiperl->ToolBarLinks($plug_id)->Add($btn,0);
#add the menu to the main toolbar assigned to this plugin
#(the one created next to the main menu)
$optiperl->UpdateToolBars($plug_id);
#Necessary! An error will occur if the above is not called after
#messing around with the toolbars.
$optiperl->ToolBarVisible($plug_id,1);
$win->ReDraw;
}
}

sub ButtonTestEvent {
my $handle = $optiperl->Handle;
my $string = $optiperl->InputBox('Plug in','Enter Text:','Default');
$optiperl->OutputAddLine($string);
$main->Label(-text => $string)->pack;
}
 
sub Finalization {
$optiperl->DestroyWindow($plug_id,$win);
$win->Hide;
$optiperl->EndPlugIn($plug_id);
}
 
if (! defined $valid_plugin)
{
sub ProcessEvents {};
Initialization;
MainLoop;
}