From 2fd2cd1e7a9b753320eaa914557d25b74e7453e3 Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Thu, 18 Feb 2016 04:21:27 +0000 Subject: [PATCH] Some code cleanup of FNFC Win32: no functional changes. > Replace huge static WCHAR wpattern[] array with dynamic in win32 FNFC (ABI 1.3.4) (Should decrease the memory weight WIN32 lib) > Simplify current working directory management > Comment simplification, CMP adherence git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@11188 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- FL/Fl_Native_File_Chooser.H | 5 +- src/Fl_Native_File_Chooser_WIN32.cxx | 78 +++++++++++++++++----------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/FL/Fl_Native_File_Chooser.H b/FL/Fl_Native_File_Chooser.H index 1a9912a29..b025f69c6 100644 --- a/FL/Fl_Native_File_Chooser.H +++ b/FL/Fl_Native_File_Chooser.H @@ -159,8 +159,9 @@ private: int _btype; // kind-of browser to show() int _options; // general options #if FLTK_ABI_VERSION >= 10304 - fl_OPENFILENAMEW *_ofn_ptr; // GetOpenFileName() & GetSaveFileName() struct - fl_BROWSEINFOW *_binf_ptr; // SHBrowseForFolder() struct + fl_OPENFILENAMEW *_ofn_ptr; // GetOpenFileName() & GetSaveFileName() struct + fl_BROWSEINFOW *_binf_ptr; // SHBrowseForFolder() struct + WCHAR *_wpattern; // pattern buffer for filter #else fl_OPENFILENAMEW _ofn; fl_BROWSEINFOW _binf; diff --git a/src/Fl_Native_File_Chooser_WIN32.cxx b/src/Fl_Native_File_Chooser_WIN32.cxx index 2f36635bc..1155af195 100644 --- a/src/Fl_Native_File_Chooser_WIN32.cxx +++ b/src/Fl_Native_File_Chooser_WIN32.cxx @@ -136,6 +136,7 @@ Fl_Native_File_Chooser::Fl_Native_File_Chooser(int val) { #if FLTK_ABI_VERSION >= 10304 _ofn_ptr = new OPENFILENAMEW; _binf_ptr = new BROWSEINFOW; + _wpattern = 0; #endif memset((void*)_ofn_ptr, 0, sizeof(OPENFILENAMEW)); _ofn_ptr->lStructSize = sizeof(OPENFILENAMEW); @@ -170,6 +171,7 @@ Fl_Native_File_Chooser::~Fl_Native_File_Chooser() { #if FLTK_ABI_VERSION >= 10304 delete _binf_ptr; delete _ofn_ptr; + if ( _wpattern ) delete[] _wpattern; #endif } @@ -286,21 +288,42 @@ void Fl_Native_File_Chooser::Unix2Win(char *s) { if ( *s == '/' ) *s = '\\'; } +// SAVE THE CURRENT WORKING DIRECTORY +// Returns a malloc()ed copy of the cwd that can +// later be freed with RestoreCWD(). May return 0 on error. +// +static char *SaveCWD() { + char *thecwd = 0; + DWORD thecwdsz = GetCurrentDirectory(0,0); + if ( thecwdsz > 0 ) { + thecwd = (char*)malloc(thecwdsz); + if (GetCurrentDirectory(thecwdsz, thecwd) == 0 ) { + free(thecwd); thecwd = 0; + } + } + return thecwd; +} + +// RESTORES THE CWD SAVED BY SaveCWD(), FREES STRING +// Always returns NULL (string was freed). +// +static void RestoreCWD(char *thecwd) { + if ( !thecwd ) return; + SetCurrentDirectory(thecwd); + free(thecwd); +} + // SHOW FILE BROWSER int Fl_Native_File_Chooser::showfile() { ClearOFN(); clear_pathnames(); size_t fsize = FNFC_MAX_PATH; - _ofn_ptr->Flags |= OFN_NOVALIDATE; // prevent disabling of front slashes + _ofn_ptr->Flags |= OFN_NOVALIDATE; // prevent disabling of front slashes _ofn_ptr->Flags |= OFN_HIDEREADONLY; // hide goofy readonly flag // USE NEW BROWSER - _ofn_ptr->Flags |= OFN_EXPLORER; // use newer explorer windows + _ofn_ptr->Flags |= OFN_EXPLORER; // use newer explorer windows _ofn_ptr->Flags |= OFN_ENABLESIZING; // allow window to be resized (hey, why not?) - - // XXX: The docs for OFN_NOCHANGEDIR says the flag is 'ineffective' on XP/2K/NT! - // But let's set it anyway.. - // - _ofn_ptr->Flags |= OFN_NOCHANGEDIR; // prevent dialog for messing up the cwd + _ofn_ptr->Flags |= OFN_NOCHANGEDIR; // XXX: docs say ineffective on XP/2K/NT, but set it anyway.. switch ( _btype ) { case BROWSE_DIRECTORY: @@ -336,17 +359,24 @@ int Fl_Native_File_Chooser::showfile() { } // FILTER if (_parsedfilt != NULL) { // to convert a null-containing char string into a widechar string - static WCHAR wpattern[FNFC_MAX_PATH]; +#if FLTK_ABI_VERSION >= 10304 + // NEW + if ( !_wpattern ) _wpattern = new WCHAR[FNFC_MAX_PATH]; +#else + // OLD + static WCHAR _wpattern[FNFC_MAX_PATH]; // yuck -- replace with managed class member +#endif const char *p = _parsedfilt; while(*(p + strlen(p) + 1) != 0) p += strlen(p) + 1; p += strlen(p) + 2; - MultiByteToWideChar(CP_UTF8, 0, _parsedfilt, (int) (p - _parsedfilt), wpattern, FNFC_MAX_PATH); - _ofn_ptr->lpstrFilter = wpattern; + MultiByteToWideChar(CP_UTF8, 0, _parsedfilt, (int) (p - _parsedfilt), _wpattern, FNFC_MAX_PATH); + _ofn_ptr->lpstrFilter = _wpattern; } else { _ofn_ptr->lpstrFilter = NULL; } // PRESET FILE // If set, supercedes _directory. See KB Q86920 for details + // XXX: this doesn't preselect the item in the listview.. why? // if ( _preset_file ) { size_t len = strlen(_preset_file); @@ -356,7 +386,7 @@ int Fl_Native_File_Chooser::showfile() { return(-1); } wcscpy(_ofn_ptr->lpstrFile, utf8towchar(_preset_file)); -// Unix2Win(_ofn_ptr->lpstrFile); + // Unix2Win(_ofn_ptr->lpstrFile); len = wcslen(_ofn_ptr->lpstrFile); _ofn_ptr->lpstrFile[len+0] = 0; // multiselect needs dnull _ofn_ptr->lpstrFile[len+1] = 0; @@ -371,18 +401,9 @@ int Fl_Native_File_Chooser::showfile() { // Unix2Win((char*)_ofn_ptr->lpstrInitialDir); } // SAVE THE CURRENT DIRECTORY - // XXX: Save the cwd because GetOpenFileName() is probably going to - // change it, in spite of the OFN_NOCHANGEDIR flag, due to its docs - // saying the flag is 'ineffective'. %^( + // See above warning (XXX) for OFN_NOCHANGEDIR // - char *oldcwd = 0; - DWORD oldcwdsz = GetCurrentDirectory(0,0); - if ( oldcwdsz > 0 ) { - oldcwd = (char*)malloc(oldcwdsz); - if (GetCurrentDirectory(oldcwdsz, oldcwd) == 0 ) { - free(oldcwd); oldcwd = 0; - } - } + char *save_cwd = SaveCWD(); // must be freed with RestoreCWD() // OPEN THE DIALOG WINDOW int err; if ( _btype == BROWSE_SAVE_FILE ) { @@ -392,11 +413,8 @@ int Fl_Native_File_Chooser::showfile() { } // GET EXTENDED ERROR int exterr = CommDlgExtendedError(); - // XXX: RESTORE CWD - if ( oldcwd ) { - SetCurrentDirectory(oldcwd); - free(oldcwd); oldcwd = 0; - } + // RESTORE CURRENT DIRECTORY + RestoreCWD(save_cwd); save_cwd = 0; // also frees save_cwd // ERROR OR CANCEL? if ( err == 0 ) { if ( exterr == 0 ) return(1); // user hit cancel @@ -929,8 +947,7 @@ int Fl_Native_File_Chooser::filters() const { return(_nfilters); } -static char *wchartoutf8(LPCWSTR in) -{ +static char *wchartoutf8(LPCWSTR in) { static char *out = NULL; static int lchar = 0; if (in == NULL)return NULL; @@ -943,8 +960,7 @@ static char *wchartoutf8(LPCWSTR in) return out; } -static LPCWSTR utf8towchar(const char *in) -{ +static LPCWSTR utf8towchar(const char *in) { static WCHAR *wout = NULL; static int lwout = 0; if (in == NULL)return NULL;