Updated Spanish translation
[evolution.git] / e-util / e-table-model.c
blob7f59db9c31922357c8f2d024e28c6c6dfbe137cf
1 /*
2 * e-table-model.c
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "e-table-model.h"
20 #define d(x)
22 d (static gint depth = 0;)
24 G_DEFINE_INTERFACE (ETableModel, e_table_model, G_TYPE_OBJECT)
26 enum {
27 MODEL_NO_CHANGE,
28 MODEL_CHANGED,
29 MODEL_PRE_CHANGE,
30 MODEL_ROW_CHANGED,
31 MODEL_CELL_CHANGED,
32 MODEL_ROWS_INSERTED,
33 MODEL_ROWS_DELETED,
34 ROW_SELECTION,
35 LAST_SIGNAL
38 static guint signals[LAST_SIGNAL] = { 0, };
40 static gint
41 table_model_is_frozen (ETableModel *table_model)
43 gpointer data;
45 data = g_object_get_data (G_OBJECT (table_model), "frozen");
47 return (GPOINTER_TO_INT (data) != 0);
50 static void
51 e_table_model_default_init (ETableModelInterface *iface)
53 signals[MODEL_NO_CHANGE] = g_signal_new (
54 "model_no_change",
55 G_TYPE_FROM_INTERFACE (iface),
56 G_SIGNAL_RUN_LAST,
57 G_STRUCT_OFFSET (ETableModelInterface, model_no_change),
58 NULL, NULL, NULL,
59 G_TYPE_NONE, 0);
61 signals[MODEL_CHANGED] = g_signal_new (
62 "model_changed",
63 G_TYPE_FROM_INTERFACE (iface),
64 G_SIGNAL_RUN_LAST,
65 G_STRUCT_OFFSET (ETableModelInterface, model_changed),
66 NULL, NULL, NULL,
67 G_TYPE_NONE, 0);
69 signals[MODEL_PRE_CHANGE] = g_signal_new (
70 "model_pre_change",
71 G_TYPE_FROM_INTERFACE (iface),
72 G_SIGNAL_RUN_LAST,
73 G_STRUCT_OFFSET (ETableModelInterface, model_pre_change),
74 NULL, NULL, NULL,
75 G_TYPE_NONE, 0);
77 signals[MODEL_ROW_CHANGED] = g_signal_new (
78 "model_row_changed",
79 G_TYPE_FROM_INTERFACE (iface),
80 G_SIGNAL_RUN_LAST,
81 G_STRUCT_OFFSET (ETableModelInterface, model_row_changed),
82 NULL, NULL, NULL,
83 G_TYPE_NONE, 1,
84 G_TYPE_INT);
86 signals[MODEL_CELL_CHANGED] = g_signal_new (
87 "model_cell_changed",
88 G_TYPE_FROM_INTERFACE (iface),
89 G_SIGNAL_RUN_LAST,
90 G_STRUCT_OFFSET (ETableModelInterface, model_cell_changed),
91 NULL, NULL, NULL,
92 G_TYPE_NONE, 2,
93 G_TYPE_INT,
94 G_TYPE_INT);
96 signals[MODEL_ROWS_INSERTED] = g_signal_new (
97 "model_rows_inserted",
98 G_TYPE_FROM_INTERFACE (iface),
99 G_SIGNAL_RUN_LAST,
100 G_STRUCT_OFFSET (ETableModelInterface, model_rows_inserted),
101 NULL, NULL, NULL,
102 G_TYPE_NONE, 2,
103 G_TYPE_INT,
104 G_TYPE_INT);
106 signals[MODEL_ROWS_DELETED] = g_signal_new (
107 "model_rows_deleted",
108 G_TYPE_FROM_INTERFACE (iface),
109 G_SIGNAL_RUN_LAST,
110 G_STRUCT_OFFSET (ETableModelInterface, model_rows_deleted),
111 NULL, NULL, NULL,
112 G_TYPE_NONE, 2,
113 G_TYPE_INT,
114 G_TYPE_INT);
118 * e_table_model_column_count:
119 * @table_model: The e-table-model to operate on
121 * Returns: the number of columns in the table model.
123 gint
124 e_table_model_column_count (ETableModel *table_model)
126 ETableModelInterface *iface;
128 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), 0);
130 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
131 g_return_val_if_fail (iface->column_count != NULL, 0);
133 return iface->column_count (table_model);
137 * e_table_model_row_count:
138 * @table_model: the e-table-model to operate on
140 * Returns: the number of rows in the Table model.
142 gint
143 e_table_model_row_count (ETableModel *table_model)
145 ETableModelInterface *iface;
147 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), 0);
149 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
150 g_return_val_if_fail (iface->row_count != NULL, 0);
152 return iface->row_count (table_model);
156 * e_table_model_append_row:
157 * @table_model: the table model to append the a row to.
158 * @source:
159 * @row:
162 void
163 e_table_model_append_row (ETableModel *table_model,
164 ETableModel *source,
165 gint row)
167 ETableModelInterface *iface;
169 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
171 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
173 if (iface->append_row != NULL)
174 iface->append_row (table_model, source, row);
178 * e_table_value_at:
179 * @table_model: the e-table-model to operate on
180 * @col: column in the model to pull data from.
181 * @row: row in the model to pull data from.
183 * Return value: This function returns the value that is stored
184 * by the @table_model in column @col and row @row. The data
185 * returned can be a pointer or any data value that can be stored
186 * inside a pointer.
188 * The data returned is typically used by an ECell renderer.
190 * The data returned must be valid until the model sends a signal that
191 * affect that piece of data. model_changed affects all data.
192 * row_changed affects the data in that row. cell_changed affects the
193 * data in that cell. rows_deleted affects all data in those rows.
194 * rows_inserted and no_change don't affect any data in this way.
196 gpointer
197 e_table_model_value_at (ETableModel *table_model,
198 gint col,
199 gint row)
201 ETableModelInterface *iface;
203 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
205 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
206 g_return_val_if_fail (iface->value_at != NULL, NULL);
208 return iface->value_at (table_model, col, row);
212 * e_table_model_set_value_at:
213 * @table_model: the table model to operate on.
214 * @col: the column where the data will be stored in the model.
215 * @row: the row where the data will be stored in the model.
216 * @value: the data to be stored.
218 * This function instructs the model to store the value in @data in the
219 * the @table_model at column @col and row @row. The @data typically
220 * comes from one of the ECell rendering objects.
222 * There should be an agreement between the Table Model and the user
223 * of this function about the data being stored. Typically it will
224 * be a pointer to a set of data, or a datum that fits inside a gpointer .
226 void
227 e_table_model_set_value_at (ETableModel *table_model,
228 gint col,
229 gint row,
230 gconstpointer value)
232 ETableModelInterface *iface;
234 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
236 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
237 g_return_if_fail (iface->set_value_at != NULL);
239 iface->set_value_at (table_model, col, row, value);
243 * e_table_model_is_cell_editable:
244 * @table_model: the table model to query.
245 * @col: column to query.
246 * @row: row to query.
248 * Returns: %TRUE if the cell in @table_model at @col,@row can be
249 * edited, %FALSE otherwise
251 gboolean
252 e_table_model_is_cell_editable (ETableModel *table_model,
253 gint col,
254 gint row)
256 ETableModelInterface *iface;
258 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
260 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
261 g_return_val_if_fail (iface->is_cell_editable != NULL, FALSE);
263 return iface->is_cell_editable (table_model, col, row);
266 gpointer
267 e_table_model_duplicate_value (ETableModel *table_model,
268 gint col,
269 gconstpointer value)
271 ETableModelInterface *iface;
273 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
275 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
277 if (iface->duplicate_value == NULL)
278 return NULL;
280 return iface->duplicate_value (table_model, col, value);
283 void
284 e_table_model_free_value (ETableModel *table_model,
285 gint col,
286 gpointer value)
288 ETableModelInterface *iface;
290 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
292 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
294 if (iface->free_value != NULL)
295 iface->free_value (table_model, col, value);
298 gboolean
299 e_table_model_has_save_id (ETableModel *table_model)
301 ETableModelInterface *iface;
303 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
305 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
307 if (iface->has_save_id == NULL)
308 return FALSE;
310 return iface->has_save_id (table_model);
313 gchar *
314 e_table_model_get_save_id (ETableModel *table_model,
315 gint row)
317 ETableModelInterface *iface;
319 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
321 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
323 if (iface->get_save_id == NULL)
324 return NULL;
326 return iface->get_save_id (table_model, row);
329 gboolean
330 e_table_model_has_change_pending (ETableModel *table_model)
332 ETableModelInterface *iface;
334 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
336 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
338 if (iface->has_change_pending == NULL)
339 return FALSE;
341 return iface->has_change_pending (table_model);
344 gpointer
345 e_table_model_initialize_value (ETableModel *table_model,
346 gint col)
348 ETableModelInterface *iface;
350 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
352 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
354 if (iface->initialize_value == NULL)
355 return NULL;
357 return iface->initialize_value (table_model, col);
360 gboolean
361 e_table_model_value_is_empty (ETableModel *table_model,
362 gint col,
363 gconstpointer value)
365 ETableModelInterface *iface;
367 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
369 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
371 if (iface->value_is_empty == NULL)
372 return FALSE;
374 return iface->value_is_empty (table_model, col, value);
377 gchar *
378 e_table_model_value_to_string (ETableModel *table_model,
379 gint col,
380 gconstpointer value)
382 ETableModelInterface *iface;
384 g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
386 iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
388 if (iface->value_to_string == NULL)
389 return g_strdup ("");
391 return iface->value_to_string (table_model, col, value);
394 #if d(!)0
395 static void
396 print_tabs (void)
398 gint i;
399 for (i = 0; i < depth; i++)
400 g_print ("\t");
402 #endif
404 void
405 e_table_model_pre_change (ETableModel *table_model)
407 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
409 if (table_model_is_frozen (table_model))
410 return;
412 d (print_tabs ());
413 d (depth++);
414 g_signal_emit (table_model, signals[MODEL_PRE_CHANGE], 0);
415 d (depth--);
419 * e_table_model_no_change:
420 * @table_model: the table model to notify of the lack of a change
422 * Use this function to notify any views of this table model that
423 * the contents of the table model have changed. This will emit
424 * the signal "model_no_change" on the @table_model object.
426 * It is preferable to use the e_table_model_row_changed() and
427 * the e_table_model_cell_changed() to notify of smaller changes
428 * than to invalidate the entire model, as the views might have
429 * ways of caching the information they render from the model.
431 void
432 e_table_model_no_change (ETableModel *table_model)
434 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
436 if (table_model_is_frozen (table_model))
437 return;
439 d (print_tabs ());
440 d (depth++);
441 g_signal_emit (table_model, signals[MODEL_NO_CHANGE], 0);
442 d (depth--);
446 * e_table_model_changed:
447 * @table_model: the table model to notify of the change
449 * Use this function to notify any views of this table model that
450 * the contents of the table model have changed. This will emit
451 * the signal "model_changed" on the @table_model object.
453 * It is preferable to use the e_table_model_row_changed() and
454 * the e_table_model_cell_changed() to notify of smaller changes
455 * than to invalidate the entire model, as the views might have
456 * ways of caching the information they render from the model.
458 void
459 e_table_model_changed (ETableModel *table_model)
461 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
463 if (table_model_is_frozen (table_model))
464 return;
466 d (print_tabs ());
467 d (depth++);
468 g_signal_emit (table_model, signals[MODEL_CHANGED], 0);
469 d (depth--);
473 * e_table_model_row_changed:
474 * @table_model: the table model to notify of the change
475 * @row: the row that was changed in the model.
477 * Use this function to notify any views of the table model that
478 * the contents of row @row have changed in model. This function
479 * will emit the "model_row_changed" signal on the @table_model
480 * object
482 void
483 e_table_model_row_changed (ETableModel *table_model,
484 gint row)
486 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
488 if (table_model_is_frozen (table_model))
489 return;
491 d (print_tabs ());
492 d (depth++);
493 g_signal_emit (table_model, signals[MODEL_ROW_CHANGED], 0, row);
494 d (depth--);
498 * e_table_model_cell_changed:
499 * @table_model: the table model to notify of the change
500 * @col: the column.
501 * @row: the row
503 * Use this function to notify any views of the table model that
504 * contents of the cell at @col,@row has changed. This will emit
505 * the "model_cell_changed" signal on the @table_model
506 * object
508 void
509 e_table_model_cell_changed (ETableModel *table_model,
510 gint col,
511 gint row)
513 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
515 if (table_model_is_frozen (table_model))
516 return;
518 d (print_tabs ());
519 d (depth++);
520 g_signal_emit (
521 table_model, signals[MODEL_CELL_CHANGED], 0, col, row);
522 d (depth--);
526 * e_table_model_rows_inserted:
527 * @table_model: the table model to notify of the change
528 * @row: the row that was inserted into the model.
529 * @count: The number of rows that were inserted.
531 * Use this function to notify any views of the table model that
532 * @count rows at row @row have been inserted into the model. This
533 * function will emit the "model_rows_inserted" signal on the
534 * @table_model object
536 void
537 e_table_model_rows_inserted (ETableModel *table_model,
538 gint row,
539 gint count)
541 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
543 if (table_model_is_frozen (table_model))
544 return;
546 d (print_tabs ());
547 d (depth++);
548 g_signal_emit (
549 table_model, signals[MODEL_ROWS_INSERTED], 0, row, count);
550 d (depth--);
554 * e_table_model_row_inserted:
555 * @table_model: the table model to notify of the change
556 * @row: the row that was inserted into the model.
558 * Use this function to notify any views of the table model that the
559 * row @row has been inserted into the model. This function will emit
560 * the "model_rows_inserted" signal on the @table_model object
562 void
563 e_table_model_row_inserted (ETableModel *table_model,
564 gint row)
566 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
568 e_table_model_rows_inserted (table_model, row, 1);
572 * e_table_model_row_deleted:
573 * @table_model: the table model to notify of the change
574 * @row: the row that was deleted
575 * @count: The number of rows deleted
577 * Use this function to notify any views of the table model that
578 * @count rows at row @row have been deleted from the model. This
579 * function will emit the "model_rows_deleted" signal on the
580 * @table_model object
582 void
583 e_table_model_rows_deleted (ETableModel *table_model,
584 gint row,
585 gint count)
587 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
589 if (table_model_is_frozen (table_model))
590 return;
592 d (print_tabs ());
593 d (depth++);
594 g_signal_emit (
595 table_model, signals[MODEL_ROWS_DELETED], 0, row, count);
596 d (depth--);
600 * e_table_model_row_deleted:
601 * @table_model: the table model to notify of the change
602 * @row: the row that was deleted
604 * Use this function to notify any views of the table model that the
605 * row @row has been deleted from the model. This function will emit
606 * the "model_rows_deleted" signal on the @table_model object
608 void
609 e_table_model_row_deleted (ETableModel *table_model,
610 gint row)
612 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
614 e_table_model_rows_deleted (table_model, row, 1);
617 void
618 e_table_model_freeze (ETableModel *table_model)
620 gpointer data;
622 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
624 e_table_model_pre_change (table_model);
626 data = g_object_get_data (G_OBJECT (table_model), "frozen");
627 data = GINT_TO_POINTER (GPOINTER_TO_INT (data) + 1);
628 g_object_set_data (G_OBJECT (table_model), "frozen", data);
631 void
632 e_table_model_thaw (ETableModel *table_model)
634 gpointer data;
636 g_return_if_fail (E_IS_TABLE_MODEL (table_model));
638 data = g_object_get_data (G_OBJECT (table_model), "frozen");
639 data = GINT_TO_POINTER (GPOINTER_TO_INT (data) - 1);
640 g_object_set_data (G_OBJECT (table_model), "frozen", data);
642 e_table_model_changed (table_model);