5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
8 #include <YPushButton.h>
14 #include <YLayoutBox.h>
15 #include "ygtkratiobox.h"
19 bool m_customIcon, m_labelIcon;
23 : YPushButton (NULL, label),
24 YGWidget (
this, parent, GTK_TYPE_BUTTON,
"can-default", TRUE, NULL)
26 m_customIcon = m_labelIcon =
false;
27 gtk_button_set_use_underline (GTK_BUTTON (getWidget()), TRUE);
29 connect (getWidget(),
"clicked", G_CALLBACK (clicked_cb),
this);
30 g_signal_connect (getWidget(),
"realize", G_CALLBACK (realize_cb),
this);
33 void setStockIcon (
const std::string &label)
36 const char *icon = NULL;
37 switch (functionKey()) {
38 case 1: icon =
"help-contents";
break;
39 case 2: icon =
"dialog-information";
break;
40 case 3: icon =
"list-add";
break;
41 case 4: icon =
"edit-copy";
break;
42 case 5: icon =
"edit-delete";
break;
43 case 6: icon =
"system-run";
break;
44 case 7: icon =
"edit-copy";
break;
47 case 9: icon =
"application-exit";
break;
48 case 10: icon =
"application-exit";
break;
52 case YOKButton: icon =
"document-save";
break;
53 case YApplyButton: icon =
"document-save";
break;
54 case YCancelButton: icon =
"document-revert";
break;
55 case YHelpButton: icon =
"help-contents";
break;
56 case YCustomButton:
case YMaxButtonRole:
case YRelNotesButton:
break;
58 m_labelIcon = YGUtils::setStockIcon (getWidget(), label, icon);
63 virtual void setLabel (
const std::string &label)
65 YPushButton::setLabel (label);
66 std::string str = YGUtils::mapKBAccel (label);
67 gtk_button_set_label (GTK_BUTTON (getWidget()), str.c_str());
71 virtual void setRole (YButtonRole role)
73 YPushButton::setRole (role);
74 if (!m_labelIcon && role != YCustomButton)
75 setStockIcon (label());
78 virtual void setFunctionKey (
int key)
80 YPushButton::setFunctionKey (key);
81 if (!m_labelIcon && hasFunctionKey())
82 setStockIcon (label());
85 virtual void setHelpButton (
bool helpButton)
87 YPushButton::setHelpButton (helpButton);
88 if (!m_labelIcon && helpButton)
89 setStockIcon (label());
92 virtual void setIcon (
const std::string &icon)
94 GtkButton *button = GTK_BUTTON (getWidget());
98 GtkWidget *image = gtk_button_get_image (button);
100 gtk_widget_hide (image);
104 std::string path (icon);
106 path = std::string (THEMEDIR) +
"/" + path;
108 char *p = strdup(path.c_str());
109 char *p1 = strdup(path.c_str());
110 char *dname = dirname(p);
111 char *fname = basename(p1);
112 char *name = strtok (fname,
".");
113 GtkIconTheme * theme = gtk_icon_theme_get_default ();
114 gtk_icon_theme_add_resource_path (theme, dname);
115 gtk_icon_theme_prepend_search_path (theme, dname);
116 gtk_icon_theme_rescan_if_needed (theme);
118 GdkPixbuf *pixbuf = gtk_icon_theme_load_icon (theme,
121 GTK_ICON_LOOKUP_FORCE_SIZE,
126 GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
127 gtk_button_set_image (button, image);
129 gtk_button_set_always_show_image (button, TRUE);
130 g_object_unref (G_OBJECT (pixbuf));
133 yuiWarning() <<
"YGPushButton: Couldn't load icon image: " << path << std::endl
134 <<
"Reason: " << error->message << std::endl;
138 virtual void setDefaultButton (
bool isDefault)
141 static void realize_cb (GtkWidget *widget)
143 gtk_widget_grab_default (widget);
144 gtk_widget_grab_focus (widget);
148 YPushButton::setDefaultButton (isDefault);
150 GtkWidget *button = getWidget();
151 gtk_widget_set_can_default(button, TRUE);
152 if (gtk_widget_get_realized (button))
153 inner::realize_cb (button);
154 g_signal_connect (G_OBJECT (button),
"realize",
155 G_CALLBACK (inner::realize_cb),
this);
159 static bool hasIcon (YWidget *ywidget)
161 if (
dynamic_cast <YPushButton *
> (ywidget)) {
162 GtkWidget *button = YGWidget::get (ywidget)->getWidget();
163 GtkWidget *icon = gtk_button_get_image (GTK_BUTTON (button));
164 return icon && gtk_widget_get_visible (icon);
170 static gboolean treat_icon_cb (GtkWidget *widget, GdkEventExpose *event,
173 YLayoutBox *ybox =
dynamic_cast <YLayoutBox *
> (pThis->parent());
174 if (ybox && !pThis->m_customIcon) {
175 if (ybox->primary() == YD_HORIZ) {
178 for (YWidgetListConstIterator it = ybox->childrenBegin();
179 it != ybox->childrenEnd(); it++) {
180 if ((YWidget *) pThis == *it) {
181 if (ylast && !hasIcon (ylast))
186 if (!
dynamic_cast <YPushButton *
> (ylast))
192 bool disableIcons =
false;
193 for (YWidgetListConstIterator it = ybox->childrenBegin();
194 it != ybox->childrenEnd(); it++)
198 for (YWidgetListConstIterator it = ybox->childrenBegin();
199 it != ybox->childrenEnd(); it++)
200 if (
dynamic_cast <YPushButton *
> (*it)) {
203 if (!button->m_customIcon)
204 button->setIcon (
"");
208 g_signal_handlers_disconnect_by_func (widget, (gpointer) treat_icon_cb, pThis);
214 static void clicked_cb (GtkButton *button,
YGPushButton *pThis)
215 { pThis->emitEvent (YEvent::Activated, IGNORE_NOTIFY_EVENT); }
218 #define DEFAULT_CHILD_MIN_WIDTH 85
219 #define DEFAULT_CHILD_MIN_HEIGHT 27
221 static void realize_cb (GtkWidget *widget,
YGPushButton *pThis)
223 YWidget *yparent = pThis->m_ywidget->parent();
224 if (yparent && !strcmp (yparent->widgetClass(),
"YButtonBox"))
225 ygtk_adj_size_set_min (YGTK_ADJ_SIZE(pThis->getLayout()),
226 DEFAULT_CHILD_MIN_WIDTH, DEFAULT_CHILD_MIN_HEIGHT);
229 YGWIDGET_IMPL_COMMON (YPushButton)
232 YPushButton *YGWidgetFactory::createPushButton (YWidget *parent,
const std::string &label)