433 |
|
|
434 |
#endif |
#endif |
435 |
|
|
436 |
|
#include <gtkmm/label.h> |
437 |
|
|
438 |
|
/** @brief Auto wrapped (multi-line) text, auto shrank to min. dimensions. |
439 |
|
* |
440 |
|
* This widgets is intended for large text blocks which are expected to require |
441 |
|
* the text to be automatically wrapped (i.e. auto inserted line breaks) such |
442 |
|
* that it would not exceed the label's maximum allowed width according to its |
443 |
|
* parent container. |
444 |
|
* |
445 |
|
* This class overrides get_preferred_height_for_width_vfunc() to fix the |
446 |
|
* default behaviour of Gtk::Label, which by default calculates a height under |
447 |
|
* the assumption that EVERY word of the text had a line break, which would |
448 |
|
* cause the label to claim much more vertical space than actually required for |
449 |
|
* the text. |
450 |
|
*/ |
451 |
|
class MultiLineLabel : public Gtk::Label { |
452 |
|
public: |
453 |
|
MultiLineLabel() : Gtk::Label() { |
454 |
|
#if GTKMM_MAJOR_VERSION >= 3 |
455 |
|
set_line_wrap(); |
456 |
|
#endif |
457 |
|
} |
458 |
|
|
459 |
|
void set_markup(const Glib::ustring& s) { |
460 |
|
Gtk::Label::set_markup(s); |
461 |
|
m_markup = s; |
462 |
|
} |
463 |
|
|
464 |
|
void get_preferred_height_for_width_vfunc(int width, int& minimum_height, int& natural_height) const { |
465 |
|
Gtk::Label::get_preferred_height_for_width_vfunc(width, minimum_height, natural_height); |
466 |
|
//printf("super suggests minimum_height=%d natural_height=%d\n", minimum_height, natural_height); |
467 |
|
|
468 |
|
Pango::Layout* origLayout = const_cast<Pango::Layout*>(get_layout().operator->()); |
469 |
|
Glib::RefPtr<Pango::Layout> layout = origLayout->copy(); |
470 |
|
Glib::ustring s = (!m_markup.empty()) ? m_markup : get_text(); |
471 |
|
layout->set_markup(s); |
472 |
|
int w, h; |
473 |
|
layout->get_pixel_size(w, h); |
474 |
|
h += get_margin_top() + get_margin_bottom(); |
475 |
|
//printf("calculated w=%d h=%d\n", w, h); |
476 |
|
|
477 |
|
minimum_height = h; |
478 |
|
if (natural_height < h) |
479 |
|
natural_height = h; |
480 |
|
} |
481 |
|
|
482 |
|
private: |
483 |
|
Glib::ustring m_markup; |
484 |
|
}; |
485 |
|
|
486 |
#endif // GIGEDIT_COMPAT_H |
#endif // GIGEDIT_COMPAT_H |