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_setup_field (field, 0, 2,
"0123456789");
102 ygtk_field_entry_setup_field (field, 1, 2,
"0123456789");
104 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
108 virtual void setValue (
const std::string &time)
111 if (time.empty())
return;
112 char hours[3], mins[3];
113 sscanf (time.c_str(),
"%2s:%2s", hours, mins);
116 ygtk_field_entry_set_field_text (entry, 0, hours);
117 ygtk_field_entry_set_field_text (entry, 1, mins);
120 virtual std::string value()
122 const gchar *hours, *mins;
124 hours = ygtk_field_entry_get_field_text (entry, 0);
125 mins = ygtk_field_entry_get_field_text (entry, 1);
127 gchar *time = g_strdup_printf (
"%02d:%02d:00", atoi (hours), atoi (mins));
128 std::string str (time);
134 static void value_changed_cb (
YGtkFieldEntry *entry, gint field_nb,
136 { pThis->emitEvent (YEvent::ValueChanged); }
138 YGLABEL_WIDGET_IMPL (YTimeField)
141 YTimeField *YGOptionalWidgetFactory::createTimeField (YWidget *parent,
const std::string &label)
144 #include "YDateField.h"
145 #include "ygtkmenubutton.h"
149 GtkWidget *m_calendar, *m_popup_calendar;
152 YGDateField (YWidget *parent,
const std::string &label)
153 : YDateField (NULL, label),
154 YGLabeledWidget (
this, parent, label, YD_HORIZ, YGTK_TYPE_FIELD_ENTRY, NULL)
156 ygtk_field_entry_add_field (getField(),
'-');
157 ygtk_field_entry_add_field (getField(),
'-');
158 ygtk_field_entry_add_field (getField(),
'-');
159 ygtk_field_entry_setup_field (getField(), 0, 4,
"0123456789");
160 ygtk_field_entry_setup_field (getField(), 1, 2,
"0123456789");
161 ygtk_field_entry_setup_field (getField(), 2, 2,
"0123456789");
163 m_calendar = gtk_calendar_new();
164 gtk_widget_show (m_calendar);
165 GtkWidget *popup = ygtk_popup_window_new (m_calendar);
167 GtkWidget *menu_button = ygtk_menu_button_new_with_label (
"");
168 ygtk_menu_button_set_popup (YGTK_MENU_BUTTON (menu_button), popup);
169 gtk_widget_show (menu_button);
170 gtk_box_pack_start (GTK_BOX (getWidget()), menu_button, FALSE, TRUE, 6);
172 connect (getWidget(),
"field-entry-changed", G_CALLBACK (value_changed_cb),
this);
173 connect (m_calendar,
"day-selected", G_CALLBACK (calendar_changed_cb),
this);
174 g_signal_connect (G_OBJECT (m_calendar),
"day-selected-double-click",
175 G_CALLBACK (double_click_cb), popup);
178 inline GtkCalendar *getCalendar()
179 {
return GTK_CALENDAR (m_calendar); }
181 {
return YGTK_FIELD_ENTRY (getWidget()); }
184 virtual void setValue (
const std::string &date)
187 if (date.empty())
return;
188 char year[5], month[3], day[3];
189 sscanf (date.c_str(),
"%4s-%2s-%2s", year, month, day);
191 gtk_calendar_select_month (getCalendar(), atoi (month)-1, atoi (year));
192 gtk_calendar_select_day (getCalendar(), atoi (day));
194 ygtk_field_entry_set_field_text (getField(), 0, year);
195 ygtk_field_entry_set_field_text (getField(), 1, month);
196 ygtk_field_entry_set_field_text (getField(), 2, day);
199 virtual std::string value()
201 const gchar *year, *month, *day;
202 year = ygtk_field_entry_get_field_text (getField(), 0);
203 month = ygtk_field_entry_get_field_text (getField(), 1);
204 day = ygtk_field_entry_get_field_text (getField(), 2);
206 gchar *time = g_strdup_printf (
"%04d-%02d-%02d", atoi (year),
207 atoi (month), atoi (day));
208 std::string str (time);
214 static void value_changed_cb (
YGtkFieldEntry *entry, gint field_nb,
217 int year, month, day;
218 year = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 0));
219 month = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 1));
220 day = atoi (ygtk_field_entry_get_field_text (pThis->getField(), 2));
222 if (day < 1 || day > 31 || month < 1 || month > 12)
225 g_signal_handlers_block_by_func (pThis->getCalendar(),
226 (gpointer) calendar_changed_cb, pThis);
228 gtk_calendar_select_month (pThis->getCalendar(), month-1, year);
229 gtk_calendar_select_day (pThis->getCalendar(), day);
231 g_signal_handlers_unblock_by_func (pThis->getCalendar(),
232 (gpointer) calendar_changed_cb, pThis);
234 pThis->emitEvent (YEvent::ValueChanged);
237 static void calendar_changed_cb (GtkCalendar *calendar,
YGDateField *pThis)
239 guint year, month, day;
240 gtk_calendar_get_date (calendar, &year, &month, &day);
243 gchar *year_str, *month_str, *day_str;
244 year_str = g_strdup_printf (
"%d", year);
245 month_str = g_strdup_printf (
"%d", month);
246 day_str = g_strdup_printf (
"%d", day);
248 g_signal_handlers_block_by_func (pThis->getField(),
249 (gpointer) value_changed_cb, pThis);
252 ygtk_field_entry_set_field_text (entry, 0, year_str);
253 ygtk_field_entry_set_field_text (entry, 1, month_str);
254 ygtk_field_entry_set_field_text (entry, 2, day_str);
256 g_signal_handlers_unblock_by_func (pThis->getField(),
257 (gpointer) value_changed_cb, pThis);
263 pThis->emitEvent (YEvent::ValueChanged);
266 static void double_click_cb (GtkCalendar *calendar,
YGtkPopupWindow *popup)
269 gtk_widget_hide (GTK_WIDGET (popup));
272 YGLABEL_WIDGET_IMPL (YDateField)
275 YDateField *YGOptionalWidgetFactory::createDateField (YWidget *parent,
const std::string &label)
278 #include "YTimezoneSelector.h"
279 #include "ygtktimezonepicker.h"
285 const std::map <std::string, std::string> &timezones)
286 : YTimezoneSelector (NULL, pixmap, timezones),
287 YGWidget (
this, parent, YGTK_TYPE_TIME_ZONE_PICKER, NULL)
289 setStretchable (YD_HORIZ,
true);
290 setStretchable (YD_VERT,
true);
291 ygtk_time_zone_picker_set_map (YGTK_TIME_ZONE_PICKER (getWidget()),
292 pixmap.c_str(), convert_code_to_name, (gpointer) &timezones);
294 connect (getWidget(),
"zone-clicked", G_CALLBACK (zone_clicked_cb),
this);
298 virtual std::string currentZone()
const
301 const gchar *zone = ygtk_time_zone_picker_get_current_zone (
302 YGTK_TIME_ZONE_PICKER (pThis->getWidget()));
305 return std::string();
308 virtual void setCurrentZone (
const std::string &zone,
bool zoom)
311 ygtk_time_zone_picker_set_current_zone (YGTK_TIME_ZONE_PICKER (getWidget()),
316 static const gchar *convert_code_to_name (
const gchar *code, gpointer pData)
318 const std::map <std::string, std::string> *timezones =
319 (std::map <std::string, std::string> *) pData;
320 std::map <std::string, std::string>::const_iterator name =
321 timezones->find (code);
322 if (name == timezones->end())
324 return name->second.c_str();
330 { pThis->emitEvent (YEvent::ValueChanged); }
332 YGWIDGET_IMPL_COMMON (YTimezoneSelector)
335 YTimezoneSelector *YGOptionalWidgetFactory::createTimezoneSelector (YWidget *parent,
336 const std::string &pixmap,
const std::map <std::string, std::string> &timezones)