]> Pileus Git - ~andy/gtk/blob - gtk/gtkseparatortoolitem.c
add new "is_important" property
[~andy/gtk] / gtk / gtkseparatortoolitem.c
1 /* gtkseparatortoolitem.c
2  *
3  * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se>
4  * Copyright (C) 2002 James Henstridge <james@daa.com.au>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #undef GTK_DISABLE_DEPRECATED
23
24 #include "gtkseparatormenuitem.h"
25 #include "gtkseparatortoolitem.h"
26 #include "gtkintl.h"
27 #include "gtktoolbar.h"
28
29 /* note: keep in sync with DEFAULT_SPACE_SIZE and DEFAULT_SPACE_STYLE in gtktoolbar.c */
30 #define DEFAULT_SPACE_SIZE 4
31 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
32
33 #define SPACE_LINE_DIVISION 10
34 #define SPACE_LINE_START    3
35 #define SPACE_LINE_END      7
36
37 #define MENU_ID "gtk-separator-tool-item-menu-id"
38
39 static void                 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class);
40 static gboolean             gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item);
41 static void                 gtk_separator_tool_item_size_request (GtkWidget      *widget,
42                                                                   
43                                                                   GtkRequisition *requisition);
44 static gboolean             gtk_separator_tool_item_expose     (GtkWidget                 *widget,
45                                                                 GdkEventExpose            *event);
46 static void                 gtk_separator_tool_item_add        (GtkContainer              *container,
47                                                                 GtkWidget                 *child);
48 static GtkToolbarSpaceStyle get_space_style                    (GtkToolItem               *tool_item);
49 static gint                 get_space_size                     (GtkToolItem               *tool_item);
50
51 static GObjectClass *parent_class = NULL;
52
53
54 GType
55 gtk_separator_tool_item_get_type (void)
56 {
57   static GType type = 0;
58
59   if (!type)
60     {
61       static const GTypeInfo type_info =
62         {
63           sizeof (GtkSeparatorToolItemClass),
64           (GBaseInitFunc) 0,
65           (GBaseFinalizeFunc) 0,
66           (GClassInitFunc) gtk_separator_tool_item_class_init,
67           (GClassFinalizeFunc) 0,
68           NULL,
69           sizeof (GtkSeparatorToolItem),
70           0, /* n_preallocs */
71           (GInstanceInitFunc) NULL,
72         };
73
74       type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
75                                      "GtkSeparatorToolItem", &type_info, 0);
76     }
77   return type;
78 }
79
80 static GtkToolbarSpaceStyle
81 get_space_style (GtkToolItem *tool_item)
82 {
83   GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
84   GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
85
86   if (GTK_IS_TOOLBAR (parent))
87     {
88       gtk_widget_style_get (parent,
89                             "space_style", &space_style,
90                             NULL);
91     }
92
93   return space_style;  
94 }
95
96 static gint
97 get_space_size (GtkToolItem *tool_item)
98 {
99   gint space_size = DEFAULT_SPACE_SIZE;
100   GtkWidget *parent = GTK_WIDGET (tool_item)->parent;
101   
102   if (GTK_IS_TOOLBAR (parent))
103     {
104       gtk_widget_style_get (parent,
105                             "space_size", &space_size,
106                             NULL);
107     }
108
109   return space_size;
110 }
111
112 static void
113 gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
114 {
115   GtkContainerClass *container_class;
116   GtkToolItemClass *toolitem_class;
117   GtkWidgetClass *widget_class;
118
119   parent_class = g_type_class_peek_parent (class);
120   container_class = (GtkContainerClass *)class;
121   toolitem_class = (GtkToolItemClass *)class;
122   widget_class = (GtkWidgetClass *)class;
123
124   widget_class->size_request = gtk_separator_tool_item_size_request;
125   widget_class->expose_event = gtk_separator_tool_item_expose;
126   toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
127
128   container_class->add = gtk_separator_tool_item_add;
129 }
130
131 static void
132 gtk_separator_tool_item_add (GtkContainer *container,
133                              GtkWidget    *child)
134 {
135   g_warning("attempt to add a child to an GtkSeparatorToolItem");
136 }
137
138 static gboolean
139 gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
140 {
141   GtkWidget *menu_item = NULL;
142   
143   menu_item = gtk_separator_menu_item_new();
144   
145   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
146   
147   return TRUE;
148 }
149
150 static void
151 gtk_separator_tool_item_size_request (GtkWidget      *widget,
152                                       GtkRequisition *requisition)
153 {
154   GtkToolItem *item = GTK_TOOL_ITEM (widget);
155   GtkOrientation orientation = gtk_tool_item_get_orientation (item);
156
157   if (orientation == GTK_ORIENTATION_HORIZONTAL)
158     {
159       requisition->width = get_space_size (item);
160       requisition->height = 1;
161     }
162   else
163     {
164       requisition->height = get_space_size (item);
165       requisition->width = 1;
166     }
167 }
168
169 static gboolean
170 gtk_separator_tool_item_expose (GtkWidget      *widget,
171                                 GdkEventExpose *event)
172 {
173   GtkToolItem *tool_item = GTK_TOOL_ITEM (widget);
174   gint space_size;
175   GtkAllocation *allocation;
176   GtkOrientation orientation;
177   GdkRectangle *area;
178
179   if (get_space_style (tool_item) == GTK_TOOLBAR_SPACE_LINE)
180     {
181       space_size = get_space_size (tool_item);
182       allocation = &(widget->allocation);
183       orientation = gtk_tool_item_get_orientation (tool_item);
184       area = &(event->area);
185
186       if (orientation == GTK_ORIENTATION_HORIZONTAL)
187         {
188           gtk_paint_vline (widget->style, widget->window,
189                            GTK_WIDGET_STATE (widget), area, widget,
190                            "separator_tool_item",
191                            allocation->y + allocation->height *
192                            SPACE_LINE_START / SPACE_LINE_DIVISION,
193                            allocation->y + allocation->height *
194                            SPACE_LINE_END / SPACE_LINE_DIVISION,
195                            allocation->x + (space_size - widget->style->xthickness) / 2);
196         }
197       else if (orientation == GTK_ORIENTATION_VERTICAL)
198         {
199           gtk_paint_hline (widget->style, widget->window,
200                            GTK_WIDGET_STATE (widget), area, widget,
201                            "separator_tool_item",
202                            allocation->x + allocation->width *
203                            SPACE_LINE_START / SPACE_LINE_DIVISION,
204                            allocation->x + allocation->width *
205                            SPACE_LINE_END / SPACE_LINE_DIVISION,
206                            allocation->y + (space_size - widget->style->ythickness) / 2);
207         }
208     }
209   
210   return TRUE;
211 }
212
213 /**
214  * gtk_separator_tool_item_new:
215  * 
216  * Create a new #GtkSeparatorToolItem
217  * 
218  * Return value: the new #GtkSeparatorToolItem
219  * 
220  * Since: 2.4
221  **/
222 GtkToolItem *
223 gtk_separator_tool_item_new (void)
224 {
225   GtkToolItem *self;
226
227   self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
228                        NULL);
229   
230   return self;
231 }