File: TerminalViewPrefs.m

package info (click to toggle)
terminal.app 0.9.4%2Bcvs20051125-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 456 kB
  • ctags: 106
  • sloc: objc: 6,097; makefile: 44; sh: 13
file content (715 lines) | stat: -rw-r--r-- 15,630 bytes parent folder | download | duplicates (2)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
copyright 2002, 2003 Alexander Malmberg <alexander@malmberg.org>

This file is a part of Terminal.app. Terminal.app 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; version 2
of the License. See COPYING or main.m for more information.
*/

#include <Foundation/NSNotification.h>
#include <Foundation/NSString.h>
#include <Foundation/NSUserDefaults.h>
#include <AppKit/NSBox.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSColorPanel.h>
#include <AppKit/NSColorWell.h>
#include <AppKit/NSFont.h>
#include <AppKit/NSGraphics.h>
#include <AppKit/NSImage.h>
#include <AppKit/NSTextField.h>
#include <GNUstepGUI/GSVbox.h>
#include <GNUstepGUI/GSHbox.h>
#include "Label.h"

#include "TerminalViewPrefs.h"


NSString *TerminalViewDisplayPrefsDidChangeNotification=
	@"TerminalViewDisplayPrefsDidChangeNotification";

static NSUserDefaults *ud;


static NSString
	*TerminalFontKey=@"TerminalFont",
	*TerminalFontSizeKey=@"TerminalFontSize",
	*BoldTerminalFontKey=@"BoldTerminalFont",
	*BoldTerminalFontSizeKey=@"BoldTerminalFontSize",
	*UseMultiCellGlyphsKey=@"UseMultiCellGlyphs",
	*CursorStyleKey=@"CursorStyle",
	*ScrollBackLinesKey=@"ScrollBackLines",

	*CursorColorRKey=@"CursorColorR",
	*CursorColorGKey=@"CursorColorG",
	*CursorColorBKey=@"CursorColorB",
	*CursorColorAKey=@"CursorColorA";


static NSFont *terminalFont,*boldTerminalFont;

static BOOL useMultiCellGlyphs;

static float brightness[3]={0.6,0.8,1.0};
static float saturation[3]={1.0,1.0,0.75};

static int cursorStyle;
static NSColor *cursorColor;

static int scrollBackLines;


@implementation TerminalViewDisplayPrefs

+(void) initialize
{
	if (!ud)
		ud=[NSUserDefaults standardUserDefaults];

	if (!cursorColor)
	{
		NSString *s;
		float size;


		size=[ud floatForKey: TerminalFontSizeKey];
		s=[ud stringForKey: TerminalFontKey];
		if (!s)
			terminalFont=[[NSFont userFixedPitchFontOfSize: size] retain];
		else
		{
			terminalFont=[[NSFont fontWithName: s  size: size] retain];
			if (!terminalFont)
				terminalFont=[[NSFont userFixedPitchFontOfSize: size] retain];
		}

		size=[ud floatForKey: BoldTerminalFontSizeKey];
		s=[ud stringForKey: BoldTerminalFontKey];
		if (!s)
			boldTerminalFont=[[NSFont userFixedPitchFontOfSize: size] retain];
		else
		{
			boldTerminalFont=[[NSFont fontWithName: s  size: size] retain];
			if (!boldTerminalFont)
				boldTerminalFont=[[NSFont userFixedPitchFontOfSize: size] retain];
		}

		useMultiCellGlyphs=[ud boolForKey: UseMultiCellGlyphsKey];

		cursorStyle=[ud integerForKey: CursorStyleKey];
		if ([ud objectForKey: CursorColorRKey])
		{
			float r,g,b,a;
			r=[ud floatForKey: CursorColorRKey];
			g=[ud floatForKey: CursorColorGKey];
			b=[ud floatForKey: CursorColorBKey];
			a=[ud floatForKey: CursorColorAKey];
			cursorColor=[[NSColor colorWithCalibratedRed: r
				green: g
				blue: b
				alpha: a] retain];
		}
		else
		{
			cursorColor=[[NSColor whiteColor] retain];
		}

		scrollBackLines=[ud integerForKey: ScrollBackLinesKey];
		if (scrollBackLines<=0)
			scrollBackLines=256;
	}
}

+(NSFont *) terminalFont
{
	NSFont *f=[terminalFont screenFont];
	if (f)
		return f;
	return terminalFont;
}

+(NSFont *) boldTerminalFont
{
	NSFont *f=[boldTerminalFont screenFont];
	if (f)
		return f;
	return boldTerminalFont;
}

+(BOOL) useMultiCellGlyphs
{
	return useMultiCellGlyphs;
}

+(const float *) brightnessForIntensities
{
	return brightness;
}
+(const float *) saturationForIntensities
{
	return saturation;
}

+(int) cursorStyle
{
	return cursorStyle;
}

+(NSColor *) cursorColor
{
	return cursorColor;
}

+(int) scrollBackLines
{
	return scrollBackLines;
}


-(void) save
{
	if (!top) return;

	cursorStyle=[[m_cursorStyle selectedCell] tag];
	[ud setInteger: cursorStyle
		forKey: CursorStyleKey];

	{
		DESTROY(cursorColor);
		cursorColor=[w_cursorColor color];
		cursorColor=[[cursorColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace] retain];
		[ud setFloat: [cursorColor redComponent]
			forKey: CursorColorRKey];
		[ud setFloat: [cursorColor greenComponent]
			forKey: CursorColorGKey];
		[ud setFloat: [cursorColor blueComponent]
			forKey: CursorColorBKey];
		[ud setFloat: [cursorColor alphaComponent]
			forKey: CursorColorAKey];
	}

	ASSIGN(terminalFont,[f_terminalFont font]);
	[ud setFloat: [terminalFont pointSize]
		forKey: TerminalFontSizeKey];
	[ud setObject: [terminalFont fontName]
		forKey: TerminalFontKey];

	ASSIGN(boldTerminalFont,[f_boldTerminalFont font]);
	[ud setFloat: [boldTerminalFont pointSize]
		forKey: BoldTerminalFontSizeKey];
	[ud setObject: [boldTerminalFont fontName]
		forKey: BoldTerminalFontKey];

	scrollBackLines=[f_scrollBackLines intValue];
	[ud setInteger: scrollBackLines
		forKey: ScrollBackLinesKey];

	useMultiCellGlyphs=!![b_useMultiCellGlyphs state];
	[ud setBool: useMultiCellGlyphs
		forKey: UseMultiCellGlyphsKey];

	[[NSNotificationCenter defaultCenter]
		postNotificationName: TerminalViewDisplayPrefsDidChangeNotification
		object: self];
}

-(void) revert
{
	NSFont *f;

	[b_useMultiCellGlyphs setState: useMultiCellGlyphs];

	[m_cursorStyle selectCellWithTag: [[self class] cursorStyle]];
	[w_cursorColor setColor: [[self class] cursorColor]];

	f=[isa terminalFont];
	[f_terminalFont setStringValue: [NSString stringWithFormat: @"%@ %0.1f",[f fontName],[f pointSize]]];
	[f_terminalFont setFont: f];

	f=[isa boldTerminalFont];
	[f_boldTerminalFont setStringValue: [NSString stringWithFormat: @"%@ %0.1f",[f fontName],[f pointSize]]];
	[f_boldTerminalFont setFont: f];

	[f_scrollBackLines setIntValue: scrollBackLines];
}


-(NSString *) name
{
	return _(@"Display");
}

-(void) setupButton: (NSButton *)b
{
	[b setTitle: _(@"Display")];
	[b sizeToFit];
}

-(void) willHide
{
}

-(NSView *) willShow
{
	if (!top)
	{
		top=[[GSVbox alloc] init];
		[top setDefaultMinYMargin: 2];

		[top addView: [[[NSView alloc] init] autorelease] enablingYResizing: YES];

		{
			NSTextField *f;
			NSButton *b;
			GSHbox *hb;

			hb=[[GSHbox alloc] init];
			[hb setDefaultMinXMargin: 4];
			[hb setAutoresizingMask: NSViewWidthSizable];

			f=[NSTextField newLabel: _(@"Scroll-back length in lines:")];
			[f setAutoresizingMask: 0];
			[hb addView: f  enablingXResizing: NO];
			DESTROY(f);

			f_scrollBackLines=f=[[NSTextField alloc] init];
			[f setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
			[f sizeToFit];
			[hb addView: f  enablingXResizing: YES];
			DESTROY(f);

			[top addView: hb enablingYResizing: NO];
			DESTROY(hb);

			[top addView: [[[NSView alloc] init] autorelease] enablingYResizing: YES];

			{
				NSBox *b;
				GSTable *t;
				NSColorWell *w;

				b=[[NSBox alloc] init];
				[b setAutoresizingMask: NSViewMinXMargin|NSViewMaxXMargin];
				[b setTitle: _(@"Cursor")];

				t=[[GSTable alloc] initWithNumberOfRows: 2 numberOfColumns: 2];
				[t setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];

				f=[NSTextField newLabel: _(@"Style:")];
				[f setAutoresizingMask: NSViewMinXMargin|NSViewMinYMargin|NSViewMaxYMargin];
				[t putView: f  atRow: 0 column: 0  withXMargins: 2 yMargins: 2];
				DESTROY(f);

				{
					NSMatrix *m;
					NSButtonCell *b=[NSButtonCell new];
					NSSize s;

					[b setImagePosition: NSImageOnly];
					[b setHighlightsBy: NSChangeBackgroundCellMask];
					[b setShowsStateBy: NSChangeBackgroundCellMask];

					m=m_cursorStyle=[[NSMatrix alloc] initWithFrame: NSMakeRect(0,0,1,1)
						mode: NSRadioModeMatrix
						prototype: b
						numberOfRows: 1
						numberOfColumns: 4];

					[[m cellAtRow: 0 column: 0] setImage: [NSImage imageNamed: @"cursor_line"]];
					[[m cellAtRow: 0 column: 1] setImage: [NSImage imageNamed: @"cursor_stroked"]];
					[[m cellAtRow: 0 column: 2] setImage: [NSImage imageNamed: @"cursor_filled"]];
					[[m cellAtRow: 0 column: 3] setImage: [NSImage imageNamed: @"cursor_inverted"]];
					[[m cellAtRow: 0 column: 0] setTag: 0];
					[[m cellAtRow: 0 column: 1] setTag: 1];
					[[m cellAtRow: 0 column: 2] setTag: 2];
					[[m cellAtRow: 0 column: 3] setTag: 3];

					s=[[m cellAtRow: 0 column: 0] cellSize];
					s.width+=6;
					s.height+=6;
					[m setCellSize: s];
					[m sizeToCells];

					[t putView: m  atRow: 0 column: 1  withXMargins: 2 yMargins: 2];
					DESTROY(m);
				}


				f=[NSTextField newLabel: _(@"Color:")];
				[f setAutoresizingMask: NSViewMinXMargin|NSViewMinYMargin|NSViewMaxYMargin];
				[t putView: f  atRow: 1 column: 0  withXMargins: 2 yMargins: 2];
				DESTROY(f);

				w_cursorColor=w=[[NSColorWell alloc] initWithFrame: NSMakeRect(0,0,40,30)];
				[t putView: w  atRow: 1 column: 1  withXMargins: 2 yMargins: 2];
				DESTROY(w);

				[[NSColorPanel sharedColorPanel] setShowsAlpha: YES];


				[t sizeToFit];
				[b setContentView: t];
				[b sizeToFit];
				[top addView: b enablingYResizing: NO];
				DESTROY(b);
			}

			[top addView: [[[NSView alloc] init] autorelease] enablingYResizing: YES];


			b=b_useMultiCellGlyphs=[[NSButton alloc] init];
			[b setTitle: _(@"Handle wide (multi-cell) glyphs")];
			[b setButtonType: NSSwitchButton];
			[b sizeToFit];
			[top addView: b enablingYResizing: NO];
			DESTROY(b);


			hb=[[GSHbox alloc] init];
			[hb setDefaultMinXMargin: 4];
			[hb setAutoresizingMask: NSViewWidthSizable];

			f=[NSTextField newLabel: _(@"Bold font:")];
			[f setAutoresizingMask: 0];
			[hb addView: f  enablingXResizing: NO];
			DESTROY(f);

			f_boldTerminalFont=f=[[NSTextField alloc] init];
			[f setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
			[f setEditable: NO];
			[hb addView: f  enablingXResizing: YES];
			DESTROY(f);

			b=[[NSButton alloc] init];
			[b setTitle: _(@"Pick font...")];
			[b setTarget: self];
			[b setAction: @selector(_pickBoldTerminalFont:)];
			[b sizeToFit];
			[hb addView: b  enablingXResizing: NO];
			DESTROY(b);

			[top addView: hb enablingYResizing: NO];
			DESTROY(hb);


			hb=[[GSHbox alloc] init];
			[hb setDefaultMinXMargin: 4];
			[hb setAutoresizingMask: NSViewWidthSizable];

			f=[NSTextField newLabel: _(@"Normal font:")];
			[f setAutoresizingMask: 0];
			[hb addView: f  enablingXResizing: NO];
			DESTROY(f);

			f_terminalFont=f=[[NSTextField alloc] init];
			[f setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
			[f setEditable: NO];
			[hb addView: f  enablingXResizing: YES];
			DESTROY(f);

			b=[[NSButton alloc] init];
			[b setTitle: _(@"Pick font...")];
			[b setTarget: self];
			[b setAction: @selector(_pickTerminalFont:)];
			[b sizeToFit];
			[hb addView: b  enablingXResizing: NO];
			DESTROY(b);

			[top addView: hb enablingYResizing: NO];
			DESTROY(hb);


			[top addView: [[[NSView alloc] init] autorelease] enablingYResizing: YES];
		}

		[self revert];
	}
	return top;
}

-(void) dealloc
{
	DESTROY(top);
	[super dealloc];
}


-(void) _pickFont
{
	NSFontManager *fm=[NSFontManager sharedFontManager];
	[fm setSelectedFont: [f_cur font] isMultiple: NO];
	[fm orderFrontFontPanel: self];
}

-(void) _pickTerminalFont: (id)sender
{
	f_cur=f_terminalFont;
	[self _pickFont];
}

-(void) _pickBoldTerminalFont: (id)sender
{
	f_cur=f_boldTerminalFont;
	[self _pickFont];
}

/*
TODO: The return type here should be (void), but due to forwarding issues in
-base, it has to be (id) to avoid a return type mismatch error
*/
-(id) changeFont: (id)sender
{
	NSFont *f;

	if (!f_cur) return nil;
	f=[sender convertFont: [f_cur font]];
	if (!f) return nil;

	[f_cur setStringValue: [NSString stringWithFormat: @"%@ %0.1f",[f fontName],[f pointSize]]];
	[f_cur setFont: f];

	return nil;
}

@end


static NSString
	*LoginShellKey=@"LoginShell",
	*ShellKey=@"Shell";

static NSString *shell;
static BOOL loginShell;

@implementation TerminalViewShellPrefs

+(void) initialize
{
	if (!ud)
		ud=[NSUserDefaults standardUserDefaults];

	if (!shell)
	{
		loginShell=[ud boolForKey: LoginShellKey];
		shell=[ud stringForKey: ShellKey];
		if (!shell && getenv("SHELL"))
			shell=[NSString stringWithCString: getenv("SHELL")];
		if (!shell)
			shell=@"/bin/sh";
		shell=[shell retain];
	}
}

+(NSString *) shell
{
	return shell;
}

+(BOOL) loginShell
{
	return loginShell;
}


-(void) save
{
	if (!top) return;

	if ([b_loginShell state])
		loginShell=YES;
	else
		loginShell=NO;
	[ud setBool: loginShell forKey: LoginShellKey];

	DESTROY(shell);
	shell=[[tf_shell stringValue] copy];
	[ud setObject: shell forKey: ShellKey];
}

-(void) revert
{
	[b_loginShell setState: loginShell];
	[tf_shell setStringValue: shell];
}


-(NSString *) name
{
	return _(@"Shell");
}

-(void) setupButton: (NSButton *)b
{
	[b setTitle: _(@"Shell")];
	[b sizeToFit];
}

-(void) willHide
{
}

-(NSView *) willShow
{
	if (!top)
	{
		top=[[GSVbox alloc] init];
		[top setDefaultMinYMargin: 4];

		{
			NSTextField *f;
			NSButton *b;

			b=b_loginShell=[[NSButton alloc] init];
			[b setAutoresizingMask: NSViewMinYMargin];
			[b setTitle: _(@"Start as login-shell")];
			[b setButtonType: NSSwitchButton];
			[b sizeToFit];
			[top addView: b enablingYResizing: YES];
			DESTROY(b);

			tf_shell=f=[[NSTextField alloc] init];
			[f sizeToFit];
			[f setAutoresizingMask: NSViewWidthSizable];
			[top addView: f enablingYResizing: NO];
			DESTROY(f);

			f=[NSTextField newLabel: _(@"Shell:")];
			[f setAutoresizingMask: NSViewMaxYMargin];
			[f sizeToFit];
			[top addView: f enablingYResizing: YES];
			DESTROY(f);
		}

		[self revert];
	}
	return top;
}

-(void) dealloc
{
	DESTROY(top);
	[super dealloc];
}

@end


static NSString
	*CommandAsMetaKey=@"CommandAsMeta",
	*DoubleEscapeKey=@"DoubleEscape";

static BOOL commandAsMeta,doubleEscape;

@implementation TerminalViewKeyboardPrefs

+(void) initialize
{
	if (!ud)
		ud=[NSUserDefaults standardUserDefaults];

	commandAsMeta=[ud boolForKey: CommandAsMetaKey];
	doubleEscape=[ud boolForKey: DoubleEscapeKey];
}

+(BOOL) commandAsMeta
{
	return commandAsMeta;
}

+(BOOL) doubleEscape
{
	return doubleEscape;
}


-(void) save
{
	if (!top) return;

	if ([b_commandAsMeta state])
		commandAsMeta=YES;
	else
		commandAsMeta=NO;
	[ud setBool: commandAsMeta forKey: CommandAsMetaKey];

	if ([b_doubleEscape state])
		doubleEscape=YES;
	else
		doubleEscape=NO;
	[ud setBool: doubleEscape forKey: DoubleEscapeKey];
}

-(void) revert
{
	[b_commandAsMeta setState: commandAsMeta];
	[b_doubleEscape setState: doubleEscape];
}


-(NSString *) name
{
	return _(@"Keyboard");
}

-(void) setupButton: (NSButton *)b
{
	[b setTitle: _(@"Keyboard")];
	[b sizeToFit];
}

-(void) willHide
{
}

-(NSView *) willShow
{
	if (!top)
	{
		top=[[GSVbox alloc] init];
		[top setDefaultMinYMargin: 8];

		{
			NSButton *b;

			b=b_commandAsMeta=[[NSButton alloc] init];
			[b setAutoresizingMask: NSViewMinYMargin|NSViewMaxYMargin|NSViewWidthSizable];
			[b setTitle:
				_(@"Treat the command key as meta.\n"
				  @"\n"
				  @"Note that with this enabled, you won't be\n"
				  @"able to access menu entries with the\n"
				  @"keyboard.")];
			[b setButtonType: NSSwitchButton];
			[b sizeToFit];
			[top addView: b enablingYResizing: YES];
			DESTROY(b);

			[top addSeparator];

			b=b_doubleEscape=[[NSButton alloc] init];
			[b setAutoresizingMask: NSViewMinYMargin|NSViewMaxYMargin|NSViewWidthSizable];
			[b setTitle:
				_(@"Send a double escape for the escape key.\n"
				  @"\n"
				  @"This means that the escape key will be\n"
				  @"recognized faster by many programs, but\n"
				  @"you can't use it as a substitute for meta.")];
			[b setButtonType: NSSwitchButton];
			[b sizeToFit];
			[top addView: b enablingYResizing: YES];
			DESTROY(b);
		}

		[self revert];
	}
	return top;
}

-(void) dealloc
{
	DESTROY(top);
	[super dealloc];
}

@end