Instantly share code, notes, and snippets.
Misko-2083/button_label.c Secret
Created
March 13, 2021 21:20
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Misko-2083/6621bb5ddbcdf18f17be00979d81f704 to your computer and use it in GitHub Desktop.
Align label over GtkButton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <gtk/gtk.h> | |
typedef struct { | |
GtkWidget *button; | |
GtkWidget *button_label; | |
GtkStyleProvider *button_style_provider; | |
} app_widgets; | |
void destroy_handler (GtkApplication* app, gpointer data) | |
{ | |
g_application_quit(G_APPLICATION (data)); | |
} | |
static void | |
button_size_allocate (GtkWidget *label, | |
GdkRectangle *allocation, | |
gpointer user_data) | |
{ | |
app_widgets *widgets = (app_widgets *) user_data; | |
char *css_text; | |
css_text = g_strdup_printf ("button,\n" | |
"button\n" | |
"{\n" | |
" padding: 0em %dpx 0em %dpx;\n" | |
"}\n", | |
20+allocation->height, 10+allocation->height); | |
gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (widgets->button_style_provider), | |
css_text, -1, NULL); | |
g_free (css_text); | |
} | |
void | |
button_clicked_callback (GtkButton *button, | |
app_widgets *widgets) | |
{ | |
GtkAlign align = gtk_widget_get_halign(widgets->button_label); | |
switch (align) { | |
case GTK_ALIGN_CENTER: | |
gtk_widget_set_halign (widgets->button_label, | |
GTK_ALIGN_END); | |
gtk_label_set_text (GTK_LABEL(widgets->button_label), "Right"); | |
return; | |
case GTK_ALIGN_END: | |
gtk_widget_set_halign (widgets->button_label, | |
GTK_ALIGN_START); | |
gtk_label_set_text (GTK_LABEL(widgets->button_label), "Left"); | |
return; | |
default: | |
gtk_widget_set_halign (widgets->button_label, | |
GTK_ALIGN_CENTER); | |
gtk_label_set_text (GTK_LABEL(widgets->button_label), "Center"); | |
return; | |
} | |
} | |
static void | |
activate (GtkApplication *app, | |
gpointer user_data) | |
{ | |
GtkSizeGroup *size_group; | |
GtkWidget *window; | |
GtkWidget *grid; | |
GtkWidget *button_overlay; | |
GtkWidget *button_cancel; | |
app_widgets *widgets = g_slice_new(app_widgets); | |
window = gtk_application_window_new (app); | |
gtk_window_set_title (GTK_WINDOW (window), "Button"); | |
gtk_window_set_default_size (GTK_WINDOW (window), 400, 60); | |
grid = gtk_grid_new (); | |
gtk_grid_set_column_homogeneous (GTK_GRID (grid), | |
TRUE); | |
gtk_grid_set_row_homogeneous (GTK_GRID (grid), | |
FALSE); | |
gtk_grid_set_row_spacing (GTK_GRID (grid), 10); | |
widgets->button = gtk_button_new_with_label(""); | |
button_cancel = gtk_button_new_with_label("Cancel"); | |
button_overlay = gtk_overlay_new (); | |
gtk_widget_set_hexpand (button_overlay, TRUE); | |
gtk_container_add (GTK_CONTAINER (button_overlay), | |
widgets->button); | |
widgets->button_label = gtk_label_new ("Center"); | |
gtk_widget_set_valign (widgets->button_label, GTK_ALIGN_CENTER); | |
gtk_overlay_add_overlay (GTK_OVERLAY (button_overlay), | |
widgets->button_label); | |
gtk_overlay_set_overlay_pass_through (GTK_OVERLAY (button_overlay), | |
widgets->button_label, TRUE); | |
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); | |
gtk_size_group_add_widget (size_group, widgets->button); | |
gtk_size_group_add_widget (size_group, widgets->button_label); | |
widgets->button_style_provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ()); | |
gtk_style_context_add_provider (gtk_widget_get_style_context (widgets->button), | |
widgets->button_style_provider, | |
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); | |
gtk_widget_set_halign (widgets->button_label, | |
GTK_ALIGN_CENTER); | |
gtk_grid_attach (GTK_GRID (grid), button_overlay, 0, 0, 4, 1); | |
gtk_grid_attach (GTK_GRID (grid), button_cancel, 3, 1, 1, 1); | |
gtk_container_add (GTK_CONTAINER (window), grid); | |
gtk_container_set_border_width(GTK_CONTAINER(window),5); | |
g_signal_connect (widgets->button_label, | |
"size-allocate", | |
G_CALLBACK (button_size_allocate), widgets); | |
g_signal_connect (G_OBJECT(window), "destroy", | |
G_CALLBACK (destroy_handler), app); | |
g_signal_connect (widgets->button, "clicked", | |
G_CALLBACK (button_clicked_callback), widgets); | |
g_signal_connect (G_OBJECT(button_cancel), "clicked", | |
G_CALLBACK (destroy_handler), app); | |
gtk_widget_show_all (window); | |
} | |
int | |
main (int argc, | |
char **argv) | |
{ | |
GtkApplication *app; | |
int status; | |
app = gtk_application_new ("org.gtk.button_overlay", G_APPLICATION_FLAGS_NONE); | |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); | |
status = g_application_run (G_APPLICATION (app), argc, argv); | |
g_object_unref (app); | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment