LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-14-2022, 02:24 PM   #1
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 549

Rep: Reputation: 84
Programming with GTK v3 - position controls


Is it possible to position controls, such as LABELS, EDITBOX, COMBOBOX, CHECKBOXES, BUTTONS manually?
Or must we use gtk_box_new() and gtk_box_pack_start()


Example:
This code just creates a window with a button on it.
The button is not visible because something is wrong.

I have similar code done in Qt.
Qt has functions to establish a parent------child relationship.
So, I can create a window
create a button
make the button a child of the window,
then I can change the position and size of the button and the coordinate is with respect to the {0,0} of the client area of the window.

Qt has some box and autopositioning functions. But it feels like I don’t have control and besides, sometimes, I want to do something custom.




Code:
static void OnActivate(GtkApplication *app)
{
	//Create a new window
	GtkWidget *pTheWindow=gtk_application_window_new(app);

	gtk_window_set_title(GTK_WINDOW(pTheWindow), "www.lucidarme.me");

	gtk_window_resize((GtkWindow *)pTheWindow, 500, 700);

	//Create a new button
	GtkWidget *button=gtk_button_new_with_label("Hello, World!");
	//When the button is clicked, close the window passed as an argument
	g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_close), pTheWindow);

	//gtk_window_set_child(GTK_WINDOW(window), button);
	gtk_window_set_attached_to(GTK_WINDOW(button), pTheWindow);

	gtk_window_present(GTK_WINDOW(pTheWindow));

	gtk_window_move(GTK_WINDOW(pTheWindow), 10, 10);
	gtk_window_move(GTK_WINDOW(button), 10, 10);
	gtk_window_resize(GTK_WINDOW(button), 100, 22);


	//gtk_window_set_default_geometry(GtkWindow *window, gint width, gint height);
	//gtk_window_move(GtkWindow *window, gint x, gint y);
	//gtk_window_resize((GtkWindow *)pTheLabel, 100, 22);

}

int main(int argc, char *argv[])
{
	//Create a new application
	GtkApplication *app;
	int status;


	app=gtk_application_new("com.example.GtkApplication", G_APPLICATION_FLAGS_NONE);
	g_signal_connect(app, "activate", G_CALLBACK(OnActivate), NULL);
	status=g_application_run(G_APPLICATION(app), argc, argv);
	g_object_unref(app);

	return status;
}
 
Old 11-14-2022, 03:00 PM   #2
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 999

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
You have to pack the button into a container like GtkBox or GtkGrid. The container controls the positioning of its children.
Ed
 
Old 11-15-2022, 09:21 AM   #3
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 549

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by EdGr View Post
You have to pack the button into a container like GtkBox or GtkGrid. The container controls the positioning of its children.
Ed
Alright but it seems to have a mind of its own. I can’t scale and position my BUTTON?
What if I want a button that is 10 x 10 pixels?

Code:
static void OnActivate(GtkApplication *app)
{
	GtkWidget *vbox;

	//Create a new window
	GtkWidget *pTheWindow=gtk_application_window_new(app);

	gtk_window_set_title(GTK_WINDOW(pTheWindow), "www.lucidarme.me");

	gtk_window_resize((GtkWindow *)pTheWindow, 500, 700);

	//Create a new button
	GtkWidget *button=gtk_button_new_with_label("Hello, World!");
	//When the button is clicked, close the window passed as an argument
	g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_close), pTheWindow);

	//I think this is part of GTK v4.0
	//gtk_window_set_child(GTK_WINDOW(window), button);

	//This does not work
	//gtk_window_set_attached_to(GTK_WINDOW(button), pTheWindow);

	vbox=gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
	//gtk_box_pack_start(GTK_BOX(vbox), label, 0, 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), button, 0, 0, 0);
	gtk_container_add(GTK_CONTAINER(pTheWindow), vbox);

	gtk_window_present(GTK_WINDOW(pTheWindow));
	gtk_widget_show_all(pTheWindow);

	gtk_window_move(GTK_WINDOW(pTheWindow), 10, 10);
	gtk_window_move(GTK_WINDOW(button), 10, 10);
	gtk_window_resize(GTK_WINDOW(button), 100, 22);


	//gtk_window_set_default_geometry(GtkWindow *window, gint width, gint height);
	//gtk_window_move(GtkWindow *window, gint x, gint y);
	//gtk_window_resize((GtkWindow *)pTheLabel, 100, 22);

}
Attached Thumbnails
Click image for larger version

Name:	zzzzlucida.png
Views:	7
Size:	6.7 KB
ID:	39869  
 
Old 11-15-2022, 10:08 AM   #4
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 999

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
GTK determines the widget's size and position from the theme, font, DPI settings, etc. This is a nontrivial amount of logic.

The application can choose its own image sizes. GTK will adjust the layout to accommodate images.
Ed
 
Old 11-15-2022, 04:22 PM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
Couple of examples from my snippets.

bclickgtk3.c
Code:
#include <gtk/gtk.h>

void button_clicked(GtkWidget *widget, gpointer data) {    
    g_print("clicked gtk3\n");
}

int main(int argc, char *argv[]) 
{
    GtkWidget *window;
    GtkWidget *halign;
    GtkWidget *btn;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "GtkButton");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
    gtk_container_set_border_width(GTK_CONTAINER(window), 15);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    btn = gtk_button_new_with_label("Click");
    gtk_widget_set_halign(btn, GTK_ALIGN_START);
    gtk_widget_set_valign(btn, GTK_ALIGN_START);
    gtk_widget_set_size_request(btn, 70, 30);
    
    gtk_container_add(GTK_CONTAINER(window), btn);

    g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_clicked), NULL);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

//gcc bclickgtk3.c -o bclickgtk3 $(pkg-config --cflags --libs gtk+-3.0)
corner.c
Code:
#include <gtk/gtk.h>

int main(int argc, char *argv[]) 
{
    GtkWidget *window;
    GtkWidget *okBtn;
    GtkWidget *clsBtn;

    GtkWidget *vbox;
    GtkWidget *hbox;
    GtkWidget *halign;
    GtkWidget *valign;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 350, 200);
    gtk_window_set_title(GTK_WINDOW(window), "Corner buttons");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);

    vbox = gtk_vbox_new(FALSE, 5);

    valign = gtk_alignment_new(0, 1, 0, 0);
    gtk_container_add(GTK_CONTAINER(vbox), valign);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    hbox = gtk_hbox_new(TRUE, 3);

    okBtn = gtk_button_new_with_label("OK");
    gtk_widget_set_size_request(okBtn, 70, 30);
    gtk_container_add(GTK_CONTAINER(hbox), okBtn);
    clsBtn = gtk_button_new_with_label("Close");
    gtk_container_add(GTK_CONTAINER(hbox), clsBtn);

    halign = gtk_alignment_new(1, 0, 0, 0);
    gtk_container_add(GTK_CONTAINER(halign), hbox);

    gtk_box_pack_start(GTK_BOX(vbox), halign, FALSE, FALSE, 0);

    g_signal_connect(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}
fixed.c
Code:
//Fixed Buttons

#include <gtk/gtk.h>

int main(int argc, char *argv[]) 
{    
    GtkWidget *window;
    GtkWidget *fixed;

    GtkWidget *btn1;
    GtkWidget *btn2;
    GtkWidget *btn3;
    GtkWidget *btn4;
    GtkWidget *btn5;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "GtkFixed");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    fixed = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(window), fixed);

    btn1 = gtk_button_new_with_label("Button1");
    gtk_fixed_put(GTK_FIXED(fixed), btn1, 15, 15);
    gtk_widget_set_size_request(btn1, 200, 30);

    btn2 = gtk_button_new_with_label("Button2");
    gtk_fixed_put(GTK_FIXED(fixed), btn2, 100, 60);
    gtk_widget_set_size_request(btn2, 80, 50);

    btn3 = gtk_button_new_with_label("Run");
    gtk_fixed_put(GTK_FIXED(fixed), btn3, 15, 180);
    gtk_widget_set_size_request(btn3, 40, 30);
    
    btn4 = gtk_button_new_with_label("Save");
    gtk_fixed_put(GTK_FIXED(fixed), btn4, 100, 180);
    gtk_widget_set_size_request(btn3, 80, 40);
    
    btn5 = gtk_button_new_with_label("Quit");
    gtk_fixed_put(GTK_FIXED(fixed), btn5, 195, 180);
    gtk_widget_set_size_request(btn3, 40, 60);

    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

//gcc fixed.c -o fixed $(pkg-config --cflags --libs gtk+-3.0)
 
  


Reply



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
[SOLVED] "<gtk/gtk.h> not found" when trying to compile C GTK program. bcsm Programming 4 07-25-2021 12:28 PM
Glitch: no icons for "image position" option in xsane when run using oxygen-gtk theme lems Slackware 0 10-27-2013 04:44 PM
LXer: Carla Schroder: Whoever controls technology controls society LXer Syndicated Linux News 0 03-22-2012 09:40 AM
large gtk+ controls - making them smaller jayesh.bhoot Linux - General 1 09-17-2010 05:39 AM
GTK controls colours TheStarLion Linux - Desktop 2 01-22-2010 07:57 AM

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

All times are GMT -5. The time now is 05:24 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