3 * Copyright (c) 2007 - 2008, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "BSLineNumberView.h"
20 @implementation BSLineNumberView
22 @synthesize sourceView
;
25 * Initializer for the line number view
27 - (id)initWithFrame
:(NSRect
)frame
29 if (self = [super initWithFrame
:frame
])
37 * Flip the coordinates
45 * Draws the line numbers whenever necessary
47 - (void)drawRect
:(NSRect
)rect
50 [[NSColor colorWithDeviceRed
:0.871 green
:0.871 blue
:0.871 alpha
:1] set
];
51 [NSBezierPath fillRect
:rect
];
53 [[NSColor blackColor
] set
];
54 [NSBezierPath strokeLineFromPoint
:NSMakePoint(rect.origin.x
, rect.origin.y
) toPoint
:NSMakePoint(rect.origin.x
+ rect.size.width
, rect.origin.y
)];
56 [[NSColor grayColor
] set
];
57 [NSBezierPath strokeLineFromPoint
:NSMakePoint(rect.origin.x
, rect.size.height
) toPoint
:NSMakePoint(rect.origin.x
+ rect.size.width
, rect.size.height
)];
59 // font attributes for the line number
60 NSDictionary
*attrs
= [NSDictionary dictionaryWithObjectsAndKeys
:[NSFont fontWithName
:@
"Monaco" size
:9.0], NSFontAttributeName
, [NSColor grayColor
], NSForegroundColorAttributeName
, nil];
62 unsigned i
= 0, line
= 1;
63 while (i
< [[[sourceView textView
] layoutManager
] numberOfGlyphs
])
66 NSRect fragRect
= [self convertRect
:[[[sourceView textView
] layoutManager
] lineFragmentRectForGlyphAtIndex
:i effectiveRange
:&fragRange
] fromView
:[sourceView textView
]];
67 fragRect.origin.x
= rect.origin.x
; // horizontal scrolling matters not
69 // we want to paint the top and bottom line number even if they're cut off
70 NSRect testRect
= rect
;
71 testRect.origin.y
-= fragRect.size.height
- 1;
72 testRect.size.height
+= fragRect.size.height
- 1;
73 if (NSPointInRect(fragRect.origin
, testRect
))
75 NSString
*num
= [NSString stringWithFormat
:@
"%u", line
];
76 NSSize strSize
= [num sizeWithAttributes
:attrs
];
77 [num drawAtPoint
:NSMakePoint([self frame
].size.width
- strSize.width
- 3, fragRect.origin.y
+ ((fragRect.size.height
- strSize.height
) / 2)) withAttributes
:attrs
];
80 i
+= fragRange.length
;