xed-encodings-combo-box: Clean up code styling

This commit is contained in:
JosephMcc 2016-12-11 20:43:00 -08:00
parent f1c1e6c689
commit 71306fa224
1 changed files with 261 additions and 282 deletions

View File

@ -42,392 +42,374 @@
#define ENCODING_KEY "Enconding" #define ENCODING_KEY "Enconding"
#define XED_ENCODINGS_COMBO_BOX_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \ #define XED_ENCODINGS_COMBO_BOX_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
XED_TYPE_ENCODINGS_COMBO_BOX, \ XED_TYPE_ENCODINGS_COMBO_BOX, \
XedEncodingsComboBoxPrivate)) XedEncodingsComboBoxPrivate))
struct _XedEncodingsComboBoxPrivate struct _XedEncodingsComboBoxPrivate
{ {
GtkListStore *store; GtkListStore *store;
glong changed_id; glong changed_id;
guint activated_item; guint activated_item;
guint save_mode : 1; guint save_mode : 1;
}; };
enum enum
{ {
NAME_COLUMN, NAME_COLUMN,
ENCODING_COLUMN, ENCODING_COLUMN,
ADD_COLUMN, ADD_COLUMN,
N_COLUMNS N_COLUMNS
}; };
/* Properties */ /* Properties */
enum enum
{ {
PROP_0, PROP_0,
PROP_SAVE_MODE PROP_SAVE_MODE
}; };
G_DEFINE_TYPE(XedEncodingsComboBox, xed_encodings_combo_box, GTK_TYPE_COMBO_BOX) G_DEFINE_TYPE(XedEncodingsComboBox, xed_encodings_combo_box, GTK_TYPE_COMBO_BOX)
static void update_menu (XedEncodingsComboBox *combo_box); static void update_menu (XedEncodingsComboBox *combo_box);
static void static void
xed_encodings_combo_box_set_property (GObject *object, xed_encodings_combo_box_set_property (GObject *object,
guint prop_id, guint prop_id,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
XedEncodingsComboBox *combo; XedEncodingsComboBox *combo;
combo = XED_ENCODINGS_COMBO_BOX (object); combo = XED_ENCODINGS_COMBO_BOX (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_SAVE_MODE: case PROP_SAVE_MODE:
combo->priv->save_mode = g_value_get_boolean (value); combo->priv->save_mode = g_value_get_boolean (value);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
} }
} }
static void static void
xed_encodings_combo_box_get_property (GObject *object, xed_encodings_combo_box_get_property (GObject *object,
guint prop_id, guint prop_id,
GValue *value, GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
XedEncodingsComboBox *combo; XedEncodingsComboBox *combo;
combo = XED_ENCODINGS_COMBO_BOX (object); combo = XED_ENCODINGS_COMBO_BOX (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_SAVE_MODE: case PROP_SAVE_MODE:
g_value_set_boolean (value, combo->priv->save_mode); g_value_set_boolean (value, combo->priv->save_mode);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
} }
} }
static void static void
xed_encodings_combo_box_dispose (GObject *object) xed_encodings_combo_box_dispose (GObject *object)
{ {
XedEncodingsComboBox *combo = XED_ENCODINGS_COMBO_BOX (object); XedEncodingsComboBox *combo = XED_ENCODINGS_COMBO_BOX (object);
if (combo->priv->store != NULL) if (combo->priv->store != NULL)
{ {
g_object_unref (combo->priv->store); g_object_unref (combo->priv->store);
combo->priv->store = NULL; combo->priv->store = NULL;
} }
G_OBJECT_CLASS (xed_encodings_combo_box_parent_class)->dispose (object); G_OBJECT_CLASS (xed_encodings_combo_box_parent_class)->dispose (object);
} }
static void static void
xed_encodings_combo_box_class_init (XedEncodingsComboBoxClass *klass) xed_encodings_combo_box_class_init (XedEncodingsComboBoxClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = xed_encodings_combo_box_set_property; object_class->set_property = xed_encodings_combo_box_set_property;
object_class->get_property = xed_encodings_combo_box_get_property; object_class->get_property = xed_encodings_combo_box_get_property;
object_class->dispose = xed_encodings_combo_box_dispose; object_class->dispose = xed_encodings_combo_box_dispose;
g_object_class_install_property (object_class, g_object_class_install_property (object_class,
PROP_SAVE_MODE, PROP_SAVE_MODE,
g_param_spec_boolean ("save-mode", g_param_spec_boolean ("save-mode",
"Save Mode", "Save Mode",
"Save Mode", "Save Mode",
FALSE, FALSE,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS)); G_PARAM_STATIC_STRINGS));
g_type_class_add_private (object_class, sizeof (XedEncodingsComboBoxPrivate)); g_type_class_add_private (object_class, sizeof (XedEncodingsComboBoxPrivate));
} }
static void static void
dialog_response_cb (GtkDialog *dialog, dialog_response_cb (GtkDialog *dialog,
gint response_id, gint response_id,
XedEncodingsComboBox *menu) XedEncodingsComboBox *menu)
{ {
if (response_id == GTK_RESPONSE_OK) if (response_id == GTK_RESPONSE_OK)
{ {
update_menu (menu); update_menu (menu);
} }
gtk_widget_destroy (GTK_WIDGET (dialog)); gtk_widget_destroy (GTK_WIDGET (dialog));
} }
static void static void
add_or_remove (XedEncodingsComboBox *menu, add_or_remove (XedEncodingsComboBox *menu,
GtkTreeModel *model) GtkTreeModel *model)
{ {
GtkTreeIter iter; GtkTreeIter iter;
gboolean add_item = FALSE; gboolean add_item = FALSE;
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (menu), &iter)) if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (menu), &iter))
{ {
gtk_tree_model_get (model, &iter, gtk_tree_model_get (model, &iter, ADD_COLUMN, &add_item, -1);
ADD_COLUMN, &add_item, }
-1);
}
if (!add_item) if (!add_item)
{ {
menu->priv->activated_item = gtk_combo_box_get_active (GTK_COMBO_BOX (menu)); menu->priv->activated_item = gtk_combo_box_get_active (GTK_COMBO_BOX (menu));
} }
else else
{ {
GtkWidget *dialog; GtkWidget *dialog;
GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu)); GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu));
if (!gtk_widget_is_toplevel (toplevel)) if (!gtk_widget_is_toplevel (toplevel))
toplevel = NULL; {
toplevel = NULL;
}
g_signal_handler_block (menu, menu->priv->changed_id); g_signal_handler_block (menu, menu->priv->changed_id);
gtk_combo_box_set_active (GTK_COMBO_BOX (menu), gtk_combo_box_set_active (GTK_COMBO_BOX (menu), menu->priv->activated_item);
menu->priv->activated_item); g_signal_handler_unblock (menu, menu->priv->changed_id);
g_signal_handler_unblock (menu, menu->priv->changed_id);
dialog = xed_encodings_dialog_new(); dialog = xed_encodings_dialog_new();
if (toplevel != NULL) if (toplevel != NULL)
{ {
GtkWindowGroup *wg; GtkWindowGroup *wg;
gtk_window_set_transient_for (GTK_WINDOW (dialog), gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (toplevel));
GTK_WINDOW (toplevel));
wg = gtk_window_get_group (GTK_WINDOW (toplevel)); wg = gtk_window_get_group (GTK_WINDOW (toplevel));
if (wg == NULL) if (wg == NULL)
{ {
wg = gtk_window_group_new (); wg = gtk_window_group_new ();
gtk_window_group_add_window (wg, gtk_window_group_add_window (wg, GTK_WINDOW (toplevel));
GTK_WINDOW (toplevel)); }
}
gtk_window_group_add_window (wg, gtk_window_group_add_window (wg, GTK_WINDOW (dialog));
GTK_WINDOW (dialog)); }
}
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
g_signal_connect (dialog, g_signal_connect (dialog, "response", G_CALLBACK (dialog_response_cb), menu);
"response",
G_CALLBACK (dialog_response_cb),
menu);
gtk_widget_show (dialog); gtk_widget_show (dialog);
} }
} }
static gboolean static gboolean
separator_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data) separator_func (GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{ {
gchar *str; gchar *str;
gboolean ret; gboolean ret;
gtk_tree_model_get (model, iter, NAME_COLUMN, &str, -1); gtk_tree_model_get (model, iter, NAME_COLUMN, &str, -1);
ret = (str == NULL || *str == '\0'); ret = (str == NULL || *str == '\0');
g_free (str); g_free (str);
return ret; return ret;
} }
static void static void
update_menu (XedEncodingsComboBox *menu) update_menu (XedEncodingsComboBox *menu)
{ {
GtkListStore *store; GtkListStore *store;
GtkTreeIter iter; GtkTreeIter iter;
GSList *encodings, *l; GSList *encodings, *l;
gchar *str; gchar *str;
const XedEncoding *utf8_encoding; const XedEncoding *utf8_encoding;
const XedEncoding *current_encoding; const XedEncoding *current_encoding;
store = menu->priv->store; store = menu->priv->store;
/* Unset the previous model */ /* Unset the previous model */
g_signal_handler_block (menu, menu->priv->changed_id); g_signal_handler_block (menu, menu->priv->changed_id);
gtk_list_store_clear (store); gtk_list_store_clear (store);
gtk_combo_box_set_model (GTK_COMBO_BOX (menu), gtk_combo_box_set_model (GTK_COMBO_BOX (menu), NULL);
NULL);
utf8_encoding = xed_encoding_get_utf8 (); utf8_encoding = xed_encoding_get_utf8 ();
current_encoding = xed_encoding_get_current (); current_encoding = xed_encoding_get_current ();
if (!menu->priv->save_mode) if (!menu->priv->save_mode)
{ {
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, _("Automatically Detected"), NAME_COLUMN, _("Automatically Detected"),
ENCODING_COLUMN, NULL, ENCODING_COLUMN, NULL,
ADD_COLUMN, FALSE, ADD_COLUMN, FALSE,
-1); -1);
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, "", NAME_COLUMN, "",
ENCODING_COLUMN, NULL, ENCODING_COLUMN, NULL,
ADD_COLUMN, FALSE, ADD_COLUMN, FALSE,
-1); -1);
} }
if (current_encoding != utf8_encoding) if (current_encoding != utf8_encoding)
str = xed_encoding_to_string (utf8_encoding); {
else str = xed_encoding_to_string (utf8_encoding);
str = g_strdup_printf (_("Current Locale (%s)"), }
xed_encoding_get_charset (utf8_encoding)); else
{
str = g_strdup_printf (_("Current Locale (%s)"), xed_encoding_get_charset (utf8_encoding));
}
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, str, NAME_COLUMN, str,
ENCODING_COLUMN, utf8_encoding, ENCODING_COLUMN, utf8_encoding,
ADD_COLUMN, FALSE, ADD_COLUMN, FALSE,
-1); -1);
g_free (str); g_free (str);
if ((utf8_encoding != current_encoding) && if ((utf8_encoding != current_encoding) && (current_encoding != NULL))
(current_encoding != NULL)) {
{ str = g_strdup_printf (_("Current Locale (%s)"), xed_encoding_get_charset (current_encoding));
str = g_strdup_printf (_("Current Locale (%s)"),
xed_encoding_get_charset (current_encoding));
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, str, NAME_COLUMN, str,
ENCODING_COLUMN, current_encoding, ENCODING_COLUMN, current_encoding,
ADD_COLUMN, FALSE, ADD_COLUMN, FALSE,
-1); -1);
g_free (str); g_free (str);
} }
encodings = xed_prefs_manager_get_shown_in_menu_encodings (); encodings = xed_prefs_manager_get_shown_in_menu_encodings ();
for (l = encodings; l != NULL; l = g_slist_next (l)) for (l = encodings; l != NULL; l = g_slist_next (l))
{ {
const XedEncoding *enc = (const XedEncoding *)l->data; const XedEncoding *enc = (const XedEncoding *)l->data;
if ((enc != current_encoding) && if ((enc != current_encoding) && (enc != utf8_encoding) && (enc != NULL))
(enc != utf8_encoding) && {
(enc != NULL)) str = xed_encoding_to_string (enc);
{
str = xed_encoding_to_string (enc);
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, str, NAME_COLUMN, str,
ENCODING_COLUMN, enc, ENCODING_COLUMN, enc,
ADD_COLUMN, FALSE, ADD_COLUMN, FALSE,
-1); -1);
g_free (str); g_free (str);
} }
} }
g_slist_free (encodings); g_slist_free (encodings);
if (xed_prefs_manager_shown_in_menu_encodings_can_set ()) if (xed_prefs_manager_shown_in_menu_encodings_can_set ())
{ {
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
/* separator */ /* separator */
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, "", NAME_COLUMN, "",
ENCODING_COLUMN, NULL, ENCODING_COLUMN, NULL,
ADD_COLUMN, FALSE, ADD_COLUMN, FALSE,
-1); -1);
gtk_list_store_append (store, &iter); gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, gtk_list_store_set (store, &iter,
NAME_COLUMN, _("Add or Remove..."), NAME_COLUMN, _("Add or Remove..."),
ENCODING_COLUMN, NULL, ENCODING_COLUMN, NULL,
ADD_COLUMN, TRUE, ADD_COLUMN, TRUE,
-1); -1);
} }
/* set the model back */ /* set the model back */
gtk_combo_box_set_model (GTK_COMBO_BOX (menu), gtk_combo_box_set_model (GTK_COMBO_BOX (menu), GTK_TREE_MODEL (menu->priv->store));
GTK_TREE_MODEL (menu->priv->store)); gtk_combo_box_set_active (GTK_COMBO_BOX (menu), 0);
gtk_combo_box_set_active (GTK_COMBO_BOX (menu), 0);
g_signal_handler_unblock (menu, menu->priv->changed_id); g_signal_handler_unblock (menu, menu->priv->changed_id);
} }
static void static void
xed_encodings_combo_box_init (XedEncodingsComboBox *menu) xed_encodings_combo_box_init (XedEncodingsComboBox *menu)
{ {
GtkCellRenderer *text_renderer; GtkCellRenderer *text_renderer;
menu->priv = XED_ENCODINGS_COMBO_BOX_GET_PRIVATE (menu); menu->priv = XED_ENCODINGS_COMBO_BOX_GET_PRIVATE (menu);
menu->priv->store = gtk_list_store_new (N_COLUMNS, menu->priv->store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
G_TYPE_STRING,
G_TYPE_POINTER,
G_TYPE_BOOLEAN);
/* Setup up the cells */ /* Setup up the cells */
text_renderer = gtk_cell_renderer_text_new (); text_renderer = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (menu), gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (menu), text_renderer, TRUE);
text_renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (menu), gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (menu),
text_renderer, text_renderer,
"text", "text",
NAME_COLUMN, NAME_COLUMN,
NULL); NULL);
gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (menu), gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (menu), separator_func, NULL, NULL);
separator_func, NULL,
NULL);
menu->priv->changed_id = g_signal_connect (menu, "changed", menu->priv->changed_id = g_signal_connect (menu, "changed", G_CALLBACK (add_or_remove), menu->priv->store);
G_CALLBACK (add_or_remove),
menu->priv->store);
update_menu (menu); update_menu (menu);
} }
GtkWidget * GtkWidget *
xed_encodings_combo_box_new (gboolean save_mode) xed_encodings_combo_box_new (gboolean save_mode)
{ {
return g_object_new (XED_TYPE_ENCODINGS_COMBO_BOX, return g_object_new (XED_TYPE_ENCODINGS_COMBO_BOX,
"save_mode", save_mode, "save_mode", save_mode,
NULL); NULL);
} }
const XedEncoding * const XedEncoding *
xed_encodings_combo_box_get_selected_encoding (XedEncodingsComboBox *menu) xed_encodings_combo_box_get_selected_encoding (XedEncodingsComboBox *menu)
{ {
GtkTreeIter iter; GtkTreeIter iter;
g_return_val_if_fail (XED_IS_ENCODINGS_COMBO_BOX (menu), NULL); g_return_val_if_fail (XED_IS_ENCODINGS_COMBO_BOX (menu), NULL);
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (menu), &iter)) if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (menu), &iter))
{ {
const XedEncoding *ret; const XedEncoding *ret;
GtkTreeModel *model; GtkTreeModel *model;
model = gtk_combo_box_get_model (GTK_COMBO_BOX (menu)); model = gtk_combo_box_get_model (GTK_COMBO_BOX (menu));
gtk_tree_model_get (model, &iter, gtk_tree_model_get (model, &iter, ENCODING_COLUMN, &ret, -1);
ENCODING_COLUMN, &ret,
-1);
return ret; return ret;
} }
return NULL; return NULL;
} }
/** /**
@ -437,33 +419,30 @@ xed_encodings_combo_box_get_selected_encoding (XedEncodingsComboBox *menu)
**/ **/
void void
xed_encodings_combo_box_set_selected_encoding (XedEncodingsComboBox *menu, xed_encodings_combo_box_set_selected_encoding (XedEncodingsComboBox *menu,
const XedEncoding *encoding) const XedEncoding *encoding)
{ {
GtkTreeIter iter; GtkTreeIter iter;
GtkTreeModel *model; GtkTreeModel *model;
gboolean b; gboolean b;
g_return_if_fail (XED_IS_ENCODINGS_COMBO_BOX (menu)); g_return_if_fail (XED_IS_ENCODINGS_COMBO_BOX (menu));
g_return_if_fail (GTK_IS_COMBO_BOX (menu)); g_return_if_fail (GTK_IS_COMBO_BOX (menu));
model = gtk_combo_box_get_model (GTK_COMBO_BOX (menu)); model = gtk_combo_box_get_model (GTK_COMBO_BOX (menu));
b = gtk_tree_model_get_iter_first (model, &iter); b = gtk_tree_model_get_iter_first (model, &iter);
while (b) while (b)
{ {
const XedEncoding *enc; const XedEncoding *enc;
gtk_tree_model_get (model, &iter, gtk_tree_model_get (model, &iter, ENCODING_COLUMN, &enc, -1);
ENCODING_COLUMN, &enc,
-1);
if (enc == encoding) if (enc == encoding)
{ {
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (menu), gtk_combo_box_set_active_iter (GTK_COMBO_BOX (menu), &iter);
&iter);
return; return;
} }
b = gtk_tree_model_iter_next (model, &iter); b = gtk_tree_model_iter_next (model, &iter);
} }
} }