How we react to move events of a window in gtk+

The below example shows how we react to move events of a window in gtk+

#include 
void frame_callback(GtkWindow *window,
GdkEvent *event, gpointer data)
{
int x, y;
char buf[10];
x = event->configure.x;
y = event->configure.y;
sprintf(buf, "%d, %d", x, y);
gtk_window_set_title(window, buf);
}
int main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
gtk_window_set_title(GTK_WINDOW(window), "Simple");
gtk_widget_add_events(GTK_WIDGET(window), GDK_CONFIGURE);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect(G_OBJECT(window), "configure-event",
G_CALLBACK(frame_callback), NULL);
gtk_widget_show(window);
gtk_main();
return 0;
}
In the example, we show the current position of the upper left corner of our window in the titlebar.

gtk_widget_add_events(GTK_WIDGET(window), GDK_CONFIGURE);

The event mask of the widget determines, what kind of event will a
particular widget receive. Some event are preconfigured, other events
have to be added to the event mask. The gtk_widget_add_events() adds a
GDK_CONFIGURE event type to the mask. The GDK_CONFIGURE event type accounts for all size, position and stack order events.
 g_signal_connect(G_OBJECT(window), "configure-event",
G_CALLBACK(frame_callback), NULL);



The configure-event is emitted to size, position and stack order events.

void frame_callback(GtkWindow *window,
GdkEvent *event, gpointer data)
{
int x, y;
char buf[10];
x = event->configure.x;
y = event->configure.y;
sprintf(buf, "%d, %d", x, y);
gtk_window_set_title(window, buf);
}

The callback function has three parameters. The object that emitted the signal,
GdkEvent and the optional data. We determine the x, y positions and set it to the
title.


Figure: Move event



No comments:

 
Top Blogs