I made a basic GtkGrid application with a simple layout but I can't make the GtkGrid fill the entire GtkWindow. This is the code for creating the layout:
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
GtkWidget *grid = gtk_grid_new();
g_signal_connect(window,"destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *vpaned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
gtk_paned_set_position (GTK_PANED (vpaned), 400);
GtkWidget *label = gtk_label_new("Label1111111111111111111111111111111");
GtkWidget *label2 = gtk_label_new("Label2");
GtkWidget *label3 = gtk_label_new("Label3");
GtkWidget *label4 = gtk_label_new("Label4");
gtk_paned_pack1 (GTK_PANED (vpaned), label, FALSE, TRUE);
gtk_paned_pack2 (GTK_PANED (vpaned), label2, FALSE, TRUE);
gtk_widget_show (label);
gtk_widget_show (label2);
gtk_widget_show (label3);
gtk_widget_show (label4);
gtk_widget_show (vpaned);
gtk_grid_attach(GTK_GRID(grid), label3, 0,0,1,1);
gtk_grid_attach(GTK_GRID(grid), vpaned, 0,1,1,1);
gtk_grid_attach(GTK_GRID(grid), label4, 0,2,1,1);
gtk_widget_show(grid);
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_widget_show (window);
You may have a look at expand property and assure it is set to TRUE (default if you do your UI within glade designer)
Related
I used the GtkAlignment widget to control the alignment and size of its child widget. But gtk_alignment_new has been deprecated since version 3.14 and should not be used in newly-written code. What functions should I use as alternatives to let the code be gtk3+ compatible?
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button;
GtkWidget *halign;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Tooltip");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
button = gtk_button_new_with_label("Button");
gtk_widget_set_tooltip_text(button, "Button widget");
halign = gtk_alignment_new(0, 0, 0, 0);
gtk_container_add(GTK_CONTAINER(halign), button);
gtk_container_add(GTK_CONTAINER(window), halign);
gtk_widget_show_all(window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
As suggested in the comment, use these two functions instead:
void gtk_widget_set_halign (GtkWidget *widget, GtkAlign align);
Sets the horizontal alignment of widget.
gtk_widget_get_valign (GtkWidget *widget); Sets the vertical alignment of widget.
The enum GtkAlign types are the following:
GTK_ALIGN_START: The 'start' of the layout. Vertically this is the top, horizontally this is left/right according to LTR/RTL
GTK_ALIGN_END: Opposite of GTK_ALIGN_START
GTK_ALIGN_CENTER: The middle of the layout
GTK_ALIGN_FILL : Take all available space
for scaling use GTK_ALIGN_FILL.
So the gtk3+ compatible alternative for your code is the following:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Tooltip");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
button = gtk_button_new_with_label("Button");
gtk_widget_set_tooltip_text(button, "Button widget");
gtk_widget_set_halign (button, GTK_ALIGN_START);
gtk_widget_set_valign (button, GTK_ALIGN_START);
gtk_container_add (GTK_CONTAINER (window), button);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
In my GTK program I have a TextEntry widget and a TreeView widget. When I click on the TextEntry widget and select some text, then click on the TreeView widget, it doesn't deselect the text in the TextEntry widget. How do I get it to deselect the text in the TextEntry widget when I click on the TreeView widget?
You can bind gtk_editable_select_region to the entry's "focus-out-event" signal, see the sample below:
#include <gtk/gtk.h>
gboolean unselect_on_focus_lost (GtkWidget *widget)
{
gtk_editable_select_region (GTK_EDITABLE (widget), 0, 0);
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *window, *box, *entry, *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width (GTK_CONTAINER (window), 5);
gtk_window_set_default_size (GTK_WINDOW (window), 400, 130);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
gtk_container_add (GTK_CONTAINER (window), box);
entry = gtk_entry_new ();
gtk_entry_set_placeholder_text (GTK_ENTRY (entry), "I will lose my selection when you click the button below");
g_signal_connect (G_OBJECT (entry), "focus-out-event", G_CALLBACK (unselect_on_focus_lost), NULL);
gtk_container_add (GTK_CONTAINER (box), entry);
entry = gtk_entry_new ();
gtk_entry_set_placeholder_text (GTK_ENTRY (entry), "I will NOT lose my selection when you click the button below");
gtk_container_add (GTK_CONTAINER (box), entry);
/* I used a button instead of a treeview to keep the code short */
button = gtk_button_new_with_label("Click me");
gtk_container_add (GTK_CONTAINER (box), button);
gtk_widget_grab_focus (button);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
i am new to gtk and i dont know how to display a toggle button in gtk.
there are no examples of the same for gtk+3.
here is what i did
GtkToolItem *tog;
tog = gtk_toggle_tool_button_new();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tog), FALSE);
i tried to add toggle button to grid, container. i also tried using gtk_widget_show and pass tog but of no use.
can any one show me the an example code or how to solve this.
PS: not a C++ programmer.
You need to show all your toplevel's widgets:
// cc `pkg-config --cflags --libs gtk+-3.0` main.c
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *grid;
GtkToolItem *tool;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
grid = gtk_grid_new ();
tool = gtk_toggle_tool_button_new ();
gtk_tool_button_set_label (GTK_TOOL_BUTTON (tool), "Hi there");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET (tool), 0, 0, 1, 1);
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
I am new to GTK+ world and trying to learn it from Foundations of GTK+ Development.
Mean while I am getting some problem in using GtkAlignment widget, as in the code below.
Even if I change the value of either one of the below I am not getting the Ok and calcel button getting aligned to right.
GtkWidget* halign = gtk_alignment_new(0, 0, 0, 0);
GtkWidget* halign = gtk_alignment_new(0, 1, 0, 0);
I think I'm missing something, but not getting exactly
Note: I am using GTK+3 on windows 7
int main(int argc, char* argv[]) {
gtk_init(&argc, &argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Gtk Alignment Sample");
gtk_container_set_border_width(GTK_CONTAINER(window), 5);
gtk_widget_set_size_request(window, 250, 400);
g_signal_connect(window, "delete_event", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget* ok_button = gtk_button_new_from_stock(GTK_STOCK_OK);
GtkWidget* cancel_button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
GtkWidget* hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start(GTK_BOX(hbox), ok_button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), cancel_button, FALSE, FALSE, 0);
GtkWidget* halign = gtk_alignment_new(0, 0, 0, 0);
gtk_container_add(GTK_CONTAINER(halign), hbox);
GtkWidget* vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(vbox), halign, FALSE, FALSE, 0);
GtkWidget* valign = gtk_alignment_new(0, 1, 0, 0);
gtk_container_add(GTK_CONTAINER(valign), vbox);
gtk_container_add(GTK_CONTAINER(window), valign);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Quote from the docs:
Note that the desired effect can in most cases be achieved by using
the "halign", "valign" and "margin" properties on the child widget, so
GtkAlignment should not be used in new code.
Try to just use
gtk_widget_set_alignment (button_ok, GTK_ALIGN_END);
gtk_widget_set_alignment (button_cancel, GTK_ALIGN_END);
//or
gtk_widget_set_alignment (hbox, GTK_ALIGN_END);
and remove all alignment widgets
I have put an image on gtklayout calling put method, but i could not get the image visible.
What is the procedure for putting an image gtklayout object so that it appears as its back ground.
Regards,
iSight
pls check if the code below would work for you:
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *layout;
GtkWidget *image;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 290, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
layout = gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER (window), layout);
gtk_widget_show(layout);
image = gtk_image_new_from_file("/home/my_test_image.jpg"); // put path to your image here
gtk_layout_put(GTK_LAYOUT(layout), image, 0, 0);
button = gtk_button_new_with_label("Button");
gtk_layout_put(GTK_LAYOUT(layout), button, 150, 50);
gtk_widget_set_size_request(button, 80, 35);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
hope this helps, regards