--- trunk/panel-plugin/datetime-dialog.c 2008-05-27 21:47:03.000000000 -0700 +++ exp1/panel-plugin/datetime-dialog.c 2008-06-01 10:57:35.000000000 -0700 @@ -1,4 +1,4 @@ -/* $Id$ +/* * * Copyright (C) 2003 Choe Hwanjin(krisna@kldp.org) * Copyright (c) 2006 Remco den Breeje @@ -44,27 +44,52 @@ N_("Time, then date") }; -/* builtin formats - derived from xfce4-panel-clock.patch by Nick Schermer */ -#define TIME_FORMAT_COUNT 6 -static const char *time_format[] = { - "%H:%M", "%H:%M:%S", - "%l:%M %P", "%l:%M:%S %P", - "---", - N_("Custom...") +typedef enum { + DT_COMBOBOX_ITEM_TYPE_FORMAT, /* format string that is replaced with an example date or time */ + DT_COMBOBOX_ITEM_TYPE_TEXT, /* literal item text, possibly with translation */ + DT_COMBOBOX_ITEM_TYPE_SEPARATOR, /* inactive separator */ +} dt_combobox_item_type; + +typedef struct { + char *item; + dt_combobox_item_type type; +} dt_combobox_item; + +/* + * Builtin formats are derived from xfce4-panel-clock.patch by Nick Schermer. + */ +static const dt_combobox_item dt_combobox_date[] = { + "%Y/%m/%d", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%m/%d/%Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%B %d, %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%b %d, %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%A, %B %d, %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%a, %b %d, %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%d/%m/%Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%d %B %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%d %b %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%A, %d %B %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%a, %d %b %Y", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "-", DT_COMBOBOX_ITEM_TYPE_SEPARATOR, /* item member is a placeholder */ + N_("Custom..."), DT_COMBOBOX_ITEM_TYPE_TEXT }; +#define DT_COMBOBOX_DATE_COUNT (sizeof(dt_combobox_date)/sizeof(dt_combobox_item)) -#define DATE_FORMAT_COUNT 13 -static const char *date_format[] = { - "%Y/%m/%d", "%m/%d/%Y", - "%B %d, %Y", "%b %d, %Y", - "%A, %B %d, %Y", "%a, %b %d, %Y", - "%d/%m/%Y", "%d %B %Y", - "%d %b %Y", "%A, %d %B %Y", - "%a, %d %b %Y", "---", - N_("Custom...") +static const dt_combobox_item dt_combobox_time[] = { + "%H:%M", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%H:%M:%S", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%l:%M %P", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "%l:%M:%S %P", DT_COMBOBOX_ITEM_TYPE_FORMAT, + "-", DT_COMBOBOX_ITEM_TYPE_SEPARATOR, /* item member is a placeholder */ + N_("Custom..."), DT_COMBOBOX_ITEM_TYPE_TEXT }; +#define DT_COMBOBOX_TIME_COUNT (sizeof(dt_combobox_time)/sizeof(dt_combobox_item)) -/* example timestamp to show in the dialog */ +/* + * Example timestamp to show in the dialog. + * Compute with: + * date +%s -u -d 'dec 31 1999 23:59:59' + */ static const time_t example_time_t = 946684799; /* @@ -167,7 +192,7 @@ } /* - * Row seperator for format-comboboxes of date and time + * Row separator for format-comboboxes of date and time * derived from xfce4-panel-clock.patch by Nick Schermer */ static gboolean @@ -175,21 +200,15 @@ GtkTreeIter *iter, gpointer data) { - /* Hack: the 2nd item from the end will be a separator */ - - gint total, current; - GtkTreePath *path; + const dt_combobox_item *items = (dt_combobox_item *)data; + gint current; + GtkTreePath *path; path = gtk_tree_model_get_path (model, iter); current = gtk_tree_path_get_indices (path)[0]; gtk_tree_path_free (path); - total = gtk_tree_model_iter_n_children (model, NULL); - - if (total == (current + 2)) - return TRUE; - else - return FALSE; + return(items[current].type == DT_COMBOBOX_ITEM_TYPE_SEPARATOR); } /* @@ -200,18 +219,20 @@ { const gint active = gtk_combo_box_get_active(cbox); - /* check if user choose the last option (custom entry) */ - if(active > (DATE_FORMAT_COUNT - 2)) - { - /* set last text and enable field */ - gtk_entry_set_text(GTK_ENTRY(dt->date_format_entry), dt->date_format); - gtk_widget_show(dt->date_format_entry); - } - else + switch(dt_combobox_date[active].type) { - /* disable custom-text-field and tell datetime which format we choose */ - gtk_widget_hide(dt->date_format_entry); - datetime_apply_format(dt, date_format[active], NULL); + case DT_COMBOBOX_ITEM_TYPE_FORMAT: + /* disable custom-text-field and tell datetime which format we choose */ + gtk_widget_hide(dt->date_format_entry); + datetime_apply_format(dt, dt_combobox_date[active].item, NULL); + break; + case DT_COMBOBOX_ITEM_TYPE_TEXT: + /* set last text and enable field */ + gtk_entry_set_text(GTK_ENTRY(dt->date_format_entry), dt->date_format); + gtk_widget_show(dt->date_format_entry); + break; + default: + break; /* separators should never be active */ } datetime_update(dt); @@ -225,18 +246,20 @@ { const gint active = gtk_combo_box_get_active(cbox); - /* check if user choose the last option (custom entry) */ - if(active > (TIME_FORMAT_COUNT - 2)) - { - /* set last text and enable field */ - gtk_entry_set_text(GTK_ENTRY(dt->time_format_entry), dt->time_format); - gtk_widget_show(dt->time_format_entry); - } - else + switch(dt_combobox_time[active].type) { - /* disable custom-text-field and tell datetime which format we choose */ - gtk_widget_hide(dt->time_format_entry); - datetime_apply_format(dt, NULL, time_format[active]); + case DT_COMBOBOX_ITEM_TYPE_FORMAT: + /* disable custom-text-field and tell datetime which format we choose */ + gtk_widget_hide(dt->time_format_entry); + datetime_apply_format(dt, NULL, dt_combobox_time[active].item); + break; + case DT_COMBOBOX_ITEM_TYPE_TEXT: + /* set last text and enable field */ + gtk_entry_set_text(GTK_ENTRY(dt->time_format_entry), dt->time_format); + gtk_widget_show(dt->time_format_entry); + break; + default: + break; /* separators should never be active */ } datetime_update(dt); @@ -285,7 +308,7 @@ { gint i; gchar *utf8str; - struct tm *exampletm; + struct tm *exampletm = gmtime(&example_time_t); GtkWidget *dlg, *frame, *vbox, @@ -395,32 +418,36 @@ /* format combobox */ date_combobox = gtk_combo_box_new_text(); gtk_box_pack_start(GTK_BOX(hbox), date_combobox, TRUE, TRUE, 0); - exampletm = gmtime(&example_time_t); - for(i=0; i < DATE_FORMAT_COUNT; i++) + for(i=0; i < DT_COMBOBOX_DATE_COUNT; i++) { - if(i < DATE_FORMAT_COUNT - 1) + switch(dt_combobox_date[i].type) { - utf8str = datetime_do_utf8strftime(date_format[i], exampletm); - gtk_combo_box_append_text(GTK_COMBO_BOX(date_combobox), utf8str); - g_free(utf8str); + case DT_COMBOBOX_ITEM_TYPE_FORMAT: + utf8str = datetime_do_utf8strftime(dt_combobox_date[i].item, exampletm); + gtk_combo_box_append_text(GTK_COMBO_BOX(date_combobox), utf8str); + g_free(utf8str); + /* set active + * strcmp isn't fast, but it is done only once while opening the dialog + */ + if(strcmp(datetime->date_format, dt_combobox_date[i].item) == 0) + gtk_combo_box_set_active(GTK_COMBO_BOX(date_combobox), i); + break; + case DT_COMBOBOX_ITEM_TYPE_TEXT: + gtk_combo_box_append_text(GTK_COMBO_BOX(date_combobox), _(dt_combobox_date[i].item)); + break; + case DT_COMBOBOX_ITEM_TYPE_SEPARATOR: /* placeholder item does not need to be translated */ + gtk_combo_box_append_text(GTK_COMBO_BOX(date_combobox), dt_combobox_date[i].item); + break; + default: + break; } - else - { - gtk_combo_box_append_text(GTK_COMBO_BOX(date_combobox), _(date_format[i])); - } - - /* set active - * strcmp isn't fast, but it is done only once while opening the dialog - */ - if(strcmp(datetime->date_format,date_format[i]) == 0) - gtk_combo_box_set_active(GTK_COMBO_BOX(date_combobox), i); } - /* if no field selected -> select custom field */ + /* if no item activated -> activate custom item */ if(gtk_combo_box_get_active(GTK_COMBO_BOX(date_combobox)) < 0) - gtk_combo_box_set_active(GTK_COMBO_BOX(date_combobox), DATE_FORMAT_COUNT-1); + gtk_combo_box_set_active(GTK_COMBO_BOX(date_combobox), DT_COMBOBOX_DATE_COUNT-1); gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(date_combobox), combo_box_row_separator, - NULL, NULL); + (gpointer)dt_combobox_date, NULL); g_signal_connect(G_OBJECT(date_combobox), "changed", G_CALLBACK(date_format_changed), datetime); datetime->date_format_combobox = date_combobox; @@ -481,32 +508,36 @@ /* format combobox */ time_combobox = gtk_combo_box_new_text(); gtk_box_pack_start(GTK_BOX(hbox), time_combobox, TRUE, TRUE, 0); - exampletm = gmtime(&example_time_t); - for(i=0; i < TIME_FORMAT_COUNT; i++) + for(i=0; i < DT_COMBOBOX_TIME_COUNT; i++) { - if(i < TIME_FORMAT_COUNT - 1) + switch(dt_combobox_time[i].type) { - utf8str = datetime_do_utf8strftime(time_format[i], exampletm); - gtk_combo_box_append_text(GTK_COMBO_BOX(time_combobox), utf8str); - g_free(utf8str); + case DT_COMBOBOX_ITEM_TYPE_FORMAT: + utf8str = datetime_do_utf8strftime(dt_combobox_time[i].item, exampletm); + gtk_combo_box_append_text(GTK_COMBO_BOX(time_combobox), utf8str); + g_free(utf8str); + /* set active + * strcmp isn't fast, but it is done only once while opening the dialog + */ + if(strcmp(datetime->time_format, dt_combobox_time[i].item) == 0) + gtk_combo_box_set_active(GTK_COMBO_BOX(time_combobox), i); + break; + case DT_COMBOBOX_ITEM_TYPE_TEXT: + gtk_combo_box_append_text(GTK_COMBO_BOX(time_combobox), _(dt_combobox_time[i].item)); + break; + case DT_COMBOBOX_ITEM_TYPE_SEPARATOR: /* placeholder item does not need to be translated */ + gtk_combo_box_append_text(GTK_COMBO_BOX(time_combobox), dt_combobox_time[i].item); + break; + default: + break; } - else - { - gtk_combo_box_append_text(GTK_COMBO_BOX(time_combobox), _(time_format[i])); - } - - /* set active - * strcmp isn't fast, but it is done only once while opening the dialog - */ - if(strcmp(datetime->time_format,time_format[i]) == 0) - gtk_combo_box_set_active(GTK_COMBO_BOX(time_combobox), i); } - /* if no field selected -> select custom field */ + /* if no item activated -> activate custom item */ if(gtk_combo_box_get_active(GTK_COMBO_BOX(time_combobox)) < 0) - gtk_combo_box_set_active(GTK_COMBO_BOX(time_combobox), TIME_FORMAT_COUNT-1); + gtk_combo_box_set_active(GTK_COMBO_BOX(time_combobox), DT_COMBOBOX_TIME_COUNT-1); gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(time_combobox), combo_box_row_separator, - NULL, NULL); + (gpointer)dt_combobox_time, NULL); g_signal_connect(G_OBJECT(time_combobox), "changed", G_CALLBACK(time_format_changed), datetime); datetime->time_format_combobox = time_combobox;