Skip to content

Commit

Permalink
Merge pull request #71 from anaselli/label_autowrap
Browse files Browse the repository at this point in the history
Label autowrap
  • Loading branch information
shundhammer authored Jun 22, 2020
2 parents a9ed427 + 7777ad9 commit 43af234
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 47 deletions.
73 changes: 39 additions & 34 deletions src/YGDialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -427,40 +427,45 @@ void YGDialog::busyCursor()

void YGDialog::doSetSize (int width, int height)
{
// libyui calls YDialog::setSize() to force a geometry recalculation as a
// result of changed layout properties
bool resize = false;
GtkWidget *window = m_window->getWidget();

gint w,h;
gtk_window_get_size(GTK_WINDOW (window), &w, &h);

if (w < width || h < height) {
resize = true;
width = MAX (width, w),
height = MAX (height, h);
}

if (gtk_widget_get_realized (window)) {
gtk_widget_queue_resize (window);
width = MIN (width, YUI::app()->displayWidth());
height = MIN (height, YUI::app()->displayHeight());
if (isMainDialog()) {
GtkAllocation allocation;
gtk_widget_get_allocation(window, &allocation);
if (allocation.width < width || allocation.height < height) {
resize = true;
width = MAX (width, allocation.width),
height = MAX (height, allocation.height);
}
}
else
resize = true;
}
if (resize)
gtk_window_resize (GTK_WINDOW (window), width, height);
else
gtk_window_set_default_size (GTK_WINDOW (window), width, height);
//yuiDebug() << "layout pass " << layoutPass() << " (" << width << "x" << height << ")" << endl;
// libyui calls YDialog::setSize() to force a geometry recalculation as a
// result of changed layout properties
bool resize = false;
GtkWidget *window = m_window->getWidget();

gint w,h;
gtk_window_get_size(GTK_WINDOW (window), &w, &h);

if (w < width || h < height) {
resize = true;
width = MAX (width, w),
height = MAX (height, h);
}

if (gtk_widget_get_realized (window)) {
gtk_widget_queue_resize (window);
width = MIN (width, YUI::app()->displayWidth());
height = MIN (height, YUI::app()->displayHeight());
if (isMainDialog()) {
GtkAllocation allocation;
gtk_widget_get_allocation(window, &allocation);
if (allocation.width < width || allocation.height < height) {
resize = true;
width = MAX (width, allocation.width),
height = MAX (height, allocation.height);
}
}
else
resize = true;
}
int lpass = layoutPass();
if ( lpass == 0 )
{
if (resize)
gtk_window_resize (GTK_WINDOW (window), width, height);
else
gtk_window_set_default_size (GTK_WINDOW (window), width, height);
}
}

void YGDialog::highlight (YWidget *ywidget)
Expand Down
117 changes: 104 additions & 13 deletions src/YGLabel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include "YGWidget.h"
#include "YLabel.h"

#define AUTO_WRAP_WIDTH 100
#define AUTO_WRAP_HEIGHT 20

class YGLabel : public YLabel, public YGWidget
{
public:
YGLabel (YWidget *parent, const std::string &text, bool heading, bool outputField)
: YLabel (NULL, text, heading, outputField),
YGWidget (this, parent, GTK_TYPE_LABEL, NULL)
YGWidget (this, parent, GTK_TYPE_LABEL, NULL), _layoutPass1Width(0), _layoutPass1Height(0)
{
# if GTK_CHECK_VERSION (3, 14, 0)
gtk_widget_set_halign (getWidget(), GTK_ALIGN_START);
Expand All @@ -38,21 +41,109 @@ class YGLabel : public YLabel, public YGWidget
{
YLabel::setText (label);
gtk_label_set_label (GTK_LABEL (getWidget()), label.c_str());
std::string::size_type i = label.find ('\n', 0);
if (isOutputField()) { // must not have a breakline
if (i != std::string::npos) {
std::string l (label, 0, i);
gtk_label_set_label (GTK_LABEL (getWidget()), l.c_str());
}
}
else {
bool selectable = i != std::string::npos && i != label.size()-1;
gtk_label_set_selectable (GTK_LABEL (getWidget()), selectable);
}
std::string::size_type i = label.find ('\n', 0);

if (!isOutputField()) {
bool selectable = i != std::string::npos && i != label.size()-1;
gtk_label_set_selectable (GTK_LABEL (getWidget()), selectable);
}

}

YGWIDGET_IMPL_COMMON (YLabel)
void setAutoWrap( bool autoWrap )
{
yuiDebug() << endl;
YLabel::setAutoWrap( autoWrap );
gtk_label_set_line_wrap (GTK_LABEL (getWidget()), gboolean(autoWrap));
gtk_label_set_single_line_mode (GTK_LABEL (getWidget()), gboolean(!autoWrap));
}

//YGWIDGET_IMPL_COMMON (YLabel)
virtual bool setKeyboardFocus() {
return doSetKeyboardFocus();
}

virtual void setEnabled (bool enabled)
{ YLabel::setEnabled (enabled);
doSetEnabled (enabled);
}

virtual int preferredWidth()
{

// return 100;
int width;

if ( autoWrap() )
{
int lpass = layoutPass();
//yuiDebug() << "autowrap " << autoWrap() << " layoutpass " << lpass << endl;
if ( lpass != 1 )
{
width = _layoutPass1Width + 2; // some pixel more for border
}
else
{
width = doPreferredSize (YD_HORIZ);
}
}
else // ! autoWrap()
width = doPreferredSize (YD_HORIZ);


return (width > AUTO_WRAP_WIDTH ? width : AUTO_WRAP_WIDTH);
}

virtual int preferredHeight()
{
int height;

if ( autoWrap() )
{
if ( layoutPass() != 1 )
{
height = _layoutPass1Height + 4; // some pixel more for border
}
else
{
height = doPreferredSize (YD_VERT);
}
}
else // ! autoWrap()
{
height = doPreferredSize (YD_VERT);
}

return (height > AUTO_WRAP_HEIGHT ? height : AUTO_WRAP_HEIGHT);

}

virtual void setSize (int width, int height)
{
if ( autoWrap() )
{
int lpass = layoutPass();
//yuiDebug() << "layoutpass " << lpass << endl;
if (lpass == 1 || _layoutPass1Width <= 0)
{
_layoutPass1Width = width;
gint minimum_height, natural_height;

gtk_widget_get_preferred_height_for_width
(getWidget(),
_layoutPass1Width,
&minimum_height,
&natural_height);
_layoutPass1Height = natural_height;
}
}
doSetSize (width, height);
}

YGWIDGET_IMPL_USE_BOLD (YLabel)
protected:
int _layoutPass1Width;
int _layoutPass1Height;
};

YLabel *YGWidgetFactory::createLabel (YWidget *parent,
Expand Down

0 comments on commit 43af234

Please sign in to comment.