GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Aug 31, 2014 6:17 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Gtk Button
PostPosted: Fri Dec 09, 2011 6:32 pm 
Offline
Familiar Face

Joined: Fri Dec 09, 2011 6:25 pm
Posts: 12
First topic. I'm new to GTK.
I'm doing a C++ project with Gtk.
I have to put a button in a frame. I would like to know if it is possibile to to put the button not in the centre and with th size that I want inside the frame.
How can i do it?

Code:
vbox = gtk_vbox_new (FALSE, NULL);
   
gtk_container_add (GTK_CONTAINER (window), vbox);

menubar = gtk_menu_bar_new ();
gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, NULL);

hbox = gtk_hbox_new (FALSE, NULL);
gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, NULL);

frame = gtk_frame_new (NULL);
gtk_frame_set_label_align (GTK_FRAME (frame), 0.0, 0.0);
gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_ETCHED_OUT);
   
gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, NULL);
   

button = gtk_button_new_with_label ("Load map");
gtk_box_pack_start (GTK_BOX (frame), button, TRUE, TRUE, NULL);

drawing_area = gtk_drawing_area_new ();
gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 600, 400);
gtk_box_pack_start (GTK_BOX (hbox), drawing_area, TRUE, FALSE, NULL);

of course i've created and packed the Main window


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Fri Dec 09, 2011 7:16 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hi,

What you need to do (I think 8-) ) is add the lines
Code:
  gtk_widget_set_halign(button, GTK_ALIGN_CENTRE);
  gtk_widget_set_valign(button, GTK_ALIGN_CENTRE);

This will place the button in the centre at its natural size, there are other alignment options. You may also want to set the flags when packing the button to not expand and fill.

Also not that the final argument to gtk_box_pack_start() is an integer and not a pointer. So you should not be using NULL.
Code:
// Bad code
  gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, NULL);

//Good code
  gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0);

_________________
E.


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Fri Dec 09, 2011 8:08 pm 
Offline
Familiar Face

Joined: Fri Dec 09, 2011 6:25 pm
Posts: 12
thank you, but adding your 2 lines I have got problems..
Code:
error: ‘GTK_ALIGN_CENTRE’ was not declared in this scope
error: ‘gtk_widget_set_halign’ was not declared in this scope
error: ‘gtk_widget_set_valign’ was not declared in this scope

I use
Code:
#include <gtk/gtk.h>

and I also tried
Code:
#include <sdl/sdl.h>

but I still haven't found a solution.
I need to put the button at the center put on the top of the frame (for ex if I can divide the frame in 3 parts, i'll put the button in the 1st part).
Is it possible also to change the size of the frame? Or to put the button in the hbox instead than in the frame?

I compile with
Code:
g++ -Wall -O3 `pkg-config --libs --cflags gtk+-2.0` -o prog prog.cc


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Fri Dec 09, 2011 9:03 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
You did not say which version of GTK you were using, so I used the latest which is version 3. gtk_widget_set_halign() and gtk_widget_set_valign() are in GTK version 3 and not in 2 which I now see that is what you are using.

Note SDL has nothing to do with GTK.

You can place a Button into any container, so yes you can place it into a HBox.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Fri Dec 09, 2011 9:42 pm 
Offline
Familiar Face

Joined: Fri Dec 09, 2011 6:25 pm
Posts: 12
ok! does gtk 3 work with ubuntu 10.04?

Code:
gtk_widget_set_halign (button, GTK_ALIGN_CENTRE);
     gtk_widget_set_valign (button, GTK_ALIGN_CENTRE);

how are they called in gtk2?


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Fri Dec 16, 2011 1:48 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
You said you're doing a C++ project, GTK for C++ is called Gtkmm. http://www.gtkmm.org/en/
Does Gtk work with 10.04, http://blog.sudobits.com/2010/06/01/how-to-install-gtk-in-ubuntu-10-04/
If you want to stick with C code, use Gtk3 and also install libgtk-3-doc (Documentation for the GTK+ graphical user interface library) and study it. But you can always look at the documentation online. If you want to stick with Gtk2, I believe this is the newest documentation version. http://developer.gnome.org/gtk/2.24/
Since I've upgrade to gtk3 I've got the documentation for both gtk2 and gtk3. gtk_widget_set_valign is only in Gtk3. http://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-set-valign I would also suggest you install Glade along with libgtk-3-doc. I don't use Glade to design interfaces, It's just great way to look up properties. Once you add a widget, you can just right click and select "Read Documentation" which will take you to the associated documentation page.
Also have a look in the tutorials sections here in the forums. I've posted some Gtk3 examples with CSS for styling and I use gtk_widget_set_valign.


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Fri Dec 16, 2011 12:22 pm 
Offline
Familiar Face

Joined: Fri Dec 09, 2011 6:25 pm
Posts: 12
nice!
I've done a lot of improvements now. Thank you very much. When I try to install GTK+3 using the package from http://www.gtk.org/download/linux.php I have a lot of problems.
I'm on Ubuntu 10.04 trying to install GTK+ 3. First of all i have to install atk, gdk-pixbuf, pango and glib.
I try to install atk and i get
Code:
atktext.c:1433: error: expected declaration specifiers or ‘...’ before ‘atk_text_range’
atktext.c:1433: error: expected declaration specifiers or ‘...’ before ‘atk_text_range_copy’
atktext.c:1434: error: expected declaration specifiers or ‘...’ before ‘atk_text_range_free’
atktext.c:1434: warning: return type defaults to ‘int’
atktext.c: In function ‘G_DEFINE_BOXED_TYPE’:
atktext.c:1433: error: parameter name omitted
atktext.c:1434: error: expected ‘{’ at end of input
make[3]: *** [atktext.lo] Errore 1
make[3]: uscita dalla directory «/home/cristiano/Desktop/atk-2.0.1/atk»
make[2]: *** [all] Errore 2
make[2]: uscita dalla directory «/home/cristiano/Desktop/atk-2.0.1/atk»
make[1]: *** [all-recursive] Errore 1
make[1]: uscita dalla directory «/home/cristiano/Desktop/atk-2.0.1»
make: *** [all] Errore 2

Somebody knows why?


Top
 Profile  
 
 Post subject: Re: Gtk Button
PostPosted: Mon Dec 19, 2011 3:57 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
What version of GTK and ATK are you trying to install ?

GTK3 has minimum version requirements for each package and some packages in turn have their own requirements so read the INSTALL and README file for each one.
For instance: If your installing the Gtk+-3.2.3, the INSTALL says:
GTK+ 3.2.3 requires at least:
GLib 2.29.14
Pango 1.29.0
GdkPixbuf 2.23.5
ATK 2.1.5
cairo 1.10.0.
gobject-introspection 0.10.1

What order are you installing the dependencies ? http://developer.gnome.org/gtk3/stable/gtk-building.html

You could just install Ubuntu 11.10 (Oneiric Ocelot) It has Gtk+-3.2.0 and Glade 3.10.0 in packages so it's an automadated install. But I don't reccommend using Software Center to install anything. Use Synaptic package manager and install Glade. That should install everything you need. But Synaptic doesn't come with Ubuntu 11.10, you have to install it: sudo apt-get install synaptic
Then run gnome classic: sudo apt-get install gnome-session-fallback
http://linux-software-news-tutorials.bl ... y-and.html

Note: I believe there's a bug in gtk+-3.2.0 and CSS with Ubuntu 11.10, but maybe it's just my system.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group