401 lines
12 KiB
C
401 lines
12 KiB
C
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
#include <glade/glade.h>
|
|
#include <gtksourceview/gtksourceview.h>
|
|
#include <gtksourceview/gtksourcelanguage.h>
|
|
#include <gtksourceview/gtksourcelanguage.h>
|
|
#include <gtksourceview/gtksourcelanguagesmanager.h>
|
|
#include <gtksourceview/gtksourcetag.h>
|
|
|
|
#include "../gm-app.h"
|
|
#include "gm-preferences-dialog.h"
|
|
#include "../widgets/gm-editor-view.h"
|
|
#include "../gm-support.h"
|
|
#include "../gm-debug.h"
|
|
#include "../gm-options.h"
|
|
#include "../gm-color-table.h"
|
|
|
|
void gm_preferences_dialog_run_dialog();
|
|
|
|
void on_gm_preferences_dialog_check_button_embed_editor_clicked(
|
|
GtkButton *button, gpointer user_data);
|
|
void on_gm_preferences_dialog_check_button_alt_editor_clicked(
|
|
GtkButton *button, gpointer user_data);
|
|
|
|
void on_gm_preferences_dialog_combo_box_scheme_changed(GtkComboBox *box,
|
|
gpointer user_data);
|
|
void on_gm_preferences_dialog_check_button_fg_clicked(
|
|
GtkButton *button, gpointer user_data);
|
|
void on_gm_preferences_dialog_check_button_bg_clicked(
|
|
GtkButton *button, gpointer user_data);
|
|
void on_gm_preferences_dialog_toggle_button_style_clicked(
|
|
GtkButton *button, gpointer user_data);
|
|
|
|
void on_gm_preferences_dialog_tree_view_editor_colors_row_changed(
|
|
GtkTreeSelection *selection, gpointer data);
|
|
void on_gm_preferences_dialog_color_button_fg_color_set(GtkColorButton *widget,
|
|
gpointer data);
|
|
void on_gm_preferences_dialog_color_button_bg_color_set(GtkColorButton *widget,
|
|
gpointer data);
|
|
void on_gm_preferences_dialog_response(GtkDialog *dialog, gint response,
|
|
gpointer user_data);
|
|
|
|
void gm_preferences_dialog_load_colors();
|
|
|
|
typedef struct _GmPreferencesDialog {
|
|
GladeXML *xml;
|
|
GtkWidget *dialog;
|
|
|
|
} GmPreferencesDialog;
|
|
|
|
static const GmKeyValuePair color_mapping[] = {
|
|
{"color_button_fg_default", "fg_default"},
|
|
{"color_button_fg_default_h", "fg_default_h"},
|
|
{"color_button_bg_default", "bg_default"},
|
|
|
|
{"color_button_fg_black", "fg_black"},
|
|
{"color_button_fg_black_h", "fg_black_h"},
|
|
{"color_button_fg_red", "fg_red"},
|
|
{"color_button_fg_red_h", "fg_red_h"},
|
|
{"color_button_fg_green", "fg_green"},
|
|
{"color_button_fg_green_h", "fg_green_h"},
|
|
{"color_button_fg_yellow", "fg_yellow"},
|
|
{"color_button_fg_yellow_h", "fg_yellow_h"},
|
|
{"color_button_fg_blue", "fg_blue"},
|
|
{"color_button_fg_blue_h", "fg_blue_h"},
|
|
{"color_button_fg_purple", "fg_purple"},
|
|
{"color_button_fg_purple_h", "fg_purple_h"},
|
|
{"color_button_fg_cyan", "fg_cyan"},
|
|
{"color_button_fg_cyan_h", "fg_cyan_h"},
|
|
{"color_button_fg_white", "fg_white"},
|
|
{"color_button_fg_white_h", "fg_white_h"},
|
|
|
|
{"color_button_bg_black", "bg_black"},
|
|
{"color_button_bg_red", "bg_red"},
|
|
{"color_button_bg_green", "bg_green"},
|
|
{"color_button_bg_yellow", "bg_yellow"},
|
|
{"color_button_bg_blue", "bg_blue"},
|
|
{"color_button_bg_purple", "bg_purple"},
|
|
{"color_button_bg_cyan", "bg_cyan"},
|
|
{"color_button_bg_white", "bg_white"}
|
|
};
|
|
|
|
static const GmKeyValuePair color_schemes[] = {
|
|
{N_("Default"), "default"},
|
|
{N_("White on black"), "white_on_black"},
|
|
{N_("User defined"), "user"},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static GmPreferencesDialog *preferences = NULL;
|
|
|
|
enum {
|
|
SCHEME_NAME,
|
|
SCHEME_OPTION,
|
|
SCHEME_N_COLUMNS
|
|
};
|
|
|
|
GtkWidget *
|
|
gm_preferences_dialog_widget(gchar *name) {
|
|
return glade_xml_get_widget(preferences->xml, name);
|
|
}
|
|
|
|
void
|
|
gm_preferences_dialog_init_combo_box_scheme() {
|
|
GtkComboBox *box = GTK_COMBO_BOX(gm_preferences_dialog_widget(
|
|
"combo_box_scheme"));
|
|
GtkListStore *store = gtk_list_store_new(SCHEME_N_COLUMNS, G_TYPE_STRING,
|
|
G_TYPE_STRING);
|
|
GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
|
|
GtkTreeIter iter;
|
|
int i, select = 0;
|
|
const gchar *scheme = gm_options_get(gm_app_options(gm_app_instance()),
|
|
"color_scheme");
|
|
|
|
gtk_cell_layout_clear(GTK_CELL_LAYOUT(box));
|
|
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(box), renderer, TRUE);
|
|
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(box), renderer, "text",
|
|
SCHEME_NAME, NULL);
|
|
|
|
gtk_combo_box_set_model(box, GTK_TREE_MODEL(store));
|
|
|
|
i = 0;
|
|
|
|
while (color_schemes[i].key != NULL) {
|
|
gtk_list_store_append(store, &iter);
|
|
gtk_list_store_set(store, &iter, SCHEME_NAME, color_schemes[i].key,
|
|
SCHEME_OPTION, color_schemes[i].value, -1);
|
|
|
|
if (scheme != NULL && strcasecmp(scheme, color_schemes[i].value) == 0) {
|
|
select = i;
|
|
}
|
|
|
|
++i;
|
|
}
|
|
|
|
gtk_combo_box_set_active(box, select);
|
|
}
|
|
|
|
#define GM_PREFERENCES_DIALOG_XML \
|
|
PACKAGE_DATA_DIR "/" PACKAGE "/ui/gm-preferences.glade"
|
|
|
|
void
|
|
gm_preferences_dialog_run() {
|
|
const gchar *alt_editor;
|
|
GmOptions *options = gm_app_options(gm_app_instance());
|
|
|
|
if (preferences != NULL) {
|
|
gtk_window_present(GTK_WINDOW(preferences->dialog));
|
|
return;
|
|
} else {
|
|
preferences = g_new0(GmPreferencesDialog, 1);
|
|
}
|
|
|
|
preferences->xml = glade_xml_new(GM_PREFERENCES_DIALOG_XML,
|
|
"gm_preferences_dialog", NULL);
|
|
preferences->dialog =
|
|
gm_preferences_dialog_widget("gm_preferences_dialog");
|
|
|
|
gm_preferences_dialog_load_colors();
|
|
gm_preferences_dialog_init_combo_box_scheme();
|
|
|
|
gtk_font_button_set_font_name(GTK_FONT_BUTTON(
|
|
gm_preferences_dialog_widget("font_button_font")),
|
|
gm_options_get(options, "font-family"));
|
|
|
|
alt_editor = gm_options_get(options, "editor_alternative");
|
|
|
|
if (strcmp(alt_editor, "0") == 0) {
|
|
alt_editor = NULL;
|
|
} else {
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_alt_editor")),
|
|
TRUE);
|
|
gtk_entry_set_text(GTK_ENTRY(
|
|
gm_preferences_dialog_widget("entry_alt_editor")),
|
|
alt_editor);
|
|
}
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_embed_editor")),
|
|
gm_options_get_int(options, "editor_embed"));
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_needs_terminal")),
|
|
gm_options_get_int(options, "editor_needs_terminal"));
|
|
|
|
gtk_widget_set_sensitive(
|
|
gm_preferences_dialog_widget("entry_alt_editor"),
|
|
alt_editor != NULL);
|
|
gtk_widget_set_sensitive(
|
|
gm_preferences_dialog_widget("check_button_embed_editor"),
|
|
alt_editor != NULL);
|
|
gtk_widget_set_sensitive(
|
|
gm_preferences_dialog_widget("check_button_needs_terminal"),
|
|
alt_editor != NULL
|
|
&& gm_options_get_int(options, "editor_embed") == 0);
|
|
|
|
glade_xml_signal_connect(preferences->xml,
|
|
"on_check_button_alt_editor_clicked", G_CALLBACK(
|
|
on_gm_preferences_dialog_check_button_alt_editor_clicked));
|
|
glade_xml_signal_connect(preferences->xml,
|
|
"on_check_button_embed_editor_clicked", G_CALLBACK(
|
|
on_gm_preferences_dialog_check_button_embed_editor_clicked));
|
|
|
|
glade_xml_signal_connect(preferences->xml,
|
|
"on_combo_box_scheme_changed", G_CALLBACK(
|
|
on_gm_preferences_dialog_combo_box_scheme_changed));
|
|
|
|
glade_xml_signal_connect(preferences->xml,
|
|
"on_color_button_fg_color_set", G_CALLBACK(
|
|
on_gm_preferences_dialog_color_button_fg_color_set));
|
|
glade_xml_signal_connect(preferences->xml,
|
|
"on_color_button_bg_color_set", G_CALLBACK(
|
|
on_gm_preferences_dialog_color_button_bg_color_set));
|
|
|
|
gm_preferences_dialog_run_dialog();
|
|
}
|
|
|
|
void
|
|
gm_preferences_dialog_load_colors() {
|
|
int i;
|
|
GtkColorButton *but;
|
|
GdkColor col;
|
|
GmColorTable *color_table = gm_app_color_table(gm_app_instance());
|
|
|
|
for (i = 0; i < (int)(sizeof(color_mapping) / sizeof(GmKeyValuePair)); i++) {
|
|
but = GTK_COLOR_BUTTON(gm_preferences_dialog_widget(
|
|
color_mapping[i].key));
|
|
|
|
gm_color_table_get(color_table, color_mapping[i].value, &col);
|
|
gtk_color_button_set_color(but, &col);
|
|
}
|
|
}
|
|
|
|
void
|
|
gm_preferences_dialog_save_colors() {
|
|
int i;
|
|
GtkColorButton *but;
|
|
GdkColor col;
|
|
GmColorTable *color_table = gm_app_color_table(gm_app_instance());
|
|
gchar *col_str;
|
|
|
|
for (i = 0; i < (int)(sizeof(color_mapping) / sizeof(GmKeyValuePair)); i++) {
|
|
but = GTK_COLOR_BUTTON(gm_preferences_dialog_widget(
|
|
color_mapping[i].key));
|
|
|
|
gtk_color_button_get_color(but, &col);
|
|
col_str = g_strdup_printf("#%04X%04X%04X", col.red, col.green,
|
|
col.blue);
|
|
|
|
gm_color_table_set(color_table, color_mapping[i].value, col_str);
|
|
g_free(col_str);
|
|
}
|
|
}
|
|
|
|
gboolean
|
|
gm_preferences_dialog_check_values() {
|
|
const gchar *alt_editor = gtk_entry_get_text(GTK_ENTRY(
|
|
gm_preferences_dialog_widget("entry_alt_editor")));
|
|
const gchar *font_description = gtk_font_button_get_font_name(
|
|
GTK_FONT_BUTTON(gm_preferences_dialog_widget("font_button_font")));
|
|
gboolean use_alt_editor = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_alt_editor")));
|
|
GmOptions *options = gm_app_options(gm_app_instance());
|
|
|
|
if (use_alt_editor && *alt_editor == '\0') {
|
|
gm_error_dialog(_("Editor can't be empty, enter a non empty command"),
|
|
GTK_WINDOW(preferences->dialog));
|
|
gm_notebook_focus_from_label(GTK_NOTEBOOK(gm_preferences_dialog_widget(
|
|
"notebook_preferences")), _("Editor"));
|
|
gtk_widget_grab_focus(gm_preferences_dialog_widget("entry_alt_editor"));
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
if (use_alt_editor) {
|
|
gm_options_set(options, "editor_alternative", alt_editor);
|
|
} else {
|
|
gm_options_set(options, "editor_alternative", "0");
|
|
}
|
|
|
|
gm_options_set_int(options, "editor_embed", gtk_toggle_button_get_active(
|
|
GTK_TOGGLE_BUTTON(gm_preferences_dialog_widget
|
|
("check_button_embed_editor"))));
|
|
gm_options_set_int(options, "editor_needs_terminal",
|
|
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_needs_terminal"))));
|
|
|
|
gm_options_set(options, "font-family", font_description);
|
|
gm_preferences_dialog_save_colors();
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void
|
|
gm_preferences_dialog_run_dialog() {
|
|
g_signal_connect(preferences->dialog, "response",
|
|
G_CALLBACK(on_gm_preferences_dialog_response), NULL);
|
|
|
|
gtk_widget_show(GTK_WIDGET(preferences->dialog));
|
|
}
|
|
|
|
/* CALLBACKS */
|
|
|
|
void
|
|
on_gm_preferences_dialog_response(GtkDialog *dialog, gint response,
|
|
gpointer user_data) {
|
|
gboolean is_okay = TRUE;
|
|
|
|
switch (response) {
|
|
case GTK_RESPONSE_OK: case GTK_RESPONSE_APPLY:
|
|
if (gm_preferences_dialog_check_values()) {
|
|
gm_options_save(gm_app_options(gm_app_instance()));
|
|
|
|
if (response == GTK_RESPONSE_APPLY) {
|
|
is_okay = FALSE;
|
|
}
|
|
} else {
|
|
is_okay = FALSE;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (is_okay) {
|
|
gtk_widget_destroy(GTK_WIDGET(dialog));
|
|
g_object_unref(preferences->xml);
|
|
g_free(preferences);
|
|
preferences = NULL;
|
|
}
|
|
}
|
|
|
|
void
|
|
on_gm_preferences_dialog_check_button_embed_editor_clicked(
|
|
GtkButton *button, gpointer user_data) {
|
|
gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
|
|
GmOptions *options = gm_app_options(gm_app_instance());
|
|
|
|
if (active) {
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_needs_terminal")),
|
|
TRUE);
|
|
} else {
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_needs_terminal")),
|
|
gm_options_get_int(options, "editor_needs_terminal"));
|
|
}
|
|
|
|
gtk_widget_set_sensitive(gm_preferences_dialog_widget(
|
|
"check_button_needs_terminal"), !active);
|
|
}
|
|
|
|
void
|
|
on_gm_preferences_dialog_check_button_alt_editor_clicked(
|
|
GtkButton *button, gpointer user_data) {
|
|
gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
|
|
|
|
if (!active) {
|
|
gtk_entry_set_text(GTK_ENTRY(gm_preferences_dialog_widget(
|
|
"entry_alt_editor")), "");
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_embed_editor")),
|
|
FALSE);
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
|
|
gm_preferences_dialog_widget("check_button_needs_terminal")),
|
|
FALSE);
|
|
}
|
|
|
|
gtk_widget_set_sensitive(gm_preferences_dialog_widget(
|
|
"entry_alt_editor"), active);
|
|
gtk_widget_set_sensitive(gm_preferences_dialog_widget(
|
|
"check_button_embed_editor"), active);
|
|
gtk_widget_set_sensitive(gm_preferences_dialog_widget(
|
|
"check_button_needs_terminal"), active);
|
|
}
|
|
|
|
void
|
|
on_gm_preferences_dialog_combo_box_scheme_changed(GtkComboBox *box,
|
|
gpointer user_data) {
|
|
GtkTreeIter iter;
|
|
gchar *scheme;
|
|
GtkTreeModel *model = gtk_combo_box_get_model(box);
|
|
GmColorTable *color_table = gm_app_color_table(gm_app_instance());
|
|
|
|
gtk_combo_box_get_active_iter(box, &iter);
|
|
gtk_tree_model_get(model, &iter, SCHEME_OPTION, &scheme, -1);
|
|
gm_color_table_set_from_scheme_name(color_table, scheme);
|
|
|
|
g_free(scheme);
|
|
}
|