Click here to Skip to main content
Licence Public Domain
First Posted 9 Mar 2004
Views 91,942
Bookmarked 57 times

Controls-in-controls: A line-numbering edit box

By | 11 May 2004 | Article
A line-numbering edit control

Sample Image - linenumberedit.gif

Introduction

CLineNumberEdit is a CEdit-derived class with automatic line-numbering along the left margin. Colors for the sidebar are editable, so is the format of the line numbers. The subclassing is an experiment in containing controls inside an edit box, and even if I have added enough functionality to make the class mildly useful, the primary goal is to investigate this containment (see also www.codeproject.com/editctrl/editwithicon.asp).

Using the code

Include the cpp- and h-file to the project. CLineNumberEdit can be instantiated either dynamically, by a call to Create, or from a dialog template. In the latter case, a control variable can be created and bound to the control with the class wizard, or SubclassDlgItem can be used.

Public members of this class:

To set the background color of the line number field, call SetMarginBackgroundColor. The default is RGB( 255, 255, 248 ) (see the picture)

SetMarginForegroundColor will set the text color. The default is black.

SetLineNumberFormat sets a formatting string, later used with CString::Format, so it follows the rules for this call. The format string must contain one (and only one) numerical type, of course. The default is "%05i", yielding a right aligned, zero-padded 5 character string.

In PreSubclassWindow, the edit box style is checked. If it doesn't contain ES_MULTILINE, the class ASSERTs. Apart from being slightly irrelevant for non-multi line edits, to say the least, the SetRect call used to make place for the line numbers will not work for single-line edits.

The margin is set up in Prepare. Here, the necessary width of the line-number field is calculated. This is accomplished by creating a string, using the control format string, formatting it with the max number of characters for the control (calling CEdit::GetLimitText). This assumes that each line will only be one character long, so we know that this will be our maximum number of lines. The string is measured, and a small margin is added.

Finally, a CLineNumberStatic - a CStatic-derived control - is created with the desired size. The top- and bottom line of the edit box is sent to this control. The top margin is also set.

CLineNumberStatic is a simple CStatic-derived class, managing the painting of the line-numbers through OnPaint. The drawing is made to a memory CDC and blitted to the screen, and WM_ERASEBKGND is handled to avoid flicker.

The CLineNumberStatic is synchronized to the CLineNumberEdit by setting new top- and bottom-line values. This has to be done as soon as the control is scrolled, and/or when the contents are changed. Fortunately, this is simple - by handling the EN_CHANGE and WM_SETTEXT messages for modifications, and EN_VSCROLL plus WM_VSCROLL for scrolling. EN_VSCROLL takes car of scrolling "inside" the edit (such as moving with the arrow keys), WM_VSCROLL updates the line-numbers while the thumb-track is moved.

WM_SIZE is handled, both to reestablish the line-number field size and the edit rectangle. Finally, WM_SETFONT is also handled to reestablish the edit rectangle - as it is reset when the control receives this message. WM_SETFONT will also make it necessary to redraw the line-number control.

Points of Interest

This control presented a few problems. The edit control and the line number static must be synchronized. The edit rectangle must be re-established as soon as it is destroyed. Primarily, the problem is to find the messages sent to the control where those two tasks must be accomplished. A murky area is when the edit rectangle gets destroyed, I've later found that SetWindowPlacement also does this - with no solution other than "use MoveWindow instead".

Getting the first visible line is simple - it's just to call GetFirstVisibleLine. The bottom one is trickier, however. I first tried this using the really devious method of getting the difference between two lines, using a blend of calls to GetLine and GetPosFromChar, but the contents of the control was not reformatted in a way to make this reliably possible. Now, I simply use the height of a line in the font of the control, and the top margin from GetRect of the parent.

History

Dawn of time

First version

14/4 2004

Some really nice additions from Keith Bowes; the possibility to use system colors for the line-number part, and handling of start- and max values.

UseSystemColours( BOOL bUseEnabled = TRUE, BOOL bUseDisabled = TRUE ); can be called to use the current system colors for the enabled and disabled state respectively.

SetLineNumberRange( UINT nMin, UINT nMax = 0 ); can be used to set the starting line number and the max number of lines to be numbered. Note that this is not a min-max range, a minimum value of 100 and a max of 10 will number the lines from 100, 101, 102 etc. up to 109.

Setting nMax to zero means no upper limit. Lines above the max-value will not be numbered, although they will of course still be displayed.

22/4 2004

A new addition from Keith Bowes - handling of the WM_LINESCROLL message. This is community spirit!

Not to be outdone, I've added code for selecting a complete line by clicking on the line number. Accomplished by a registered message, and click-handling on the line-number control.

Added a small demo-program. I realize that some people might just want to take a look - creating a project just for this reason is perhaps a bit much :-)

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Johan Rosengren

Software Developer (Senior)
Abstrakt Mekanik AB
Sweden Sweden

Member

45 years old, married, three kids.
 
Started with computers more than 20 years ago on a CBM-64.
 
Read Theoretical Philosophy at the University of Lund.
 
Working as a C++ consultant developer.
 
Science-fiction freak. Enjoy vintage punkrock.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow could I Create CLineNumberEdit dynamically ? Pinmemberanceswu19:41 21 Dec '11  
GeneralAdaptation to Rich Text Pinmemberflyontheweb5:59 14 Oct '10  
GeneralThanks for the code! PinmemberDavid Howe15:47 21 May '07  
GeneralRe: Thanks for the code! PinmemberJohan Rosengren6:26 22 May '07  
Questionin Visual Basic .NET?? Pinmemberuniquekaiser19:12 12 Feb '07  
AnswerRe: in Visual Basic .NET?? PinmemberJohan Rosengren6:27 13 Feb '07  
GeneralFlickering Pinmembernightrider138:59 27 Dec '06  
Hey Johan,
really good work.
But I have a little problem with flickering when I
attach the control in a CView as you explained it.
When I press the enter button continously and make a lot of new linenumbers, the
margin is flickering a bit.
Do you have any ideas how I can avoid that?
GeneralRe: Flickering PinmemberJohan Rosengren19:50 27 Dec '06  
GeneralRe: Flickering Pinmembernightrider132:21 28 Dec '06  
GeneralRe: Flickering PinmemberJohan Rosengren4:23 28 Dec '06  
GeneralRe: Flickering Pinmembernightrider1314:41 28 Dec '06  
GeneralRe: Flickering PinmemberJohan Rosengren21:24 28 Dec '06  
GeneralRe: Flickering Pinmembernightrider1317:07 31 Dec '06  
GeneralGood Job PinmemberCourageousSam8:04 11 Dec '06  
GeneralRe: Good Job PinmemberJohan Rosengren6:44 13 Dec '06  
GeneralNice PinmemberBassam Abdul-Baki5:09 6 Apr '06  
GeneralRe: Nice PinmemberJohan Rosengren23:03 8 Apr '06  
GeneralBug PinmemberJohann Gerell22:13 13 May '04  
GeneralRe: Bug PinmemberJohan Rosengren22:59 13 May '04  
QuestionHow to change it to CEditView? Pinmemberli00006:19 12 May '04  
AnswerRe: How to change it to CEditView? PinmemberJohan Rosengren8:01 12 May '04  
GeneralPlug & play missing! PinmemberA sleepy one6:54 10 Mar '04  
GeneralRe: Plug & play missing! PinmemberJohan Rosengren8:19 10 Mar '04  
GeneralRe: Plug & play missing! PinmemberBowes11:22 8 Apr '04  
GeneralRe: Plug & play missing! PinmemberJohan Rosengren20:55 8 Apr '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 12 May 2004
Article Copyright 2004 by Johan Rosengren
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid