5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
10 #include "ygtkfieldentry.h"
12 #include <YInputField.h>
17 YGInputField (YWidget *parent,
const std::string &label,
bool passwordMode)
18 : YInputField (NULL, label, passwordMode),
20 YGTK_TYPE_FIELD_ENTRY, NULL)
22 gtk_widget_set_size_request (getWidget(), 0, -1);
24 ygtk_field_entry_add_field (field, 0);
26 GtkEntry *entry = ygtk_field_entry_get_field_widget (field, 0);
27 gtk_entry_set_activates_default (entry, TRUE);
28 gtk_entry_set_visibility (entry, !passwordMode);
30 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
34 virtual std::string value()
37 return ygtk_field_entry_get_field_text (field, 0);
40 virtual void setValue (
const std::string &text)
44 ygtk_field_entry_set_field_text (field, 0, text.c_str());
50 ygtk_field_entry_setup_field (field, 0, inputMaxLength(), validChars().c_str());
53 virtual void setInputMaxLength (
int len)
55 YInputField::setInputMaxLength (len);
59 virtual void setValidChars (
const std::string &validChars)
61 YInputField::setValidChars (validChars);
67 { pThis->emitEvent (YEvent::ValueChanged); }
70 virtual bool doSetKeyboardFocus()
73 return ygtk_field_entry_set_focus (field);
76 virtual unsigned int getMinSize (YUIDimension dim)
77 {
return dim == YD_HORIZ ? (shrinkable() ? 30 : 200) : 0; }
79 YGLABEL_WIDGET_IMPL (YInputField)
82 YInputField *YGWidgetFactory::createInputField (YWidget *parent,
const std::string &label,
88 #include "YTimeField.h"
93 YGTimeField (YWidget *parent,
const std::string &label)
94 : YTimeField (NULL, label),
96 YGTK_TYPE_FIELD_ENTRY, NULL)
99 ygtk_field_entry_add_field (field,
':');
100 ygtk_field_entry_add_field (field,
':');
101 ygtk_field_entry_add_field (field,
':');
102 ygtk_field_entry_setup_field (field, 0, 2,
"0123456789");
103 ygtk_field_entry_setup_field (field, 1, 2,
"0123456789");
104 ygtk_field_entry_setup_field (field, 2, 2,
"0123456789");
106 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
110 virtual void setValue (
const std::string &time)
113 if (time.empty())
return;
114 char hours[3], mins[3], secs[3];
115 sscanf (time.c_str(),
"%2s:%2s:%2s", hours, mins, secs);
118 ygtk_field_entry_set_field_text (entry, 0, hours);
119 ygtk_field_entry_set_field_text (entry, 1, mins);
120 ygtk_field_entry_set_field_text (entry, 2, secs);
123 virtual std::string value()
125 const gchar *hours, *mins, *secs;
127 hours = ygtk_field_entry_get_field_text (entry, 0);
128 mins = ygtk_field_entry_get_field_text (entry, 1);
129 secs = ygtk_field_entry_get_field_text (entry, 2);
131 gchar *time = g_strdup_printf (
"%02d:%02d:%02d", atoi (hours), atoi (mins), atoi (secs));
132 std::string str (time);
138 static void value_changed_cb (
YGtkFieldEntry *entry, gint field_nb,
140 { pThis->emitEvent (YEvent::ValueChanged); }
142 YGLABEL_WIDGET_IMPL (YTimeField)
145 YTimeField *YGOptionalWidgetFactory::createTimeField (YWidget *parent,
const std::string &label)
148 #include "YDateField.h"
149 #include "ygtkmenubutton.h"
153 GtkWidget *m_calendar, *m_popup_calendar;
156 YGDateField (YWidget *parent,
const std::string &label)
157 : YDateField (NULL, label),
158 YGLabeledWidget (
this, parent, label, YD_HORIZ, YGTK_TYPE_FIELD_ENTRY, NULL)
160 ygtk_field_entry_add_field (getField(),
'-');
161 ygtk_field_entry_add_field (getField(),
'-');
162 ygtk_field_entry_add_field (getField(),
'-');
163 ygtk_field_entry_setup_field (getField(), 0, 4,
"0123456789");
164 ygtk_field_entry_setup_field (getField(), 1, 2,
"0123456789");
165 ygtk_field_entry_setup_field (getField(), 2, 2,
"0123456789");
167 m_calendar = gtk_calendar_new();
168 gtk_widget_show (m_calendar);
169 GtkWidget *popup = ygtk_popup_window_new (m_calendar);
171 GtkWidget *menu_button = ygtk_menu_button_new_with_label (
"");
172 ygtk_menu_button_set_popup (YGTK_MENU_BUTTON (menu_button), popup);
173 gtk_widget_show (menu_button);
174 gtk_box_pack_start (GTK_BOX (getWidget()), menu_button, FALSE, TRUE, 6);
176 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
177 connect (m_calendar,
"day-selected", G_CALLBACK (calendar_changed_cb),
this);
178 g_signal_connect (G_OBJECT (m_calendar),
"day-selected-double-click",
179 G_CALLBACK (double_click_cb), popup);
182 inline GtkCalendar *getCalendar()
183 {
return GTK_CALENDAR (m_calendar); }
185 {
return YGTK_FIELD_ENTRY (getWidget()); }
188 virtual void setValue (
const std::string &date)
191 if (date.empty())
return;
192 char year[5], month[3], day[3];
193 sscanf (date.c_str(),
"%4s-%2s-%2s", year, month, day);
195 gtk_calendar_select_month (getCalendar(), atoi (month)-1, atoi (year));
196 gtk_calendar_select_day (getCalendar(), atoi (day));
198 ygtk_field_entry_set_field_text (getField(), 0, year);
199 ygtk_field_entry_set_field_text (getField(), 1, month);
200 ygtk_field_entry_set_field_text (getField(), 2, day);
203 virtual std::string value()
205 const gchar *year, *month, *day;
206 year = ygtk_field_entry_get_field_text (getField(), 0);
207 month = ygtk_field_entry_get_field_text (getField(), 1);
208 day = ygtk_field_entry_get_field_text (getField(), 2);
210 gchar *time = g_strdup_printf (
"%04d-%02d-%02d", atoi (year),
211 atoi (month), atoi (day));
212 std::string str (time);
218 static void value_changed_cb (
YGtkFieldEntry *entry, gint field_nb,
221 int year, month, day;
222 year = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 0));
223 month = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 1));
224 day = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 2));
226 if (day < 1 || day > 31 || month < 1 || month > 12)
229 g_signal_handlers_block_by_func (pThis->getCalendar(),
230 (gpointer) calendar_changed_cb, pThis);
232 gtk_calendar_select_month (pThis->getCalendar(), month-1, year);
233 gtk_calendar_select_day (pThis->getCalendar(), day);
235 g_signal_handlers_unblock_by_func (pThis->getCalendar(),
236 (gpointer) calendar_changed_cb, pThis);
238 pThis->emitEvent (YEvent::ValueChanged);
241 static void calendar_changed_cb (GtkCalendar *calendar,
YGDateField *pThis)
243 guint year, month, day;
244 gtk_calendar_get_date (calendar, &year, &month, &day);
247 gchar *year_str, *month_str, *day_str;
248 year_str = g_strdup_printf (
"%d", year);
249 month_str = g_strdup_printf (
"%d", month);
250 day_str = g_strdup_printf (
"%d", day);
252 g_signal_handlers_block_by_func (pThis->getField(),
253 (gpointer) value_changed_cb, pThis);
256 ygtk_field_entry_set_field_text (entry, 0, year_str);
257 ygtk_field_entry_set_field_text (entry, 1, month_str);
258 ygtk_field_entry_set_field_text (entry, 2, day_str);
260 g_signal_handlers_unblock_by_func (pThis->getField(),
261 (gpointer) value_changed_cb, pThis);
267 pThis->emitEvent (YEvent::ValueChanged);
270 static void double_click_cb (GtkCalendar *calendar,
YGtkPopupWindow *popup)
273 gtk_widget_hide (GTK_WIDGET (popup));
276 YGLABEL_WIDGET_IMPL (YDateField)
279 YDateField *YGOptionalWidgetFactory::createDateField (YWidget *parent,
const std::string &label)
282 #include "YTimezoneSelector.h"
283 #include "ygtktimezonepicker.h"
289 const std::map <std::string, std::string> &timezones)
290 : YTimezoneSelector (NULL, pixmap, timezones),
291 YGWidget (
this, parent, YGTK_TYPE_TIME_ZONE_PICKER, NULL)
293 setStretchable (YD_HORIZ,
true);
294 setStretchable (YD_VERT,
true);
295 ygtk_time_zone_picker_set_map (YGTK_TIME_ZONE_PICKER (getWidget()),
296 pixmap.c_str(), convert_code_to_name, (gpointer) &timezones);
298 connect (getWidget(),
"zone-clicked", G_CALLBACK (zone_clicked_cb),
this);
302 virtual std::string currentZone()
const
305 const gchar *zone = ygtk_time_zone_picker_get_current_zone (
306 YGTK_TIME_ZONE_PICKER (pThis->getWidget()));
309 return std::string();
312 virtual void setCurrentZone (
const std::string &zone,
bool zoom)
315 ygtk_time_zone_picker_set_current_zone (YGTK_TIME_ZONE_PICKER (getWidget()),
320 static const gchar *convert_code_to_name (
const gchar *code, gpointer pData)
322 const std::map <std::string, std::string> *timezones =
323 (std::map <std::string, std::string> *) pData;
324 std::map <std::string, std::string>::const_iterator name =
325 timezones->find (code);
326 if (name == timezones->end())
328 return name->second.c_str();
334 { pThis->emitEvent (YEvent::ValueChanged); }
336 YGWIDGET_IMPL_COMMON (YTimezoneSelector)
339 YTimezoneSelector *YGOptionalWidgetFactory::createTimezoneSelector (YWidget *parent,
340 const std::string &pixmap,
const std::map <std::string, std::string> &timezones)