5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
8 #include <YPushButton.h>
13 #include <YLayoutBox.h>
14 #include "ygtkratiobox.h"
18 bool m_customIcon, m_labelIcon;
22 : YPushButton (NULL, label),
23 YGWidget (
this, parent, GTK_TYPE_BUTTON,
"can-default", TRUE, NULL)
25 m_customIcon = m_labelIcon =
false;
26 gtk_button_set_use_underline (GTK_BUTTON (getWidget()), TRUE);
28 connect (getWidget(),
"clicked", G_CALLBACK (clicked_cb),
this);
29 g_signal_connect (getWidget(),
"realize", G_CALLBACK (realize_cb),
this);
32 void setStockIcon (
const std::string &label)
35 const char *icon = NULL;
36 switch (functionKey()) {
37 case 1: icon =
"help-contents";
break;
38 case 2: icon =
"dialog-information";
break;
39 case 3: icon =
"list-add";
break;
40 case 4: icon =
"edit-copy";
break;
41 case 5: icon =
"edit-delete";
break;
42 case 6: icon =
"system-run";
break;
43 case 7: icon =
"edit-copy";
break;
46 case 9: icon =
"application-exit";
break;
47 case 10: icon =
"application-exit";
break;
51 case YOKButton: icon =
"document-save";
break;
52 case YApplyButton: icon =
"document-save";
break;
53 case YCancelButton: icon =
"document-revert";
break;
54 case YHelpButton: icon =
"help-contents";
break;
55 case YCustomButton:
case YMaxButtonRole:
case YRelNotesButton:
break;
57 m_labelIcon = YGUtils::setStockIcon (getWidget(), label, icon);
62 virtual void setLabel (
const std::string &label)
64 YPushButton::setLabel (label);
65 std::string str = YGUtils::mapKBAccel (label);
66 gtk_button_set_label (GTK_BUTTON (getWidget()), str.c_str());
70 virtual void setRole (YButtonRole role)
72 YPushButton::setRole (role);
73 if (!m_labelIcon && role != YCustomButton)
74 setStockIcon (label());
77 virtual void setFunctionKey (
int key)
79 YPushButton::setFunctionKey (key);
80 if (!m_labelIcon && hasFunctionKey())
81 setStockIcon (label());
84 virtual void setHelpButton (
bool helpButton)
86 YPushButton::setHelpButton (helpButton);
87 if (!m_labelIcon && helpButton)
88 setStockIcon (label());
91 virtual void setIcon (
const std::string &icon)
93 GtkButton *button = GTK_BUTTON (getWidget());
97 GtkWidget *image = gtk_button_get_image (button);
99 gtk_widget_hide (image);
103 std::string path (icon);
105 path = std::string (THEMEDIR) +
"/" + path;
108 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (path.c_str(), &error);
110 GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
111 gtk_button_set_image (button, image);
113 gtk_widget_show (image);
114 g_object_unref (G_OBJECT (pixbuf));
117 yuiWarning() <<
"YGPushButton: Couldn't load icon image: " << path << std::endl
118 <<
"Reason: " << error->message << std::endl;
122 virtual void setDefaultButton (
bool isDefault)
125 static void realize_cb (GtkWidget *widget)
127 gtk_widget_grab_default (widget);
128 gtk_widget_grab_focus (widget);
132 YPushButton::setDefaultButton (isDefault);
134 GtkWidget *button = getWidget();
135 gtk_widget_set_can_default(button, TRUE);
136 if (gtk_widget_get_realized (button))
137 inner::realize_cb (button);
138 g_signal_connect (G_OBJECT (button),
"realize",
139 G_CALLBACK (inner::realize_cb),
this);
143 static bool hasIcon (YWidget *ywidget)
145 if (dynamic_cast <YPushButton *> (ywidget)) {
146 GtkWidget *button = YGWidget::get (ywidget)->getWidget();
147 GtkWidget *icon = gtk_button_get_image (GTK_BUTTON (button));
148 return icon && gtk_widget_get_visible (icon);
154 static gboolean treat_icon_cb (GtkWidget *widget, GdkEventExpose *event,
157 YLayoutBox *ybox = dynamic_cast <YLayoutBox *> (pThis->parent());
158 if (ybox && !pThis->m_customIcon) {
159 if (ybox->primary() == YD_HORIZ) {
162 for (YWidgetListConstIterator it = ybox->childrenBegin();
163 it != ybox->childrenEnd(); it++) {
164 if ((YWidget *) pThis == *it) {
165 if (ylast && !hasIcon (ylast))
170 if (!dynamic_cast <YPushButton *> (ylast))
176 bool disableIcons =
false;
177 for (YWidgetListConstIterator it = ybox->childrenBegin();
178 it != ybox->childrenEnd(); it++)
182 for (YWidgetListConstIterator it = ybox->childrenBegin();
183 it != ybox->childrenEnd(); it++)
184 if (dynamic_cast <YPushButton *> (*it)) {
187 if (!button->m_customIcon)
188 button->setIcon (
"");
192 g_signal_handlers_disconnect_by_func (widget, (gpointer) treat_icon_cb, pThis);
198 static void clicked_cb (GtkButton *button,
YGPushButton *pThis)
199 { pThis->emitEvent (YEvent::Activated, IGNORE_NOTIFY_EVENT); }
202 #define DEFAULT_CHILD_MIN_WIDTH 85
203 #define DEFAULT_CHILD_MIN_HEIGHT 27
205 static void realize_cb (GtkWidget *widget,
YGPushButton *pThis)
207 YWidget *yparent = pThis->m_ywidget->parent();
208 if (yparent && !strcmp (yparent->widgetClass(),
"YButtonBox"))
209 ygtk_adj_size_set_min (YGTK_ADJ_SIZE(pThis->getLayout()),
210 DEFAULT_CHILD_MIN_WIDTH, DEFAULT_CHILD_MIN_HEIGHT);
213 YGWIDGET_IMPL_COMMON (YPushButton)
216 YPushButton *YGWidgetFactory::createPushButton (YWidget *parent,
const std::string &label)