This question already has answers here:
Passing additional arguments to gtk function
(3 answers)
GTK passing structure to callback function in C
(1 answer)
Closed 3 years ago.
Hi I'm trying to program with GTK+ 2 and when I need to create a popup window after the "clicked' signal of a button, I use " gtk_widget_set_sensitive(button, FALSE) " so as not to be able to launch another popoup window from the same button when one is already launched. Yet the only way to close the popup with another callback function containing " gtk_widget_destroy(popup) " also means this callback can't "see" the first button to turn it sensitive again. Should I use a global variable and asign the popup window to it so my second callback function will see it? Or do I define more global widgets ?
What I'm really getting at is using global variables in something like GTK a common thing to do?, such as in header files, or is it bad practice at all?
Here is a sample program (with less stuff in the first callback than normal!)
#include <gtk/gtk.h>
// Is it good to set a temporary global GtkWidget * so I can asign it to
// button1 later and then use this temporary variable to set the widget sensitive again.
// GtkWidget * temp;
static void popup_win(GtkWidget *, gpointer data);
static void close_up(GtkWidget *, GtkWidget *);
int main(int argc, char * argv[])
{
GtkWidget * window, * button1, * vbox;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "EXAMPLE PROGRAM");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_widget_set_size_request(window, 800, 300);
button1 = gtk_button_new_with_label("Click here");
g_signal_connect(G_OBJECT(button1), "clicked",
G_CALLBACK(popup_win), NULL);
vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button1, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void popup_win(GtkWidget * btn, gpointer data)
{
gtk_widget_set_sensitive(btn, FALSE);
// I could use temp here. Then get to it in close_up function.
// temp = btn
GtkWidget * window2, * button2, * hbox;
window2 = gtk_window_new(GTK_WINDOW_POPUP);
gtk_window_set_title(GTK_WINDOW(window2), "POPUP WINDOW");
gtk_container_set_border_width(GTK_CONTAINER(window2), 10);
gtk_widget_set_size_request(window2, 100, 200);
button2 = gtk_button_new_with_label("Click me too");
g_signal_connect(G_OBJECT(button2), "clicked",
G_CALLBACK(close_up), (gpointer) window2);
hbox = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window2), hbox);
gtk_widget_show_all(window2);
}
static void close_up(GtkWidget * btn, GtkWidget * win)
{
// can't get at button1 so I can't set widget sensitve again.
// gtk_widget_set_sensitive(button1, TRUE);
// Unless I'm using temp variable.
// gtk_widget_set_sensitive(temp, TRUE);
gtk_widget_destroy(win);
}```
Related
I'm trying to implement the Ctrl+C shortcut a widget without it disturbing other defined shortcuts.
Problem
My window looks like this:
GtkWindow
GtkEntry
GtkToggleButton
Part of the code
// --- add checkbox ---
GtkWidget * checkbutton = gtk_check_button_new_with_label("My Checkbox");
gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(checkbutton));
// --- setup checkbox shortcut ---
GtkAccelGroup * accel_group = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
gtk_widget_add_accelerator(checkbutton, "clicked", accel_group,
GDK_KEY_c, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
g_signal_connect(checkbutton, "clicked", G_CALLBACK(onCopyCheckbox), NULL);
// problem: this event fires, even if GtkEntry is focussed
// it then block the <kbd>Ctrl+C</kbd>-Event of GtkEntry
Expected behaviour
If GtkEntry is focussed and Ctrl+C is pressed, the function callback1() should be triggered.
If GtkToggleButton is focussed and Ctrl+C is pressed, it should print "onCopyCheckbox() called\n".
Actual behavior
If GtkEntry is focussed and Ctrl+C is pressed, "onCopyCheckbox() called\n" gets printed and nothing gets copied.
If GtkToggleButton is focussed and Ctrl+C is pressed, "onCopyCheckbox() called\n" gets printed.
Please do not...
tell me I should use a different shortcut / accelerator.
Full compilable and executable code:
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
// ------------------------------------------------------------
void
onCopyCheckbox (GtkWidget *widget,
GdkDragContext *context,
gpointer user_data)
{
printf("onCopyCheckbox() called\n");
}
// ------------------------------------------------------------
void fillWindow (GtkWindow * window)
{
// ------- create layout ------------
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(vbox));
// --- add line edit ---
GtkWidget * lineedit = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(lineedit));
// --- add checkbox ---
GtkWidget * checkbutton = gtk_check_button_new_with_label("My Checkbox");
gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(checkbutton));
// --- setup checkbox shortcut ---
GtkAccelGroup * accel_group = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
gtk_widget_add_accelerator(checkbutton, "clicked", accel_group,
GDK_KEY_c, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
g_signal_connect(checkbutton, "clicked", G_CALLBACK(onCopyCheckbox), NULL);
// problem: this event fires, even if GtkEntry is focussed
// it then block the <kbd>Ctrl+C</kbd>-Event of GtkEntry
}
// ------------------------------------------------------------
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWindow * window;
{
window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (window, "Window title");
gtk_window_set_default_size (window, 200, 200);
g_signal_connect(window, "destroy", gtk_main_quit, NULL);
}
fillWindow(window);
gtk_widget_show_all ((GtkWidget*)window);
gtk_main();
return 0;
}
// ------------------------------------------------------------
Imagine you want to spawn the function below on Ctrl+C while the checkbox is focussed.
void
shortcutAction(GSimpleAction* a, GVariant * b, gpointer c){
printf("shortcutAction() called\n");
}
This is how you setup your checkbox
// --- add checkbox ---
GtkWidget * checkbox = gtk_check_button_new_with_label("My Checkbox");
gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(checkbox));
// ========= setup checkbox shortcut ==============
GtkApplication * app = GTK_APPLICATION(g_application_get_default());
// ------- Part 1: create the action itself -----------
// All the action you want to add go in one ActionGroup
GSimpleActionGroup * group = g_simple_action_group_new();
// define all action you want to have
// these action can be basically seen as some kind of signal
static const GActionEntry actions[] {
{ .name="print", .activate=shortcutAction}
};
// ActionGroup.add(actions[])
g_action_map_add_action_entries(G_ACTION_MAP(group), actions, G_N_ELEMENTS(actions), NULL);
// Widget.addActionGroup(ActionGroup) // connect widget and action group
// No you also create a name for the group. Here "checkbox" (but can be anything)
gtk_widget_insert_action_group (GTK_WIDGET(checkbox), "checkbox", G_ACTION_GROUP(group));
// ------- Part 2: create the shortcut to create the action --------
const gchar * const shortcuts[] = { "<Ctrl>C", NULL };
gtk_application_set_accels_for_action (app, "checkbox.print", shortcuts);
Remarks
As you see in the code, there's a strict difference between
defining actions
defining shortcuts
I'm trying to use gtk3 radiobutton following the turorial bellow. The problem is I noticed the callback funcion is being called twice everytime I click in the button.
My question is why and how can I change this for the callback to be called one time when I click in a radio button?
https://developer.gnome.org/gnome-devel-demos/stable/radiobutton.c.html.en
#include <gtk/gtk.h>
static void foo(GtkWidget *widget, gpointer data)
{
char *id_radio = (char*)data;
g_print("%s\n", id_radio);
}
int main(int argc, char *argv[])
{
gtk_init(&argc, & argv);
GtkWidget *radio_1, *radio_2, *radio_3, *vbox, *window;
radio_1 = gtk_radio_button_new_with_label(NULL, "Radio 01");
radio_2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_1), "Radio 02");
radio_3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_1), "Radio 03");
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(GTK_TOGGLE_BUTTON(radio_1), "toggled", G_CALLBACK(foo), (gpointer)"1");
g_signal_connect(GTK_TOGGLE_BUTTON(radio_2), "toggled", G_CALLBACK(foo), (gpointer)"2");
g_signal_connect(GTK_TOGGLE_BUTTON(radio_3), "toggled", G_CALLBACK(foo), (gpointer)"3");
gtk_box_pack_start(GTK_BOX(vbox), radio_1, 1, 1, 0);
gtk_box_pack_start(GTK_BOX(vbox), radio_2, 1, 1, 0);
gtk_box_pack_start(GTK_BOX(vbox), radio_3, 1, 1, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
The "toggled" signal which is inherited from a toggle button, triggers both on deactivation and activation. when you select a radio button, the previously selected button gets deactivated (the first callback), and the newly pressed one activates (the second callback).
If you want to filter out just the activations, get the toggle button's state inside the callback with gtk_toggle_button_get_active ().
As per documentation from:
https://developer.gnome.org/gtk3/stable/GtkToggleButton.html#gtk-toggle-button-get-active
On on GTK window there is a red close icon rendered in the title bar. Normally when you click on this, the window is closed and it's resources released.
Is there a way of intercepting the normal flow to prevent the window from being destroyed so that I can show it again later? i.e. I want to hide the window not close/destroy it.
This is what I have so far.
void destroy_window_callback(GtkWidget* widget, WebWindow_Linux* source)
{
printf("Don't destroy the window, just hide it.\n");
}
g_signal_connect(web_window, "destroy", G_CALLBACK(destroy_window_callback), this);
This is probably what you need
#include <gtk/gtk.h>
void
on_button_clicked(GtkButton *button, gpointer data)
{
GtkWidget *widget;
widget = (GtkWidget *) data;
if (widget == NULL)
return;
gtk_widget_show(widget);
return;
}
gboolean
on_widget_deleted(GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_widget_hide(widget);
return TRUE;
}
int
main(int argc, char **argv)
{
GtkWidget *window1;
GtkWidget *window2;
GtkWidget *button;
gtk_init(&argc, &argv);
window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label("Show again...");
g_signal_connect(G_OBJECT(window1),
"destroy", gtk_main_quit, NULL);
g_signal_connect(G_OBJECT(window2),
"delete-event", G_CALLBACK(on_widget_deleted), NULL);
g_signal_connect(G_OBJECT(button),
"clicked", G_CALLBACK(on_button_clicked), window2);
gtk_container_add(GTK_CONTAINER(window1), button);
gtk_widget_set_size_request(window1, 300, 100);
gtk_widget_set_size_request(window2, 300, 100);
gtk_widget_show_all(window1);
gtk_widget_show(window2);
gtk_main();
return 0;
}
We basically have three widgets, two top level windows and a button. The first window has it's "destroy" event connected to gtk_main_quit() quitting the application when the window's close button is pressed. The second window has it's "delete-event" connected to a custom function. This is the important one. As you see it returns TRUE indicating that the signal was handled and thus preventing to call the default handler and hence preventing the call to gtk_widget_destroy(). Also in it we can hide the widget if we want.
I want to print small warning messages. To achieve that, I'm creating a new window and I want it to be centered on the main window. So far I've tried setting its parent with gtk_widget_set_parent, using GTK_WINDOW_POPUP and GTK_WINDOW_TOPLEVEL but nothing worked.
How to center the window on the main window?
Here's the code for the function:
static void pop_warning(char *title, char *text)
{
GtkWidget *warning_window;
GtkWidget *box;
GtkWidget *label;
GtkWidget *button;
warning_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(warning_window), title);
g_signal_connect(GTK_WINDOW(warning_window), "destroy", G_CALLBACK(gtk_widget_destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(warning_window), 20);
gtk_window_set_resizable(GTK_WINDOW(warning_window), FALSE);
//window is the main window
gtk_widget_set_parent(warning_window, window);
gtk_window_set_position(GTK_WINDOW(warning_window), GTK_WIN_POS_CENTER_ON_PARENT);
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_add(GTK_CONTAINER(warning_window), box);
label = gtk_label_new(text);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, FALSE, 0);
button = gtk_button_new_with_label("Ok");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), warning_window);
gtk_box_pack_start(GTK_BOX(box), button, TRUE, FALSE, 0);
gtk_widget_show_all(warning_window);
}
gtk_window_set_position(GTK_WINDOW(warning_window), GTK_WIN_POS_CENTER_ON_PARENT);
only works if you first call
gtk_window_set_transient_for(GtkWindow *window, GtkWindow *parent);
as mentioned explicitly in the documentation.
You could also try to use GTK_WIN_POS_CENTER_ALWAYS that has not the above mentioned constrain - at least not according the docs.
When I have GtkTextView in GtkScrolledWindow, it scrolls automatically when user appends new line at the bottom of the widget. When I put GtkTextView in GtkBox and then in GtkScrolledWindow I doesn't work. I need to put box between GtkTextView and scrollbar and I can't do that without placing whole thing in another box. Is there any way to preserve autoscrolling behaviour when using GtkBox inside GtkScrolledWindow?
There is code that ilustrates my problem:
#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_default_size(GTK_WINDOW(window),
gdk_screen_width()*0.5, gdk_screen_height()*0.5);
GtkWidget *main = gtk_box_new(0, 0);
gtk_container_add(GTK_CONTAINER(window), main);
/*/////////////////////////////////////////////////*/
/* IMPORTANT PART */
/* FIRST CASE (this one works correctly) */
GtkWidget *scrolled_window_first = gtk_scrolled_window_new(NULL, NULL);
GtkWidget *text_view_first = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(scrolled_window_first), text_view_first);
gtk_box_pack_start(GTK_BOX(main), scrolled_window_first, 1, 1, 0);
/* SECOND CASE (there is no auto scroll which I need) */
GtkWidget *scrolled_window_second = gtk_scrolled_window_new(NULL, NULL);
GtkWidget *text_view_second = gtk_text_view_new();
GtkWidget *box_from_second_example = gtk_box_new(0, 0);
GtkWidget *example_box_before_scroller = gtk_box_new(0,0);
GtkWidget *example_label = gtk_label_new("I need this box badly!");
gtk_box_pack_start(GTK_BOX(box_from_second_example), text_view_second, 1, 1, 0);
gtk_container_add(GTK_CONTAINER(scrolled_window_second), box_from_second_example);
gtk_container_add(GTK_CONTAINER(box_from_second_example), example_box_before_scroller);
gtk_box_pack_start(GTK_BOX(main), scrolled_window_second, 1, 1, 0);
/* END IMPORTANT PART */
/*/////////////////////////////////////////////////*/
gtk_container_add(GTK_CONTAINER(example_box_before_scroller), example_label);
g_signal_connect(GTK_WINDOW(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
The problem is GtkTextView implements GtkScrollable but GtkBox does not. This is not a minor issue: if you want to go the GtkBox way you should put it inside a GtkViewport and add the scrollability stuff... quite some code that involves deep understanding of how the whole thing scrolls.
Anyway if you are lazy enough you could also note GtkTextview is a GtkContainer. In other words you could add widgets around the GtkTextview without the need to incomodate GtkBox. Not exactly what you were looking for but maybe good enough for your purposes:
#include <gtk/gtk.h>
gint main(gint argc, gchar **argv)
{
GtkWidget *window, *scrolled_window, *text_view, *label;
gtk_init(&argc, &argv);
text_view = gtk_text_view_new();
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)),
"1\n\n\n2\n\n\n3\n\n\n4\n\n\n5\n\n\n6\n\n\n7\n\n\n8", -1);
/* Use this to set the size you want to reserve on the right */
gtk_text_view_set_border_window_size(GTK_TEXT_VIEW(text_view),
GTK_TEXT_WINDOW_RIGHT,
130);
/* Add whatever you want instead of a GtkLabel */
gtk_text_view_add_child_in_window(GTK_TEXT_VIEW(text_view),
gtk_label_new("You badly need this"),
GTK_TEXT_WINDOW_RIGHT,
0, 0);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrolled_window), text_view);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 320, 240);
gtk_container_add(GTK_CONTAINER(window), scrolled_window);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
Did you try to modify the GtkAdjustments the textview creates by default (GtkScrolledWindow implements the GtkScrollable afaik).
GtkAdjustment * gtk_scrollable_get_hadjustment (GtkScrollable *scrollable);
I think the best approach would be to detect insertions to the GtkTextBuffer (get the views buffer via
GtkTextBuffer * gtk_text_view_get_buffer (GtkTextView *text_view);
and hook a callback (which tests for \n) to its insert-text g_signal and use
void gtk_adjustment_set_value (GtkAdjustment *adjustment,
gdouble value);
to modify the scrolling height.
Note that this is untested and a simpler method might be around the corner.