|
From: Nick J. <sk...@er...> - 2002-03-18 13:35:36
|
I'm using wxPerl on Win32, and I'm struggling. I've looked at all the
introductory tutorials I can find--and while I can now easily create a
frame and add menus to it, I'm having trouble doing anything else.
Anyway, I am creating a music organiser application. The general design
is that there will be variable-width "split" at the left hand side of the
window which will display the user's music collection as a tree structure.
At the minute, my program creates a window, menus and is split into two
using Wx::SplitterWindow. Each of the split canvases is a
Wx::ScrolledWindow.
Generally, I don't really understand how wxPerl operates. What I
understand is that you must, at the very least, extend Wx::Frame and
Wx::App and provide an OnInit method for Wx::App. That's fine, I can
understand that, and I can understand wx's event model. I can understand
creating menus, and that seems relatively straightforward.
What I don't understand is how to create other controls inside the
different canvases. When I look at the documentation for the C++ version
of wx, I see that when I create a control, I need to specify a parent. As
far as I know, this can be any class derived from Wx::Window. However,
some of the example wxPerl programs (from which I've learnt most of what I
know about wx!) seem to suggest that objects derived from Wx::Window have
a method called "add" or similar that can be used to add controls to
windows or canvases. However, Wx::ScrolledWindow doesn't seem to support
this.
In the code below, when I've created a tree, I've specified $lcanvas, a
reference to the left hand canvas, as the parent. If I change this to
$self (the window or frame?), the window isn't split properly and only a
small rectangular region is displayed. So, my main questions are:
- How can I add a tree to the left-hand part/canvas of the window?
- How can I generally add controls to frames?
- How can I create multiple frames for my application? How can I load and unload them?
I'm also having problems with toolbars (not included in code below). I can
create a toolbar but no buttons appear in it (I have added one button and
I have called the realise method on the toolbar).
Here's the problematic code:
#!/usr/bin/perl
use strict;
use Wx;
package MyCanvas;
use strict;
use vars qw(@ISA);
@ISA = qw(Wx::ScrolledWindow);
sub new {
my( $class ) = shift;
my( $this ) = $class->SUPER::new( @_[0,1,2,3], 0, $_[4] );
return $this;
}
package MusicOrganiserSplitterWindow;
use strict;
use vars qw(@ISA);
@ISA = qw(Wx::SplitterWindow);
use Wx qw(:splitterwindow wxDefaultPosition wxDefaultSize);
use Wx::Event qw(EVT_SPLITTER_SASH_POS_CHANGED);
sub new {
my( $class ) = shift;
my( $this ) = $class->SUPER::new( @_, wxDefaultPosition, wxDefaultSize,
wxSP_3D|wxSP_LIVE_UPDATE );
$this->{FRAME} = $_[0];
return $this;
}
# Extend Wx's "Frame" class to create the main window.
package MusicOrganiserFrame;
use base qw(Wx::Frame);
use Wx qw(wxOK wxICON_INFORMATION wxDefaultPosition wxDefaultSize);
use Wx::Event qw(EVT_MENU);
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_); # Call superclass' constructor
# Assign ID values
my ($ID_MENU_FILE_REPORT, $ID_MENU_FILE_STATISTICS, $ID_MENU_EDIT_ADD,
$ID_MENU_EDIT_DELETE, $ID_MENU_TOOLS_SETUP, $ID_MENU_HELP_CONTENTS,
$ID_MENU_HELP_ABOUT, $ID_MENU_FILE_EXIT) = (1 .. 100);
# Create menu objects
my $file_menu = Wx::Menu->new();
my $tools_menu = Wx::Menu->new();
my $edit_menu = Wx::Menu->new();
my $help_menu = Wx::Menu->new();
# Create File menu
$file_menu->Append($ID_MENU_FILE_REPORT, "\&Report\tCtrl+R",
"Produce reports about your collection.");
$file_menu->Append($ID_MENU_FILE_STATISTICS, "\&Statistics\tCtrl+S",
"Display statistics for part or all of your collection.");
$file_menu->AppendSeparator();
$file_menu->Append($ID_MENU_FILE_EXIT, "E\&xit\tCtrl+Q");
# Create Edit menu
$edit_menu->Append($ID_MENU_EDIT_ADD, "\&Add volume",
"Add a volume to your collection.");
$edit_menu->Append($ID_MENU_EDIT_DELETE, "\&Delete volume",
"Delete a volume from your collection.");
# Create Tools menu
$tools_menu->Append($ID_MENU_TOOLS_SETUP, "\&Setup",
"Configure Music Organiser.");
# Create Help menu
$help_menu->Append($ID_MENU_HELP_CONTENTS, "\&Contents");
$help_menu->AppendSeparator();
$help_menu->Append($ID_MENU_HELP_ABOUT, "\&About");
# Create the menu bar that stores all the above menus
my $menubar = Wx::MenuBar->new();
$menubar->Append($file_menu, '&File');
$menubar->Append($edit_menu, '&Edit');
$menubar->Append($tools_menu, '&Tools');
$menubar->Append($help_menu, '&Help');
$self->SetMenuBar($menubar);
# Set up event handling for menu items.
EVT_MENU($self, $ID_MENU_FILE_REPORT, \&OnMenu);
# Set window icon
$self->SetIcon(Wx::GetWxPerlIcon());
# Create status bar and assign values to the various segments
$self->CreateStatusBar(3);
$self->SetStatusText("Welcome to Music Organiser!", 1);
$self->SetStatusText(scalar(gmtime), 2);
my ($splitter) = $self->{SPLITTER} =
MusicOrganiserSplitterWindow->new($self, -1);
my ($lcanvas) = $self->{LEFTCANVAS} = MyCanvas->new($splitter, -1,
[0,0], [400,400],
'Test1');
my ($rcanvas) = $self->{RIGHTCANVAS} = MyCanvas->new($splitter, -1,
[0,0], [400,400],
'Test2');
$self->{TREE} = MusicOrganiserTree->new($lcanvas, -1, wxDefaultPosition,
wxDefaultSize, wxTR_HAS_BUTTONS);
$self->{SPLITTER}->SetMinimumPaneSize(250);
$splitter->Initialize($lcanvas);
$self->{LEFTCANVAS}->Show( 1 );
$self->{RIGHTCANVAS}->Show( 1 );
$self->{SPLITTER}->SplitVertically( @{$self}{'LEFTCANVAS','RIGHTCANVAS'}
);
$self->{SPLITTER}->SetSashPosition(250,1);
return $self;
}
sub OnMenu {
Wx::MessageBox("Menu item selected", "Menu selected",
wxOK | wxICON_INFORMATION, $_[0]);
}
package MusicOrganiserTree;
use strict;
use base qw(Wx::TreeCtrl);
sub new {
my $class = shift;
my $this = $class->SUPER::new(@_);
my $root = $this->AddRoot('Root', -1, -1,
Wx::TreeItemData->new('Data'));
my $tree_item;
$tree_item = $this->AppendItem($root, "Please work", -1, -1,
Wx::TreeItemData->new("I really hope this works!"));
}
package MusicOrganiserApp;
use base qw(Wx::App);
sub OnInit {
my $self = shift;
my $frame = MusicOrganiserFrame->new(
undef, # Parent window
-1, # Window ID
'Music Organiser', # Window title
[1, 1], # X, Y position
[600, 300] # X, Y size
);
$self->SetTopWindow($frame); # Set the top-level window.
$frame->Show(1); # Show the frame.
}
package main;
my $app = MusicOrganiserApp->new();
$app->MainLoop;
I would really appreciate any help on this.
Thanks in advance,
Nick
|