problem with combo boxes



Combo boxes do not behave properly when items are added to it dynamically.

I have following code. where before any new addition to the combo box behaves properly that is changed event is generated only when an item is selected. however as soon as a new item is added to the list dynamically change event is generated merely by the movement of the mouse over the added items.

any ideas please!!!

attached is my code

it can be compiled by following commad   g++ `pkg-config --cflags --libs gtk+-2.0` combobox_test.cpp

Munira Ahmed

#include <gtk/gtk.h>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>

using namespace std;

gboolean         ComboboxTimeout(GtkComboBox  *widget);
int              DialogComboAcc(GtkComboBox  *widget,gpointer user_data);
void             CreateCombobox();

GtkTreeStore     * m_TreeAcc;
GtkTreeModel     * m_ModelAcc;
GtkCellRenderer  * m_ModelRendererAcc;
GtkWidget        * m_wComboboxAcc;
GtkWidget        * m_Vbox;
GtkWidget        * m_Hbox;
GtkTreeStore     *treestore;

const int WINDOW_WIDTH   = 200; 
const int WINDOW_LENGTH  = 400;

enum
{
  COL_FIRST_NAME = 0,
  COL_LAST_NAME,
  COL_YEAR_BORN,
  NUM_COLS
} ;

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

    GtkWidget *window;
    GtkWidget *view;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Combo Box Testing");
    gtk_widget_set_size_request (GTK_WIDGET (window), WINDOW_WIDTH, WINDOW_LENGTH);

    g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */

    m_Vbox = gtk_vbox_new (false, 0);
    gtk_container_add (GTK_CONTAINER (window), m_Vbox);
    gtk_widget_show (m_Vbox);
    m_Hbox = gtk_hbox_new (false, 0);
    gtk_widget_show (m_Hbox);
    gtk_box_pack_start (GTK_BOX (m_Vbox), m_Hbox, false, false, 0);
    CreateCombobox();
    gtk_box_pack_start (GTK_BOX (m_Hbox), m_wComboboxAcc, false, false, 0);

    gtk_widget_show_all(window);
    g_timeout_add(10,(GSourceFunc) ComboboxTimeout, NULL);

    gtk_main();

    return 0;
}

void CreateCombobox()
{
    GtkTreeIter    toplevel;
    GtkTreeIter    child;

    m_TreeAcc   = gtk_tree_store_new(1, G_TYPE_STRING);

    m_ModelAcc  = GTK_TREE_MODEL (m_TreeAcc);

    m_ModelRendererAcc = gtk_cell_renderer_combo_new();
    m_wComboboxAcc     = gtk_combo_box_new_with_model(m_ModelAcc);
                                                                                                             
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(m_wComboboxAcc), m_ModelRendererAcc, true);
    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(m_wComboboxAcc),
                                    m_ModelRendererAcc,"text", 0, (gchar *)0);
    g_signal_connect (G_OBJECT (GTK_COMBO_BOX (m_wComboboxAcc)),
                      "changed",G_CALLBACK (DialogComboAcc),  NULL);

                                                                                                             
// g_signal_handler_block(GTK_COMBO_BOX (m_wComboboxAcc),m_iSignalIdAcc);
   for (int i=0; i < 5 ; i++)
    {
                                                                                                             
      gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &toplevel, NULL);
      gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &toplevel, 0, "Tulip", -1);
                                                                                                             
      /* Create Child */
      gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &child, &toplevel);
      gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &child, 0, "Edit-Tulip",-1);
                                                                                                             
      gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &child, &toplevel);
      gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &child, 0, "Delete-Tulip",-1);
                                                                                                             
    }
 //   g_signal_handler_unblock(GTK_COMBO_BOX (m_wComboboxAcc),m_iSignalIdAcc);

    gtk_widget_show_all (m_wComboboxAcc);
}


int  DialogComboAcc(GtkComboBox  *widget,gpointer user_data)
{
                                                                                                             
     GtkTreeIter    toplevel;
     GtkTreeIter    child;
     cout << "Under DialogComboAcc " << endl;

#if 0
// g_signal_handler_block(GTK_COMBO_BOX (m_wComboboxAcc),m_iSignalIdAcc);
   for (int i=0; i < 1 ; i++)
    {
                                                                                                             
      gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &toplevel, NULL);
      gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &toplevel, 0, "Green", -1);
                                                                                                             
      /* Create - Child */
      gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &child, &toplevel);
      gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &child, 0, "Edit-Green",-1);
                                                                                                             
      gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &child, &toplevel);
      gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &child, 0, "Delete-Green",-1);
                                                                                                             
    }
 //   g_signal_handler_unblock(GTK_COMBO_BOX (m_wComboboxAcc),m_iSignalIdAcc);
#endif
    return false;
}

int  ComboboxTimeout(GtkComboBox  *widget)
{
                                                                                                             
    GtkTreeIter    toplevel;
    GtkTreeIter    child;
    cout << "Under ComboboxTimeout " << endl;
                                                                                                             
    //g_signal_handler_block(GTK_COMBO_BOX (m_wComboboxAcc),m_iSignalIdAcc);
    for (int i=0; i < 3 ; i++)
    {
        gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &toplevel, NULL);
        gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &toplevel, 0, "Auto", -1);
                                                                                                             
        /* Create - Child */
        gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &child, &toplevel);
        gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &child, 0, "Edit-Auto",-1);
                                                                                                             
        gtk_tree_store_append(GTK_TREE_STORE (m_TreeAcc), &child, &toplevel);
        gtk_tree_store_set(GTK_TREE_STORE (m_TreeAcc), &child, 0, "Delete-Auto",-1);
                                                                                                             
    }
    //g_signal_handler_unblock(GTK_COMBO_BOX (m_wComboboxAcc),m_iSignalIdAcc);
    return false;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]