~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/widgets/gtkhwrapbox.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GTK - The GIMP Toolkit
 
2
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 
3
 *
 
4
 * GtkHWrapBox: Horizontal wrapping box widget
 
5
 * Copyright (C) 1999 Tim Janik
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "gtkhwrapbox.h"
 
26
 
 
27
#include "libgimpmath/gimpmath.h"
 
28
 
 
29
 
 
30
/* --- prototypes --- */
 
31
static void    gtk_hwrap_box_class_init    (GtkHWrapBoxClass   *klass);
 
32
static void    gtk_hwrap_box_init          (GtkHWrapBox        *hwbox);
 
33
static void    gtk_hwrap_box_size_request  (GtkWidget          *widget,
 
34
                                            GtkRequisition     *requisition);
 
35
static void    gtk_hwrap_box_size_allocate (GtkWidget          *widget,
 
36
                                            GtkAllocation      *allocation);
 
37
static GSList* reverse_list_row_children   (GtkWrapBox         *wbox,
 
38
                                            GtkWrapBoxChild   **child_p,
 
39
                                            GtkAllocation      *area,
 
40
                                            guint              *max_height,
 
41
                                            gboolean           *can_vexpand);
 
42
 
 
43
 
 
44
/* --- variables --- */
 
45
static gpointer parent_class = NULL;
 
46
 
 
47
 
 
48
/* --- functions --- */
 
49
GType
 
50
gtk_hwrap_box_get_type (void)
 
51
{
 
52
  static GType hwrap_box_type = 0;
 
53
  
 
54
  if (!hwrap_box_type)
 
55
    {
 
56
      static const GTypeInfo hwrap_box_info =
 
57
      {
 
58
        sizeof (GtkHWrapBoxClass),
 
59
        NULL,           /* base_init */
 
60
        NULL,           /* base_finalize */
 
61
        (GClassInitFunc) gtk_hwrap_box_class_init,
 
62
        NULL,           /* class_finalize */
 
63
        NULL,           /* class_data */
 
64
        sizeof (GtkHWrapBox),
 
65
        0,              /* n_preallocs */
 
66
        (GInstanceInitFunc) gtk_hwrap_box_init,
 
67
      };
 
68
      
 
69
      hwrap_box_type = g_type_register_static (GTK_TYPE_WRAP_BOX, "GtkHWrapBox",
 
70
                                               &hwrap_box_info, 0);
 
71
    }
 
72
  
 
73
  return hwrap_box_type;
 
74
}
 
75
 
 
76
static void
 
77
gtk_hwrap_box_class_init (GtkHWrapBoxClass *class)
 
78
{
 
79
  GObjectClass *object_class;
 
80
  GtkWidgetClass *widget_class;
 
81
  GtkContainerClass *container_class;
 
82
  GtkWrapBoxClass *wrap_box_class;
 
83
  
 
84
  object_class = G_OBJECT_CLASS (class);
 
85
  widget_class = GTK_WIDGET_CLASS (class);
 
86
  container_class = GTK_CONTAINER_CLASS (class);
 
87
  wrap_box_class = GTK_WRAP_BOX_CLASS (class);
 
88
  
 
89
  parent_class = g_type_class_peek_parent (class);
 
90
  
 
91
  widget_class->size_request = gtk_hwrap_box_size_request;
 
92
  widget_class->size_allocate = gtk_hwrap_box_size_allocate;
 
93
 
 
94
  wrap_box_class->rlist_line_children = reverse_list_row_children;
 
95
}
 
96
 
 
97
static void
 
98
gtk_hwrap_box_init (GtkHWrapBox *hwbox)
 
99
{
 
100
  hwbox->max_child_width = 0;
 
101
  hwbox->max_child_height = 0;
 
102
}
 
103
 
 
104
GtkWidget*
 
105
gtk_hwrap_box_new (gboolean homogeneous)
 
106
{
 
107
  return g_object_new (GTK_TYPE_HWRAP_BOX, "homogeneous", homogeneous, NULL);
 
108
}
 
109
 
 
110
static inline void
 
111
get_child_requisition (GtkWrapBox     *wbox,
 
112
                       GtkWidget      *child,
 
113
                       GtkRequisition *child_requisition)
 
114
{
 
115
  if (wbox->homogeneous)
 
116
    {
 
117
      GtkHWrapBox *hwbox = GTK_HWRAP_BOX (wbox);
 
118
      
 
119
      child_requisition->width = hwbox->max_child_width;
 
120
      child_requisition->height = hwbox->max_child_height;
 
121
    }
 
122
  else
 
123
    gtk_widget_get_child_requisition (child, child_requisition);
 
124
}
 
125
 
 
126
static gfloat
 
127
get_layout_size (GtkHWrapBox *this,
 
128
                 guint        max_width,
 
129
                 guint       *width_inc)
 
130
{
 
131
  GtkWrapBox *wbox = GTK_WRAP_BOX (this);
 
132
  GtkWrapBoxChild *child;
 
133
  guint n_rows, left_over = 0, total_height = 0;
 
134
  gboolean last_row_filled = TRUE;
 
135
 
 
136
  *width_inc = this->max_child_width + 1;
 
137
 
 
138
  n_rows = 0;
 
139
  for (child = wbox->children; child; child = child->next)
 
140
    {
 
141
      GtkWrapBoxChild *row_child;
 
142
      GtkRequisition child_requisition;
 
143
      guint row_width, row_height, n = 1;
 
144
 
 
145
      if (!GTK_WIDGET_VISIBLE (child->widget))
 
146
        continue;
 
147
 
 
148
      get_child_requisition (wbox, child->widget, &child_requisition);
 
149
      if (!last_row_filled)
 
150
        *width_inc = MIN (*width_inc, child_requisition.width - left_over);
 
151
      row_width = child_requisition.width;
 
152
      row_height = child_requisition.height;
 
153
      for (row_child = child->next; row_child && n < wbox->child_limit; row_child = row_child->next)
 
154
        {
 
155
          if (GTK_WIDGET_VISIBLE (row_child->widget))
 
156
            {
 
157
              get_child_requisition (wbox, row_child->widget, &child_requisition);
 
158
              if (row_width + wbox->hspacing + child_requisition.width > max_width)
 
159
                break;
 
160
              row_width += wbox->hspacing + child_requisition.width;
 
161
              row_height = MAX (row_height, child_requisition.height);
 
162
              n++;
 
163
            }
 
164
          child = row_child;
 
165
        }
 
166
      last_row_filled = n >= wbox->child_limit;
 
167
      left_over = last_row_filled ? 0 : max_width - (row_width + wbox->hspacing);
 
168
      total_height += (n_rows ? wbox->vspacing : 0) + row_height;
 
169
      n_rows++;
 
170
    }
 
171
 
 
172
  if (*width_inc > this->max_child_width)
 
173
    *width_inc = 0;
 
174
  
 
175
  return MAX (total_height, 1);
 
176
}
 
177
 
 
178
static void
 
179
gtk_hwrap_box_size_request (GtkWidget      *widget,
 
180
                            GtkRequisition *requisition)
 
181
{
 
182
  GtkHWrapBox *this = GTK_HWRAP_BOX (widget);
 
183
  GtkWrapBox *wbox = GTK_WRAP_BOX (widget);
 
184
  GtkWrapBoxChild *child;
 
185
  gfloat ratio_dist, layout_width = 0;
 
186
  guint row_inc = 0;
 
187
  
 
188
  g_return_if_fail (requisition != NULL);
 
189
  
 
190
  requisition->width = 0;
 
191
  requisition->height = 0;
 
192
  this->max_child_width = 0;
 
193
  this->max_child_height = 0;
 
194
 
 
195
  /* size_request all children */
 
196
  for (child = wbox->children; child; child = child->next)
 
197
    if (GTK_WIDGET_VISIBLE (child->widget))
 
198
      {
 
199
        GtkRequisition child_requisition;
 
200
        
 
201
        gtk_widget_size_request (child->widget, &child_requisition);
 
202
 
 
203
        this->max_child_width = MAX (this->max_child_width, child_requisition.width);
 
204
        this->max_child_height = MAX (this->max_child_height, child_requisition.height);
 
205
      }
 
206
 
 
207
  /* figure all possible layouts */
 
208
  ratio_dist = 32768;
 
209
  layout_width = this->max_child_width;
 
210
  do
 
211
    {
 
212
      gfloat layout_height;
 
213
      gfloat ratio, dist;
 
214
 
 
215
      layout_width += row_inc;
 
216
      layout_height = get_layout_size (this, layout_width, &row_inc);
 
217
      ratio = layout_width / layout_height;             /*<h2v-skip>*/
 
218
      dist = MAX (ratio, wbox->aspect_ratio) - MIN (ratio, wbox->aspect_ratio);
 
219
      if (dist < ratio_dist)
 
220
        {
 
221
          ratio_dist = dist;
 
222
          requisition->width = layout_width;
 
223
          requisition->height = layout_height;
 
224
        }
 
225
      
 
226
      /* g_print ("ratio for width %d height %d = %f\n",
 
227
         (gint) layout_width,
 
228
         (gint) layout_height,
 
229
         ratio);
 
230
      */
 
231
    }
 
232
  while (row_inc);
 
233
 
 
234
  requisition->width += GTK_CONTAINER (wbox)->border_width * 2; /*<h2v-skip>*/
 
235
  requisition->height += GTK_CONTAINER (wbox)->border_width * 2; /*<h2v-skip>*/
 
236
  /* g_print ("choosen: width %d, height %d\n",
 
237
     requisition->width,
 
238
     requisition->height);
 
239
  */
 
240
}
 
241
 
 
242
static GSList*
 
243
reverse_list_row_children (GtkWrapBox       *wbox,
 
244
                           GtkWrapBoxChild **child_p,
 
245
                           GtkAllocation    *area,
 
246
                           guint            *max_child_size,
 
247
                           gboolean         *expand_line)
 
248
{
 
249
  GSList *slist = NULL;
 
250
  guint width = 0, row_width = area->width;
 
251
  GtkWrapBoxChild *child = *child_p;
 
252
  
 
253
  *max_child_size = 0;
 
254
  *expand_line = FALSE;
 
255
  
 
256
  while (child && !GTK_WIDGET_VISIBLE (child->widget))
 
257
    {
 
258
      *child_p = child->next;
 
259
      child = *child_p;
 
260
    }
 
261
  
 
262
  if (child)
 
263
    {
 
264
      GtkRequisition child_requisition;
 
265
      guint n = 1;
 
266
      
 
267
      get_child_requisition (wbox, child->widget, &child_requisition);
 
268
      width += child_requisition.width;
 
269
      *max_child_size = MAX (*max_child_size, child_requisition.height);
 
270
      *expand_line |= child->vexpand;
 
271
      slist = g_slist_prepend (slist, child);
 
272
      *child_p = child->next;
 
273
      child = *child_p;
 
274
      
 
275
      while (child && n < wbox->child_limit)
 
276
        {
 
277
          if (GTK_WIDGET_VISIBLE (child->widget))
 
278
            {
 
279
              get_child_requisition (wbox, child->widget, &child_requisition);
 
280
              if (width + wbox->hspacing + child_requisition.width > row_width ||
 
281
                  child->wrapped)
 
282
                break;
 
283
              width += wbox->hspacing + child_requisition.width;
 
284
              *max_child_size = MAX (*max_child_size, child_requisition.height);
 
285
              *expand_line |= child->vexpand;
 
286
              slist = g_slist_prepend (slist, child);
 
287
              n++;
 
288
            }
 
289
          *child_p = child->next;
 
290
          child = *child_p;
 
291
        }
 
292
    }
 
293
  
 
294
  return slist;
 
295
}
 
296
 
 
297
static void
 
298
layout_row (GtkWrapBox    *wbox,
 
299
            GtkAllocation *area,
 
300
            GSList        *children,
 
301
            guint          children_per_line,
 
302
            gboolean       vexpand)
 
303
{
 
304
  GSList *slist;
 
305
  guint n_children = 0, n_expand_children = 0, have_expand_children = 0;
 
306
  gint total_width = 0;
 
307
  gfloat x, width, extra;
 
308
  GtkAllocation child_allocation;
 
309
  
 
310
  for (slist = children; slist; slist = slist->next)
 
311
    {
 
312
      GtkWrapBoxChild *child = slist->data;
 
313
      GtkRequisition child_requisition;
 
314
      
 
315
      n_children++;
 
316
      if (child->hexpand)
 
317
        n_expand_children++;
 
318
      
 
319
      get_child_requisition (wbox, child->widget, &child_requisition);
 
320
      total_width += child_requisition.width;
 
321
    }
 
322
  
 
323
  width = MAX (1, area->width - (n_children - 1) * wbox->hspacing);
 
324
  if (width > total_width)
 
325
    extra = width - total_width;
 
326
  else
 
327
    extra = 0;
 
328
  have_expand_children = n_expand_children && extra;
 
329
  
 
330
  x = area->x;
 
331
  if (wbox->homogeneous)
 
332
    {
 
333
      width = MAX (1, area->width - (children_per_line - 1) * wbox->hspacing);
 
334
      width /= ((gdouble) children_per_line);
 
335
      extra = 0;
 
336
    }
 
337
  else if (have_expand_children && wbox->justify != GTK_JUSTIFY_FILL)
 
338
    {
 
339
      width = extra;
 
340
      extra /= ((gdouble) n_expand_children);
 
341
    }
 
342
  else
 
343
    {
 
344
      if (wbox->justify == GTK_JUSTIFY_FILL)
 
345
        {
 
346
          width = extra;
 
347
          have_expand_children = TRUE;
 
348
          n_expand_children = n_children;
 
349
          extra /= ((gdouble) n_expand_children);
 
350
        }
 
351
      else if (wbox->justify == GTK_JUSTIFY_CENTER)
 
352
        {
 
353
          x += extra / 2;
 
354
          width = 0;
 
355
          extra = 0;
 
356
        }
 
357
      else if (wbox->justify == GTK_JUSTIFY_LEFT)
 
358
        {
 
359
          width = 0;
 
360
          extra = 0;
 
361
        }
 
362
      else if (wbox->justify == GTK_JUSTIFY_RIGHT)
 
363
        {
 
364
          x += extra;
 
365
          width = 0;
 
366
          extra = 0;
 
367
        }
 
368
    }
 
369
  
 
370
  n_children = 0;
 
371
  for (slist = children; slist; slist = slist->next)
 
372
    {
 
373
      GtkWrapBoxChild *child = slist->data;
 
374
      
 
375
      child_allocation.x = x;
 
376
      child_allocation.y = area->y;
 
377
      if (wbox->homogeneous)
 
378
        {
 
379
          child_allocation.height = area->height;
 
380
          child_allocation.width = width;
 
381
          x += child_allocation.width + wbox->hspacing;
 
382
        }
 
383
      else
 
384
        {
 
385
          GtkRequisition child_requisition;
 
386
          
 
387
          get_child_requisition (wbox, child->widget, &child_requisition);
 
388
          
 
389
          if (child_requisition.height >= area->height)
 
390
            child_allocation.height = area->height;
 
391
          else
 
392
            {
 
393
              child_allocation.height = child_requisition.height;
 
394
              if (wbox->line_justify == GTK_JUSTIFY_FILL || child->vfill)
 
395
                child_allocation.height = area->height;
 
396
              else if (child->vexpand || wbox->line_justify == GTK_JUSTIFY_CENTER)
 
397
                child_allocation.y += (area->height - child_requisition.height) / 2;
 
398
              else if (wbox->line_justify == GTK_JUSTIFY_BOTTOM)
 
399
                child_allocation.y += area->height - child_requisition.height;
 
400
            }
 
401
          
 
402
          if (have_expand_children)
 
403
            {
 
404
              child_allocation.width = child_requisition.width;
 
405
              if (child->hexpand || wbox->justify == GTK_JUSTIFY_FILL)
 
406
                {
 
407
                  guint space;
 
408
                  
 
409
                  n_expand_children--;
 
410
                  space = extra * n_expand_children;
 
411
                  space = width - space;
 
412
                  width -= space;
 
413
                  if (child->hfill)
 
414
                    child_allocation.width += space;
 
415
                  else
 
416
                    {
 
417
                      child_allocation.x += space / 2;
 
418
                      x += space;
 
419
                    }
 
420
                }
 
421
            }
 
422
          else
 
423
            {
 
424
              /* g_print ("child_allocation.x %d += %d * %f ",
 
425
                       child_allocation.x, n_children, extra); */
 
426
              child_allocation.x += n_children * extra;
 
427
              /* g_print ("= %d\n",
 
428
                       child_allocation.x); */
 
429
              child_allocation.width = MIN (child_requisition.width,
 
430
                                            area->width - child_allocation.x + area->x);
 
431
            }
 
432
        }
 
433
      
 
434
      x += child_allocation.width + wbox->hspacing;
 
435
      gtk_widget_size_allocate (child->widget, &child_allocation);
 
436
      n_children++;
 
437
    }
 
438
}
 
439
 
 
440
typedef struct _Line Line;
 
441
struct _Line
 
442
{
 
443
  GSList  *children;
 
444
  guint16  min_size;
 
445
  guint    expand : 1;
 
446
  Line     *next;
 
447
};
 
448
 
 
449
static void
 
450
layout_rows (GtkWrapBox    *wbox,
 
451
             GtkAllocation *area)
 
452
{
 
453
  GtkWrapBoxChild *next_child;
 
454
  guint min_height;
 
455
  gboolean vexpand;
 
456
  GSList *slist;
 
457
  Line *line_list = NULL;
 
458
  guint total_height = 0, n_expand_lines = 0, n_lines = 0;
 
459
  gfloat shrink_height;
 
460
  guint children_per_line;
 
461
  
 
462
  next_child = wbox->children;
 
463
  slist = GTK_WRAP_BOX_GET_CLASS (wbox)->rlist_line_children (wbox,
 
464
                                                              &next_child,
 
465
                                                              area,
 
466
                                                              &min_height,
 
467
                                                              &vexpand);
 
468
  slist = g_slist_reverse (slist);
 
469
 
 
470
  children_per_line = g_slist_length (slist);
 
471
  while (slist)
 
472
    {
 
473
      Line *line = g_new (Line, 1);
 
474
      
 
475
      line->children = slist;
 
476
      line->min_size = min_height;
 
477
      total_height += min_height;
 
478
      line->expand = vexpand;
 
479
      if (vexpand)
 
480
        n_expand_lines++;
 
481
      line->next = line_list;
 
482
      line_list = line;
 
483
      n_lines++;
 
484
      
 
485
      slist = GTK_WRAP_BOX_GET_CLASS (wbox)->rlist_line_children (wbox,
 
486
                                                                  &next_child,
 
487
                                                                  area,
 
488
                                                                  &min_height,
 
489
                                                                  &vexpand);
 
490
      slist = g_slist_reverse (slist);
 
491
    }
 
492
  
 
493
  if (total_height > area->height)
 
494
    shrink_height = total_height - area->height;
 
495
  else
 
496
    shrink_height = 0;
 
497
  
 
498
  if (1) /* reverse lines and shrink */
 
499
    {
 
500
      Line *prev = NULL, *last = NULL;
 
501
      gfloat n_shrink_lines = n_lines;
 
502
      
 
503
      while (line_list)
 
504
        {
 
505
          Line *tmp = line_list->next;
 
506
 
 
507
          if (shrink_height)
 
508
            {
 
509
              Line *line = line_list;
 
510
              guint shrink_fract = shrink_height / n_shrink_lines + 0.5;
 
511
 
 
512
              if (line->min_size > shrink_fract)
 
513
                {
 
514
                  shrink_height -= shrink_fract;
 
515
                  line->min_size -= shrink_fract;
 
516
                }
 
517
              else
 
518
                {
 
519
                  shrink_height -= line->min_size - 1;
 
520
                  line->min_size = 1;
 
521
                }
 
522
            }
 
523
          n_shrink_lines--;
 
524
 
 
525
          last = line_list;
 
526
          line_list->next = prev;
 
527
          prev = line_list;
 
528
          line_list = tmp;
 
529
        }
 
530
      line_list = last;
 
531
    }
 
532
  
 
533
  if (n_lines)
 
534
    {
 
535
      Line *line;
 
536
      gfloat y, height, extra = 0;
 
537
      
 
538
      height = area->height;
 
539
      height = MAX (n_lines, height - (n_lines - 1) * wbox->vspacing);
 
540
      
 
541
      if (wbox->homogeneous)
 
542
        height /= ((gdouble) n_lines);
 
543
      else if (n_expand_lines)
 
544
        {
 
545
          height = MAX (0, height - total_height);
 
546
          extra = height / ((gdouble) n_expand_lines);
 
547
        }
 
548
      else
 
549
        height = 0;
 
550
      
 
551
      y = area->y;
 
552
      line = line_list;
 
553
      while (line)
 
554
        {
 
555
          GtkAllocation row_allocation;
 
556
          Line *next_line = line->next;
 
557
          
 
558
          row_allocation.x = area->x;
 
559
          row_allocation.width = area->width;
 
560
          if (wbox->homogeneous)
 
561
            row_allocation.height = height;
 
562
          else
 
563
            {
 
564
              row_allocation.height = line->min_size;
 
565
              
 
566
              if (line->expand)
 
567
                row_allocation.height += extra;
 
568
            }
 
569
          
 
570
          row_allocation.y = y;
 
571
          
 
572
          y += row_allocation.height + wbox->vspacing;
 
573
          layout_row (wbox,
 
574
                      &row_allocation,
 
575
                      line->children,
 
576
                      children_per_line,
 
577
                      line->expand);
 
578
 
 
579
          g_slist_free (line->children);
 
580
          g_free (line);
 
581
          line = next_line;
 
582
        }
 
583
    }
 
584
}
 
585
 
 
586
static void
 
587
gtk_hwrap_box_size_allocate (GtkWidget     *widget,
 
588
                             GtkAllocation *allocation)
 
589
{
 
590
  GtkWrapBox *wbox = GTK_WRAP_BOX (widget);
 
591
  GtkAllocation area;
 
592
  gint border = GTK_CONTAINER (wbox)->border_width; /*<h2v-skip>*/
 
593
  
 
594
  widget->allocation = *allocation;
 
595
  area.x = allocation->x + border;
 
596
  area.y = allocation->y + border;
 
597
  area.width = MAX (1, (gint) allocation->width - border * 2);
 
598
  area.height = MAX (1, (gint) allocation->height - border * 2);
 
599
  
 
600
  /*<h2v-off>*/
 
601
  /* g_print ("got: width %d, height %d\n",
 
602
     allocation->width,
 
603
     allocation->height);
 
604
  */
 
605
  /*<h2v-on>*/
 
606
  
 
607
  layout_rows (wbox, &area);
 
608
}