5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
10 #include "ygtkfieldentry.h"
12 #include <YInputField.h>
13 #include <boost/date_time/posix_time/posix_time.hpp>
17 using namespace boost::gregorian;
22 YGInputField (YWidget *parent,
const std::string &label,
bool passwordMode)
23 : YInputField (NULL, label, passwordMode),
25 YGTK_TYPE_FIELD_ENTRY, NULL)
27 gtk_widget_set_size_request (getWidget(), 0, -1);
29 ygtk_field_entry_add_field (field, 0);
31 GtkEntry *entry = ygtk_field_entry_get_field_widget (field, 0);
32 gtk_entry_set_activates_default (entry, TRUE);
33 gtk_entry_set_visibility (entry, !passwordMode);
35 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
39 virtual std::string value()
42 return ygtk_field_entry_get_field_text (field, 0);
45 virtual void setValue (
const std::string &text)
49 ygtk_field_entry_set_field_text (field, 0, text.c_str());
55 ygtk_field_entry_setup_field (field, 0, inputMaxLength(), validChars().c_str());
58 virtual void setInputMaxLength (
int len)
60 YInputField::setInputMaxLength (len);
64 virtual void setValidChars (
const std::string &validChars)
66 YInputField::setValidChars (validChars);
72 { pThis->emitEvent (YEvent::ValueChanged); }
75 virtual bool doSetKeyboardFocus()
78 return ygtk_field_entry_set_focus (field);
81 virtual unsigned int getMinSize (YUIDimension dim)
82 {
return dim == YD_HORIZ ? (shrinkable() ? 30 : 200) : 0; }
84 YGLABEL_WIDGET_IMPL (YInputField)
87 YInputField *YGWidgetFactory::createInputField (YWidget *parent,
const std::string &label,
93 #include "YTimeField.h"
99 YGTimeField (YWidget *parent,
const std::string &label)
100 : YTimeField (NULL, label),
102 YGTK_TYPE_FIELD_ENTRY, NULL)
105 old_time =
"00:00:00";
107 ygtk_field_entry_add_field (field,
':');
108 ygtk_field_entry_add_field (field,
':');
109 ygtk_field_entry_add_field (field,
':');
110 ygtk_field_entry_setup_field (field, 0, 2,
"0123456789");
111 ygtk_field_entry_setup_field (field, 1, 2,
"0123456789");
112 ygtk_field_entry_setup_field (field, 2, 2,
"0123456789");
114 setValue ( old_time );
116 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
119 bool validTime(
const std::string& input_time)
122 std::stringstream ss;
126 if (!(ss >> tm1.tm_hour))
130 if (!(ss >> tm1.tm_min))
134 if (!(ss >> tm1.tm_sec))
137 return (tm1.tm_hour<=23 && tm1.tm_min <= 59 && tm1.tm_sec <= 59);
141 virtual void setValue (
const std::string &time)
144 if (time.empty())
return;
145 if (!validTime(time))
return;
147 char hours[3], mins[3], secs[3];
148 sscanf (time.c_str(),
"%2s:%2s:%2s", hours, mins, secs);
151 ygtk_field_entry_set_field_text (entry, 0, hours);
152 ygtk_field_entry_set_field_text (entry, 1, mins);
153 ygtk_field_entry_set_field_text (entry, 2, secs);
158 virtual std::string value()
160 const gchar *hours, *mins, *secs;
162 if (!gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 0)) ||
163 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 1)) ||
164 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 2)))
167 hours = ygtk_field_entry_get_field_text (entry, 0);
168 mins = ygtk_field_entry_get_field_text (entry, 1);
169 secs = ygtk_field_entry_get_field_text (entry, 2);
171 gchar *time = g_strdup_printf (
"%02d:%02d:%02d", atoi (hours), atoi (mins), atoi (secs));
172 std::string str (time);
178 static void value_changed_cb (
YGtkFieldEntry *entry, gint field_nb,
181 if (!gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 0)) ||
182 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 1)) ||
183 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 2)))
186 if (pThis->validTime(pThis->value()))
188 pThis->old_time = pThis->value();
189 pThis->emitEvent (YEvent::ValueChanged);
193 pThis->setValue(pThis->old_time);
197 YGLABEL_WIDGET_IMPL (YTimeField)
200 YTimeField *YGOptionalWidgetFactory::createTimeField (YWidget *parent,
const std::string &label)
203 #include "YDateField.h"
204 #include "ygtkmenubutton.h"
208 GtkWidget *m_calendar, *m_popup_calendar;
209 std::string old_date;
212 YGDateField (YWidget *parent,
const std::string &label)
213 : YDateField (NULL, label),
214 YGLabeledWidget (
this, parent, label, YD_HORIZ, YGTK_TYPE_FIELD_ENTRY, NULL)
217 old_date =
"2000-01-01";
218 ygtk_field_entry_add_field (getField(),
'-');
219 ygtk_field_entry_add_field (getField(),
'-');
220 ygtk_field_entry_add_field (getField(),
'-');
221 ygtk_field_entry_setup_field (getField(), 0, 4,
"0123456789");
222 ygtk_field_entry_setup_field (getField(), 1, 2,
"0123456789");
223 ygtk_field_entry_setup_field (getField(), 2, 2,
"0123456789");
225 m_calendar = gtk_calendar_new();
226 gtk_widget_show (m_calendar);
227 GtkWidget *popup = ygtk_popup_window_new (m_calendar);
229 GtkWidget *menu_button = ygtk_menu_button_new_with_label (
"");
230 ygtk_menu_button_set_popup (YGTK_MENU_BUTTON (menu_button), popup);
231 gtk_widget_show (menu_button);
232 gtk_box_pack_start (GTK_BOX (getWidget()), menu_button, FALSE, TRUE, 6);
236 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
237 connect (m_calendar,
"day-selected", G_CALLBACK (calendar_changed_cb),
this);
238 g_signal_connect (G_OBJECT (m_calendar),
"day-selected-double-click",
239 G_CALLBACK (double_click_cb), popup);
242 inline GtkCalendar *getCalendar()
243 {
return GTK_CALENDAR (m_calendar); }
245 {
return YGTK_FIELD_ENTRY (getWidget()); }
247 bool validDate(
const std::string& input_date)
249 std::wstringstream ss;
250 wdate_input_facet * fac =
new wdate_input_facet(L
"%Y-%m-%d");
251 ss.imbue(std::locale(std::locale::classic(), fac));
254 ss << input_date.c_str();
260 virtual void setValue (
const std::string &date)
263 if (date.empty())
return;
264 if (!validDate(date))
return;
266 char year[5], month[3], day[3];
267 sscanf (date.c_str(),
"%4s-%2s-%2s", year, month, day);
269 gtk_calendar_select_month (getCalendar(), atoi (month)-1, atoi (year));
270 gtk_calendar_select_day (getCalendar(), atoi (day));
272 ygtk_field_entry_set_field_text (getField(), 0, year);
273 ygtk_field_entry_set_field_text (getField(), 1, month);
274 ygtk_field_entry_set_field_text (getField(), 2, day);
278 virtual std::string value()
280 if (gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (getField(), 0)) < 4 ||
281 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (getField(), 1)) ||
282 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (getField(), 2)))
285 const gchar *year, *month, *day;
286 year = ygtk_field_entry_get_field_text (getField(), 0);
287 month = ygtk_field_entry_get_field_text (getField(), 1);
288 day = ygtk_field_entry_get_field_text (getField(), 2);
290 gchar *time = g_strdup_printf (
"%04d-%02d-%02d", atoi (year),
291 atoi (month), atoi (day));
292 std::string str (time);
298 static void value_changed_cb (
YGtkFieldEntry *entry, gint field_nb,
301 if (gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 0)) < 4 ||
302 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 1)) ||
303 !gtk_entry_get_text_length (ygtk_field_entry_get_field_widget (entry, 2)))
306 std::string new_date = pThis->value();
307 bool changed =
false;
309 if (pThis->validDate(new_date))
312 pThis->old_date = new_date;
316 pThis->setValue(pThis->old_date);
321 int year, month, day;
322 year = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 0));
323 month = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 1));
324 day = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 2));
326 g_signal_handlers_block_by_func (pThis->getCalendar(),
327 (gpointer) calendar_changed_cb, pThis);
328 gtk_calendar_select_month (pThis->getCalendar(), month-1, year);
329 gtk_calendar_select_day (pThis->getCalendar(), day);
330 g_signal_handlers_unblock_by_func (pThis->getCalendar(),
331 (gpointer) calendar_changed_cb, pThis);
333 pThis->emitEvent (YEvent::ValueChanged);
337 static void calendar_changed_cb (GtkCalendar *calendar,
YGDateField *pThis)
339 guint year, month, day;
340 gtk_calendar_get_date (calendar, &year, &month, &day);
343 gchar *year_str, *month_str, *day_str;
344 year_str = g_strdup_printf (
"%d", year);
345 month_str = g_strdup_printf (
"%d", month);
346 day_str = g_strdup_printf (
"%d", day);
348 g_signal_handlers_block_by_func (pThis->getField(),
349 (gpointer) value_changed_cb, pThis);
352 ygtk_field_entry_set_field_text (entry, 0, year_str);
353 ygtk_field_entry_set_field_text (entry, 1, month_str);
354 ygtk_field_entry_set_field_text (entry, 2, day_str);
356 pThis->old_date = pThis->value();
357 g_signal_handlers_unblock_by_func (pThis->getField(),
358 (gpointer) value_changed_cb, pThis);
364 pThis->emitEvent (YEvent::ValueChanged);
367 static void double_click_cb (GtkCalendar *calendar,
YGtkPopupWindow *popup)
370 gtk_widget_hide (GTK_WIDGET (popup));
373 YGLABEL_WIDGET_IMPL (YDateField)
376 YDateField *YGOptionalWidgetFactory::createDateField (YWidget *parent,
const std::string &label)
379 #include "YTimezoneSelector.h"
380 #include "ygtktimezonepicker.h"
386 const std::map <std::string, std::string> &timezones)
387 : YTimezoneSelector (NULL, pixmap, timezones),
388 YGWidget (
this, parent, YGTK_TYPE_TIME_ZONE_PICKER, NULL)
390 setStretchable (YD_HORIZ,
true);
391 setStretchable (YD_VERT,
true);
392 ygtk_time_zone_picker_set_map (YGTK_TIME_ZONE_PICKER (getWidget()),
393 pixmap.c_str(), convert_code_to_name, (gpointer) &timezones);
395 connect (getWidget(),
"zone-clicked", G_CALLBACK (zone_clicked_cb),
this);
399 virtual std::string currentZone()
const
402 const gchar *zone = ygtk_time_zone_picker_get_current_zone (
403 YGTK_TIME_ZONE_PICKER (pThis->getWidget()));
406 return std::string();
409 virtual void setCurrentZone (
const std::string &zone,
bool zoom)
412 ygtk_time_zone_picker_set_current_zone (YGTK_TIME_ZONE_PICKER (getWidget()),
417 static const gchar *convert_code_to_name (
const gchar *code, gpointer pData)
419 const std::map <std::string, std::string> *timezones =
420 (std::map <std::string, std::string> *) pData;
421 std::map <std::string, std::string>::const_iterator name =
422 timezones->find (code);
423 if (name == timezones->end())
425 return name->second.c_str();
431 { pThis->emitEvent (YEvent::ValueChanged); }
433 YGWIDGET_IMPL_COMMON (YTimezoneSelector)
436 YTimezoneSelector *YGOptionalWidgetFactory::createTimezoneSelector (YWidget *parent,
437 const std::string &pixmap,
const std::map <std::string, std::string> &timezones)