Index: src/Fl_Text_Display.cxx
===================================================================
--- src/Fl_Text_Display.cxx	(revision 9705)
+++ src/Fl_Text_Display.cxx	(working copy)
@@ -175,7 +175,22 @@
 }
 
 
+/**
+ LZA
+ Added to support line numbers
+*/
+void Fl_Text_Display::linenumber_width( int width )
+{
+  if (width < 0 || width > 150) return;
+  mLineNumWidth = width;
+  resize(x(), y(), w(), h());
+}
 
+int Fl_Text_Display::linenumber_width()
+{
+    return mLineNumWidth;
+}
+ 
 /**
  Attach a text buffer to display, replacing the current buffer (if any)
  \param buf attach this text buffer
@@ -405,7 +420,14 @@
       }
     }
   }
-
+  /**
+   LZA
+   add linenum width to the text area
+  */
+  if (mLineNumWidth != 0) {
+    text_area.x += mLineNumWidth;
+    text_area.w -= mLineNumWidth;
+  }
   // user request to change viewport
   if (mTopLineNumHint != mTopLineNum || mHorizOffsetHint != mHorizOffset)
     scroll_(mTopLineNumHint, mHorizOffsetHint);
@@ -465,9 +487,13 @@
   for ( line = firstLine; line <= lastLine; line++ )
     draw_vline( line, left, left + width, 0, INT_MAX );
 
+  /*
+   LZA
+   found this commented out, not sure why it's needed here
+  */
   /* draw the line numbers if exposed area includes them */
-  if (mLineNumWidth != 0 && left <= mLineNumLeft + mLineNumWidth)
-    draw_line_numbers(false);
+  //if (mLineNumWidth != 0 && left <= mLineNumLeft + mLineNumWidth)
+  //  draw_line_numbers(false);
 
   fl_pop_clip();
 }
@@ -700,7 +726,12 @@
     mNBufferLines  = 0;
     mFirstChar     = 0;
     mTopLineNum    = 1;
-    mAbsTopLineNum = 0;
+    /*
+     LZA
+     changed this to show the proper line numbers when wrapping is enabled
+    */
+    //mAbsTopLineNum = 0;
+    mAbsTopLineNum = 1;
   }
 
   resize(x(), y(), w(), h());
@@ -1515,7 +1546,14 @@
       //                ( origCursorPos <= endDispPos || endDispPos == buf->length() ) )
     }
 
-    if (linesInserted > 1) textD->draw_line_numbers(false);
+    //if (linesInserted > 1) textD->draw_line_numbers(false);
+    if (linesInserted > 1) 	{
+        // LZA
+        //apd: textD->draw_line_numbers(false); can't do this b/c not called from virtual draw();
+        textD->damage( ::FL_DAMAGE_EXPOSE );
+        //textD->draw_line_numbers(false);
+    }
+      
   } else {
     endDispPos = buf->next_char(textD->mLastChar);
     // CET - FIXME   if ( origCursorPos >= pos )
@@ -1524,7 +1562,13 @@
      have changed. If only one line is altered, line numbers cannot
      be affected (the insertion or removal of a line break always
      results in at least two lines being redrawn). */
-    textD->draw_line_numbers(false);
+    
+      /* LZA
+       without calling the draw_line_numbers method here when we 
+       hit enter for new line no line number gets drawn
+      */
+      textD->draw_line_numbers(true);
+      //textD->damage( ::FL_DAMAGE_EXPOSE );
   }
   IS_UTF8_ALIGNED2(buf, startDispPos)
   IS_UTF8_ALIGNED2(buf, endDispPos)
@@ -1543,7 +1587,10 @@
 }
 
 
-
+/*
+ LZA
+ line numbering methods
+*/
 /**
  \brief Line numbering stuff, currently unused.
 
@@ -1570,10 +1617,11 @@
  Returns 0 if the absolute top line number is not being maintained.
  */
 int Fl_Text_Display::get_absolute_top_line_number() const {
-  if (!mContinuousWrap)
-    return mTopLineNum;
-  if (maintaining_absolute_top_line_number())
-    return mAbsTopLineNum;
+    if (!mContinuousWrap)
+      return mTopLineNum;
+    if (maintaining_absolute_top_line_number())
+      return mAbsTopLineNum;
+
   return 0;
 }
 
@@ -2634,59 +2682,78 @@
  If clearAll is False, writes only over the character cell areas.  Setting
  clearAll to True will clear out any stray marks outside of the character cell
  area, which might have been left from before a resize or font change.
-
- This function is not used.
  */
-void Fl_Text_Display::draw_line_numbers(bool /*clearAll*/) {
-#if 0
-  int y, line, visLine, nCols, lineStart;
-  char lineNumString[12];
-  int lineHeight = mMaxsize ? mMaxsize : textsize_;
-  int charWidth = TMPFONTWIDTH;   //mFontStruct->max_bounds.width;
+/*
+ LZA
+ added this draw_line_method based on the patch from
+ http://www.mail-archive.com/fltk-dev@easysw.com/msg06376.html
+ altered to support line numbers right alignment
+*/
+void Fl_Text_Display::draw_line_numbers(bool clearAlxl) {
+  int Y, line, visLine, lineStart;
+  char lineNumString[16];
+  int lineHeight = mMaxsize;
+  int charWidth = TMPFONTWIDTH;
 
-  /* Don't draw if mLineNumWidth == 0 (line numbers are hidden), or widget is
-   not yet realized */
-  if (mLineNumWidth == 0 || visible_r())
-    return;
+  /* Don't draw if lineNumWidth == 0 (line numbers are hidden), or widget is
+     not yet realized */
+  if (mLineNumWidth == 0 || !visible_r())
+      return;
 
-  /* GC is allocated on demand, since not everyone will use line numbering */
-  if (textD->lineNumGC == NULL) {
-    XGCValues values;
-    values.foreground = textD->lineNumFGPixel;
-    values.background = textD->bgPixel;
-    values.font = textD->fontStruct->fid;
-    textD->lineNumGC = XtGetGC(textD->w,
-                               GCFont| GCForeground | GCBackground, &values);
-  }
+  /* Make sure we reset the clipping range for the line numbers GC, because
+     the GC may be shared (eg, if the line numbers and text have the same
+     color) and therefore the clipping ranges may be invalid. */
+  int xoff = Fl::box_dx(box());
 
-  /* Erase the previous contents of the line number area, if requested */
-  if (clearAll)
-    XClearArea(XtDisplay(textD->w), XtWindow(textD->w), textD->lineNumLeft,
-               textD->top, textD->lineNumWidth, textD->height, False);
+  ::fl_push_clip(
+	  x()+xoff,
+	  y()+Fl::box_dy(box()),
+	  mLineNumWidth-xoff,
+	  h()-Fl::box_dh(box()) - (mHScrollBar->visible()?mHScrollBar->h():0) );
 
-  /* Draw the line numbers, aligned to the text */
-  nCols = min(11, textD->lineNumWidth / charWidth);
-  y = textD->top;
-  line = getAbsTopLineNum(textD);
-  for (visLine=0; visLine < textD->nVisibleLines; visLine++) {
-    lineStart = textD->lineStarts[visLine];
-    if (lineStart != -1 && (lineStart==0 ||
-                            BufGetCharacter(textD->buffer, lineStart-1)=='\n')) {
-      sprintf(lineNumString, "%*d", nCols, line);
-      XDrawImageString(XtDisplay(textD->w), XtWindow(textD->w),
-                       textD->lineNumGC, textD->lineNumLeft, y + textD->ascent,
-                       lineNumString, strlen(lineNumString));
+  /*
+   LZA
+   set background color for line number area
+   TODO: update this not to be hard coded so we can theme it
+   */
+  // Erase background
+  ::fl_color( 240,240,240 );
+  ::fl_rectf( x(), y(), mLineNumWidth, h() );
+
+  // Draw separator line
+  //::fl_color( 180,180,180 );
+  //::fl_line(x()+mLineNumWidth-1, y(), x()+mLineNumWidth-1, y()+h());
+
+  // Draw text
+  ::fl_font(textfont(), textsize());
+
+  Y = y();
+  line = get_absolute_top_line_number();
+
+  int last_y = y();
+  /* 
+   LZA
+   set font color for line numbers
+   TODO: update this not to be hard coded so we can theme it
+  */
+  ::fl_color( 110,110,110 );
+  for (visLine=0; visLine < mNVisibleLines; visLine++) {
+    lineStart = mLineStarts[visLine];
+    if (lineStart != -1 && (lineStart==0 || buffer()->char_at(lineStart-1)=='\n')) {
+      int lineNumStringLen = sprintf(lineNumString, "%d", line);
+      //::fl_draw( lineNumString, lineNumStringLen, x()+xoff+LEFT_MARGIN+1, (Y + mMaxsize));
+    int linenum_xpos = mLineNumWidth - (xoff+LEFT_MARGIN) - (charWidth * lineNumStringLen) - ((lineNumStringLen * 1) * 2);
+      ::fl_draw( lineNumString, lineNumStringLen, linenum_xpos, (Y + mMaxsize));
+	  last_y = Y;
       line++;
     } else {
-      XClearArea(XtDisplay(textD->w), XtWindow(textD->w),
-                 textD->lineNumLeft, y, textD->lineNumWidth,
-                 textD->ascent + textD->descent, False);
       if (visLine == 0)
         line++;
     }
-    y += lineHeight;
+    Y += lineHeight;
   }
-#endif
+  
+  fl_pop_clip();
 }
 
 static int max( int i1, int i2 ) {
@@ -3401,8 +3468,8 @@
       fl_rectf(mVScrollBar->x(), mHScrollBar->y(),
                mVScrollBar->w(), mHScrollBar->h(),
                FL_GRAY);
-
-    // blank the previous cursor protrusions
+    // LZA
+    //draw_line_numbers(true);
   }
   else if (damage() & (FL_DAMAGE_SCROLL | FL_DAMAGE_EXPOSE)) {
     //    printf("blanking previous cursor extrusions at Y: %d\n", mCursorOldY);
@@ -3473,6 +3540,13 @@
     //printf("drew cursor at pos: %d (%d,%d)\n", mCursorPos, X, Y);
     fl_pop_clip();
   }
+  /**
+   LZA
+   This is very important to be at the end of this method, otherwise the line
+   numbers will not scroll with the text edit area
+  */
+  draw_line_numbers(true);
+    
   fl_pop_clip();
 }
 
Index: FL/Fl_Text_Display.H
===================================================================
--- FL/Fl_Text_Display.H	(revision 9705)
+++ FL/Fl_Text_Display.H	(working copy)
@@ -290,6 +290,10 @@
    */
   double col_to_x(double col) const;
   
+  /* line number methods */
+  void linenumber_width(int width);
+  int linenumber_width();
+
 protected:
   // Most (all?) of this stuff should only be called from resize() or
   // draw().
Index: test/editor.cxx
===================================================================
--- test/editor.cxx	(revision 9705)
+++ test/editor.cxx	(working copy)
@@ -771,7 +771,8 @@
     w->editor = new Fl_Text_Editor(0, 30, 660, 370);
     w->editor->textfont(FL_COURIER);
     w->editor->textsize(TS);
-  //w->editor->wrap_mode(Fl_Text_Editor::WRAP_AT_BOUNDS, 250);
+    w->editor->linenumber_width(50);
+    w->editor->wrap_mode(Fl_Text_Editor::WRAP_AT_BOUNDS, 50);
     w->editor->buffer(textbuf);
     w->editor->highlight_data(stylebuf, styletable,
                               sizeof(styletable) / sizeof(styletable[0]),
