static void rut_icon_button_get_preferred_height (void *object, float for_width, float *min_height_p, float *natural_height_p) { RutIconButton *button = object; rut_sizable_get_preferred_height (button->stack, for_width, min_height_p, natural_height_p); }
static void rut_button_get_preferred_height (void *object, float for_width, float *min_height_p, float *natural_height_p) { RutButton *button = object; rut_sizable_get_preferred_height (button->text, for_width, min_height_p, natural_height_p); if (min_height_p) *min_height_p += BUTTON_VPAD; if (natural_height_p) *natural_height_p += BUTTON_VPAD; }
static void rut_bin_get_preferred_height (void *sizable, float for_width, float *min_height_p, float *natural_height_p) { RutBin *bin = RUT_BIN (sizable); float min_height = bin->top_padding + bin->bottom_padding; float natural_height = min_height; if (bin->child) { float child_min_height = 0.0f, child_natural_height = 0.0f; if (for_width != -1.0f) { for_width -= bin->left_padding + bin->right_padding; if (for_width < 0.0f) for_width = 0.0f; } rut_sizable_get_preferred_height (bin->child, for_width, min_height_p ? &child_min_height : NULL, natural_height_p ? &child_natural_height : NULL); min_height += child_min_height; natural_height += child_natural_height; } if (min_height_p) *min_height_p = min_height; if (natural_height_p) *natural_height_p = natural_height; }
static void _rut_button_allocate_cb (RutObject *graphable, void *user_data) { RutButton *button = graphable; float text_min_width, text_natural_width; float text_min_height, text_natural_height; int text_width, text_height; int text_x, text_y; rut_sizable_get_preferred_width (button->text, -1, &text_min_width, &text_natural_width); rut_sizable_get_preferred_height (button->text, -1, &text_min_height, &text_natural_height); if (button->width > (BUTTON_HPAD + text_natural_width)) text_width = text_natural_width; else text_width = MAX (0, button->width - BUTTON_HPAD); if (button->height > (BUTTON_VPAD + text_natural_height)) text_height = text_natural_height; else text_height = MAX (0, button->height - BUTTON_VPAD); rut_sizable_set_size (button->text, text_width, text_height); rut_transform_init_identity (button->text_transform); text_x = (button->width / 2) - (text_width / 2.0); text_y = (button->height / 2) - (text_height / 2.0); rut_transform_translate (button->text_transform, text_x, text_y, 0); }
static void rut_shim_get_preferred_height (void *sizable, float for_width, float *min_height_p, float *natural_height_p) { RutShim *shim = sizable; if (shim->axis == RUT_SHIM_AXIS_X && shim->child) { rut_sizable_get_preferred_height (shim->child, shim->width, min_height_p, natural_height_p); } else { if (min_height_p) *min_height_p = shim->height; if (natural_height_p) *natural_height_p = shim->height; } }
RutIconButton * rut_icon_button_new (RutContext *ctx, const char *label, RutIconButtonPosition label_position, const char *normal_icon, const char *hover_icon, const char *active_icon, const char *disabled_icon) { RutIconButton *button = g_slice_new0 (RutIconButton); float natural_width, natural_height; static CoglBool initialized = FALSE; if (initialized == FALSE) { _rut_icon_button_init_type (); initialized = TRUE; } rut_object_init (RUT_OBJECT (button), &rut_icon_button_type); button->ref_count = 1; rut_list_init (&button->on_click_cb_list); rut_graphable_init (RUT_OBJECT (button)); rut_paintable_init (RUT_OBJECT (button)); button->ctx = ctx; button->state = ICON_BUTTON_STATE_NORMAL; button->stack = rut_stack_new (ctx, 100, 100); button->layout = rut_box_layout_new (ctx, RUT_BOX_LAYOUT_PACKING_TOP_TO_BOTTOM); rut_stack_add (button->stack, button->layout); rut_refable_unref (button->layout); button->bin = rut_bin_new (ctx); rut_box_layout_add (button->layout, TRUE, button->bin); rut_refable_unref (button->bin); button->label_position = label_position; if (label) { RutBin *bin = rut_bin_new (ctx); rut_bin_set_x_position (bin, RUT_BIN_POSITION_CENTER); button->label = rut_text_new_with_text (ctx, NULL, label); rut_bin_set_child (bin, button->label); rut_box_layout_add (button->layout, FALSE, bin); rut_refable_unref (bin); update_layout (button); } rut_icon_button_set_normal (button, normal_icon); rut_icon_button_set_hover (button, hover_icon); rut_icon_button_set_active (button, active_icon); rut_icon_button_set_disabled (button, disabled_icon); button->input_region = rut_input_region_new_rectangle (0, 0, 100, 100, _rut_icon_button_input_cb, button); rut_stack_add (button->stack, button->input_region); rut_refable_unref (button->input_region); rut_sizable_get_preferred_width (button->stack, -1, NULL, &natural_width); rut_sizable_get_preferred_height (button->stack, natural_width, NULL, &natural_height); rut_sizable_set_size (button->stack, natural_width, natural_height); rut_graphable_add_child (button, button->stack); return button; }
static void allocate_cb (RutObject *graphable, void *user_data) { RutBin *bin = RUT_BIN (graphable); if (bin->child) { float child_width, child_height; float child_x = bin->left_padding; float child_y = bin->top_padding; float available_width = bin->width - bin->left_padding - bin->right_padding; float available_height = bin->height - bin->top_padding - bin->bottom_padding; rut_sizable_get_preferred_width (bin->child, -1, /* for_height */ NULL, /* min_width_p */ &child_width); if (child_width > available_width) child_width = available_width; switch (bin->x_position) { case RUT_BIN_POSITION_BEGIN: break; case RUT_BIN_POSITION_CENTER: if (child_width < available_width) child_x = nearbyintf (bin->width / 2.0f - child_width / 2.0f); break; case RUT_BIN_POSITION_END: if (child_width < available_width) child_x = bin->width - bin->right_padding - child_width; break; case RUT_BIN_POSITION_EXPAND: child_width = available_width; break; } rut_sizable_get_preferred_height (bin->child, child_width, /* for_width */ NULL, /* min_height_p */ &child_height); if (child_height > available_height) child_height = available_height; switch (bin->y_position) { case RUT_BIN_POSITION_BEGIN: break; case RUT_BIN_POSITION_CENTER: if (child_height < available_height) child_y = nearbyintf (bin->height / 2.0f - child_height / 2.0f); break; case RUT_BIN_POSITION_END: if (child_height < available_height) child_y = bin->height - bin->bottom_padding - child_height; break; case RUT_BIN_POSITION_EXPAND: child_height = available_height; break; } rut_transform_init_identity (bin->child_transform); rut_transform_translate (bin->child_transform, child_x, child_y, 0.0f); rut_sizable_set_size (bin->child, child_width, child_height); } }