gtkseparator.c 8.44 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25
#include "config.h"
26

27
#include "gtkorientableprivate.h"
Elliot Lee's avatar
Elliot Lee committed
28
#include "gtkseparator.h"
29
#include "gtkprivate.h"
30
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
31

32 33
/**
 * SECTION:gtkseparator
34
 * @Short_description: A separator widget
35 36
 * @Title: GtkSeparator
 *
37
 * GtkSeparator is a horizontal or vertical separator widget, depending on the 
38
 * value of the #GtkOrientable:orientation property, used to group the widgets within a
39 40
 * window. It displays a line with a shadow to make it appear sunken into the 
 * interface.
41 42 43
 */


44
struct _GtkSeparatorPrivate
45 46 47 48 49
{
  GtkOrientation orientation;
};


50 51 52 53
enum {
  PROP_0,
  PROP_ORIENTATION
};
54 55 56 57 58 59 60 61 62

static void       gtk_separator_set_property (GObject        *object,
                                              guint           prop_id,
                                              const GValue   *value,
                                              GParamSpec     *pspec);
static void       gtk_separator_get_property (GObject        *object,
                                              guint           prop_id,
                                              GValue         *value,
                                              GParamSpec     *pspec);
63 64 65 66 67 68 69 70
static void       gtk_separator_get_preferred_width
                                             (GtkWidget      *widget,
                                              gint           *minimum,
                                              gint           *natural);
static void       gtk_separator_get_preferred_height
                                             (GtkWidget      *widget,
                                              gint           *minimum,
                                              gint           *natural);
71 72
static gboolean   gtk_separator_draw         (GtkWidget      *widget,
                                              cairo_t        *cr);
73 74


75
G_DEFINE_TYPE_WITH_CODE (GtkSeparator, gtk_separator, GTK_TYPE_WIDGET,
76 77
                         G_ADD_PRIVATE (GtkSeparator)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
78

Elliot Lee's avatar
Elliot Lee committed
79 80 81 82

static void
gtk_separator_class_init (GtkSeparatorClass *class)
{
83 84 85 86 87 88
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->set_property = gtk_separator_set_property;
  object_class->get_property = gtk_separator_get_property;

89 90 91
  widget_class->get_preferred_width = gtk_separator_get_preferred_width;
  widget_class->get_preferred_height = gtk_separator_get_preferred_height;

Matthias Clasen's avatar
Matthias Clasen committed
92
  widget_class->draw = gtk_separator_draw;
93 94

  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_SEPARATOR);
95

96
  g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
Elliot Lee's avatar
Elliot Lee committed
97 98 99 100 101
}

static void
gtk_separator_init (GtkSeparator *separator)
{
102
  GtkStyleContext *context;
103

104 105
  separator->priv = gtk_separator_get_instance_private (separator);
  separator->priv->orientation = GTK_ORIENTATION_HORIZONTAL;
106

107
  gtk_widget_set_has_window (GTK_WIDGET (separator), FALSE);
108

109 110
  context = gtk_widget_get_style_context (GTK_WIDGET (separator));
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
111 112 113 114 115 116 117 118
}

static void
gtk_separator_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
119
  GtkSeparator *separator = GTK_SEPARATOR (object);
120
  GtkSeparatorPrivate *private = separator->priv;
121 122 123 124 125

  switch (prop_id)
    {
    case PROP_ORIENTATION:
      private->orientation = g_value_get_enum (value);
126
      _gtk_orientable_set_style_classes (GTK_ORIENTABLE (object));
127 128 129 130 131 132 133 134 135 136 137 138 139 140
      gtk_widget_queue_resize (GTK_WIDGET (object));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_separator_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
141
  GtkSeparator *separator = GTK_SEPARATOR (object);
142
  GtkSeparatorPrivate *private = separator->priv;
143 144 145 146 147 148 149 150 151 152 153 154 155

  switch (prop_id)
    {
    case PROP_ORIENTATION:
      g_value_set_enum (value, private->orientation);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
156 157 158 159
gtk_separator_get_preferred_size (GtkWidget      *widget,
                                  GtkOrientation  orientation,
                                  gint           *minimum,
                                  gint           *natural)
160
{
161
  GtkSeparator *separator = GTK_SEPARATOR (widget);
162
  GtkSeparatorPrivate *private = separator->priv;
163 164 165
  gboolean wide_sep;
  gint     sep_width;
  gint     sep_height;
166 167

  gtk_widget_style_get (widget,
168 169 170
                        "wide-separators",  &wide_sep,
                        "separator-width",  &sep_width,
                        "separator-height", &sep_height,
171 172
                        NULL);

173
  if (orientation == private->orientation)
174
    {
175 176 177 178
      *minimum = *natural = 1;
    }
  else if (orientation == GTK_ORIENTATION_VERTICAL)
    {
179
      *minimum = *natural = wide_sep ? sep_height : 1;
180 181 182
    }
  else
    {
183
      *minimum = *natural = wide_sep ? sep_width : 1;
184 185 186
    }
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static void
gtk_separator_get_preferred_width (GtkWidget *widget,
                                   gint      *minimum,
                                   gint      *natural)
{
  gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
}

static void
gtk_separator_get_preferred_height (GtkWidget *widget,
                                    gint      *minimum,
                                    gint      *natural)
{
  gtk_separator_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
}

203
static gboolean
204 205
gtk_separator_draw (GtkWidget *widget,
                    cairo_t   *cr)
206
{
207
  GtkSeparator *separator = GTK_SEPARATOR (widget);
208
  GtkSeparatorPrivate *private = separator->priv;
209
  GtkStyleContext *context;
210
  gboolean wide_separators;
211 212 213
  gint separator_width;
  gint separator_height;
  int width, height;
214 215 216 217 218 219 220

  gtk_widget_style_get (widget,
                        "wide-separators",  &wide_separators,
                        "separator-width",  &separator_width,
                        "separator-height", &separator_height,
                        NULL);

221
  context = gtk_widget_get_style_context (widget);
222 223
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
224

225 226 227
  if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      if (wide_separators)
228 229 230
        gtk_render_frame (context, cr,
                          0, (height - separator_height) / 2,
                          width, separator_height);
231
      else
232
        gtk_render_line (context, cr,
233 234
                         0, height / 2,
                         width - 1, height / 2);
235 236 237 238
    }
  else
    {
      if (wide_separators)
239 240 241
        gtk_render_frame (context, cr,
                          (width - separator_width) / 2, 0,
                          separator_width, height);
242
      else
243
        gtk_render_line (context, cr,
244 245
                         width / 2, 0,
                         width / 2, height - 1);
246 247 248 249 250 251 252
    }

  return FALSE;
}

/**
 * gtk_separator_new:
253
 * @orientation: the separator’s orientation.
254 255 256 257 258
 *
 * Creates a new #GtkSeparator with the given orientation.
 *
 * Return value: a new #GtkSeparator.
 *
259
 * Since: 3.0
260
 */
261 262 263 264 265 266
GtkWidget *
gtk_separator_new (GtkOrientation orientation)
{
  return g_object_new (GTK_TYPE_SEPARATOR,
                       "orientation", orientation,
                       NULL);
Elliot Lee's avatar
Elliot Lee committed
267
}