有没有办法可以在gtk中创建一个字符串周围的“边框”?

时间:2011-12-04 22:17:23

标签: button gtk border

我的第一次GUI体验可以追溯到1996年的Xaw`和Xlib。 2004年之后,我放弃了,回到了CLI。现在我需要创建一个小的GUI“widget [?]”,其底部以“0”开头,上面有两个按钮“inc”和“dec”。在此之上,顶部是字符串

文件名 _ ___

inc / dec按钮增加/减少文件名。

例如,如果用户输入说话,则在底部,“0”将变为

speak.0.text。增加/减少按钮会将底部字符串向上或向下更改,而不是低于0.

我现在陷入困境,试图使“0”或“.0.text脱颖而出。我已尝试将filename.N.text转换为gtk按钮.gcc要么是核心要么拒绝编译。” p>

任何人都可以建议在“0”附近设置边框,或者让它脱颖而出吗?

非常感谢。

1 个答案:

答案 0 :(得分:2)

我不确定边框类型的效果是否可用,但您可以使用Pango Markup使用gtk_label_set_markup突出显示文本,进行其他与字体相关的更改。此外,当计数器达到0时,您可以使用gtk_widget_set_sensitive使递减按钮不敏感,以便不能进一步降低。以下是供您参考的示例代码:

/*
 * Compilation : gcc `pkg-config --cflags --libs gtk+-2.0` -Wall -Wextra -Werror label-sample.c -o label-sample
 */

#include <gtk/gtk.h>
#include <string.h>

static int counter = 0;
static GtkWidget *label;
static char *suffix = ".txt";
static char *prefix;


static void
update_label(void)
{
    char buffer[1024];
    memset(&buffer, 0, sizeof buffer);

    /*If counter is < 1, use markup to highlight text*/
    if(counter < 1)
    {
        if(prefix)
            g_snprintf(buffer, 1023, "<span foreground=\"red\" background=\"yellow\" size=\"x-large\">%s%d%s</span>",prefix, counter, suffix);
        else
            g_snprintf(buffer, 1023, "<span foreground=\"red\" background=\"yellow\" size=\"x-large\">%d%s</span>", counter, suffix);
        gtk_label_set_markup(GTK_LABEL(label), buffer);
    }
    else
    {
        if(prefix)
            g_snprintf(buffer, 1023, "%s%d%s", prefix, counter, suffix);
        else
            g_snprintf(buffer, 1023, "%d%s", counter, suffix);
        gtk_label_set_label(GTK_LABEL(label), buffer);
    }
}

static void
inc_button_click_cb(GtkButton *button, gpointer data)
{
    (void)button;
    GtkWidget *dec_button = data;
    counter++;
    /* Change senstivity of the decrement button based on counter*/
    if(counter > 0 && !gtk_widget_is_sensitive(dec_button))
        gtk_widget_set_sensitive(dec_button, TRUE);
    /* Update label to show updated counter */
    update_label();
    return;
}

static void
dec_button_click_cb(GtkButton *button, gpointer data)
{
    (void)data;
    counter--;
    /* Change senstivity of the decrement button based on counter*/
    if(counter < 1 && gtk_widget_is_sensitive(GTK_WIDGET(button)))
        gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
    /* Update label to show updated counter */
    update_label();
    return;
}


static void
entry_changed_cb(GtkEditable *editable, gpointer data)
{
    (void)data;
    /* Caller has to free the text, so call g_free */
    g_free(prefix);
    /* Get the complete text */
    prefix=gtk_editable_get_chars(editable,0, -1);
    /* Update label to show updated prefix */
    update_label();
    return;
}

int main(void)
{
    GtkWidget *button_inc;
    GtkWidget *button_dec;
    GtkWidget *entry_label;
    GtkWidget *entry;
    GtkWidget *window;
    GtkWidget *vbox;

    gtk_init(NULL, NULL);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    vbox = gtk_vbox_new(FALSE, 5);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    label = gtk_label_new("");
    update_label();

    button_dec = gtk_button_new_with_label("Decrease counter");
    g_signal_connect(button_dec, "clicked", G_CALLBACK(dec_button_click_cb), NULL);
    gtk_widget_set_sensitive(button_dec, FALSE);

    button_inc = gtk_button_new_with_label("Increase counter");
    g_signal_connect(button_inc, "clicked", G_CALLBACK(inc_button_click_cb), button_dec);

    entry_label = gtk_label_new("Entry prefix to be displayed in the entry below:");

    entry = gtk_entry_new();
    g_signal_connect(entry,"changed", G_CALLBACK(entry_changed_cb), NULL);

    gtk_box_pack_start(GTK_BOX(vbox), label, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), button_inc, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), button_dec, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), entry_label, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), entry, 0, 0, 0);

    gtk_widget_show_all(window);

    gtk_main();

    g_free(prefix);

    return 0;
}

希望这有帮助!