1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "pspp-widget-facade.h"
27 inset_rectangle (const GdkRectangle *src,
28 const GtkBorder *inset,
31 dst->x = src->x + inset->left;
32 dst->y = src->y + inset->top;
33 dst->width = MAX (1, src->width - inset->left - inset->right);
34 dst->height = MAX (1, src->height - inset->top - inset->bottom);
38 thicken_border (gint x, gint y, GtkBorder *border)
47 facade_get_style (GtkWidget *base,
51 GString path, class_path;
57 gtk_widget_path (base, NULL, &path.str, NULL);
58 path.len = path.allocated_len = strlen (path.str);
60 gtk_widget_class_path (base, NULL, &class_path.str, NULL);
61 class_path.len = class_path.allocated_len = strlen (class_path.str);
63 va_start (args, type1);
64 for (type = final_type = type1; type != 0; type = va_arg (args, GType))
66 const gchar *type_name = g_type_name (type);
67 g_string_append_printf (&path, ".%s", type_name);
68 g_string_append_printf (&class_path, ".%s", type_name);
73 style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (base),
74 path.str, class_path.str, final_type);
77 free (class_path.str);
83 facade_hbox_get_base_size_request (gint border_width,
86 GtkRequisition *request)
88 request->width = border_width * 2;
90 request->width += spacing * (n_children - 1);
92 request->height = border_width * 2;
96 facade_hbox_add_child_size_request (gint hbox_border_width,
97 const GtkRequisition *child_request,
99 GtkRequisition *request)
101 request->width += child_request->width + child_padding * 2;
102 request->height = MAX (request->height,
103 hbox_border_width * 2 + child_request->height);
107 facade_arrow_get_size_request (gint xpad,
109 GtkRequisition *request)
111 #define MIN_ARROW_SIZE 15
112 request->width = MIN_ARROW_SIZE + xpad * 2;
113 request->height = MIN_ARROW_SIZE + ypad * 2;
117 facade_alignment_get_size_request (gint border_width,
122 const GtkRequisition *child_request,
123 GtkRequisition *request)
125 request->width = (border_width * 2 + padding_left + padding_right
126 + child_request->width);
127 request->height = (border_width * 2 + padding_top + padding_bottom
128 + child_request->height);
132 facade_label_get_size_request (gint xpad,
136 GtkRequisition *request)
140 layout = facade_label_get_layout (base, text);
141 facade_label_get_size_request_from_layout (xpad, ypad, layout, request);
142 g_object_unref (layout);
146 facade_label_get_size_request_from_layout (gint xpad,
149 GtkRequisition *request)
151 PangoRectangle logical_rect;
153 pango_layout_get_extents (layout, NULL, &logical_rect);
154 request->width = xpad * 2 + PANGO_PIXELS (logical_rect.width);
155 request->height = ypad * 2 + PANGO_PIXELS (logical_rect.height);
159 facade_label_get_layout (GtkWidget *base,
162 PangoAlignment alignment;
166 rtl = gtk_widget_get_direction (base) == GTK_TEXT_DIR_RTL;
167 alignment = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
169 layout = gtk_widget_create_pango_layout (base, text);
170 pango_layout_set_alignment (layout, alignment);
176 facade_button_get_inner_border (GtkStyle *button_style,
177 GtkBorder *inner_border)
179 GtkBorder *tmp_border;
181 gtk_style_get (button_style, GTK_TYPE_BUTTON,
182 "inner-border", &tmp_border,
187 *inner_border = *tmp_border;
188 gtk_border_free (tmp_border);
192 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
193 *inner_border = default_inner_border;
198 facade_button_get_size_request (gint border_width,
200 GtkStyle *button_style,
201 const GtkRequisition *child_request,
202 GtkRequisition *request)
204 GtkBorder inner_border;
209 gtk_style_get (button_style, GTK_TYPE_BUTTON,
210 "focus-line-width", &focus_width,
211 "focus-padding", &focus_pad,
213 facade_button_get_inner_border (button_style, &inner_border);
215 common_width = 2 * (border_width + focus_width + focus_pad);
216 request->width = (common_width
217 + 2 * button_style->xthickness
218 + inner_border.left + inner_border.right
219 + child_request->width);
220 request->height = (common_width
221 + 2 * button_style->ythickness
222 + inner_border.top + inner_border.bottom
223 + child_request->height);
227 facade_button_get_focus_inset (gint border_width,
229 GtkStyle *button_style,
230 GtkBorder *focus_inset)
232 facade_button_get_inner_border (button_style, focus_inset);
233 thicken_border (border_width + button_style->xthickness,
234 border_width + button_style->ythickness,
239 facade_button_get_label_inset (gint border_width,
241 GtkStyle *button_style,
242 GtkBorder *label_inset)
247 facade_button_get_focus_inset (border_width, base, button_style,
250 gtk_style_get (button_style, GTK_TYPE_BUTTON,
251 "focus-line-width", &focus_width,
252 "focus-padding", &focus_pad,
254 thicken_border (focus_width + focus_pad,
255 focus_width + focus_pad,
260 get_layout_location (GtkWidget *base,
261 const GdkRectangle *label_area,
270 PangoRectangle logical;
273 if (gtk_widget_get_direction (base) == GTK_TEXT_DIR_LTR)
276 xalign = 1.0 - xalign;
278 pango_layout_get_pixel_extents (layout, NULL, &logical);
280 facade_label_get_size_request_from_layout (xpad, ypad, layout, &req);
282 *x = floor (label_area->x + xpad + xalign * (label_area->width - req.width));
284 if (gtk_widget_get_direction (base) == GTK_TEXT_DIR_LTR)
285 *x = MAX (*x, label_area->x + xpad);
287 *x = MIN (*x, label_area->x + label_area->width - xpad);
290 /* bgo#315462 - For single-line labels, *do* align the requisition with
291 * respect to the allocation, even if we are under-allocated. For multi-line
292 * labels, always show the top of the text when they are under-allocated.
293 * The rationale is this:
295 * - Single-line labels appear in GtkButtons, and it is very easy to get them
296 * to be smaller than their requisition. The button may clip the label,
297 * but the label will still be able to show most of itself and the focus
298 * rectangle. Also, it is fairly easy to read a single line of clipped
301 * - Multi-line labels should not be clipped to showing "something in the
302 * middle". You want to read the first line, at least, to get some
305 if (pango_layout_get_line_count (layout) == 1)
306 *y = floor (label_area->y + ypad
307 + (label_area->height - req.height) * yalign);
309 *y = floor (label_area->y + ypad
310 + MAX (((label_area->height - req.height) * yalign),
315 facade_button_render (GtkWidget *base,
317 GdkRectangle *expose_area,
319 GdkRectangle *button_area,
321 GtkStyle *button_style,
322 GtkStateType state_type,
324 GtkStyle *label_style,
331 GdkRectangle label_area;
336 /* Paint the button. */
337 gtk_paint_box (button_style, window,
339 GTK_SHADOW_OUT, expose_area, base, "button",
340 button_area->x + border_width,
341 button_area->y + border_width,
342 button_area->width - border_width * 2,
343 button_area->height - border_width * 2);
345 /* Figure out where the label should go. */
346 facade_button_get_label_inset (border_width, base, button_style, &inset);
347 inset_rectangle (button_area, &inset, &label_area);
349 /* Paint the label. */
350 layout = facade_label_get_layout (base, label);
351 get_layout_location (base, &label_area, layout, xpad, ypad, xalign, yalign,
353 gtk_paint_layout (label_style, window, state_type, FALSE, expose_area,
354 base, "label", x, y, layout);
355 g_object_unref (layout);