GnuCash  5.6-150-g038405b370+
gnucash-cursor.c
1 /********************************************************************\
2  * This program is free software; you can redistribute it and/or *
3  * modify it under the terms of the GNU General Public License as *
4  * published by the Free Software Foundation; either version 2 of *
5  * the License, or (at your option) any later version. *
6  * *
7  * This program is distributed in the hope that it will be useful, *
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
10  * GNU General Public License for more details. *
11  * *
12  * You should have received a copy of the GNU General Public License*
13  * along with this program; if not, contact: *
14  * *
15  * Free Software Foundation Voice: +1-617-542-5942 *
16  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
17  * Boston, MA 02110-1301, USA gnu@gnu.org *
18  * *
19 \********************************************************************/
20 
21 /*
22  * The Gnucash Cursor Canvas Item
23  *
24  * Based heavily (i.e., cut and pasted from) on the Gnumeric ItemCursor.
25  *
26  * Authors:
27  * Heath Martin <martinh@pegasus.cc.ucf.edu>
28  * Dave Peticolas <dave@krondo.com>
29  */
30 
31 #include <config.h>
32 
33 #include "gnucash-color.h"
34 #include "gnucash-cursor.h"
35 #include "gnucash-sheet.h"
36 #include "gnucash-sheetP.h"
37 #include "gnucash-style.h"
38 
39 enum
40 {
41  PROP_0,
42  PROP_SHEET,
43 };
44 
45 
46 G_DEFINE_TYPE (GnucashCursor, gnucash_cursor, G_TYPE_OBJECT)
47 
48 
49 static void
50 gnucash_cursor_get_pixel_coords (GnucashCursor *cursor,
51  gint *x, gint *y,
52  gint *w, gint *h)
53 {
54  GnucashSheet *sheet = cursor->sheet;
55  VirtualCellLocation vcell_loc;
56  CellDimensions *cd;
57  VirtualCell *vcell;
58  SheetBlock *block;
59  gint col;
60 
61  vcell_loc.virt_row = cursor->row;
62  vcell_loc.virt_col = cursor->col;
63 
64  block = gnucash_sheet_get_block (sheet, vcell_loc);
65  if (!block)
66  return;
67 
68  vcell = gnc_table_get_virtual_cell (sheet->table, vcell_loc);
69  if (!vcell)
70  return;
71 
72  for (col = 0; col < vcell->cellblock->num_cols; col++)
73  {
74  BasicCell *cell;
75 
76  cell = gnc_cellblock_get_cell (vcell->cellblock, 0, col);
77  if (cell && cell->cell_name)
78  break;
79  }
80 
81  *y = block->origin_y;
82 
83  cd = gnucash_style_get_cell_dimensions (block->style, 0, col);
84  if (cd)
85  *x = cd->origin_x;
86  else
87  *x = block->origin_x;
88 
89  for (col = vcell->cellblock->num_cols - 1; col >= 0; col--)
90  {
91  BasicCell *cell;
92 
93  cell = gnc_cellblock_get_cell (vcell->cellblock, 0, col);
94  if (cell && cell->cell_name)
95  break;
96  }
97 
98  *h = block->style->dimensions->height;
99 
100  cd = gnucash_style_get_cell_dimensions (block->style, 0, col);
101  if (cd)
102  *w = cd->origin_x + cd->pixel_width - *x;
103  else
104  *w = block->style->dimensions->width - *x;
105 }
106 
107 
108 void
109 gnucash_cursor_set_style (GnucashCursor *cursor, SheetBlockStyle *style)
110 {
111  g_return_if_fail (cursor != NULL);
112  g_return_if_fail (GNUCASH_IS_CURSOR(cursor));
113 
114  cursor->style = style;
115 }
116 
117 
118 void
119 gnucash_cursor_get_virt (GnucashCursor *cursor, VirtualLocation *virt_loc)
120 {
121  g_return_if_fail (cursor != NULL);
122  g_return_if_fail (GNUCASH_IS_CURSOR (cursor));
123 
124  virt_loc->vcell_loc.virt_row = cursor->row;
125  virt_loc->vcell_loc.virt_col = cursor->col;
126 
127  virt_loc->phys_row_offset = cursor->cell.row;
128  virt_loc->phys_col_offset = cursor->cell.col;
129 }
130 
131 
132 void
133 gnucash_cursor_configure (GnucashCursor *cursor)
134 {
135  gint x = 0, y = 0, w = 0, h = 0;
136 
137  g_return_if_fail (cursor != NULL);
138  g_return_if_fail (GNUCASH_IS_CURSOR (cursor));
139 
140  if (!cursor->sheet)
141  return;
142 
143  g_return_if_fail (GTK_IS_LAYOUT (cursor->sheet));
144 
145  gnucash_cursor_get_pixel_coords (cursor, &x, &y, &w, &h);
146  cursor->x = x;
147  cursor->y = y;
148  cursor->w = w;
149  cursor->h = h + 2;
150 
151  gnucash_sheet_style_get_cell_pixel_rel_coords (cursor->style,
152  cursor->cell.row, cursor->cell.col,
153  &x, &y, &w, &h);
154  cursor->cell.x = x;
155  cursor->cell.y = y;
156  cursor->cell.w = w;
157  cursor->cell.h = h;
158 }
159 
160 static void
161 gnucash_cursor_set_block (GnucashCursor *cursor, VirtualCellLocation vcell_loc)
162 {
163  GnucashSheet *sheet;
164 
165  g_return_if_fail (cursor != NULL);
166  g_return_if_fail (GNUCASH_IS_CURSOR (cursor));
167 
168  sheet = cursor->sheet;
169 
170  if (vcell_loc.virt_row < 0 ||
171  vcell_loc.virt_row >= sheet->num_virt_rows ||
172  vcell_loc.virt_col < 0 ||
173  vcell_loc.virt_col >= sheet->num_virt_cols)
174  return;
175 
176  cursor->style = gnucash_sheet_get_style (sheet, vcell_loc);
177  cursor->row = vcell_loc.virt_row;
178  cursor->col = vcell_loc.virt_col;
179 }
180 
181 
182 static void
183 gnucash_cursor_set_cell (GnucashCursor *cursor, gint cell_row, gint cell_col)
184 {
185  SheetBlockStyle *style;
186 
187  g_return_if_fail (cursor != NULL);
188  g_return_if_fail (GNUCASH_IS_CURSOR (cursor));
189 
190  style = cursor->style;
191 
192  if (cell_row < 0 || cell_row >= style->nrows ||
193  cell_col < 0 || cell_col >= style->ncols)
194  return;
195 
196  cursor->cell.row = cell_row;
197  cursor->cell.col = cell_col;
198 }
199 
200 
201 void
202 gnucash_cursor_set (GnucashCursor *cursor, VirtualLocation virt_loc)
203 {
204  GnucashSheet *sheet;
205 
206  g_return_if_fail (cursor != NULL);
207  g_return_if_fail (GNUCASH_IS_CURSOR (cursor));
208 
209  sheet = cursor->sheet;
210 
211  gnucash_cursor_set_block (cursor, virt_loc.vcell_loc);
212  gnucash_cursor_set_cell (cursor,
213  virt_loc.phys_row_offset,
214  virt_loc.phys_col_offset);
215 
216  gnucash_cursor_configure (cursor);
217 
218  g_object_set (G_OBJECT(sheet->header_item),
219  "cursor_name",
220  cursor->style->cursor->cursor_name,
221  NULL);
222 }
223 
224 
225 static void
226 gnucash_cursor_set_property (GObject *object,
227  guint prop_id,
228  const GValue *value,
229  GParamSpec *pspec)
230 {
231  GnucashCursor *cursor;
232 
233  cursor = GNUCASH_CURSOR (object);
234 
235  switch (prop_id)
236  {
237  case PROP_SHEET:
238  cursor->sheet =
239  GNUCASH_SHEET (g_value_get_object (value));
240  break;
241  default:
242  break;
243  }
244 }
245 
246 
247 /* Note that g_value_set_object() refs the object, as does
248  * g_object_get(). But g_object_get() only unrefs once when it disgorges
249  * the object, leaving an unbalanced ref, which leaks. So instead of
250  * using g_value_set_object(), use g_value_take_object() which doesn't
251  * ref the object when used in get_property().
252  */
253 static void
254 gnucash_cursor_get_property (GObject *object,
255  guint prop_id,
256  GValue *value,
257  GParamSpec *pspec)
258 {
259  GnucashCursor *cursor = GNUCASH_CURSOR (object);
260 
261  switch (prop_id)
262  {
263  case PROP_SHEET:
264  g_value_take_object (value, cursor->sheet);
265  break;
266  default:
267  break;
268  }
269 }
270 
271 
272 static void
273 gnucash_cursor_init (GnucashCursor *instance)
274 {
275 }
276 
277 
278 static void
279 gnucash_cursor_class_init (GnucashCursorClass *klass)
280 {
281  GObjectClass *object_class;
282 
283  object_class = G_OBJECT_CLASS (klass);
284 
285  /* GObject method overrides */
286  object_class->set_property = gnucash_cursor_set_property;
287  object_class->get_property = gnucash_cursor_get_property;
288 
289  /* properties */
290  g_object_class_install_property
291  (object_class,
292  PROP_SHEET,
293  g_param_spec_object ("sheet",
294  "Sheet Value",
295  "Sheet Value",
296  GNUCASH_TYPE_SHEET,
297  G_PARAM_READWRITE));
298 }
299 
300 
302 gnucash_cursor_new (GnucashSheet *sheet)
303 {
304  return GNUCASH_CURSOR(
305  g_object_new (gnucash_cursor_get_type(),
306  "sheet", sheet,
307  NULL));
308 }
309 
310 
holds information about each virtual cell.
Definition: table-allgui.h:132
Convenience wrapper around GdkRGBA for use in Register Gnome classes.
Public declarations for GnucashCursor class.
VirtualCell * gnc_table_get_virtual_cell(Table *table, VirtualCellLocation vcell_loc)
returns the virtual cell associated with a particular virtual location.
Definition: table-allgui.c:227
Public declarations of GnucashRegister class.
Private declarations for GnucashSheet class.
SheetBlockStyle * style
The style for this block.
Definition: gnucash-sheet.h:54
Styling functions for RegisterGnome.
gint origin_y
x origin of block
Definition: gnucash-sheet.h:57
BasicCell * gnc_cellblock_get_cell(CellBlock *cellblock, int row, int col)
Retrieve the Cell at the specified coordinates.
Definition: cellblock.c:109