Re: Can I wrap a label when it exceeds the width of parent?

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

 



> From: Maurizio Colucci <seguso.forever@xxxxxx>
> To: gtk-list@xxxxxxxxx
> Subject: Can I wrap a label when it exceeds the width of parent?
> Date: Wed, 17 Dec 2003 09:49:23 +0000
>
---SNIP---
>
> Unfortunately, the scrolledwindow (and therefore also the viewport and vbox1)
> is very narrow horizontally, and the filenames are very long.
>
> So I need to wrap the filenames: I need to add newlines to label_name so it
> does not cross the right boundary of the scrolledwindow.
>
> (TextViews are way too slow, tested. And the wrap option of labels wraps at a
> fixed size, hardcoded, so it is useless for me)
>
> Currently I do a brutal wrapping of the label at character 26
> (hardcoded). But
> if the font/resolution/window-size changes, this is obviously wrong.
>
> In order to wrap correctly, I believe I must use pango somehow, and take into
> account:
> 1) the width of image1
> 2) the width of button1 (which can also be hidden!)
> 3) the current font width, and the width of a string with a given font
> 4) the width of the scrolledwindow//viewport/vbox1
>
> But I don't know how.
Hi Maurizio,
we had a similar problem where we had to implement some custom line
wrapping. We used a drawing area to display some kind of graph with lots
of labels, icons and so on. So I'm not sure whether our method works
equally well with standard GtkLabels in a container, but you can give it a
try.
First of all you have to calulate the available width for your
text. To do so connect to the configure-event of the parent container
where you place your text and images etc.
When this callback is called you should store the new available size,
which is the container widget size minus the sizes of all other widgets
except your text label. Then you know how many pixels wide your text can
be.
The next step is to insert newlines in your text to wrap it at that width.
Take a part of your text and set it to a PangoLayout:
  pango_layout_set_text(pLayout, szText, len);
Then get the size of this text:
  pango_layout_get_pixel_size(pLayout, &w, &h);
When this still fits in your available width you can add another character
(len++) and try again, else you have to insert a newline.
You'll get the PangoLayout from your label with
  PangoLayout* gtk_label_get_layout (GtkLabel *label);

Every time your application changes its size your configure-event will
refresh your linewrapping. So don't forget to remove any newlines from
former calls before you insert new ones.
I also noticed that Gtk sometimes calls configure-event more than once
when the widgets negotiate their new size. Only the size values of the
last call are relevant to you. So if you should have problems with the
method as described above you could modify it, so that the real container
size is stored in "configure" and a flag "UpdateLinewrapping" is set. The
final size calculation and linewrappings are then carried out in an
expose-event when your flag is set.

Hope this helps...Peter
-- 
====================================================================
Peter Krüger

applied software solutions (appss) GmbH
Sandtorstr. 23
D-39106 Magdeburg
Germany

Phone:  +49-(0)391-54486-19388
Fax:    +49-(0)391-54486-19222
email:  krueger@xxxxxxxx
URL:    http://www.appss.de

Managing Director: Uwe Hess, Dietmar Schäfer
Register: HRB12386, AG Mageburg

"Virtual business becomes reality!"

This e-mail may contain confidential and/or privileged information. If
you are not the intended recipient (or have received this e-mail in
error) please notify the sender immediately and destroy this e-mail. Any
unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
====================================================================

_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list


[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux