in reply to Re: 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?

James: I tried to translate and run your example. As you can see from the program here, it lets you set/change the font of a ListCtrl item, but not the headers (tried a couple of approaches).
(Unless I'm doing something wrong). (Although, interestingly, (as you can see by pressing the buttons), it does let you change the header text. But no font or color).
It's a pity, since, if your approach worked, I wouldn't need to start wrestling with Wx::Grid.

# Change header following James: +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'; 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); my $itemCol = Wx::ListItem->new; $itemCol->SetText('Column 1'); my $f = Wx::Font->new(12, -1, wxNORMAL, wxBOLD, 0, 'Arial'); $itemCol->SetFont($f); $self->{list_control} = Wx::ListCtrl->new($panel, -1, wxDefaultPosition, [ +320,100], wxLC_REPORT); # create a list control $self->{list_control}->InsertColumn(0, $itemCol); $self->{list_control}->SetColumnWidth(0, 100); $itemCol->SetText('Column 2'); $self->{list_control}->InsertColumn(1, $itemCol); $self->{list_control}->SetColumnWidth(1, 100); $itemCol->SetText('Column 3'); $self->{list_control}->InsertColumn(2, $itemCol); $self->{list_control}->SetColumnWidth(2, 100); $self->{list_control}->InsertStringItem( 0, 'Data 1' ); $self->{list_control}->SetItem( 0, 1, 'Data 3'); $self->{list_control}->InsertStringItem( 1, 'Data 2' ); $self->{list_control}->SetItem( 1, 1, 'Data 4'); 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_item }); 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; say 'In on_header'; my $column = $this->GetColumn(1); say '$column->GetText =', $column->GetText; $column->SetMask(wxLIST_MASK_TEXT); $column->SetText('NEW TITLE'); my $f = Wx::Font->new(12, -1, wxNORMAL, wxBOLD, 0, 'times new +roman'); $column->SetFont($f); $this->SetColumn(1, $column); } #1 end sub on_header sub on_item { my $this = shift; say 'In on_item'; my $item = $this->GetItem(1); my $f = Wx::Font->new(12, -1, wxNORMAL, wxBOLD, 0, 'times new +roman'); say "In on_item, \$this= $this, \$item = $item, \$f= $f"; $item->SetFont($f); $this->SetItem($item); } #1 end sub on_item sub on_header2 { my $this = shift; say 'In on_header2'; my $itemCol = Wx::ListItem->new; $itemCol->SetText('Column 2'); my $f = Wx::Font->new(12, -1, wxNORMAL, wxBOLD, 0, 'Arial'); $itemCol->SetFont($f); $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;

In the meantime, Anonymous Monk has provided another example based on your snippet - it's impressive to see DWIM in action.

Many thanks - Helen

  • Comment on Re^2: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
  • Download Code

Replies are listed 'Best First'.
Re^3: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
by jmlynesjr (Hermit) on Mar 27, 2013 at 01:09 UTC

    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.

    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.


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

      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.


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