~ubuntu-branches/ubuntu/quantal/glom/quantal

« back to all changes in this revision

Viewing changes to glom/utility_widgets/canvas/canvas_table_movable.cc

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-01-11 17:12:01 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090111171201-0ov9zh1fxfueshxc
Tags: 1.8.5-0ubuntu1
* New upstream release (LP: #256701), fixes LP bugs: 
  + Clear the text in property dialogs for text items (LP: #309147)
  + Don't crash when showing and then hiding the grid (LP: #303453)
  + Temporarily remember the sort order so it is the same when navigating 
    away and back (LP: #303422)
  + Use the list's sort order for the top-level records in the report 
    (LP: #303425)
  + Users/Groups: Disable drag-and-drop for the treeview, because it is
    useless and confusing (LP: #299573)
  + Import: Sort the fields list alphabetically (LP: #306593)
  + delete primary key make unusuable the database (LP: #299549)
  + Spanish translate incomplete (LP: #299556)
  + import date format error (LP: #299591)
  + can't delete data from table list view (LP: #299853)
  + Field definition: default value not saved (LP: #299896)
  + reports crashing (LP: #300054)
  + Year error with date fields (LP: #300057)
  + list view: 2 records added instead of 1 (LP: #300819)
* debian/control: Refreshed dependencies for libglom-dev.
* debian/control: Updated build-deps to match configure checks. goocavnasmm
  build-dep bumped to version without .la files.
* debian/rules: Don't delete the directory containing the templates.
* debian/control, debian/rules, debian/glom-doc.*: Split out
  arch-independent manual and examples into separate package glom-doc, which
  is now recommended by glom (glom will still work if they're not available)
* debian/copyright: Rewritten to new machine-readable format. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Glom
 
2
 *
 
3
 * Copyright (C) 2007 Murray Cumming
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
#include "canvas_table_movable.h"
 
22
#include <goocanvasmm/canvas.h>
 
23
#include <gdkmm/cursor.h>
 
24
#include <iostream>
 
25
 
 
26
namespace Glom
 
27
{
 
28
 
 
29
 
 
30
CanvasTableMovable::CanvasTableMovable()
 
31
: m_x(0), m_y(0)
 
32
{
 
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));
 
36
 
 
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));
 
39
}
 
40
 
 
41
CanvasTableMovable::~CanvasTableMovable()
 
42
{
 
43
}
 
44
 
 
45
Glib::RefPtr<CanvasTableMovable> CanvasTableMovable::create()
 
46
{
 
47
  return Glib::RefPtr<CanvasTableMovable>(new CanvasTableMovable());
 
48
}
 
49
 
 
50
void CanvasTableMovable::get_xy(double& x, double& y) const
 
51
{
 
52
  x = m_x;
 
53
  y = m_y;
 
54
 
 
55
  // Or we could use the child at the top-left:
 
56
  /*
 
57
  Glib::RefPtr<const Goocanvas::Item> first_child = get_child(0);
 
58
  if(!first_child)
 
59
    return;
 
60
 
 
61
  Glib::RefPtr<const CanvasItemMovable> movable = CanvasItemMovable::cast_const_to_movable(first_child);
 
62
  if(movable)
 
63
     movable->get_xy(x, y);
 
64
  */
 
65
}
 
66
 
 
67
void CanvasTableMovable::set_xy(double x, double y)
 
68
{
 
69
  //Discover the offset:
 
70
  double old_x = 0;
 
71
  double old_y = 0;
 
72
  get_xy(old_x, old_y);
 
73
 
 
74
  const double offset_x = x - old_x;
 
75
  const double offset_y = y - old_y;
 
76
 
 
77
  //Apply the offset:
 
78
  translate(offset_x, offset_y);
 
79
 
 
80
  //Remember the position, because GooCanvasTable does not:
 
81
  m_x = x;
 
82
  m_y = y;
 
83
}
 
84
 
 
85
void CanvasTableMovable::get_width_height(double& width, double& height) const
 
86
{
 
87
  width = property_width();
 
88
  height = property_height();
 
89
 
 
90
  if(width == -1) //Means "default width" - presumably the width demanded by the children. But we don't use that.
 
91
    width = 0;
 
92
 
 
93
  if(height == -1) //Means "default height" - presumably the height demanded by the children. But we don't use that.
 
94
    height = 0;
 
95
}
 
96
 
 
97
void CanvasTableMovable::set_width_height(double width, double height)
 
98
{
 
99
  if(width == -1)
 
100
  {
 
101
    std::cout << "CanvasTableMovable::set_width_height(): width is -1" << std::endl;
 
102
  }
 
103
 
 
104
  property_width() = width;
 
105
  property_height() = height;
 
106
}
 
107
 
 
108
void CanvasTableMovable::set_grid(const Glib::RefPtr<const CanvasGroupGrid>& grid)
 
109
{
 
110
  //Call the base class:
 
111
  CanvasItemMovable::set_grid(grid);
 
112
 
 
113
  //Apply the grid to all children:
 
114
  const int count = get_n_children();
 
115
  for(int i = 0; i < count; ++i)
 
116
  {
 
117
    Glib::RefPtr<Goocanvas::Item> child = get_child(i);
 
118
    Glib::RefPtr<CanvasItemMovable> movable = CanvasItemMovable::cast_to_movable(child);
 
119
    if(movable)
 
120
    {
 
121
      movable->set_grid(grid);
 
122
    }
 
123
  }
 
124
}
 
125
 
 
126
void CanvasTableMovable::snap_position_one_corner(Corners corner, double& x, double& y) const
 
127
{
 
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());
 
131
 
 
132
  //Choose the offset of the part to snap to the grid:
 
133
  double corner_x_offset = 0;
 
134
  double corner_y_offset = 0;
 
135
  switch(corner)
 
136
  {
 
137
    case CORNER_TOP_LEFT:
 
138
      corner_x_offset = 0;
 
139
      corner_y_offset = 0;
 
140
      break;
 
141
    case CORNER_TOP_RIGHT:
 
142
      corner_x_offset = width;
 
143
      corner_y_offset = 0;
 
144
      break;
 
145
    case CORNER_BOTTOM_LEFT:
 
146
      corner_x_offset = 0;
 
147
      corner_y_offset = height;
 
148
      break;
 
149
    case CORNER_BOTTOM_RIGHT:
 
150
      corner_x_offset = width;
 
151
      corner_y_offset = height;
 
152
      break;
 
153
    default:
 
154
      break;
 
155
  }
 
156
 
 
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);
 
163
 
 
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;
 
167
 
 
168
  //Apply that offset to the regular position:
 
169
  x += snapped_offset_x;
 
170
  y += snapped_offset_y;
 
171
}
 
172
 
 
173
void CanvasTableMovable::snap_position(double& x, double& y) const
 
174
{
 
175
  //std::cout << "CanvasTableMovable::snap_position" << std::endl;
 
176
 
 
177
  double offset_x_min = 0;
 
178
  double offset_y_min = 0;
 
179
 
 
180
  //Try snapping each corner, to choose the one that snapped closest:
 
181
  for(int i = CORNER_TOP_LEFT; i < CORNER_COUNT; ++i)
 
182
  {
 
183
    const Corners corner = (Corners)i;
 
184
    double temp_x = x;
 
185
    double temp_y = y;
 
186
    snap_position_one_corner(corner, temp_x, temp_y);
 
187
 
 
188
    const double offset_x = temp_x -x;
 
189
    const double offset_y = temp_y - y;
 
190
 
 
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;
 
194
 
 
195
    if(offset_y && ((std::abs(offset_y) < std::abs(offset_y_min)) || !offset_y_min))
 
196
      offset_y_min = offset_y;
 
197
  }
 
198
 
 
199
  x += offset_x_min;
 
200
  y += offset_y_min;
 
201
}
 
202
 
 
203
Goocanvas::Canvas* CanvasTableMovable::get_parent_canvas_widget()
 
204
{
 
205
  return get_canvas();
 
206
}
 
207
 
 
208
 
 
209
} //namespace Glom
 
210