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 "BSSourceView.h"
19 @interface BSSourceView (Private
)
23 @implementation BSSourceView
25 @synthesize numberView
, textView
, scrollView
, markedLine
, delegate
, file
;
28 * Initializes the source view with the path of a file
30 - (id)initWithFrame
:(NSRect
)frame
32 if (self = [super initWithFrame
:frame
])
40 * Sets the file name as well as the text of the source view
42 - (void)setFile
:(NSString
*)f
45 [textView setString
:[NSString stringWithContentsOfFile
:f
]];
49 * Flip the coordinates
57 * Tells the text view to scroll to a certain line
59 - (void)scrollToLine
:(int)line
61 // go through the document until we find the NSRange for the line we want
63 for (int i
= 0; i
< line
; i
++)
65 rangeIndex
= NSMaxRange([[textView string
] lineRangeForRange
:NSMakeRange(rangeIndex
, 0)]);
68 // now get the true start/end markers for it
69 unsigned lineStart
, lineEnd
;
70 [[textView string
] getLineStart
:&lineStart end
:NULL contentsEnd
:&lineEnd forRange
:NSMakeRange(rangeIndex
- 1, 0)];
71 [textView scrollRangeToVisible
:[[textView string
] lineRangeForRange
:NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
75 * Setup all the subviews for the source metaview
81 // setup the line number view
82 NSRect numberFrame
= [self bounds
];
83 numberFrame.origin
= NSMakePoint(0.0, 0.0);
84 numberFrame.size.width
= gutterWidth
;
85 numberView
= [[BSLineNumberView alloc
] initWithFrame
:numberFrame
];
86 [numberView setAutoresizingMask
:NSViewHeightSizable
];
87 [numberView setSourceView
:self];
88 [self addSubview
:numberView
];
90 // create the scroll view
91 NSRect scrollFrame
= [self bounds
];
92 scrollFrame.origin.x
= gutterWidth
;
93 scrollFrame.size.width
= scrollFrame.size.width
- gutterWidth
;
94 scrollView
= [[NSScrollView alloc
] initWithFrame
:scrollFrame
];
95 [scrollView setHasHorizontalScroller
:YES
];
96 [scrollView setHasVerticalScroller
:YES
];
97 [scrollView setAutohidesScrollers
:YES
];
98 [scrollView setBorderType
:NSBezelBorder
];
99 [scrollView setAutoresizingMask
:(NSViewWidthSizable | NSViewHeightSizable
)];
100 [[scrollView contentView
] setAutoresizesSubviews
:YES
];
101 [self addSubview
:scrollView
];
103 // add the text view to the scroll view
105 textFrame.origin
= NSMakePoint(0.0, 0.0);
106 textFrame.size
= [scrollView contentSize
];
107 textView
= [[BSSourceViewTextView alloc
] initWithFrame
:textFrame
];
108 [textView setSourceView
:self];
109 [textView setEditable
:NO
];
110 [textView setFont
:[NSFont fontWithName
:@
"Monaco" size
:10.0]];
111 [textView setHorizontallyResizable
:YES
];
112 [textView setVerticallyResizable
:YES
];
113 [textView setMinSize
:textFrame.size
];
114 [textView setMaxSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
115 [[textView textContainer
] setContainerSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
116 [[textView textContainer
] setWidthTracksTextView
:NO
];
117 [[textView textContainer
] setHeightTracksTextView
:NO
];
118 [textView setAutoresizingMask
:NSViewNotSizable
];
119 [scrollView setDocumentView
:textView
];