From 681ef5ea41a09183afe9490baad416bd0cecc458 Mon Sep 17 00:00:00 2001 From: donho Date: Thu, 11 Oct 2007 23:24:56 +0000 Subject: [PATCH] [NEW_FEATURE] Enhance auto-added ext in file save dialog. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository@51 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/Notepad_plus.cpp | 14 ++- .../OpenSaveFileDialog/FileDialog.cpp | 96 ++++++++++++++- .../OpenSaveFileDialog/FileDialog.h | 113 +++++++----------- PowerEditor/src/langs.xml | 2 + 4 files changed, 152 insertions(+), 73 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index c5acad07..042725c4 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -320,6 +320,7 @@ bool Notepad_plus::doOpen(const char *fileName, bool isReadOnly) char longFileName[MAX_PATH]; ::GetFullPathName(fileName, MAX_PATH, longFileName, NULL); + //printInt(getCurrentView()); if (switchToFile(longFileName)) { if (_pTrayIco) @@ -531,6 +532,8 @@ void Notepad_plus::setFileOpenSaveDlgFilters(FileDialog & fDlg) int i = 0; Lang *l = NppParameters::getInstance()->getLangFromIndex(i++); + LangType curl = _pEditView->getCurrentBuffer().getLangType(); + while (l) { LangType lid = l->getLangID(); @@ -570,7 +573,14 @@ void Notepad_plus::setFileOpenSaveDlgFilters(FileDialog & fDlg) string stringFilters = exts2Filters(list); const char *filters = stringFilters.c_str(); if (filters[0]) - fDlg.setExtsFilter(getLangDesc(lid, true).c_str(), filters); + { + int nbExt = fDlg.setExtsFilter(getLangDesc(lid, true).c_str(), filters); + + if (curl == lid) + { + fDlg.setInitIndex(nbExt - 1); + } + } } l = (NppParameters::getInstance())->getLangFromIndex(i++); } @@ -4270,6 +4280,7 @@ void Notepad_plus::docGotoAnotherEditView(bool mode) switchEditViewTo((getCurrentView() == MAIN_VIEW)?SUB_VIEW:MAIN_VIEW); } } + //printInt(getCurrentView()); _linkTriggered = true; } @@ -4309,6 +4320,7 @@ int Notepad_plus::switchEditViewTo(int gid) setLangStatus(_pEditView->getCurrentDocType()); updateStatusBar(); dynamicCheckMenuAndTB(); + //printInt(getCurrentView()); return oldView; } diff --git a/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.cpp b/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.cpp index 27c28606..7b29b275 100644 --- a/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.cpp +++ b/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.cpp @@ -20,7 +20,7 @@ FileDialog *FileDialog::staticThis = NULL; FileDialog::FileDialog(HWND hwnd, HINSTANCE hInst) - : _nbCharFileExt(0), _nbExt(0) + : _nbCharFileExt(0), _nbExt(0), _initIndex(-1) {staticThis = this; for (int i = 0 ; i < nbExtMax ; i++) _extArray[i][0] = '\0'; @@ -101,7 +101,7 @@ void FileDialog::setExtFilter(const char *extText, const char *ext, ...) _nbCharFileExt += exts.length() + 1; } -void FileDialog::setExtsFilter(const char *extText, const char *exts) +int FileDialog::setExtsFilter(const char *extText, const char *exts) { // fill out the ext array for save as file dialog if (_nbExt < nbExtMax) @@ -120,6 +120,8 @@ void FileDialog::setExtsFilter(const char *extText, const char *exts) pFileExt = _fileExt + _nbCharFileExt; memcpy(pFileExt, exts, strlen(exts) + 1); _nbCharFileExt += strlen(exts) + 1; + + return _nbExt; } char * FileDialog::doOpenSingleFileDlg() @@ -200,6 +202,37 @@ char * FileDialog::doSaveDlg() return (fn); } +static HWND hFileDlg = NULL; +static WNDPROC oldProc = NULL; +static string currentExt = ""; + +static BOOL CALLBACK fileDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { + switch (message) + { + case WM_COMMAND : + { + switch (wParam) + { + case IDOK : + { + HWND fnControl = ::GetDlgItem(hwnd, edt1); + char fn[256]; + ::GetWindowText(fnControl, fn, sizeof(fn)); + if (*fn == '\0') + return oldProc(hwnd, message, wParam, lParam); + + string fnExt = changeExt(fn, currentExt); + ::SetWindowText(fnControl, fnExt.c_str()); + return oldProc(hwnd, message, wParam, lParam); + } + + default : + break; + } + } + } + return oldProc(hwnd, message, wParam, lParam); +}; UINT_PTR CALLBACK FileDialog::OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { @@ -208,8 +241,21 @@ UINT_PTR CALLBACK FileDialog::OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, L case WM_INITDIALOG : { ::SetWindowLong(hWnd, GWL_USERDATA, (long)staticThis); - //printStr("en train"); - //pStaticDlg->run_dlgProc(message, wParam, lParam); + hFileDlg = ::GetParent(hWnd); + goToCenter(hFileDlg); + if (staticThis->_initIndex != -1) + { + HWND typeControl = ::GetDlgItem(hFileDlg, cmb1); + ::SendMessage(typeControl, CB_SETCURSEL, staticThis->_initIndex, 0); + char ext[256]; + ::SendMessage(typeControl, CB_GETLBTEXT, staticThis->_initIndex, (LPARAM)ext); + char *pExt = staticThis->get1stExt(ext); + if (*pExt == '\0') + return TRUE; + + currentExt = pExt; + } + oldProc = (WNDPROC)::SetWindowLong(hFileDlg, GWL_WNDPROC, (LONG)fileDlgProc); return FALSE; } @@ -221,7 +267,47 @@ UINT_PTR CALLBACK FileDialog::OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, L return pFileDialog->run(hWnd, uMsg, wParam, lParam); } } - //::OFNHookProc(hWnd, uMsg, wParam, lParam); return FALSE; } +BOOL APIENTRY FileDialog::run(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + switch (uMsg) + { + case WM_NOTIFY : + { + LPNMHDR pNmhdr = (LPNMHDR)lParam; + switch(pNmhdr->code) + { + case CDN_TYPECHANGE : + { + HWND fnControl = ::GetDlgItem(::GetParent(hWnd), edt1); + char fn[256]; + ::GetWindowText(fnControl, fn, sizeof(fn)); + if (*fn == '\0') + return TRUE; + + HWND typeControl = ::GetDlgItem(::GetParent(hWnd), cmb1); + int i = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0); + char ext[256]; + ::SendMessage(typeControl, CB_GETLBTEXT, i, (LPARAM)ext); + + char *pExt = get1stExt(ext); + if (*pExt == '\0') + return TRUE; + + currentExt = pExt; + string fnExt = changeExt(fn, pExt); + + ::SetWindowText(fnControl, fnExt.c_str()); + break; + } + default : + return FALSE; + } + return TRUE; + } + default : + return FALSE; + } +} diff --git a/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.h b/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.h index 43f4df0d..ca9c911d 100644 --- a/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.h +++ b/PowerEditor/src/WinControls/OpenSaveFileDialog/FileDialog.h @@ -26,8 +26,8 @@ #include #include "SysMsg.h" -const int nbExtMax = 10; -const int extLenMax = 10; +const int nbExtMax = 256; +const int extLenMax = 64; using namespace std; @@ -35,86 +35,63 @@ typedef vector stringVector; //const bool styleOpen = true; //const bool styleSave = false; + +static string changeExt(string fn, string ext) +{ + string fnExt = fn; + int index = fnExt.find_last_of("."); + string extension = "."; + extension += ext; + if (index == string::npos) + { + fnExt += extension; + } + else + { + int len = (extension.length() > fnExt.length() - index + 1)?extension.length():fnExt.length() - index + 1; + fnExt.replace(index, len, extension); + } + return fnExt; +}; + +static void goToCenter(HWND hwnd) +{ + RECT rc; + HWND hParent = ::GetParent(hwnd); + ::GetClientRect(hParent, &rc); + POINT center; + center.x = rc.left + (rc.right - rc.left)/2; + center.y = rc.top + (rc.bottom - rc.top)/2; + ::ClientToScreen(hParent, ¢er); + + RECT _rc; + ::GetWindowRect(hwnd, &_rc); + int x = center.x - (_rc.right - _rc.left)/2; + int y = center.y - (_rc.bottom - _rc.top)/2; + + ::SetWindowPos(hwnd, HWND_TOP, x, y, _rc.right - _rc.left, _rc.bottom - _rc.top, SWP_SHOWWINDOW); +}; + class FileDialog { public: FileDialog(HWND hwnd, HINSTANCE hInst); void setExtFilter(const char *, const char *, ...); - void setExtsFilter(const char *extText, const char *exts); + int setExtsFilter(const char *extText, const char *exts); void setDefFileName(const char *fn){strcpy(_fileName, fn);} char * doSaveDlg(); stringVector * doOpenMultiFilesDlg(); char * doOpenSingleFileDlg(); bool isReadOnly() {return _ofn.Flags & OFN_READONLY;}; + void setInitIndex(int i) { + _initIndex = i; + }; protected : static UINT APIENTRY OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); - BOOL APIENTRY run(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - switch (uMsg) - { - case WM_NOTIFY : - { - LPNMHDR pNmhdr = (LPNMHDR)lParam; - switch(pNmhdr->code) - { - case CDN_FILEOK : - { - printStr("CDN_FILEOK"); - if ((_fileName)&&(!strrchr(_extArray[_ofn.nFilterIndex - 1], '*')) - && (strcmp(_extArray[_ofn.nFilterIndex - 1], _fileName + _ofn.nFileExtension - 1))) - { - strcat(_fileName, _extArray[_ofn.nFilterIndex - 1]); - } - break; - } - case CDN_TYPECHANGE : - { - HWND fnControl = ::GetDlgItem(::GetParent(hWnd), edt1); - char fn[256]; - ::GetWindowText(fnControl, fn, sizeof(fn)); - if (*fn == '\0') - return TRUE; - - HWND typeControl = ::GetDlgItem(::GetParent(hWnd), cmb1); - int i = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0); - char ext[256]; - ::SendMessage(typeControl, CB_GETLBTEXT, i, (LPARAM)ext); - //printInt(i); - // - char *pExt = get1stExt(ext); - if (*pExt == '\0') - return TRUE; - //::SendMessage(::GetParent(hWnd), CDM_SETDEFEXT, 0, (LPARAM)pExt); - - string fnExt = fn; - - int index = fnExt.find_last_of("."); - string extension = "."; - extension += pExt; - if (index == string::npos) - { - fnExt += extension; - } - else - { - int len = (extension.length() > fnExt.length() - index + 1)?extension.length():fnExt.length() - index + 1; - fnExt.replace(index, len, extension); - } - - ::SetWindowText(fnControl, fnExt.c_str()); - break; - } - default : - return FALSE; - } - return TRUE; - } - default : - return FALSE; - } - }; + BOOL APIENTRY run(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); private: char _fileName[MAX_PATH*8]; @@ -127,6 +104,8 @@ private: char _extArray[nbExtMax][extLenMax]; int _nbExt; + int _initIndex; + char * get1stExt(char *ext) { // precondition : ext should be under the format : Batch (*.bat;*.cmd;*.nt) char *begin = ext; diff --git a/PowerEditor/src/langs.xml b/PowerEditor/src/langs.xml index 2aa5e7ad..278260d9 100644 --- a/PowerEditor/src/langs.xml +++ b/PowerEditor/src/langs.xml @@ -2,6 +2,8 @@ + + if else switch case default break goto return for while do continue typedef sizeof NULL void struct union enum char short int long double float signed unsigned const static extern auto register volatile