You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovdCFilenameSettingView.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "ovdCFilenameSettingView.h"
  2. #include "../ovd_base.h"
  3. #include <cstring>
  4. #include <iterator>
  5. namespace OpenViBE {
  6. namespace Designer {
  7. namespace Setting {
  8. static void OnButtonSettingFilenameBrowsePressed(GtkButton* /*button*/, gpointer data) { static_cast<CFilenameSettingView*>(data)->browse(); }
  9. static void OnChange(GtkEntry* /*entry*/, gpointer data) { static_cast<CFilenameSettingView*>(data)->onChange(); }
  10. #if defined TARGET_OS_Windows
  11. static gboolean OnFocusOutEvent(GtkEntry* /*entry*/, GdkEvent* /*event*/, gpointer data)
  12. {
  13. static_cast<CFilenameSettingView*>(data)->onFocusLost();
  14. return FALSE;
  15. }
  16. #endif
  17. CFilenameSettingView::CFilenameSettingView(Kernel::IBox& box, const size_t index, CString& builderName, const Kernel::IKernelContext& ctx)
  18. : CAbstractSettingView(box, index, builderName, "settings_collection-hbox_setting_filename"), m_kernelCtx(ctx)
  19. {
  20. GtkWidget* settingWidget = CAbstractSettingView::getEntryFieldWidget();
  21. std::vector<GtkWidget*> widgets;
  22. CAbstractSettingView::extractWidget(settingWidget, widgets);
  23. m_entry = GTK_ENTRY(widgets[0]);
  24. g_signal_connect(G_OBJECT(m_entry), "changed", G_CALLBACK(OnChange), this);
  25. #if defined TARGET_OS_Windows
  26. // Only called for Windows path
  27. g_signal_connect(G_OBJECT(m_entry), "focus_out_event", G_CALLBACK(OnFocusOutEvent), this);
  28. #endif
  29. g_signal_connect(G_OBJECT(widgets[1]), "clicked", G_CALLBACK(OnButtonSettingFilenameBrowsePressed), this);
  30. CAbstractSettingView::initializeValue();
  31. }
  32. void CFilenameSettingView::getValue(CString& value) const { value = CString(gtk_entry_get_text(m_entry)); }
  33. void CFilenameSettingView::setValue(const CString& value)
  34. {
  35. m_onValueSetting = true;
  36. gtk_entry_set_text(m_entry, value);
  37. m_onValueSetting = false;
  38. }
  39. void CFilenameSettingView::browse() const
  40. {
  41. GtkWidget* widgetDialogOpen = gtk_file_chooser_dialog_new("Select file to open...", nullptr, GTK_FILE_CHOOSER_ACTION_SAVE,
  42. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, nullptr);
  43. const CString initialFilename = m_kernelCtx.getConfigurationManager().expand(gtk_entry_get_text(m_entry));
  44. if (g_path_is_absolute(initialFilename.toASCIIString()))
  45. {
  46. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), initialFilename.toASCIIString());
  47. }
  48. else
  49. {
  50. char* fullPath = g_build_filename(g_get_current_dir(), initialFilename.toASCIIString(), nullptr);
  51. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), fullPath);
  52. g_free(fullPath);
  53. }
  54. gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(widgetDialogOpen), false);
  55. if (gtk_dialog_run(GTK_DIALOG(widgetDialogOpen)) == GTK_RESPONSE_ACCEPT)
  56. {
  57. char* fileName = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widgetDialogOpen));
  58. char* backslash;
  59. while ((backslash = strchr(fileName, '\\')) != nullptr) { *backslash = '/'; }
  60. gtk_entry_set_text(m_entry, fileName);
  61. g_free(fileName);
  62. }
  63. gtk_widget_destroy(widgetDialogOpen);
  64. }
  65. void CFilenameSettingView::onChange()
  66. {
  67. if (!m_onValueSetting)
  68. {
  69. const gchar* value = gtk_entry_get_text(m_entry);
  70. getBox().setSettingValue(getSettingIndex(), value);
  71. }
  72. }
  73. #if defined TARGET_OS_Windows
  74. void CFilenameSettingView::onFocusLost()
  75. {
  76. // We replace antislash, interpreted as escape, by slash in Windows path
  77. if (!m_onValueSetting)
  78. {
  79. std::string fileName = gtk_entry_get_text(m_entry);
  80. auto it = fileName.begin();
  81. while ((it = std::find(it, fileName.end(), '\\')) != fileName.end())
  82. {
  83. if (it == std::prev(fileName.end()))
  84. {
  85. *it = '/';
  86. break;
  87. }
  88. if (*std::next(it) != '{' && *std::next(it) != '$' && *std::next(it) != '}') { *it = '/'; }
  89. std::advance(it, 1);
  90. }
  91. gtk_entry_set_text(m_entry, fileName.c_str());
  92. getBox().setSettingValue(this->getSettingIndex(), fileName.c_str());
  93. }
  94. }
  95. #endif
  96. } // namespace Setting
  97. } // namespace Designer
  98. } // namespace OpenViBE