I'm in the process of creating a gtk2 application which I would like drawn over all other apps at all times, I would rather use xlib to do that if it is possible
here's my code so far:
#include <gtk/gtk.h>
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *mainwin;
mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show_all (mainwin);
gtk_main ();
return 0;
}
I'd like to do this in the simplest manner possible
thanks
Yes it is possible and not complicated
#include <gtk/gtk.h>
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *mainwin;
mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_keep_above ( (GtkWindow *) mainwin, TRUE);
gtk_widget_show_all (mainwin);
gtk_main ();
return 0;
}
gtk_window_set_keep_above does trick if window manager is cooperative.
Related
I have a simple GTK app written in C that segfaults on the line window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
Here is the code:
#include <gtk/gtk.h>
int main(int argc, char **argv) {
GtkApplication *app = NULL;
GtkWidget *window = NULL;
app = gtk_application_new("com.github.Toothless204", G_APPLICATION_FLAGS_NONE);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_application_add_window(app, GTK_WINDOW(window));
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
I have googled around to find an answer but didn't find anything that actually provided an answer as to what the problem could be
Problem: tried to create a window before the GTK+ initialization functions was called by the GtkApplication.
Before using GTK+, you need to initialize it.
https://developer.gnome.org/gtk3/stable/gtk3-General.html
#include <gtk/gtk.h>
int main(int argc, char **argv) {
GtkWidget *window = NULL;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
gtk_main();
return 0;
}
For the same reason GtkApplication windows must be added after the GApplication 'startup' signal has been emitted.
https://wiki.gnome.org/HowDoI/GtkApplication
#include <gtk/gtk.h>
static void activate (GApplication *app, gpointer user_data) {
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW(window));
gtk_widget_show (window);
}
int main (int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new ("com.github.Toothless204", 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 a new C programmer coming from Java. After reading some old books and articles, I've written the following code:
#include <gtk/gtk.h>
static void activate(GtkApplication* app, gpointer user_data) {
GtkWidget *window;
GtkWidget *button1;
GtkWidget *box;
box = gtk_alignment_new(0, 0, 0, 0);
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Calculator");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 400);
gtk_container_add(GTK_CONTAINER(window), box);
button1 = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(button1), "1");
gtk_widget_set_size_request(button1, 40, 30);
gtk_container_add(GTK_CONTAINER(box), button1);
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("me.test.calculator", 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;
}
The code compiles and runs correctly. The problem is that gtk_alignment_new is deprecated and I want to get rid of it.
I've tried replacing gtk_alignment_new with:
gtk_widget_set_halign(box, GTK_ALIGN_START);
gtk_widget_set_valign(box, GTK_ALIGN_START);
but the window does not show up when using this method. Thanks.
You want to set the halign/valign properties of the button (and then add the button straight into the window) to achieve the same functional results as your original code. 'box' is no longer needed at all.
Note that a GtkWindow is a GtkBin so only takes a single child: you will need to add additional containers in between to actually make a calculator. Maybe start by adding a GtkGrid as the sole window child and then attach all your buttons into the grid.
Using jku's advice I've written the following code which compiles and runs correctly without using gtk_alignment_new:
#include <gtk/gtk.h>
static void activate(GtkApplication* app, gpointer user_data) {
GtkWidget *window;
GtkWidget *button1;
GtkWidget *fixed;
fixed = gtk_fixed_new();
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Calculator");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 400);
gtk_container_add(GTK_CONTAINER(window), fixed);
button1 = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(button1), "1");
gtk_widget_set_size_request(button1, 45, 35);
gtk_fixed_put(GTK_FIXED(fixed), button1, 5, 200);
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("me.test.calculator", 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;
}
NOTE: I ended up using GtxFixed because the size of the window will be fixed as well.
i´m just working arround in GTK3 with an input field and found a problem what i do not understand. I can write in the field gtk_entry_new on TOPLEVEL modus but i can not in POPUP modus. Here is my shortened code (I kow this does noting but i is more easy to read):
#include <gtk/gtk.h>
int main(int argc, char **argv) {
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *inputfield;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
inputfield = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(window), inputfield);
//this two lines are make TOPLEVEL look like POPUP
gtk_window_set_decorated (GTK_WINDOW(window), FALSE);
gtk_window_set_resizable (GTK_WINDOW(window), FALSE);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
When I change the line window = gtk_window_new(GTK_WINDOW_TOPLEVEL); to window = gtk_window_new(GTK_WINDOW_POPUP); I can not insert any characters into the field. Someone has a hint for me?
I created a filechooser dialog in Glade. I placed OK and Cancel buttons in it. After that I set the clicked handler of the GtkButton to be open_clicked or whatever; also I have a button which displays the filechooser dialog. Its signal handler is cb_show_filed. Here is my code:
#include <gtk/gtk.h>
typedef struct _Data Data;
struct _Data
{
GtkWidget *file;
};
G_MODULE_EXPORT void cb_show_filed(GtkButton *button, Data *data)
{
gtk_dialog_run(GTK_DIALOG(data->file));
gtk_widget_hide(data->file);
}
int main(int argc, char **argv)
{
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *filechooserdialog1;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "ui.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
filechooser =
GTK_WIDGET(gtk_builder_get_object(builder, "filechooserdialog1"));
gtk_builder_connect_signals(builder, &data);
g_object_unref(G_OBJECT(builder));
gtk_widget_show(window);
gtk_main();
return (0);
}
Now, how can I get path and filename from filechooserdialog1?
Use gtk_file_chooser_get_file() and friends:
GFile *chosen_file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(filechooser));
I have been scratching my head on Why is below code triggers on-draw callback three times as opposed to just once.
#include <iostream>
#include <gtk/gtk.h>
using namespace std;
void on_draw(){
cout << "drawing"<<endl;
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *drgArea;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
drgArea = gtk_drawing_area_new();
// gtk_widget_set_redraw_on_allocate(drgArea, -1);
gtk_container_add(GTK_CONTAINER(window), drgArea);
gtk_widget_show_all(window);
g_signal_connect(drgArea, "expose-event",
G_CALLBACK(on_draw), NULL);
gtk_main();
return 0;
}
This is probably related to your compositor/window manager.
Using cinnamon [2.0.14] shows 2 redraws when starting up the application (no matter if I use gtk3/3.10.6/"draw" or gtk2/2.24.22/"expose-event").