From 78a4fd75621ab829ed0005b043f7262ebec13f69 Mon Sep 17 00:00:00 2001 From: joelholdsworth Date: Thu, 28 Feb 2008 16:37:08 +0000 Subject: [PATCH] Fixed off-by-one filter bug with the windows native file dialogs, and tidied code a bit in filedialogimpl-win32.cpp/h --- src/ui/dialog/filedialogimpl-win32.cpp | 67 +++++++------------------- src/ui/dialog/filedialogimpl-win32.h | 42 +++++++++------- 2 files changed, 42 insertions(+), 67 deletions(-) diff --git a/src/ui/dialog/filedialogimpl-win32.cpp b/src/ui/dialog/filedialogimpl-win32.cpp index db49b4a86..354d673bc 100644 --- a/src/ui/dialog/filedialogimpl-win32.cpp +++ b/src/ui/dialog/filedialogimpl-win32.cpp @@ -104,9 +104,11 @@ FileDialogBaseWin32::FileDialogBaseWin32(Gtk::Window &parent, parent(parent), _current_directory(dir) { - //_mutex = NULL; _main_loop = NULL; + _filter_index = 1; + _filter_count = 0; + _title = (wchar_t*)g_utf8_to_utf16(title, -1, NULL, NULL, NULL); Glib::RefPtr parentWindow = parent.get_window(); @@ -299,7 +301,8 @@ void FileOpenDialogImplWin32::createFilterMenu() } *(filterptr++) = L'\0'; - _filterIndex = 2; + _filter_count = extension_index; + _filter_index = 2; // Select the 2nd filter in the list - NOT the 3rd } void FileOpenDialogImplWin32::GetOpenFileName_thread() @@ -307,9 +310,7 @@ void FileOpenDialogImplWin32::GetOpenFileName_thread() OPENFILENAMEEXW ofn; g_assert(this != NULL); - //g_assert(_mutex != NULL); - g_assert(_main_loop != NULL); - + WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16( _current_directory.data(), -1, NULL, NULL, NULL); @@ -332,14 +333,15 @@ void FileOpenDialogImplWin32::GetOpenFileName_thread() ofn.lpstrTitle = _title; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_ENABLESIZING; ofn.lpstrFilter = _filter; - ofn.nFilterIndex = _filterIndex; + ofn.nFilterIndex = _filter_index; ofn.lpfnHook = GetOpenFileName_hookproc; ofn.lCustData = (LPARAM)this; _result = GetOpenFileNameW(&ofn) != 0; - _filterIndex = ofn.nFilterIndex; - _extension = _extension_map[ofn.nFilterIndex]; + g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count); + _filter_index = ofn.nFilterIndex; + _extension = _extension_map[ofn.nFilterIndex - 1]; myFilename = utf16_to_ustring(_path_string, _MAX_PATH); @@ -352,9 +354,6 @@ void FileOpenDialogImplWin32::GetOpenFileName_thread() _mutex->lock(); _finished = true; _mutex->unlock(); - //g_main_loop_quit(_main_loop); - - } void FileOpenDialogImplWin32::register_preview_wnd_class() @@ -1143,7 +1142,6 @@ void FileOpenDialogImplWin32::render_preview() context->stroke_preserve(); // Draw the image - if(_preview_bitmap_image) // Is the image a pixbuf? { // Set the transformation @@ -1194,7 +1192,7 @@ bool FileOpenDialogImplWin32::show() { // We can only run one worker thread at a time - //if(_mutex != NULL) return false; + if(_mutex != NULL) return false; if(!Glib::thread_supported()) Glib::thread_init(); @@ -1225,7 +1223,6 @@ FileOpenDialogImplWin32::show() Sleep(10); } - //g_main_loop_run(_main_loop); } // Tidy up @@ -1260,8 +1257,6 @@ FileSaveDialogImplWin32::FileSaveDialogImplWin32(Gtk::Window &parent, const Glib::ustring &/*default_key*/) : FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.save_as") { - _main_loop = NULL; - createFilterMenu(); } @@ -1340,7 +1335,8 @@ void FileSaveDialogImplWin32::createFilterMenu() } *(filterptr++) = 0; - _filterIndex = 0; + _filter_count = extension_index; + _filter_index = 1; // A value of 1 selects the 1st filter - NOT the 2nd } void FileSaveDialogImplWin32::GetSaveFileName_thread() @@ -1348,7 +1344,6 @@ void FileSaveDialogImplWin32::GetSaveFileName_thread() OPENFILENAMEEXW ofn; g_assert(this != NULL); - //g_assert(_mutex != NULL); g_assert(_main_loop != NULL); gunichar2* current_directory_string = g_utf8_to_utf16( @@ -1366,28 +1361,24 @@ void FileSaveDialogImplWin32::GetSaveFileName_thread() ofn.hwndOwner = _ownerHwnd; ofn.lpstrFile = _path_string; ofn.nMaxFile = _MAX_PATH; - ofn.nFilterIndex = _filterIndex; + ofn.nFilterIndex = _filter_index; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = (wchar_t*)current_directory_string; ofn.lpstrTitle = _title; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; ofn.lpstrFilter = _filter; - ofn.nFilterIndex = _filterIndex; + ofn.nFilterIndex = _filter_index; _result = GetSaveFileNameW(&ofn) != 0; - _filterIndex = ofn.nFilterIndex; - _extension = _extension_map[ofn.nFilterIndex]; + g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count); + _filter_index = ofn.nFilterIndex; + _extension = _extension_map[ofn.nFilterIndex - 1]; // Copy the selected file name, converting from UTF-16 to UTF-8 myFilename = utf16_to_ustring(_path_string, _MAX_PATH); - //_mutex->lock(); - //_finished = true; - //_mutex->unlock(); - - // Tidy up g_free(current_directory_string); @@ -1400,36 +1391,14 @@ void FileSaveDialogImplWin32::GetSaveFileName_thread() bool FileSaveDialogImplWin32::show() { - // We can only run one worker thread at a time - //if(_mutex != NULL) return false; - if(!Glib::thread_supported()) Glib::thread_init(); _result = false; - //_finished = false; - //_mutex = new Glib::Mutex(); _main_loop = g_main_loop_new(g_main_context_default(), FALSE); if(Glib::Thread::create(sigc::mem_fun(*this, &FileSaveDialogImplWin32::GetSaveFileName_thread), true)) - { - /*while(1) - { - // While the dialog runs - keep the main UI alive - g_main_context_iteration(g_main_context_default(), FALSE); - - if(_mutex->trylock()) - { - if(_finished) break; - _mutex->unlock(); - } - - Sleep(10); - }*/ g_main_loop_run(_main_loop); - } - //delete _mutex; - //_mutex = NULL; if(_result) appendExtension(myFilename, (Inkscape::Extension::Output*)_extension); diff --git a/src/ui/dialog/filedialogimpl-win32.h b/src/ui/dialog/filedialogimpl-win32.h index fd0d1c259..9de490193 100644 --- a/src/ui/dialog/filedialogimpl-win32.h +++ b/src/ui/dialog/filedialogimpl-win32.h @@ -57,17 +57,8 @@ public: protected: /// The dialog type FileDialogType dialogType; - - /// This mutex is used to ensure that the worker thread - /// that calls GetOpenFileName cannot collide with the - /// main Inkscape thread - Glib::Mutex *_mutex; - - /// This flag is set true when the GetOpenFileName call - /// has returned - bool _finished; - - /// A pointer to the GTK main-loop context object. This + + /// A pointer to the GTK main-loop context object. This /// is used to keep the rest of the inkscape UI running /// while the file dialog is displayed GMainLoop *_main_loop; @@ -95,8 +86,13 @@ protected: /// The filter string for GetOpenFileName in UTF-16 wchar_t *_filter; - /// The index of the currently selected filter - int _filterIndex; + /// The index of the currently selected filter. + /// This value must be greater than or equal to 1, + /// and less than or equal to _filter_count. + int _filter_index; + + /// The number of filters registered + int _filter_count; /// An array of the extensions associated with the /// file types of each filter. So the Nth entry of @@ -104,8 +100,8 @@ protected: /// filter in the list. NULL if no specific extension is /// specified/ Inkscape::Extension::Extension **_extension_map; - - /// The currently selected extension. Valid after an [OK] + + /// The currently selected extension. Valid after an [OK] Inkscape::Extension::Extension *_extension; }; @@ -213,6 +209,15 @@ private: /// This flag is set true if a file has been selected bool _file_selected; + + /// This flag is set true when the GetOpenFileName call + /// has returned + bool _finished; + + /// This mutex is used to ensure that the worker thread + /// that calls GetOpenFileName cannot collide with the + /// main Inkscape thread + Glib::Mutex *_mutex; /// The controller function for the thread which calls @@ -321,12 +326,13 @@ public: private: - /** - * Create a filter menu for this type of dialog - */ + /// Create a filter menu for this type of dialog void createFilterMenu(); + /// The controller function for the thread which calls + /// GetSaveFileName void GetSaveFileName_thread(); + }; -- 2.30.2