Iceman
Iceman

Reputation: 4362

Make a gtk button label bold

How can I make a Gtk button label bold?

I know how to set a label markup as:

GtkWidget *label = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(label), "<b>Text to be bold</b>");

but not sure how to pass the argument to:

gtk_button_set_label()

Upvotes: 1

Views: 4284

Answers (3)

enthusiasticgeek
enthusiasticgeek

Reputation: 2736

The following pygtk code to make Bold Label Button should be easy to translate in C

    # Create "Quit" button
    button = gtk.Button("")
    for child in button.get_children():
        child.set_label("<b>Quit</b>")
        child.set_use_markup(True)
    # When the button is clicked, we call the main_quit function
    # and the program exits
    button.connect("clicked", lambda wid: gtk.main_quit())
    # Insert the quit button
    table.attach(button, 1,2,1,2, gtk.SHRINK|gtk.EXPAND|gtk.FILL)
    button.show()

Upvotes: 2

drahnr
drahnr

Reputation: 6886

The issue is that gtk_button_new_with_label(...) does not accept NULL as valid param and thus things get wacky.


Below a minimal working example.

Compile using gcc $(pkg-config --libs --cflags gtk+-3.0) ./main.c && ./a.out

main.c:

#include <gtk/gtk.h>
#include "stdlib.h"


void mycb (GtkWidget *button, gpointer user_data)
{
    GList *list;
    GtkWidget *label;
    list = gtk_container_get_children (GTK_CONTAINER (button));
    // list should only contain one element, since the button can only hold one child
    if (list->data && GTK_IS_LABEL (list->data)) {
        // set the markup string
        // for the markup syntax see 
        // https://developer.gnome.org/pygtk/stable/pango-markup-language.html
        //gtk_label_set_markup (GTK_LABEL (list->data), "<span weight=\"bold\">hello markup A!</span>");
        gtk_label_set_markup (GTK_LABEL (list->data), "hello <b>bold</b> markup");
    } else {
        label = gtk_label_new ("");
        gtk_label_set_markup (GTK_LABEL (label), "hello <span style=\"italic\">italic </span> markup B!");
        gtk_container_add (GTK_CONTAINER (button), label);
    }
}

int main(int argc, char *argv[])
{

    gtk_init (&argc, &argv);

    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *label;
    GList *list;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label("no markup");
    //button = gtk_button_new ();
    #if 0
    g_signal_connect (button, "clicked", G_CALLBACK (mycb), NULL);
    #else
    mycb(button, NULL);
    #endif
    gtk_container_add (GTK_CONTAINER (window), button);
    gtk_widget_show_all (window);
    gtk_main();
    return EXIT_SUCCESS;
}

Upvotes: 0

xuhdev
xuhdev

Reputation: 9333

This code works for me

button = gtk_button_new_with_label("");
list = gtk_container_get_children(GTK_CONTAINER (button));
gtk_label_set_markup(GTK_LABEL(list->data), "<b>hello!</b>");

Upvotes: 2

Related Questions