[gtkmm] Can this be done with gtkmm?



I am writing a Label capable of wrapping itself in order 
not to be wider than X pixels. Can you please help me clean up the code
of extract_text_of_line() in order to use only gtkmm (currently I am using 
PangoLayoutLine *)?

I don't know if it's possible.

(Go see my free file manager! http://segusoland.sourceforge.net)

        /// 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;
                }
        };


Thank you,

Maurizio




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