LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-14-2006, 11:59 PM   #1
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Rep: Reputation: 46
glade and scale widgets


So I'm new to this and I'm just trying to make a simple front end, but I cant seem to figure out how to get the value of my scale widgets. the only how-tos that I can find are either very simple how to make an interface with glade but not how to use them outside the limited scope of the example programs or, the gtk+-2.0 tutorial geared more to coding the whole thing yourself. but I cant find anything about using the adjustments that glade sets up. basically all I need to do is get the value of a scale widget and print it to the console that the programm is running in.
 
Old 03-15-2006, 01:25 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you just need to back up to the abstract GtkRange widget, calling get_value via whatever language this is done in. http://www.gtk.org/api/2.6/gtk/GtkRa...ange-get-value you can see the heirarchy of widgets in the list at the top there. so any method in an abstract widget will be still accesible in any specific based upon it.
 
Old 03-15-2006, 02:08 AM   #3
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Original Poster
Rep: Reputation: 46
thank you for your reply acid,
I should've mentioned it's gtk 2.0 with C. the link you have apears to be for gtk 2.6 but if I cut and paste it exactly it compiles fine but I'm not sure how to print this or get it into a variable so that I can use it.
 
Old 03-15-2006, 02:41 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well that's just plain how to use c i'm afraid.
Code:
double my_value;
my_value = gtk_range_get_value($my_widget);
printf ("value = %d", my_value);
etc...
 
Old 03-15-2006, 09:49 AM   #5
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Here's a C/libglade version. I didn't test it... it's just off the top of my head. It assumes the glade file is named "gui.glade" and that your scale widget has a signal handler named "on_scale_value_changed" defined in the glade file (under the signals tab of the properties window).

Code:
/* this would be in main() */
GladeXML *gxml;
gxml = glade_xml_new ("gui.glade", NULL, NULL);

/* this OR glade_xml_signal_autoconnect */
glade_xml_signal_connect (gxml,
                "value-changed",
                G_CALLBACK(on_scale_value_changed));
                
                
/* this is the callback for the "value-changed" signal */                                    
void 
on_scale_value_changed (GtkWidget *widget, gpointer user_data)
{
        gdouble val;
        val = gtk_range_get_value (GTK_RANGE(widget));
        
        /* print to screen */
        g_print("Range value: %e" val);
}
 
Old 03-15-2006, 09:59 AM   #6
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Okay, there should be a comma in that g_print call up there. Also, just to be sure I compiled a quick test, here's the whole thing. Compiles on my system using:

Code:
gcc -o test -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0
main.c
Code:
#define GLADE_FILE "gui.glade"

#include <gtk/gtk.h>
#include <glade/glade.h>

void 
on_hscale1_value_changed (GtkWidget *widget, gpointer user_data)
{
        gdouble val;
        val = gtk_range_get_value (GTK_RANGE(widget));
        
        /* print to screen */
        g_print("Range value: %d\n", (guint)val);
}

void 
on_window1_destroy (GtkWidget *widget, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                        
}

int 
main (int argc, char *argv[])
{
        
        GtkWidget *main_window;  
        GladeXML *gxml;
        
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /*
        create an instance of the GladeXML object and build widgets within
        the window1 root node.
        */
        gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
        
        /* get the window widget from the glade XML file */
        main_window = glade_xml_get_widget (gxml, "window1");
        
        /* connect signals */
        glade_xml_signal_connect (gxml, "on_window1_destroy", 
                G_CALLBACK(on_window1_destroy));
        
        glade_xml_signal_connect (gxml, "on_hscale1_value_changed", 
                G_CALLBACK(on_hscale1_value_changed));

        /* show the main window */
        gtk_widget_show (main_window);

        /* begin main GTK loop */
        gtk_main ();
        
        return 0;     
}
gui.glade
Code:
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <signal name="destroy" handler="on_window1_destroy" last_modification_time="Wed, 15 Mar 2006 15:55:53 GMT"/>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
	<widget class="GtkHScale" id="hscale1">
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>
	  <property name="draw_value">True</property>
	  <property name="value_pos">GTK_POS_TOP</property>
	  <property name="digits">0</property>
	  <property name="update_policy">GTK_UPDATE_CONTINUOUS</property>
	  <property name="inverted">False</property>
	  <property name="adjustment">5 0 100 1 5 5</property>
	  <signal name="value_changed" handler="on_hscale1_value_changed" last_modification_time="Wed, 15 Mar 2006 15:52:49 GMT"/>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>

      <child>
	<widget class="GtkLabel" id="label1">
	  <property name="visible">True</property>
	  <property name="label" translatable="yes">www.micahcarrick.com</property>
	  <property name="use_underline">False</property>
	  <property name="use_markup">False</property>
	  <property name="justify">GTK_JUSTIFY_LEFT</property>
	  <property name="wrap">False</property>
	  <property name="selectable">False</property>
	  <property name="xalign">0.5</property>
	  <property name="yalign">0.5</property>
	  <property name="xpad">5</property>
	  <property name="ypad">10</property>
	  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
	  <property name="width_chars">-1</property>
	  <property name="single_line_mode">False</property>
	  <property name="angle">0</property>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>
 
Old 03-15-2006, 02:21 PM   #7
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Original Poster
Rep: Reputation: 46
Thank you all for your help. I will study your examples and I am sure I will learn some things (I have not used c since middleschool and then just simple dos stuff so I'm just about completely lost) but, for some reason I only received email notification for acid's first post; so I stayed up till 4:00am and figured it out before I saw these examples. this is what I ended up with:
Code:
void
on_Fader2_value_changed                (GtkRange        *range,
                                        gpointer         user_data)
{	
	
	gdouble value2_float=
	gtk_range_get_value(GTK_RANGE(range));
	int value2 = value2_float;
	g_print("\n%d", value2);
	
}
but thank you very much for your help any notes on what I might be doing wrong would be appreciated.

Last edited by johnson_steve; 03-15-2006 at 02:24 PM.
 
Old 03-15-2006, 02:46 PM   #8
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Glad you got it working. Just a quick note...

Code:
gdouble value2_float=
	gtk_range_get_value(GTK_RANGE(range));
	int value2 = value2_float;
	g_print("\n%d", value2);
could instead be...

Code:
int value2_float=(int)gtk_range_get_value(GTK_RANGE(range));
	int value2 = value2_float;
	g_print("\n%d", value2);
which is "casting" the gdouble value into an integer... which is essentially what you're doing by assigning value2_float to value2.
 
Old 03-15-2006, 02:56 PM   #9
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Original Poster
Rep: Reputation: 46
oh, now I get Email. so are you saing:
Code:
int value2=(int)gtk_range_get_value(GTK_RANGE(range));
would put the value as an int in value2 ?
 
Old 03-15-2006, 03:17 PM   #10
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Right. Lookup "casting in C" on google to get specifics.

Also, the "GTK_RANGE(range)" is a casting macro. In that case, it's casting a GtkRange into a GtkRange-- redundant. However, if range was declared as a GtkWidget, you would then need to cast it into a GtkRange pointer before passing it to any gtk_range_SOMETHING function.

Casting is pretty common with GTK+/C and will help you out quite a bit to understand it and it's pit falls.

Good luck.
 
Old 03-15-2006, 03:29 PM   #11
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Original Poster
Rep: Reputation: 46
thanks for your help
 
  


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
scale sundaresh Linux - General 1 11-14-2005 03:15 PM
widgets bullettobinary Linux - Software 1 10-22-2005 01:50 AM
widgets purevil Linux - Newbie 1 05-15-2005 02:31 PM
Glade 2.5.0 and Perl-Glade-Two - a little help please Lake-end Programming 1 03-07-2005 04:47 AM
Gnucash Widgets davecs Linux - Software 2 07-16-2003 12:09 PM

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

All times are GMT -5. The time now is 10:38 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
Open Source Consulting | Domain Registration