File: mainWindowController.m

package info (click to toggle)
helpviewer.app 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,940 kB
  • sloc: objc: 2,416; makefile: 12
file content (446 lines) | stat: -rw-r--r-- 11,648 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
    This file is part of HelpViewer (http://www.roard.com/helpviewer)
    Copyright (C) 2003      Nicolas Roard (nicolas@roard.com)
                  2020-2021 Riccardo Mottola <rm@gnu.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the
    Free Software Foundation, Inc.  
    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#ifdef GNUSTEP
#import <GNUstepBase/GNUstep.h>
#else
#import "GNUstep.h"
#endif

#import <AppKit/NSCell.h>

#import "mainWindowController.h"
#import "Section.h"

@implementation MainWindowController

- (id) initWithTextView: (NSTextView*) _text andBrowserView:(NSBrowser*) browser {
  if ((self = [super init]))
    {
	resultTextView = [_text retain];
	resultOutlineView = [browser retain];

	[resultTextView setDelegate: self];
	[resultTextView setTextContainerInset: NSMakeSize (8,8)];
	
	[resultOutlineView setDelegate: self];
	[resultOutlineView setAllowsMultipleSelection: NO];
	[resultOutlineView setCellClass: [BrowserCell class]];
	[resultOutlineView setAction: @selector(browserClick:)];
	[resultOutlineView setTarget: self];
	//[resultOutlineView setDataSource: self];


	//handler = RETAIN ([XMLHandler new]);
	handler = [HandlerStructureXLP new];
	
	[handler setTextView: resultTextView];

	id TextFormatter = [[TextFormatterXLP alloc] init];
	[TextFormatter setTextView: resultTextView];
	[Section setTextFormatter: TextFormatter];
	[TextFormatter release];

	prevRow = 0;
    }
  return self;
}

- (void) print: (id) sender {
        [[NSPrintOperation printOperationWithView: resultTextView] runOperation];
}

- (BOOL) loadFile: (NSString*) fileName 
{
    BOOL ret = NO;

    ASSIGN (handler, [HandlerStructureXLP new]);

    NSBundle* Bundle = [NSBundle bundleWithPath: fileName];
    [Section setBundle: Bundle];
    [handler setPath: [Bundle pathForResource: @"main" ofType: @"xlp"]];
    [handler parse];

/*
    string = [handler getPart: 0];
    Part* currentPage = [handler getPage: 0];

    if ([handler title] != nil)
    {
    	[window setTitle: [handler title]];
    }
    
    //NSLog (@"string : %@", string);
    //NSLog (@"currentPage : %@", currentPage);
    if ((string != nil) && (currentPage != nil))
    {
	[currentPage addSubviewsToView: resultTextView];
	[[resultTextView textStorage] setAttributedString: string];
	[resultOutlineView reloadData];
	ret = YES;
    }
    else 
    {
	NSLog (@"no parts !!!");
    }
 */
    NSLog (@"loadFile : %@", fileName);
    [resultOutlineView reloadColumn: 0];
    [resultOutlineView selectRow:0 inColumn:0];
    [self browserClick: resultOutlineView];
    
    return ret;
}

- (void) setWindow: (id) win { window = win; }

/*
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex
{
    BOOL ret = NO;
    //NSLog (@"shouldSelectRow : %d", rowIndex);
    if (handler != nil)
    {
	int nb = [[handler structuredText] count]; // nb of pages
	int i;
	int cur = 0;
	for (i=0; i<nb; i++)
	{
	    Part* page = [handler getPage: i];
	    int count = [[page sections] count];
	    if (cur == rowIndex) // Page 
	    {
		//ret = [[page title] string];
		NSAttributedString* string;
		string = [handler getPart: i];
		Part* currentPage = [handler getPage: prevRow];
		[currentPage removeSubviews];
		currentPage = [handler getPage: i];
		prevRow = i;
		[currentPage addSubviewsToView: resultTextView];

		[[resultTextView textStorage] setAttributedString: string];
		if ([[resultTextView textStorage] length] > 0)
		{
		    [resultTextView scrollRangeToVisible: 
			NSMakeRange (0, 1)];
		}
		//[aTableView reloadData];
		ret = YES;
		break;
	    }
	    if (cur + count >= rowIndex) // if rowIndex a section of the current page ...
	    {
		cur = rowIndex - cur - 1; // get the section's index

		//ret = [NSString stringWithFormat: @"    %@", [[[page sections] objectAtIndex: cur] header]];

		if (prevRow != i) // wrong page ... we must update
		{
		    NSAttributedString* string;
		    string = [handler getPart: i];
		    Part* currentPage = [handler getPage: prevRow];
		    [currentPage removeSubviews];
		    currentPage = [handler getPage: i];
		    prevRow = i;
		    [currentPage addSubviewsToView: resultTextView];

		    [[resultTextView textStorage] setAttributedString: string];
		}

		int length = [[resultTextView textStorage] length];
		if (length > 1)
		{
		    [resultTextView scrollRangeToVisible: 
			NSMakeRange (length - 1, 1)];
		}
			    
		[resultTextView scrollRangeToVisible: 
		    [[[page sections] objectAtIndex: cur] range]];

		ret = YES;
		break;
	    }
	    cur += count + 1;
	}

    }
    return ret;
}
*/

/*
- (void)tableView:(NSTableView*)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
    NSAttributedString* string;
    NSLog (@"didClickTableColumn (%@)", [tableColumn identifier]);
    if (handler != nil)
    {
	string = [handler getPart: [[tableColumn identifier] intValue]];
	[[[theWindow resultTextView] textStorage] setAttributedString: string];
	[tableView reloadData];
    }
}
*/

/*
-(void) tableView: (NSTableView *)tv willDisplayCell: (NSCell *)c forTableColumn: (NSTableColumn *)tc row: (int)rowIndex
{
    if (handler != nil)
    {
	int nb = [[handler structuredText] count]; // nb of pages
	int i;
	int cur = 0;
	for (i=0; i<nb; i++)
	{
	    Part* page = [handler getPage: i];
	    int count = [[page sections] count];
	    if (cur == rowIndex) // Page 
	    {
//		[c setFont: [NSFont boldSystemFontOfSize: 0]];
		break;
	    }
	    else
	    {
//		[c setFont: [NSFont systemFontOfSize: 0]];
	    }
	    cur += count + 1;
	}
    }
}


- (int) numberOfRowsInTableView: (NSTableView*) aTableView
{
    int ret = 0;
    if (handler != nil)
    {
	int nb = [[handler structuredText] count]; // nb of pages
	int i;
	for (i=0; i<nb; i++)
	{
	    Part* page = [handler getPage: i];
	    ret += [[page sections] count] + 1;
	}
    }
    return ret;
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
    //NSLog (@"tableView:objectValueForTableColumn:row:%d", rowIndex);
    id ret = nil; 
    
    if (handler != nil)
    {
	int nb = [[handler structuredText] count]; // nb of pages
	int i;
	int cur = 0;
	for (i=0; i<nb; i++)
	{
	    Part* page = [handler getPage: i];
	    int count = [[page sections] count];
	    if (cur == rowIndex) 
	    {
		//ret = [[page title] string];
		ret = [page title];
		break;
	    }
	    if (cur + count >= rowIndex) // if rowIndex a section of the current page ...
	    {
		cur = rowIndex - cur - 1; // get the section's index

		ret = [NSString stringWithFormat: @"    %@", [[[page sections] objectAtIndex: cur] header]];
		break;
	    }
	    cur += count + 1;
	}
	
	//NSLog (@"*** retour (%d) : %@", rowIndex, ret);
	[aTableColumn setIdentifier: [NSNumber numberWithInt: rowIndex]];
	//NSLog (@"identifieur mis");
    }
    return ret;
}
*/
/*
- (BOOL) textView: (NSTextView *) textView
    clickedOnLink: (id) link
              atIndex: (unsigned) charIndex
{
    BOOL ret = NO;
    if (handler != nil)
    {
	NSLog (@"clickedonlink !!!");
	if ([link isKindOfClass: [NSURL class]])
	{
	    NSLog(@"Opening URL : <%@>", [link description]);
	    ret = [[NSWorkspace sharedWorkspace] openURL: link];
	}
	else if ([link isKindOfClass: [NSString class]])
	{
	    NSLog (@"opening reference : %@", link);
	    Label* label = [handler getLabelForReference: link];
	    if ((label != nil) && ([label isKindOfClass: [Label class]]))
	    {
		NSLog (@"scrolling ...");
		NSLog (@"range to scroll : %d -> %d", [label range].location, [label range].length);
		Part* currentPage = [handler getPage: prevRow];
		[currentPage removeSubviews];
		currentPage = [label page];
		[currentPage addSubviewsToView: resultTextView];
		[[resultTextView textStorage] setAttributedString: [currentPage getPage]];		
		
		[resultTextView scrollRangeToVisible: 
		    [label range]];
	    }
	}
	NSLog (@"link : %@", link);
    }

    return ret;
}
*/

- (NSInteger)browser:(NSBrowser *)sender numberOfRowsInColumn:(NSInteger)column
{
  NSInteger ret = 0;

  if (column == 0) // First column
    {
      Section* section = [handler sections];
      ret = (NSInteger)[[section subs] count];
    }
  else
    {
      id cell = [sender selectedCellInColumn: column -1];
      ret = (NSInteger)[[(Section*)[cell section] subs] count];
    }

  return ret;
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell atRow:(NSInteger)row column:(NSInteger)column
{
	Section* sub = nil;
	[cell setLeaf: YES];

	//NSLog (@"browser:willDisplayCell:atRow:%dcolumn:%d",row,column);

	if (column == 0) // First column
	{
		Section* section = [handler sections];
		sub = [[section subs] objectAtIndex: row];
	}
	else
	{
		BrowserCell *cell = [sender selectedCellInColumn: column -1];
		sub = [[(Section *)[cell section] subs] objectAtIndex: row];
	}

	if (sub != nil)
	{
		id subs = [sub subs];
		if ((subs != nil) && ([subs count] > 0))
		{
			[cell setLeaf: NO];
		}
		[cell setSection: sub];
		[cell setStringValue: [sub header]];

		if ([sub loaded] == NO)
		{
			//NSLog (@"not loaded : %@", [sub header]);
			[cell setImage: [NSImage imageNamed: @"notloaded.tiff"]];
			//[cell setLeaf: NO];
			//[sub load];
		}
		else
		{
			if (([sub type] == SECTION_TYPE_PLAIN)
			 || ([sub type] == SECTION_TYPE_CHAPTER))
			{
				//NSLog (@"chapter : %@", [sub header]);
				[cell setImage: [NSImage imageNamed: @"chapter.tiff"]];
			}
			else if ([sub type] == SECTION_TYPE_PART)
			{
				//NSLog (@"chapter : %@", [sub header]);
				[cell setImage: [NSImage imageNamed: @"part.tiff"]];
			}
		}
		
		//NSLog (@"sub : %@", [sub header]);
	}
	else
	{
		//NSLog (@"sub == nil");
		[cell setStringValue: @"ERROR"];
	}
	//NSLog (@"fin de browser:willDisplayCell:atRow:%dcolumn:%d",row,column);
}

- (void) browserClick: (id) sender
{
  Section* sub = [(BrowserCell *)[sender selectedCell] section];

	if (sub != nil)
	{
		//NSLog (@"browserClick");
		if ([sub loaded] == NO)
		{
			[sub load];
    			[resultOutlineView reloadColumn: [resultOutlineView lastColumn]];
			[resultOutlineView selectRow: [resultOutlineView selectedRowInColumn: [resultOutlineView lastColumn]]
				inColumn: [resultOutlineView lastColumn]];
		}
			
		if (([sub type] == SECTION_TYPE_PLAIN)
		 || ([sub type] == SECTION_TYPE_CHAPTER))
		{
			// We have a "new" page, so we replace the entire text 
			// NSLog (@"on a une nouvelle page ...");
			id str = [sub contentWithLevel: 0];
			// NSLog (@"on a recu : %@ et on va le mettre dans le textview", str);
			[str retain];
			[[resultTextView textStorage] setAttributedString: str];
			[str release];
		}
		else if ([sub type] == SECTION_TYPE_NORMAL)
		{
			// We should select the right position in the textview
			// (ie, point the user to the right section)
		}
    		[resultOutlineView reloadColumn: [resultOutlineView lastColumn]];
	}
	//NSLog (@"FIN browserClick");
}
		   
- (void) dealloc
{
    NSLog (@"=== dealloc mainWindowController ===");
    RELEASE ((NSObject*)handler);
    RELEASE (resultTextView);
    RELEASE (resultOutlineView);
    [super dealloc];
}

@end