-
Notifications
You must be signed in to change notification settings - Fork 383
/
Copy pathgtkcellarea.c
3695 lines (3262 loc) · 127 KB
/
gtkcellarea.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* gtkcellarea.c
*
* Copyright (C) 2010 Openismus GmbH
*
* Authors:
* Tristan Van Berkom <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:gtkcellarea
* @Short_Description: An abstract class for laying out GtkCellRenderers
* @Title: GtkCellArea
*
* The #GtkCellArea is an abstract class for #GtkCellLayout widgets
* (also referred to as "layouting widgets") to interface with an
* arbitrary number of #GtkCellRenderers and interact with the user
* for a given #GtkTreeModel row.
*
* The cell area handles events, focus navigation, drawing and
* size requests and allocations for a given row of data.
*
* Usually users dont have to interact with the #GtkCellArea directly
* unless they are implementing a cell-layouting widget themselves.
*
* # Requesting area sizes
*
* As outlined in
* [GtkWidget’s geometry management section][geometry-management],
* GTK+ uses a height-for-width
* geometry management system to compute the sizes of widgets and user
* interfaces. #GtkCellArea uses the same semantics to calculate the
* size of an area for an arbitrary number of #GtkTreeModel rows.
*
* When requesting the size of a cell area one needs to calculate
* the size for a handful of rows, and this will be done differently by
* different layouting widgets. For instance a #GtkTreeViewColumn
* always lines up the areas from top to bottom while a #GtkIconView
* on the other hand might enforce that all areas received the same
* width and wrap the areas around, requesting height for more cell
* areas when allocated less width.
*
* It’s also important for areas to maintain some cell
* alignments with areas rendered for adjacent rows (cells can
* appear “columnized” inside an area even when the size of
* cells are different in each row). For this reason the #GtkCellArea
* uses a #GtkCellAreaContext object to store the alignments
* and sizes along the way (as well as the overall largest minimum
* and natural size for all the rows which have been calculated
* with the said context).
*
* The #GtkCellAreaContext is an opaque object specific to the
* #GtkCellArea which created it (see gtk_cell_area_create_context()).
* The owning cell-layouting widget can create as many contexts as
* it wishes to calculate sizes of rows which should receive the
* same size in at least one orientation (horizontally or vertically),
* However, it’s important that the same #GtkCellAreaContext which
* was used to request the sizes for a given #GtkTreeModel row be
* used when rendering or processing events for that row.
*
* In order to request the width of all the rows at the root level
* of a #GtkTreeModel one would do the following:
*
* |[<!-- language="C" -->
* GtkTreeIter iter;
* gint minimum_width;
* gint natural_width;
*
* valid = gtk_tree_model_get_iter_first (model, &iter);
* while (valid)
* {
* gtk_cell_area_apply_attributes (area, model, &iter, FALSE, FALSE);
* gtk_cell_area_get_preferred_width (area, context, widget, NULL, NULL);
*
* valid = gtk_tree_model_iter_next (model, &iter);
* }
* gtk_cell_area_context_get_preferred_width (context, &minimum_width, &natural_width);
* ]|
*
* Note that in this example it’s not important to observe the
* returned minimum and natural width of the area for each row
* unless the cell-layouting object is actually interested in the
* widths of individual rows. The overall width is however stored
* in the accompanying #GtkCellAreaContext object and can be consulted
* at any time.
*
* This can be useful since #GtkCellLayout widgets usually have to
* support requesting and rendering rows in treemodels with an
* exceedingly large amount of rows. The #GtkCellLayout widget in
* that case would calculate the required width of the rows in an
* idle or timeout source (see g_timeout_add()) and when the widget
* is requested its actual width in #GtkWidgetClass.get_preferred_width()
* it can simply consult the width accumulated so far in the
* #GtkCellAreaContext object.
*
* A simple example where rows are rendered from top to bottom and
* take up the full width of the layouting widget would look like:
*
* |[<!-- language="C" -->
* static void
* foo_get_preferred_width (GtkWidget *widget,
* gint *minimum_size,
* gint *natural_size)
* {
* Foo *foo = FOO (widget);
* FooPrivate *priv = foo->priv;
*
* foo_ensure_at_least_one_handfull_of_rows_have_been_requested (foo);
*
* gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
* }
* ]|
*
* In the above example the Foo widget has to make sure that some
* row sizes have been calculated (the amount of rows that Foo judged
* was appropriate to request space for in a single timeout iteration)
* before simply returning the amount of space required by the area via
* the #GtkCellAreaContext.
*
* Requesting the height for width (or width for height) of an area is
* a similar task except in this case the #GtkCellAreaContext does not
* store the data (actually, it does not know how much space the layouting
* widget plans to allocate it for every row. It’s up to the layouting
* widget to render each row of data with the appropriate height and
* width which was requested by the #GtkCellArea).
*
* In order to request the height for width of all the rows at the
* root level of a #GtkTreeModel one would do the following:
*
* |[<!-- language="C" -->
* GtkTreeIter iter;
* gint minimum_height;
* gint natural_height;
* gint full_minimum_height = 0;
* gint full_natural_height = 0;
*
* valid = gtk_tree_model_get_iter_first (model, &iter);
* while (valid)
* {
* gtk_cell_area_apply_attributes (area, model, &iter, FALSE, FALSE);
* gtk_cell_area_get_preferred_height_for_width (area, context, widget,
* width, &minimum_height, &natural_height);
*
* if (width_is_for_allocation)
* cache_row_height (&iter, minimum_height, natural_height);
*
* full_minimum_height += minimum_height;
* full_natural_height += natural_height;
*
* valid = gtk_tree_model_iter_next (model, &iter);
* }
* ]|
*
* Note that in the above example we would need to cache the heights
* returned for each row so that we would know what sizes to render the
* areas for each row. However we would only want to really cache the
* heights if the request is intended for the layouting widgets real
* allocation.
*
* In some cases the layouting widget is requested the height for an
* arbitrary for_width, this is a special case for layouting widgets
* who need to request size for tens of thousands of rows. For this
* case it’s only important that the layouting widget calculate
* one reasonably sized chunk of rows and return that height
* synchronously. The reasoning here is that any layouting widget is
* at least capable of synchronously calculating enough height to fill
* the screen height (or scrolled window height) in response to a single
* call to #GtkWidgetClass.get_preferred_height_for_width(). Returning
* a perfect height for width that is larger than the screen area is
* inconsequential since after the layouting receives an allocation
* from a scrolled window it simply continues to drive the scrollbar
* values while more and more height is required for the row heights
* that are calculated in the background.
*
* # Rendering Areas
*
* Once area sizes have been aquired at least for the rows in the
* visible area of the layouting widget they can be rendered at
* #GtkWidgetClass.draw() time.
*
* A crude example of how to render all the rows at the root level
* runs as follows:
*
* |[<!-- language="C" -->
* GtkAllocation allocation;
* GdkRectangle cell_area = { 0, };
* GtkTreeIter iter;
* gint minimum_width;
* gint natural_width;
*
* gtk_widget_get_allocation (widget, &allocation);
* cell_area.width = allocation.width;
*
* valid = gtk_tree_model_get_iter_first (model, &iter);
* while (valid)
* {
* cell_area.height = get_cached_height_for_row (&iter);
*
* gtk_cell_area_apply_attributes (area, model, &iter, FALSE, FALSE);
* gtk_cell_area_render (area, context, widget, cr,
* &cell_area, &cell_area, state_flags, FALSE);
*
* cell_area.y += cell_area.height;
*
* valid = gtk_tree_model_iter_next (model, &iter);
* }
* ]|
*
* Note that the cached height in this example really depends on how
* the layouting widget works. The layouting widget might decide to
* give every row its minimum or natural height or, if the model content
* is expected to fit inside the layouting widget without scrolling, it
* would make sense to calculate the allocation for each row at
* #GtkWidget::size-allocate time using gtk_distribute_natural_allocation().
*
* # Handling Events and Driving Keyboard Focus
*
* Passing events to the area is as simple as handling events on any
* normal widget and then passing them to the gtk_cell_area_event()
* API as they come in. Usually #GtkCellArea is only interested in
* button events, however some customized derived areas can be implemented
* who are interested in handling other events. Handling an event can
* trigger the #GtkCellArea::focus-changed signal to fire; as well as
* #GtkCellArea::add-editable in the case that an editable cell was
* clicked and needs to start editing. You can call
* gtk_cell_area_stop_editing() at any time to cancel any cell editing
* that is currently in progress.
*
* The #GtkCellArea drives keyboard focus from cell to cell in a way
* similar to #GtkWidget. For layouting widgets that support giving
* focus to cells it’s important to remember to pass %GTK_CELL_RENDERER_FOCUSED
* to the area functions for the row that has focus and to tell the
* area to paint the focus at render time.
*
* Layouting widgets that accept focus on cells should implement the
* #GtkWidgetClass.focus() virtual method. The layouting widget is always
* responsible for knowing where #GtkTreeModel rows are rendered inside
* the widget, so at #GtkWidgetClass.focus() time the layouting widget
* should use the #GtkCellArea methods to navigate focus inside the area
* and then observe the GtkDirectionType to pass the focus to adjacent
* rows and areas.
*
* A basic example of how the #GtkWidgetClass.focus() virtual method
* should be implemented:
*
* |[<!-- language="C" -->
* static gboolean
* foo_focus (GtkWidget *widget,
* GtkDirectionType direction)
* {
* Foo *foo = FOO (widget);
* FooPrivate *priv = foo->priv;
* gint focus_row;
* gboolean have_focus = FALSE;
*
* focus_row = priv->focus_row;
*
* if (!gtk_widget_has_focus (widget))
* gtk_widget_grab_focus (widget);
*
* valid = gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, priv->focus_row);
* while (valid)
* {
* gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
*
* if (gtk_cell_area_focus (priv->area, direction))
* {
* priv->focus_row = focus_row;
* have_focus = TRUE;
* break;
* }
* else
* {
* if (direction == GTK_DIR_RIGHT ||
* direction == GTK_DIR_LEFT)
* break;
* else if (direction == GTK_DIR_UP ||
* direction == GTK_DIR_TAB_BACKWARD)
* {
* if (focus_row == 0)
* break;
* else
* {
* focus_row--;
* valid = gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, focus_row);
* }
* }
* else
* {
* if (focus_row == last_row)
* break;
* else
* {
* focus_row++;
* valid = gtk_tree_model_iter_next (priv->model, &iter);
* }
* }
* }
* }
* return have_focus;
* }
* ]|
*
* Note that the layouting widget is responsible for matching the
* GtkDirectionType values to the way it lays out its cells.
*
* # Cell Properties
*
* The #GtkCellArea introduces cell properties for #GtkCellRenderers
* in very much the same way that #GtkContainer introduces
* [child properties][child-properties]
* for #GtkWidgets. This provides some general interfaces for defining
* the relationship cell areas have with their cells. For instance in a
* #GtkCellAreaBox a cell might “expand” and receive extra space when
* the area is allocated more than its full natural request, or a cell
* might be configured to “align” with adjacent rows which were requested
* and rendered with the same #GtkCellAreaContext.
*
* Use gtk_cell_area_class_install_cell_property() to install cell
* properties for a cell area class and gtk_cell_area_class_find_cell_property()
* or gtk_cell_area_class_list_cell_properties() to get information about
* existing cell properties.
*
* To set the value of a cell property, use gtk_cell_area_cell_set_property(),
* gtk_cell_area_cell_set() or gtk_cell_area_cell_set_valist(). To obtain
* the value of a cell property, use gtk_cell_area_cell_get_property(),
* gtk_cell_area_cell_get() or gtk_cell_area_cell_get_valist().
*/
#include "config.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "gtkintl.h"
#include "gtkcelllayout.h"
#include "gtkcellarea.h"
#include "gtkcellareacontext.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkrender.h"
#include <gobject/gvaluecollector.h>
/* GObjectClass */
static void gtk_cell_area_dispose (GObject *object);
static void gtk_cell_area_finalize (GObject *object);
static void gtk_cell_area_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_cell_area_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
/* GtkCellAreaClass */
static void gtk_cell_area_real_add (GtkCellArea *area,
GtkCellRenderer *renderer);
static void gtk_cell_area_real_remove (GtkCellArea *area,
GtkCellRenderer *renderer);
static void gtk_cell_area_real_foreach (GtkCellArea *area,
GtkCellCallback callback,
gpointer callback_data);
static void gtk_cell_area_real_foreach_alloc (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
const GdkRectangle *cell_area,
const GdkRectangle *background_area,
GtkCellAllocCallback callback,
gpointer callback_data);
static gint gtk_cell_area_real_event (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
GdkEvent *event,
const GdkRectangle *cell_area,
GtkCellRendererState flags);
static void gtk_cell_area_real_render (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
cairo_t *cr,
const GdkRectangle *background_area,
const GdkRectangle *cell_area,
GtkCellRendererState flags,
gboolean paint_focus);
static void gtk_cell_area_real_apply_attributes (GtkCellArea *area,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gboolean is_expander,
gboolean is_expanded);
static GtkCellAreaContext *gtk_cell_area_real_create_context (GtkCellArea *area);
static GtkCellAreaContext *gtk_cell_area_real_copy_context (GtkCellArea *area,
GtkCellAreaContext *context);
static GtkSizeRequestMode gtk_cell_area_real_get_request_mode (GtkCellArea *area);
static void gtk_cell_area_real_get_preferred_width (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
gint *minimum_width,
gint *natural_width);
static void gtk_cell_area_real_get_preferred_height (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
gint *minimum_height,
gint *natural_height);
static void gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
gint width,
gint *minimum_height,
gint *natural_height);
static void gtk_cell_area_real_get_preferred_width_for_height (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
gint height,
gint *minimum_width,
gint *natural_width);
static gboolean gtk_cell_area_real_is_activatable (GtkCellArea *area);
static gboolean gtk_cell_area_real_activate (GtkCellArea *area,
GtkCellAreaContext *context,
GtkWidget *widget,
const GdkRectangle *cell_area,
GtkCellRendererState flags,
gboolean edit_only);
static gboolean gtk_cell_area_real_focus (GtkCellArea *area,
GtkDirectionType direction);
/* GtkCellLayoutIface */
static void gtk_cell_area_cell_layout_init (GtkCellLayoutIface *iface);
static void gtk_cell_area_pack_default (GtkCellLayout *cell_layout,
GtkCellRenderer *renderer,
gboolean expand);
static void gtk_cell_area_clear (GtkCellLayout *cell_layout);
static void gtk_cell_area_add_attribute (GtkCellLayout *cell_layout,
GtkCellRenderer *renderer,
const gchar *attribute,
gint column);
static void gtk_cell_area_set_cell_data_func (GtkCellLayout *cell_layout,
GtkCellRenderer *cell,
GtkCellLayoutDataFunc func,
gpointer func_data,
GDestroyNotify destroy);
static void gtk_cell_area_clear_attributes (GtkCellLayout *cell_layout,
GtkCellRenderer *renderer);
static void gtk_cell_area_reorder (GtkCellLayout *cell_layout,
GtkCellRenderer *cell,
gint position);
static GList *gtk_cell_area_get_cells (GtkCellLayout *cell_layout);
static GtkCellArea *gtk_cell_area_get_area (GtkCellLayout *cell_layout);
/* GtkBuildableIface */
static void gtk_cell_area_buildable_init (GtkBuildableIface *iface);
static void gtk_cell_area_buildable_custom_tag_end (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer *data);
/* Used in foreach loop to check if a child renderer is present */
typedef struct {
GtkCellRenderer *renderer;
gboolean has_renderer;
} HasRendererCheck;
/* Used in foreach loop to get a cell's allocation */
typedef struct {
GtkCellRenderer *renderer;
GdkRectangle allocation;
} RendererAllocationData;
/* Used in foreach loop to render cells */
typedef struct {
GtkCellArea *area;
GtkWidget *widget;
cairo_t *cr;
GdkRectangle focus_rect;
GtkCellRendererState render_flags;
guint paint_focus : 1;
guint focus_all : 1;
guint first_focus : 1;
} CellRenderData;
/* Used in foreach loop to get a cell by position */
typedef struct {
gint x;
gint y;
GtkCellRenderer *renderer;
GdkRectangle cell_area;
} CellByPositionData;
/* Attribute/Cell metadata */
typedef struct {
const gchar *attribute;
gint column;
} CellAttribute;
typedef struct {
GSList *attributes;
GtkCellLayoutDataFunc func;
gpointer data;
GDestroyNotify destroy;
GtkCellLayout *proxy;
} CellInfo;
static CellInfo *cell_info_new (GtkCellLayoutDataFunc func,
gpointer data,
GDestroyNotify destroy);
static void cell_info_free (CellInfo *info);
static CellAttribute *cell_attribute_new (GtkCellRenderer *renderer,
const gchar *attribute,
gint column);
static void cell_attribute_free (CellAttribute *attribute);
static gint cell_attribute_find (CellAttribute *cell_attribute,
const gchar *attribute);
/* Internal functions/signal emissions */
static void gtk_cell_area_add_editable (GtkCellArea *area,
GtkCellRenderer *renderer,
GtkCellEditable *editable,
const GdkRectangle *cell_area);
static void gtk_cell_area_remove_editable (GtkCellArea *area,
GtkCellRenderer *renderer,
GtkCellEditable *editable);
static void gtk_cell_area_set_edit_widget (GtkCellArea *area,
GtkCellEditable *editable);
static void gtk_cell_area_set_edited_cell (GtkCellArea *area,
GtkCellRenderer *renderer);
/* Struct to pass data along while looping over
* cell renderers to apply attributes
*/
typedef struct {
GtkCellArea *area;
GtkTreeModel *model;
GtkTreeIter *iter;
gboolean is_expander;
gboolean is_expanded;
} AttributeData;
struct _GtkCellAreaPrivate
{
/* The GtkCellArea bookkeeps any connected
* attributes in this hash table.
*/
GHashTable *cell_info;
/* Current path is saved as a side-effect
* of gtk_cell_area_apply_attributes()
*/
gchar *current_path;
/* Current cell being edited and editable widget used */
GtkCellEditable *edit_widget;
GtkCellRenderer *edited_cell;
/* Signal connections to the editable widget */
gulong remove_widget_id;
/* Currently focused cell */
GtkCellRenderer *focus_cell;
/* Tracking which cells are focus siblings of focusable cells */
GHashTable *focus_siblings;
};
enum {
PROP_0,
PROP_FOCUS_CELL,
PROP_EDITED_CELL,
PROP_EDIT_WIDGET
};
enum {
SIGNAL_APPLY_ATTRIBUTES,
SIGNAL_ADD_EDITABLE,
SIGNAL_REMOVE_EDITABLE,
SIGNAL_FOCUS_CHANGED,
LAST_SIGNAL
};
/* Keep the paramspec pool internal, no need to deliver notifications
* on cells. at least no perceived need for now
*/
static GParamSpecPool *cell_property_pool = NULL;
static guint cell_area_signals[LAST_SIGNAL] = { 0 };
#define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
#define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkCellArea, gtk_cell_area, G_TYPE_INITIALLY_UNOWNED,
G_ADD_PRIVATE (GtkCellArea)
G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
gtk_cell_area_cell_layout_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_cell_area_buildable_init))
static void
gtk_cell_area_init (GtkCellArea *area)
{
GtkCellAreaPrivate *priv;
area->priv = gtk_cell_area_get_instance_private (area);
priv = area->priv;
priv->cell_info = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
(GDestroyNotify)cell_info_free);
priv->focus_siblings = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
(GDestroyNotify)g_list_free);
priv->focus_cell = NULL;
priv->edited_cell = NULL;
priv->edit_widget = NULL;
priv->remove_widget_id = 0;
}
static void
gtk_cell_area_class_init (GtkCellAreaClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
/* GObjectClass */
object_class->dispose = gtk_cell_area_dispose;
object_class->finalize = gtk_cell_area_finalize;
object_class->get_property = gtk_cell_area_get_property;
object_class->set_property = gtk_cell_area_set_property;
/* general */
class->add = gtk_cell_area_real_add;
class->remove = gtk_cell_area_real_remove;
class->foreach = gtk_cell_area_real_foreach;
class->foreach_alloc = gtk_cell_area_real_foreach_alloc;
class->event = gtk_cell_area_real_event;
class->render = gtk_cell_area_real_render;
class->apply_attributes = gtk_cell_area_real_apply_attributes;
/* geometry */
class->create_context = gtk_cell_area_real_create_context;
class->copy_context = gtk_cell_area_real_copy_context;
class->get_request_mode = gtk_cell_area_real_get_request_mode;
class->get_preferred_width = gtk_cell_area_real_get_preferred_width;
class->get_preferred_height = gtk_cell_area_real_get_preferred_height;
class->get_preferred_height_for_width = gtk_cell_area_real_get_preferred_height_for_width;
class->get_preferred_width_for_height = gtk_cell_area_real_get_preferred_width_for_height;
/* focus */
class->is_activatable = gtk_cell_area_real_is_activatable;
class->activate = gtk_cell_area_real_activate;
class->focus = gtk_cell_area_real_focus;
/* Signals */
/**
* GtkCellArea::apply-attributes:
* @area: the #GtkCellArea to apply the attributes to
* @model: the #GtkTreeModel to apply the attributes from
* @iter: the #GtkTreeIter indicating which row to apply the attributes of
* @is_expander: whether the view shows children for this row
* @is_expanded: whether the view is currently showing the children of this row
*
* This signal is emitted whenever applying attributes to @area from @model
*
* Since: 3.0
*/
cell_area_signals[SIGNAL_APPLY_ATTRIBUTES] =
g_signal_new (I_("apply-attributes"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkCellAreaClass, apply_attributes),
NULL, NULL,
_gtk_marshal_VOID__OBJECT_BOXED_BOOLEAN_BOOLEAN,
G_TYPE_NONE, 4,
GTK_TYPE_TREE_MODEL,
GTK_TYPE_TREE_ITER,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN);
g_signal_set_va_marshaller (cell_area_signals[SIGNAL_APPLY_ATTRIBUTES], G_TYPE_FROM_CLASS (class),
_gtk_marshal_VOID__OBJECT_BOXED_BOOLEAN_BOOLEANv);
/**
* GtkCellArea::add-editable:
* @area: the #GtkCellArea where editing started
* @renderer: the #GtkCellRenderer that started the edited
* @editable: the #GtkCellEditable widget to add
* @cell_area: the #GtkWidget relative #GdkRectangle coordinates
* where @editable should be added
* @path: the #GtkTreePath string this edit was initiated for
*
* Indicates that editing has started on @renderer and that @editable
* should be added to the owning cell-layouting widget at @cell_area.
*
* Since: 3.0
*/
cell_area_signals[SIGNAL_ADD_EDITABLE] =
g_signal_new (I_("add-editable"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0, /* No class closure here */
NULL, NULL,
_gtk_marshal_VOID__OBJECT_OBJECT_BOXED_STRING,
G_TYPE_NONE, 4,
GTK_TYPE_CELL_RENDERER,
GTK_TYPE_CELL_EDITABLE,
GDK_TYPE_RECTANGLE,
G_TYPE_STRING);
/**
* GtkCellArea::remove-editable:
* @area: the #GtkCellArea where editing finished
* @renderer: the #GtkCellRenderer that finished editeding
* @editable: the #GtkCellEditable widget to remove
*
* Indicates that editing finished on @renderer and that @editable
* should be removed from the owning cell-layouting widget.
*
* Since: 3.0
*/
cell_area_signals[SIGNAL_REMOVE_EDITABLE] =
g_signal_new (I_("remove-editable"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0, /* No class closure here */
NULL, NULL,
_gtk_marshal_VOID__OBJECT_OBJECT,
G_TYPE_NONE, 2,
GTK_TYPE_CELL_RENDERER,
GTK_TYPE_CELL_EDITABLE);
/**
* GtkCellArea::focus-changed:
* @area: the #GtkCellArea where focus changed
* @renderer: the #GtkCellRenderer that has focus
* @path: the current #GtkTreePath string set for @area
*
* Indicates that focus changed on this @area. This signal
* is emitted either as a result of focus handling or event
* handling.
*
* It's possible that the signal is emitted even if the
* currently focused renderer did not change, this is
* because focus may change to the same renderer in the
* same cell area for a different row of data.
*
* Since: 3.0
*/
cell_area_signals[SIGNAL_FOCUS_CHANGED] =
g_signal_new (I_("focus-changed"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0, /* No class closure here */
NULL, NULL,
_gtk_marshal_VOID__OBJECT_STRING,
G_TYPE_NONE, 2,
GTK_TYPE_CELL_RENDERER,
G_TYPE_STRING);
/* Properties */
/**
* GtkCellArea:focus-cell:
*
* The cell in the area that currently has focus
*
* Since: 3.0
*/
g_object_class_install_property (object_class,
PROP_FOCUS_CELL,
g_param_spec_object
("focus-cell",
P_("Focus Cell"),
P_("The cell which currently has focus"),
GTK_TYPE_CELL_RENDERER,
GTK_PARAM_READWRITE));
/**
* GtkCellArea:edited-cell:
*
* The cell in the area that is currently edited
*
* This property is read-only and only changes as
* a result of a call gtk_cell_area_activate_cell().
*
* Since: 3.0
*/
g_object_class_install_property (object_class,
PROP_EDITED_CELL,
g_param_spec_object
("edited-cell",
P_("Edited Cell"),
P_("The cell which is currently being edited"),
GTK_TYPE_CELL_RENDERER,
G_PARAM_READABLE));
/**
* GtkCellArea:edit-widget:
*
* The widget currently editing the edited cell
*
* This property is read-only and only changes as
* a result of a call gtk_cell_area_activate_cell().
*
* Since: 3.0
*/
g_object_class_install_property (object_class,
PROP_EDIT_WIDGET,
g_param_spec_object
("edit-widget",
P_("Edit Widget"),
P_("The widget currently editing the edited cell"),
GTK_TYPE_CELL_EDITABLE,
G_PARAM_READABLE));
/* Pool for Cell Properties */
if (!cell_property_pool)
cell_property_pool = g_param_spec_pool_new (FALSE);
}
/*************************************************************
* CellInfo Basics *
*************************************************************/
static CellInfo *
cell_info_new (GtkCellLayoutDataFunc func,
gpointer data,
GDestroyNotify destroy)
{
CellInfo *info = g_slice_new0 (CellInfo);
info->func = func;
info->data = data;
info->destroy = destroy;
return info;
}
static void
cell_info_free (CellInfo *info)
{
if (info->destroy)
info->destroy (info->data);
g_slist_free_full (info->attributes, (GDestroyNotify)cell_attribute_free);
g_slice_free (CellInfo, info);
}
static CellAttribute *
cell_attribute_new (GtkCellRenderer *renderer,
const gchar *attribute,
gint column)
{
GParamSpec *pspec;
/* Check if the attribute really exists and point to
* the property string installed on the cell renderer
* class (dont dup the string)
*/
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (renderer), attribute);
if (pspec)
{
CellAttribute *cell_attribute = g_slice_new (CellAttribute);
cell_attribute->attribute = pspec->name;
cell_attribute->column = column;
return cell_attribute;
}
return NULL;
}
static void
cell_attribute_free (CellAttribute *attribute)
{
g_slice_free (CellAttribute, attribute);
}
/* GCompareFunc for g_slist_find_custom() */
static gint
cell_attribute_find (CellAttribute *cell_attribute,
const gchar *attribute)
{
return g_strcmp0 (cell_attribute->attribute, attribute);
}
/*************************************************************
* GObjectClass *
*************************************************************/
static void
gtk_cell_area_finalize (GObject *object)
{
GtkCellArea *area = GTK_CELL_AREA (object);
GtkCellAreaPrivate *priv = area->priv;
/* All cell renderers should already be removed at this point,
* just kill our (empty) hash tables here.
*/
g_hash_table_destroy (priv->cell_info);
g_hash_table_destroy (priv->focus_siblings);
g_free (priv->current_path);
G_OBJECT_CLASS (gtk_cell_area_parent_class)->finalize (object);
}
static void
gtk_cell_area_dispose (GObject *object)
{
/* This removes every cell renderer that may be added to the GtkCellArea,
* subclasses should be breaking references to the GtkCellRenderers
* at this point.
*/
gtk_cell_layout_clear (GTK_CELL_LAYOUT (object));
/* Remove any ref to a focused/edited cell */
gtk_cell_area_set_focus_cell (GTK_CELL_AREA (object), NULL);
gtk_cell_area_set_edited_cell (GTK_CELL_AREA (object), NULL);
gtk_cell_area_set_edit_widget (GTK_CELL_AREA (object), NULL);
G_OBJECT_CLASS (gtk_cell_area_parent_class)->dispose (object);
}
static void
gtk_cell_area_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkCellArea *area = GTK_CELL_AREA (object);
switch (prop_id)
{
case PROP_FOCUS_CELL:
gtk_cell_area_set_focus_cell (area, (GtkCellRenderer *)g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_cell_area_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkCellArea *area = GTK_CELL_AREA (object);
GtkCellAreaPrivate *priv = area->priv;
switch (prop_id)
{
case PROP_FOCUS_CELL:
g_value_set_object (value, priv->focus_cell);
break;
case PROP_EDITED_CELL:
g_value_set_object (value, priv->edited_cell);
break;
case PROP_EDIT_WIDGET:
g_value_set_object (value, priv->edit_widget);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/*************************************************************
* GtkCellAreaClass *
*************************************************************/
static void
gtk_cell_area_real_add (GtkCellArea *area,
GtkCellRenderer *renderer)
{
g_warning ("GtkCellAreaClass::add not implemented for '%s'",
g_type_name (G_TYPE_FROM_INSTANCE (area)));
}
static void