3
* Copyright (C) 2007 Murray Cumming
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public
16
* License along with this program; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
* Boston, MA 02111-1307, USA.
21
#include "canvas_table_movable.h"
22
#include <goocanvasmm/canvas.h>
23
#include <gdkmm/cursor.h>
30
CanvasTableMovable::CanvasTableMovable()
33
signal_motion_notify_event().connect(sigc::mem_fun(*this, &CanvasItemMovable::on_motion_notify_event));
34
signal_button_press_event().connect(sigc::mem_fun(*this, &CanvasItemMovable::on_button_press_event));
35
signal_button_release_event().connect(sigc::mem_fun(*this, &CanvasItemMovable::on_button_release_event));
37
signal_enter_notify_event().connect(sigc::mem_fun(*this, &CanvasItemMovable::on_enter_notify_event));
38
signal_leave_notify_event().connect(sigc::mem_fun(*this, &CanvasItemMovable::on_leave_notify_event));
41
CanvasTableMovable::~CanvasTableMovable()
45
Glib::RefPtr<CanvasTableMovable> CanvasTableMovable::create()
47
return Glib::RefPtr<CanvasTableMovable>(new CanvasTableMovable());
50
void CanvasTableMovable::get_xy(double& x, double& y) const
55
// Or we could use the child at the top-left:
57
Glib::RefPtr<const Goocanvas::Item> first_child = get_child(0);
61
Glib::RefPtr<const CanvasItemMovable> movable = CanvasItemMovable::cast_const_to_movable(first_child);
63
movable->get_xy(x, y);
67
void CanvasTableMovable::set_xy(double x, double y)
69
//Discover the offset:
74
const double offset_x = x - old_x;
75
const double offset_y = y - old_y;
78
translate(offset_x, offset_y);
80
//Remember the position, because GooCanvasTable does not:
85
void CanvasTableMovable::get_width_height(double& width, double& height) const
87
width = property_width();
88
height = property_height();
90
if(width == -1) //Means "default width" - presumably the width demanded by the children. But we don't use that.
93
if(height == -1) //Means "default height" - presumably the height demanded by the children. But we don't use that.
97
void CanvasTableMovable::set_width_height(double width, double height)
101
std::cout << "CanvasTableMovable::set_width_height(): width is -1" << std::endl;
104
property_width() = width;
105
property_height() = height;
108
void CanvasTableMovable::set_grid(const Glib::RefPtr<const CanvasGroupGrid>& grid)
110
//Call the base class:
111
CanvasItemMovable::set_grid(grid);
113
//Apply the grid to all children:
114
const int count = get_n_children();
115
for(int i = 0; i < count; ++i)
117
Glib::RefPtr<Goocanvas::Item> child = get_child(i);
118
Glib::RefPtr<CanvasItemMovable> movable = CanvasItemMovable::cast_to_movable(child);
121
movable->set_grid(grid);
126
void CanvasTableMovable::snap_position_one_corner(Corners corner, double& x, double& y) const
128
Goocanvas::Bounds bounds = get_bounds();
129
const double width = std::abs(bounds.get_x2() - bounds.get_x1());
130
const double height = std::abs(bounds.get_y2() - bounds.get_y1());
132
//Choose the offset of the part to snap to the grid:
133
double corner_x_offset = 0;
134
double corner_y_offset = 0;
137
case CORNER_TOP_LEFT:
141
case CORNER_TOP_RIGHT:
142
corner_x_offset = width;
145
case CORNER_BOTTOM_LEFT:
147
corner_y_offset = height;
149
case CORNER_BOTTOM_RIGHT:
150
corner_x_offset = width;
151
corner_y_offset = height;
157
//Snap that point to the grid:
158
const double x_to_snap = x + corner_x_offset;
159
const double y_to_snap = y + corner_y_offset;
160
double corner_x_snapped = x_to_snap;
161
double corner_y_snapped = y_to_snap;
162
CanvasItemMovable::snap_position(corner_x_snapped, corner_y_snapped);
164
//Discover what offset the snapping causes:
165
const double snapped_offset_x = corner_x_snapped - x_to_snap;
166
const double snapped_offset_y = corner_y_snapped - y_to_snap;
168
//Apply that offset to the regular position:
169
x += snapped_offset_x;
170
y += snapped_offset_y;
173
void CanvasTableMovable::snap_position(double& x, double& y) const
175
//std::cout << "CanvasTableMovable::snap_position" << std::endl;
177
double offset_x_min = 0;
178
double offset_y_min = 0;
180
//Try snapping each corner, to choose the one that snapped closest:
181
for(int i = CORNER_TOP_LEFT; i < CORNER_COUNT; ++i)
183
const Corners corner = (Corners)i;
186
snap_position_one_corner(corner, temp_x, temp_y);
188
const double offset_x = temp_x -x;
189
const double offset_y = temp_y - y;
191
//Use the smallest offset, preferring some offset to no offset:
192
if(offset_x && ((std::abs(offset_x) < std::abs(offset_x_min)) || !offset_x_min))
193
offset_x_min = offset_x;
195
if(offset_y && ((std::abs(offset_y) < std::abs(offset_y_min)) || !offset_y_min))
196
offset_y_min = offset_y;
203
Goocanvas::Canvas* CanvasTableMovable::get_parent_canvas_widget()