?? windowsdlg.cpp
字號:
#include <windows.h>
#include "WindowsDlg.h"
#include "WindowsDlgRc.h"
#include "ScintillaEditView.h"
#include <algorithm>
#include <functional>
#include <vector>
#ifndef _countof
#define _countof(x) (sizeof(x)/sizeof((x)[0]))
#endif
#ifndef LVS_EX_DOUBLEBUFFER
#define LVS_EX_DOUBLEBUFFER 0x00010000
#endif
static const char *readonlyString = " [Read Only]";
const UINT WDN_NOTIFY = RegisterWindowMessage("WDN_NOTIFY");
inline static DWORD GetStyle(HWND hWnd) {
return (DWORD)GetWindowLong(hWnd, GWL_STYLE);
}
inline static DWORD GetExStyle(HWND hWnd) {
return (DWORD)GetWindowLong(hWnd, GWL_EXSTYLE);
}
inline static BOOL ModifyStyle(HWND hWnd, DWORD dwRemove, DWORD dwAdd) {
DWORD dwStyle = ::GetWindowLong(hWnd, GWL_STYLE);
DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
if(dwStyle == dwNewStyle)
return FALSE;
::SetWindowLong(hWnd, GWL_STYLE, dwNewStyle);
return TRUE;
}
inline static BOOL ModifyStyleEx(HWND hWnd, DWORD dwRemove, DWORD dwAdd) {
DWORD dwStyle = ::GetWindowLong(hWnd, GWL_EXSTYLE);
DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
if(dwStyle == dwNewStyle)
return FALSE;
::SetWindowLong(hWnd, GWL_EXSTYLE, dwNewStyle);
return TRUE;
}
struct NumericStringEquivalence
{
bool operator()(const char* s1, const char* s2) const
{ return numstrcmp(s1, s2) < 0; }
static inline int numstrcmp_get(const char **str, int *length)
{
const char *p = *str;
int value = 0;
for (*length = 0; isdigit(*p); (*length)++)
value = value * 10 + *p++ - '0';
*str = p;
return (value);
}
static int numstrcmp(const char *str1, const char *str2)
{
char *p1, *p2;
int c1, c2, lcmp;
for(;;)
{
c1 = tolower(*str1), c2 = tolower(*str2);
if ( c1 == 0 || c2 == 0 )
break;
else if (isdigit(c1) && isdigit(c2))
{
lcmp = strtol(str1, &p1, 10) - strtol(str2, &p2, 10);
if ( lcmp == 0 )
lcmp = (p2 - str2) - (p1 - str1);
if ( lcmp != 0 )
return (lcmp > 0 ? 1 : -1);
str1 = p1, str2 = p2;
}
else
{
lcmp = (c1 - c2);
if (lcmp != 0)
return (lcmp > 0 ? 1 : -1);
++str1, ++str2;
}
}
lcmp = (c1 - c2);
return ( lcmp < 0 ) ? -1 : (lcmp > 0 ? 1 : 0);
}
};
struct BufferEquivalent
{
NumericStringEquivalence _strequiv;
ScintillaEditView *_pView;
int _iColumn;
bool _reverse;
BufferEquivalent(ScintillaEditView *pView, int iColumn, bool reverse)
: _pView(pView), _iColumn(iColumn), _reverse(reverse)
{}
bool operator()(int i1, int i2) const
{
if (i1 == i2) return false; // equivalence test not equality
if (_reverse) std::swap(i1, i2);
return compare(i1, i2);
}
bool compare(int i1, int i2) const
{
const Buffer& b1 = _pView->getBufferAt(i1);
const Buffer& b2 = _pView->getBufferAt(i2);
if (_iColumn == 0)
{
const char *s1 = PathFindFileName(b1.getFileName());
const char *s2 = PathFindFileName(b2.getFileName());
return _strequiv(s1, s2);
}
else if (_iColumn == 1)
{
char buf1[MAX_PATH];
char buf2[MAX_PATH];
const char *f1 = b1.getFileName();
const char *f2 = b2.getFileName();
const char *s1 = PathFindFileName(b1.getFileName());
const char *s2 = PathFindFileName(b2.getFileName());
int l1 = min((s1 - f1), (_countof(buf1)-1));
int l2 = min((s2 - f2), (_countof(buf2)-1));
strncpy(buf1, f1, l1); buf1[l1] = 0;
strncpy(buf2, f2, l2); buf2[l2] = 0;
return _strequiv(buf1, buf2);
}
else if (_iColumn == 2)
{
int t1 = (int)b1.getLangType();
int t2 = (int)b2.getLangType();
return (t1 < t2); // yeah should be the name
}
return false;
}
};
//////////////////
// Window map tells CWinMgr how to position dialog controls
//
BEGIN_WINDOW_MAP(WindowsDlgMap)
BEGINROWS(WRCT_REST,0,RCMARGINS(8,8))
BEGINCOLS(WRCT_REST,0,0) // Begin list control column
BEGINROWS(WRCT_REST,0,0)
RCREST(IDC_WINDOWS_LIST)
RCSPACE(20)
ENDGROUP()
RCSPACE(12)
BEGINROWS(WRCT_TOFIT,0,0)
RCSPACE(12)
RCTOFIT(IDOK)
RCSPACE(-12)
RCTOFIT(IDC_WINDOWS_SAVE)
RCSPACE(-12)
RCTOFIT(IDC_WINDOWS_CLOSE)
RCSPACE(-12)
RCTOFIT(IDC_WINDOWS_SORT)
RCREST(-1)
RCTOFIT(IDCANCEL)
ENDGROUP()
ENDGROUP()
ENDGROUP()
END_WINDOW_MAP()
RECT WindowsDlg::_lastKnownLocation;
WindowsDlg::WindowsDlg() : MyBaseClass(WindowsDlgMap), _isSorted(false)
{
_szMinButton = SIZEZERO;
_szMinListCtrl = SIZEZERO;
}
void WindowsDlg::init(HINSTANCE hInst, HWND parent, ScintillaEditView *pView)
{
MyBaseClass::init(hInst, parent);
_pView = pView;
}
void WindowsDlg::init(HINSTANCE hInst, HWND parent)
{
assert(!"Call other initialize method");
MyBaseClass::init(hInst, parent);
_pView = NULL;
}
BOOL CALLBACK WindowsDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG :
{
changeDlgLang();
return MyBaseClass::run_dlgProc(message, wParam, lParam);
}
case WM_COMMAND :
{
switch (wParam)
{
case IDOK :
activateCurrent();
return TRUE;
case IDCANCEL :
::GetWindowRect(_hSelf, &_lastKnownLocation);
EndDialog(_hSelf, IDCANCEL);
return TRUE;
case IDC_WINDOWS_SAVE:
doSave();
return TRUE;
case IDC_WINDOWS_CLOSE:
doClose();
return TRUE;
case IDC_WINDOWS_SORT:
doSortToTabs();
_isSorted = false;
updateButtonState();
break;
default :
break;
}
}
case WM_DESTROY :
//destroy();
return TRUE;
case WM_NOTIFY :
{
if (wParam == IDC_WINDOWS_LIST)
{
NMHDR* pNMHDR = (NMHDR*)lParam;
if (pNMHDR->code == LVN_GETDISPINFO)
{
NMLVDISPINFO *pLvdi = (NMLVDISPINFO *)pNMHDR;
//if(pLvdi->item.mask & LVIF_IMAGE)
// ;
if(pLvdi->item.mask & LVIF_TEXT)
{
pLvdi->item.pszText[0] = 0;
size_t index = pLvdi->item.iItem;
if (index >= _pView->getNbDoc() || index >= _idxMap.size())
return FALSE;
index = _idxMap[index];
const Buffer& buffer = _pView->getBufferAt(index);
if (pLvdi->item.iSubItem == 0) // file name
{
int len = pLvdi->item.cchTextMax;
const char *fullName = buffer.getFileName();
strncpy(pLvdi->item.pszText, PathFindFileName(fullName), len-1);
pLvdi->item.pszText[len-1] = 0;
len = strlen(pLvdi->item.pszText);
if (buffer.isDirty())
{
if (len < pLvdi->item.cchTextMax)
{
pLvdi->item.pszText[len++] = '*';
pLvdi->item.pszText[len] = 0;
}
}
else if (buffer.isReadOnly())
{
len += strlen(readonlyString);
if (len <= pLvdi->item.cchTextMax)
strcat(pLvdi->item.pszText, readonlyString);
}
}
else if (pLvdi->item.iSubItem == 1) // directory
{
const char *fullName = buffer.getFileName();
const char *fileName = PathFindFileName(fullName);
int len = fileName-fullName+1;
if (pLvdi->item.cchTextMax < len)
len = pLvdi->item.cchTextMax;
strncpy(pLvdi->item.pszText, fullName, len-1);
pLvdi->item.pszText[len-1] = 0;
}
else if (pLvdi->item.iSubItem == 2) // Type
{
int len = pLvdi->item.cchTextMax;
NppParameters *pNppParameters = NppParameters::getInstance();
Lang *lang = pNppParameters->getLangFromID(buffer.getLangType());
if (NULL != lang)
{
strncpy(pLvdi->item.pszText, lang->getLangName(), len-1);
}
}
}
return TRUE;
}
else if (pNMHDR->code == LVN_COLUMNCLICK) // sort columns with stable sort
{
NMLISTVIEW *pNMLV = (NMLISTVIEW *)pNMHDR;
if (pNMLV->iItem == -1)
{
bool reverse = false;
int iColumn = pNMLV->iSubItem;
if (_lastSort == iColumn)
{
reverse = true;
_lastSort = -1;
}
else
{
_lastSort = iColumn;
}
int i;
int n = _idxMap.size();
vector<int> sortMap;
sortMap.resize(n);
for (i=0; i<n; ++i) sortMap[_idxMap[i]] = ListView_GetItemState(_hList, i, LVIS_SELECTED);
stable_sort(_idxMap.begin(), _idxMap.end(), BufferEquivalent(_pView, iColumn, reverse));
for (i=0; i<n; ++i) ListView_SetItemState(_hList, i, sortMap[_idxMap[i]] ? LVIS_SELECTED : 0, LVIS_SELECTED);
::InvalidateRect(_hList, &_rc, FALSE);
_isSorted = true;
updateButtonState();
}
return TRUE;
}
else if (pNMHDR->code == LVN_ITEMACTIVATE || pNMHDR->code == LVN_ITEMCHANGED || pNMHDR->code == LVN_ODSTATECHANGED)
{
updateButtonState();
return TRUE;
}
else if (pNMHDR->code == NM_DBLCLK)
{
::PostMessage(_hSelf, WM_COMMAND, IDOK, 0);
//activateCurrent();
return TRUE;
}
else if (pNMHDR->code == LVN_KEYDOWN)
{
NMLVKEYDOWN *lvkd = (NMLVKEYDOWN *)pNMHDR;
// Ctrl+A
short ctrl = GetKeyState(VK_CONTROL);
short alt = GetKeyState(VK_MENU);
short shift = GetKeyState(VK_SHIFT);
if (lvkd->wVKey == 0x41/*a*/ && ctrl<0 && alt>=0 && shift>=0)
{
for (int i=0, n=ListView_GetItemCount(_hList); i<n; ++i)
ListView_SetItemState(_hList, i, LVIS_SELECTED, LVIS_SELECTED);
}
return TRUE;
}
}
}
break;
}
return MyBaseClass::run_dlgProc(message, wParam, lParam);
}
void WindowsDlg::updateButtonState()
{
int nSelection = ListView_GetSelectedCount(_hList);
if (nSelection == 0)
{
EnableWindow(GetDlgItem(_hSelf, IDOK), FALSE);
EnableWindow(GetDlgItem(_hSelf, IDC_WINDOWS_SAVE), FALSE);
EnableWindow(GetDlgItem(_hSelf, IDC_WINDOWS_CLOSE), FALSE);
}
else
{
EnableWindow(GetDlgItem(_hSelf, IDC_WINDOWS_SAVE), TRUE);
EnableWindow(GetDlgItem(_hSelf, IDC_WINDOWS_CLOSE), TRUE);
if (nSelection == 1)
EnableWindow(GetDlgItem(_hSelf, IDOK), TRUE);
else
EnableWindow(GetDlgItem(_hSelf, IDOK), FALSE);
}
EnableWindow(GetDlgItem(_hSelf, IDC_WINDOWS_SORT), _isSorted);
}
int WindowsDlg::doDialog(TiXmlNode *dlgNode)
{
_dlgNode = dlgNode;
return ::DialogBoxParam(_hInst, MAKEINTRESOURCE(IDD_WINDOWS), _hParent, (DLGPROC)dlgProc, (LPARAM)this);
};
bool WindowsDlg::changeDlgLang()
{
if (!_dlgNode) return false;
// Set Title
const char *titre = (_dlgNode->ToElement())->Attribute("title");
if (titre && titre[0])
{
::SetWindowText(_hSelf, titre);
}
// Set the text of child control
for (TiXmlNode *childNode = _dlgNode->FirstChildElement("Item");
childNode ;
childNode = childNode->NextSibling("Item") )
{
TiXmlElement *element = childNode->ToElement();
int id;
const char *sentinel = element->Attribute("id", &id);
const char *name = element->Attribute("name");
if (sentinel && (name && name[0]))
{
HWND hItem = ::GetDlgItem(_hSelf, id);
if (hItem)
::SetWindowText(hItem, name);
}
}
return true;
}
BOOL WindowsDlg::onInitDialog()
{
_winMgr.InitToFitSizeFromCurrent(_hSelf);
// save min size for OK/Cancel buttons
_szMinButton = RectToSize(_winMgr.GetRect(IDOK));
_szMinListCtrl = RectToSize(_winMgr.GetRect(IDC_WINDOWS_LIST));
_lastSort = -1;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -