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
48 // Attempt to use the PHP CLI to highlight the source file as HTML
49 NSPipe
* pipe
= [NSPipe pipe
];
50 NSTask
* task
= [NSTask
new];
51 [task setLaunchPath
:@
"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
52 [task setArguments
:[NSArray arrayWithObjects
:@
"-s", f
, nil]];
53 [task setStandardOutput
:pipe
];
55 NSData
* data
= [[pipe fileHandleForReading
] readDataToEndOfFile
];
56 NSAttributedString
* source
= [[NSAttributedString alloc
] initWithHTML
:data documentAttributes
:NULL
];
57 [[textView textStorage
] setAttributedString
:source
];
59 @catch(NSException
* exception
)
61 // If the PHP executable is not available then the NSTask will throw an exception
62 [textView setString
:[NSString stringWithContentsOfFile
:f
]];
67 * Flip the coordinates
75 * Tells the text view to scroll to a certain line
77 - (void)scrollToLine
:(int)line
79 // go through the document until we find the NSRange for the line we want
81 for (int i
= 0; i
< line
; i
++)
83 rangeIndex
= NSMaxRange([[textView string
] lineRangeForRange
:NSMakeRange(rangeIndex
, 0)]);
86 // now get the true start/end markers for it
87 unsigned lineStart
, lineEnd
;
88 [[textView string
] getLineStart
:&lineStart end
:NULL contentsEnd
:&lineEnd forRange
:NSMakeRange(rangeIndex
- 1, 0)];
89 [textView scrollRangeToVisible
:[[textView string
] lineRangeForRange
:NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
93 * Setup all the subviews for the source metaview
99 // setup the line number view
100 NSRect numberFrame
= [self bounds
];
101 numberFrame.origin
= NSMakePoint(0.0, 0.0);
102 numberFrame.size.width
= gutterWidth
;
103 numberView
= [[BSLineNumberView alloc
] initWithFrame
:numberFrame
];
104 [numberView setAutoresizingMask
:NSViewHeightSizable
];
105 [numberView setSourceView
:self];
106 [self addSubview
:numberView
];
108 // create the scroll view
109 NSRect scrollFrame
= [self bounds
];
110 scrollFrame.origin.x
= gutterWidth
;
111 scrollFrame.size.width
= scrollFrame.size.width
- gutterWidth
;
112 scrollView
= [[NSScrollView alloc
] initWithFrame
:scrollFrame
];
113 [scrollView setHasHorizontalScroller
:YES
];
114 [scrollView setHasVerticalScroller
:YES
];
115 [scrollView setAutohidesScrollers
:YES
];
116 [scrollView setBorderType
:NSBezelBorder
];
117 [scrollView setAutoresizingMask
:(NSViewWidthSizable | NSViewHeightSizable
)];
118 [[scrollView contentView
] setAutoresizesSubviews
:YES
];
119 [self addSubview
:scrollView
];
121 // add the text view to the scroll view
123 textFrame.origin
= NSMakePoint(0.0, 0.0);
124 textFrame.size
= [scrollView contentSize
];
125 textView
= [[BSSourceViewTextView alloc
] initWithFrame
:textFrame
];
126 [textView setSourceView
:self];
127 [textView setEditable
:NO
];
128 [textView setFont
:[NSFont fontWithName
:@
"Monaco" size
:10.0]];
129 [textView setHorizontallyResizable
:YES
];
130 [textView setVerticallyResizable
:YES
];
131 [textView setMinSize
:textFrame.size
];
132 [textView setMaxSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
133 [[textView textContainer
] setContainerSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
134 [[textView textContainer
] setWidthTracksTextView
:NO
];
135 [[textView textContainer
] setHeightTracksTextView
:NO
];
136 [textView setAutoresizingMask
:NSViewNotSizable
];
137 [scrollView setDocumentView
:textView
];