No issues found
Tool | Failure ID | Location | Function | Message | Data |
---|---|---|---|---|---|
clang-analyzer | no-output-found | e-selection-model-array.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None | |
clang-analyzer | no-output-found | e-selection-model-array.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None |
1 /*
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with the program; if not, see <http://www.gnu.org/licenses/>
15 *
16 *
17 * Authors:
18 * Chris Lahey <clahey@ximian.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <gtk/gtk.h>
29
30 #include <glib/gi18n.h>
31 #include "e-util/e-util.h"
32
33 #include "e-selection-model-array.h"
34
35 G_DEFINE_TYPE (
36 ESelectionModelArray,
37 e_selection_model_array,
38 E_TYPE_SELECTION_MODEL)
39
40 enum {
41 PROP_0,
42 PROP_CURSOR_ROW,
43 PROP_CURSOR_COL
44 };
45
46 void
47 e_selection_model_array_confirm_row_count (ESelectionModelArray *esma)
48 {
49 if (esma->eba == NULL) {
50 gint row_count = e_selection_model_array_get_row_count (esma);
51 esma->eba = e_bit_array_new (row_count);
52 esma->selected_row = -1;
53 esma->selected_range_end = -1;
54 }
55 }
56
57 static gint
58 es_row_model_to_sorted (ESelectionModelArray *esma,
59 gint model_row)
60 {
61 if (model_row >= 0 && esma && esma->base.sorter && e_sorter_needs_sorting (esma->base.sorter))
62 return e_sorter_model_to_sorted (esma->base.sorter, model_row);
63
64 return model_row;
65 }
66
67 static gint
68 es_row_sorted_to_model (ESelectionModelArray *esma,
69 gint sorted_row)
70 {
71 if (sorted_row >= 0 && esma && esma->base.sorter && e_sorter_needs_sorting (esma->base.sorter))
72 return e_sorter_sorted_to_model (esma->base.sorter, sorted_row);
73
74 return sorted_row;
75 }
76
77 /* FIXME: Should this deal with moving the selection if it's in single mode? */
78 void
79 e_selection_model_array_delete_rows (ESelectionModelArray *esma,
80 gint row,
81 gint count)
82 {
83 if (esma->eba) {
84 if (E_SELECTION_MODEL (esma)->mode == GTK_SELECTION_SINGLE)
85 e_bit_array_delete_single_mode (esma->eba, row, count);
86 else
87 e_bit_array_delete (esma->eba, row, count);
88
89 if (esma->cursor_row >= row && esma->cursor_row < row + count) {
90 /* we should move the cursor_row, because some lines before us are going to be removed */
91 if (esma->cursor_row_sorted >= e_bit_array_bit_count (esma->eba)) {
92 esma->cursor_row_sorted = e_bit_array_bit_count (esma->eba) - 1;
93 }
94
95 if (esma->cursor_row_sorted >= 0) {
96 esma->cursor_row = es_row_sorted_to_model (esma, esma->cursor_row_sorted);
97 esma->selection_start_row = 0;
98 e_bit_array_change_one_row (esma->eba, esma->cursor_row, TRUE);
99 } else {
100 esma->cursor_row = -1;
101 esma->cursor_row_sorted = -1;
102 esma->selection_start_row = 0;
103 }
104 } else {
105 /* some code earlier changed the selected row, so just update the sorted one */
106 if (esma->cursor_row >= row)
107 esma->cursor_row = MAX (0, esma->cursor_row - count);
108
109 if (esma->cursor_row >= e_bit_array_bit_count (esma->eba))
110 esma->cursor_row = e_bit_array_bit_count (esma->eba) - 1;
111
112 if (esma->cursor_row >= 0) {
113 esma->cursor_row_sorted = es_row_model_to_sorted (esma, esma->cursor_row);
114 esma->selection_start_row = 0;
115 e_bit_array_change_one_row (esma->eba, esma->cursor_row, TRUE);
116 } else {
117 esma->cursor_row = -1;
118 esma->cursor_row_sorted = -1;
119 esma->selection_start_row = 0;
120 }
121 }
122
123 esma->selected_row = -1;
124 esma->selected_range_end = -1;
125 e_selection_model_selection_changed (E_SELECTION_MODEL (esma));
126 e_selection_model_cursor_changed (E_SELECTION_MODEL (esma), esma->cursor_row, esma->cursor_col);
127 }
128 }
129
130 void
131 e_selection_model_array_insert_rows (ESelectionModelArray *esma,
132 gint row,
133 gint count)
134 {
135 if (esma->eba) {
136 e_bit_array_insert (esma->eba, row, count);
137
138 /* just recalculate new position of the previously set cursor row */
139 esma->cursor_row = es_row_sorted_to_model (esma, esma->cursor_row_sorted);
140
141 esma->selected_row = -1;
142 esma->selected_range_end = -1;
143 e_selection_model_selection_changed (E_SELECTION_MODEL (esma));
144 e_selection_model_cursor_changed (E_SELECTION_MODEL (esma), esma->cursor_row, esma->cursor_col);
145 }
146 }
147
148 void
149 e_selection_model_array_move_row (ESelectionModelArray *esma,
150 gint old_row,
151 gint new_row)
152 {
153 ESelectionModel *esm = E_SELECTION_MODEL (esma);
154
155 if (esma->eba) {
156 gboolean selected = e_bit_array_value_at (esma->eba, old_row);
157 gboolean cursor = (esma->cursor_row == old_row);
158 gint old_row_sorted, new_row_sorted;
159
160 old_row_sorted = es_row_model_to_sorted (esma, old_row);
161 new_row_sorted = es_row_model_to_sorted (esma, new_row);
162
163 if (old_row_sorted < esma->cursor_row_sorted && esma->cursor_row_sorted < new_row_sorted)
164 esma->cursor_row_sorted--;
165 else if (new_row_sorted < esma->cursor_row_sorted && esma->cursor_row_sorted < old_row_sorted)
166 esma->cursor_row_sorted++;
167
168 e_bit_array_move_row (esma->eba, old_row, new_row);
169
170 if (selected) {
171 if (esm->mode == GTK_SELECTION_SINGLE)
172 e_bit_array_select_single_row (esma->eba, new_row);
173 else
174 e_bit_array_change_one_row (esma->eba, new_row, TRUE);
175 }
176 if (cursor) {
177 esma->cursor_row = new_row;
178 esma->cursor_row_sorted = es_row_model_to_sorted (esma, esma->cursor_row);
179 } else
180 esma->cursor_row = es_row_sorted_to_model (esma, esma->cursor_row_sorted);
181
182 esma->selected_row = -1;
183 esma->selected_range_end = -1;
184 e_selection_model_selection_changed (esm);
185 e_selection_model_cursor_changed (esm, esma->cursor_row, esma->cursor_col);
186 }
187 }
188
189 static void
190 esma_dispose (GObject *object)
191 {
192 ESelectionModelArray *esma;
193
194 esma = E_SELECTION_MODEL_ARRAY (object);
195
196 if (esma->eba) {
197 g_object_unref (esma->eba);
198 esma->eba = NULL;
199 }
200
201 /* Chain up to parent's dispose() method. */
202 G_OBJECT_CLASS (e_selection_model_array_parent_class)->dispose (object);
203 }
204
205 static void
206 esma_get_property (GObject *object,
207 guint property_id,
208 GValue *value,
209 GParamSpec *pspec)
210 {
211 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (object);
212
213 switch (property_id) {
214 case PROP_CURSOR_ROW:
215 g_value_set_int (value, esma->cursor_row);
216 break;
217
218 case PROP_CURSOR_COL:
219 g_value_set_int (value, esma->cursor_col);
220 break;
221 }
222 }
223
224 static void
225 esma_set_property (GObject *object,
226 guint property_id,
227 const GValue *value,
228 GParamSpec *pspec)
229 {
230 ESelectionModel *esm = E_SELECTION_MODEL (object);
231 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (object);
232
233 switch (property_id) {
234 case PROP_CURSOR_ROW:
235 e_selection_model_do_something (esm, g_value_get_int (value), esma->cursor_col, 0);
236 break;
237
238 case PROP_CURSOR_COL:
239 e_selection_model_do_something (esm, esma->cursor_row, g_value_get_int (value), 0);
240 break;
241 }
242 }
243
244 /**
245 * e_selection_model_is_row_selected
246 * @selection: #ESelectionModel to check
247 * @n: The row to check
248 *
249 * This routine calculates whether the given row is selected.
250 *
251 * Returns: %TRUE if the given row is selected
252 */
253 static gboolean
254 esma_is_row_selected (ESelectionModel *selection,
255 gint n)
256 {
257 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
258 if (esma->eba)
259 return e_bit_array_value_at (esma->eba, n);
260 else
261 return FALSE;
262 }
263
264 /**
265 * e_selection_model_foreach
266 * @selection: #ESelectionModel to traverse
267 * @callback: The callback function to call back.
268 * @closure: The closure
269 *
270 * This routine calls the given callback function once for each
271 * selected row, passing closure as the closure.
272 */
273 static void
274 esma_foreach (ESelectionModel *selection,
275 EForeachFunc callback,
276 gpointer closure)
277 {
278 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
279 if (esma->eba)
280 e_bit_array_foreach (esma->eba, callback, closure);
281 }
282
283 /**
284 * e_selection_model_clear
285 * @selection: #ESelectionModel to clear
286 *
287 * This routine clears the selection to no rows selected.
288 */
289 static void
290 esma_clear (ESelectionModel *selection)
291 {
292 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
293 if (esma->eba) {
294 g_object_unref (esma->eba);
295 esma->eba = NULL;
296 }
297 esma->cursor_row = -1;
298 esma->cursor_col = -1;
299 esma->cursor_row_sorted = -1;
300 esma->selected_row = -1;
301 esma->selected_range_end = -1;
302 e_selection_model_selection_changed (E_SELECTION_MODEL (esma));
303 e_selection_model_cursor_changed (E_SELECTION_MODEL (esma), -1, -1);
304 }
305
306 #define PART(x,n) (((x) & (0x01010101 << n)) >> n)
307 #define SECTION(x, n) (((x) >> (n * 8)) & 0xff)
308
309 /**
310 * e_selection_model_selected_count
311 * @selection: #ESelectionModel to count
312 *
313 * This routine calculates the number of rows selected.
314 *
315 * Returns: The number of rows selected in the given model.
316 */
317 static gint
318 esma_selected_count (ESelectionModel *selection)
319 {
320 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
321 if (esma->eba)
322 return e_bit_array_selected_count (esma->eba);
323 else
324 return 0;
325 }
326
327 /**
328 * e_selection_model_select_all
329 * @selection: #ESelectionModel to select all
330 *
331 * This routine selects all the rows in the given
332 * #ESelectionModel.
333 */
334 static void
335 esma_select_all (ESelectionModel *selection)
336 {
337 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
338
339 e_selection_model_array_confirm_row_count (esma);
340
341 e_bit_array_select_all (esma->eba);
342
343 esma->cursor_col = 0;
344 esma->cursor_row_sorted = 0;
345 esma->cursor_row = es_row_sorted_to_model (esma, esma->cursor_row_sorted);
346 esma->selection_start_row = esma->cursor_row;
347 esma->selected_row = -1;
348 esma->selected_range_end = -1;
349 e_selection_model_selection_changed (E_SELECTION_MODEL (esma));
350 e_selection_model_cursor_changed (E_SELECTION_MODEL (esma), 0, 0);
351 }
352
353 /**
354 * e_selection_model_invert_selection
355 * @selection: #ESelectionModel to invert
356 *
357 * This routine inverts all the rows in the given
358 * #ESelectionModel.
359 */
360 static void
361 esma_invert_selection (ESelectionModel *selection)
362 {
363 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
364
365 e_selection_model_array_confirm_row_count (esma);
366
367 e_bit_array_invert_selection (esma->eba);
368
369 esma->cursor_col = -1;
370 esma->cursor_row = -1;
371 esma->cursor_row_sorted = -1;
372 esma->selection_start_row = 0;
373 esma->selected_row = -1;
374 esma->selected_range_end = -1;
375 e_selection_model_selection_changed (E_SELECTION_MODEL (esma));
376 e_selection_model_cursor_changed (E_SELECTION_MODEL (esma), -1, -1);
377 }
378
379 static gint
380 esma_row_count (ESelectionModel *selection)
381 {
382 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
383 e_selection_model_array_confirm_row_count (esma);
384 return e_bit_array_bit_count (esma->eba);
385 }
386
387 static void
388 esma_change_one_row (ESelectionModel *selection,
389 gint row,
390 gboolean grow)
391 {
392 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
393 e_selection_model_array_confirm_row_count (esma);
394 e_bit_array_change_one_row (esma->eba, row, grow);
395 }
396
397 static void
398 esma_change_cursor (ESelectionModel *selection,
399 gint row,
400 gint col)
401 {
402 ESelectionModelArray *esma;
403
404 g_return_if_fail (selection != NULL);
405 g_return_if_fail (E_IS_SELECTION_MODEL (selection));
406
407 esma = E_SELECTION_MODEL_ARRAY (selection);
408
409 esma->cursor_row = row;
410 esma->cursor_col = col;
411 esma->cursor_row_sorted = es_row_model_to_sorted (esma, esma->cursor_row);
412 }
413
414 static void
415 esma_change_range (ESelectionModel *selection,
416 gint start,
417 gint end,
418 gboolean grow)
419 {
420 gint i;
421 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
422 if (start != end) {
423 if (selection->sorter && e_sorter_needs_sorting (selection->sorter)) {
424 for (i = start; i < end; i++) {
425 e_bit_array_change_one_row (esma->eba, e_sorter_sorted_to_model (selection->sorter, i), grow);
426 }
427 } else {
428 e_selection_model_array_confirm_row_count (esma);
429 e_bit_array_change_range (esma->eba, start, end, grow);
430 }
431 }
432 }
433
434 static gint
435 esma_cursor_row (ESelectionModel *selection)
436 {
437 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
438 return esma->cursor_row;
439 }
440
441 static gint
442 esma_cursor_col (ESelectionModel *selection)
443 {
444 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
445 return esma->cursor_col;
446 }
447
448 static void
449 esma_real_select_single_row (ESelectionModel *selection,
450 gint row)
451 {
452 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
453
454 e_selection_model_array_confirm_row_count (esma);
455
456 e_bit_array_select_single_row (esma->eba, row);
457
458 esma->selection_start_row = row;
459 esma->selected_row = row;
460 esma->selected_range_end = row;
461 }
462
463 static void
464 esma_select_single_row (ESelectionModel *selection,
465 gint row)
466 {
467 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
468 gint selected_row = esma->selected_row;
469 esma_real_select_single_row (selection, row);
470
471 if (selected_row != -1 && esma->eba && selected_row < e_bit_array_bit_count (esma->eba)) {
472 if (selected_row != row) {
473 e_selection_model_selection_row_changed (selection, selected_row);
474 e_selection_model_selection_row_changed (selection, row);
475 }
476 } else {
477 e_selection_model_selection_changed (selection);
478 }
479 }
480
481 static void
482 esma_toggle_single_row (ESelectionModel *selection,
483 gint row)
484 {
485 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
486
487 e_selection_model_array_confirm_row_count (esma);
488 e_bit_array_toggle_single_row (esma->eba, row);
489
490 esma->selection_start_row = row;
491 esma->selected_row = -1;
492 esma->selected_range_end = -1;
493 e_selection_model_selection_row_changed (E_SELECTION_MODEL (esma), row);
494 }
495
496 static void
497 esma_real_move_selection_end (ESelectionModel *selection,
498 gint row)
499 {
500 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
501 gint old_start;
502 gint old_end;
503 gint new_start;
504 gint new_end;
505 if (selection->sorter && e_sorter_needs_sorting (selection->sorter)) {
506 old_start = MIN (
507 e_sorter_model_to_sorted (selection->sorter, esma->selection_start_row),
508 e_sorter_model_to_sorted (selection->sorter, esma->cursor_row));
509 old_end = MAX (
510 e_sorter_model_to_sorted (selection->sorter, esma->selection_start_row),
511 e_sorter_model_to_sorted (selection->sorter, esma->cursor_row)) + 1;
512 new_start = MIN (
513 e_sorter_model_to_sorted (selection->sorter, esma->selection_start_row),
514 e_sorter_model_to_sorted (selection->sorter, row));
515 new_end = MAX (
516 e_sorter_model_to_sorted (selection->sorter, esma->selection_start_row),
517 e_sorter_model_to_sorted (selection->sorter, row)) + 1;
518 } else {
519 old_start = MIN (esma->selection_start_row, esma->cursor_row);
520 old_end = MAX (esma->selection_start_row, esma->cursor_row) + 1;
521 new_start = MIN (esma->selection_start_row, row);
522 new_end = MAX (esma->selection_start_row, row) + 1;
523 }
524 /* This wouldn't work nearly so smoothly if one end of the selection weren't held in place. */
525 if (old_start < new_start)
526 esma_change_range (selection, old_start, new_start, FALSE);
527 if (new_start < old_start)
528 esma_change_range (selection, new_start, old_start, TRUE);
529 if (old_end < new_end)
530 esma_change_range (selection, old_end, new_end, TRUE);
531 if (new_end < old_end)
532 esma_change_range (selection, new_end, old_end, FALSE);
533 esma->selected_row = -1;
534 esma->selected_range_end = -1;
535 }
536
537 static void
538 esma_move_selection_end (ESelectionModel *selection,
539 gint row)
540 {
541 esma_real_move_selection_end (selection, row);
542 e_selection_model_selection_changed (selection);
543 }
544
545 static void
546 esma_set_selection_end (ESelectionModel *selection,
547 gint row)
548 {
549 ESelectionModelArray *esma = E_SELECTION_MODEL_ARRAY (selection);
550 gint selected_range_end = esma->selected_range_end;
551 gint view_row = e_sorter_model_to_sorted (selection->sorter, row);
552
553 esma_real_select_single_row (selection, esma->selection_start_row);
554 esma->cursor_row = esma->selection_start_row;
555 esma->cursor_row_sorted = es_row_model_to_sorted (esma, esma->cursor_row);
556 esma_real_move_selection_end (selection, row);
557
558 esma->selected_range_end = view_row;
559 if (selected_range_end != -1 && view_row != -1) {
560 if (selected_range_end == view_row - 1 ||
561 selected_range_end == view_row + 1) {
562 e_selection_model_selection_row_changed (selection, selected_range_end);
563 e_selection_model_selection_row_changed (selection, view_row);
564 }
565 }
566 e_selection_model_selection_changed (selection);
567 }
568
569 gint
570 e_selection_model_array_get_row_count (ESelectionModelArray *esma)
571 {
572 g_return_val_if_fail (esma != NULL, 0);
573 g_return_val_if_fail (E_IS_SELECTION_MODEL_ARRAY (esma), 0);
574
575 if (E_SELECTION_MODEL_ARRAY_GET_CLASS (esma)->get_row_count)
576 return E_SELECTION_MODEL_ARRAY_GET_CLASS (esma)->get_row_count (esma);
577 else
578 return 0;
579 }
580
581 static void
582 e_selection_model_array_init (ESelectionModelArray *esma)
583 {
584 esma->eba = NULL;
585 esma->selection_start_row = 0;
586 esma->cursor_row = -1;
587 esma->cursor_col = -1;
588 esma->cursor_row_sorted = -1;
589
590 esma->selected_row = -1;
591 esma->selected_range_end = -1;
592 }
593
594 static void
595 e_selection_model_array_class_init (ESelectionModelArrayClass *class)
596 {
597 GObjectClass *object_class;
598 ESelectionModelClass *esm_class;
599
600 object_class = G_OBJECT_CLASS (class);
601 esm_class = E_SELECTION_MODEL_CLASS (class);
602
603 object_class->dispose = esma_dispose;
604 object_class->get_property = esma_get_property;
605 object_class->set_property = esma_set_property;
606
607 esm_class->is_row_selected = esma_is_row_selected;
608 esm_class->foreach = esma_foreach;
609 esm_class->clear = esma_clear;
610 esm_class->selected_count = esma_selected_count;
611 esm_class->select_all = esma_select_all;
612 esm_class->invert_selection = esma_invert_selection;
613 esm_class->row_count = esma_row_count;
614
615 esm_class->change_one_row = esma_change_one_row;
616 esm_class->change_cursor = esma_change_cursor;
617 esm_class->cursor_row = esma_cursor_row;
618 esm_class->cursor_col = esma_cursor_col;
619
620 esm_class->select_single_row = esma_select_single_row;
621 esm_class->toggle_single_row = esma_toggle_single_row;
622 esm_class->move_selection_end = esma_move_selection_end;
623 esm_class->set_selection_end = esma_set_selection_end;
624
625 class->get_row_count = NULL;
626
627 g_object_class_install_property (
628 object_class,
629 PROP_CURSOR_ROW,
630 g_param_spec_int (
631 "cursor_row",
632 "Cursor Row",
633 NULL,
634 0, G_MAXINT, 0,
635 G_PARAM_READWRITE));
636
637 g_object_class_install_property (
638 object_class,
639 PROP_CURSOR_COL,
640 g_param_spec_int (
641 "cursor_col",
642 "Cursor Column",
643 NULL,
644 0, G_MAXINT, 0,
645 G_PARAM_READWRITE));
646 }