LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2005, 02:59 AM   #1
geminigal
Member
 
Registered: Apr 2005
Posts: 53

Rep: Reputation: 15
Gtk


Hi ppl. i would like to ask bout my coding on GTK+1.2 using C.

After asking the user to input an integer as an entry of a dialog box, is there a way to pass the integer value to another function(function2()) to create the exact number of buttons according to the user input? Which means the new function(function2()) is to create buttons. Now, the problem is how do i pass the integer to the function???

Next, i will then return the buttons from the function2() to the main() to be packed in a table or a scrolled window. Is there a way to do so???

tq... any help will be very much appreciated.
Tq
 
Old 04-30-2005, 08:23 AM   #2
mandeltuete
Member
 
Registered: Mar 2003
Location: Switzerland
Distribution: Fedora 3
Posts: 75

Rep: Reputation: 15
You probably have a callback function for that entry box (or a button) which is called if the user hits enter or clicks that button.

Code:
void entrycallback(GtkWidget *widget, gpointer data)
{
    gchar *text = gtk_entry_get_text(GTK_ENTRY(widget));
    long int number = strtol(text, NULL, 0);
    function2(number);
}

void function2(long int number)
{
    for(i=0;i<number;i++)
    {
        GtkWidget *button = gtk_button_new();
        /* add buttons to box, table, ... */
    }
}
I'd add the buttons to the table or box in function2 rather than in the main function, because you can't return several buttons.

HTH
phylor
 
Old 05-02-2005, 09:13 PM   #3
geminigal
Member
 
Registered: Apr 2005
Posts: 53

Original Poster
Rep: Reputation: 15
Hi! Firstly, tq mandeltuete for ur kind advise. However, i still can't execute the number of buttons as passed in the entry of a dialog box. Can u pls help to take a look on my coding?

i suppose the problem is how the function2 being called in main(). i do not know how to pass the value input to function2() in main() in order to get the returned widget box.

Another problem is though i had pased the long int number to the string, however whenever i execute this, it will appear "Number of PCs: 0"?? now y is that so since i had passed the argument to it?

can anyone pls help to c and solve my probs??
Code:
#include <gtk/gtk.h>
#include <glib.h>
#include<unistd.h>
#include<stdio.h>

/*-- Declare global variables for the file dialog box and the filename --*/
GtkWidget *file_selection_box;
gchar *filename;

/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
  g_print ("\nQuitting...\n");
  gtk_main_quit();
  return (FALSE);
}

void enter_callback( GtkWidget *widget,
                     GtkWidget *entry )
{
  gchar *entry_text;
  entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
  printf("Entry contents: %s\n", entry_text);
}

void entry_toggle_editable( GtkWidget *checkbutton,
                            GtkWidget *entry )
{
  gtk_entry_set_editable(GTK_ENTRY(entry),
			 GTK_TOGGLE_BUTTON(checkbutton)->active);
}

void entry_toggle_visibility( GtkWidget *checkbutton,
                              GtkWidget *entry )
{
  gtk_entry_set_visibility(GTK_ENTRY(entry),
			 GTK_TOGGLE_BUTTON(checkbutton)->active);
}


GtkWidget* create2(long int num)
{
  GtkWidget *button;
  GtkWidget *box1;
  long int i;
  gchar buffer[32];
  
   /* Create box for xpm and label */
  box1 = gtk_hbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (box1), 2);
  
  for (i=1;i<=num;i++)
  { 
    sprintf (buffer, "PC %d\n", i);
    button = gtk_button_new_with_label(buffer);
    gtk_box_pack_start (GTK_BOX (box1), button, FALSE, FALSE, 3);
    gtk_widget_show (button);
    
    gtk_widget_show(box1);
  }
 return box1;
}

void create()
{
  gchar *text;
  long int number;
  GtkWidget *dialog, *entry, *okay_button;
  GtkWidget *vbox;
 
  dialog = gtk_dialog_new();
  entry = gtk_entry_new();
 // gtk_entry_set_max_length(GTK_ENTRY(entry),5);
  text = gtk_entry_get_text(GTK_ENTRY(entry));
  
 //   gchar *text = gtk_entry_get_text(GTK_ENTRY(widget));
    number = strtol(text, NULL, 0);
    create2(number);  
  printf("Number of PCs: %d\n",number);
  
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

 okay_button = gtk_button_new_with_label("Ok");
 gtk_signal_connect_object(GTK_OBJECT(okay_button),"clicked",GTK_SIGNAL_FUNC(create2),NULL);
  gtk_container_add (GTK_CONTAINER (vbox), okay_button);

  gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),vbox);
  gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),entry);
  gtk_widget_show_all(dialog);
  g_print("Button was clicked");
}

void store_filename(GtkFileSelection *file_selection, gpointer data)
{
  /*-- Extract filename from the file selection dialog --*/
  filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(file_selection_box));

  /*-- For the example, print the filename to the command line --*/
  g_print (filename);
}

void create_file_selection(void) 
{
  /*-- Create the selector widget --*/
  file_selection_box = gtk_file_selection_new("Please select a file for editing.");

  /*-- Link the ok button to the store_filename function --*/   
  gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->ok_button), "clicked", GTK_SIGNAL_FUNC (store_filename), NULL);
                             
  /*-- Destroy the dialog box when the user clicks the ok_button --*/
  gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->ok_button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) file_selection_box);

  /*-- Destroy the dialog box when the user clicks the cancel_button --*/
  gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->cancel_button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) file_selection_box);
     
  /* Display the file dialog box */
  gtk_widget_show (file_selection_box);
}


/* Create a Button Box with the specified parameters */
GtkWidget *create_bbox( gint  horizontal,
                        char *title,
                        gint  spacing,
                        gint  child_w,
                        gint  child_h,
                        gint  layout )
{
  GtkWidget *frame;
  GtkWidget *bbox;
  GtkWidget *button;

  frame = gtk_frame_new (title);

  if (horizontal)
    bbox = gtk_hbutton_box_new ();
  else
    bbox = gtk_vbutton_box_new ();

  gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
  gtk_container_add (GTK_CONTAINER (frame), bbox);

  /* Set the appearance of the Button Box */
  gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), layout);
  gtk_button_box_set_spacing (GTK_BUTTON_BOX (bbox), spacing);
  gtk_button_box_set_child_size (GTK_BUTTON_BOX (bbox), child_w, child_h);

  button = gtk_button_new_with_label ("Close");
  gtk_container_add (GTK_CONTAINER (bbox), button);
  gtk_signal_connect(GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (destroyapp), NULL);

  return(frame);
}

void help()
{
  GtkWidget *main_vbox;
  GtkWidget *frame_horz;
  gchar *markup_text;

  GtkWidget *dialog, *label, *okay_button;
  GtkWidget *vbox;
  dialog = gtk_dialog_new();
 // label = gtk_label_new ("A <span color=\"blue\">blue</span> <big>and</big> <b>bold</b> label");
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);


  label = gtk_label_new("\t\t\t\tThis is a help box.\n The buttons are connected via VNCviewer to the IP address typed.\n \nFor more information, you may surf at www.realvnc.com. \n");
  gtk_label_set_use_markup (GTK_LABEL (label), TRUE);


  main_vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (main_vbox), vbox);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
  gtk_container_add (GTK_CONTAINER (main_vbox), vbox);

  gtk_box_pack_start (GTK_BOX (vbox),
           create_bbox (TRUE, "Click this to close", 180, 85, 20, GTK_BUTTONBOX_SPREAD),
                      TRUE, TRUE, 0);//40 85 20
   gtk_box_pack_start (GTK_BOX (main_vbox), frame_horz, TRUE, TRUE, 10);

  gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),main_vbox);
  gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),label);
  gtk_widget_show_all(dialog);
  g_print("Button was clicked");
}


int main (int argc, char *argv[])
{
  /*-- Declare the GTK Widgets used in the program --*/
  GtkWidget *window;
  GtkWidget *menuFile;
  GtkWidget *menuExit;
  GtkWidget *menuHelp;  
  GtkWidget *menubar;
  GtkWidget *menu;
  GtkWidget *menuitem;
  GtkWidget *vbox;
  GtkWidget *handlebox;  
  GtkWidget *quitbox;
   //modified
   GtkWidget *scrolled_window;
   GtkWidget *button, *button1;
   GtkWidget *box1;
   //char buffer[32];
   //int i, j; 
   long int n;
  /*--  Initialize GTK --*/
  gtk_init (&argc, &argv);

  /*-- Create the new window --*/
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  /*-- Create the vbox --*/
  vbox = gtk_vbox_new(FALSE, 0);

  /*-- Create a text area --*/
  //text = gtk_text_new(NULL, NULL);

  /*-- Create the handlebox --*/
  handlebox = gtk_handle_box_new();

  /*-- Create the menu bar --*/
  menubar = gtk_menu_bar_new();

  /*-- Connect the window to the destroyapp function  --*/
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

  /*---------------- Create File menu items ------------------*/

  menuFile = gtk_menu_item_new_with_label ("File");
  gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuFile);
  gtk_widget_show(menuFile);

  /*-- Create File submenu  --*/
  menu = gtk_menu_new();
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuFile), menu);

  /*-- Create New menu item under File submenu --*/
  menuitem = gtk_menu_item_new_with_label ("New");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (create), NULL);
  gtk_widget_show (menuitem);

  /*-- Create Open menu item under File submenu --*/
  menuitem = gtk_menu_item_new_with_label ("Open");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (create_file_selection), NULL);
  gtk_widget_show (menuitem);

  /*-- Create Exit menu item under File submenu --*/
  menuitem = gtk_menu_item_new_with_label ("Exit");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (destroyapp), NULL);
  gtk_widget_show (menuitem);
  /*---------------- End File menu declarations ----------------*/

   /*---------------- Create Edit menu items --------------------*/

  menuExit = gtk_menu_item_new_with_label ("Exit");
  gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuExit);
  gtk_signal_connect(GTK_OBJECT (menuExit), "activate", GTK_SIGNAL_FUNC (destroyapp), NULL);
  gtk_widget_show(menuExit);

  /*---------------- Create Edit menu items --------------------*/


  /*---------------- Start Help menu declarations ----------------*/
  menuHelp = gtk_menu_item_new_with_label ("Help");
  gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuHelp);
  gtk_widget_show(menuHelp);

  /*-- Create Help submenu --*/
  menu = gtk_menu_new();
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuHelp), menu);

  /*-- Create About menu item under Help submenu --*/
  menuitem = gtk_menu_item_new_with_label ("About");
  gtk_menu_append(GTK_MENU(menu), menuitem);
    gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (help), NULL);
  gtk_widget_show (menuitem);
  /*---------------- End Help menu declarations ----------------*/

  /*-- Add the menubar to the handlebox --*/
  gtk_container_add(GTK_CONTAINER(handlebox), menubar);

  /*-- Pack the handlebox into the top of the vbox.  
   *-- You must use gtk_box_pack_start here instead of gtk_container_add
   *-- or the menu will get larger as the window is enlarged
   */
  gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, TRUE, 0);    

  //modified
    /* create a new scrolled window. */
    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    
    gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
    
    /* the policy is one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
     * GTK_POLICY_AUTOMATIC will automatically decide whether you need
     * scrollbars, whereas GTK_POLICY_ALWAYS will always leave the scrollbars
     * there.  The first one is the horizontal scrollbar, the second, 
     * the vertical. */
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
    /* The dialog window is created with a vbox packed into it. */                                                              
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, 
                        TRUE, TRUE, 0);
    gtk_widget_show (scrolled_window);
    
    box1 = create2(n);


  gtk_container_add(GTK_CONTAINER(scrolled_window), box1);

  /*-- Add the scrolled window area to the window --*/
  gtk_container_add(GTK_CONTAINER(vbox), scrolled_window); 
  

  /*-- Add the vbox to the main window --*/
  gtk_container_add(GTK_CONTAINER(window), vbox);

   /* Create another new hbox.. remember we can use as many as we need! */
    quitbox = gtk_hbox_new (FALSE, 0);
    
    /* Our quit button. */
    button1 = gtk_button_new_with_label ("Quit");
    
    /* Setup the signal to terminate the program when the button is clicked */
    gtk_signal_connect_object (GTK_OBJECT (button1), "clicked",
                               GTK_SIGNAL_FUNC (destroyapp),
                               GTK_OBJECT (window));
    /* Pack the button into the quitbox.
     * The last 3 arguments to gtk_box_pack_start are:
     * expand, fill, padding. */
    gtk_box_pack_start (GTK_BOX (quitbox), button1, TRUE, FALSE, 0);
    /* pack the quitbox into the vbox (box1) */
    gtk_box_pack_start (GTK_BOX (vbox), quitbox, FALSE, FALSE, 0);
    
    /* Pack the vbox (box1) which now contains all our widgets, into the
     * main window. */
    gtk_container_add (GTK_CONTAINER (window), vbox);
    
    
    /* This grabs this button to be the default button. Simply hitting
     * the "Enter" key will cause this button to activate. */
    gtk_widget_grab_default (button1);
    //gtk_widget_show (button1);
 

  /*-- Set window border to zero so that text area takes up the whole window --*/
  gtk_container_border_width (GTK_CONTAINER (window), 0);

  /*-- Set the window to be 640 x 480 pixels --*/
  gtk_window_set_default_size (GTK_WINDOW(window),640, 400);

  /*-- Set the window title --*/
  gtk_window_set_title(GTK_WINDOW (window), "File Dialog");

  /*-- Display the widgets --*/
  gtk_widget_show(handlebox);
  gtk_widget_show (button1);
  gtk_widget_show(quitbox);
  gtk_widget_show(vbox);
  gtk_widget_show(menuitem);
  gtk_widget_show(menubar);      
  gtk_widget_show(window);

  /*-- Start the GTK event loop --*/
  gtk_main();

  /*-- Return 0 if exit is successful --*/
  return 0;
}

Last edited by geminigal; 05-03-2005 at 07:41 PM.
 
Old 05-02-2005, 09:29 PM   #4
geminigal
Member
 
Registered: Apr 2005
Posts: 53

Original Poster
Rep: Reputation: 15
-
 
Old 05-03-2005, 02:14 PM   #5
mandeltuete
Member
 
Registered: Mar 2003
Location: Switzerland
Distribution: Fedora 3
Posts: 75

Rep: Reputation: 15
I can't find your mistake right now, have to look into your code more closely. But you should definitely correct compile errors and warnings. For example use:
Code:
text = (gchar *)gtk_entry_get_text(GTK_ENTRY(entry));
instead of
Code:
text = gtk_entry_get_text(GTK_ENTRY(entry));
That will get rid of two warnings.

Start your program in a terminal and you will see some errors:
Code:
(main:4215): GLib-GObject-WARNING **: invalid cast from `GtkWindow' to `GtkDialog'

(main:4215): Gtk-CRITICAL **: file gtkbox.c: line 372 (gtk_box_pack_start): assertion `GTK_IS_BOX (box)' failed
(null)

(main:4215): Gtk-WARNING **: gtk_scrolled_window_add(): cannot add non scrollable widget use gtk_scrolled_window_add_with_viewport() instead

(main:4215): Gtk-WARNING **: Attempting to add a widget with type GtkVBox to a container of type GtkWindow, but the widget is already inside a container of type GtkWindow, the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.

(main:4215): Gtk-CRITICAL **: file gtkwidget.c: line 4000 (gtk_widget_grab_default): assertion `GTK_WIDGET_CAN_DEFAULT (widget)' failed
Correction for the errors is indicated quite good. You should also correct the warnings.
If you have any questions, just ask.
 
Old 05-03-2005, 02:32 PM   #6
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
geminigal, please post your code inside

code
code goes here
/code

tags (those "code" lines should have brackets around them -- dunno how to show that in plain text here though. Maybe \[code\] ... \[/code\]
 
Old 05-03-2005, 08:23 PM   #7
geminigal
Member
 
Registered: Apr 2005
Posts: 53

Original Poster
Rep: Reputation: 15
hi. i have a question here. How do i declare that the entry input by the user is an integer?

In gtk+1.2, is it possible to receive an integer value as an entry? This is because I m not converting a string into a long integer by using strtol().

tq
 
Old 05-04-2005, 08:57 PM   #8
geminigal
Member
 
Registered: Apr 2005
Posts: 53

Original Poster
Rep: Reputation: 15
hi ppl.

i m having this warning error so i could not execute my sourcecode. can anyone pls help?

Code:
Gtk-WARNING **: Unable to locate loadable module in module_path: "libbluecurve.so",
 
Old 05-05-2005, 03:55 PM   #9
clausi
Member
 
Registered: Mar 2004
Distribution: Debian
Posts: 48

Rep: Reputation: 15
Pardon my question but why do you still use GTK1?
 
Old 05-05-2005, 07:39 PM   #10
geminigal
Member
 
Registered: Apr 2005
Posts: 53

Original Poster
Rep: Reputation: 15
i m currently assigned to use GTK+1.2 for the project i m doin now. That's why i hav no other choice. By the way, is there any GTK that you would llke to recommend? ie: GTK#, PHP-GTK? Which is better?? I still cannot differentiate between the both of them in terms of functionality. Tq.

Last edited by geminigal; 05-05-2005 at 07:41 PM.
 
Old 05-06-2005, 05:03 AM   #11
clausi
Member
 
Registered: Mar 2004
Distribution: Debian
Posts: 48

Rep: Reputation: 15
Making a recommendation is hard. It basically boils down to what language you'd like to use:
  • GTK+2 for C
  • GTKmm for C++
  • GTK# for C#/Mono
  • Python-GTK2 for Python
  • GTK2-Perl for Perl
  • Ruby/GTK for Ruby

These are the most important and most widely deployed bindings. Well, GTK+ is not a binding, and Ruby is probably used in Asian countries mostly. All of them use GTK+2, and you get Windows ports for each of them, AFAIK. If you'd like to deploy to Mac users, too, maybe you should consider wxWidgets instead of GTKmm.

The PHP bindings are currently updated to GTK2. I personally wouldn't use PHP for graphical apps but your mileage might vary. The Java bindings are also up-to-date but I guess it's better to use SWT which also binds to GTK+ on Linux.

Other bindings exists to nearly all other programming languages including rather unknown languages such as Euphoria, for example.

Last edited by clausi; 05-06-2005 at 05:04 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Freerock uninstall stole GTK looking for up-to-date pango, atk, gtk, glib and xft tgz Oholiab Slackware 8 09-18-2005 11:57 AM
GTK source install doesn't give gtk-config Feebles Linux - Software 4 06-15-2005 12:51 PM
cant find gtk-config file or gtk-devel to install happychappy Linux - Software 2 02-16-2005 12:46 AM
GTK error when starting gtk-chtheme Dswissmiss Linux - Software 1 10-01-2004 11:53 PM
compiling gaim/ gtk error (two versions of gtk) webazoid Linux - Software 0 07-06-2004 06:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration