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
])
46 [numberView removeFromSuperview
];
47 [scrollView removeFromSuperview
];
48 [textView removeFromSuperview
];
54 * Sets the file name as well as the text of the source view
56 - (void)setFile
:(NSString
*)f
66 // Attempt to use the PHP CLI to highlight the source file as HTML
67 NSPipe
*pipe
= [NSPipe pipe
];
68 NSTask
*task
= [[NSTask
new] autorelease
];
69 [task setLaunchPath
:@
"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
70 [task setArguments
:[NSArray arrayWithObjects
:@
"-s", f
, nil]];
71 [task setStandardOutput
:pipe
];
73 NSData
*data
= [[pipe fileHandleForReading
] readDataToEndOfFile
];
74 NSAttributedString
*source
= [[NSAttributedString alloc
] initWithHTML
:data documentAttributes
:NULL
];
75 [[textView textStorage
] setAttributedString
:source
];
78 @catch (NSException
*exception
)
80 // If the PHP executable is not available then the NSTask will throw an exception
81 [textView setString
:[NSString stringWithContentsOfFile
:f
]];
86 * Flip the coordinates
94 * Tells the text view to scroll to a certain line
96 - (void)scrollToLine
:(int)line
98 // go through the document until we find the NSRange for the line we want
100 for (int i
= 0; i
< line
; i
++)
102 rangeIndex
= NSMaxRange([[textView string
] lineRangeForRange
:NSMakeRange(rangeIndex
, 0)]);
105 // now get the true start/end markers for it
106 unsigned lineStart
, lineEnd
;
107 [[textView string
] getLineStart
:&lineStart end
:NULL contentsEnd
:&lineEnd forRange
:NSMakeRange(rangeIndex
- 1, 0)];
108 [textView scrollRangeToVisible
:[[textView string
] lineRangeForRange
:NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
112 * Setup all the subviews for the source metaview
116 int gutterWidth
= 30;
118 // setup the line number view
119 NSRect numberFrame
= [self bounds
];
120 numberFrame.origin
= NSMakePoint(0.0, 0.0);
121 numberFrame.size.width
= gutterWidth
;
122 numberView
= [[BSLineNumberView alloc
] initWithFrame
:numberFrame
];
123 [numberView setAutoresizingMask
:NSViewHeightSizable
];
124 [numberView setSourceView
:self];
125 [self addSubview
:numberView
];
127 // create the scroll view
128 NSRect scrollFrame
= [self bounds
];
129 scrollFrame.origin.x
= gutterWidth
;
130 scrollFrame.size.width
= scrollFrame.size.width
- gutterWidth
;
131 scrollView
= [[NSScrollView alloc
] initWithFrame
:scrollFrame
];
132 [scrollView setHasHorizontalScroller
:YES
];
133 [scrollView setHasVerticalScroller
:YES
];
134 [scrollView setAutohidesScrollers
:YES
];
135 [scrollView setBorderType
:NSBezelBorder
];
136 [scrollView setAutoresizingMask
:(NSViewWidthSizable | NSViewHeightSizable
)];
137 [[scrollView contentView
] setAutoresizesSubviews
:YES
];
138 [self addSubview
:scrollView
];
140 // add the text view to the scroll view
142 textFrame.origin
= NSMakePoint(0.0, 0.0);
143 textFrame.size
= [scrollView contentSize
];
144 textView
= [[BSSourceViewTextView alloc
] initWithFrame
:textFrame
];
145 [textView setSourceView
:self];
146 [textView setEditable
:NO
];
147 [textView setFont
:[NSFont fontWithName
:@
"Monaco" size
:10.0]];
148 [textView setHorizontallyResizable
:YES
];
149 [textView setVerticallyResizable
:YES
];
150 [textView setMinSize
:textFrame.size
];
151 [textView setMaxSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
152 [[textView textContainer
] setContainerSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
153 [[textView textContainer
] setWidthTracksTextView
:NO
];
154 [[textView textContainer
] setHeightTracksTextView
:NO
];
155 [textView setAutoresizingMask
:NSViewNotSizable
];
156 [scrollView setDocumentView
:textView
];