Re: manipulating the labels of a notebook-tab?

Bruno Boettcher said:
  i am trying to colorize the labels of a notebook widget as soon as new
  data arrives for a given page, but have some trouble with that...

  the perl gtk tutorial doesn't seem  to speak about colorizing labels,

that's because it's a taboo subject.  in fact, i have to give the standard
disclaimer that You Shouldn't Colorize Anything Yourself Without Very Good
Reasons Because That's The Theme's Job.  with that out of the way...

  and the functions given from the gtk interface don't seem to work?

depends on whether you want to change the color of the text or of the widget's

a label is a "no-window" widget; that is, it doesn't have its own
GdkWindow/XWindow, and instead draws on the window of the containing widget
(however far up the widget tree that might be).  most of the functions that
set widget background colors and the like therefore do not work on labels.  if
you want them to, then you can add the label to an EventBox, and do the color
trick on that instead.

to change the color of the *text* within the label is a lot easier, especially
if you use Pango markup.

  $label->set_markup ('<span foreground="red">something important</span>');

there's a page in the pango API reference which details the markup language.

in keeping with the disclaimer mentioned above, colorizing could be a very bad
idea (what if the user chose a theme which colorizes the widgets to the same
color as the text you've chosen for highlight?), you may be better off
changing the text in addition to changing its attributes.  for example, you
could have

 sub set_label_state {
   my $label = shift;
   my $name = shift;
   my $active = shift || 0;

   if ($active) {
     # something special, make it bold to stand out; also
     # append an asterisk, in case the theme already makes
     # all the text bold.
     $label->set_markup ("<b>$name *</b>");
   } else {
     # normal state, just plain old text
     $label->set_markup ($name);

i would recommend the text trick rather than the background trick, as the text
trick is less likely to conflict with gtk themes and thus more likely to make
users happy.  :-)

muppet <scott at asofyet dot org>

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]