testing-repository/Gtkmm3/gtk119_imageviewer2/src/MyWin.cc

174 lines
4.7 KiB
C++
Raw Normal View History

2021-12-05 18:39:33 +08:00
#include "MyWin.hh"
2021-12-24 23:28:46 +08:00
MyWin::MyWin()
2021-12-28 13:34:59 +08:00
: main_box(Gtk::ORIENTATION_VERTICAL, 5),
btnbox(Gtk::ORIENTATION_HORIZONTAL, 5),
btnopen("Open Image")
2021-12-24 23:28:46 +08:00
{
2021-12-28 13:34:59 +08:00
// Add Widgets
set_default_size(800, 450);
2021-12-24 23:28:46 +08:00
set_icon_name("org.gtk.daleclack");
2021-12-28 13:34:59 +08:00
// Scrolled Window
sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
2021-12-24 23:28:46 +08:00
sw.add(image_area);
main_box.pack_start(sw);
2021-12-28 13:34:59 +08:00
// Initalize Scale
m_adjustment = Gtk::Adjustment::create(1.0, 0.1, 10.0, 0.1, 0.1);
2021-12-26 00:05:54 +08:00
scale.set_default_direction(Gtk::TEXT_DIR_LTR);
scale.set_adjustment(m_adjustment);
2021-12-28 13:34:59 +08:00
scale.signal_value_changed().connect(sigc::mem_fun(*this, &MyWin::scale_changed));
2021-12-26 00:05:54 +08:00
2021-12-28 13:34:59 +08:00
// Add control widgets
2021-12-26 00:05:54 +08:00
btnbox.pack_start(scale);
2021-12-28 13:34:59 +08:00
btnbox.pack_start(btnopen, Gtk::PACK_SHRINK);
main_box.pack_start(btnbox, Gtk::PACK_SHRINK);
btnopen.signal_clicked().connect(sigc::mem_fun(*this, &MyWin::btnopen_clicked));
2021-12-24 23:28:46 +08:00
2021-12-28 16:38:56 +08:00
// Add Drag Gesture
2021-12-28 13:16:20 +08:00
gesture_drag = Gtk::GestureDrag::create(image_area);
gesture_drag->set_button(GDK_BUTTON_PRIMARY);
2021-12-28 13:34:59 +08:00
gesture_drag->signal_drag_update().connect(sigc::mem_fun(*this, &MyWin::drag_update));
gesture_drag->signal_drag_end().connect(sigc::mem_fun(*this, &MyWin::drag_end));
2021-12-28 13:16:20 +08:00
2021-12-28 16:38:56 +08:00
gesture_click = Gtk::GestureMultiPress::create(image_area);
gesture_click->set_button(GDK_BUTTON_SECONDARY);
2021-12-28 16:40:53 +08:00
gesture_click->signal_pressed().connect(sigc::mem_fun(*this, &MyWin::press));
2021-12-28 16:38:56 +08:00
// Add Menu
auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/appmenu.xml");
auto object = builder->get_object("model");
auto gmenu = Glib::RefPtr<Gio::MenuModel>::cast_dynamic(object);
popover.bind_model(gmenu);
popover.set_relative_to(image_area);
// Add actions for menu
add_action("zoom_in", sigc::mem_fun(*this, &MyWin::image_zoom_in));
add_action("zoom_out", sigc::mem_fun(*this, &MyWin::image_zoom_out));
add_action("zoom_reset", sigc::mem_fun(*this, &MyWin::image_zoom_reset));
2021-12-28 13:34:59 +08:00
overlay.add(main_box);
add(overlay);
2021-12-24 23:28:46 +08:00
show_all_children();
2021-12-26 00:05:54 +08:00
}
2021-12-28 13:34:59 +08:00
void MyWin::btnopen_clicked()
{
// Create a file open dialog
dialog = Gtk::FileChooserNative::create("Open a image File", *this, Gtk::FILE_CHOOSER_ACTION_OPEN,
"OK", "Cancel");
dialog->signal_response().connect(sigc::mem_fun(*this, &MyWin::dialog_response));
2021-12-26 00:05:54 +08:00
2021-12-26 13:31:00 +08:00
dialog->show();
2021-12-26 00:05:54 +08:00
}
2021-12-28 13:34:59 +08:00
void MyWin::dialog_response(int response_id)
{
if (response_id == Gtk::RESPONSE_ACCEPT)
{
// Show the image in a drawing area
2021-12-26 13:31:00 +08:00
auto filename = dialog->get_filename();
auto pixbuf = Gdk::Pixbuf::create_from_file(filename);
image_area.set_pixbuf(pixbuf);
pixbuf.reset();
2021-12-28 13:34:59 +08:00
// Set Adjustments
if (hadjustment)
{
2021-12-28 13:16:20 +08:00
hadjustment.reset();
vadjustment.reset();
}
hadjustment = sw.get_hadjustment();
vadjustment = sw.get_vadjustment();
2021-12-26 13:31:00 +08:00
}
dialog.reset();
2021-12-27 23:06:00 +08:00
}
2021-12-28 13:34:59 +08:00
void MyWin::scale_changed()
{
2021-12-28 16:38:56 +08:00
// Get Value of scale widget and scale image
2021-12-27 23:06:00 +08:00
double value = scale.get_value();
2021-12-28 16:38:56 +08:00
// g_print("%f\n", value);
2021-12-27 23:06:00 +08:00
image_area.scale_draw(value);
}
2021-12-28 13:16:20 +08:00
2021-12-28 16:40:53 +08:00
void MyWin::press(int n_press, double x, double y)
{
2021-12-28 16:38:56 +08:00
// Set Popover to the position of mouse and show
2021-12-28 16:40:53 +08:00
popover.set_pointing_to(Gdk::Rectangle(x, y, 1, 1));
2021-12-28 16:38:56 +08:00
popover.popup();
2021-12-28 13:16:20 +08:00
}
2021-12-28 13:34:59 +08:00
void MyWin::drag_update(double x, double y)
{
// g_print("drag updated\n");
move_to(x, y);
2021-12-28 13:16:20 +08:00
}
2021-12-28 13:34:59 +08:00
void MyWin::drag_end(double x, double y)
{
// g_print("drag ended\n");
move(x, y);
2021-12-28 13:16:20 +08:00
}
2021-12-28 13:34:59 +08:00
void MyWin::move_to(double x, double y)
{
// g_print("%f %f\n",x,y);
// Get Current Value
double h_value = hadjustment->get_value();
double v_value = vadjustment->get_value();
// Get Max Value for movement
double hmaxvalue = hadjustment->get_upper(); //- hadjustment->get_page_size();
double vmaxvalue = hadjustment->get_upper(); //- vadjustment->get_page_size();
h_value -= x;
v_value -= y;
2021-12-28 16:40:53 +08:00
// Before movement, make sure the value is vaild
2021-12-28 13:34:59 +08:00
if (h_value < 0)
{
h_value = 0;
}
if (h_value > hmaxvalue)
{
h_value = hmaxvalue;
}
2021-12-28 16:40:53 +08:00
// Before movement, make sure the value is vaild
2021-12-28 13:34:59 +08:00
if (v_value < 0)
{
v_value = 0;
}
if (v_value > vmaxvalue)
{
v_value = vmaxvalue;
}
// Perform movement
hadjustment->set_value(h_value);
vadjustment->set_value(v_value);
2021-12-28 13:16:20 +08:00
}
2021-12-28 16:38:56 +08:00
2021-12-28 16:40:53 +08:00
void MyWin::image_zoom_in()
{
2021-12-28 16:38:56 +08:00
// Scale 0.1 More
double value = scale.get_value();
2021-12-28 16:40:53 +08:00
value += 0.1;
2021-12-28 16:38:56 +08:00
scale.set_value(value);
image_area.scale_draw(value);
}
2021-12-28 16:40:53 +08:00
void MyWin::image_zoom_out()
{
2021-12-28 16:38:56 +08:00
// Scale 0.1 Less
double value = scale.get_value();
2021-12-28 16:40:53 +08:00
value -= 0.1;
2021-12-28 16:38:56 +08:00
scale.set_value(value);
image_area.scale_draw(value);
}
2021-12-28 16:40:53 +08:00
void MyWin::image_zoom_reset()
{
2021-12-28 16:38:56 +08:00
// Scale as 1:1
scale.set_value(1.0);
image_area.scale_draw(1.0);
}