Is there a GTK3 function that detects if a window has focus? I am currently using the following code:
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
GtkWidget *LinuxWindow;
static void buttonMessage(GtkWidget *widget, gpointer data)
{
g_print("Yay, you clicked me!\n");
}
int main() {
GtkWidget *Box, *Button;
int argC = 0;
char **argV;
// Setup the window and fixed grid
gtk_init(&argC, &argV);
LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(LinuxWindow), "Title");
// Setup the window events
gtk_widget_show_all(LinuxWindow);
g_signal_connect(G_OBJECT(LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
NULL);
// Add controls
Button = gtk_button_new_with_label("Click Me!");
g_signal_connect(Button, "clicked", G_CALLBACK(buttonMessage), NULL);
gtk_fixed_put(GTK_FIXED(Box), Button, 20, 20);
gtk_fixed_move(GTK_FIXED(Box), Button, 20, 20);
gtk_widget_set_size_request(Button, 30, 100);
gtk_container_add(GTK_CONTAINER(LinuxWindow), Box);
gtk_widget_show_all(LinuxWindow);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(
GTK_WINDOW(LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Hello and welcome to my GTK GUI...", NULL);
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_main();
return 0;
}
I am compiling it using
g++ -std=c++17 -m64 -o gtkTest myGtkApp.cpp -lX11 `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
I want to detect if the window is focused and print it to the console.
There is a function: gtk_widget_is_focus ().
You need to assure parents have the "has-focus" property set.
Doc. excerpt:
gtk_widget_is_focus (GtkWidget *widget);
Determines if the widget is the focus widget within its toplevel. (This does not mean that the “has-focus” property is necessarily set; “has-focus” will only be set if the toplevel widget additionally has the global input focus.)
If you want to receive an event when the window is focused then register a callback for the enter-notify-event (signal)
In the linked doc. there is a section just three points after enter-notify-event that is what you want:
The “focus” signal
Sorry, I should have to mention this event the first time.
Related
this code show me on xubuntu 21.04 form with size (400px width + 400px height) and with ONE button.
How can I add to this code two buttons?
#include <gtk/gtk.h>
static void activate(GtkApplication *app, void *user_data) {
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_child(GTK_WINDOW(window), gtk_label_new("Hello World!"));
gtk_window_present(GTK_WINDOW(window));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
// Create a new button
GtkWidget *button = gtk_button_new_with_label ("press 123");
gtk_window_set_child (GTK_WINDOW (window), button);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
}
int main(int argc, char *argv[]) {
g_autoptr(GtkApplication) app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
In GTK4, I have found that a window (GtkWindow or GtkApplicationWindow) can only have one child. So to include multiple widgets within a window (such as a label and three buttons) one usually has to first create a grid object (GtkGrid), place the widgets within the grid at specified rows and columns, and then set the grid as the child of the window. Using your sample code above, I revised the code to look like the following:
#include <gtk/gtk.h>
static void activate(GtkApplication *app, void *user_data)
{
GtkWidget *window = gtk_application_window_new(app);
GtkWidget *grid = gtk_grid_new();
GtkWidget *label = gtk_label_new("Hello World");
//gtk_window_set_child(GTK_WINDOW(window), gtk_label_new("Hello World!"));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
gtk_grid_set_column_spacing(GTK_GRID(grid),10);
gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
// Create a new button
GtkWidget *button1 = gtk_button_new_with_label ("Press 1");
GtkWidget *button2 = gtk_button_new_with_label ("Press 2");
GtkWidget *button3 = gtk_button_new_with_label ("Press 3");
gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 3, 1);
gtk_grid_attach(GTK_GRID(grid), button1, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button2, 1, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button3, 2, 1, 1, 1);
gtk_window_set_child (GTK_WINDOW (window), grid);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (gtk_window_close),
window);
g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_window_close),
window);
g_signal_connect_swapped (button3, "clicked", G_CALLBACK (gtk_window_close),
window);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char *argv[])
{
g_autoptr(GtkApplication) app = gtk_application_new(NULL,
G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
This results in establishing a window with the two additional buttons you wanted. I did not know what type of functions those buttons should trigger, so for my revisions to your sample code, I just hooked the two additional buttons to the same closure signal. Following is a sample of the window when I run the program.
I hope that helps you out.
Regards.
Additional Notes:
Regarding the request as how to add "label2" and then have that label's text updated to "Network Connections", the following additions to your sample program could provide a way to do this.
First, a callback function for updating the new label's text would be added to the program (usually at the beginning of the program).
void on_button1_clicked (GtkLabel *lbl)
{
gtk_label_set_text(lbl, "Network Connections");
}
Then, within the activation function, the new label would be defined.
GtkWidget *label2 = gtk_label_new("");
Next, the new label widget would be added to the grid (in this example, it was added next to the "label" widget in the first row).
gtk_grid_attach(GTK_GRID(grid), label2, 1, 0, 2, 1);
Finally, since the request in the comment was to have the label's text updated to "Network Connections", the signal connection for the first button would be revised to call the new "on_button1_clicked" callback function and passing the "label2" widget instead of the "window" widget.
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (on_button1_clicked), label2);
The result should net the desired behavior.
Hopefully, that addresses your comment.
Regards.
I have programed the GUI in C++.
#include <gtk/gtk.h>
#include <unistd.h>
#include <cstddef>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
int main(){
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char** argV;
// Setup the window and fixed grid
gtk_init(&argC,&argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow),"Title");
// Finish up
gtk_widget_show(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow),"destroy",G_CALLBACK(gtk_main_quit), NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box),_Button,20,20);
gtk_fixed_move(GTK_FIXED(_Box),_Button,20,20);
gtk_widget_show(_Box);
gtk_widget_set_size_request(_Button,30,100);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK_CANCEL,"OK or Cancel?",NULL,g_strerror(errno));
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_container_add(GTK_CONTAINER(_LinuxWindow),_Box);
gtk_widget_show(_Box);
gtk_widget_show_all(_LinuxWindow);
gtk_main();
return 0;
}
When I run it using
g++ -o out-withoutCross without-cross.cpp -lX11 `pkg-config --cflags gtk+-3.0`pkg-config --libs gtk+-3.0`
A message box and a window appears. The problem is the controls in the window do not appear while the message box is open. I think this may be because the message box is being ran on the same thread that the main window is. Is there any way to add multi-threading to GTK?
Also the return value of the message box printed to console is running after the main window is exited.
The problem is the structure of your code..\
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char **argV;
// Setup the window and fixed grid
gtk_init(&argC, &argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow), "Title");
// Finish up
gtk_widget_show_all(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box), _Button, 20, 20);
gtk_fixed_move(GTK_FIXED(_Box), _Button, 20, 20);
gtk_widget_set_size_request(_Button, 30, 100);
gtk_container_add(GTK_CONTAINER(_LinuxWindow), _Box);
gtk_widget_show_all(_LinuxWindow);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(
GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "OK or Cancel?", NULL);
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_main();
return 0;
}
gtk_dialog_run is a blocking function so it will wait until it gets a response from the dialog box.. to proceed further
In your code, you are adding the button to the window only after running the dialog box window (which is a blocking call) so after its execution, the button is being added to the window
I'm really new with GTK, and im trying to learn how to handle it for a project i have in mind.
The thing is, i could manage to make an app for placing "Generators" on a window ... Each time i click on the screen, the app place a gen in the window, and prepare another one ready to be placed ...
The problem is that the program blinks when is drawing all the generators ...
Each gen has to redraw itself all the time, and this is i think the problem... Here is the code ... how can i make it faster? ... Thanks in advance!!
// gcc main.c -o main `pkg-config gtk+-3.0 --cflags --libs`
#include <gtk/gtk.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
class Gen{
public:
int x;
int y;
GdkPixbuf *pix;
GtkWidget *canvas;
bool placed;
Gen(GtkWidget *canvas){
this->canvas=canvas;
GError *err = NULL;
pix = gdk_pixbuf_new_from_file("./Img/gen.png", &err);
pix= gdk_pixbuf_scale_simple(pix,50,50, GDK_INTERP_BILINEAR);
x=10;y=10;
placed=0;
}
void draw(cairo_t *cr){
gdk_cairo_set_source_pixbuf(cr, pix, x, y);
cairo_paint(cr);
}
void updatePosition(int a, int b){
if(placed==0){
x=a-25;
y=b-25;
}
}
void place(){
placed=1;
}
};
class Map{
public:
vector<Gen *> Gens;
GtkWidget *window;
GtkWidget *canvas;
int xPointer,yPointer;
Map(GtkWidget *_window, GtkWidget *_canvas){
window=_window;
canvas=_canvas;
}
void draw(){
cairo_t *cr;
cr = gdk_cairo_create (gtk_widget_get_window(canvas));
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, xPointer-35, yPointer-35, 70, 70);
cairo_paint(cr);
for(vector<Gen *>::const_iterator i=Gens.begin();i!=Gens.end();i++){
(*i)->draw(cr);
}
cairo_destroy (cr);
}
void place(){
Gen *aux=Gens.back();
aux->place();
//Gens.push_back(new Gen(canvas));
}
void moving(int x,int y){
xPointer=x;yPointer=y;
if(Gens.size()==0){
Gens.push_back(new Gen(canvas));
}
else if (Gens.back()->placed==1){
Gens.push_back(new Gen(canvas));
}
Gen *aux=Gens.back();
aux->updatePosition(x,y);
this->draw();
cout<<"Elementos -> "<<Gens.size()<<"\n";
}
};
static gboolean
moving(GtkWidget *da, GdkEvent *event, gpointer data)
{
int x, y;
GdkModifierType state;
gdk_window_get_device_position (gdk_event_get_window ((GdkEvent *) event),
gdk_event_get_device ((GdkEvent *) event),
&x, &y, &state);
/*
(void)event; (void)data;
((Gen *)da)->draw();*/
Map *g=(Map *)data;
g->moving(x,y);
}
static gboolean
placing (GtkWidget *da, GdkEvent *event, gpointer data)
{
Map *g=(Map *)data;
g->place();
}
int main ( int argc, char **argv) {
GtkWidget *window;
GtkWidget *canvas;
gtk_init (&argc , &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (window,
500, 500);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_main_quit) , NULL);
canvas = gtk_drawing_area_new ();
Map *g=new Map(window,canvas);
gtk_container_add (GTK_CONTAINER (window), canvas);
gtk_widget_set_events(window, GDK_POINTER_MOTION_MASK);
//g_signal_connect (canvas, "draw", G_CALLBACK (drawing), (gpointer *)g);
g_signal_connect (window, "motion-notify-event", G_CALLBACK (moving), (gpointer *)g);
g_signal_connect (window, "button-press-event", G_CALLBACK (placing), (gpointer *)g);
//g_signal_connect (canvas, "motion-notify-event", (GCallback) on_window_draw, NULL);
gtk_widget_set_app_paintable(canvas, TRUE);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Several remarks here:
Gen::pix is allocated each time a Gen object is created. It's the same pixbuf, but you create one, then another one when using the scaling function (meaning you're leaking memory of the original pixbuf), and this for each Gen object. This is really unefficient, so using a static pix member, loading then scaling the pixbuf and fixing the memory leak would allow you to do this only once.
Then: you're calling gdk_cairo_create in the draw handler, but since GTK 3, you're supposed to get the cairo context as an input parameter in the draw signal callback. I see you're calling a custom draw method through the motion events, that's not how the GTK+ drawing stack works!
To do it right:
in the main, connect to the draw signal of the GtkDrawingArea
in your motion callbacks, just change positions of the Gen objects, and call gtk_widget_queue_draw for the drawing area. This will fire the draw signal for you.
in the callback connected to the draw signal, you then redraw your Gen objects in the cairo context you're given.
to improve performance, you can use the cairo clipping functions, or call gtk_widget_queue_draw_area or gtk_widget_queue_draw_region instead of gtk_widget_queue_draw. You'll get then a pre-computed clipping region in the cairo context you'll receive in your draw callback. With those hints, you can determine exactly what part of the image needs to be redrawn, and avoid unnecessary work.
Please read The GTK+ Drawing Model in the official documentation.
I'm developing a Gtk application in c++. (Gtk 3.0). My problem is that I can't change ProgressBar height!
(https://developer.gnome.org/gtk3/stable/GtkProgressBar.html)
The picture below will show what I need. In general I would like to have a possibility to set up height of progress bar in my code.progress bar height
I'm using following command to compile my example:
gcc progress.cpp `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0
This the sample of my code:
#include <gtk/gtk.h>
static gboolean
fill (gpointer user_data)
{
GtkWidget *progress_bar = user_data;
/*Get the current progress*/
gdouble fraction;
fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress_bar));
/*Increase the bar by 10% each time this function is called*/
fraction += 0.1;
/*Fill in the bar with the new fraction*/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);
/*Ensures that the fraction stays below 1.0*/
if (fraction < 1.0)
return TRUE;
return FALSE;
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *progress_bar;
gdouble fraction = 0.0;
/*Create a window with a title, and a default size*/
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "ProgressBar Example");
gtk_window_set_default_size (GTK_WINDOW (window), 220, 20);
/*Create a progressbar and add it to the window*/
progress_bar = gtk_progress_bar_new ();
gtk_container_add (GTK_CONTAINER (window), progress_bar);
/*Fill in the given fraction of the bar. Has to be between 0.0-1.0 inclusive*/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);
/*Use the created fill function every 500 milliseconds*/
g_timeout_add (500, fill, GTK_PROGRESS_BAR (progress_bar));
gtk_widget_show_all (window);
}
int
main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
For GTK 3.20 and newer, when defining the CSS, it seems that, instead of using the progressbar class, you must use the progress and trough classes. This is what worked for me:
progress, trough {
min-height: 30px;
}
I am trying to compile a gtk example program, but I am getting this compilation error:
gcc: error: -lgtk-x11-2.0: No such file or directory
this is the command I am using to compile:
gcc -Wall -g helloworld.c -o helloworld `pkg-config --cflags gtk+-2.0` \ `pkg-config --libs gtk+-2.0`
and this is my code (it is from this site https://developer.gnome.org/gtk-tutorial/2.90/c39.html):
#include <gtk/gtk.h>
/* This is a callback function. The data arguments are ignored
* in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
/* If you return FALSE in the "delete-event" signal handler,
* GTK will emit the "destroy" signal. Returning TRUE means
* you don't want the window to be destroyed.
* This is useful for popping up 'are you sure you want to quit?'
* type dialogs. */
g_print ("delete event occurred\n");
/* Change TRUE to FALSE and the main window will be destroyed with
* a "delete-event". */
return TRUE;
}
/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* When the window is given the "delete-event" signal (this is given
* by the window manager, usually by the "close" option, or on the
* titlebar), we ask it to call the delete_event () function
* as defined above. The data passed to the callback
* function is NULL and is ignored in the callback function. */
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
/* Here we connect the "destroy" event to a signal handler.
* This event occurs when we call gtk_widget_destroy() on the window,
* or if we return FALSE in the "delete-event" callback. */
g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* Creates a new button with the label "Hello World". */
button = gtk_button_new_with_label ("Hello World");
/* When the button receives the "clicked" signal, it will call the
* function hello() passing it NULL as its argument. The hello()
* function is defined above. */
g_signal_connect (button, "clicked",
G_CALLBACK (hello), NULL);
/* This will cause the window to be destroyed by calling
* gtk_widget_destroy(window) when "clicked". Again, the destroy
* signal could come from here, or the window manager. */
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),
window);
/* This packs the button into the window (a gtk container). */
gtk_container_add (GTK_CONTAINER (window), button);
/* The final step is to display this newly created widget. */
gtk_widget_show (button);
/* and the window */
gtk_widget_show (window);
/* All GTK applications must have a gtk_main(). Control ends here
* and waits for an event to occur (like a key press or
* mouse event). */
gtk_main ();
return 0;
}
The backslash before the second call to pkg-config is adding a space at the beginning of the argument. Remove it.
gcc: error: -lgtk-x11-2.0: No such file or directory
^