2

I have a program that really needs to know when the user has released the mouse button after resizing the window. I asked to get button-release signals for the window, and I get them when the mouse is clicked INSIDE the window, but NOT when I resize the window and release the mouse button. Below is a short program that demonstrates this. Can anyone tell me what I need to do to get these events?

People have suggested "just use configure signals to do that", but the last configure signal occurs when the mouse stops moving. The user might hold down the mouse button long after that.

C code: '''

#include <stdlib.h>
#include <gtk/gtk.h>

GtkWidget   *window;
GtkBuilder  *builder; 

int
main(int argc, char *argv[]) {

    gtk_init(0, NULL); // init Gtk

    builder = gtk_builder_new_from_file ("test.glade");
    window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_builder_connect_signals(builder, NULL);
    gtk_widget_show(window);
    gtk_main();

    return 0;
}

gboolean
on_window_button_release_event(GtkWidget *w, GdkEvent *e, gpointer p)
{
    printf("button release\n");
    return FALSE;
}

'''

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.4 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <signal name="button-release-event" handler="on_window_button_release_event" swapped="no"/>
    <child>
      <object class="GtkFixed" id="fixed1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkLabel">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Label</property>
          </object>
          <packing>
            <property name="x">113</property>
            <property name="y">93</property>
          </packing>
        </child>
      </object>
    </child>
    <child type="titlebar">
      <placeholder/>
    </child>
  </object>
</interface>

'''

tgphelps
  • 47
  • 1
  • 4

1 Answers1

1

Since click on the boundary of the window and top bar is OUTSIDE the window, you will get a focus-out-event signal sent when you click. More importantly, when you release the mouse button, it sends a focus-in-event as the window becomes active again. So you could try using gboolean on_focus_in (GtkWidget* w, GdkEventFocus* ef, gpointer p) and g_signal_connect to focus-in-event to detect mouse click releases originating from clicks outside the window. Issue seems to be ignoring other focus in signal such as opening window, presses that aren't on the boundary, which could potentially be done with gdk_device_get_position (this answer might get you on the right path), etc...

But here is the general idea that might be worth trying to hack it:

#include <stdlib.h>
#include <gtk/gtk.h>

GtkWidget   *window;

gboolean on_focus_in (GtkWidget* w, GdkEventFocus* ef, gpointer p) {
  printf("release\n");
  return TRUE;
}
gboolean on_focus_out (GtkWidget* w, GdkEventFocus* ef, gpointer p) {
    printf("press\n");
    return TRUE;
}

int
main(int argc, char *argv[]) {

    gtk_init(0, NULL); // init Gtk

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect (window,
              "focus-in-event",
              G_CALLBACK (on_focus_in),
              NULL);
    g_signal_connect (window,
              "focus-out-event",
              G_CALLBACK (on_focus_out),
              NULL);
    gtk_widget_show(window);
    gtk_main();

    return 0;
}

resize event

jackw11111
  • 1,457
  • 1
  • 17
  • 34
  • I'm not seeing the signals you said I should see. I added a focus-out signal to your code and tested: With the window in focus, I clicked the boundary, dragged it, released it. No signals at all. With the window not in focus, I clicked the boundary (focus in), dragged it, released it (no signals). In short, I never got ANY signal on a mouse button release. Am I missing something? – tgphelps Aug 03 '20 at 12:54
  • @tgphelps The above code example didn't print "release" at all? No output on focus-out either? Let me think...What is your gtk version? – jackw11111 Aug 03 '20 at 12:57
  • It printed 'release' when I clicked the boundary with the window not in focus. Clicking the boundary with the window in focus did NOT give a 'focus out', like you thought it would. – tgphelps Aug 03 '20 at 13:03
  • @tgphelps Hmmm unusual... I have updated the code and gif, is that what your system produces / is that what you are expecting to happen? – jackw11111 Aug 03 '20 at 13:18
  • 1
    I ran your code with no changes. This happens: (1) startup ('release'), (2) move window (nothing), (3) resize window (nothing), (4) click outside window ('press'), (5) click and hold window boundary ('release'), (6) drag window edge and release (nothing). Apparently, you're seeing very different results. I'm stumped. I never see a signal on button-release, and see one on button-press only when (a) click outside window border with focus-in,, and (b) click on window border or inside with focus-out. – tgphelps Aug 03 '20 at 13:34
  • Unfortunately it sounds like there are bugs somewhere on your system, perhaps the gtk installation has deprecated code, as the behaviour in the gif is AFAIK the expected behaviour. If you let me know your gtk version, and the command you are using to compile, we can try and iron out the issues. – jackw11111 Aug 03 '20 at 13:45
  • You should return "something" in those callback function. Any way those signals are there for "keyboard focus" tought. – Michi Aug 06 '20 at 15:37
  • @Michi Thanks, edited. Yes, I couldn't find much else in the way of getting toolbar focus events. Doesn't look like it will work consistently enough though... – jackw11111 Aug 06 '20 at 22:53