I'm running a basic GTK app from the GTK website's tutorials, and I'm getting the following warning:-
*** WARNING: Method userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead.
I've installed the gtk+ and gtk+3 packages with brew. Here is the code from the file:-
#include
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
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;
}
I'm running OS X 10.10.5, MacBook Pro 13in Retina.
Any ideas? Thanks!
Related
I have read the install instructions on GTK for GTK4, which I used
sudo dnf install gtk4 gtk4-devel
The code of the Hello, World example is
#include <gtk/gtk.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
gtk_window_set_child (GTK_WINDOW (window), button);
gtk_window_present (GTK_WINDOW (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;
}
I used the compile function for GCC:
gcc -o hello-world-gtk hello-world-gtk.c `pkg-config --cflags --libs gtk4`
But this error presents
gtktest.c:1:10: fatal error: gtk/gtk.h: No such file or directory
1 | #include <gtk/gtk.h>
| ^~~~~~~~~~~
As I see, I should have all the requisite packages installed and I am unsure why the compiler cannot pull the gtk library. Any help would be awesome.
Alright I found a strange solution.
For whatever reason, this did not work using their pasted compile code, however when I switched to the below, it worked just fine.
gcc `pkg-config --cflags gtk4` hello.c -o hello `pkg-config --libs gtk4`
I'm trying to run a simple function when the 'x' button is clicked in my GTK window but am having trouble getting this to work. I get this error every time I run it:
(process:17950): GLib-GObject-WARNING **: 11:58:51.480: ../../../../gobject/gsignal.c:2523: signal 'delete-event' is invalid for instance '0x55a183d991a0' of type 'GtkApplication'
Here are my functions:
main():
int main(int argc, char **argv) {
GtkApplication *app;
int status;
// Set up the application and start it
app = gtk_application_new ("com.sds.hashcrack.server", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (init), NULL);
g_signal_connect (app, "delete-event", G_CALLBACK (test), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
test():
gboolean test(GtkWidget *widget, GdkEvent *event, gpointer user_data) {
g_print("Closed");
return true;
}
Can anyone shed any light on what I'm doing wrong? Many thanks
The "delete-event" signal is available on GtkWidget instances, not on GtkApplication ones.
You will need to connect the test callback to the delete-event signal on the GtkWindow you're adding to your application.
I am coding a GUI (using GTK3) for a simple program that performs a simulation and plots the requested graph (using gnuplot).
When I'm using only gnuplot, I have no problem. The graph looks exactly like I want. However, when I try using this same code from GTK, it doesn't work anymore. It truncates all my value. Why ? What should I do to fix this?
This is the expected result (gnuplot without GTK):
And this is the actual result (gnuplot with GTK) :
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
FILE *gp = popen("gnuplot -persist", "w");
fprintf(gp, "plot '-' using 1:2 with lines\n");
int i;
for(i = 0; i < 13; i++){
fprintf(gp, "%d %f\n", i, 0.1*i);
}
pclose(gp);
}
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;
}
I have a problem with quitting a gtk application. The function call of g_application_quit results in the subsequent error:
GLib-GIO-CRITICAL **: g_application_quit: assertion 'G_IS_APPLICATION (application)' failed
The code snippet looks like this:
g_signal_connect(app_window, "destroy", G_CALLBACK(g_application_quit), app);
I tried this also, but still throws the same error:
g_signal_connect(app_window, "destroy", G_CALLBACK(g_application_quit), G_APPLICATION(app));
The app got initialized as:
GtkApplication *app = gtk_application_new("my.ne.sweeper", G_APPLICATION_FLAGS_NONE);
Any help is appreciated. Thank you.
You don't need to connect g_application_quit to window's destroy signal explicitly if You are using G_APPLICATION, it is done automatically.
Here goes the minimalistic example:
#include <gtk/gtk.h>
static void activate (GtkApplication* app, gpointer data) {
GtkWidget *window;
window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW (window), 400, 200);
gtk_widget_show_all(window);
}
int main (int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("your.app.name", 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;
}
I am trying to change the cursor of a window dynamically with GTK3 but gtk_widget_get_parent_window doesn't seem to work.
Could someone please point out what I'm doing wrong? Thanks!
// https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
// minimal example
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
// Here \/\/\/\/\/ .
GdkWindow* w = gtk_widget_get_parent_window(window);
GdkCursor* c = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_WATCH);
gdk_window_set_cursor(w, c);
// /\/\/\/\/\ .
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;
}
(main.exe:16508): Gdk-CRITICAL **: gdk_window_set_cursor: assertion
'GDK_IS_WINDOW (window)' failed
I am using GTK 3.16 with msys2
Many thanks in advance.
Extending #andlabs comment
Any attempt to change the cursor needs to be done after the widget has been added to the widget hierarchy or in GTK terms realized.
Calling gtk_widget_get_parent_window() or even gtk_widget_get_window() before a realize event has been fired for the widget will result in a NULL pointers in both cases.
Like #andlabs it is safer to use gtk_widget_get_window() in combination with a GtkWindow.
The solution.
static GdkWindow* G_WINDOW = 0;
static GdkCursor* G_CURSOR = 0;
// call after WindowRealize()
void changecursor()
{
assert(G_WINDOW != NULL);
gdk_window_set_cursor(G_WINDOW, G_CURSOR);
}
static void WindowRealize(GtkWidget *window, gpointer data)
{
G_CURSOR_HAND = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_HAND2);
G_WINDOW = gtk_widget_get_window(window);
}
static void activate(GtkApplication* app,gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(app);
...
g_signal_connect(window, "realize", G_CALLBACK(WindowRealize), NULL);
gtk_widget_show_all (window);
}