summaryrefslogtreecommitdiff
path: root/src/msm/gtkdisclosurebox.c
blob: b26e869479c9d35fbed8a58159b1bc24de1b41dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * this file Copyright (C) 2001 Havoc Pennington 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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



/* FIXME implementation contains a bunch of cut-and-paste from GtkFrame
 * that would be easy to avoid by adding an "int space_before_label_widget"
 * in GtkFrame that was overridden by subclasses.
 */

/* FIXME the whole GtkFrame derivation idea is fucked since we can't get
 * the click event on the arrow.
 */


#define ARROW_SIZE 12
#define ARROW_PAD 2

enum {
  PROP_0,
  PROP_DISCLOSED,
  PROP_LAST
};

static void gtk_disclosure_box_class_init         (GtkDisclosureBoxClass *klass);
static void gtk_disclosure_box_init               (GtkDisclosureBox      *box);
static void gtk_disclosure_box_set_property       (GObject           *object,
                                                   guint              prop_id,
                                                   const GValue      *value,
                                                   GParamSpec        *pspec);
static void gtk_disclosure_box_get_property       (GObject           *object,
                                                   guint              prop_id,
                                                   GValue            *value,
                                                   GParamSpec        *pspec);

static void gtk_disclosure_box_paint         (GtkWidget      *widget,
                                              GdkRectangle   *area);
static gint gtk_disclosure_box_expose        (GtkWidget      *widget,
                                              GdkEventExpose *event);
static void gtk_disclosure_box_size_request  (GtkWidget      *widget,
                                              GtkRequisition *requisition);
static void gtk_disclosure_box_size_allocate (GtkWidget      *widget,
                                              GtkAllocation  *allocation);

static void gtk_frame_compute_child_allocation (GtkFrame      *frame,
                                                GtkAllocation *child_allocation);

GType
gtk_disclosure_box_get_type (void)
{
  static GType disclosure_box_type = 0;

  if (!disclosure_box_type)
    {
      static const GtkTypeInfo disclosure_box_info =
      {
	"GtkDisclosureBox",
	sizeof (GtkDisclosureBox),
	sizeof (GtkDisclosureBoxClass),
	(GtkClassInitFunc) gtk_disclosure_box_class_init,
	(GtkObjectInitFunc) gtk_disclosure_box_init,
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      disclosure_box_type = gtk_type_unique (gtk_box_get_type (), &disclosure_box_info);
    }

  return disclosure_box_type;
}

static void
gtk_disclosure_box_class_init (GtkDisclosureBoxClass *class)
{
  GtkWidgetClass *widget_class;
  GObjectClass *gobject_class;
  GtkContainerClass *container_class;
  GtkFrameClass *frame_class;
  
  gobject_class = G_OBJECT_CLASS (class);
  widget_class = GTK_WIDGET_CLASS (class);
  container_class = GTK_CONTAINER_CLASS (class);
  frame_class = GTK_FRAME_CLASS (class);
  
  gobject_class->set_property = gtk_disclosure_box_set_property;
  gobject_class->get_property = gtk_disclosure_box_get_property;

  widget_class->size_request = gtk_disclosure_box_size_request;
  widget_class->size_allocate = gtk_disclosure_box_size_allocate;
  widget_class->expose_event = gtk_disclosure_box_expose;
}

static void
gtk_disclosure_box_init (GtkDisclosureBox *disclosure_box)
{

}

GtkWidget*
gtk_disclosure_box_new (const char *label)
{
  return g_object_new (GTK_TYPE_DISCLOSURE_BOX, "label", label, NULL);
}

static void
gtk_disclosure_box_set_property (GObject         *object,
                                 guint            prop_id,
                                 const GValue    *value,
                                 GParamSpec      *pspec)
{
  GtkDisclosureBox *box;

  box = GTK_DISCLOSURE_BOX (object);
  
  switch (prop_id) 
    {
    case PROP_DISCLOSED:
      gtk_disclosure_box_set_disclosed (box,
                                        g_value_get_boolean (value));
      break;
      
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_disclosure_box_get_property (GObject         *object,
                                 guint            prop_id,
                                 GValue          *value,
                                 GParamSpec      *pspec)
{
  GtkDisclosureBox *box;

  box = GTK_DISCLOSURE_BOX (object);

  switch (prop_id)
    {
    case PROP_DISCLOSED:
      g_value_set_boolean (value, box->disclosed);
      break;
      
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

void
gtk_disclosure_box_set_disclosed (GtkDisclosureBox *box,
                                  gboolean          disclosed)
{
  g_return_if_fail (GTK_IS_DISCLOSURE_BOX (box));
  
  disclosed = disclosed != FALSE;

  if (disclosed != box->disclosed)
    {
      box->disclosed = disclosed;
      gtk_widget_queue_resize (GTK_WIDGET (box));
    }
}

gboolean
gtk_disclosure_box_get_disclosed (GtkDisclosureBox *box)
{
  g_return_val_if_fail (GTK_IS_DISCLOSURE_BOX (box), FALSE);

  return box->disclosed;
}


static void
gtk_disclosure_box_paint (GtkWidget    *widget,
                          GdkRectangle *area)
{
  GtkFrame *frame;
  gint x, y, width, height;

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      frame = GTK_FRAME (widget);

      x = frame->child_allocation.x - widget->style->xthickness;
      y = frame->child_allocation.y - widget->style->ythickness;
      width = frame->child_allocation.width + 2 * widget->style->xthickness;
      height =  frame->child_allocation.height + 2 * widget->style->ythickness;

      if (frame->label_widget)
	{
	  GtkRequisition child_requisition;
	  gfloat xalign;
	  gint height_extra;
	  gint x2;

	  gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);

	  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	    xalign = frame->label_xalign;
	  else
	    xalign = 1 - frame->label_xalign;

	  height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
	  y -= height_extra * (1 - frame->label_yalign);
	  height += height_extra * (1 - frame->label_yalign);
	  
	  x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;

	  
	  gtk_paint_shadow_gap (widget->style, widget->window,
				GTK_STATE_NORMAL, frame->shadow_type,
				area, widget, "frame",
				x, y, width, height,
				GTK_POS_TOP, 
				x2 + ARROW_SIZE + ARROW_PAD * 2, child_requisition.width + 2 * LABEL_PAD);

          gtk_paint_arrow (widget->style, widget->window,
                           widget->state, GTK_SHADOW_OUT,
                           area, widget, "arrow",
                           GTK_DISCLOSURE_BOX (widget)->disclosed ?
                           GTK_ARROW_RIGHT : GTK_ARROW_DOWN,
                           TRUE,
                           x2 + ARROW_PAD, y, ARROW_SIZE, ARROW_SIZE);
	}
       else
	 gtk_paint_shadow (widget->style, widget->window,
			   GTK_STATE_NORMAL, frame->shadow_type,
			   area, widget, "frame",
			   x, y, width, height);
    }
}

static gboolean
gtk_disclosure_box_expose (GtkWidget      *widget,
                           GdkEventExpose *event)
{
  if (GTK_WIDGET_DRAWABLE (widget))
    {
      gtk_disclosure_box_paint (widget, &event->area);

      (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
    }

  return FALSE;
}

static void
gtk_disclosure_box_size_request (GtkWidget      *widget,
                                 GtkRequisition *requisition)
{
  GtkFrame *frame = GTK_FRAME (widget);
  GtkBin *bin = GTK_BIN (widget);
  GtkRequisition child_requisition;
  
  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
    {
      gtk_widget_size_request (frame->label_widget, &child_requisition);

      requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD + ARROW_SIZE + ARROW_PAD * 2;
      requisition->height =
	MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
    }
  else
    {
      requisition->width = 0;
      requisition->height = 0;
    }
  
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
      gtk_widget_size_request (bin->child, &child_requisition);

      requisition->width = MAX (requisition->width, child_requisition.width);
      requisition->height += child_requisition.height;
    }

  requisition->width += (GTK_CONTAINER (widget)->border_width +
			 GTK_WIDGET (widget)->style->xthickness) * 2;
  requisition->height += (GTK_CONTAINER (widget)->border_width +
			  GTK_WIDGET (widget)->style->ythickness) * 2;
}

static void
gtk_disclosure_box_size_allocate (GtkWidget     *widget,
                                  GtkAllocation *allocation)
{
  GtkFrame *frame = GTK_FRAME (widget);
  GtkBin *bin = GTK_BIN (widget);
  GtkAllocation new_allocation;

  widget->allocation = *allocation;

  gtk_frame_compute_child_allocation (frame, &new_allocation);
  
  /* If the child allocation changed, that means that the frame is drawn
   * in a new place, so we must redraw the entire widget.
   */
  if (GTK_WIDGET_MAPPED (widget) &&
      (new_allocation.x != frame->child_allocation.x ||
       new_allocation.y != frame->child_allocation.y ||
       new_allocation.width != frame->child_allocation.width ||
       new_allocation.height != frame->child_allocation.height))
    gtk_widget_queue_clear (widget);
  
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    gtk_widget_size_allocate (bin->child, &new_allocation);
  
  frame->child_allocation = new_allocation;
  
  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
    {
      GtkRequisition child_requisition;
      GtkAllocation child_allocation;
      gfloat xalign;

      gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);

      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	xalign = frame->label_xalign;
      else
	xalign = 1 - frame->label_xalign;
      
      child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
	(frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD + ARROW_SIZE + ARROW_PAD * 2;
      child_allocation.width = child_requisition.width;

      child_allocation.y = frame->child_allocation.y - child_requisition.height;
      child_allocation.height = child_requisition.height;

      gtk_widget_size_allocate (frame->label_widget, &child_allocation);
    }
}