Allow selecting file as external editor

This commit is contained in:
Lawrence Lee
2022-03-05 18:26:52 -08:00
parent 732316dcaf
commit 58f0783561
3 changed files with 92 additions and 0 deletions

View File

@@ -18,6 +18,11 @@
*/
#include <iostream>
#include <giomm/contenttype.h>
#include <glibmm/shell.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/stock.h>
#include "externaleditorpreferences.h"
#include "multilangmgr.h"
#include "rtimage.h"
@@ -54,11 +59,14 @@ ExternalEditorPreferences::ExternalEditorPreferences():
button_add->set_image(*add_image);
button_remove->set_image(*remove_image);
button_app_chooser = Gtk::make_managed<Gtk::Button>(M("PREFERENCES_EXTERNALEDITOR_CHANGE"));
button_file_chooser = Gtk::manage(new Gtk::Button(M("PREFERENCES_EXTERNALEDITOR_CHANGE_FILE")));
button_app_chooser->signal_pressed().connect(sigc::mem_fun(
*this, &ExternalEditorPreferences::openAppChooserDialog));
button_add->signal_pressed().connect(sigc::mem_fun(
*this, &ExternalEditorPreferences::addEditor));
button_file_chooser->signal_pressed().connect(sigc::mem_fun(
*this, &ExternalEditorPreferences::openFileChooserDialog));
button_remove->signal_pressed().connect(sigc::mem_fun(
*this, &ExternalEditorPreferences::removeSelectedEditors));
@@ -69,6 +77,7 @@ ExternalEditorPreferences::ExternalEditorPreferences():
// Toolbar.
toolbar.set_halign(Gtk::Align::ALIGN_END);
toolbar.add(*button_app_chooser);
toolbar.add(*button_file_chooser);
toolbar.add(*button_add);
toolbar.add(*button_remove);
@@ -204,6 +213,38 @@ void ExternalEditorPreferences::onAppChooserDialogResponse(
}
}
void ExternalEditorPreferences::onFileChooserDialogResponse(
int response_id, Gtk::FileChooserDialog *dialog)
{
switch (response_id) {
case Gtk::RESPONSE_OK: {
dialog->close();
auto selection = list_view->get_selection()->get_selected_rows();
for (const auto &selected : selection) {
auto row = *list_model->get_iter(selected);
row[model_columns.icon] = Glib::RefPtr<Gio::Icon>(nullptr);
row[model_columns.command] =
#ifdef WIN32
'"' + dialog->get_filename() + '"';
#else
Glib::shell_quote(dialog->get_filename());
#endif
}
break;
}
case Gtk::RESPONSE_CANCEL:
case Gtk::RESPONSE_CLOSE:
dialog->close();
break;
default:
break;
}
}
void ExternalEditorPreferences::openAppChooserDialog()
{
if (app_chooser_dialog.get()) {
@@ -221,6 +262,35 @@ void ExternalEditorPreferences::openAppChooserDialog()
app_chooser_dialog->show();
}
void ExternalEditorPreferences::openFileChooserDialog()
{
if (file_chooser_dialog.get()) {
file_chooser_dialog->show();
return;
}
file_chooser_dialog.reset(new Gtk::FileChooserDialog(M("PREFERENCES_EXTERNALEDITOR_CHANGE_FILE")));
const auto exe_filter = Gtk::FileFilter::create();
exe_filter->set_name(M("FILECHOOSER_FILTER_EXECUTABLE"));
exe_filter->add_custom(Gtk::FILE_FILTER_MIME_TYPE, [](const Gtk::FileFilter::Info &info) {
return Gio::content_type_can_be_executable(info.mime_type);
});
const auto all_filter = Gtk::FileFilter::create();
all_filter->set_name(M("FILECHOOSER_FILTER_ANY"));
all_filter->add_pattern("*");
file_chooser_dialog->add_filter(exe_filter);
file_chooser_dialog->add_filter(all_filter);
file_chooser_dialog->signal_response().connect(sigc::bind(
sigc::mem_fun(*this, &ExternalEditorPreferences::onFileChooserDialogResponse),
file_chooser_dialog.get()));
file_chooser_dialog->set_modal();
file_chooser_dialog->add_button(M("GENERAL_CANCEL"), Gtk::RESPONSE_CANCEL);
file_chooser_dialog->add_button(M("GENERAL_OPEN"), Gtk::RESPONSE_OK);
file_chooser_dialog->show();
}
void ExternalEditorPreferences::removeSelectedEditors()
{
auto selection = list_view->get_selection()->get_selected_rows();
@@ -265,6 +335,7 @@ void ExternalEditorPreferences::updateToolbarSensitivity()
{
bool selected = list_view->get_selection()->count_selected_rows();
button_app_chooser->set_sensitive(selected);
button_file_chooser->set_sensitive(selected);
button_remove->set_sensitive(selected);
}

View File

@@ -28,6 +28,14 @@
#include "rtappchooserdialog.h"
namespace Gtk
{
class FileChooserDialog;
}
/**
* Widget for editing the external editors options.
*/
@@ -98,8 +106,10 @@ private:
Gtk::Box toolbar; // Contains buttons for editing the list.
Gtk::Button *button_app_chooser;
Gtk::Button *button_add;
Gtk::Button *button_file_chooser;
Gtk::Button *button_remove;
std::unique_ptr<RTAppChooserDialog> app_chooser_dialog;
std::unique_ptr<Gtk::FileChooserDialog> file_chooser_dialog;
/**
* Inserts a new editor entry after the current selection, or at the end if
@@ -119,10 +129,19 @@ private:
* Closes the dialog and updates the selected entry if an app was chosen.
*/
void onAppChooserDialogResponse(int responseId, RTAppChooserDialog *dialog);
/**
* Called when the user is done interacting with the file chooser dialog.
* Closes the dialog and updates the selected entry if a file was chosen.
*/
void onFileChooserDialogResponse(int responseId, Gtk::FileChooserDialog *dialog);
/**
* Shows the app chooser dialog.
*/
void openAppChooserDialog();
/**
* Shows the file chooser dialog for picking an executable.
*/
void openFileChooserDialog();
/**
* Removes all selected editors.
*/