Opened 4 years ago

Last modified 11 months ago

#12539 confirmed defect

wxStaticText alignment is ignored.

Reported by: blinde Owned by:
Priority: low Milestone:
Component: wxGTK Version: dev-latest
Keywords: wxStaticText alignment Cc: rollrom@…
Blocked By: Blocking:
Patch: no



Issue discussed in

Using 2.8.11 with gtk-2 or gtk-1, the wxAlign flag have not effect on the result.

I have made many tests (using or not wxST_NO_RESIZE, using or not wxDefaultSize & wxDefaultPos ) but the result is always the same : the text seems to be left aligned.

This worked fine under 2.4. So the solution proposed in wxforum to always use wxSizer is very boring in case of porting an application from 2.4


Attachments (4)

trace.JPG download (241.2 KB) - added by blinde 4 years ago.
minimal download (263.6 KB) - added by blinde 4 years ago.
stattext.cpp.patch download (676 bytes) - added by rollrom 4 years ago.
do not set wrap flag when justify is not GTK_JUSTIFY_LEFT
stattext.cpp.2.patch download (1.3 KB) - added by rollrom 3 years ago.
revised patch

Download all attachments as: .zip

Change History (21)

comment:1 Changed 4 years ago by vadz

  • Resolution set to outdated
  • Status changed from new to closed

The following works as expected for me with wxGTK 2.9.1:

  • samples/minimal/minimal.cpp

    a b bool MyApp::OnInit() 
    172172    CreateStatusBar(2);
    173173    SetStatusText("Welcome to wxWidgets!");
    174174#endif // wxUSE_STATUSBAR
     177    new wxStaticText(this, -1, "wxALIGN_CENTRE", wxPoint(10, 10), wxSize(200, -1), wxALIGN_CENTRE);
     178    new wxStaticText(this, -1, "wxALIGN_RIGHT", wxPoint(10, 30), wxSize(200, -1), wxALIGN_RIGHT);
     179    new wxStaticText(this, -1, "wxALIGN_LEFT", wxPoint(10, 50), wxSize(200, -1), wxALIGN_LEFT);

Changed 4 years ago by blinde

Changed 4 years ago by blinde

comment:2 Changed 4 years ago by blinde

  • Resolution outdated deleted
  • Status changed from closed to reopened


Just made the test with 2.9.1.

IMHO, I still see the bug.

All the text are left aligned.

Can it be related to the gtk version ?

Using 2.18.1...

I don't think so, because on the wx 2.8.11, I had the same problem with gtk1 & gtk2 build...

Running under open suse 11.2

Sorry if I am missing something... Open for testing...



comment:3 Changed 4 years ago by vadz

  • Status changed from reopened to confirmed
  • Summary changed from Parameter Style for wxStaticText has not effect. to wxStaticText alignment is ignored.
  • Version changed from 2.8.11 to 2.9-svn

Hmm, indeed, I can reproduce the problem with GTK+ 2.20 (while the version I tested with before, where it worked, was 2.12). I don't know what to do about it though, we just use gtk_misc_set_alignment() function and it seems to not do anything with 2.20.

I thought this could be something similar to r64876 but no amount of rearranging the code helped in this case.

I guess the next thing to do would be to try to write a small GTK+-only example testing this and check if the alignment works there. If it doesn't, a bug would need to be reported to GTK+ people. Otherwise it would at least give us a starting point for debugging wxGTK.

comment:4 Changed 4 years ago by blinde


Just tried this code in a basic gtk appli :

m_widget = gtk_label_new (NULL);
    GtkJustification justify = GTK_JUSTIFY_CENTER;
    static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
    gtk_label_set_justify(GTK_LABEL(m_widget), justify);
    gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
    gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
    gtk_label_set_text_with_mnemonic(GTK_LABEL(m_widget),"Hello World");

The alignment is OK.

I replaced the code in wxStaticText::Create with the previous code, and the alignment is NOK.

I am rather newbie in gtk & wx but I would say that current code in stattext.cpp looks  OK, and the reason why it doesn't work is somewhere else.

Maybe an issue in functions like CreateBase, or DoAddChild or PostCreation or a more global config or Gtk :[


comment:5 Changed 4 years ago by ddreggors

  • Version changed from 2.9-svn to 2.8.12

I am using the following versions of gtk and wx in Fedora 14:


I can also confirm that this bug is present, using Code::Blocks and wxSmith with the following code:

StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT2, _("Package Name:"), wxPoint(24,24), wxSize(144,16), wxALIGN_RIGHT, _T("ID_STATICTEXT2"));

And on the advice of others I added "wxST_NO_AUTORESIZE":

StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT2, _("Package Name:"), wxPoint(24,24), wxSize(144,16), wxST_NO_AUTORESIZE|wxALIGN_RIGHT, _T("ID_STATICTEXT2"));

The label (wxStaticText) object displays on the panel but remains left aligned.

comment:6 Changed 4 years ago by vadz

  • Version changed from 2.8.12 to 2.9-svn

Changed 4 years ago by rollrom

do not set wrap flag when justify is not GTK_JUSTIFY_LEFT

comment:7 Changed 4 years ago by rollrom

I debugged this problem (using g-inspector), it seems indeed to be coming from GTK+. When the line wrap flag is set, the label is aligned (justified) to the right using the width of the first line, but gtk_misc_set_alignment is ignored, so it looks like

|           some text           |
|aligned to the right           |

instead of

|                      some text|
|           aligned to the right|

A workaround is to disable wrapping when the alignment is not the default one (see attached stattext.cpp.patch) until this is fixed in GTK+.

comment:8 Changed 4 years ago by vadz

Unfortunately the patch doesn't work for me, the label is still not aligned correctly even if we don't call gtk_label_set_line_wrap() at all (or call it with FALSE). I also tried using gtk_label_set_single_line_mode() but this didn't help neither.

FWIW I was testing with GTK+ 2.24, it looks like behaviour keeps changing from version to version.

Thanks for g-inspector hint BTW, I didn't know about it but it looks like a useful tool. Unfortunately it doesn't show absolutely anything wrong with the labels, in particular their bounding boxes are correct, they simply don't align correctly.

comment:9 Changed 4 years ago by vadz

I can also confirm that the bug is really in wx, not GTK, as the following simple program does work correctly with both GTK+ 2.12 and 2.24:

#include <gtk/gtk.h>

static void quit(GtkWidget *widget, gpointer data)
    gtk_main_quit ();

static GtkWidget *CreateLabel(const char *text, GtkJustification justify)
    GtkWidget *label = gtk_label_new(NULL);

    static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
    gtk_label_set_justify(GTK_LABEL(label), justify);
    gtk_misc_set_alignment(GTK_MISC(label), labelAlignments[justify], 0.0);
    gtk_label_set_line_wrap( GTK_LABEL(label), TRUE );
    gtk_label_set_text_with_mnemonic(GTK_LABEL(label), text);

    return label;

int main(int argc, char *argv[])
    GtkWidget *window;
    GtkWidget *boxV;
    GtkWidget *label;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(quit), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);

    boxV = gtk_vbox_new(FALSE, 10);

    gtk_box_pack_start(GTK_BOX(boxV), CreateLabel("Left", GTK_JUSTIFY_LEFT), TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(boxV), CreateLabel("Centre", GTK_JUSTIFY_CENTER), TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(boxV), CreateLabel("Right", GTK_JUSTIFY_RIGHT), TRUE, TRUE, 0);

    gtk_container_add(GTK_CONTAINER(window), boxV);


    gtk_main ();

    return 0;

But I have no idea what does it do differently from us, the code looks exactly the same.

comment:10 Changed 4 years ago by kvsoft

  • Version 2.9-svn deleted

Ubuntu (gtk 2.24)
Centos (gtk 2.10)

It only fails to align to the right on Ubuntu for some reason.

i figure out that if you remove wxEXPAND it will align as it should.

comment:11 Changed 3 years ago by rollrom

  • Patch set

I also have had the problem again after updating (I'm running Ubuntu 10.04.3 LTS with wxWidgets/GTK Today I had some time to investigate.

This time, it is because of the following code in wxStaticText::DoGetBestSize (src/gtk/stattext.cpp):

    GTK_LABEL(m_widget)->wrap = FALSE;

    GtkRequisition req;
    req.width = -1;
    req.height = -1;
    (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
        (m_widget, &req );

    GTK_LABEL(m_widget)->wrap = TRUE; // restore old value

which should be changed to something like

    bool wrap = GTK_LABEL(m_widget)->wrap;
    GTK_LABEL(m_widget)->wrap = FALSE;

    GtkRequisition req;
    req.width = -1;
    req.height = -1;
    (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
        (m_widget, &req );

    GTK_LABEL(m_widget)->wrap = wrap; // restore old value

I'm attaching the revised patch.

Changed 3 years ago by rollrom

revised patch

comment:12 Changed 3 years ago by rollrom

  • Cc rollrom@… added
  • Patch unset

comment:13 Changed 3 years ago by vadz

I don't think we're going to change this in 2.8, the danger of breaking something is too high for relatively low gain. But if you could please make a patch fixing this in 2.9 it would be definitely very welcome.

comment:14 Changed 3 years ago by rollrom

Sure. I've compiled 2.9 with gtk 2.24. There was already (closed) ticket 10716, which however does not solve the problem: if ellipsize mode is NONE the text is still always placed on the left side of the parent.

There is a following difference in the behavior in 2.8 and 2.9:

  • 2.8: alignment (xalign property) has effect if wrap flag is false
  • 2.9: alignment has effect if ellipsize mode in not PANGO_ELLIPSIZE_NONE, wrap flag value is not important.

I don't know the reason for this difference. In g-inspector all properties of GtkLabel are the same, yet the result is different. Maybe it is related to the layout of the parent container. In minimal sample with added label, in 2.9 the frame has GtkVBox as child (it is the parent of menu bar and label), while 2.8 uses GtkPizza.

It seems in 2.9 the only way to use the alignment style is to set the ellipsize to non-default value.

comment:15 Changed 3 years ago by vadz

So would simply not calling gtk_label_set_ellipsize() at all if no explicit wxST_ELLIPSIZE_XXX is specified, i.e. not calling it with PANGO_ELLIPSIZE_NONE fix this?

comment:16 Changed 3 years ago by rollrom

Er, no. PANGO_ELLIPSIZE_NONE is the default, so gtk_label_set_ellipsize(..., PANGO_ELLIPSIZE_NONE) has actually no effect for a new control and can be skipped. For the fix to work, gtk_label_set_ellipsize() should be always called with something else than PANGO_ELLIPSIZE_NONE, if the alignment is non-default. A suggestion is to use PANGO_ELLIPSIZE_START when the alignment is on the right and PANGO_ELLIPSIZE_MIDDLE if centered, unless the user passes his own wxST_ELLIPSIZE_* flag. I'll prepare this as a patch soon.

comment:17 Changed 11 months ago by vadz

  • Version set to dev-latest

This is apparently still present in current version.

Note: See TracTickets for help on using tickets.