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

132 lines
3.5 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 13:34:59 +08:00
// Add 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_begin().connect(sigc::mem_fun(*this,&MyWin::drag_begin));
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 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-27 23:06:00 +08:00
double value = scale.get_value();
2021-12-28 13:34:59 +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 13:34:59 +08:00
void MyWin::drag_begin(double x, double y)
{
// g_print("drag begins\n");
// move_to(x,y);
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;
//Before movement, make sure the value is vaild
if (h_value < 0)
{
h_value = 0;
}
if (h_value > hmaxvalue)
{
h_value = hmaxvalue;
}
//Before movement, make sure the value is vaild
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
}