[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
pull/343/head^2
donho 17 years ago
parent a088bf7f0f
commit 681ef5ea41

@ -320,6 +320,7 @@ bool Notepad_plus::doOpen(const char *fileName, bool isReadOnly)
char longFileName[MAX_PATH]; char longFileName[MAX_PATH];
::GetFullPathName(fileName, MAX_PATH, longFileName, NULL); ::GetFullPathName(fileName, MAX_PATH, longFileName, NULL);
//printInt(getCurrentView());
if (switchToFile(longFileName)) if (switchToFile(longFileName))
{ {
if (_pTrayIco) if (_pTrayIco)
@ -531,6 +532,8 @@ void Notepad_plus::setFileOpenSaveDlgFilters(FileDialog & fDlg)
int i = 0; int i = 0;
Lang *l = NppParameters::getInstance()->getLangFromIndex(i++); Lang *l = NppParameters::getInstance()->getLangFromIndex(i++);
LangType curl = _pEditView->getCurrentBuffer().getLangType();
while (l) while (l)
{ {
LangType lid = l->getLangID(); LangType lid = l->getLangID();
@ -570,7 +573,14 @@ void Notepad_plus::setFileOpenSaveDlgFilters(FileDialog & fDlg)
string stringFilters = exts2Filters(list); string stringFilters = exts2Filters(list);
const char *filters = stringFilters.c_str(); const char *filters = stringFilters.c_str();
if (filters[0]) 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++); l = (NppParameters::getInstance())->getLangFromIndex(i++);
} }
@ -4270,6 +4280,7 @@ void Notepad_plus::docGotoAnotherEditView(bool mode)
switchEditViewTo((getCurrentView() == MAIN_VIEW)?SUB_VIEW:MAIN_VIEW); switchEditViewTo((getCurrentView() == MAIN_VIEW)?SUB_VIEW:MAIN_VIEW);
} }
} }
//printInt(getCurrentView());
_linkTriggered = true; _linkTriggered = true;
} }
@ -4309,6 +4320,7 @@ int Notepad_plus::switchEditViewTo(int gid)
setLangStatus(_pEditView->getCurrentDocType()); setLangStatus(_pEditView->getCurrentDocType());
updateStatusBar(); updateStatusBar();
dynamicCheckMenuAndTB(); dynamicCheckMenuAndTB();
//printInt(getCurrentView());
return oldView; return oldView;
} }

@ -20,7 +20,7 @@
FileDialog *FileDialog::staticThis = NULL; FileDialog *FileDialog::staticThis = NULL;
FileDialog::FileDialog(HWND hwnd, HINSTANCE hInst) FileDialog::FileDialog(HWND hwnd, HINSTANCE hInst)
: _nbCharFileExt(0), _nbExt(0) : _nbCharFileExt(0), _nbExt(0), _initIndex(-1)
{staticThis = this; {staticThis = this;
for (int i = 0 ; i < nbExtMax ; i++) for (int i = 0 ; i < nbExtMax ; i++)
_extArray[i][0] = '\0'; _extArray[i][0] = '\0';
@ -101,7 +101,7 @@ void FileDialog::setExtFilter(const char *extText, const char *ext, ...)
_nbCharFileExt += exts.length() + 1; _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 // fill out the ext array for save as file dialog
if (_nbExt < nbExtMax) if (_nbExt < nbExtMax)
@ -120,6 +120,8 @@ void FileDialog::setExtsFilter(const char *extText, const char *exts)
pFileExt = _fileExt + _nbCharFileExt; pFileExt = _fileExt + _nbCharFileExt;
memcpy(pFileExt, exts, strlen(exts) + 1); memcpy(pFileExt, exts, strlen(exts) + 1);
_nbCharFileExt += strlen(exts) + 1; _nbCharFileExt += strlen(exts) + 1;
return _nbExt;
} }
char * FileDialog::doOpenSingleFileDlg() char * FileDialog::doOpenSingleFileDlg()
@ -200,6 +202,37 @@ char * FileDialog::doSaveDlg()
return (fn); 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) 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 : case WM_INITDIALOG :
{ {
::SetWindowLong(hWnd, GWL_USERDATA, (long)staticThis); ::SetWindowLong(hWnd, GWL_USERDATA, (long)staticThis);
//printStr("en train"); hFileDlg = ::GetParent(hWnd);
//pStaticDlg->run_dlgProc(message, wParam, lParam); 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; 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); return pFileDialog->run(hWnd, uMsg, wParam, lParam);
} }
} }
//::OFNHookProc(hWnd, uMsg, wParam, lParam);
return FALSE; 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;
}
}

@ -26,8 +26,8 @@
#include <string> #include <string>
#include "SysMsg.h" #include "SysMsg.h"
const int nbExtMax = 10; const int nbExtMax = 256;
const int extLenMax = 10; const int extLenMax = 64;
using namespace std; using namespace std;
@ -35,86 +35,63 @@ typedef vector<string> stringVector;
//const bool styleOpen = true; //const bool styleOpen = true;
//const bool styleSave = false; //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, &center);
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 class FileDialog
{ {
public: public:
FileDialog(HWND hwnd, HINSTANCE hInst); FileDialog(HWND hwnd, HINSTANCE hInst);
void setExtFilter(const char *, const char *, ...); 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);} void setDefFileName(const char *fn){strcpy(_fileName, fn);}
char * doSaveDlg(); char * doSaveDlg();
stringVector * doOpenMultiFilesDlg(); stringVector * doOpenMultiFilesDlg();
char * doOpenSingleFileDlg(); char * doOpenSingleFileDlg();
bool isReadOnly() {return _ofn.Flags & OFN_READONLY;}; bool isReadOnly() {return _ofn.Flags & OFN_READONLY;};
void setInitIndex(int i) {
_initIndex = i;
};
protected : protected :
static UINT APIENTRY OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); static UINT APIENTRY OFNHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL APIENTRY run(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;
}
};
private: private:
char _fileName[MAX_PATH*8]; char _fileName[MAX_PATH*8];
@ -127,6 +104,8 @@ private:
char _extArray[nbExtMax][extLenMax]; char _extArray[nbExtMax][extLenMax];
int _nbExt; int _nbExt;
int _initIndex;
char * get1stExt(char *ext) { // precondition : ext should be under the format : Batch (*.bat;*.cmd;*.nt) char * get1stExt(char *ext) { // precondition : ext should be under the format : Batch (*.bat;*.cmd;*.nt)
char *begin = ext; char *begin = ext;

@ -2,6 +2,8 @@
<NotepadPlus> <NotepadPlus>
<!-- The key words of the supported languages, don't touch them! --> <!-- The key words of the supported languages, don't touch them! -->
<Languages> <Languages>
<Language name="normal" ext="txt">
</Language>
<Language name="c" ext="c" commentLine="//" commentStart="/*" commentEnd="*/"> <Language name="c" ext="c" commentLine="//" commentStart="/*" commentEnd="*/">
<Keywords name="instre1">if else switch case default break goto return for while do continue typedef sizeof NULL</Keywords> <Keywords name="instre1">if else switch case default break goto return for while do continue typedef sizeof NULL</Keywords>
<Keywords name="type1">void struct union enum char short int long double float signed unsigned const static extern auto register volatile</Keywords> <Keywords name="type1">void struct union enum char short int long double float signed unsigned const static extern auto register volatile</Keywords>

Loading…
Cancel
Save