Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?

by jmlynesjr (Pilgrim)
on Mar 27, 2013 at 01:09 UTC ( #1025611=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
in thread Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?

Found the following hint in the wxWidgets Wiki

Using Specific Colours for Each Item

Trying to set specific item colours for a wxListCtrl doesn't have any effect with wxMSW 2.4.0 standard release. You have to enable it. Indeed, it is not set as default in the downloadable releases so you will have to recompile the library with _WIN32_IE at least defined as 0x300. To do so, just insert the -D_WIN32_IE=0x300 compile flag when making. See the Guides & Tutorials for further details about building wx. Then, just use wxListItem's methods to set the text colour, background colour and font of each item. For example:

MyListCtrl::AppendColouredItem(const wxString& Label) { wxListItem NewItem; NewItem.SetMask(wxLIST_MASK_TEXT); NewItem.SetId(GetItemCount()); NewItem.SetText(sLabel); NewItem.SetFont(*wxITALIC_FONT); NewItem.SetTextColour(wxColour(*wxRED)); NewItem.SetBackgroundColour(wxColour(235, 235, 235)); InsertItem(NewItem); }

This piece of code appends a new line (item) to MyListCtrl with the label of sLabel's value, an italic font, a red text colour and a light grey background.

Important part of this mess:

NOTE: It seems that these methods still don't work with columns so the tip is to set the global text/background colours and font of MyListCtrl and then, when you append an item, set its colours/font to the proper values.

Try playing around with my attempt at the above suggestion.

#! /home/xxxx/CitrusPerl/perl/bin/perl # Change header 2.pl: following James: http://www.perlmonks.org/?node_ +id=1025552 use strict; use warnings; use Wx; use 5.014; use autodie; use Carp; use Carp qw {cluck}; # use Carp::Always; package MyFrame; use Wx ':everything'; use Wx ':listctrl'; use Wx::Event 'EVT_BUTTON'; use parent -norequire, 'Wx::Frame'; use Data::Dumper; sub new { #1 MyFrame:: my ($class, $parent, $title) = @_; my $self = $class->SUPER::new( $parent, -1, # parent window; ID -1 means any $title, # title [150, 150 ], # position [ 350, 400 ], # size ); my $panel = Wx::Panel->new($self); $self->{list_control} = Wx::ListCtrl->new($panel, -1, wxDefaultP +osition, [340,100], wxLC_REPORT); $self->{list_control}->SetBackgroundColour(wxWHITE); $self->{list_control}->SetTextColour(wxBLUE); $self->{list_control}->SetFont(Wx::Font->new(14, wxFONTFAMILY_RO +MAN, wxNORMAL, wxNORMAL)); my $itemCol = Wx::ListItem->new; $itemCol->SetText('Column 1'); $itemCol->SetWidth(100); $self->{list_control}->InsertColumn(0, $itemCol); $itemCol->SetText('Column 2'); $itemCol->SetWidth(135); $self->{list_control}->InsertColumn(1, $itemCol); $itemCol->SetText('Column 3'); $itemCol->SetWidth(100); $self->{list_control}->InsertColumn(2, $itemCol); $self->{list_control}->InsertStringItem( 0, 'Data 1' ); $self->{list_control}->SetItem( 0, 1, 'Data 3'); my $f = Wx::Font->new(12, wxFONTFAMILY_ROMAN, wxNORMAL +, wxBOLD); my $item = $self->{list_control}->GetItem(0); $item->SetTextColour(wxRED); $item->SetFont($f); $self->{list_control}->SetItem($item); $self->{list_control}->InsertStringItem( 1, 'Data 2' ); $self->{list_control}->SetItem( 1, 1, 'Data 4'); $item = $self->{list_control}->GetItem(1); $item->SetTextColour(wxRED); $item->SetFont($f); $self->{list_control}->SetItem($item); my $btn_header = Wx::Button->new($panel, -1, 'Change header', +wxDefaultPosition, wxDefaultSize); my $btn_item = Wx::Button->new($panel, -1, 'Change item font', + wxDefaultPosition, wxDefaultSize); my $btn_header2 = Wx::Button->new($panel, -1, 'Change header2 +font', wxDefaultPosition, wxDefaultSize); EVT_BUTTON ($self, $btn_header, sub { $self->{list_control}->M +yFrame::on_header }); EVT_BUTTON ($self, $btn_item, sub { $self->{list_control}->MyF +rame::on_item }); EVT_BUTTON ($self, $btn_header2, sub { $self->{list_control}-> +MyFrame::on_header2 }); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{list_control}, 0, wxALL, 10); $sizer->Add($btn_header, 0, wxALL, 10); $sizer->Add($btn_item, 0, wxALL, 10); $sizer->Add($btn_header2, 0, wxALL, 10); $panel->SetSizer($sizer); $panel->Layout(); return $self; } #1 end sub new MyFrame:: sub on_header { my $this = shift; my $column = $this->GetColumn(1); $column->SetText('NEW TITLE'); $this->SetColumn(1, $column); } #1 end sub on_header sub on_item { my $this = shift; my $item = $this->GetItem(1); $item->SetTextColour(wxGREEN); my $f = Wx::Font->new(12, wxFONTFAMILY_ROMAN, wxNORMAL, wxBOLD +); $item->SetFont($f); $this->SetItem($item); } #1 end sub on_item sub on_header2 { my $this = shift; my $itemCol = Wx::ListItem->new; $itemCol->SetText('Column 2'); $this->SetColumn(1, $itemCol); } #1 end sub on_header2 # end package MyFrame:: package main; my $frame = MyFrame->new(undef, 'Setting headers'); $frame->Show(1); my $app = Wx::SimpleApp->new; $app->MainLoop; 1;

When you move to a virtual control I think you will have to also start using Wx::ListItemAttr to hold the color/font info for the virtual row items.

James

There's never enough time to do it right, but always enough time to do it over...


Comment on Re^3: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
Select or Download Code
Re^4: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
by HelenCr (Monk) on Mar 27, 2013 at 07:27 UTC

    James: thank you for this serious work.
    I ran your program and changed it a bit here and there. It seems that with the vanilla installation (I have DWIM Perl 5.14.2 on Windows 7):

    $self->{list_control}->SetBackgroundColour(wxWHITE); $self->{list_control}->SetTextColour(wxBLUE); my $f_list = Wx::Font->new(12, -1, wxNORMAL, wxBOLD, 0, 'Arial'); $self->{list_control}->SetFont($f_list);

    works as intended on the list items, but on columns, it will let you set/change the font, but not foreground or background colors. (BTW, how do I check the wxWidgets version?)

    I don't feel like recompiling the library, - sounds very scary to me, and also, sometimes I run the programs on other pc's, so this will necessitate recompiling on all of them - that's not practical.

    It seems I have no choice but to venture into the Grid (shudder), (where I'm encountering another set of problems, which I'll report soon).

    Many thanks for your dedicated work - Helen

      When I run the code that I posted, on my Ubuntu 10.10, CitrusPerl 5.16, wxWidgets 2.8.12 setup, the $self->{list_control}->SetTextColour(wxBLUE); does change the column header text to blue. The column background text always stays grey. This seems to be almost everything that you needed to do. Are you not seeing the header text color change? I can search for wxWidgets in my Synaptic package manager to see the version number(of my original install, the CitrusPerl install is a newer version).

      You could try a CitrusPerl install. It was quick and easy for me. You could even try the CitrusPerl developmental 2.9.4 wxWidgets version and see if this issue might have been fixed.

      I will cross-post a question to the mail-list and see if anyone over there knows the status.

      James

      There's never enough time to do it right, but always enough time to do it over...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025611]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2014-09-15 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls