[gnome-latex: 37/205] Preferences with a key file
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-latex: 37/205] Preferences with a key file
- Date: Fri, 14 Dec 2018 10:49:59 +0000 (UTC)
commit f965bdc274e71daa0689104fdc50148a128a4327
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date: Mon Aug 31 19:26:40 2009 +0200
Preferences with a key file
The preferences are saved to a key file (like .ini) in the home
directory (~/.latexila).
INSTALL | 44 ++++++++-----
README | 14 ++---
TODO | 8 +--
src/callbacks.c | 181 ++++++++++++++++++++++++++++++++++++++++++++---------
src/callbacks.h | 8 ++-
src/config.h.cmake | 2 -
src/main.c | 99 +++++++++++++++++++++++++----
src/main.h | 4 +-
src/ui.xml | 3 +-
9 files changed, 290 insertions(+), 73 deletions(-)
---
diff --git a/INSTALL b/INSTALL
index 98a8fbe..f683954 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,22 +1,36 @@
LaTeXila requires GTK+-2.16.x and GtkSourceView 2.6.x.
Maybe it will work with older versions of GTK and GtkSourceView but it's not
-supported.
+tested.
-Simple install procedure:
+Simple install procedure
+========================
- $ tar xvf latexila-0.0.1.tar.gz # unpack the sources
- $ cd latexila-0.0.1/build_directory/ # go to the build directory
- $ cmake ../ # run the Makefile generator
- $ make # build LaTeXila
- [ Become root if necessary ]
- $ make install # install LaTeXila
+$ tar xvf latexila-0.0.1.tar.gz # unpack the sources
+$ cd latexila-0.0.1/
+$ cmake . # run the Makefile generator
+$ make # build LaTeXila
+$ make translations # build the *.mo files
+[ Become root if necessary ]
+$ make install # install LaTeXila
-Simple uninstall procedure:
+Simple uninstall procedure
+==========================
- All the files installed are listed in the "install_manifest.txt" file
- located in the build directory. So you can uninstall LaTeXila by doing
- this:
+All the files installed are listed in the "install_manifest.txt" file.
+So you can uninstall LaTeXila by doing this:
+
+$ cd latexila-0.0.1/
+[ Become root if necessary ]
+$ xargs rm < install_manifest.txt
+
+Configuration
+=============
+
+* Disable Native Language Support (NLS)
+ run cmake with this option: -DENABLE_NLS=OFF
+ then you don't need to run "make translations"
+
+* Change the install directory
+ run cmake with this option:
+ -DCMAKE_INSTALL_PREFIX=/path/to/install/directory/
- $ cd latexila-0.0.1/build_directory/
- [ Become root if necessary ]
- $ xargs rm < install_manifest.txt
diff --git a/README b/README
index 85574d6..9281931 100644
--- a/README
+++ b/README
@@ -16,24 +16,24 @@ Installation
LaTeXila requires GTK+-2.16.x and GtkSourceView 2.6.x.
Maybe it will work with older versions of GTK and GtkSourceView but it's not
-supported.
+tested.
Simple install procedure:
$ tar xvf latexila-0.0.1.tar.gz # unpack the sources
- $ cd latexila-0.0.1/build_directory/ # go to the build directory
- $ cmake ../ # run the Makefile generator
+ $ cd latexila-0.0.1/
+ $ cmake . # run the Makefile generator
$ make # build LaTeXila
+ $ make translations # build the *.mo files
[ Become root if necessary ]
$ make install # install LaTeXila
Simple uninstall procedure:
- All the files installed are listed in the "install_manifest.txt" file
- located in the build directory. So you can uninstall LaTeXila by doing
- this:
+ All the files installed are listed in the "install_manifest.txt" file.
+ So you can uninstall LaTeXila by doing this:
- $ cd latexila-0.0.1/build_directory/
+ $ cd latexila-0.0.1/
[ Become root if necessary ]
$ xargs rm < install_manifest.txt
diff --git a/TODO b/TODO
index 0a99582..fe23f67 100644
--- a/TODO
+++ b/TODO
@@ -12,10 +12,10 @@ Mon Aug 24, 2009 to Mon Aug 31, 2009
[x] enlarge/shrink font
-[-] Preferences
- - show line numbers
- - commands
- - source code font
+[x] Preferences
+ x show line numbers
+ x commands
+ x source code font
[-] bugs correction
- sometimes command output is not UTF-8:
diff --git a/src/callbacks.c b/src/callbacks.c
index eb1d1fc..2de91f9 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -54,6 +54,10 @@ static void scroll_to_cursor (void);
static gboolean find_next_match (const gchar *what, GtkSourceSearchFlags flags,
gboolean backward, GtkTextIter *match_start, GtkTextIter *match_end);
static void change_font_source_view (void);
+static void create_preferences (void);
+
+static GtkWidget *pref_dialog = NULL;
+static gboolean pref_changed = FALSE;
void
cb_new (void)
@@ -276,24 +280,37 @@ cb_select_all (void)
gtk_text_buffer_select_range (buffer, &start, &end);
}
+void
+cb_preferences (void)
+{
+ if (pref_dialog == NULL)
+ create_preferences ();
+ gtk_window_present (GTK_WINDOW (pref_dialog));
+}
+
void
cb_zoom_in (void)
{
- latexila.font_size++;
+ latexila.font_size += PANGO_SCALE;
+ pango_font_description_set_size (latexila.font_desc, latexila.font_size);
change_font_source_view ();
}
void
cb_zoom_out (void)
{
- latexila.font_size--;
+ latexila.font_size -= PANGO_SCALE;
+ pango_font_description_set_size (latexila.font_desc, latexila.font_size);
change_font_source_view ();
}
void
cb_zoom_reset (void)
{
- latexila.font_size = 10;
+ gchar *font_string = g_key_file_get_string (latexila.key_file,
+ PROGRAM_NAME, "font", NULL);
+ latexila.font_desc = pango_font_description_from_string (font_string);
+ latexila.font_size = pango_font_description_get_size (latexila.font_desc);
change_font_source_view ();
}
@@ -522,21 +539,6 @@ cb_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data)
return TRUE;
}
-void
-cb_line_numbers (GtkToggleAction *action, gpointer user_data)
-{
- latexila.show_line_numbers = gtk_toggle_action_get_active (action);
-
- //TODO optimisation?
- guint nb_docs = g_list_length (latexila.all_docs);
- for (guint i = 0 ; i < nb_docs ; i++)
- {
- document_t *doc = g_list_nth_data (latexila.all_docs, i);
- gtk_source_view_set_show_line_numbers (
- GTK_SOURCE_VIEW (doc->source_view), latexila.show_line_numbers);
- }
-}
-
void
cb_latex (void)
{
@@ -715,6 +717,56 @@ cb_recent_item_activated (GtkRecentAction *action, gpointer user_data)
open_new_document (filename, uri);
}
+void
+cb_pref_dialog_close (GtkDialog *dialog, gint response_id, gpointer user_data)
+{
+ gtk_widget_hide (GTK_WIDGET (dialog));
+
+ if (pref_changed)
+ save_preferences ();
+}
+
+void
+cb_line_numbers (GtkToggleButton *toggle_button, gpointer user_data)
+{
+ gboolean show_line_numbers = gtk_toggle_button_get_active (toggle_button);
+ g_key_file_set_boolean (latexila.key_file, PROGRAM_NAME,
+ "show_line_numbers", show_line_numbers);
+
+ //TODO optimisation?
+ guint nb_docs = g_list_length (latexila.all_docs);
+ for (guint i = 0 ; i < nb_docs ; i++)
+ {
+ document_t *doc = g_list_nth_data (latexila.all_docs, i);
+ gtk_source_view_set_show_line_numbers (
+ GTK_SOURCE_VIEW (doc->source_view), show_line_numbers);
+ }
+
+ pref_changed = TRUE;
+}
+
+void
+cb_command_view (GtkButton *button, gpointer user_data)
+{
+ GtkEntry *entry = (GtkEntry *) user_data;
+ const gchar *new_command_view = gtk_entry_get_text (entry);
+ g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
+ "command_view", new_command_view);
+ pref_changed = TRUE;
+}
+
+void
+cb_font_set (GtkFontButton *font_button, gpointer user_data)
+{
+ const gchar *font_string = gtk_font_button_get_font_name (font_button);
+ g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
+ "font", font_string);
+ latexila.font_desc = pango_font_description_from_string (font_string);
+ latexila.font_size = pango_font_description_get_size (latexila.font_desc);
+ change_font_source_view ();
+ pref_changed = TRUE;
+}
+
void
open_new_document (const gchar *filename, const gchar *uri)
{
@@ -739,6 +791,24 @@ open_new_document (const gchar *filename, const gchar *uri)
print_warning ("impossible to open the file \"%s\"", filename);
}
+void
+save_preferences (void)
+{
+ gchar *key_file_data = g_key_file_to_data (latexila.key_file, NULL, NULL);
+ FILE* file = fopen (latexila.pref_file, "w");
+ if (file != NULL)
+ {
+ fprintf (file, "%s", key_file_data);
+ fclose (file);
+ }
+ else
+ {
+ print_warning ("impossible to save user preferences to \"%s\"",
+ latexila.pref_file);
+ }
+ g_free (key_file_data);
+}
+
/******************************************************************************
* local functions
*****************************************************************************/
@@ -783,18 +853,17 @@ create_document_in_new_tab (const gchar *path, const gchar *text, const gchar *t
gtk_source_view_set_auto_indent (GTK_SOURCE_VIEW (new_doc->source_view), TRUE);
// set the font
- gchar *font_string = g_strdup_printf ("%s %d", FONT, latexila.font_size);
- PangoFontDescription *font_desc;
- font_desc = pango_font_description_from_string (font_string);
- gtk_widget_modify_font (new_doc->source_view, font_desc);
+ gtk_widget_modify_font (new_doc->source_view, latexila.font_desc);
// enable text wrapping (between words only)
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (new_doc->source_view),
GTK_WRAP_WORD);
// show line numbers?
+ gboolean show_line_numbers = g_key_file_get_boolean (latexila.key_file,
+ PROGRAM_NAME, "show_line_numbers", NULL);
gtk_source_view_set_show_line_numbers (
- GTK_SOURCE_VIEW (new_doc->source_view), latexila.show_line_numbers);
+ GTK_SOURCE_VIEW (new_doc->source_view), show_line_numbers);
// put the text into the buffer
gtk_source_buffer_begin_not_undoable_action (new_doc->source_buffer);
@@ -1097,7 +1166,9 @@ view_document (gchar *title, gchar *doc_extension)
gchar *doc_path = g_regex_replace_literal (regex, latexila.active_doc->path,
-1, 0, doc_extension, 0, NULL);
- command = g_strdup_printf ("evince %s", doc_path);
+ gchar *command_view = g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
+ "command_view", NULL);
+ command = g_strdup_printf ("%s %s", command_view, doc_path);
/* the current document is a *.tex file? */
gboolean tex_file = g_regex_match (regex, latexila.active_doc->path, 0, NULL);
@@ -1391,10 +1462,64 @@ change_font_source_view (void)
for (guint i = 0 ; i < nb_docs ; i++)
{
document_t *doc = g_list_nth_data (latexila.all_docs, i);
- gchar *font_string = g_strdup_printf ("%s %d", FONT, latexila.font_size);
- PangoFontDescription *font_desc;
- font_desc = pango_font_description_from_string (font_string);
- gtk_widget_modify_font (doc->source_view, font_desc);
+ gtk_widget_modify_font (doc->source_view, latexila.font_desc);
}
}
+static void
+create_preferences (void)
+{
+ pref_dialog = gtk_dialog_new_with_buttons (_("Preferences"),
+ latexila.main_window, 0,
+ GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
+ gtk_dialog_set_has_separator (GTK_DIALOG (pref_dialog), FALSE);
+
+ g_signal_connect (G_OBJECT (pref_dialog), "response",
+ G_CALLBACK (cb_pref_dialog_close), NULL);
+ g_signal_connect (G_OBJECT (pref_dialog), "destroy",
+ G_CALLBACK (gtk_widget_destroyed), &pref_dialog);
+
+ GtkWidget *content_area = gtk_dialog_get_content_area (
+ GTK_DIALOG (pref_dialog));
+ gtk_box_set_spacing (GTK_BOX (content_area), 3);
+
+ /* show line numbers */
+ GtkWidget *line_numbers = gtk_check_button_new_with_label (
+ _("Display line numbers"));
+ gboolean tmp = g_key_file_get_boolean (latexila.key_file, PROGRAM_NAME,
+ "show_line_numbers", NULL);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (line_numbers), tmp);
+ g_signal_connect (G_OBJECT (line_numbers), "toggled",
+ G_CALLBACK (cb_line_numbers), NULL);
+ gtk_box_pack_start (GTK_BOX (content_area), line_numbers, FALSE, FALSE, 0);
+
+ /* font */
+ GtkWidget *hbox = gtk_hbox_new (FALSE, 5);
+ GtkWidget *label = gtk_label_new (_("Font:"));
+ gchar *current_font = g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
+ "font", NULL);
+ GtkWidget *font_button = gtk_font_button_new_with_font (current_font);
+ g_signal_connect (G_OBJECT (font_button), "font-set",
+ G_CALLBACK (cb_font_set), NULL);
+ gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (hbox), font_button, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, FALSE, 0);
+
+ /* command view entry */
+ hbox = gtk_hbox_new (FALSE, 5);
+ label = gtk_label_new (_("Program for viewing documents:"));
+ GtkWidget *command_view_entry = gtk_entry_new ();
+ gchar *txt = g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
+ "command_view", NULL);
+ gtk_entry_set_text (GTK_ENTRY (command_view_entry), txt);
+ GtkWidget *button = gtk_button_new_with_label (_("OK"));
+ g_signal_connect (G_OBJECT (button), "clicked",
+ G_CALLBACK (cb_command_view), GTK_ENTRY (command_view_entry));
+ gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (hbox), command_view_entry, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, FALSE, 0);
+
+
+ gtk_widget_show_all (content_area);
+}
diff --git a/src/callbacks.h b/src/callbacks.h
index a11470e..90b90f9 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -34,6 +34,7 @@ void cb_copy (void);
void cb_paste (void);
void cb_delete (void);
void cb_select_all (void);
+void cb_preferences (void);
void cb_zoom_in (void);
void cb_zoom_out (void);
void cb_zoom_reset (void);
@@ -56,9 +57,14 @@ void cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page,
guint page_num, gpointer user_data);
gboolean cb_delete_event (GtkWidget *widget, GdkEvent *event,
gpointer user_data);
-void cb_line_numbers (GtkToggleAction *action, gpointer user_data);
void cb_recent_item_activated (GtkRecentAction *action, gpointer user_data);
+void cb_pref_dialog_close (GtkDialog *dialog, gint response_id,
+ gpointer user_data);
+void cb_line_numbers (GtkToggleButton *toggle_button, gpointer user_data);
+void cb_command_view (GtkButton *button, gpointer user_data);
+void cb_font_set (GtkFontButton *font_button, gpointer user_data);
void open_new_document (const gchar *filename, const gchar *uri);
+void save_preferences (void);
#endif /* CALLBACKS_H */
diff --git a/src/config.h.cmake b/src/config.h.cmake
index 29f1cfc..57acda6 100644
--- a/src/config.h.cmake
+++ b/src/config.h.cmake
@@ -29,6 +29,4 @@
#define DATA_DIR "${DATA_DIR}"
-#define FONT "Monospace"
-
#endif /* CONFIG_H */
diff --git a/src/main.c b/src/main.c
index f831912..c71265a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -32,7 +32,7 @@
#include "print.h"
latexila_t latexila = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL};
+ NULL, NULL, NULL, NULL, NULL, NULL};
static struct {
gchar *filename;
@@ -149,6 +149,8 @@ main (int argc, char *argv[])
_("Delete the selected text"), G_CALLBACK (cb_delete)},
{"EditSelectAll", GTK_STOCK_SELECT_ALL, _("Select All"), "<Control>A",
_("Select the entire document"), G_CALLBACK (cb_select_all)},
+ {"EditPreferences", GTK_STOCK_PREFERENCES, _("Preferences"), NULL,
+ _("Configure the application"), G_CALLBACK (cb_preferences)},
{"View", NULL, _("View"), NULL, NULL, NULL},
{"ViewZoomIn", GTK_STOCK_ZOOM_IN, _("Zoom In"), "<Control>plus",
@@ -185,15 +187,7 @@ main (int argc, char *argv[])
_("About LaTeXila"), G_CALLBACK (cb_about_dialog)}
};
- // name, stock_id, label, accelerator, tooltip, callback
- GtkToggleActionEntry toggle_entries[] =
- {
- {"ViewLineNumbers", NULL, _("Line numbers"), NULL,
- _("Display line numbers"), G_CALLBACK (cb_line_numbers)}
- };
-
guint nb_entries = G_N_ELEMENTS (entries);
- guint nb_toggle_entries = G_N_ELEMENTS (toggle_entries);
// recent document
GtkAction *recent = gtk_recent_action_new ("FileOpenRecent",
@@ -208,7 +202,6 @@ main (int argc, char *argv[])
// create the action group and the ui manager
GtkActionGroup *action_group = gtk_action_group_new ("menuActionGroup");
gtk_action_group_add_actions (action_group, entries, nb_entries, NULL);
- gtk_action_group_add_toggle_actions (action_group, toggle_entries, nb_toggle_entries, NULL);
gtk_action_group_add_action (action_group, recent);
GtkUIManager *ui_manager = gtk_ui_manager_new ();
gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
@@ -252,9 +245,6 @@ main (int argc, char *argv[])
gtk_paned_pack1 (GTK_PANED (vpaned), notebook, TRUE, TRUE);
latexila.notebook = GTK_NOTEBOOK (notebook);
- latexila.font_size = 10;
- latexila.show_line_numbers = FALSE;
-
/* log zone */
//TODO set default height and width
GtkWidget *hpaned = gtk_hpaned_new ();
@@ -327,9 +317,92 @@ main (int argc, char *argv[])
gtk_box_pack_end (GTK_BOX (statusbar), cursor_position_statusbar,
FALSE, TRUE, 0);
+ /* user preferences */
+ gboolean default_value_show_line_numbers = FALSE;
+ gchar *default_value_font = "Monospace 10";
+ gchar *default_value_command_view = "evince";
+
+ gchar *pref_file = g_strdup_printf ("%s/.latexila", g_get_home_dir ());
+ latexila.pref_file = pref_file;
+ latexila.key_file = g_key_file_new ();
+ gboolean prefs_loaded = FALSE;
+ error = NULL;
+
+ if (g_file_test (pref_file, G_FILE_TEST_IS_REGULAR))
+ {
+ g_key_file_load_from_file (latexila.key_file, pref_file,
+ G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
+ if (error != NULL)
+ {
+ print_warning ("load user preferences failed: %s", error->message);
+ g_error_free (error);
+ }
+ else
+ {
+ /* check if all keys exist
+ * if not, set the default value for that key
+ */
+ gboolean prefs_saved = TRUE;
+ g_key_file_get_boolean (latexila.key_file, PROGRAM_NAME,
+ "show_line_numbers", &error);
+ if (error != NULL)
+ {
+ g_key_file_set_boolean (latexila.key_file, PROGRAM_NAME,
+ "show_line_numbers", default_value_show_line_numbers);
+ g_error_free (error);
+ prefs_saved = FALSE;
+ }
+
+ g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
+ "font", &error);
+ if (error != NULL)
+ {
+ g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
+ "font", default_value_font);
+ g_error_free (error);
+ prefs_saved = FALSE;
+ }
+
+ g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
+ "command_view", &error);
+ if (error != NULL)
+ {
+ g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
+ "command_view", default_value_command_view);
+ g_error_free (error);
+ prefs_saved = FALSE;
+ }
+
+ if (! prefs_saved)
+ save_preferences ();
+
+ print_info ("load user preferences: OK");
+ prefs_loaded = TRUE;
+ }
+ }
+
+ if (! prefs_loaded)
+ {
+ // set default values
+ g_key_file_set_boolean (latexila.key_file, PROGRAM_NAME,
+ "show_line_numbers", default_value_show_line_numbers);
+ g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
+ "font", default_value_font);
+ g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
+ "command_view", default_value_command_view);
+
+ save_preferences ();
+ }
+
+ gchar *font_string = g_key_file_get_string (latexila.key_file,
+ PROGRAM_NAME, "font", NULL);
+ latexila.font_desc = pango_font_description_from_string (font_string);
+ latexila.font_size = pango_font_description_get_size (latexila.font_desc);
+
gtk_widget_show_all (window);
+ /* open documents given in arguments */
for (int i = 1 ; i < argc ; i++)
{
gchar *uri = g_filename_to_uri (argv[i], NULL, NULL);
diff --git a/src/main.h b/src/main.h
index 648a33f..d3d6c9c 100644
--- a/src/main.h
+++ b/src/main.h
@@ -52,8 +52,10 @@ typedef struct
GtkStatusbar *cursor_position;
GtkAction *undo;
GtkAction *redo;
+ GKeyFile *key_file;
+ gchar *pref_file;
+ PangoFontDescription *font_desc;
gint font_size;
- gboolean show_line_numbers;
} latexila_t;
// all the documents are accessible by the docs variable
diff --git a/src/ui.xml b/src/ui.xml
index 34395d1..0ccb566 100644
--- a/src/ui.xml
+++ b/src/ui.xml
@@ -46,11 +46,10 @@ In the code, GtkUIManager is used to construct them.
<menuitem action="EditDelete" />
<separator />
<menuitem action="EditSelectAll" />
+ <menuitem action="EditPreferences" />
</menu>
<menu action="View">
- <menuitem action="ViewLineNumbers" />
- <separator />
<menuitem action="ViewZoomIn" />
<menuitem action="ViewZoomOut" />
<menuitem action="ViewZoomReset" />
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]