Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Wx::FontDialog Problem

by T.G. Cornholio (Scribe)
on Mar 30, 2005 at 03:51 UTC ( #443335=perlquestion: print w/replies, xml ) Need Help??
T.G. Cornholio has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks, I am trying to use Wx::FontDialog on Windows XP to let the user select a font. However, I don't seem to be able to retrieve the font information from the dialog box. I have created a short demo (adapted from this one) of the problem below. The way I understand it (may or may not be correct) is as follows: The GetFontData method returns a reference to a Wx::FontData object. From this object, I should be able to get the Wx::Font that the user chose. However, I always get output as seen here:
OK button pressed, getting font info... Font Data: Wx::FontData=SCALAR(0x1c051ec) Font: 1
Shouldn't $font be a reference to Wx::Font instead of a "1"? My setup is as follows:

Windows XP
ActiveState Perl 5.6.1 build 635
Wx version 0.21 installed from here.

Any advice is most appreciated.

use Wx; package MyApp; use strict; use vars qw(@ISA); @ISA=qw(Wx::App); $| = 1; sub OnInit { my($this) = @_; my($frame) = MyFrame->new("Font Selector Demo", Wx::Point->new(50, + 50), Wx::Size->new(450, 350)); $this->SetTopWindow($frame); $frame->Show(1); 1; } package MyFrame; use strict; use vars qw(@ISA); @ISA=qw(Wx::Frame); use Wx::Event qw(EVT_MENU); use Wx qw(wxBITMAP_TYPE_ICO wxMENU_TEAROFF); sub new { my($class) = shift; my($this) = $class->SUPER::new(undef, -1, $_[0], $_[1], $_[2]); my($mfile) = Wx::Menu->new(undef, wxMENU_TEAROFF); my($ID_TEST, $ID_EXIT) = (1, 2); $mfile->Append($ID_TEST, "&Test Font Dialog\tCtrl-T", "Display a f +ont dialog"); $mfile->Append($ID_EXIT, "E&xit\tAlt-X", "Quit this program"); my($mbar) = Wx::MenuBar->new(); $mbar->Append($mfile, "&Test"); $this->SetMenuBar($mbar); EVT_MENU($this, $ID_TEST, \&OnTest); EVT_MENU($this, $ID_EXIT, \&OnQuit); $this; } sub OnQuit { my($this, $event) = @_; $this->Close(1); } use Wx qw(wxID_OK wxOK wxICON_INFORMATION wxVERSION_STRING); sub OnTest { my($this, $event) = @_; my $fontdata; my $font; my $facename; my $dialog = Wx::FontDialog->new($this); $dialog->Create($this); if ($dialog->ShowModal() == wxID_OK) { print "OK button pressed, getting font info...\n"; $fontdata = $dialog->GetFontData; print " Font Data: $fontdata\n"; $font = $fontdata->GetChosenFont(); print " Font: $font\n"; } } package main; my($app) = MyApp->new(); $app->MainLoop();

2005-03-30 Janitored by Arunbear - added readmore tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Wx::FontDialog Problem
by PodMaster (Abbot) on Mar 30, 2005 at 04:21 UTC
    print "Face: ", $font->GetFaceName;
    I don't know why $font stringifies to 1 (you should ask about that on wxperl-users, probably a bug), but you do have a bonafied object.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Wx::FontDialog Problem
by crenz (Priest) on Mar 30, 2005 at 08:50 UTC

    I tried it on my machine on Windows, and it does work as PodMaster suggested: It is an object, as you can see using

    print " ref Font: " . ref($font) . "\n";

    which prints ref Font: Wx::Font on my machine.

    By the way, don't forget to use SetInitialFont() and remember the font data to set the initial font displayed to the user, or else your application will forget it's settings. E.g., do this:

    sub OnTest { my($self, $event) = @_; my $dialog = Wx::FontDialog->new($self, $self->{fontdata}); $dialog->Create($self); if ($dialog->ShowModal() == wxID_OK) { $self->{fontdata} = $dialog->GetFontData(); my $font = $self->{fontdata}->GetChosenFont(); $self->{fontdata}->SetInitialFont($font); printf("You chose %s %ipt\n", $font->GetFaceName(), $font->GetPointSize()); } else { print "No choice.\n"; } }
Re: Wx::FontDialog Problem
by T.G. Cornholio (Scribe) on Mar 31, 2005 at 04:09 UTC
    Thank you both for your replies. I guess I was just thrown off that it didn't seem like it was a reference. But it's working perfectly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://443335]
Front-paged by ysth
[choroba]: Can't touch this... ♪ ♫
LanX ("To be or not to be" from Ernst Lubitsch)
[choroba]: holli The video is best when watched after Damian's talk (not sure if it's online already)
[ambrus]: I'm currently teaching to a co-worker about the stupid bugs and misfeatures and workarounds my current program has. The ones that I will have to eventually fix, but we have to use the programs now before I have time to fix it.
[holli]: "three little words", you mean
[ambrus]: Some of them are rather embarrassing historical misfeatures. One I partly worked around right now as I told him.
[holli]: `?
[choroba]: yes
[holli]: yes, that's a great one. it's one the yapc::na channel for quite a while now
[holli]: 'on

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-11-21 13:44 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (301 votes). Check out past polls.