[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Google
  Web www.spinics.net

combo box trouble



I have been staring at my code for too long - I must be missing something
very obvious!

Attached is what I hope is a minimal example with:
- a quit button
- a populated combo box
- a not populated combo box

The quit button works in that when you press it, the application stops.
This tests that signals are connected?

The not populated combo box is not sensitive, but the populated one is,
which suggests that the gtk_tree_model == list_store is connected to the
populated one.

Making the gtk_combo_box_set_active() call seems to cause a changed
signal to be sent to the populated combo box, and "One" is found and
printed in the location, which again suggests that the list_store
is connected and populated.

If however I click on the arrow of the populated combobox, the popup
doesn't show any text. No signals are emitted either.

I have a feeling that I am missing something simple...

(This is on ubuntu 10.04 with its gtk 2.20.1, and NetBSD-current with
pkgsrc gtk 2.24.8 - so must be my code!)

Any thoughts?

Cheers,

Patrick
#include <err.h>

#include <gtk/gtk.h>

void window_close_cb(GtkWidget *w, gpointer unused)
{
	printf("Hello!\n");
	gtk_main_quit();
}

void long_list_cb(GtkWidget *w, gpointer unused)
{
	GtkComboBox *cb = GTK_COMBO_BOX(w);
	GtkTreeIter *i;
	GValue a = {0};

	if (gtk_combo_box_get_active_iter(cb, i) == 1) {
		gtk_tree_model_get_value(gtk_combo_box_get_model(cb), i, 0, &a);
		printf("Choose: %s\n", g_value_get_string(&a));
	} else {
		printf("None active\n");
	}
}

void short_list_cb(GtkWidget *w, gpointer unused)
{
}

void active_cb(GtkWidget *w, gpointer unused)
{
	printf("active\n");
}

void popdown_cb(GtkWidget *w, gpointer unused)
{
	printf("popdown\n");
}

void popup_cb(GtkWidget *w, gpointer unused)
{
	printf("popup\n");
}

void focus_cb(GtkWidget *w, gpointer unused)
{
	printf("focus\n");
}

int main(int argc, char *argv[])
{
	const char *fields[] = {"One", "Two", "Three", "Four", "Five", "Six",
                            "Seven", "Eight", "Nine", "Ten"};
	int i;

	GError *gerr;
	GtkBuilder *ui;
	GtkWidget *mainwindow, *longlist, *shortlist;
	GtkListStore *longstore, *shortstore;
	GtkTreeIter longiter;

	gtk_init(&argc, &argv);

	longstore  = gtk_list_store_new(1, G_TYPE_STRING);
	shortstore = gtk_list_store_new(1, G_TYPE_STRING);

	for (i = 0; i < 10; ++i) {
		gtk_list_store_append(longstore, &longiter);
		gtk_list_store_set(longstore, &longiter, 0, fields[i], -1);
	}

	gerr = NULL;
	ui = gtk_builder_new();
	if (gtk_builder_add_from_file(ui, "combobox.ui", &gerr) == 0)
		errx(gerr->code, "%s", gerr->message);

	gtk_builder_connect_signals(ui, NULL);

	mainwindow = GTK_WIDGET(gtk_builder_get_object(ui, "mainwindow"));
	longlist   = GTK_WIDGET(gtk_builder_get_object(ui, "longlist"));
	shortlist  = GTK_WIDGET(gtk_builder_get_object(ui, "shortlist"));

	gtk_combo_box_set_model(GTK_COMBO_BOX(longlist), GTK_TREE_MODEL(longstore));

	gtk_combo_box_set_active(GTK_COMBO_BOX(longlist), 0);

	gtk_widget_show_all(mainwindow);


	gtk_main();

	return 0;
}
<?xml version="1.0"?>
<interface>
  <object class="GtkWindow" id="mainwindow">
    <property name="default_width">300</property>
    <property name="default_height">100</property>
    <signal name="delete_event" handler="window_close_cb"/>
    <child>
      <object class="GtkVBox" id="vbox">
        <property name="homogeneous">FALSE</property>
        <property name="spacing">3</property>
        <child>
          <object class="GtkButton" id="quit">
            <property name="label">Quit</property>
            <signal name="clicked" handler="window_close_cb"/>
          </object>
          <packing>
            <property name="expand">FALSE</property>
            <property name="fill">FALSE</property>
          </packing>
        </child>
        <child>
          <object class="GtkComboBox" id="longlist">
            <signal name="changed" handler="long_list_cb"/>
            <signal name="move-active" handler="active_cb"/>
            <signal name="popdown" handler="popdown_cb"/>
            <signal name="popup" handler="popup_cb"/>
            <signal name="focus" handler="focus_cb"/>
          </object>
          <packing>
            <property name="expand">FALSE</property>
            <property name="fill">FALSE</property>
          </packing>
        </child>
        <child>
          <object class="GtkComboBox" id="shortlist">
            <signal name="changed" handler="short_list_cb"/>
          </object>
          <packing>
            <property name="expand">FALSE</property>
            <property name="fill">FALSE</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
DBG+=		-g -O0 -Wall
CPPFLAGS+=	`pkg-config --cflags-only-I gtk+-2.0 gmodule-export-2.0`
CFLAGS+=	`pkg-config --cflags-only-other gtk+-2.0 gmodule-export-2.0`
LDFLAGS+=	`pkg-config --libs gtk+-2.0 gmodule-export-2.0`

combobox:	combobox.c
	gcc $(DBG) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o combobox combobox.c

clean:
	rm -f combobox combobox.o core combobox.core
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[GIMP Users]     [Yosemite News]    [Yosemite Photos]    [Gimp's Home]     [Steve's Art]

Add to Google Powered by Linux