1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2003 Ximian, Inc. (www.ximian.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib/gi18n.h>
30 #include "vgsearchbar.h"
31 #include "vgmarshal.h"
39 static int search_bar_signals
[LAST_SIGNAL
] = { 0, };
42 static void vg_search_bar_class_init (VgSearchBarClass
*klass
);
43 static void vg_search_bar_init (VgSearchBar
*bar
);
44 static void vg_search_bar_destroy (GtkObject
*obj
);
45 static void vg_search_bar_finalize (GObject
*obj
);
47 static void search_bar_set_menu_items (VgSearchBar
*bar
, VgSearchBarItem
*items
);
50 static GtkHBoxClass
*parent_class
= NULL
;
54 vg_search_bar_get_type (void)
56 static GType type
= 0;
59 static const GTypeInfo info
= {
60 sizeof (VgSearchBarClass
),
61 NULL
, /* base_class_init */
62 NULL
, /* base_class_finalize */
63 (GClassInitFunc
) vg_search_bar_class_init
,
64 NULL
, /* class_finalize */
65 NULL
, /* class_data */
68 (GInstanceInitFunc
) vg_search_bar_init
,
71 type
= g_type_register_static (GTK_TYPE_HBOX
, "VgSearchBar", &info
, 0);
78 vg_search_bar_class_init (VgSearchBarClass
*klass
)
80 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
81 GtkObjectClass
*gtk_object_class
= GTK_OBJECT_CLASS (klass
);
83 parent_class
= g_type_class_ref (GTK_TYPE_VBOX
);
85 object_class
->finalize
= vg_search_bar_finalize
;
86 gtk_object_class
->destroy
= vg_search_bar_destroy
;
89 klass
->set_menu_items
= search_bar_set_menu_items
;
92 search_bar_signals
[SEARCH
] =
93 g_signal_new ("search",
94 G_OBJECT_CLASS_TYPE (object_class
),
96 G_STRUCT_OFFSET (VgSearchBarClass
, search
),
99 G_TYPE_NONE
, 1, G_TYPE_INT
);
101 search_bar_signals
[CLEAR
] =
102 g_signal_new ("clear",
103 G_OBJECT_CLASS_TYPE (object_class
),
105 G_STRUCT_OFFSET (VgSearchBarClass
, clear
),
107 vg_marshal_NONE__NONE
,
113 entry_activate (GtkEntry
*entry
, VgSearchBar
*bar
)
115 g_signal_emit (bar
, search_bar_signals
[SEARCH
], 0, bar
->item_id
);
119 clear_clicked (GtkWidget
*widget
, VgSearchBar
*bar
)
121 gtk_entry_set_text (bar
->entry
, "");
122 gtk_option_menu_set_history (bar
->menu
, 0);
123 g_signal_emit (bar
, search_bar_signals
[CLEAR
], 0);
127 vg_search_bar_init (VgSearchBar
*bar
)
131 gtk_box_set_spacing (GTK_BOX(bar
), 6);
135 bar
->menu
= GTK_OPTION_MENU (widget
= gtk_option_menu_new ());
136 gtk_widget_show (widget
);
137 gtk_box_pack_start (GTK_BOX (bar
), widget
, FALSE
, FALSE
, 0);
139 bar
->entry
= GTK_ENTRY (widget
= gtk_entry_new ());
140 gtk_widget_show (widget
);
141 gtk_box_pack_start (GTK_BOX (bar
), widget
, TRUE
, TRUE
, 0);
142 g_signal_connect (bar
->entry
, "activate", G_CALLBACK (entry_activate
), bar
);
144 bar
->clear
= widget
= gtk_button_new_from_stock (GTK_STOCK_CLEAR
);
145 gtk_widget_show (widget
);
146 gtk_box_pack_start (GTK_BOX (bar
), widget
, FALSE
, FALSE
, 0);
147 g_signal_connect (bar
->clear
, "clicked", G_CALLBACK (clear_clicked
), bar
);
151 vg_search_bar_finalize (GObject
*obj
)
153 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
157 vg_search_bar_destroy (GtkObject
*obj
)
159 GTK_OBJECT_CLASS (parent_class
)->destroy (obj
);
164 vg_search_bar_new (void)
166 return g_object_new (VG_TYPE_SEARCH_BAR
, NULL
);
171 item_activate (GtkMenuItem
*item
, VgSearchBar
*bar
)
173 bar
->item_id
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item
), "item_id"));
177 search_bar_set_menu_items (VgSearchBar
*bar
, VgSearchBarItem
*items
)
179 GtkWidget
*menu
, *item
;
182 g_return_if_fail (VG_IS_SEARCH_BAR (bar
));
183 g_return_if_fail (items
!= NULL
);
185 menu
= gtk_menu_new ();
186 bar
->item_id
= items
->id
;
188 for (i
= 0; items
[i
].label
!= NULL
; i
++) {
189 item
= gtk_menu_item_new_with_label (_(items
[i
].label
));
190 g_object_set_data (G_OBJECT (item
), "item_id", GINT_TO_POINTER (items
[i
].id
));
191 g_signal_connect (item
, "activate", G_CALLBACK (item_activate
), bar
);
192 gtk_widget_show (item
);
194 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
197 gtk_option_menu_set_menu (bar
->menu
, menu
);
198 gtk_option_menu_set_history (bar
->menu
, 0);
203 vg_search_bar_set_menu_items (VgSearchBar
*bar
, VgSearchBarItem
*items
)
205 g_return_if_fail (VG_IS_SEARCH_BAR (bar
));
206 g_return_if_fail (items
!= NULL
);
208 VG_SEARCH_BAR_GET_CLASS (bar
)->set_menu_items (bar
, items
);
212 vg_search_bar_get_text (VgSearchBar
*bar
)
214 g_return_val_if_fail (VG_IS_SEARCH_BAR (bar
), NULL
);
216 return gtk_entry_get_text (bar
->entry
);