Optionmenu woes: catching callbacks and getting results





Hi,

attached is a Gtk-- program which successfully creates an option menu.
Unfortunateley, this is where my problems start. I have tried to catch the
individual (radio)menuItems' callbacks, with little success (they only
seem to be triggered in groups). With that not doing what I want it to do,
I tried to use get_active () to return the active menuItem, but
that doesn't seem to return one of the (radio)menuItems either. The parts
which I'm not sure about, are marked with a comment containing a bunch
of question marks ...

Could anybody enlighten me as to what I'm doing wrong?

Thanks a lot
--> Robert

----------------------- cut here ------------------
// test to create an option menu
#include <string.h>
#include <gtk--.h>
#include <gtk/gtk.h>

class testoptmenu : public Gtk_Dialog
     {
     public:
                         testoptmenu (char *title=NULL,
                              char *messagage=NULL);
                         ~testoptmenu ();
          virtual void        buildDialogWindow ();

     protected:
          void           cb1 ();
          void           cb2 ();
          void                fillVBox ();
          void           fillActionArea ();
          void                buttonCallbackOK ();
          void                buttonCallbackApply ();
          void                buttonCallbackCancel ();
          gint           delete_event_impl (GdkEventAny *e);


          Gtk_Button          d_buttonOK,
                         d_buttonApply,
                         d_buttonCancel;
          Gtk_Label      d_msgColormap;
          Gtk_OptionMenu      d_colormapOptMenu;
                Gtk_Menu      d_colormapMenu;
          Gtk_RadioMenuItem   *d_rmiGrayscale,
                         *d_rmiColorBands;
          GSList              *d_colormapMenuGroup;

          char           *d_title,
                         *d_message;

     private:
          Gtk_HBox       *d_defaultHBox;
     };



testoptmenu::testoptmenu(char *title, char *message)
           : Gtk_Dialog (),
          d_buttonOK ("OK"),
          d_buttonApply ("Apply"),
          d_buttonCancel ("Cancel"),
          d_msgColormap ("Colormap")
{
     char           *s;

     if (title)
          s = title;
     else
          s = "testoptmenu Window";

     d_title = strdup (s);
     d_message = message;

     this->set_position (GTK_WIN_POS_MOUSE);
}


testoptmenu::~testoptmenu()
{
     if (d_title)
          delete d_title;
}


void testoptmenu::buildDialogWindow ()
{
     this->set_usize (300, 120);
     this->set_title (d_title);

     this->get_vbox()->set_border_width (2);

     fillVBox ();
     fillActionArea ();

     this->show ();
}


void testoptmenu::fillVBox ()
{
     d_defaultHBox = new Gtk_HBox (TRUE, 5);
     d_msgColormap.set_justify (GTK_JUSTIFY_LEFT);
        d_defaultHBox->pack_start (d_msgColormap, TRUE, FALSE, 10);
        d_msgColormap.show ();

        d_rmiColorBands = new Gtk_RadioMenuItem  (d_colormapMenuGroup,
          "ColorBands");
        d_colormapMenuGroup = d_rmiColorBands->group ();
        d_rmiColorBands->show ();
        d_colormapMenu.append (*d_rmiColorBands);
     connect_to_method (d_rmiColorBands->select, this, &cb1); // ???????

        d_rmiGrayscale = new Gtk_RadioMenuItem  (d_colormapMenuGroup,
          "Grayscale");
        d_colormapMenuGroup = d_rmiGrayscale->group ();
        d_rmiGrayscale->show ();
        d_colormapMenu.append (*d_rmiGrayscale);
     connect_to_method (d_rmiColorBands->select, this, &cb2); // ???????

        d_colormapMenu.show ();
        d_colormapOptMenu.set_menu (d_colormapMenu);
        d_defaultHBox->pack_end (d_colormapOptMenu, TRUE, FALSE, 10);
        d_colormapOptMenu.show ();
     this->get_vbox()->pack_start (*d_defaultHBox, TRUE, TRUE, 0);
     d_defaultHBox->show ();
}


void testoptmenu::fillActionArea ()
{
     // create new hbox and add buttons to it
     connect_to_method (d_buttonOK.clicked, this, &buttonCallbackOK);
     this->get_action_area()->pack_start (d_buttonOK, TRUE, TRUE, 0);
     d_buttonOK.show ();

     connect_to_method (d_buttonApply.clicked, this, &buttonCallbackApply);
     this->get_action_area()->pack_start (d_buttonApply, TRUE, TRUE, 0);
     d_buttonApply.show ();

     connect_to_method (d_buttonCancel.clicked, this, &buttonCallbackCancel);
     this->get_action_area()->pack_start (d_buttonCancel, TRUE, TRUE, 0);
     d_buttonCancel.show ();
}


void testoptmenu::cb1()
{
     printf ("Callback 1\n");
     fflush (stdout);
}


void testoptmenu::cb2()
{
     printf ("Callback 2\n");
     fflush (stdout);
}

void testoptmenu::buttonCallbackOK ()
{
     printf ("testoptmenu: Default Callback OK\n");
        //printf ("%s\n", d_colormapMenu.get_active());
}


void testoptmenu::buttonCallbackApply ()
{
     printf ("testoptmenu: Default Callback Apply\n");

     Gtk_MenuItem        *menu;

     menu = d_colormapMenu.get_active ();     // ???????????????????

     if (menu == d_rmiColorBands)
          printf ("ColorBands apply\n");
     else
     if (menu == d_rmiGrayscale)
          printf ("Grayscale apply\n");
     else
          printf ("Uncaught menu item!!!\n");

     fflush (stdout);
}


void testoptmenu::buttonCallbackCancel ()
{
     this->hide ();
     exit (0);
}


gint testoptmenu::delete_event_impl(GdkEventAny *)
{
     this->hide ();
     return (0);
}


int main (int argc, char *argv[])
{
     Gtk_Main  gtkMain (&argc, &argv);
     testoptmenu    *testDialog;

     testDialog = new testoptmenu ("Default Title", "Default Message");
     testDialog->buildDialogWindow ();
     testDialog->show ();
     gtkMain.run ();
}




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