I`ve got a GtkLabel
whose text is to remain centered regardless of whether it is shorter or longer than the label.
For example, a Win32 static control that has the SS_CENTER
style flag set behaves like that:
┌===========================┐ │ Lorem ipsum dolor │ └===========================┘
— when the text is shorter than the control;
┌===========================┐ Lorem ipsum│dolor sit amet, consectetur│adipiscing └===========================┘
— when the text is longer than the control.
N.B.: The only part of the text seen by the user is inside the frame.
I expected GtkLabel
-s to do the same thing, but actually they render centered text differently:
┌===========================┐ │ Lorem ipsum dolor │ └===========================┘
— when the text is shorter than the control;
┌===========================┐ │Lorem ipsum dolor sit amet,│consectetur adipiscing └===========================┘
— when the text is longer than the control.
How do I make centered text in a GtkLabel
remain centered even when it is long?
Just in case: the actual text the user sees is updated at ~4 FPS and is unknown prior to runtime.
That may be doable, but showing a semi-random part of a sentence doesn't really make sense from a user experience point of view.
Maybe give a look to gtk_label_set_ellipsize
?
They use a PangoEllipsizeMode
to tell which part of the sentence to hide. You can then hide the beginning, middle, or end part of the label. It's an enum though, not a flag, so you won't be able to hide beginning + end to show only the middle part.
So the possible solutions I see is:
If this is not a strong requirement, just use a saner comportment and use existing elipsize modes.
At last the solution is ready.
#include <gtk/gtk.h>
struct SCTX {
GtkWidget *text;
GdkRectangle rect;
};
void Resize(GtkWidget *view, GdkRectangle *rect, gpointer user) {
struct SCTX *sctx = user;
GtkRequisition requ;
if ((sctx->rect.width != rect->width )
|| (sctx->rect.height != rect->height)) {
sctx->rect = *rect;
gtk_widget_size_request(sctx->text, &requ);
gtk_layout_move(view, sctx->text, (rect->width - requ.width ) / 2,
(rect->height - requ.height) / 2);
}
}
int main(int argc, char *argv[]) {
GtkWidget *hwnd, *view;
struct SCTX sctx = {0};
gtk_init(&argc, &argv);
hwnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(hwnd, "destroy", gtk_main_quit, 0);
view = gtk_layout_new(0, 0);
g_signal_connect(view, "size-allocate", G_CALLBACK(Resize), &sctx);
gtk_widget_set_size_request(view, 320, 200);
sctx.text = gtk_label_new("Lorem ipsum dolor sit amet, consectetur "
"adipiscing elit, sed do eiusmod tempor "
"incididunt ut labore et dolore magna aliqua");
gtk_container_add(view, sctx.text);
gtk_container_add(hwnd, view);
gtk_container_set_border_width(GTK_CONTAINER(hwnd), 32);
gtk_window_set_position(GTK_WINDOW(hwnd), GTK_WIN_POS_CENTER);
gtk_widget_show_all(hwnd);
gtk_main();
return 0;
}
The crucial part here is wrapping a GtkLayout
around our GtkLabel
so the former could move the latter around inside its bounds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With