2

I am trying to move a Gtk::Button in Gtk::Fixed like a Gtk::Window would on a window manager.I have Gtk::GestureDrag around the button. So far I can move the Button around.

If I move the Button to a new position and click on the button there, the button moves back to its original position.

Here is a minified code

main.cpp

#include <gtkmm.h>
#include <iostream>

class Content: public Gtk::Fixed
{
public:
    Content();
    long int x, y;
protected:
    Glib::RefPtr<Gtk::GestureDrag> drag;
    Gtk::Button button{"Button"};
    void begin_drag(double nx, double ny);
    void update_drag(double nx, double ny);
    void end_drag(double nx, double ny);

};
Content::Content()
{
    add(button);
    drag = Gtk::GestureDrag::create(button);
    drag->set_button(GDK_BUTTON_PRIMARY);
    drag->signal_drag_begin().connect(
        sigc::mem_fun(*this, &Content::begin_drag));
    drag->signal_drag_update().connect(
        sigc::mem_fun(*this, &Content::update_drag));
    drag->signal_drag_end().connect(
        sigc::mem_fun(*this, &Content::end_drag));
}
void Content::begin_drag(double nx, double ny) {}
void Content::update_drag(double nx, double ny)
{
    x = nx;
    y = ny;
    move(button, x, y);
}
void Content::end_drag(double nx, double ny)
{
    x = nx;
    y = ny;
    move(button, x, y);
}

int main(int argc, char *argv[])
{
    auto app =
        Gtk::Application::create(argc, argv,
                             "org.gtkmm.moving.widgets");

    Gtk::Window window;
    Content fixed;
    window.set_default_size(480, 320);
    window.add(fixed);
    window.show_all_children();
    app->run(window);
    return 0;
}

How do I make the button stay on the new position when I click on it without dragging? `

Devab LLC
  • 115
  • 1
  • 8

0 Answers0