gdl-dock-item-button-image.c 5.23 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 3 4 5
 *
 * gdl-dock-item-button-image.c
 *
 * Author: Joel Holdsworth <joel@airwebreathe.org.uk>
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20

21 22 23 24
#include "gdl-dock-item-button-image.h"

#include <math.h>

25 26 27 28
/**
 * SECTION:gdl-dock-item-button-image
 * @title: GdlDockItemButtonImage
 * @short_description: A widget displaying an image used in dock grip
29
 * @stability: Private
30 31 32 33 34 35 36 37
 * @see_also: GtlDockItemGrip
 *
 * The #GdlDockItemButtonImage displays a small image which could be a cross
 * (close) or a triangle (iconify). These images are used for buttons in the
 * #GdlDockItemGrip object.
 */


38
#define ICON_SIZE 13
39

40 41 42
G_DEFINE_TYPE (GdlDockItemButtonImage,
               gdl_dock_item_button_image,
               GTK_TYPE_WIDGET);
43

44
static gboolean
45 46
gdl_dock_item_button_image_draw (GtkWidget      *widget,
                                 cairo_t *cr)
47 48
{
    GdlDockItemButtonImage *button_image;
49 50
    GtkStyleContext *context;
    GdkRGBA color;
51

52
    g_return_val_if_fail (widget != NULL, 0);
53
    button_image = GDL_DOCK_ITEM_BUTTON_IMAGE (widget);
54

55 56
    /* Set up the pen */
    cairo_set_line_width(cr, 1.0);
57

58 59 60 61
    context = gtk_widget_get_style_context (widget);
    gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
    color.alpha = 0.55;
    gdk_cairo_set_source_rgba(cr, &color);
62 63 64 65 66 67 68 69 70 71 72

    /* Draw the icon border */
    cairo_move_to (cr, 10.5, 2.5);
    cairo_arc (cr, 10.5, 4.5, 2, -0.5 * M_PI, 0);
    cairo_line_to (cr, 12.5, 10.5);
    cairo_arc (cr, 10.5, 10.5, 2, 0, 0.5 * M_PI);
    cairo_line_to (cr, 4.5, 12.5);
    cairo_arc (cr, 4.5, 10.5, 2, 0.5 * M_PI, M_PI);
    cairo_line_to (cr, 2.5, 4.5);
    cairo_arc (cr, 4.5, 4.5, 2, M_PI, 1.5 * M_PI);
    cairo_close_path (cr);
73

74
    cairo_stroke (cr);
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    /* Draw the icon */
    cairo_new_path (cr);

    switch(button_image->image_type) {
    case GDL_DOCK_ITEM_BUTTON_IMAGE_CLOSE:
        cairo_move_to (cr, 4.0, 5.5);
        cairo_line_to (cr, 4.0, 5.5);
        cairo_line_to (cr, 6.0, 7.5);
        cairo_line_to (cr, 4.0, 9.5);
        cairo_line_to (cr, 5.5, 11.0);
        cairo_line_to (cr, 7.5, 9.0);
        cairo_line_to (cr, 9.5, 11.0);
        cairo_line_to (cr, 11.0, 9.5);
        cairo_line_to (cr, 9.0, 7.5);
        cairo_line_to (cr, 11.0, 5.5);
        cairo_line_to (cr, 9.5, 4.0);
        cairo_line_to (cr, 7.5, 6.0);
        cairo_line_to (cr, 5.5, 4.0);
        cairo_close_path (cr);
        break;
96

97 98 99 100 101 102 103 104 105 106 107 108 109
    case GDL_DOCK_ITEM_BUTTON_IMAGE_ICONIFY:
        if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL) {
            cairo_move_to (cr, 4.5, 7.5);
            cairo_line_to (cr, 10.0, 4.75);
            cairo_line_to (cr, 10.0, 10.25);
            cairo_close_path (cr);
        } else {
            cairo_move_to (cr, 10.5, 7.5);
            cairo_line_to (cr, 5, 4.75);
            cairo_line_to (cr, 5, 10.25);
            cairo_close_path (cr);
        }
        break;
110

111 112 113
    default:
        break;
    }
114

115
    cairo_fill (cr);
116

117
    return FALSE;
118 119 120
}

static void
121
gdl_dock_item_button_image_init (
122 123
    GdlDockItemButtonImage *button_image)
{
124
    gtk_widget_set_has_window (GTK_WIDGET (button_image), FALSE);
125 126 127
}

static void
128 129 130
gdl_dock_item_button_image_get_preferred_width (GtkWidget *widget,
                                                gint      *minimum,
                                                gint      *natural)
131
{
132 133 134 135 136 137 138 139 140
   *minimum = *natural = ICON_SIZE;
}

static void
gdl_dock_item_button_image_get_preferred_height (GtkWidget *widget,
                                                 gint      *minimum,
                                                 gint      *natural)
{
   *minimum = *natural = ICON_SIZE;
141 142 143 144 145 146 147 148
}

static void
gdl_dock_item_button_image_class_init (
    GdlDockItemButtonImageClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
149

150 151
    widget_class->draw =
        gdl_dock_item_button_image_draw;
152 153 154 155
    widget_class->get_preferred_width =
        gdl_dock_item_button_image_get_preferred_width;
    widget_class->get_preferred_height =
        gdl_dock_item_button_image_get_preferred_height;
156 157 158 159 160 161
}

/* ----- Public interface ----- */

/**
 * gdl_dock_item_button_image_new:
162 163
 * @image_type: Specifies what type of image the widget should display
 *
164
 * Creates a new GDL dock button image object.
165 166 167
 *
 * Returns: The newly created dock item button image widget
 */
168 169 170 171 172 173 174 175 176
GtkWidget*
gdl_dock_item_button_image_new (GdlDockItemButtonImageType image_type)
{
    GdlDockItemButtonImage *button_image = g_object_new (
        GDL_TYPE_DOCK_ITEM_BUTTON_IMAGE, NULL);
    button_image->image_type = image_type;

    return GTK_WIDGET (button_image);
}