1/*
2    This file is part of HelpViewer (http://www.roard.com/helpviewer)
3    Copyright (C) 2003 Nicolas Roard (nicolas@roard.com)
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "Section.h"
21#include "HandlerStructureXLP.h"
22
23static id <TextFormatter> TextFormatter = nil;
24static NSBundle* Bundle = nil;
25
26@implementation Section
27
28+ (void) setTextFormatter: (id) obj {
29	ASSIGN (TextFormatter, obj);
30}
31
32+ (void) setBundle: (NSBundle*) obj {
33	ASSIGN (Bundle, obj);
34	[TextFormatter setBundle: Bundle];
35}
36
37- (id) initWithHeader: (NSString*) pheader {
38    self = [super init];
39    ASSIGN (header, pheader);
40    text = [[NSMutableAttributedString alloc] init];
41    subs = [[NSMutableArray alloc] init];
42    parent = nil;
43    rendered = NO;
44    loaded = NO;
45    path = nil;
46    return self;
47}
48
49- (void) dealloc {
50    RELEASE (subs);
51    RELEASE (text);
52    RELEASE (header);
53    RELEASE (path);
54}
55
56- (NSMutableAttributedString*) text {
57	return text;
58}
59
60- (void) setPath: (NSString*) src {
61	ASSIGN (path, [Bundle pathForResource: [src stringByDeletingPathExtension] ofType: [src pathExtension]]);
62}
63
64- (void) setLoaded: (BOOL) load {
65	loaded = load;
66}
67
68- (BOOL) loaded {
69	return loaded;
70}
71
72- (void) load {
73	if ([[NSFileManager defaultManager] fileExistsAtPath: path])
74	{
75		id <HandlerStructure> handler = [[HandlerStructureXLP alloc] initWithSection: self];
76		[handler setPath: path];
77		[handler parse];
78		loaded = YES;
79	}
80}
81
82- (NSMutableAttributedString*) contentWithLevel: (int) level {
83	int i;
84	id ret = nil;
85
86	//NSLog (@"Section contentWithLevel: %d (%@)", level, [self header]);
87	if (rendered)
88	{
89		ret = [[NSMutableAttributedString alloc] initWithAttributedString: text];
90	}
91	else
92	{
93		if (loaded == NO)
94		{
95			[self load];
96		}
97
98		if ((loaded == YES) && (TextFormatter != nil))
99		{
100			ret = [[NSMutableAttributedString alloc] init];
101
102			if (type != SECTION_TYPE_PLAIN)
103			{
104				id head  = [TextFormatter renderHeader: header withLevel: level];
105				[ret appendAttributedString: head];
106			}
107			id ttext = [TextFormatter renderText: text];
108			[ret appendAttributedString: ttext];
109			for (i=0; i < [subs count]; i++)
110			{
111				id sub = [subs objectAtIndex: i];
112				//id head = [[NSAttributedString alloc] initWithString: [sub header]];
113				//[ret appendAttributedString: head];
114				//[head release];
115				[ret appendAttributedString: [sub contentWithLevel: level+1]];
116			}
117			[text release];
118			text = [[NSMutableAttributedString alloc] initWithAttributedString: ret];
119			rendered = YES;
120		}
121	}
122	//NSLog (@"fin Section contentWithLevel: %d (%@)", level, [self header]);
123	//NSLog (@"on retourne : %@", ret);
124
125    return AUTORELEASE (ret);
126}
127
128- (void) setType: (int) t { type = t; }
129- (int) type { return type; }
130
131/*
132- (void) setText: (NSMutableAttributedString*) t {
133	NSLog (@"setText : %@", t);
134	RELEASE (text);
135	text = [[NSMutableAttributedString alloc] initWithAttributedString: t];
136}*/
137
138- (NSString*) header {
139    return header;
140}
141
142- (NSRange) range {
143    return range;
144}
145
146- (NSMutableArray*) subs {
147	return subs;
148};
149
150- (void) setRange: (NSRange) prange {
151    range = prange;
152}
153
154- (void) addSub: (Section*) sub {
155	//NSLog (@"addSub: Section (%@)", [sub header]);
156	[sub setParent: self];
157	[subs addObject: sub];
158	//NSLog (@"fin addSub: Section (%@)", [sub header]);
159}
160
161- (void) setParent: (Section*) par {
162	parent = par;
163}
164
165- (Section*) parent { return parent; }
166
167- (void) print {
168	int i;
169	NSLog (@"(nom : %@) {", header);
170	for (i=0; i < [subs count]; i++)
171	{
172		[[subs objectAtIndex: i] print];
173	}
174	NSLog (@"} (nom : %@)", header);
175}
176
177@end
178