summaryrefslogtreecommitdiff
path: root/tikzit/src/gtk/tztoolpalette.m
blob: a948127519ee6e7a1c19d091e0f03a7e34b2981a (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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * tztoolpalette.c, based on gimptoolpalette.c
 * Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
 * Copyright (C) 2012 Alex Merry <alex.merry@kdemail.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>

#include "tztoolpalette.h"


#define DEFAULT_TOOL_ICON_SIZE GTK_ICON_SIZE_BUTTON
#define DEFAULT_BUTTON_RELIEF  GTK_RELIEF_NONE

#define TOOL_BUTTON_DATA_KEY   "tz-tool-palette-item"
#define TOOL_INFO_DATA_KEY     "tz-tool-info"


typedef struct _TzToolPalettePrivate TzToolPalettePrivate;

struct _TzToolPalettePrivate
{
  gint         tool_rows;
  gint         tool_columns;
};

#define GET_PRIVATE(p) G_TYPE_INSTANCE_GET_PRIVATE (p, \
                                                    TZ_TYPE_TOOL_PALETTE, \
                                                    TzToolPalettePrivate)


static void     tz_tool_palette_size_allocate       (GtkWidget       *widget,
                                                       GtkAllocation   *allocation);


G_DEFINE_TYPE (TzToolPalette, tz_tool_palette, GTK_TYPE_TOOL_PALETTE)

#define parent_class tz_tool_palette_parent_class


static void
tz_tool_palette_class_init (TzToolPaletteClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  widget_class->size_allocate         = tz_tool_palette_size_allocate;

  g_type_class_add_private (klass, sizeof (TzToolPalettePrivate));
}

static void
tz_tool_palette_init (TzToolPalette *palette)
{
}

static GtkToolItemGroup *
tz_tool_palette_tool_group (TzToolPalette *palette)
{
  GList                *children;
  GtkToolItemGroup     *group;

  children = gtk_container_get_children (GTK_CONTAINER (palette));
  g_return_val_if_fail (children, NULL);
  group = GTK_TOOL_ITEM_GROUP (children->data);
  g_list_free (children);

  return group;
}

gboolean
tz_tool_palette_get_button_size (TzToolPalette *palette,
                                 gint          *width,
                                 gint          *height)
{
  g_return_val_if_fail (width || height, FALSE);

  GtkToolItemGroup     *group = tz_tool_palette_tool_group (palette);
  g_return_val_if_fail (group, FALSE);

  guint tool_count = gtk_tool_item_group_get_n_items (group);
  if (tool_count > 0)
    {
      GtkWidget      *tool_button;
      GtkRequisition  button_requisition;

      tool_button = GTK_WIDGET (gtk_tool_item_group_get_nth_item (group, 0));
      gtk_widget_size_request (tool_button, &button_requisition);
      if (width)
        *width = button_requisition.width;
      if (height)
        *height = button_requisition.height;
      return TRUE;
    }
  return FALSE;
}

static void
tz_tool_palette_size_allocate (GtkWidget     *widget,
                               GtkAllocation *allocation)
{
  TzToolPalettePrivate *private = GET_PRIVATE (widget);
  GtkToolItemGroup     *group = tz_tool_palette_tool_group (TZ_TOOL_PALETTE (widget));

  g_return_if_fail (group);

  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);

  guint tool_count = gtk_tool_item_group_get_n_items (group);
  if (tool_count > 0)
    {
      GtkWidget      *tool_button;
      GtkRequisition  button_requisition;
      gint            tool_rows;
      gint            tool_columns;

      tool_button = GTK_WIDGET (gtk_tool_item_group_get_nth_item (group, 0));
      gtk_widget_size_request (tool_button, &button_requisition);

      tool_columns = MAX (1, (allocation->width / button_requisition.width));
      tool_rows    = tool_count / tool_columns;

      if (tool_count % tool_columns)
        tool_rows++;

      if (private->tool_rows    != tool_rows  ||
          private->tool_columns != tool_columns)
        {
          private->tool_rows    = tool_rows;
          private->tool_columns = tool_columns;

          gtk_widget_set_size_request (widget, -1,
                                       tool_rows * button_requisition.height);
        }
    }
}

GtkWidget *
tz_tool_palette_new (void)
{
  return g_object_new (TZ_TYPE_TOOL_PALETTE, NULL);
}

// vim:ft=objc:ts=8:et:sts=2:sw=2:foldmethod=marker