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 Nov 23, 2014 8:05 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Question about alignment in gtk_table_new
PostPosted: Mon Mar 09, 2009 8:40 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
This is my code:
Code:
int main (int argc, char *argv[]) {
  GtkWidget *TopLevel;
  GtkWidget *table;
  GtkWidget *name, *email;
  gtk_init (&argc, &argv);
  /* TopLevel features */
  TopLevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (TopLevel), "User Info");
  gtk_window_set_position (GTK_WINDOW (TopLevel), GTK_WIN_POS_CENTER);
  g_signal_connect (G_OBJECT (TopLevel), "destroy", G_CALLBACK (on_toplevel_destroy), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (TopLevel), 5);
  /* table creation */
  table = gtk_table_new (2, 3, FALSE);
  gtk_table_attach (GTK_TABLE (table), gtk_aligned_label ("Please fill the following form"), 0, 2, 0, 1, GTK_EXPAND, GTK_SHRINK, 0, 0); // (0, 0) (2, 1)
  gtk_table_attach (GTK_TABLE (table), gtk_aligned_label ("Name"), 0, 1, 1, 2, GTK_EXPAND, GTK_SHRINK, 0, 0); // (0, 1) (1, 2)
  name = gtk_entry_new ();
  gtk_table_attach (GTK_TABLE (table), name, 1, 2, 1, 2, GTK_EXPAND, GTK_SHRINK | GTK_FILL, 0, 0); // (1,1) (2, 2)
  gtk_table_attach (GTK_TABLE (table), gtk_aligned_label ("Email"), 0, 1, 2, 3, GTK_EXPAND, GTK_SHRINK, 0, 0); // (0, 2) (1, 3)
  email = gtk_entry_new ();
  gtk_table_attach (GTK_TABLE (table), email, 1, 2, 2, 3, GTK_EXPAND, GTK_SHRINK | GTK_FILL, 0, 0); // (1,2) (2, 3)
  gtk_container_add (GTK_CONTAINER (TopLevel), table);
  gtk_widget_show_all (TopLevel);
  gtk_main ();
  return 0;
}

callbacks:
Code:
GtkWidget *gtk_aligned_label (const gchar *string) {
    GtkWidget *aligner;
    aligner = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
    gtk_container_add (GTK_CONTAINER (aligner), gtk_label_new (string));
    return aligner;
}

My problem is that the table alignment is not showing as expected, see screenies:
Normal
Maximized
a) Normal state, labels aren't aligned to the left.
b) Maximized state labels aren't aligned to the left and how to expand the entry to can be larger as the window.

Thanks.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 9:18 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I modified your code a bit to achieve something similar to your needs (I'm not sure that I understood your needs completely, so feel free to post again and tell me I suck;)

Code:
#include <gtk/gtk.h>
GtkWidget *gtk_aligned_label (const gchar *string) {
    GtkWidget *aligner;
    aligner = gtk_label_new (string);
   g_object_set( G_OBJECT( aligner ), "xalign", 0, NULL );
    return aligner;
}
int main (int argc, char *argv[]) {
  GtkWidget *TopLevel;
  GtkWidget *table;
  GtkWidget *name, *email;
  gtk_init (&argc, &argv);
  /* TopLevel features */
  TopLevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (TopLevel), "User Info");
  gtk_window_set_position (GTK_WINDOW (TopLevel), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width (GTK_CONTAINER (TopLevel), 5);
  /* table creation */
  table = gtk_table_new (2, 3, FALSE);
  gtk_table_attach (GTK_TABLE (table), gtk_aligned_label ("Please fill the following form"), 0, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0); // (0, 0) (2, 1)
  gtk_table_attach (GTK_TABLE (table), gtk_aligned_label ("Name"), 0, 1, 1, 2, GTK_FILL, GTK_SHRINK, 0, 0); // (0, 1) (1, 2)
  name = gtk_entry_new ();
  gtk_table_attach (GTK_TABLE (table), name, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); // (1,1) (2, 2)
  gtk_table_attach (GTK_TABLE (table), gtk_aligned_label ("Email"), 0, 1, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0); // (0, 2) (1, 3)
  email = gtk_entry_new ();
  gtk_table_attach (GTK_TABLE (table), email, 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); // (1,2) (2, 3)
  gtk_container_add (GTK_CONTAINER (TopLevel), table);
  gtk_widget_show_all (TopLevel);
  gtk_main ();
  return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 11:11 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
tadeboro wrote:
Hi.

I modified your code a bit to achieve something similar to your needs (I'm not sure that I understood your needs completely, so feel free to post again and tell me I suck;)

Oh, you don't suck, thanks :)
BTW:
I see you use:
Code:
g_object_set (G_OBJECT (aligner), "xalign", 0, NULL);

instead of
Code:
gtk_alignment_new(...);

any reason why? I want to learn :)

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 8:36 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I used "xalign" property of GtkLabel (actually, "xalign" is property of GtkMisc to be exact) only to avoid using alignment widget to achieve what can be done without it (labels default xalign is 0.5).

The most important modifications were made with GtkAttachOptions combination of flags.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 1:59 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
tadeboro wrote:
Hi.

I used "xalign" property of GtkLabel (actually, "xalign" is property of GtkMisc to be exact) only to avoid using alignment widget to achieve what can be done without it (labels default xalign is 0.5).

The most important modifications were made with GtkAttachOptions combination of flags.

Oh...I see...I'll study more the wigets properties to see if I can use with g_object_set...
Thanks :)

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 1:42 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
ok, another question...in same context with gtk2::table...in normal state is correct:
good entries
But when I resize the GUI form, the entries are moved:
bad entries
My question is how to keep the entries align to the left, so only the right part of them are resize?
THanks.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 3:22 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
GtkTable can get really confusing real quick my advise to you would be to

1. Use glade3 to design your application gui.
2. Design a mock gui in glade3 and take notice to they packing, border, etc, option your setting and apply those setting to your code.

Your problem is with packing and alignment. Using Tables for layout even in glade3 is difficult to make look and act right.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 9:17 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

To keep th entries aligned to the left, you need to pack their corresponding labels without the GTK_EXPAND option mask.

The attach statements would look something like this:
Code:
gtk_table_attach( GTK_TABLE( table ), label1, 0, 1, 1, 2, GTK_FILL, GTK_SHRINK, 0, 0 );
gtk_table_attach( GTK_TABLE( table ), entry1, 1, 2, 1, 2, GTK_FILL | GTK_EXPAND, GTK_SHRINK, 0, 0 );
gtk_table_attach( GTK_TABLE( table ), label2, 0, 1, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0 );
gtk_table_attach( GTK_TABLE( table ), entry2, 1, 2, 2, 3, GTK_FILL | GTK_EXPAND, GTK_SHRINK, 0, 0 );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 2:44 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Nice :)
Thanks to both.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group