Simplify and cleanup encoding menu building

Rewrite a part of `encodings_init()` to remove duplication and some
hard-coded values.

This gives us the item for UHC back, that was lost when adding CP932 in
9d9f40cd15ad121ff3e3fffd40073706c297f2dd, due to a missing update of
the hard-coded group elements count.
This commit is contained in:
Colomban Wendling 2015-11-25 19:13:59 +01:00
parent 1a84bb6e34
commit 8f9f9c5cf3

View File

@ -404,14 +404,19 @@ void encodings_finalize(void)
void encodings_init(void) void encodings_init(void)
{ {
GtkWidget *item, *menu[2], *submenu, *menu_westeuro, *menu_easteuro, *menu_eastasian, *menu_asian, GtkWidget *menu[2];
*menu_utf8, *menu_middleeast, *item_westeuro, *item_easteuro, *item_eastasian,
*item_asian, *item_utf8, *item_middleeast;
GCallback cb_func[2]; GCallback cb_func[2];
GSList *group = NULL; gint group_sizes[GEANY_ENCODING_GROUPS_MAX] = { 0 };
gchar *label; const gchar *const groups[GEANY_ENCODING_GROUPS_MAX] =
gint order, group_size; {
guint i, j, k; [NONE] = NULL,
[WESTEUROPEAN] = N_("_West European"),
[EASTEUROPEAN] = N_("_East European"),
[EASTASIAN] = N_("East _Asian"),
[ASIAN] = N_("_SE & SW Asian"),
[MIDDLEEASTERN] = N_("_Middle Eastern"),
[UNICODE] = N_("_Unicode"),
};
init_encodings(); init_encodings();
@ -428,67 +433,41 @@ void encodings_init(void)
cb_func[0] = G_CALLBACK(encodings_radio_item_change_cb); cb_func[0] = G_CALLBACK(encodings_radio_item_change_cb);
cb_func[1] = G_CALLBACK(encodings_reload_radio_item_change_cb); cb_func[1] = G_CALLBACK(encodings_reload_radio_item_change_cb);
for (k = 0; k < 2; k++) for (guint i = 0; i < G_N_ELEMENTS(encodings); i++)
group_sizes[encodings[i].group]++;
for (guint k = 0; k < 2; k++)
{ {
menu_westeuro = gtk_menu_new(); GSList *group = NULL;
item_westeuro = gtk_menu_item_new_with_mnemonic(_("_West European")); GtkWidget *submenus[GEANY_ENCODING_GROUPS_MAX];
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_westeuro), menu_westeuro);
gtk_container_add(GTK_CONTAINER(menu[k]), item_westeuro);
gtk_widget_show_all(item_westeuro);
menu_easteuro = gtk_menu_new(); for (guint i = 0; i < GEANY_ENCODING_GROUPS_MAX; i++)
item_easteuro = gtk_menu_item_new_with_mnemonic(_("_East European"));
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_easteuro), menu_easteuro);
gtk_container_add(GTK_CONTAINER(menu[k]), item_easteuro);
gtk_widget_show_all(item_easteuro);
menu_eastasian = gtk_menu_new();
item_eastasian = gtk_menu_item_new_with_mnemonic(_("East _Asian"));
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_eastasian), menu_eastasian);
gtk_container_add(GTK_CONTAINER(menu[k]), item_eastasian);
gtk_widget_show_all(item_eastasian);
menu_asian = gtk_menu_new();
item_asian = gtk_menu_item_new_with_mnemonic(_("_SE & SW Asian"));
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_asian), menu_asian);
gtk_container_add(GTK_CONTAINER(menu[k]), item_asian);
gtk_widget_show_all(item_asian);
menu_middleeast = gtk_menu_new();
item_middleeast = gtk_menu_item_new_with_mnemonic(_("_Middle Eastern"));
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_middleeast), menu_middleeast);
gtk_container_add(GTK_CONTAINER(menu[k]), item_middleeast);
gtk_widget_show_all(item_middleeast);
menu_utf8 = gtk_menu_new();
item_utf8 = gtk_menu_item_new_with_mnemonic(_("_Unicode"));
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_utf8), menu_utf8);
gtk_container_add(GTK_CONTAINER(menu[k]), item_utf8);
gtk_widget_show_all(item_utf8);
/** TODO can it be optimized? ATM 3782 runs at line "if (encodings[j].group ...)" */
for (i = 0; i < GEANY_ENCODING_GROUPS_MAX; i++)
{ {
order = 0; if (! groups[i]) /* NONE */
switch (i) submenus[i] = menu[k];
else
{ {
case WESTEUROPEAN: submenu = menu_westeuro; group_size = 9; break; GtkWidget *item = gtk_menu_item_new_with_mnemonic(_(groups[i]));
case EASTEUROPEAN: submenu = menu_easteuro; group_size = 14; break; submenus[i] = gtk_menu_new();
case EASTASIAN: submenu = menu_eastasian; group_size = 14; break; gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenus[i]);
case ASIAN: submenu = menu_asian; group_size = 9; break; gtk_container_add(GTK_CONTAINER(menu[k]), item);
case MIDDLEEASTERN: submenu = menu_middleeast; group_size = 7; break; gtk_widget_show_all(item);
case UNICODE: submenu = menu_utf8; group_size = 8; break; }
default: submenu = menu[k]; group_size = 1;
} }
while (order < group_size) /* the biggest group has 13 elements */ /** TODO can it be optimized? ATM 3782 runs at line "if (encodings[j].group ...)" */
for (guint i = 0; i < GEANY_ENCODING_GROUPS_MAX; i++)
{ {
for (j = 0; j < GEANY_ENCODINGS_MAX; j++) for (gint order = 0; order < group_sizes[i]; order++)
{
for (guint j = 0; j < GEANY_ENCODINGS_MAX; j++)
{ {
if (encodings[j].group == i && encodings[j].order == order) if (encodings[j].group == i && encodings[j].order == order)
{ {
label = encodings_to_string(&encodings[j]); GtkWidget *item;
if (k == 0) gchar *label = encodings_to_string(&encodings[j]);
if (k == 0) /* Set Encoding menu */
{ {
item = gtk_radio_menu_item_new_with_label(group, label); item = gtk_radio_menu_item_new_with_label(group, label);
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item)); group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item));
@ -497,14 +476,13 @@ void encodings_init(void)
else else
item = gtk_menu_item_new_with_label(label); item = gtk_menu_item_new_with_label(label);
gtk_widget_show(item); gtk_widget_show(item);
gtk_container_add(GTK_CONTAINER(submenu), item); gtk_container_add(GTK_CONTAINER(submenus[i]), item);
g_signal_connect(item, "activate", cb_func[k], g_signal_connect(item, "activate", cb_func[k],
(gpointer) encodings[j].charset); (gpointer) encodings[j].charset);
g_free(label); g_free(label);
break; break;
} }
} }
order++;
} }
} }
} }