No issues found
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /**
4 * SECTION:shell-slicer
5 * @short_description: Display only part of another actor
6 *
7 * A #StBin that has 0 minimum size, and will clip its child
8 * in the middle.
9 */
10
11 #include "config.h"
12
13 #include "shell-slicer.h"
14
15 G_DEFINE_TYPE (ShellSlicer,
16 shell_slicer,
17 ST_TYPE_BIN);
18
19 static void
20 shell_slicer_get_preferred_width (ClutterActor *self,
21 gfloat for_height,
22 gfloat *min_width_p,
23 gfloat *natural_width_p)
24 {
25 ClutterActor *child = st_bin_get_child (ST_BIN (self));
26 StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
27
28 st_theme_node_adjust_for_height (theme_node, &for_height);
29
30 if (min_width_p)
31 *min_width_p = 0;
32
33 if (child == NULL)
34 {
35 if (natural_width_p)
36 *natural_width_p = 0;
37 }
38 else
39 {
40 _st_actor_get_preferred_width (child, for_height, FALSE,
41 NULL,
42 natural_width_p);
43 }
44
45 st_theme_node_adjust_preferred_width (theme_node, min_width_p, natural_width_p);
46 }
47
48 static void
49 shell_slicer_get_preferred_height (ClutterActor *self,
50 gfloat for_width,
51 gfloat *min_height_p,
52 gfloat *natural_height_p)
53 {
54 ClutterActor *child = st_bin_get_child (ST_BIN (self));
55 StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
56
57 st_theme_node_adjust_for_width (theme_node, &for_width);
58
59 if (min_height_p)
60 *min_height_p = 0;
61
62 if (child == NULL)
63 {
64 if (natural_height_p)
65 *natural_height_p = 0;
66 }
67 else
68 {
69 _st_actor_get_preferred_height (child, for_width, FALSE,
70 NULL,
71 natural_height_p);
72 }
73
74 st_theme_node_adjust_preferred_height (theme_node, min_height_p, natural_height_p);
75 }
76
77 static void
78 shell_slicer_allocate (ClutterActor *self,
79 const ClutterActorBox *box,
80 ClutterAllocationFlags flags)
81 {
82 ClutterActor *child;
83
84 clutter_actor_set_allocation (self, box, flags);
85
86 child = st_bin_get_child (ST_BIN (self));
87 if (child)
88 clutter_actor_allocate_preferred_size (child, flags);
89 }
90
91 static void
92 shell_slicer_paint_child (ShellSlicer *self)
93 {
94 ClutterActor *child;
95 ClutterActorBox self_box;
96 ClutterActorBox child_box;
97 float width, height, child_width, child_height;
98 StAlign x_align, y_align;
99 double x_align_factor, y_align_factor;
100
101 child = st_bin_get_child (ST_BIN (self));
102
103 if (!child)
104 return;
105
106 st_bin_get_alignment (ST_BIN (self), &x_align, &y_align);
107 _st_get_align_factors (x_align, y_align,
108 &x_align_factor, &y_align_factor);
109
110 clutter_actor_get_allocation_box (CLUTTER_ACTOR (self), &self_box);
111 clutter_actor_get_allocation_box (child, &child_box);
112
113 width = self_box.x2 - self_box.x1;
114 height = self_box.y2 - self_box.y1;
115 child_width = child_box.x2 - child_box.x1;
116 child_height = child_box.y2 - child_box.y1;
117
118 cogl_push_matrix ();
119
120 cogl_clip_push_rectangle (0, 0, width, height);
121 cogl_translate ((int)(0.5 + x_align_factor * (width - child_width)),
122 (int)(0.5 + y_align_factor * (height - child_height)),
123 0);
124
125 clutter_actor_paint (child);
126
127 cogl_clip_pop ();
128
129 cogl_pop_matrix ();
130 }
131
132 static void
133 shell_slicer_paint (ClutterActor *self)
134 {
135 st_widget_paint_background (ST_WIDGET (self));
136
137 shell_slicer_paint_child (SHELL_SLICER (self));
138 }
139
140 static void
141 shell_slicer_pick (ClutterActor *self,
142 const ClutterColor *pick_color)
143 {
144 shell_slicer_paint_child (SHELL_SLICER (self));
145 }
146
147 static void
148 shell_slicer_class_init (ShellSlicerClass *klass)
149 {
150 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
151
152 actor_class->get_preferred_width = shell_slicer_get_preferred_width;
153 actor_class->get_preferred_height = shell_slicer_get_preferred_height;
154 actor_class->allocate = shell_slicer_allocate;
155 actor_class->paint = shell_slicer_paint;
156 actor_class->pick = shell_slicer_pick;
157 }
158
159 static void
160 shell_slicer_init (ShellSlicer *actor)
161 {
162 }