CPANPLUS-Shell-Wx
view release on metacpan or search on metacpan
lib/CPANPLUS/Shell/Wx/ModuleTree.pm view on Meta::CPAN
use vars qw( @ISA $VERSION);
@ISA = qw( Wx::TreeCtrl);
$VERSION = '0.01';
}
#use some constants to better identify what's going on,
#so we can do stuff like:
# $self->{'sort'}=(SORTBY)[CATEGORY]; #sort by category
use constant SORTBY => (_T("Author"), _T("Name"), _T("Category"));
use constant {AUTHOR=>0,NAME=>1,CATEGORY=>2};
use constant SHOW => (_T("Installed"),_T("Updated"),_T("New"),_T("All")) ;
use constant {INSTALLED=>0,UPDATES=>1,NEW=>2,ALL=>3};
use constant MAX_PROGRESS_VALUE => 100000; #the max value of the progressdialogs
sub new {
my $class = shift;
my $self = $class->SUPER::new(); # create an 'empty' TreeCtrl object
#set default behavior
$self->{'sort'}=(SORTBY)[CATEGORY]; #DEFAULT: sort by category
# $self->{'sort'}=(SORTBY)[AUTHOR]; #sort by author
# $self->{'sort'}=(SORTBY)[NAME]; #sort by module name
$self->{'show'}=(SHOW)[INSTALLED];
# $self->{'show'}=(SHOW)[UPDATES]; #DEFAULT: List Updated Modules
# $self->{'show'}=(SHOW)[NEW];
# $self->{'show'}=(SHOW)[ALL];
#this is the thread reference to the info gathering method
#this is started ad stopped when the listbox's selection is changed
#threads may be removed in the future or not used at all.
$self->{_threads}=();
#setup category names for further use.
#(they will be used to create hashes)
$self->{catNames}=[
_T("Not In Modulelist"), _T("Perl Core Modules"),
_T("Language Extensions"), _T("Development Support"),
_T("Operating System Interfaces"), _T("Networking Devices, IPC"),
_T("Data Type Utilities"), _T("Database Interfaces"),
_T("User Interfaces"), _T("Language Interfaces"),
_T("File Names, Systems Locking"), _T("String/Language/Text Processing"),
_T("Options/Arguments/Parameters Processing"), _T("Internationalization, Locale"),
_T("Security and Encryption"), _T("World Wide Web, HTML, HTTP, CGI"),
_T("Server and Daemon Utilities"), _T("Archiving and Compression"),
_T("Images, Pixmaps, Bitmaps"), _T("Mail and Usenet News"),
_T("Control Flow Utilities"), _T("File Handle Input/Output"),
_T("Microsoft Windows Modules"), _T("Miscellaneous Modules"),
_T("Commercial Software Interfaces"), _T("Bundles"),
_T("Documentation"), _T("Pragma"),
_T("Perl6")];
#add the root item.It is hidden.
$self->AddRoot(_T('Modules'));
#create links to events
# EVT_WINDOW_CREATE( $self, $self, \&OnCreate ); #when the tree is created
EVT_TREE_SEL_CHANGED( $self, $self, \&OnSelChanged); #when a user changes the selection
EVT_TREE_ITEM_ACTIVATED($self, $self, \&OnDblClick); #When the user double-clicks an item
EVT_TREE_ITEM_RIGHT_CLICK( $self, $self, \&ShowPopupMenu );#when the user wants a pop-up menu
$self->SetWindowStyleFlag($self->GetWindowStyleFlag()|wxVSCROLL);
return $self;
}
#this is called when the control is created.
#sub OnCreate {
sub Init {
my $self = shift;
my ($event)=@_;
#get references so we can access them easier
# $self->{parent}=Wx::Window::FindWindowByName('main_window');
# $self->{parent}=$self->GetParent();
# $self->{cpan}=$self->{parent}->{cpan};
# $self->{config}=$self->{cpan}->configure_object();
#$self->AssignImageList($imgList);
# Wx::Window::FindWindowByName('info_prereqs')->AssignImageList($imgList);
#show info on what we are doing
Wx::LogMessage _T("Showing "),$self->{'show'},_T(" by "),$self->{'sort'};
#go ahead and get the list of categories
$self->{category_list}=$self->_get_categories();
#$self->{statusBar}=Wx::Window::FindWindowByName('main_window_status');
#populate tree with default values
#$self->Populate();
#$self->{podReader}=$self->{parent}->{podReader} || CPANPLUS::Shell::Wx::PODReader::Frame->new($self);
$self->SetWindowStyle($self->GetWindowStyleFlag()|wxVSCROLL|wxALWAYS_SHOW_SB);
_uShowErr;
}
###############################
####### PUBLIC METHODS ########
###############################
#these methods are called from outside to display the relevant modules
sub ShowInstalled{shift->_switch_show(INSTALLED)}
sub ShowUpdated{shift->_switch_show(UPDATES)}
sub ShowNew{shift->_switch_show(NEW)}
sub ShowAll{shift->_switch_show(ALL)}
sub SortByAuthor{shift->_switch_sort(AUTHOR)}
sub SortByName{shift->_switch_sort(NAME)}
sub SortByCategory{shift->_switch_sort(CATEGORY)}
#the following methods are for setting the event handlers for the various
# menu items in the context menu. They all take one parameter:a code ref
#The code ref is then executed with three parameters:
# the menu [Wx::Menu], the event [Wx::CommandEvent], and the name of the selected module
sub SetInfoHandler{$_[0]->{_minfoHandler}=$_[1];}
sub SetInstallMenuHandler{print "Install: ",@_;$_[0]->{_minstallHandler}=$_[1];}
sub SetUpdateMenuHandler{$_[0]->{_mupdateHandler}=$_[1];}
sub SetUninstallMenuHandler{$_[0]->{_muninstallHandler}=$_[1];}
sub SetFetchMenuHandler{$_[0]->{_mfetchHandler}=$_[1];}
sub SetPrepareMenuHandler{$_[0]->{_mprepareHandler}=$_[1];}
sub SetBuildMenuHandler{$_[0]->{_mbuildHandler}=$_[1];}
sub SetTestMenuHandler{$_[0]->{_mtestHandler}=$_[1];}
sub SetExtractMenuHandler{$_[0]->{_mextractHandler}=$_[1];}
sub SetClickHandler{$_[0]->{_clickHandler}=$_[1];}
sub SetDblClickHandler{print "DblClick:",@_,"\n";$_[0]->{_dblClickHandler}=$_[1];}
sub SetStatusBar{$_[0]->{statusBar}=$_[1];}
sub SetMenu{$_[0]->{menu}=$_[1];}
sub GetName{return $_[0]->{thisName}}
sub GetMod{return $_[0]->{thisMod}}
sub SetCPP{$_[0]->{cpan}=$_[1];$_[0]->{config}=$_[1]->configure_object();}
#this is called when the user right-clicks on an item in the tree
sub ShowPopupMenu{
my $self = shift;
my ($event)=@_;
#we can't do any actions on unknown modules
return if $self->GetItemImage($event->GetItem()) == 4;
#create the menu
$menu = CPANPLUS::Shell::Wx::ModuleTree::Menu->new($self,$event->GetItem());
#show the menu
$self->PopupMenu($menu,$event->GetPoint());
}
#this is called when the user double-clicks on an item in the tree
sub OnDblClick{
my $self = shift;
my ($event)=@_;
#we can't do any actions on unknown modules
my $img=$self->GetItemImage($event->GetItem());
print "Double Click!:".$self->{_dblClickHandler}." img = $img\n";
return if $img == 4;
&{$self->{_dblClickHandler}}(@_) if $self->{_dblClickHandler};
}
#this method calls the other methods to populate the tree
sub Populate{
my $self = shift;
$self->OnSelChanged(); #clear all values in Info pane
$self->DeleteAllItems(); #clear all items in tree
#add the root item with the name of what we are showing
my $root=$self->AddRoot($self->{'show'});
#tell the user we are populating the list
$self->{statusBar}->SetStatusText(_T("Populating List..").$self->{'show'}._T(" By ").$self->{'sort'});
#call the appropriate method for displaying the modules
if ($self->{'sort'} eq (SORTBY)[AUTHOR]){
$self->_show_installed_by_author() if ( $self->{'show'} eq (SHOW)[INSTALLED]);
$self->_show_updates_by_author() if ( $self->{'show'} eq (SHOW)[UPDATES]);
$self->_show_new_by_author() if ( $self->{'show'} eq (SHOW)[NEW]);
$self->_show_all_by_author() if ( $self->{'show'} eq (SHOW)[ALL]);
}
if ($self->{'sort'} eq (SORTBY)[NAME]){
$self->_show_installed_by_name() if ( $self->{'show'} eq (SHOW)[INSTALLED]);
$self->_show_updates_by_name() if ( $self->{'show'} eq (SHOW)[UPDATES]);
$self->_show_new_by_name() if ( $self->{'show'} eq (SHOW)[NEW]);
$self->_show_all_by_name() if ( $self->{'show'} eq (SHOW)[ALL]);
}
if ($self->{'sort'} eq (SORTBY)[CATEGORY]){
$self->_show_installed_by_category() if ( $self->{'show'} eq (SHOW)[INSTALLED]);
$self->_show_updates_by_category() if ( $self->{'show'} eq (SHOW)[UPDATES]);
$self->_show_new_by_category() if ( $self->{'show'} eq (SHOW)[NEW]);
$self->_show_all_by_category() if ( $self->{'show'} eq (SHOW)[ALL]);
}
#show any errors generated by CPANPLUS
_uShowErr;
}
#update only info tab in the lower notebook and clear other items
sub OnSelChanged{
my ($self,$event)=@_;
#set global variable for name of what the user selected
$self->{thisName}=$self->GetItemText($self->GetSelection());
#set global variable for CPANPLUS::Module object of what the user selected
$self->{thisMod}=$self->_get_mod($self->{thisName});
#return if we can't get an object reference
return unless $self->{thisMod};
lib/CPANPLUS/Shell/Wx/ModuleTree.pm view on Meta::CPAN
my $self=shift;
my $progress=shift || Wx::ProgressDialog->new(_T("Please Wait..."),
_T("Displaying List..."),
MAX_PROGRESS_VALUE,
$self,
wxPD_APP_MODAL|wxPD_CAN_ABORT|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME);
my $totalFound=shift;
return unless $totalFound;
my $numFound=$totalFound;
my @modules=@_;
my @names=();
my $count=0;
my $percent=MAX_PROGRESS_VALUE/$totalFound;
return unless $progress->Update(0,_T("Getting info for $numFound items."));
#get information from modules
foreach $mod (@modules){
last unless $progress->Update($percent*$count);
if ($mod->isa('CPANPLUS::Module')){
push(@names,$mod->name);
}
if ($mod->isa('CPANPLUS::Module::Author')){
foreach $m ($mod->modules()){
push(@names,$m->name);
}
}
$count++;
}
#populate the tree ctrl
return unless $progress->Update(0,_T("Populating tree with").$totalFound._T(" items.") );
$count=0;
foreach $item (sort {lc($a) cmp lc($b)} @names){
return unless $progress->Update($percent*$count);
$self->AppendItem($self->GetRootItem(),$item,$self->_get_status_icon($item));
$count++;
}
return 1;
}
#this method returns the index in the imageList for the status of the passed name
sub _get_status_icon{
my $self=shift;
my ($name)=@_;
my $mod=$self->_get_mod($name);
return $self->{iconList}->unknown->idx unless $mod;
return $self->{iconList}->installed->idx if $mod->is_uptodate();
return $self->{iconList}->not_installed->idx if !$mod->installed_version();
return $self->{iconList}->update->idx;
_uShowErr;
}
sub SetImageList{ #must be a Wx::ImageList
my ($self,$list)=@_;
$self->{iconList}=$list;
$self->AssignImageList($list->imageList);
}
########################################
########### Context Menu ##############
########################################
package CPANPLUS::Shell::Wx::ModuleTree::Menu;
use base 'Wx::Menu';
use Wx::Event qw/EVT_WINDOW_CREATE EVT_MENU/;
use Data::Dumper;
use Wx::Locale gettext => '_T';
sub new {
my $class = shift;
my $parent=shift;
my $item=shift;
my $self = $class->SUPER::new(); # create an 'empty' menu object
#get image so we can determine what the status is
$img=$parent->GetItemImage($item);
$actions=new Wx::Menu();
$install=$actions->Append(1000,_T("Install")) if $img == 3;
$update=$actions->Append(1001,_T("Update")) if $img == 1;
$uninstall=$actions->Append(1002,_T("Uninstall")) if ($img==0 or $img==1);
$actions->AppendSeparator();
$fetch=$actions->Append(1003,_T("Fetch"));
$extract=$actions->Append(1004,_T("Extract"));
$prepare=$actions->Append(1005,_T("Prepare"));
$build=$actions->Append(1006,_T("Build"));
$test=$actions->Append(1007,_T("Test"));
$self->AppendSubMenu($actions,_T("Actions"));
$info=$self->Append(1008,_T("Get All Information"));
my $modName=$parent->GetItemText($item);
EVT_MENU( $self, $info, sub{&{$parent->{_minfoHandler}}(@_,$modName)} ) if $parent->{_minfoHandler};
EVT_MENU( $actions, $install, sub{&{$parent->{_minstallHandler}}(@_,$modName)} ) if ($img == 3 && $parent->{_minstallHandler});
EVT_MENU( $actions, $update, sub{&{$parent->{_mupdateHandler}}(@_,$modName)} ) if ($img == 1 && $parent->{_mupdateHandler});
EVT_MENU( $actions, $uninstall, sub{&{$parent->{_muninstallHandler}}(@_,$modName)} )if (($img==0 or $img==1) && $parent->{_muninstallHandler});
EVT_MENU( $actions, $fetch, sub{&{$parent->{_mfetchHandler}}(@_,$modName)} ) if $parent->{_mfetchHandler};
EVT_MENU( $actions, $prepare, sub{&{$parent->{_mprepareHandler}}(@_,$modName)} ) if $parent->{_mprepareHandler};
EVT_MENU( $actions, $build, sub{&{$parent->{_mbuildHandler}}(@_,$modName)} ) if $parent->{_mbuildHandler};
EVT_MENU( $actions, $test,sub{&{$parent->{_mtestHandler}}(@_,$modName)} ) if $parent->{_mtestHandler};
EVT_MENU( $actions, $extract, sub{&{$parent->{_mextractHandler}}(@_,$modName)} ) if $parent->{_mextractHandler};
# print "Ending ";
return $self;
}
1;
( run in 0.576 second using v1.01-cache-2.11-cpan-39bf76dae61 )