Drang and Drop window in Gtk+

Drang and Drop window in Gtk+

In the next example, we show an interesting feature. We will show a borderless window and learn, how we can drag and move such a window.

#include 
gboolean on_button_press (GtkWidget* widget,
GdkEventButton * event, GdkWindowEdge edge)
{
if (event->type == GDK_BUTTON_PRESS)
{
if (event->button == 1) {
gtk_window_begin_move_drag(GTK_WINDOW(gtk_widget_get_toplevel(widget)),
event->button,
event->x_root,
event->y_root,
event->time);
}
}
return FALSE;
}
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), "Drag & drop");
gtk_window_set_decorated(GTK_WINDOW (window), FALSE);
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
g_signal_connect(G_OBJECT(window), "button-press-event",
G_CALLBACK(on_button_press), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_widget_show(window);
gtk_main();
return 0;
}
The example demonstrates a drag and drop of a borderless window.

gtk_window_set_decorated(GTK_WINDOW (window), FALSE);

We remove the decoration of the window. This means, that the window will not have borders and titlebar.

g_signal_connect(G_OBJECT(window), "button-press-event",
G_CALLBACK(on_button_press), NULL);

We connect the window to the button-press-event signal.

gboolean on_button_press (GtkWidget* widget,
GdkEventButton * event, GdkWindowEdge edge)
{
if (event->type == GDK_BUTTON_PRESS)
{
if (event->button == 1) {
gtk_window_begin_move_drag(GTK_WINDOW(gtk_widget_get_toplevel(widget)),
event->button,
event->x_root,
event->y_root,
event->time);
}
}

return FALSE;
}

Inside the on_button_press(), we do perform the drag and drop operation.
We check if the left mouse button was pressed. Then we call the gtk_window_begin_move_drag() function.



No comments:

 
Top Blogs