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]

 



On Thursday 18 December 2003 12:07 pm, Maurizio Colucci wrote:

> To make it faster, I thought of another approach: since Pango Layouts
> can wrap a text by themselves, by splitting it into lines, then we could
> make Pango do the wrapping and the read back the lines. We could
>  use these lines to build the label text, putting a newline char
> between the lines. The code is in MyLabel3, but I can't extract
> the text from the LayoutLine! Do you happen to know if
> I can do that? ( A negative answer would be tragic :-))

Never mind! I did it, in a very dirty way:


	/// A Label with the capability of wrapping itself if its
	/// width exceeds P pixels.
	class MyLabel3 : public Label{
	public:
		MyLabel3(string aTextWithoutNewlines) : Label(aTextWithoutNewlines){
			mTextWithoutNewlines = aTextWithoutNewlines;
			SetWrapPixel(100);
		}
		// SetWrapPixel: this function adds newlines to the
		// label text, in order not to make it be wider than P
		// pixels. Also it does not break words. Uses pango
		// for the wrapping. This would be perfect but I can't
		// write the function extract_text_of_line()!
		void SetWrapPixel(int p){
			Glib::ustring lTextWithNewlines = "";
			Glib::RefPtr<Pango::Layout> lLayout = create_pango_layout(mTextWithoutNewlines);
			lLayout->set_width(p * Pango::SCALE);
			lLayout->set_wrap(Pango::WRAP_WORD_CHAR);
			// Now Pango has correctly done the wrapping,
			// since lLayout->get_line_count() returns
			// more than 1. But I can't get the text of
			// the lines!
			for (int i = 0; i<lLayout->get_line_count(); i++){
				Glib::RefPtr<Pango::LayoutLine> lCurrentLine = lLayout->get_line(i);
				Glib::ustring lLineText = extract_text_of_line(lCurrentLine, lLayout->get_text());
				lTextWithNewlines += lLineText;
				// REGION add the newline but not at the end of the string
				if (i!=lLayout->get_line_count()-1)
					lTextWithNewlines += '\n';
				// END
			}
			this->set_text(lTextWithNewlines);
		}
	private:
		Glib::ustring mTextWithoutNewlines;
		Glib::ustring extract_text_of_line(Glib::RefPtr<Pango::LayoutLine> aLayoutLine, 
						   Glib::ustring aText){
			PangoLayoutLine * lPtr = aLayoutLine->gobj();
			const char * lText = aText.c_str();
			string s = "";
			for (int i=0; i< lPtr->length; i++){
				char c = lText[(lPtr->start_index) + i];
				s+=c;
			}
			return s;
		}
	};


Thanks again. I'll ask on the gtkmm list if this is possible without the dirtyness. :-)

Maurizio

_______________________________________________

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