📄 codefinisherlistbox.cpp
字号:
// CodeFinisherListBox.cpp: implementation of the CCodeFinisherListBox class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "visualjava.h"
#include "CodeFinisherListBox.h"
#include "VisualJavaView.h"
#include "MainFrm.h"
#include "VisualJavaDoc.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCodeFinisherListBox::CCodeFinisherListBox(): m_bEnableLastAction (false)
{
m_xMax = 0;m_yMax =0;
}
CCodeFinisherListBox::~CCodeFinisherListBox()
{
}
void CCodeFinisherListBox::AddItem(CString strItem,UINT nType,UINT nMod /*= -1*/)
{
if(FindStringExact(-1,strItem) != LB_ERR)return;
int hItem =0;
if(nType == IS_CLASS)
{
//lvi.iImage = 0;
hItem = AddString(strItem,0);
}
else
if(nType == IS_INTERFACE)
{
}
else
if(nType == IS_PACKAGE)
{
}
else
if(nType == IS_FUNC)
{
if(nMod == -1)
{
//lvi.iImage = 4;
hItem = AddString(strItem,4);
}
if(nMod == IS_PUBLIC)
{
//lvi.iImage = 1;
hItem = AddString(strItem,1);
}
else
if(nMod == IS_PROTECTED)
{
//lvi.iImage = 6;
hItem = AddString(strItem,6);
}
else
{
//lvi.iImage = 4;
hItem = AddString(strItem,4);
}
}
else
if(nType == IS_VAR)
{
if(nMod == -1)
{
//lvi.iImage = 5;
hItem = AddString(strItem,5);
}
if(nMod == IS_PUBLIC)
{
//lvi.iImage = 3;
hItem = AddString(strItem,5);
}
else
if(nMod == IS_PROTECTED)
{
//lvi.iImage = 7;
hItem = AddString(strItem,7);
}
else
{
//lvi.iImage = 5;
hItem = AddString(strItem,5);
}
}
CRect rcItem;
GetItemRect(hItem,&rcItem);
CSize size = GetDC()->GetTextExtent(strItem);
if(GetCount()<11)
m_yMax = rcItem.Height()*GetCount();
int max = size.cx;
if((max>m_xMax)&&(max<150))
m_xMax = max;
}
#define MASK_DWDATA 0x01 // dwItemData is valid
#define MASK_LPDATA 0x02 // pData is valid
#define MASK_NIMAGE 0x04 // nImage is valid
#define MASK_DWFLAGS 0x08 // dwFlags is valid
#define MASK_ALL 0xff // All fields are valid
#define TEST_BIT0 0x00000001
#define CX_1_BORDER 3
#define CY_1_BORDER 2
void CCodeFinisherListBox::DrawItem(LPDRAWITEMSTRUCT pDIStruct)
{
#define CX_BORDER 1
#define CY_BORDER 1
CDC* pDC = CDC::FromHandle(pDIStruct->hDC);
BOOL bIsSelected = FALSE;
BOOL bIsFocused = FALSE;
BOOL bIsDisabled = FALSE;
COLORREF crNormal = GetSysColor(COLOR_WINDOW);
COLORREF crSelected = GetSysColor(COLOR_HIGHLIGHT);
COLORREF crText = GetSysColor(COLOR_WINDOWTEXT);
COLORREF crColor = RGB(0, 0, 0);
CString sText; // List box item text
STRUCT_LBDATA* lpLBData = NULL;
lpLBData = (STRUCT_LBDATA*)CListBox::GetItemDataPtr(pDIStruct->itemID);
if (lpLBData == NULL || lpLBData == (LPVOID)-1L) return;
bIsSelected = (pDIStruct->itemState & ODS_SELECTED);
bIsFocused = (pDIStruct->itemState & ODS_FOCUS);
bIsDisabled = ((pDIStruct->itemState & ODS_DISABLED) || ((lpLBData->dwFlags & TEST_BIT0) == TEST_BIT0));
CRect rcItem = pDIStruct->rcItem;
CRect rcIcon = pDIStruct->rcItem;
CRect rcText = pDIStruct->rcItem;
CRect rcCenteredText = pDIStruct->rcItem;
pDC->SetBkMode(TRANSPARENT);
// ONLY FOR DEBUG
//CBrush brBtnShadow(RGB(255, 0, 0));
//pDC->FrameRect(&rcItem, &brBtnShadow);
// Calculate rcIcon
if (m_pImageList)
{
rcIcon.right = rcIcon.left + m_szImage.cx + CX_BORDER*2;
rcIcon.bottom = rcIcon.top + m_szImage.cy + CY_BORDER*2;
} // if
else rcIcon.SetRect(0, 0, 0, 0);
// Calculate rcText
rcText.left = rcIcon.right;
// Calculate rcCenteredText
// Get list box item text
CListBox::GetText(pDIStruct->itemID, sText);
rcCenteredText = rcText;
pDC->DrawText(sText, -1, rcCenteredText, DT_WORDBREAK | DT_EXPANDTABS| DT_CALCRECT | lpLBData->nFormat);
rcCenteredText.OffsetRect(0, (rcText.Height() - rcCenteredText.Height())/2);
// Draw rcIcon background
if (m_pImageList)
{
if (bIsSelected && (m_byRowSelect == ST_FULLROWSELECT) && !bIsDisabled)
crColor = crSelected;
else
crColor = crNormal;
OnDrawIconBackground(pDIStruct->itemID, pDC, &rcItem, &rcIcon, bIsDisabled, bIsSelected, crColor);
} // if
// Draw rcText/rcCenteredText background
if (bIsDisabled)
{
pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
crColor = crNormal;
} // if
else
{
if (bIsSelected)
{
pDC->SetTextColor(0x00FFFFFF & ~crText);
crColor = crSelected;
} // if
else
{
pDC->SetTextColor(crText);
crColor = crNormal;
} // else
} // else
if (m_byRowSelect == ST_TEXTSELECT)
//pDC->FillSolidRect(&rcCenteredText, crColor);
OnDrawTextBackground(pDIStruct->itemID, pDC, &rcItem, &rcCenteredText, bIsDisabled, bIsSelected, crColor);
else
//pDC->FillSolidRect(&rcText, crColor);
OnDrawTextBackground(pDIStruct->itemID, pDC, &rcItem, &rcText, bIsDisabled, bIsSelected, crColor);
// Draw the icon (if any)
if (m_pImageList)
OnDrawIcon(pDIStruct->itemID, pDC, &rcItem, &rcIcon, lpLBData->nImage, bIsDisabled, bIsSelected);
// Draw text
pDC->DrawText(sText, -1, rcCenteredText, DT_WORDBREAK | DT_EXPANDTABS | lpLBData->nFormat);
// Draw focus rectangle
if (bIsFocused && !bIsDisabled)
{
switch (m_byRowSelect)
{
case ST_FULLROWSELECT:
pDC->DrawFocusRect(&rcItem);
break;
case ST_FULLTEXTSELECT:
pDC->DrawFocusRect(&rcText);
break;
case ST_TEXTSELECT:
default:
pDC->DrawFocusRect(&rcCenteredText);
break;
} // switch
} // if
}
void CCodeFinisherListBox::ReCalcSize(CRect& rcSize)
{
//icon
SetWindowPos(NULL,0,0,m_xMax+16,m_yMax,SWP_NOZORDER);
}
void CCodeFinisherListBox::Reset()
{
ResetContent();
m_xMax = 0;m_yMax =0;
}
BEGIN_MESSAGE_MAP(CCodeFinisherListBox, CListBoxST)
//{{AFX_MSG_MAP(CMyTreeCtrl)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
ON_WM_NCPAINT()
END_MESSAGE_MAP()
void CCodeFinisherListBox::OnNcPaint()
{
CWindowDC dc(this);
CRect rectClient;
GetClientRect(rectClient);
CRect rectWindow;
GetWindowRect(rectWindow);
ScreenToClient(rectWindow);
rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
if(rectClient.top<rectClient.bottom && rectClient.top<rectClient.bottom)
dc.ExcludeClipRect(rectClient);
// erase parts not drawn
SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
// draw borders in non-client area
rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
CRect rcClip;
dc.GetClipBox(&rcClip);
CDC mdc;
mdc.CreateCompatibleDC(&dc);
CBitmap memBmp;
memBmp.CreateCompatibleBitmap(&dc,rectWindow.Width(),rectWindow.Height());
CBitmap* pOldBmp = mdc.SelectObject(&memBmp);
{
CBrush cb;
cb.CreateSolidBrush(RGB(0,0,255));
mdc.FillRect(rectWindow,&cb);
cb.DeleteObject();
//rectWindow.DeflateRect(0,1,0,1);
}
dc.BitBlt (rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(),
&mdc, rcClip.left, rcClip.top, SRCCOPY);
mdc.SelectObject(pOldBmp);
ReleaseDC(&dc);
mdc.DeleteDC();
//GetScrollBarCtrl(SB_VERT)->UpdateWindow();
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
bool CCodeFinisherListBox::XPopupCtrlEvent::OnInit ()
{
METHOD_PROLOGUE_(CCodeFinisherListBox, PopupCtrlEvent);
TRACE ("CCodeFinisherListBox::OnInit ()\n");
pThis->ModifyStyle (0, WS_BORDER, SWP_DRAWFRAME);
return true;
}
///////////////////////////////////////////////////////////////////////////////
void CCodeFinisherListBox::XPopupCtrlEvent::OnShow ()
{
METHOD_PROLOGUE_(CCodeFinisherListBox, PopupCtrlEvent);
TRACE ("CCodeFinisherListBox::OnShow ()\n");
pThis->SetFocus();
}
///////////////////////////////////////////////////////////////////////////////
void CCodeFinisherListBox::XPopupCtrlEvent::OnHide (bool bAbort)
{
METHOD_PROLOGUE_(CCodeFinisherListBox, PopupCtrlEvent);
TRACE ("CCodeFinisherListBox::OnHide (Abort = %s)\n", bAbort ? "true" : "false");
if(!bAbort)
{
InsertText();
}
}
///////////////////////////////////////////////////////////////////////////////
void CCodeFinisherListBox::XPopupCtrlEvent::InsertText()
{
CMainFrame* pFrm = (CMainFrame*)AfxGetMainWnd();
CVisualJavaView* pView = (CVisualJavaView*)(pFrm->GetActiveFrame()->GetActiveView());
if(pView)
{
int sel = pFrm->m_wndCodeCompletionList.GetCurSel();
CString strSel;
pFrm->m_wndCodeCompletionList.GetText(sel,strSel);
pView->InsertText(strSel);
}
}
#define IS_DIGIT(c) (c>47&&c<58)
#define IS_IDENTIFIER_SYM(c) ((IS_DIGIT(c))||(c>64&&c<91)||(c>96&&c<123)||(c=='_'))
/*
#define TERM(c) ((char(c) == '~' )|| (char(c) == '!') || (char(c) == '@') || (char(c) == '#')\
||(char(c) == '$') || (char(c) == '%') || (char(c) == '^') || (char(c) == '&')\
||(char(c) == '*') || (char(c) == '(') || (char(c) == ')') || (char(c) == '-')\
||(char(c) == '=') || (char(c) == '+') || (char(c) == '`') || (char(c) == ';')\
||(char(c) == ':') || (char(c) == '"') || (char(c) == ',') || (char(c) == '<')\
||(char(c) == '.' || char(c) == '>' || char(c) == '/' || char(c) == '?'\
||char(c) == '{' || char(c) == '[' || char(c) == ']' || char(c) == '}'\
||char(c) == '|' || char(c) == '\\' || c == VK_ESCAPE || c == VK_F1\
/*||c == VK_F2 || c == VK_F3 || c == VK_F4 || c == VK_F5 || c == VK_F6\
||c == VK_F7 || c == VK_F8 || c == VK_F9 || c == VK_F10 || c == VK_F11\
||c == VK_F12 || c == VK_RETURN || c == VK_TAB || c == VK_BACK\
||c == VK_SPACE || c == VK_INSERT || c == VK_DELETE || c == VK_PRINT\
||c == VK_MENU)*/
#define TERM(c) ((!IS_IDENTIFIER_SYM(c)) && ( c != VK_DOWN) && ( c != VK_UP ) && ( c != VK_HOME )\
&&( c != VK_END ) && (c != VK_LEFT ) && ( c != VK_RIGHT ) && ( c != VK_CAPITAL ) \
&&( c != VK_NUMLOCK ))
///////////////////////////////////////////////////////////////////////////////
int CCodeFinisherListBox::XPopupCtrlEvent::OnKeyDown (UINT nChar, UINT, UINT)
{
if(TERM(nChar))
{
return CPopup::end;
}
return CPopup::doNothing;
}
///////////////////////////////////////////////////////////////////////////////
int CCodeFinisherListBox::XPopupCtrlEvent::OnLButtonDown (UINT, CPoint pt)
{
METHOD_PROLOGUE_(CCodeFinisherListBox, PopupCtrlEvent);
return CPopup::doNothing;
}
///////////////////////////////////////////////////////////////////////////////
int CCodeFinisherListBox::XPopupCtrlEvent::OnLButtonUp (UINT, CPoint pt)
{
METHOD_PROLOGUE_(CCodeFinisherListBox, PopupCtrlEvent);
return CPopup::doNothing;
}
//////////////////////////////////////////////////////////////////////////////
int CCodeFinisherListBox::XPopupCtrlEvent::OnLButtonDblClk(UINT nFlags,CPoint point)
{
METHOD_PROLOGUE_(CCodeFinisherListBox, PopupCtrlEvent);
return CPopup::end;
}
///////////////////////////////////////////////////////////////////////////////
IPopupCtrlEvent* CCodeFinisherListBox::XPopupCtrlEvent::GetInterfaceOf (HWND)
{
// Not used here because this control doesn't include other controls
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -