1 | /** |
2 | * GtkCustomLayout: |
3 | * |
4 | * `GtkCustomLayout` uses closures for size negotiation. |
5 | * |
6 | * A `GtkCustomLayout `uses closures matching to the old `GtkWidget` |
7 | * virtual functions for size negotiation, as a convenience API to |
8 | * ease the porting towards the corresponding `GtkLayoutManager |
9 | * virtual functions. |
10 | */ |
11 | |
12 | #include "config.h" |
13 | |
14 | #include "gtkcustomlayout.h" |
15 | |
16 | struct _GtkCustomLayout |
17 | { |
18 | GtkLayoutManager parent_instance; |
19 | |
20 | GtkCustomRequestModeFunc request_mode_func; |
21 | GtkCustomMeasureFunc measure_func; |
22 | GtkCustomAllocateFunc allocate_func; |
23 | }; |
24 | |
25 | G_DEFINE_TYPE (GtkCustomLayout, gtk_custom_layout, GTK_TYPE_LAYOUT_MANAGER) |
26 | |
27 | static GtkSizeRequestMode |
28 | gtk_custom_layout_get_request_mode (GtkLayoutManager *manager, |
29 | GtkWidget *widget) |
30 | { |
31 | GtkCustomLayout *self = GTK_CUSTOM_LAYOUT (ptr: manager); |
32 | |
33 | if (self->request_mode_func != NULL) |
34 | return self->request_mode_func (widget); |
35 | |
36 | return GTK_LAYOUT_MANAGER_CLASS (ptr: gtk_custom_layout_parent_class)->get_request_mode (manager, widget); |
37 | } |
38 | |
39 | static void |
40 | gtk_custom_layout_measure (GtkLayoutManager *manager, |
41 | GtkWidget *widget, |
42 | GtkOrientation orientation, |
43 | int for_size, |
44 | int *minimum, |
45 | int *natural, |
46 | int *minimum_baseline, |
47 | int *natural_baseline) |
48 | { |
49 | GtkCustomLayout *self = GTK_CUSTOM_LAYOUT (ptr: manager); |
50 | int min = 0, nat = 0; |
51 | int min_baseline = -1, nat_baseline = -1; |
52 | |
53 | self->measure_func (widget, orientation, for_size, |
54 | &min, &nat, |
55 | &min_baseline, &nat_baseline); |
56 | |
57 | if (minimum != NULL) |
58 | *minimum = min; |
59 | if (natural != NULL) |
60 | *natural = nat; |
61 | |
62 | if (minimum_baseline != NULL) |
63 | *minimum_baseline = min_baseline; |
64 | if (natural_baseline != NULL) |
65 | *natural_baseline = nat_baseline; |
66 | } |
67 | |
68 | static void |
69 | gtk_custom_layout_allocate (GtkLayoutManager *manager, |
70 | GtkWidget *widget, |
71 | int width, |
72 | int height, |
73 | int baseline) |
74 | { |
75 | GtkCustomLayout *self = GTK_CUSTOM_LAYOUT (ptr: manager); |
76 | |
77 | self->allocate_func (widget, width, height, baseline); |
78 | } |
79 | |
80 | static void |
81 | gtk_custom_layout_class_init (GtkCustomLayoutClass *klass) |
82 | { |
83 | GtkLayoutManagerClass *layout_class = GTK_LAYOUT_MANAGER_CLASS (ptr: klass); |
84 | |
85 | layout_class->get_request_mode = gtk_custom_layout_get_request_mode; |
86 | layout_class->measure = gtk_custom_layout_measure; |
87 | layout_class->allocate = gtk_custom_layout_allocate; |
88 | } |
89 | |
90 | static void |
91 | gtk_custom_layout_init (GtkCustomLayout *self) |
92 | { |
93 | } |
94 | |
95 | /** |
96 | * gtk_custom_layout_new: |
97 | * @request_mode: (nullable) (scope call): a function to retrieve |
98 | * the `GtkSizeRequestMode` of the widget using the layout; the |
99 | * default request mode is %GTK_SIZE_REQUEST_CONSTANT_SIZE |
100 | * @measure: (not nullable) (scope call): a function to measure the widget using the layout manager |
101 | * @allocate: (not nullable) (scope call): a function to allocate the children of the widget using |
102 | * the layout manager |
103 | * |
104 | * Creates a new legacy layout manager. |
105 | * |
106 | * Legacy layout managers map to the old `GtkWidget` size negotiation |
107 | * virtual functions, and are meant to be used during the transition |
108 | * from layout containers to layout manager delegates. |
109 | * |
110 | * Returns: (transfer full): the newly created `GtkCustomLayout` |
111 | */ |
112 | GtkLayoutManager * |
113 | gtk_custom_layout_new (GtkCustomRequestModeFunc request_mode, |
114 | GtkCustomMeasureFunc measure, |
115 | GtkCustomAllocateFunc allocate) |
116 | { |
117 | GtkCustomLayout *self = g_object_new (GTK_TYPE_CUSTOM_LAYOUT, NULL); |
118 | |
119 | g_return_val_if_fail (measure != NULL, NULL); |
120 | g_return_val_if_fail (allocate != NULL, NULL); |
121 | |
122 | self->request_mode_func = request_mode; |
123 | self->measure_func = measure; |
124 | self->allocate_func = allocate; |
125 | |
126 | return GTK_LAYOUT_MANAGER (ptr: self); |
127 | } |
128 | |