00001 /* ========================================================================== 00002 CLineNumberEdit 00003 Author : Johan Rosengren, Abstrakt Mekanik AB 00004 Date : 2004-03-09 00005 Purpose : CLineNumberEdit is a CEdit-derived class that displays 00006 line numbers to the left of the text. 00007 Description : The class uses the edit rect to make space for the line 00008 numbers. The line numbers are relized through a special 00009 CStatic-derived class, CLineNumberStatic. As soon as the 00010 text is updated, the CLineNumberStatic is updated as 00011 well. 00012 Usage : The control can be dynamically created, or created from 00013 a dialog template. The formatting string for the line 00014 numbers can be set by calling SetLineNumberFormat (the 00015 same format string as for CString::Format). By calling 00016 SetMarginForegroundColor or SetMarginBackgroundColor 00017 the fore- and background colors for the line number 00018 display is set. 00019 ======================================================================== 00020 Update : Keith Bowes 00021 Date : 2004-04-13 00022 Purpose : 1. To allow CLineNumberEdit to properly change colour when 00023 Enabled/Disabled or when system colours change. 00024 Changing system colours only has a noticable effect when 00025 a scheme such as Marine or Plum is chosen. 00026 2. To allow a line number delta to be applied to the first 00027 line number so the index does not have to start at zero. 00028 3. To allow a max value to be specified to stop the line 00029 count and to allow smarter size formatting. 00030 Description : 1. Added OnEnable and OnSysColorChange to detect when 00031 a colour change is required. This allows the line number 00032 area and CEdit area to update colours properly. 00033 Added colour ref variables to hold enabled/disabled states 00034 of the background/foreground colours. 00035 In an attempt to allow previous functionality to take 00036 precedence, if the colours are changed explicitly, the 00037 system colours are no longer queried. 00038 2. Added m_LineDelta, applied when line numbers are formatted. 00039 3. Using m_maxval when > 0 to limit the max values and when 00040 formatting colomn width. 00041 JRO: Added m_lineDelta as well. 00042 Usage : 1. Default behaviour is to change colours to reflect CEdit. 00043 manually changing the colour will cause the colours to 00044 only change to the specified colours. 00045 2. SetLineNumberRange sets both min and max values. 00046 3. SetLineNumberRange sets both min and max values. 00047 00048 Comments : - Perhaps line values should be stored as UINT's as negative 00049 values may have unexpected results. 00050 - CLineNumberEdit::m_format creates a duplicate of 00051 CLineNumberStatic::m_format, is this needed? 00052 JRO: Even though the the two classes are thightly coupled, 00053 this duplication of data makes it easier to decouple them. 00054 A small matter, but code reuse is Politically Correct, 00055 and as such A Desirable Feature. 00056 - Added options could allow different system colours to be 00057 chosen and updated as system attributes are changed. 00058 - if m_maxval is exceeded in the edit box, new lines 00059 are added without line numbers. This might not be the 00060 desired behaviour. 00061 JRO: I think this is rather nifty, actually. If I, as a 00062 developer, sets the max number of lines to be numbered, 00063 I also expect this to be the case :-))) 00064 - It's not spelled wrong, just differently. ;0) 00065 ======================================================================== 00066 Update : Johan Rosengren 00067 Date : 2004-04-14 00068 Purpose : 1. Allow deriving of CLineNumberEdit. 00069 Description : 1. Made the message handlers virtual. 00070 Usage : 1. Declare message handlers as virtual in derived 00071 classes. Note that CLineNumberEdit is not built to 00072 be derived from, however. 00073 ======================================================================== 00074 Update : Keith Bowes 00075 Date : 2004-04-22 00076 Purpose : To allow processing of WM_LINESCROLL messages. 00077 Description : Added OnLineScroll to handle the message. 00078 Usage : Now will call UpdateTopAndBottom if the message is 00079 received. 00080 ======================================================================== 00081 Update : Johan Rosengren 00082 Date : 2004-05-02 00083 Purpose : Select complete line when a line-number is clicked 00084 Description : Added registered user message, sent when the line- 00085 number static is clicked. 00086 Usage : See urm_SELECTLINE in the code. 00087 ========================================================================*/ 00088 00089 #include "stdafx.h" 00090 #include "LineNumberEdit.h" 00091 00092 #ifdef _DEBUG 00093 #define new DEBUG_NEW 00094 #undef THIS_FILE 00095 static char THIS_FILE[] = __FILE__; 00096 #endif 00097 00098 // Registered message to allow selection of complete 00099 // lines by clicking the line number 00100 UINT urm_SELECTLINE = ::RegisterWindowMessage( _T("_LINE_NUMBER_EDIT_SELECTLINE_") ); 00101 00103 // CLineNumberEdit 00104 CLineNumberEdit::CLineNumberEdit() 00105 /* ============================================================ 00106 Function : CLineNumberEdit::CLineNumberEdit 00107 Description : constructor 00108 00109 Return : void 00110 Parameters : none 00111 00112 Usage : 00113 00114 ============================================================*/ 00115 { 00116 00117 m_hWnd = NULL; 00118 m_line.m_hWnd = NULL; 00119 m_zero.cx = 0; 00120 m_zero.cy = 0; 00121 m_format = _T( "%03i" ); 00122 m_LineDelta = 1; 00123 00124 // Could default m_maxval to 99,999, but may cause problems 00125 // if m_format is changed and m_maxval is not... 00126 m_maxval = 998; 00127 00128 // Setting up so by defult the original hard-coded colour 00129 // scheme is used when enabled and the system colours are 00130 // used when disabled. 00131 m_bUseEnabledSystemColours = FALSE; 00132 m_bUseDisabledSystemColours = TRUE; 00133 m_EnabledFgCol = RGB( 0, 0, 0 ); 00134 m_EnabledBgCol = RGB( 200, 200, 200 ); 00135 m_DisabledFgCol = GetSysColor( COLOR_GRAYTEXT ); 00136 m_DisabledBgCol = GetSysColor( COLOR_3DFACE ); 00137 00138 SetWindowColour(); 00139 00140 } 00141 00142 CLineNumberEdit::~CLineNumberEdit() 00143 /* ============================================================ 00144 Function : CLineNumberEdit::~CLineNumberEdit 00145 Description : destructor 00146 00147 Return : void 00148 Parameters : none 00149 00150 Usage : 00151 00152 ============================================================*/ 00153 { 00154 } 00155 00156 BEGIN_MESSAGE_MAP(CLineNumberEdit, CEdit) 00157 ON_CONTROL_REFLECT(EN_CHANGE, OnChange) 00158 ON_WM_VSCROLL() 00159 ON_CONTROL_REFLECT(EN_VSCROLL, OnVscroll) 00160 ON_MESSAGE(WM_SETFONT, OnSetFont) 00161 ON_WM_SIZE() 00162 ON_MESSAGE(WM_SETTEXT, OnSetText) 00163 ON_WM_SYSCOLORCHANGE() 00164 ON_WM_ENABLE() 00165 ON_MESSAGE(EM_LINESCROLL, OnLineScroll) 00166 ON_REGISTERED_MESSAGE(urm_SELECTLINE, OnSelectLine) 00167 END_MESSAGE_MAP() 00168 00169 void CLineNumberEdit::PreSubclassWindow() 00170 /* ============================================================ 00171 Function : CLineNumberEdit::PreSubclassWindow 00172 Description : This function is called before the control 00173 is subclassed for a control on a dialog 00174 template, and during creation for 00175 dynamically created controls. 00176 00177 Return : void 00178 Parameters : none 00179 00180 Usage : Called from MFC 00181 00182 ============================================================*/ 00183 { 00184 00185 // Unfortunately, we can't change to ES_MULTILINE 00186 // during run-time. 00187 ASSERT( GetStyle() & ES_MULTILINE ); 00188 00189 // Creating the line number control 00190 SetLineNumberFormat( m_format ); 00191 00192 } 00193 00195 // CLineNumberEdit message handlers 00196 00197 void CLineNumberEdit::OnSysColorChange() 00198 /* ============================================================ 00199 Function : CLineNumberEdit::OnSysColorChange 00200 Description : Handles WM_SYSCOLORCHANGE. User has changed 00201 the system colours, want to refresh. 00202 00203 Return : void 00204 Parameters : void 00205 00206 Usage : Called from Windows 00207 00208 ============================================================*/ 00209 { 00210 00211 CEdit::OnSysColorChange(); 00212 00213 // update the CStatic with the new colours 00214 SetWindowColour( IsWindowEnabled() ); 00215 00216 } 00217 00218 LRESULT CLineNumberEdit::OnSetText( WPARAM wParam, LPARAM lParam ) 00219 /* ============================================================ 00220 Function : CLineNumberEdit::OnSetText 00221 Description : Handles WM_SETTEXT. We must update the line 00222 numbers in the line number control as well. 00223 00224 Return : LRESULT - From Def proc 00225 Parameters : WPARAM wParam - From Windows 00226 LPARAM lParam - From Windows 00227 00228 Usage : Called from Windows 00229 00230 ============================================================*/ 00231 { 00232 00233 // Default processing 00234 LRESULT retval = DefWindowProc( WM_SETTEXT, wParam, lParam ); 00235 UpdateTopAndBottom(); 00236 return retval; 00237 00238 } 00239 00240 void CLineNumberEdit::OnChange() 00241 /* ============================================================ 00242 Function : CLineNumberEdit::OnChange 00243 Description : Mapped to EN_CHANGE. We must handle 00244 EN_CHANGE to let the line-number control 00245 reflect changes to the edit box content. 00246 00247 Return : void 00248 Parameters : none 00249 00250 Usage : Called from Windows 00251 00252 ============================================================*/ 00253 { 00254 00255 UpdateTopAndBottom(); 00256 00257 } 00258 00259 void CLineNumberEdit::OnVscroll() 00260 /* ============================================================ 00261 Function : CLineNumberEdit::OnVscroll 00262 Description : Mapped to EN_VSCROLL. We update the line 00263 numbers in the line number control 00264 00265 Return : void 00266 Parameters : none 00267 00268 Usage : Called from Windows 00269 00270 ============================================================*/ 00271 { 00272 00273 UpdateTopAndBottom(); 00274 00275 } 00276 00277 void CLineNumberEdit::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar ) 00278 /* ============================================================ 00279 Function : CLineNumberEdit::OnVScroll 00280 Description : Handles WM_VSCROLL. We handle WM_VSCROLL 00281 in addition to the notification EN_VSCROLL, 00282 to handle scrollbar dragging as well 00283 00284 Return : void 00285 Parameters : UINT nSBCode - From Windows 00286 UINT nPos - From Windows 00287 CScrollBar* pScrollBar - From Windows 00288 00289 Usage : Called from Windows 00290 00291 ============================================================*/ 00292 { 00293 00294 CEdit::OnVScroll( nSBCode, nPos, pScrollBar ); 00295 UpdateTopAndBottom(); 00296 00297 } 00298 00299 LRESULT CLineNumberEdit::OnLineScroll( WPARAM wParam, LPARAM lParam ) 00300 /* ============================================================ 00301 Function : CLineNumberEdit::OnLineScroll 00302 Description : Mapped to EM_LINESCROLL. We update the line 00303 numbers in the line number control. 00304 00305 Return : void 00306 Parameters : none 00307 Usage : Called from Windows 00308 ============================================================*/ 00309 { 00310 00311 // Default processing 00312 LRESULT retval = DefWindowProc( EM_LINESCROLL, wParam, lParam ); 00313 UpdateTopAndBottom(); 00314 return retval; 00315 00316 } 00317 00318 LRESULT CLineNumberEdit::OnSetFont( WPARAM wParam, LPARAM lParam ) 00319 /* ============================================================ 00320 Function : CLineNumberEdit::OnSetFont 00321 Description : Mapped to WM_SETFONT. We must recalculate 00322 the line number control size as well. 00323 00324 Return : LRESULT - Always 0 00325 Parameters : WPARAM wParam - From Windows 00326 LPARAM lParam - From Windows 00327 00328 Usage : Called from Windows 00329 00330 ============================================================*/ 00331 { 00332 00333 DefWindowProc( WM_SETFONT, wParam, lParam ); 00334 // We resize the line-number 00335 // field 00336 Prepare(); 00337 return 0; 00338 00339 } 00340 00341 void CLineNumberEdit::OnSize( UINT nType, int cx, int cy ) 00342 /* ============================================================ 00343 Function : CLineNumberEdit::OnSize 00344 Description : Handles WM_SIZE. Recalculates the line 00345 number control size as well. 00346 00347 Return : void 00348 Parameters : UINT nType - From Windows 00349 int cx - From Windows 00350 int cy - From Windows 00351 00352 Usage : Called from Windows 00353 00354 ============================================================*/ 00355 { 00356 00357 CEdit::OnSize( nType, cx, cy ); 00358 00359 // If we have the line-number 00360 // control, it must be resized 00361 // as well. 00362 if( m_line.m_hWnd ) 00363 Prepare(); 00364 00365 } 00366 00367 void CLineNumberEdit::OnEnable( BOOL bEnable ) 00368 /* ============================================================ 00369 Function : CLineNumberEdit::OnEnable 00370 Description : Handles WM_ENABLE. Calls to set colours. 00371 00372 Return : void 00373 Parameters : BOOL bEnable - From Windows 00374 00375 Usage : Called from Windows. 00376 00377 ============================================================*/ 00378 { 00379 00380 CEdit::OnEnable( bEnable ); 00381 SetWindowColour( bEnable ); 00382 00383 } 00384 00385 LRESULT CLineNumberEdit::OnSelectLine(WPARAM wParam, LPARAM /*lParam*/ ) 00386 /* ============================================================ 00387 Function : CLineNumberEdit::OnSelectLine 00388 Description : Handler for the urm_SELECTLINE registered 00389 message. Will select the line in wParam. 00390 00391 Return : LRESULT - Not used 00392 Parameters : WPARAM wParam - The line to select 00393 LPARAM lParam - Not used 00394 00395 Usage : Called from MFC. Use 00396 SendMessage( urm_SELECTLINE, line ) from 00397 code. 00398 00399 ============================================================*/ 00400 { 00401 00402 // Calc start and end position of the line 00403 int lineno = wParam + GetScrollPos( SB_VERT ); 00404 int start = LineIndex( lineno ); 00405 int end = LineIndex( lineno + 1 ); 00406 SetSel( start, end - 1 ); 00407 return 0; 00408 00409 } 00410 00411 void CLineNumberEdit::SetWindowColour( BOOL bEnable /*= TRUE*/ ) 00412 /* ============================================================ 00413 Function : CLineNumberEdit::SetWindowColour 00414 Description : Handles changing window colours. 00415 00416 Return : void 00417 Parameters : BOOL bEnable - flag if set enabled/disabled 00418 colours 00419 00420 Usage : Called to change colours in the edit box. 00421 00422 ============================================================*/ 00423 { 00424 00425 if (m_bUseEnabledSystemColours) 00426 { 00427 // re-query the system colours in case they have changed. 00428 m_EnabledFgCol = GetSysColor( COLOR_WINDOWTEXT ); 00429 m_EnabledBgCol = GetSysColor( COLOR_WINDOW ); 00430 } 00431 00432 if (m_bUseDisabledSystemColours) 00433 { 00434 // re-query the system colours in case they have changed. 00435 m_DisabledFgCol = GetSysColor( COLOR_GRAYTEXT ); 00436 m_DisabledBgCol = GetSysColor( COLOR_3DFACE ); 00437 } 00438 00439 // change the colour based on bEnable 00440 if (bEnable) 00441 { 00442 m_line.SetFgColor( m_EnabledFgCol, TRUE ); 00443 m_line.SetBgColor( m_EnabledBgCol, TRUE ); 00444 } else { 00445 m_line.SetFgColor( m_DisabledFgCol, TRUE ); 00446 m_line.SetBgColor( m_DisabledBgCol, TRUE ); 00447 } 00448 00449 } 00450 00451 void CLineNumberEdit::UseSystemColours( BOOL bUseEnabled /*= TRUE*/, BOOL bUseDisabled /*= TRUE*/ ) 00452 /* ============================================================ 00453 Function : CLineNumberEdit::UseSystemColours 00454 Description : Sets the Use*SystemColours flags. 00455 00456 Return : void 00457 Parameters : BOOL bEnabled - flag if to use enabled 00458 system colours 00459 BOOL bDisabled - flag if to use disabled 00460 system colours 00461 00462 Usage : Called to change colours in the edit box 00463 00464 ============================================================*/ 00465 { 00466 00467 m_bUseEnabledSystemColours = bUseEnabled; 00468 m_bUseDisabledSystemColours = bUseDisabled; 00469 BOOL bEnable = TRUE; 00470 if (::IsWindow(m_hWnd)) 00471 bEnable = IsWindowEnabled(); 00472 00473 SetWindowColour( bEnable ); 00474 00475 } 00476 00478 // CLineNumberEdit private implementation 00479 00480 void CLineNumberEdit::Prepare() 00481 /* ============================================================ 00482 Function : CLineNumberEdit::Prepare 00483 Description : Setting the edit rect for the control and 00484 either create or move the line number 00485 control. Also sets the top- and bottom 00486 line numbers. 00487 00488 Return : void 00489 Parameters : none 00490 00491 Usage : Must be called to (re)establish the edit 00492 rect, must also be called as soon as the 00493 control changes size. 00494 00495 ============================================================*/ 00496 { 00497 00498 // Calc sizes 00499 int width = CalcLineNumberWidth(); 00500 CRect rect; 00501 GetClientRect( &rect ); 00502 CRect rectEdit( rect ); 00503 rect.right = width; 00504 rectEdit.left = rect.right + 3; 00505 00506 // Setting the edit rect and 00507 // creating or moving child control 00508 SetRect( &rectEdit ); 00509 if( m_line.m_hWnd ) 00510 m_line.MoveWindow( 0, 0, width, rect.Height() ); 00511 else 00512 m_line.Create(NULL,WS_CHILD | WS_VISIBLE | SS_NOTIFY, rect, this, 1 ); 00513 00514 GetRect( &rectEdit ); 00515 00516 // Update line number control data 00517 m_line.SetTopMargin( rectEdit.top ); 00518 UpdateTopAndBottom(); 00519 00520 } 00521 00522 int CLineNumberEdit::CalcLineNumberWidth() 00523 /* ============================================================ 00524 Function : CLineNumberEdit::CalcLineNumberWidth 00525 Description : Calculates the desired width of the line 00526 number control, using the current format 00527 string and the max number of chars allowed 00528 (pessimistic - assumes one character per 00529 line). 00530 00531 Return : int - The width in pixels 00532 Parameters : none 00533 00534 Usage : Called as soon as the format string is 00535 changed. 00536 00537 ============================================================*/ 00538 { 00539 00540 CClientDC dc( this ); 00541 00542 // If a new font is set during runtime, 00543 // we must explicitly select the font into 00544 // the CClientDC to measure it. 00545 CFont* font = GetFont(); 00546 CFont* oldFont = dc.SelectObject( font ); 00547 00548 m_zero=dc.GetTextExtent( _T( "0" ) ); 00549 CString format; 00550 00551 // GetLimitText returns the number of bytes the edit box may contain, 00552 // not the max number of lines... 00553 //... which is the max number of lines, given one character per d:o :-) 00554 int maxval = GetLimitText(); 00555 if (m_maxval > 0) 00556 maxval = m_maxval + m_LineDelta; 00557 00558 format.Format( m_format, maxval ); 00559 CSize fmt = dc.GetTextExtent( format ); 00560 dc.SelectObject( oldFont ); 00561 00562 // Calculate the size of the line- 00563 // number field. We add a 5 pixel margin 00564 // to the max size of the format string 00565 return fmt.cx + 5; 00566 00567 } 00568 00569 void CLineNumberEdit::UpdateTopAndBottom() 00570 /* ============================================================ 00571 Function : CLineNumberEdit::UpdateTopAndBottom 00572 Description : Updates the top- and bottom line number 00573 for the line number control. 00574 00575 Return : void 00576 Parameters : none 00577 Usage : Should be called as soon as the contents of 00578 the control is changed. 00579 00580 ============================================================*/ 00581 { 00582 00583 CRect rect; 00584 GetClientRect( &rect ); 00585 int maxline = GetLineCount() + m_LineDelta; 00586 00587 // Height for individual lines 00588 int lineheight = m_zero.cy; 00589 00590 // Calculate the number of lines to draw 00591 int topline = GetFirstVisibleLine() + m_LineDelta; 00592 if( ( topline + ( rect.Height() / lineheight ) ) < maxline ) 00593 maxline = topline + ( rect.Height() / lineheight ); 00594 00595 if ( m_maxval > 0 && maxline > m_maxval + m_LineDelta ) 00596 maxline = m_maxval + m_LineDelta; 00597 00598 m_line.SetTopAndBottom( topline, maxline ); 00599 00600 } 00601 00603 // CLineNumberEdit public implementation 00604 00605 void CLineNumberEdit::SetMarginForegroundColor( COLORREF col, BOOL redraw, BOOL bEnabled /*= TRUE*/ ) 00606 /* ============================================================ 00607 Function : CLineNumberEdit::SetMarginForegroundColor 00608 Description : Sets the text color for the number 00609 margin. 00610 00611 Return : void 00612 Parameters : COLORREF col - The new text color 00613 BOOL redraw - TRUE if the control 00614 should be redrawn 00615 (default) 00616 00617 Usage : Call to set a new text color for the line 00618 number margin. The control will be redrawn 00619 if it exists. 00620 00621 ============================================================*/ 00622 { 00623 00624 m_line.SetFgColor( col, redraw ); 00625 if (bEnabled) 00626 { 00627 m_bUseEnabledSystemColours = FALSE; 00628 m_EnabledFgCol = col; 00629 } else { 00630 m_bUseDisabledSystemColours = FALSE; 00631 m_DisabledFgCol = col; 00632 } 00633 00634 } 00635 00636 void CLineNumberEdit::SetMarginBackgroundColor( COLORREF col, BOOL redraw, BOOL bEnabled /*= TRUE*/ ) 00637 /* ============================================================ 00638 Function : CLineNumberEdit::SetMarginBackgroundColor 00639 Description : Sets the background color for the number 00640 margin. 00641 00642 Return : void 00643 Parameters : COLORREF col - The new background color 00644 BOOL redraw - TRUE if the control 00645 should be redrawn 00646 (default) 00647 00648 Usage : Call to set a new background color for the 00649 line number margin. The control will be 00650 redrawn if it exists. 00651 00652 ============================================================*/ 00653 { 00654 00655 m_line.SetBgColor( col, redraw ); 00656 if (bEnabled) 00657 { 00658 m_bUseEnabledSystemColours = FALSE; 00659 m_EnabledBgCol = col; 00660 } else { 00661 m_bUseDisabledSystemColours = FALSE; 00662 m_DisabledBgCol = col; 00663 } 00664 00665 } 00666 00667 void CLineNumberEdit::SetLineNumberFormat( CString format ) 00668 /* ============================================================ 00669 Function : CLineNumberEdit::SetLineNumberFormat 00670 Description : Changes the way line numbers are presented 00671 on screen. 00672 00673 Return : void 00674 Parameters : CString format - The new format string 00675 00676 Usage : Call with a format string using the same 00677 format as CString::Format. It should contain 00678 one and only one numeric type. 00679 00680 ============================================================*/ 00681 { 00682 00683 m_format = format; 00684 m_line.SetLineNumberFormat( format ); 00685 if( m_hWnd ) 00686 Prepare(); 00687 00688 } 00689 00690 void CLineNumberEdit::SetLineNumberRange( UINT nMin, UINT nMax /*= 0*/ ) 00691 /* ============================================================ 00692 Function : CLineNumberEdit::SetLineNumberRange 00693 Description : Changes the default min and max line numbers. 00694 00695 Return : void 00696 Parameters : int nMin - changes the line offset 00697 int nMax - changes the max line number 00698 00699 Usage : Call to set up the min and max line numbers. 00700 00701 ============================================================*/ 00702 { 00703 00704 m_LineDelta = ( int ) nMin; 00705 m_maxval = ( int ) nMax; 00706 00707 } 00708 00710 // CLineNumberStatic 00711 00712 CLineNumberStatic::CLineNumberStatic() 00713 /* ============================================================ 00714 Function : CLineNumberStatic::CLineNumberStatic 00715 Description : constructor 00716 00717 Return : void 00718 Parameters : none 00719 00720 Usage : 00721 00722 ============================================================*/ 00723 { 00724 00725 m_bgcol = RGB( 255, 255, 248 ); 00726 m_fgcol = RGB( 0, 0, 0 ); 00727 m_format = _T( "%05i" ); 00728 m_topline = 0; 00729 m_bottomline = 0; 00730 } 00731 00732 CLineNumberStatic::~CLineNumberStatic() 00733 /* ============================================================ 00734 Function : CLineNumberStatic::~CLineNumberStatic 00735 Description : destructor 00736 00737 Return : void 00738 Parameters : none 00739 00740 Usage : 00741 00742 ============================================================*/ 00743 { 00744 } 00745 00746 BEGIN_MESSAGE_MAP(CLineNumberStatic, CStatic) 00747 ON_WM_PAINT() 00748 ON_WM_ERASEBKGND() 00749 ON_WM_LBUTTONDOWN() 00750 END_MESSAGE_MAP() 00751 00753 // CLineNumberStatic message handlers 00754 00755 void CLineNumberStatic::OnPaint() 00756 /* ============================================================ 00757 Function : CLineNumberStatic::OnPaint 00758 Description : Handler for WM_PAINT. 00759 00760 Return : void 00761 Parameters : none 00762 00763 Usage : Called from Windows. 00764 00765 ============================================================*/ 00766 { 00767 00768 CPaintDC dcPaint( this ); 00769 00770 CRect rect; 00771 GetClientRect( &rect ); 00772 00773 // We double buffer the drawing - 00774 // preparing the memory CDC 00775 CDC dc; 00776 dc.CreateCompatibleDC( &dcPaint ); 00777 int saved = dc.SaveDC(); 00778 00779 // Create GDI and select objects 00780 CBitmap bmp; 00781 CPen pen; 00782 bmp.CreateCompatibleBitmap( &dcPaint, rect.Width(), rect.Height() ); 00783 pen.CreatePen( PS_SOLID, 1, m_fgcol ); 00784 dc.SelectObject( &bmp ); 00785 dc.SelectObject( &pen ); 00786 00787 // Painting the background 00788 dc.FillSolidRect( &rect, m_bgcol ); 00789 dc.MoveTo( rect.right - 1, 0 ); 00790 dc.LineTo( rect.right - 1, rect.bottom ); 00791 00792 // Setting other attributes 00793 dc.SetTextColor( m_fgcol ); 00794 dc.SetBkColor( m_bgcol ); 00795 dc.SelectObject( GetParent()->GetFont() ); 00796 00797 // Output the line numbers 00798 if( m_bottomline ) 00799 { 00800 int lineheight = dc.GetTextExtent( _T( "0" ) ).cy; 00801 for( int t = m_topline ; t < m_bottomline ; t++ ) 00802 { 00803 CString output; 00804 output.Format( m_format, t ); 00805 int topposition = m_topmargin + lineheight * ( t - m_topline ); 00806 dc.TextOut( 2, topposition, output ); 00807 } 00808 } 00809 00810 dcPaint.BitBlt( 0, 0, rect. right, rect.bottom, &dc, 0, 0, SRCCOPY ); 00811 dc.RestoreDC( saved ); 00812 00813 } 00814 00815 BOOL CLineNumberStatic::OnEraseBkgnd( CDC* ) 00816 /* ============================================================ 00817 Function : CLineNumberStatic::OnEraseBkgnd 00818 Description : Mapped to WM_ERASEBKGND. Handled to avoid 00819 flicker, as we redraw the complete control 00820 in OnPaint 00821 00822 Return : BOOL - Always TRUE 00823 Parameters : CDC* - From Windows 00824 00825 Usage : Called from Windows. 00826 00827 ============================================================*/ 00828 { 00829 00830 return TRUE; 00831 00832 } 00833 00834 void CLineNumberStatic::OnLButtonDown( UINT nFlags, CPoint point ) 00835 /* ============================================================ 00836 Function : CLineNumberStatic::OnLButtonDown 00837 Description : Called when the control is clicked. Will 00838 send the urm_SELECTLINE registered message 00839 to the parent to select the line clicked on. 00840 00841 Return : void 00842 Parameters : UINT nFlags - Not used 00843 CPoint point - Position of cursor 00844 00845 Usage : Called from Windows. 00846 00847 ============================================================*/ 00848 { 00849 00850 // Find the line clicked on 00851 CClientDC dc( this ); 00852 dc.SelectObject( GetParent()->GetFont() ); 00853 int lineheight = dc.GetTextExtent( _T( "0" ) ).cy; 00854 int lineno = ( int ) ( ( double ) point.y / ( double ) lineheight ); 00855 00856 // Select this line in the edit control 00857 GetParent()->SendMessage( urm_SELECTLINE, lineno ); 00858 00859 CStatic::OnLButtonDown( nFlags, point ); 00860 00861 } 00862 00864 // CLineNumberStatic public implementation 00865 00866 void CLineNumberStatic::SetBgColor( COLORREF col, BOOL redraw ) 00867 /* ============================================================ 00868 Function : CLineNumberStatic::SetBgColor 00869 Description : This function sets the panel background 00870 color 00871 00872 Return : void 00873 Parameters : COLORREF col - New background color 00874 BOOL redraw - TRUE if the control 00875 should be redrawn 00876 (default) 00877 00878 Usage : Called from the parent. 00879 00880 ============================================================*/ 00881 { 00882 00883 m_bgcol = col; 00884 if( m_hWnd && redraw ) 00885 RedrawWindow(); 00886 00887 } 00888 00889 void CLineNumberStatic::SetFgColor( COLORREF col, BOOL redraw ) 00890 /* ============================================================ 00891 Function : CLineNumberStatic::SetFgColor 00892 Description : This function sets the panel foreground 00893 color 00894 00895 Return : void 00896 Parameters : COLORREF col - New text color 00897 BOOL redraw - TRUE if the control 00898 should be redrawn 00899 (default) 00900 00901 Usage : Called from the parent. 00902 00903 ============================================================*/ 00904 { 00905 00906 m_fgcol = col; 00907 if( m_hWnd && redraw ) 00908 RedrawWindow(); 00909 00910 } 00911 00912 void CLineNumberStatic::SetTopAndBottom( int topline, int bottomline ) 00913 /* ============================================================ 00914 Function : CLineNumberStatic::SetTopAndBottom 00915 Description : Sets the top- and bottom line and redraw 00916 the control (if it exists) 00917 00918 Return : void 00919 Parameters : int topline - The top line number 00920 int bottomline - The bottom line number 00921 00922 Usage : Called when the top and bottom line is 00923 changed in the parent. 00924 00925 ============================================================*/ 00926 { 00927 00928 m_topline = topline; 00929 m_bottomline = bottomline; 00930 if( m_hWnd ) 00931 RedrawWindow(); 00932 00933 } 00934 00935 void CLineNumberStatic::SetTopMargin( int topmargin ) 00936 /* ============================================================ 00937 Function : CLineNumberStatic::SetTopMargin 00938 Description : Sets the top margin for painting. 00939 00940 Return : void 00941 Parameters : int topmargin - The top margin to set 00942 00943 Usage : Will be called with the value of GetRect 00944 from the parent. 00945 00946 ============================================================*/ 00947 { 00948 00949 m_topmargin = topmargin; 00950 00951 } 00952 00953 void CLineNumberStatic::SetLineNumberFormat( CString format ) 00954 /* ============================================================ 00955 Function : CLineNumberStatic::SetLineNumberFormat 00956 Description : Sets the format string of the control 00957 00958 Return : void 00959 Parameters : CString format - Format string to use 00960 00961 Usage : Called from the parent when the format 00962 string is changed. 00963 00964 ============================================================*/ 00965 { 00966 00967 m_format = format; 00968 if( m_hWnd ) 00969 RedrawWindow(); 00970 00971 }