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
)
21 - (void)errorHighlightingFile
:(NSNotification
*)notif
;
24 @implementation BSSourceView
26 @synthesize numberView
, textView
, scrollView
, markedLine
, delegate
, file
;
29 * Initializes the source view with the path of a file
31 - (id)initWithFrame
:(NSRect
)frame
33 if (self = [super initWithFrame
:frame
])
36 [[NSNotificationCenter defaultCenter
] addObserver
:self
37 selector
:@selector(errorHighlightingFile
:)
38 name
:NSFileHandleReadToEndOfFileCompletionNotification
51 [numberView removeFromSuperview
];
52 [scrollView removeFromSuperview
];
53 [textView removeFromSuperview
];
59 * Sets the file name as well as the text of the source view
61 - (void)setFile
:(NSString
*)f
69 if (![[NSFileManager defaultManager
] fileExistsAtPath
:f
])
71 [textView setString
:@
""];
77 // Attempt to use the PHP CLI to highlight the source file as HTML
78 NSPipe
*outPipe
= [NSPipe pipe
];
79 NSPipe
*errPipe
= [NSPipe pipe
];
80 NSTask
*task
= [[NSTask
new] autorelease
];
82 [task setLaunchPath
:@
"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
83 [task setArguments
:[NSArray arrayWithObjects
:@
"-s", f
, nil]];
84 [task setStandardOutput
:outPipe
];
85 [task setStandardError
:errPipe
];
88 [[errPipe fileHandleForReading
] readToEndOfFileInBackgroundAndNotify
];
90 NSData
*data
= [[outPipe fileHandleForReading
] readDataToEndOfFile
];
91 NSAttributedString
*source
= [[NSAttributedString alloc
] initWithHTML
:data documentAttributes
:NULL
];
92 [[textView textStorage
] setAttributedString
:source
];
95 @catch (NSException
*exception
)
97 // If the PHP executable is not available then the NSTask will throw an exception
98 [textView setString
:[NSString stringWithContentsOfFile
:f
]];
103 * If an error occurs in reading the highlighted PHP source, this will merely set the string
105 - (void)errorHighlightingFile
:(NSNotification
*)notif
107 NSData
*data
= [[notif userInfo
] objectForKey
:NSFileHandleNotificationDataItem
];
108 if ([data length
] > 0) // there's something on stderr, so the PHP CLI failed
109 [textView setString
:[NSString stringWithContentsOfFile
:file
]];
113 * Flip the coordinates
121 * Tells the text view to scroll to a certain line
123 - (void)scrollToLine
:(int)line
125 if (![[NSFileManager defaultManager
] fileExistsAtPath
:file
])
128 // go through the document until we find the NSRange for the line we want
130 for (int i
= 0; i
< line
; i
++)
132 rangeIndex
= NSMaxRange([[textView string
] lineRangeForRange
:NSMakeRange(rangeIndex
, 0)]);
135 // now get the true start/end markers for it
136 unsigned lineStart
, lineEnd
;
137 [[textView string
] getLineStart
:&lineStart end
:NULL contentsEnd
:&lineEnd forRange
:NSMakeRange(rangeIndex
- 1, 0)];
138 [textView scrollRangeToVisible
:[[textView string
] lineRangeForRange
:NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
142 * Setup all the subviews for the source metaview
146 int gutterWidth
= 30;
148 // setup the line number view
149 NSRect numberFrame
= [self bounds
];
150 numberFrame.origin
= NSMakePoint(0.0, 0.0);
151 numberFrame.size.width
= gutterWidth
;
152 numberView
= [[BSLineNumberView alloc
] initWithFrame
:numberFrame
];
153 [numberView setAutoresizingMask
:NSViewHeightSizable
];
154 [numberView setSourceView
:self];
155 [self addSubview
:numberView
];
157 // create the scroll view
158 NSRect scrollFrame
= [self bounds
];
159 scrollFrame.origin.x
= gutterWidth
;
160 scrollFrame.size.width
= scrollFrame.size.width
- gutterWidth
;
161 scrollView
= [[NSScrollView alloc
] initWithFrame
:scrollFrame
];
162 [scrollView setHasHorizontalScroller
:YES
];
163 [scrollView setHasVerticalScroller
:YES
];
164 [scrollView setAutohidesScrollers
:YES
];
165 [scrollView setBorderType
:NSBezelBorder
];
166 [scrollView setAutoresizingMask
:(NSViewWidthSizable | NSViewHeightSizable
)];
167 [[scrollView contentView
] setAutoresizesSubviews
:YES
];
168 [self addSubview
:scrollView
];
170 // add the text view to the scroll view
172 textFrame.origin
= NSMakePoint(0.0, 0.0);
173 textFrame.size
= [scrollView contentSize
];
174 textView
= [[BSSourceViewTextView alloc
] initWithFrame
:textFrame
];
175 [textView setSourceView
:self];
176 [textView setEditable
:NO
];
177 [textView setFont
:[NSFont fontWithName
:@
"Monaco" size
:10.0]];
178 [textView setHorizontallyResizable
:YES
];
179 [textView setVerticallyResizable
:YES
];
180 [textView setMinSize
:textFrame.size
];
181 [textView setMaxSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
182 [[textView textContainer
] setContainerSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
183 [[textView textContainer
] setWidthTracksTextView
:NO
];
184 [[textView textContainer
] setHeightTracksTextView
:NO
];
185 [textView setAutoresizingMask
:NSViewNotSizable
];
186 [scrollView setDocumentView
:textView
];