?? linenumberedit.cpp
字號(hào):
/* ==========================================================================
CLineNumberEdit
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-03-09
Purpose : CLineNumberEdit is a CEdit-derived class that displays
line numbers to the left of the text.
Description : The class uses the edit rect to make space for the line
numbers. The line numbers are relized through a special
CStatic-derived class, CLineNumberStatic. As soon as the
text is updated, the CLineNumberStatic is updated as
well.
Usage : The control can be dynamically created, or created from
a dialog template. The formatting string for the line
numbers can be set by calling SetLineNumberFormat (the
same format string as for CString::Format). By calling
SetMarginForegroundColor or SetMarginBackgroundColor
the fore- and background colors for the line number
display is set.
========================================================================
Update : Keith Bowes
Date : 2004-04-13
Purpose : 1. To allow CLineNumberEdit to properly change colour when
Enabled/Disabled or when system colours change.
Changing system colours only has a noticable effect when
a scheme such as Marine or Plum is chosen.
2. To allow a line number delta to be applied to the first
line number so the index does not have to start at zero.
3. To allow a max value to be specified to stop the line
count and to allow smarter size formatting.
Description : 1. Added OnEnable and OnSysColorChange to detect when
a colour change is required. This allows the line number
area and CEdit area to update colours properly.
Added colour ref variables to hold enabled/disabled states
of the background/foreground colours.
In an attempt to allow previous functionality to take
precedence, if the colours are changed explicitly, the
system colours are no longer queried.
2. Added m_LineDelta, applied when line numbers are formatted.
3. Using m_maxval when > 0 to limit the max values and when
formatting colomn width.
JRO: Added m_lineDelta as well.
Usage : 1. Default behaviour is to change colours to reflect CEdit.
manually changing the colour will cause the colours to
only change to the specified colours.
2. SetLineNumberRange sets both min and max values.
3. SetLineNumberRange sets both min and max values.
Comments : - Perhaps line values should be stored as UINT's as negative
values may have unexpected results.
- CLineNumberEdit::m_format creates a duplicate of
CLineNumberStatic::m_format, is this needed?
JRO: Even though the the two classes are thightly coupled,
this duplication of data makes it easier to decouple them.
A small matter, but code reuse is Politically Correct,
and as such A Desirable Feature.
- Added options could allow different system colours to be
chosen and updated as system attributes are changed.
- if m_maxval is exceeded in the edit box, new lines
are added without line numbers. This might not be the
desired behaviour.
JRO: I think this is rather nifty, actually. If I, as a
developer, sets the max number of lines to be numbered,
I also expect this to be the case :-)))
- It's not spelled wrong, just differently. ;0)
========================================================================
Update : Johan Rosengren
Date : 2004-04-14
Purpose : 1. Allow deriving of CLineNumberEdit.
Description : 1. Made the message handlers virtual.
Usage : 1. Declare message handlers as virtual in derived
classes. Note that CLineNumberEdit is not built to
be derived from, however.
========================================================================
Update : Keith Bowes
Date : 2004-04-22
Purpose : To allow processing of WM_LINESCROLL messages.
Description : Added OnLineScroll to handle the message.
Usage : Now will call UpdateTopAndBottom if the message is
received.
========================================================================
Update : Johan Rosengren
Date : 2004-05-02
Purpose : Select complete line when a line-number is clicked
Description : Added registered user message, sent when the line-
number static is clicked.
Usage : See urm_SELECTLINE in the code.
========================================================================*/
#include "stdafx.h"
#include "LineNumberEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Registered message to allow selection of complete
// lines by clicking the line number
UINT urm_SELECTLINE = ::RegisterWindowMessage( "_LINE_NUMBER_EDIT_SELECTLINE_" );
/////////////////////////////////////////////////////////////////////////////
// CLineNumberEdit
CLineNumberEdit::CLineNumberEdit()
/* ============================================================
Function : CLineNumberEdit::CLineNumberEdit
Description : constructor
Return : void
Parameters : none
Usage :
============================================================*/
{
m_hWnd = NULL;
m_line.m_hWnd = NULL;
m_zero.cx = 0;
m_zero.cy = 0;
m_format = _T( "%05i" );
m_LineDelta = 0;
// Could default m_maxval to 99,999, but may cause problems
// if m_format is changed and m_maxval is not...
m_maxval = 0;
// Setting up so by defult the original hard-coded colour
// scheme is used when enabled and the system colours are
// used when disabled.
m_bUseEnabledSystemColours = FALSE;
m_bUseDisabledSystemColours = TRUE;
m_EnabledFgCol = RGB( 0, 0, 0 );
m_EnabledBgCol = RGB( 255, 255, 248 );
m_DisabledFgCol = GetSysColor( COLOR_GRAYTEXT );
m_DisabledBgCol = GetSysColor( COLOR_3DFACE );
SetWindowColour();
}
CLineNumberEdit::~CLineNumberEdit()
/* ============================================================
Function : CLineNumberEdit::~CLineNumberEdit
Description : destructor
Return : void
Parameters : none
Usage :
============================================================*/
{
}
BEGIN_MESSAGE_MAP(CLineNumberEdit, CEdit)
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
ON_WM_VSCROLL()
ON_CONTROL_REFLECT(EN_VSCROLL, OnVscroll)
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_WM_SIZE()
ON_MESSAGE(WM_SETTEXT, OnSetText)
ON_WM_SYSCOLORCHANGE()
ON_WM_ENABLE()
ON_MESSAGE(EM_LINESCROLL, OnLineScroll)
ON_REGISTERED_MESSAGE(urm_SELECTLINE, OnSelectLine)
END_MESSAGE_MAP()
void CLineNumberEdit::PreSubclassWindow()
/* ============================================================
Function : CLineNumberEdit::PreSubclassWindow
Description : This function is called before the control
is subclassed for a control on a dialog
template, and during creation for
dynamically created controls.
Return : void
Parameters : none
Usage : Called from MFC
============================================================*/
{
// Unfortunately, we can't change to ES_MULTILINE
// during run-time.
ASSERT( GetStyle() & ES_MULTILINE );
// Creating the line number control
SetLineNumberFormat( m_format );
}
/////////////////////////////////////////////////////////////////////////////
// CLineNumberEdit message handlers
void CLineNumberEdit::OnSysColorChange()
/* ============================================================
Function : CLineNumberEdit::OnSysColorChange
Description : Handles WM_SYSCOLORCHANGE. User has changed
the system colours, want to refresh.
Return : void
Parameters : void
Usage : Called from Windows
============================================================*/
{
CEdit::OnSysColorChange();
// update the CStatic with the new colours
SetWindowColour( IsWindowEnabled() );
}
LRESULT CLineNumberEdit::OnSetText( WPARAM wParam, LPARAM lParam )
/* ============================================================
Function : CLineNumberEdit::OnSetText
Description : Handles WM_SETTEXT. We must update the line
numbers in the line number control as well.
Return : LRESULT - From Def proc
Parameters : WPARAM wParam - From Windows
LPARAM lParam - From Windows
Usage : Called from Windows
============================================================*/
{
// Default processing
LRESULT retval = DefWindowProc( WM_SETTEXT, wParam, lParam );
UpdateTopAndBottom();
return retval;
}
void CLineNumberEdit::OnChange()
/* ============================================================
Function : CLineNumberEdit::OnChange
Description : Mapped to EN_CHANGE. We must handle
EN_CHANGE to let the line-number control
reflect changes to the edit box content.
Return : void
Parameters : none
Usage : Called from Windows
============================================================*/
{
UpdateTopAndBottom();
}
void CLineNumberEdit::OnVscroll()
/* ============================================================
Function : CLineNumberEdit::OnVscroll
Description : Mapped to EN_VSCROLL. We update the line
numbers in the line number control
Return : void
Parameters : none
Usage : Called from Windows
============================================================*/
{
UpdateTopAndBottom();
}
void CLineNumberEdit::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
/* ============================================================
Function : CLineNumberEdit::OnVScroll
Description : Handles WM_VSCROLL. We handle WM_VSCROLL
in addition to the notification EN_VSCROLL,
to handle scrollbar dragging as well
Return : void
Parameters : UINT nSBCode - From Windows
UINT nPos - From Windows
CScrollBar* pScrollBar - From Windows
Usage : Called from Windows
============================================================*/
{
CEdit::OnVScroll( nSBCode, nPos, pScrollBar );
UpdateTopAndBottom();
}
LRESULT CLineNumberEdit::OnLineScroll( WPARAM wParam, LPARAM lParam )
/* ============================================================
Function : CLineNumberEdit::OnLineScroll
Description : Mapped to EM_LINESCROLL. We update the line
numbers in the line number control.
Return : void
Parameters : none
Usage : Called from Windows
============================================================*/
{
// Default processing
LRESULT retval = DefWindowProc( EM_LINESCROLL, wParam, lParam );
UpdateTopAndBottom();
return retval;
}
LRESULT CLineNumberEdit::OnSetFont( WPARAM wParam, LPARAM lParam )
/* ============================================================
Function : CLineNumberEdit::OnSetFont
Description : Mapped to WM_SETFONT. We must recalculate
the line number control size as well.
Return : LRESULT - Always 0
Parameters : WPARAM wParam - From Windows
LPARAM lParam - From Windows
Usage : Called from Windows
============================================================*/
{
DefWindowProc( WM_SETFONT, wParam, lParam );
// We resize the line-number
// field
Prepare();
return 0;
}
void CLineNumberEdit::OnSize( UINT nType, int cx, int cy )
/* ============================================================
Function : CLineNumberEdit::OnSize
Description : Handles WM_SIZE. Recalculates the line
number control size as well.
Return : void
Parameters : UINT nType - From Windows
int cx - From Windows
int cy - From Windows
Usage : Called from Windows
============================================================*/
{
CEdit::OnSize( nType, cx, cy );
// If we have the line-number
// control, it must be resized
// as well.
if( m_line.m_hWnd )
Prepare();
}
void CLineNumberEdit::OnEnable( BOOL bEnable )
/* ============================================================
Function : CLineNumberEdit::OnEnable
Description : Handles WM_ENABLE. Calls to set colours.
Return : void
Parameters : BOOL bEnable - From Windows
Usage : Called from Windows.
============================================================*/
{
CEdit::OnEnable( bEnable );
SetWindowColour( bEnable );
}
LRESULT CLineNumberEdit::OnSelectLine(WPARAM wParam, LPARAM /*lParam*/ )
/* ============================================================
Function : CLineNumberEdit::OnSelectLine
Description : Handler for the urm_SELECTLINE registered
message. Will select the line in wParam.
Return : LRESULT - Not used
Parameters : WPARAM wParam - The line to select
LPARAM lParam - Not used
Usage : Called from MFC. Use
SendMessage( urm_SELECTLINE, line ) from
code.
============================================================*/
{
// Calc start and end position of the line
int lineno = wParam + GetScrollPos( SB_VERT );
int start = LineIndex( lineno );
int end = LineIndex( lineno + 1 );
SetSel( start, end - 1 );
return 0;
}
void CLineNumberEdit::SetWindowColour( BOOL bEnable /*= TRUE*/ )
/* ============================================================
Function : CLineNumberEdit::SetWindowColour
Description : Handles changing window colours.
Return : void
Parameters : BOOL bEnable - flag if set enabled/disabled
colours
Usage : Called to change colours in the edit box.
============================================================*/
{
if (m_bUseEnabledSystemColours)
{
// re-query the system colours in case they have changed.
m_EnabledFgCol = GetSysColor( COLOR_WINDOWTEXT );
m_EnabledBgCol = GetSysColor( COLOR_WINDOW );
}
if (m_bUseDisabledSystemColours)
{
// re-query the system colours in case they have changed.
m_DisabledFgCol = GetSysColor( COLOR_GRAYTEXT );
m_DisabledBgCol = GetSysColor( COLOR_3DFACE );
}
// change the colour based on bEnable
if (bEnable)
{
m_line.SetFgColor( m_EnabledFgCol, TRUE );
m_line.SetBgColor( m_EnabledBgCol, TRUE );
} else {
m_line.SetFgColor( m_DisabledFgCol, TRUE );
m_line.SetBgColor( m_DisabledBgCol, TRUE );
}
}
void CLineNumberEdit::UseSystemColours( BOOL bUseEnabled /*= TRUE*/, BOOL bUseDisabled /*= TRUE*/ )
/* ============================================================
Function : CLineNumberEdit::UseSystemColours
Description : Sets the Use*SystemColours flags.
Return : void
Parameters : BOOL bEnabled - flag if to use enabled
system colours
BOOL bDisabled - flag if to use disabled
system colours
Usage : Called to change colours in the edit box
============================================================*/
{
m_bUseEnabledSystemColours = bUseEnabled;
m_bUseDisabledSystemColours = bUseDisabled;
BOOL bEnable = TRUE;
if (::IsWindow(m_hWnd))
bEnable = IsWindowEnabled();
SetWindowColour( bEnable );
}
/////////////////////////////////////////////////////////////////////////////
// CLineNumberEdit private implementation
void CLineNumberEdit::Prepare()
/* ============================================================
Function : CLineNumberEdit::Prepare
Description : Setting the edit rect for the control and
either create or move the line number
control. Also sets the top- and bottom
line numbers.
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -