I would like to have a statusbar, so I started by making a small program with just a statusbar, so I could see how it worked.
Right now I would just like to be able to get some text in it, but it shows a random character instead.
Can someone see what's wrong with my code?
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window, *statusbar, *vbox;
gchar *info;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window, 250, -1);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
statusbar = gtk_statusbar_new();
/* stack for info messages */
g_object_set_data(G_OBJECT(statusbar), "info", (gpointer) "1");
g_object_set_data(G_OBJECT(statusbar), "info", (gpointer) "2");
g_object_set_data(G_OBJECT(statusbar), "info", (gpointer) "3");
/* stack for warning messages */
g_object_set_data(G_OBJE开发者_运维技巧CT(statusbar), "warning", (gpointer) "A");
g_object_set_data(G_OBJECT(statusbar), "warning", (gpointer) "B");
g_object_set_data(G_OBJECT(statusbar), "warning", (gpointer) "C");
/* get id for the message at the top of the info stack? */
guint id = gtk_statusbar_get_context_id(statusbar, "info");
/* show the top message from the info stack ? */
gtk_statusbar_push(statusbar, id, info);
vbox = gtk_vbox_new(FALSE, 5);
gtk_box_pack_start_defaults(GTK_BOX (vbox), statusbar);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I get these warnings
s.c:26: warning: passing argument 1 of ‘gtk_statusbar_get_context_id’ from incompatible pointer type
/usr/include/gtk-2.0/gtk/gtkstatusbar.h:94: note: expected ‘struct GtkStatusbar *’ but argument is of type ‘struct GtkWidget *’
s.c:28: warning: passing argument 1 of ‘gtk_statusbar_pop’ from incompatible pointer type
/usr/include/gtk-2.0/gtk/gtkstatusbar.h:100: note: expected ‘struct GtkStatusbar *’ but argument is of type ‘struct GtkWidget *’
s.c:28: error: too many arguments to function ‘gtk_statusbar_pop’
You declared GtkWidget *statusbar
but the gtk_statusbar_...
methods expect GtkStatusbar *
: casting it with GTK_STATUSBAR(statusbar)
removes the warnings. This is not a big issue as the program would work anyway: the biggest problem is you're using info
without initializing it.
The following code is working as expected here:
/* gcc -o status status.c $(pkg-config --cflags --libs gtk+-2.0) */
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window, *statusbar, *vbox;
gchar *info;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window, 250, -1);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
statusbar = gtk_statusbar_new();
/* stack for info messages */
g_object_set_data(G_OBJECT(statusbar), "info", (gpointer)
"1");
g_object_set_data(G_OBJECT(statusbar), "info",
(gpointer) "2");
g_object_set_data(G_OBJECT(statusbar), "info",
(gpointer) "3");
/* stack for warning messages */
g_object_set_data(G_OBJECT(statusbar), "warning",
(gpointer) "A");
g_object_set_data(G_OBJECT(statusbar), "warning",
(gpointer) "B");
g_object_set_data(G_OBJECT(statusbar),
"warning", (gpointer) "C");
/* get id for the message at the top of the
* info stack? */
guint id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "info");
/* show the top message from the info stack
* ? */
info = "This was uninitialized";
gtk_statusbar_push(GTK_STATUSBAR(statusbar), id, info);
vbox = gtk_vbox_new(FALSE, 5);
gtk_box_pack_start_defaults(GTK_BOX
(vbox),
statusbar);
gtk_container_add(GTK_CONTAINER(window),
vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I don't know what are you trying to achieve with g_object_set_data
though, but maybe they are only old tests...
Addendum:
You should use gtk_statusbar_push()
to pile up messages on the status bar stack. With gtk_statusbar_pop()
you'll remove the last pushed message, discovering the previous one.
gtk_statusbar_push(GTK_STATUSBAR(statusbar), id, "First message");
// Now the statusbar shows "First message"
gtk_statusbar_push(GTK_STATUSBAR(statusbar), id, "Second message");
// Now the statusbar shows "Second message"
gtk_statusbar_pop(GTK_STATUSBAR(statusbar), id);
// Now the statusbar shows "First message"
精彩评论