Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by jmlynesjr (Deacon)
on Mar 26, 2013 at 16:14 UTC ( [id://1025552]=note: print w/replies, xml ) Need Help??


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

The wxBook states, under the wxListItem class, "wxListItem is a class you can use to insert an item, set an item's properties, or retreive information from an item"..."Other functions set various additional visual properties: SetAlign, SetBackgroundColour, SetTextColour, and SetFont. These don't require a mask flag be specified. All wxListItem functions have equivalent accessors prefixed by Get for retrieving information about an item."..."An alternative to using wxListItem, you can use set and get properties for an item with wxListCtrl convenience functions such as SetItemText, SetItemImage...and so on"

That said in the example that followed, a wxListItem was created first that was then used to create the columns. So in print, at least, it seems that this is the way to format the column headings. Try adding colour/font calls to the example below after appropriate translation from C++ :).

// Insert three columns wxListItem itemCol; itemCol.SetText(wxT("Column 1")); itemCol.SetImage(-1); # itemCol.SetFont(....) etc listCtrlReport->InsertColumn(0, itemCol); listCtrlReport->SetColumnWidth(0, wxLIST_AUTOSIZE); itemCol.SetText(wxT("Column 2")); listCtrlReport->InsertColumn(1, itemCol); listCtrlReport->SetColumnWidth(1, wxLIST_AUTOSIZE); itemCol.SetText(wxT("Column 3")); listCtrlReport->InsertColumn(2, itemCol); listCtrlReport->SetColumnWidth(2, wxLIST_AUTOSIZE);

James

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

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

Replies are listed 'Best First'.
Re^2: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
by Anonymous Monk on Mar 26, 2013 at 16:34 UTC
    Pipe dream friend, pipe dream
    #!/usr/bin/perl -- use strict; use warnings; use Wx; my $f = Wx::Frame->new(undef, -1, ""); ## this, otherwise nothing on s +creen, nada my $l = Wx::ListCtrl->new( $f, -1, [-1,-1], [-1,-1], Wx::wxLC_REPORT() ); $l->InsertColumn( $_, "column $_" ) for 0 .. 3; for my $col( 0 .. 2 ){ for( 0 .. 10 ){ if( $col == 0 ){ $l->InsertStringItem( $_, qq{row $_ col $col} ); } $l->SetItemString( $_, $col, qq{row $_ col $col} ) } $l->SetColumnWidth($col, Wx::wxLIST_AUTOSIZE() ); ## autosize aft +er adding all items } for my $row ( 11 .. 20 ){ $l->AddStringItems ( map { "row $row col $_" } 0 .. 3 ) ; } $l->SetColumnWidth($_, Wx::wxLIST_AUTOSIZE() ) for 0 .. 3; for my $col ( 0 .. 3 ){ warn my $column0 = $l->GetColumn( $col ); $column0->SetText( $column0->GetText . " (?red?)" ); $column0->SetBackgroundColour( Wx::wxRED() ); $column0->SetTextColour( Wx::wxRED() ); warn my $column0font = $column0->GetFont; $column0font->SetPointSize( 12 + $column0font->GetPointSize ); $column0font->SetWeight( Wx::wxFONTWEIGHT_BOLD() ); $column0->SetFont( $column0font); warn $column0->GetFont; $l->Refresh; $l->SetColumn( $col, $column0); warn $l->GetColumn( $col ); } $f->Show(1); exit Wx::SimpleApp->new->MainLoop; sub Wx::ListCtrl::GetItemFont { $_[0]->GetItem($_[1])->GetFont } sub Wx::ListCtrl::SetItemFont { $_[0]->GetItem($_[1])->SetFont($_[2]) +} sub Wx::ListCtrl::AddStringItems { #~ warn "@_\n"; my $l = shift; my $ix = $l->GetItemCount; $l->InsertStringItem( $ix, 'placeholder' ); my $col = 0; while(@_){ #~ warn "$ix $col $_[0] ", $l->SetItemString( $ix, $col, shift @_); $col++; } }

      No color or font changes for me when I ran your example.

      James

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

Re^2: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
by HelenCr (Monk) on Mar 26, 2013 at 17:54 UTC

    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.

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

    Many thanks - Helen

      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.

      James

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found