Tweak towards having an option to include sc3-plugins included in the win bundle...
[supercollider.git] / Source / app / HTMLRenderer.m
blob4ab5fe55ace0d84654777c9bc6eba1cd89581c38
1 #import "HTMLRenderer.h"
3 //#define DEBUG
5 #ifdef DEBUG
6         #define Log(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
7 #else
8         #define Log(fmt, ...)
9 #endif
11 @implementation HTMLRenderer
12 + (NSAttributedString *)attributedStringWithURL:(NSURL *)url {
13         HTMLRenderer *htmlRenderer = [[HTMLRenderer alloc] init];
14         NSAttributedString *attributedString = [htmlRenderer attributedStringWithURL:url];
15         [htmlRenderer release];
16         return attributedString;
19 - (id)init {
20         if((self = [super init]) == nil)
21                 return nil;
22                 
23         renderLock = [[NSConditionLock alloc] initWithCondition:0];
24         finishedRendering = 0;
25         renderWebView = [[WebView alloc] initWithFrame:NSMakeRect(0., 0., 2000., 100.) frameName:nil groupName:nil];
26         [renderWebView setFrameLoadDelegate:self];
28         return self;
31 - (NSAttributedString *)attributedStringWithURL:(NSURL *)url {
32         NSMutableAttributedString *result = nil;
33         
34         finishedRendering = 0;
35         [self renderHTMLFragment:url];
36         
37         if(finishedRendering == 1)
38                 return [(id <WebDocumentText>)[[[renderWebView mainFrame] frameView] documentView] attributedString];
39         else
40                 return nil;
43 - (void)renderHTMLFragment:(NSURL *)url {
44         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
45         NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
47         [NSThread setThreadPriority:1.0];
48         [renderLock lock];
49         
50         [[renderWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
52         while(!finishedRendering)
53                 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
54         
55         [renderLock unlockWithCondition:1];
56         [pool release];
59 - (void)dealloc {
60         [renderLock release];
61         [renderWebView release];
62         [super dealloc];
65 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
66         finishedRendering = -1;
69 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
70         finishedRendering = -1;
73 - (void)webView:(WebView *) sender didFinishLoadForFrame:(WebFrame *) frame {
74         finishedRendering = 1;
77 @end