- box 内に image と label を配置して画像付きボタンを作成する
- 画像には xpm ファイルを使用する
Example: button.c
#include <gtk/gtk.h>
static GtkWidget *xpm_label_box(gchar *xpm_filename, gchar *label_text)
{
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;
/* Create box for image and label */
box = gtk_hbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(box), 2);
/* Load image for the button */
image = gtk_image_new_from_file(xpm_filename);
/* Create a label for the button */
label = gtk_label_new(label_text);
/* Pack the image and label into the box */
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 3);
gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);
gtk_widget_show(image);
gtk_widget_show(label);
return box;
}
static void callback(GtkWidget *widget, gpointer data)
{
g_print("Hello again - %s was pressed\n", (char*)data);
}
int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *button, *toggle_button;
GtkWidget *box;
gtk_init(&argc, &argv);
/* Create a new window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Pixmap'd buttons");
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
/* ボーダーの幅を設定 */
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
/* Create a new button */
button = gtk_button_new();
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(callback), (gpointer)"cool button");
box = xpm_label_box("CarGrey.xpm", "cool button");
/* Create a new toggle_button */
toggle_button = gtk_toggle_button_new();
g_signal_connect(G_OBJECT(toggle_button), "clicked", G_CALLBACK(callback), (gpointer)"toggle button");
/* Pack and show all our widget */
gtk_widget_show(box);
gtk_container_add(GTK_CONTAINER(button), box);
gtk_widget_show(button);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show(window);
gtk_main();
return 0;
}
実行結果
0 件のコメント:
コメントを投稿