Re: Trying to create a menu from file input




G'Day !

I did this to create dynamic toolbars, but it should work for menus ....

1)  Create a generic menu creation function.  As inputs use anything you need in
the menu structure.
2)  Create a struct {} to hold all your inputs.
3)  create an array of structs and intialize them to your needed menus.
4)  Call your menu creation function

attached is an example code snippet  (for toolbar(s)):

struct toolbutton
{
  char name[10];
  char label[10];
  char tooltip[25];
  char private_tooltip[20];
  char widget[20];
  char function[20];
  char data[10];
};

struct toolbutton button[5] = {
  {"tool_open","Open","Open an existing drawing",
"","./xpm/xpm_open","MoleculeRepaint",""},
  {"tool_new","New","Start a new drawing",
"","./xpm/xpm_new","MoleculeRepaint",""},
  {"tool_save","Save","Save current drawing",
"","./xpm/xpm_save","MoleculeRepaint",""},
  {"tool_print","Print","Print current drawing",
"","./xpm/xpm_print","MoleculeRepaint",""},
  {"tool_exit","Exit","Quit program",
"","./xpm/xpm_exit","MoleculeRepaint",""}
}

static void add_tool_buttons (GtkWidget *hbox)
{
  GtkWidget *widget;  guint i, j;

  for (i = 0;  i < NUM_COLUMNS;  i++)  {
    toolbar = gtk_toolbar_new (GTK_ORIENTATION_VERTICAL,GTK_TOOLBAR_TEXT);
    gtk_box_pack_start (GTK_BOX (hbox), toolbar, TRUE, TRUE, 0);
    gtk_widget_show (toolbar);

      for (j = 0;  j < NUM_ROWS;  j++)    {
        gtk_toolbar_append_item (GTK_TOOLBAR (toolbar),
                                 button[j].label,
                                 button[j].tooltip,
                                 NULL,
                                 NULL,
                                 (GtkSignalFunc) button[j].function,
                                 NULL);
      }
  }
}


cheers,
Jim Parker

Sailboat racing is not a matter of life and death ....  It is far more important
than that !!!


                                                                                                              
                    
                    "Bob O'Connor"                                                                            
                    
                    <roconnor vectorpartners        To:     gtk-app-devel-list gnome org                      
                    
                    .com>                           cc:                                                       
                    
                    Sent by:                        Subject:     Trying to create a menu from file input      
                    
                    gtk-app-devel-list-admin                                                                  
                    
                    @gnome.org                                                                                
                    
                                                                                                              
                    
                                                                                                              
                    
                    09/22/00 01:05 PM                                                                         
                    
                                                                                                              
                    
                                                                                                              
                    



Does anyone have any insight into how I might read a file (which might
change from day to day) into a Gtk program and create an OptionMenu from
the list, and upon selection of the option menu, update a text entry
field?

--- snippet from the function creating the optionmenu

  clientMenu1_menu = gtk_menu_new ();
  glade_menuitem = gtk_menu_item_new_with_label (_("Clients"));
  gtk_widget_show (glade_menuitem);
  gtk_menu_append (GTK_MENU (clientMenu1_menu), glade_menuitem);

  if((clientList=fopen("client_list","r"))==NULL) {
     fprintf(stderr,"Could not open client list file.  Should be in same
directory as ListEditor binary.\n");
     exit(-1);
  }

  while(!feof(clientList)) {
     bzero(clientName,30);
     fscanf(clientList,"%[^,]%s%*['\n']",clientName,junk);
     glade_menuitem = gtk_menu_item_new_with_label (_(clientName));
     gtk_signal_connect_object(GTK_OBJECT(glade_menuitem),
"activate",GTK_SIGNAL_FUNC(client_select1),GTK_OBJECT(glade_menuitem));
     gtk_menu_append (GTK_MENU (clientMenu1_menu), glade_menuitem);
     gtk_widget_show (glade_menuitem);
  }

  fclose(clientList);

  gtk_option_menu_set_menu (GTK_OPTION_MENU (clientMenu1),
clientMenu1_menu);
----

--- snippet from the callbacks funtions

extern GtkWidget
*clientEntry1,*clientEntry2,*actionEntry1,*actionEntry2;

void
client_select1           (GtkObject       *selectedItem,
                                        gpointer         user_data)
{
   GtkLabel *label;
   gchar textData[80];
   /*gtk_label_get(GTK_LABEL(GTK_BIN(selectedItem)->child),&textData);*/
   label=GTK_LABEL(GTK_BIN(user_data)->child);
   gtk_label_get(label,textData);
   gtk_entry_set_text(GTK_ENTRY(clientEntry1),textData);
}

-----

Errors received:
leah% ./listedit

Gtk-WARNING **: invalid cast from (NULL) pointer to `GtkLabel'

Gtk-CRITICAL **: file gtklabel.c: line 333 (gtk_label_get): assertion
`label != NULL' failed.

---

I believe that it caused by the menu creation that I am attempting not
creating a unique menuitem object and overwriting the last one.  Not
sure how the gtk_signal_connect_object() function creates the signal...
Any help is most welcome.
Thanks,
Bob O'Connor(See attached file: roconnor.vcf)

Attachment: roconnor.vcf
Description: Binary data



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