Skip to content

Commit

Permalink
Add capability to automatically sort editor tabs (v2)
Browse files Browse the repository at this point in the history
This doesn't modify every part of Geany that calls document_open_file().
Instead, it makes use of the "document-open" signal, and g_add_idle().

We don't connect to "document-new" since it would make "Next to current"
meaningless.
  • Loading branch information
konsolebox committed Jul 23, 2016
1 parent 5bd8b52 commit 474c0a5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
30 changes: 30 additions & 0 deletions data/geany.glade
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,36 @@
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="check_auto_sort_tabs_filename">
<property name="label" translatable="yes">Automatically sort tabs based on filename</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="check_auto_sort_tabs_pathname">
<property name="label" translatable="yes">Automatically sort tabs based on pathname</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">6</property>
</packing>
</child>
</object>
</child>
</object>
Expand Down
6 changes: 6 additions & 0 deletions src/keyfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ static void save_dialog_prefs(GKeyFile *config)
g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
g_key_file_set_boolean(config, PACKAGE, "auto_sort_tabs_filename", interface_prefs.auto_sort_tabs_filename);
g_key_file_set_boolean(config, PACKAGE, "auto_sort_tabs_pathname", interface_prefs.auto_sort_tabs_pathname);
g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
Expand Down Expand Up @@ -777,6 +779,10 @@ static void load_dialog_prefs(GKeyFile *config)
file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
interface_prefs.auto_sort_tabs_filename = utils_get_setting_boolean(config, PACKAGE, "auto_sort_tabs_filename", FALSE);
interface_prefs.auto_sort_tabs_pathname = utils_get_setting_boolean(config, PACKAGE, "auto_sort_tabs_pathname", FALSE);
if (interface_prefs.auto_sort_tabs_filename && interface_prefs.auto_sort_tabs_pathname)
interface_prefs.auto_sort_tabs_filename = FALSE;
file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
Expand Down
32 changes: 32 additions & 0 deletions src/notebook.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ static gboolean switch_in_progress = FALSE;
static GtkWidget *switch_dialog = NULL;
static GtkWidget *switch_dialog_label = NULL;

static gboolean on_idle_auto_sort_hooked = FALSE;


static void
notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
Expand All @@ -81,6 +83,8 @@ notebook_tab_close_clicked_cb(GtkButton *button, gpointer user_data);

static void setup_tab_dnd(void);

static gboolean on_document_open(GeanyDocument* doc);


static void update_mru_docs_head(GeanyDocument *doc)
{
Expand Down Expand Up @@ -546,6 +550,8 @@ void notebook_init(void)
/* in case the switch dialog misses an event while drawing the dialog */
g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);

g_signal_connect(geany_object, "document-open", G_CALLBACK(on_document_open), NULL);

setup_tab_dnd();
}

Expand Down Expand Up @@ -963,6 +969,32 @@ void on_sort_tabs_pathname_activate(GtkMenuItem *menuitem, gpointer user_data)
}


static gboolean on_idle_auto_sort_tabs(gpointer user_data)
{
if (interface_prefs.show_notebook_tabs)
{
if (interface_prefs.auto_sort_tabs_filename)
notebook_sort_tabs(NOTEBOOK_TAB_SORT_FILENAME);
else if (interface_prefs.auto_sort_tabs_pathname)
notebook_sort_tabs(NOTEBOOK_TAB_SORT_PATHNAME);
}

on_idle_auto_sort_hooked = FALSE;
return FALSE;
}


static gboolean on_document_open(GeanyDocument* doc)
{
if (!on_idle_auto_sort_hooked && interface_prefs.show_notebook_tabs &&
(interface_prefs.auto_sort_tabs_filename || interface_prefs.auto_sort_tabs_pathname))
{
if (g_idle_add(on_idle_auto_sort_tabs, NULL) > 0)
on_idle_auto_sort_hooked = TRUE;
}
}


static void
on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
gint x, gint y, GtkSelectionData *data, guint target_type,
Expand Down
40 changes: 40 additions & 0 deletions src/prefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ static gboolean kb_find_duplicate(GtkTreeStore *store, GtkWidget *parent, GtkTre
guint key, GdkModifierType mods, const gchar *shortcut);
static void on_toolbar_show_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_show_notebook_tabs_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_auto_sort_tabs_filename_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_auto_sort_tabs_pathname_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_enable_plugins_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_use_folding_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_open_encoding_toggled(GtkToggleButton *togglebutton, gpointer user_data);
Expand Down Expand Up @@ -489,6 +491,16 @@ static void prefs_init_dialog(void)
on_show_notebook_tabs_toggled(GTK_TOGGLE_BUTTON(
ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_notebook_tabs")), NULL);

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), interface_prefs.auto_sort_tabs_filename);
on_auto_sort_tabs_filename_toggled(GTK_TOGGLE_BUTTON(
ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename")), NULL);

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), interface_prefs.auto_sort_tabs_pathname);
on_auto_sort_tabs_pathname_toggled(GTK_TOGGLE_BUTTON(
ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname")), NULL);

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_tab_cross");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), file_prefs.show_tab_cross);

Expand Down Expand Up @@ -956,6 +968,12 @@ on_prefs_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_notebook_tabs");
interface_prefs.show_notebook_tabs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename");
interface_prefs.auto_sort_tabs_filename = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname");
interface_prefs.auto_sort_tabs_pathname = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_tab_cross");
file_prefs.show_tab_cross = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

Expand Down Expand Up @@ -1531,6 +1549,24 @@ static void on_show_notebook_tabs_toggled(GtkToggleButton *togglebutton, gpointe
/* tab placement only enabled when tabs are visible */
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "combo_tab_editor"), sens);
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_tab_cross"), sens);
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename"), sens);
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname"), sens);
}


static void on_auto_sort_tabs_filename_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
if (gtk_toggle_button_get_active(togglebutton))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui_lookup_widget(ui_widgets.prefs_dialog,
"check_auto_sort_tabs_pathname")), FALSE);
}


static void on_auto_sort_tabs_pathname_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
if (gtk_toggle_button_get_active(togglebutton))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui_lookup_widget(ui_widgets.prefs_dialog,
"check_auto_sort_tabs_filename")), FALSE);
}


Expand Down Expand Up @@ -1813,6 +1849,10 @@ void prefs_show_dialog(void)
"toggled", G_CALLBACK(on_toolbar_show_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_notebook_tabs"),
"toggled", G_CALLBACK(on_show_notebook_tabs_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename"),
"toggled", G_CALLBACK(on_auto_sort_tabs_filename_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname"),
"toggled", G_CALLBACK(on_auto_sort_tabs_pathname_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_folding"),
"toggled", G_CALLBACK(on_use_folding_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_open_encoding"),
Expand Down
2 changes: 2 additions & 0 deletions src/ui_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ typedef struct GeanyInterfacePrefs
gchar *tagbar_font; /**< symbol sidebar font */
gchar *msgwin_font; /**< message window font */
gboolean show_notebook_tabs; /**< whether editor tabs are visible */
gboolean auto_sort_tabs_filename; /**< whether to automatically sort tabs based on filename */
gboolean auto_sort_tabs_pathname; /**< whether to automatically sort tabs based on pathname */
gint tab_pos_editor; /**< positions of editor's tabs */
gint tab_pos_msgwin; /**< positions of message window's tabs */
gint tab_pos_sidebar; /**< positions of sidebar's tabs */
Expand Down

0 comments on commit 474c0a5

Please sign in to comment.