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
Related
I am using Ubuntu 20.04 and I have a small GTK 3.0 Form I am trying to get to maximize on first startup. I later want to get this into a Full GLADE project, but first I'm just trying to get this to maximize to full screen.
The commented sections are what I have tried to no avail.
Nothing works and it throws compiler errors, can anyone help me in my endeavor?
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
// gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
// gtk_window_fullscreen(GTK_WINDOW(window);
// gtk_window_fullscreen(GtkWindow *window);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Thank you.
Use gtk_window_maximize(GtkWindow *window):
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
//...
}
Note that you can call it before showing the window. Doc:
Asks to maximize window , so that it becomes full-screen. Note that you shouldn’t assume the window is definitely maximized afterward, because other entities (e.g. the user or window manager) could unmaximize it again, and not all window managers support maximization. But normally the window will end up maximized. Just don’t write code that crashes if not.
It’s permitted to call this function before showing a window, in which case the window will be maximized when it appears onscreen initially.
You can track maximization via the “window-state-event” signal on GtkWidget, or by listening to notifications on the “is-maximized” property.
Suggestion: Use gtkmm with C++
Example with gtkmm:
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.maximize.base");
Gtk::Window window;
window.maximize();
return app->run(window);
}
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.
GTK uses cairo for drawing. So I'm trying to create a hello world app that writes to an image (svg, png, ...) instead of X11. I'm facing 2 problems:
- The image is empty
- When starting without X11 running (which is the actual goal) I get the error "** (a.out:9021): WARNING **: Could not open X display"
The code is draft!
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <cmath>
#include <cairo.h>
#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *button;
// GtkWidget *main_window = gtk_initialize();
window = gtk_offscreen_window_new();
button = gtk_button_new_with_label ("Hello World");
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (window);
GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
std::cout << "gdk window: " << gdk_window << std::endl;
cairo_surface_t * surfp = gdk_offscreen_window_get_surface(gdk_window);
std::cout << "Created Window will now draw to png" << std::endl;
std::string filename = "image.svg";
double width = 600;
double height = 400;
Cairo::SvgSurface srfobj(surfp);
Cairo::RefPtr<Cairo::SvgSurface> refptr(&srfobj);
Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(refptr);
cr->save(); // save the state of the context
cr->show_page();
std::cout << "Wrote SVG file \"" << filename << "\"" << std::endl;
std::chrono::milliseconds dura( 200 );
std::this_thread::sleep_for(dura);
return 0;
}
Why is this code not working?
Can I run a gtk app without X11 running, or should I just ignore the warning?
Here is my solution based on your example code - keep in mind it is a dirty solution and may not work using newer versions of GTK3. It works to save the UI of a window (only tested with the one button), but still requires (somewhere) a running X-server. It also ignores / don't use your settings for the picture size - you'll have to resize it at your own. I don't know if (and how) it is possible to cut this string (X-Server // X-Framebuffer) too (DirectFB seems not to be really supported anymore), but...
Have fun!
// Default
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
// cairo / cairomm / gtk
#include <cairo.h>
#include <cairomm/context.h> //libcairomm-1.0-dev
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
// Init
gtk_init(&argc, &argv);
// Create window with a button
GtkWidget *window;
GtkWidget *button;
window = gtk_offscreen_window_new();
button = gtk_button_new_with_label("Hello World");
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
// Make a gdk window out of it, prepare cairo and draw it to it
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
cairo_surface_t* surfp = gdk_offscreen_window_get_surface(gdk_window);
cairo_t* context = cairo_create(surfp);
gtk_widget_draw(GTK_WIDGET(window), context);
// Yay - begin the dump!
Cairo::SvgSurface srfobj(surfp);
std::string filename = "image.png";
srfobj.write_to_png(filename);
std::cout << "Done." << std::endl;
// Aaand a little sleep...
std::chrono::milliseconds dura(1000);
std::this_thread::sleep_for(dura);
return 0;
}
Try to use gtk_widget_draw (widget_ptr, cairo_ctx_ptr); to draw a widget (or a hierarchy of widgets) to a cario context?
The answer to both your questions is that you cannot run GTK+ applications without some sort of output. You're using gtk-x11 which requires an XServer. You might have some luck with the DirectFB backend, but I wouldn't hold your breath as I don't know if it's even maintained anymore.
Because Gtk doesn't run without an XServer the resulting image is empty.
I am a newbie at GTK+. I want to create a GUI with an Image with 2 buttons and a label below it.
The image has to be loaded via a file dialog which is opened when I click a button called "Load", but the image is not being shown on the Window. Below is my code:
#include <gtk/gtk.h>
#include <gtkmm.h>
#include <glib.h>
#include <cstring>
#include <cstdlib>
#include "cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
GtkWidget *window, *image;
GtkWidget *vbox, *valign, *hbox, *halign;
GtkWidget *expression_label;
char* get_file()
{
GtkWidget *dialog = gtk_file_chooser_dialog_new("Open File", (GtkWindow *) window, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
char *filename;
filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(dialog) );
gtk_widget_destroy(dialog);
return filename;
}
}
void load_file(GtkWidget *widget, gpointer data)
{
g_print("Loading file\n");
char *file = get_file();
Mat img = imread(file);
//imshow("Opencv image", img);
image = gtk_image_new_from_file(file);
gtk_widget_queue_draw(image);
// gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);
gtk_label_set_text(GTK_LABEL(expression_label), "Image Loaded");
g_print("File Loaded\n");
g_print("%s\n", file);
}
void get_expression(GtkWidget *widget, gpointer data)
{
gtk_label_set_text(GTK_LABEL(expression_label), "Expression Detected");
}
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "FEAR");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 1100, 1100);
gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
vbox = gtk_vbox_new(FALSE, 5);
hbox = gtk_hbox_new(TRUE, 3);
GtkWidget *load_button = gtk_button_new_with_label("Load");
GtkWidget *run_button = gtk_button_new_with_label("Run");
gtk_widget_set_size_request(load_button, 70, 30);
gtk_widget_set_size_request(run_button, 70, 30);
gtk_container_add(GTK_CONTAINER(hbox), load_button);
gtk_container_add(GTK_CONTAINER(hbox), run_button);
expression_label = gtk_label_new("Expression");
//image = gtk_image_new();
//Display placeholder image
image = gtk_image_new_from_file("../sample.jpg");
gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), expression_label, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
int load_handler_id = g_signal_connect(G_OBJECT(load_button), "clicked", G_CALLBACK(load_file), NULL);
g_signal_connect(G_OBJECT(run_button), "clicked", G_CALLBACK(get_expression), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I basically created a main window, then added a vbox to it. In the vbox, I have added the image widget, a hbox box that holds by buttons and a label widget to give me relevant information.
When I run the program, it displays the placeholder image properly, but when I select "Load" and choose the new image to be displayed, the label changes and I get the correct output on the command line, but the image widget does not display the new image that was selected.
Can someone please help me and let me know what am I doing wrong?
You're getting a new GtkImage widget after loading, and that widget is never added to a window so (of course) it's not being displayed.
You're just overwriting a global widget pointer with the new widget, but that doesn't magically make the new widget "replace" the old in the widget hierarchy. That you store the pointer to the new widget in the variable that used to hold the old widget doesn't matter from GTK+'s perspective.
Replace your call to gtk_image_new_from_file() with a call to gtk_image_set_from_file() on your existing widget (in image) to replace the content of the existing widget, which is packed and so on, with the image from the file.
Also, you're leaking the filename, you need to add free() after using it.
UPDATE: You mention in a comment that you've made the filename a global, to make it available in more places. If so, you still need to free() the old filename before getting a new one, else your code will be leaking memory.
I'd like to have width and height of the currently focussed window. The selection of the window works like a charm whereas the height and width are always returning 1.
#include <X11/Xlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
Display *display;
Window focus;
XWindowAttributes attr;
int revert;
display = XOpenDisplay(NULL);
XGetInputFocus(display, &focus, &revert);
XGetWindowAttributes(display, focus, &attr);
printf("[0x%x] %d x %d\n", (unsigned)focus, attr.width, attr.height);
return 0;
}
Is this not the "real" window but the currently active component (like a textbox or a button?) And why would it have the size of 1x1 anyways then? If this is the case, how do i get the main window of the application containig this control? Means... kinda the top-level window, the top-most window except the root window.
PS: Don't know whether it's really important; I use Ubuntu 10.04 32 and 64 bit.
You're right - you're seeing a child window. GTK applications, in particular, create a child window under the "real" window, which is always 1x1, and that always gets the focus when the application has the focus. If you're just running your program using the GNOME terminal, you'll always be seeing a GTK application with the focus (the terminal).
If you run your program in such a way that a non-GTK program happens to have the focus, then this doesn't happen, but you could still end up finding a child window with the focus instead of the top-level window. (One way of doing this is to run sleep before your program like this: sleep 4; ./my_program - this gives you a chance to change the focus.)
To find the top-level window, I think XQueryTree will help - it returns the parent window.
This worked for me:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
/*
Returns the parent window of "window" (i.e. the ancestor of window
that is a direct child of the root, or window itself if it is a direct child).
If window is the root window, returns window.
*/
Window get_toplevel_parent(Display * display, Window window)
{
Window parent;
Window root;
Window * children;
unsigned int num_children;
while (1) {
if (0 == XQueryTree(display, window, &root,
&parent, &children, &num_children)) {
fprintf(stderr, "XQueryTree error\n");
abort(); //change to whatever error handling you prefer
}
if (children) { //must test for null
XFree(children);
}
if (window == root || parent == root) {
return window;
}
else {
window = parent;
}
}
}
int main(int argc, char *argv[])
{
Display *display;
Window focus, toplevel_parent_of_focus;
XWindowAttributes attr;
int revert;
display = XOpenDisplay(NULL);
XGetInputFocus(display, &focus, &revert);
toplevel_parent_of_focus = get_toplevel_parent(display, focus);
XGetWindowAttributes(display, toplevel_parent_of_focus, &attr);
printf("[0x%x] %d x %d\n", (unsigned)toplevel_parent_of_focus,
attr.width, attr.height);
return 0;
}