-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThemeDocument.m
1793 lines (1610 loc) · 45.4 KB
/
ThemeDocument.m
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ThemeDocument.m
*
* Copyright (C) 2006 Free Software Foundation, Inc.
*
* Author: Richard Frith-Macdonald <[email protected]>
* Date: 2006
*
* This file is part of GNUstep.
*
* 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 02111 USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepGUI/GSTheme.h>
#import <AppController.h>
#import "ThemeDocument.h"
#import "ThemeElement.h"
#import "ControlElement.h"
#import "ColorElement.h"
#import "ImageElement.h"
#import "MenuItemElement.h"
#import "MenusElement.h"
#import "MiscElement.h"
#import "PreviewElement.h"
#import "WindowsElement.h"
@interface GSTheme (TestTheme)
@end
@implementation GSTheme (TestTheme)
/*
* Method to prevent the current theme being set from the defaults system
* because we ant to set it directly from the current document.
* However, if there are no documents, we do want to use the correc theme.
*/
+ (void) defaultsDidChange: (NSNotification*)n
{
if ([[[AppController sharedController] documents] count] == 0)
{
NSUserDefaults *defs;
NSString *name;
defs = [NSUserDefaults standardUserDefaults];
name = [defs stringForKey: @"GSTheme"];
if ([name isEqual: [[GSTheme theme] name]] == NO)
{
[GSTheme setTheme: [GSTheme loadThemeNamed: name]];
}
}
}
/*
* Method to bypass NSBundle's caching of infoDictionary so that changes
* to the theme will be reflected immediately.
*/
- (NSDictionary*) infoDictionary
{
ThemeDocument *doc = [thematicController selectedDocument];
if ([doc testTheme] == self)
{
NSString *path;
path = [[self bundle] pathForResource: @"Info-gnustep" ofType: @"plist"];
return [NSDictionary dictionaryWithContentsOfFile: path];
}
else
{
return [[self bundle] infoDictionary];
}
}
@end
static NSMutableSet *untitledName = nil;
static NSColorList *systemColorList = nil;
@interface ThemeDocumentView : NSView
{
ThemeDocument *owner; // Not retained
}
- (void) setOwner: (ThemeDocument*)o;
@end
@implementation ThemeDocumentView
- (BOOL) acceptsFirstMouse: (NSEvent*)theEvent
{
return YES; /* Ensure we get initial mouse down event. */
}
/*
* Initialisation - register to receive DnD for colors and images.
*/
- (id) initWithFrame: (NSRect)aFrame
{
self = [super initWithFrame: aFrame];
if (self != nil)
{
[self registerForDraggedTypes: [NSArray arrayWithObjects:
NSColorPboardType,
NSFileContentsPboardType,
NSFilenamesPboardType,
nil]];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
/*
* Dragging destination protocol implementation
*/
- (NSDragOperation) draggingEntered: (id<NSDraggingInfo>)sender
{
return NSDragOperationCopy;;
}
- (BOOL) performDragOperation: (id<NSDraggingInfo>)sender
{
return YES;
}
- (BOOL) prepareForDragOperation: (id<NSDraggingInfo>)sender
{
return YES;
}
- (void) helpRequested: (NSEvent*)theEvent
{
NSPoint loc = [theEvent locationInWindow];
NSView *hit = [super hitTest: loc];
if (hit != nil)
{
while (hit != self)
{
if ([[NSHelpManager sharedHelpManager]
showContextHelpForObject: hit
locationHint: loc] == YES)
{
return; // Found/
}
hit = [hit superview];
}
}
[super helpRequested: theEvent];
}
/*
* Intercepting events in the view and handling them
*/
- (NSView*) hitTest: (NSPoint)loc
{
/*
* Stop the subviews receiving events - we grab them all.
*/
if ([super hitTest: loc] != nil)
return self;
return nil;
}
- (void) mouseDown: (NSEvent*)theEvent
{
NSPoint mousePoint = [theEvent locationInWindow];
NSView *view;
view = [super hitTest: mousePoint];
if (view == self)
{
view = nil;
}
/* Make sure we've found the proper control and not a subview of a
* control (like the contentView of an NSBox)
* However, sometimes we want to make special cases like finding a
* scroller in a scrollview.
*/
while (view != nil && [view superview] != self)
{
if ([view isKindOfClass: [NSScroller class]]
&& [[view superview] isKindOfClass: [NSScrollView class]])
{
break; // Handle scrollers separately from scrollview
}
view = [view superview];
}
if (view != nil)
{
mousePoint = [view convertPoint: mousePoint fromView: nil];
[owner changeSelection: view at: mousePoint];
}
}
- (void) setOwner: (ThemeDocument*)o
{
owner = o;
}
@end
@implementation ThemeDocument
+ (void) initialize
{
untitledName = [NSMutableSet new];
systemColorList = RETAIN([NSColorList colorListNamed: @"System"]);
}
- (void) activate
{
/* Tell the system that our theme is now active as the current theme.
* If we are already active, we deactivate first so that the new
* activation can take effect cleanly. If we are not the current
* theme, we just need to set ourselves to be it.
*/
if ([GSTheme theme] == _theme)
{
[_theme deactivate];
[_theme activate];
}
else
{
[GSTheme setTheme: _theme];
}
[_selected selectAt: _selectionPoint];
}
- (NSDictionary*) applicationImageNames
{
NSFileManager *mgr;
NSString *pathOfTI;
NSString *path1;
NSEnumerator *enumerator1;
NSMutableDictionary *images;
/*
We may have top-level images in ThemeImages. These are common images.
We may have then subdirectories with bundle identifieres.
We loop twice and scan only subdirectories.
We want to avoid hidden subdirs (like those created by version control systems).
*/
mgr = [NSFileManager defaultManager];
pathOfTI = [_rsrc stringByAppendingPathComponent: @"ThemeImages"];
enumerator1 = [[mgr directoryContentsAtPath: pathOfTI] objectEnumerator];
images = [NSMutableDictionary dictionary];
while ((path1 = [enumerator1 nextObject]) != nil)
{
BOOL isDir1;
NSString *fileOrDir1;
fileOrDir1 = [pathOfTI stringByAppendingPathComponent:path1];
if (![path1 hasPrefix:@"."])
{
if ([mgr fileExistsAtPath:fileOrDir1 isDirectory:&isDir1] && isDir1)
{
NSString *path2;
NSEnumerator *enumerator2;
NSMutableArray *array;
array = [NSMutableArray array];
[images setObject: array forKey:path1];
enumerator2 = [[mgr directoryContentsAtPath: fileOrDir1] objectEnumerator];
while ((path2 = [enumerator2 nextObject]) != nil)
{
BOOL isDir2;
NSString *fileOrDir2;
fileOrDir2 = [fileOrDir1 stringByAppendingPathComponent:path2];
if (![path2 hasPrefix:@"."])
{
if ([mgr fileExistsAtPath:fileOrDir2 isDirectory:&isDir2] && !isDir2)
{
[array addObject: path2];
}
}
}
}
else
{
// Here we would have images in the root (common)
}
}
}
return images;
}
- (NSString*) buildDirectory
{
return _build;
}
- (void) changeSelection: (NSView*)aView at: (NSPoint)mousePoint
{
ThemeElement *e = [self elementForView: aView];
AppController *c = [AppController sharedController];
/*
* Make sure that this is the 'selected' document and that the theme it
* is editing is active for preview purposes.
*/
[c selectDocument: self];
if (aView == nil)
{
[_selected deselect];
_selected = nil;
_selectionPoint = NSZeroPoint;
}
else
{
if (e != _selected)
{
/* Change selected view.
*/
[_selected deselect];
_selected = e;
}
/* Change selection point in view.
*/
_selectionPoint = mousePoint;
[e selectAt: _selectionPoint];
[[c inspector] orderFront: self];
}
}
- (void) close
{
/* Remove our information from the inspector
*/
[_selected deselect];
_selected = nil;
_selectionPoint = NSZeroPoint;
/* Remove our temporary work area.
*/
if (_work != nil)
{
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr removeFileAtPath: _work handler: nil];
DESTROY(_work);
}
/* Remove self from app controller
*/
[self retain];
[[AppController sharedController] removeDocument: self];
if (_theme != nil)
{
if ([GSTheme theme] == _theme
&& [[[AppController sharedController] documents] count] == 0)
{
[GSTheme setTheme: nil];
}
DESTROY(_theme);
}
[self release];
}
- (NSString*) codeForKey: (NSString*)key since: (NSDate**)since
{
NSFileManager *mgr;
NSString *code = nil;
NSString *file;
key = [key stringByReplacingString: @":" withString: @"_"];
if (since != 0)
{
*since = [_modified objectForKey: key];
}
file = [_rsrc stringByAppendingPathComponent: @"ThemeCode"];
file = [file stringByAppendingPathComponent: key];
mgr = [NSFileManager defaultManager];
if ([mgr isReadableFileAtPath: file] == YES)
{
code = [NSString stringWithContentsOfFile: file];
}
return code;
}
- (NSColor*) colorForKey: (NSString*)aName
{
return [_colors colorWithKey: aName];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[self close];
[window setDelegate: nil];
RELEASE(_modified);
RELEASE(_elements);
RELEASE(_colors);
RELEASE(_extraColors[GSThemeNormalState]);
RELEASE(_extraColors[GSThemeHighlightedState]);
RELEASE(_extraColors[GSThemeSelectedState]);
if (_name != nil)
{
[untitledName removeObject: _name];
RELEASE(_name);
}
RELEASE(_path);
RELEASE(_work);
RELEASE(_rsrc);
RELEASE(_info);
[super dealloc];
}
- (NSString*) defaultForKey: (NSString*)key
{
return [_defs objectForKey: key];
}
- (ThemeElement*) elementForView: (NSView*)aView
{
ThemeElement *e;
unsigned i;
if (aView == nil)
{
return nil;
}
/* Look through the existing elements and if we already have one for this
* view, return it.
*/
i = [_elements count];
while (i-- > 0)
{
e = [_elements objectAtIndex: i];
if (aView == [e view])
{
return e;
}
}
e = nil;
if ([aView isKindOfClass: [NSImageView class]] == YES)
{
/* This should be one of the images at the top of the main panel ...
* We allocate a specific element depending on which image it is.
*/
if (aView == colorsView)
{
e = [[ColorElement alloc] initWithView: aView owner: self];
}
else if (aView == imagesView)
{
e = [[ImageElement alloc] initWithView: aView owner: self];
}
else if (aView == menusView)
{
e = [[MenusElement alloc] initWithView: aView owner: self];
}
else if (aView == extraView)
{
e = [[MiscElement alloc] initWithView: aView owner: self];
}
else if (aView == previewView)
{
e = [[PreviewElement alloc] initWithView: aView owner: self];
}
else if (aView == windowsView)
{
e = [[WindowsElement alloc] initWithView: aView owner: self];
}
else if (aView == menuItemView)
{
e = [[MenuItemElement alloc] initWithView: aView owner: self];
}
else
{
NSLog(@"Unknown image view found");
}
}
else
{
/* Here we create an element for a specific class of control.
*/
if ([aView isKindOfClass: [NSButton class]])
{
/* We could have a subclass of ControlElement to handle button
* specific details, but as a button is a simple gui element
* we can get away with using the ControlElement class directly
*/
e = [[ControlElement alloc] initWithView: aView
owner: self];
}
else if ([aView isKindOfClass: [NSScroller class]])
{
/* Perhaps this is worth a subclass, but we just have a hack in
* the -selectAt: method to do all we need.
*/
e = [[ControlElement alloc] initWithView: aView
owner: self];
}
else
{
/* At this point we just assume that the ControlElement class is
* able to handle whatever class we actually have in a generic
* manner using information from the Resources/CodeInfo.plist
* definitions. If there's no data in there, we will raise an
* alert panel later.
*/
e = [[ControlElement alloc] initWithView: aView
owner: self];
}
}
if (e != nil)
{
[_elements addObject: e];
RELEASE(e);
}
return e;
}
- (NSColor*) extraColorForKey: (NSString*)aName
{
GSThemeControlState state = GSThemeNormalState;
if ([aName hasSuffix: @"Highlighted"] == YES)
{
state = GSThemeHighlightedState;
aName = [aName substringToIndex: [aName length] - 11];
}
else if ([aName hasSuffix: @"Selected"] == YES)
{
state = GSThemeSelectedState;
aName = [aName substringToIndex: [aName length] - 8];
}
return [_extraColors[state] colorWithKey: aName];
}
- (NSImage*) imageForKey: (NSString*)aKey
{
NSImage *image;
NSString *path;
path = [_rsrc stringByAppendingPathComponent: @"ThemeImages"];
path = [path stringByAppendingPathComponent: aKey];
image = AUTORELEASE([[NSImage alloc] initWithContentsOfFile: path]);
return image;
}
- (NSDictionary*) infoDictionary
{
return AUTORELEASE([_info copy]);
}
- (id) init
{
return [self initWithPath: nil];
}
- (id) initWithPath: (NSString*)path
{
CREATE_AUTORELEASE_POOL(pool);
NSFileManager *mgr = [NSFileManager defaultManager];
static int sequence = 0;
NSRect frame;
NSView *old;
NSEnumerator *enumerator;
ThemeDocumentView *view;
NSArray *keys;
NSString *pi;
NSString *s;
unsigned i;
BOOL isDir;
/* Timestamps of modified code fragments.
*/
_modified = [NSMutableDictionary new];
/*
* Create a working directory for temporary storage.
*/
pi = [NSString stringWithFormat: @"%d_%d_0",
[[NSProcessInfo processInfo] processIdentifier], ++sequence];
pi = [pi stringByAppendingPathExtension: @"theme"];
_work = RETAIN([NSTemporaryDirectory() stringByAppendingPathComponent: pi]);
if (path != nil)
{
if ([mgr copyPath: path toPath: _work handler: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to load theme into work area from %@"),
nil, nil, nil, path);
return nil;
}
}
if ([mgr fileExistsAtPath: _work isDirectory: &isDir] == NO || isDir == NO)
{
if ([mgr createDirectoryAtPath: _work attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create working directory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
}
_build = RETAIN([_work stringByAppendingPathComponent: @"Build"]);
if ([mgr fileExistsAtPath: _build isDirectory: &isDir] == YES)
{
if ([mgr removeFileAtPath: _build handler: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to remove old Build directory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
}
if ([mgr createDirectoryAtPath: _build attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create Build directory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
_rsrc = RETAIN([_work stringByAppendingPathComponent: @"Resources"]);
if ([mgr fileExistsAtPath: _rsrc isDirectory: &isDir] == NO || isDir == NO)
{
if ([mgr createDirectoryAtPath: _rsrc attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create Resources directory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
}
s = [_rsrc stringByAppendingPathComponent: @"ThemeImages"];
if ([mgr fileExistsAtPath: s isDirectory: &isDir] == NO || isDir == NO)
{
if ([mgr createDirectoryAtPath: s attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create working images subdirectory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
}
s = [_rsrc stringByAppendingPathComponent: @"ThemeCode"];
if ([mgr fileExistsAtPath: s isDirectory: &isDir] == NO || isDir == NO)
{
if ([mgr createDirectoryAtPath: s attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create source code subdirectory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
}
s = [_rsrc stringByAppendingPathComponent: @"ThemeTiles"];
if ([mgr fileExistsAtPath: s isDirectory: &isDir] == NO || isDir == NO)
{
if ([mgr createDirectoryAtPath: s attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create working tiles subdirectory for theme"),
_(@"OK"), nil, nil);
DESTROY(self);
return nil;
}
}
_elements = [NSMutableArray new];
if (path != nil)
{
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *file;
file = [path stringByAppendingPathComponent: @"Resources"];
file = [file stringByAppendingPathComponent: @"ThemeColors.clr"];
if ([mgr isReadableFileAtPath: file] == YES)
{
_colors = [[NSColorList alloc] initWithName: @"System"
fromFile: file];
}
file = [path stringByAppendingPathComponent: @"Resources"];
file = [file stringByAppendingPathComponent: @"ThemeExtraColors.clr"];
if ([mgr isReadableFileAtPath: file] == YES)
{
_extraColors[GSThemeNormalState]
= [[NSColorList alloc] initWithName: @"ThemeExtra"
fromFile: file];
}
file = [path stringByAppendingPathComponent: @"Resources"];
file = [file stringByAppendingPathComponent:
@"ThemeExtraHighlightedColors.clr"];
if ([mgr isReadableFileAtPath: file] == YES)
{
_extraColors[GSThemeHighlightedState]
= [[NSColorList alloc] initWithName: @"ThemeExtraHighlighted"
fromFile: file];
}
file = [path stringByAppendingPathComponent: @"Resources"];
file = [file stringByAppendingPathComponent:
@"ThemeExtraSelectedColors.clr"];
if ([mgr isReadableFileAtPath: file] == YES)
{
_extraColors[GSThemeSelectedState]
= [[NSColorList alloc] initWithName: @"ThemeExtraSelected"
fromFile: file];
}
file = [path stringByAppendingPathComponent: @"Resources"];
file = [file stringByAppendingPathComponent: @"Info-gnustep.plist"];
if ([mgr isReadableFileAtPath: file] == YES)
{
_info = [[NSMutableDictionary alloc] initWithContentsOfFile: file];
}
}
if (_colors == nil)
{
_colors = [[NSColorList alloc] initWithName: @"System"
fromFile: nil];
}
if (_extraColors[GSThemeNormalState] == nil)
{
_extraColors[GSThemeNormalState]
= [[NSColorList alloc] initWithName: @"ThemeExtra"
fromFile: nil];
}
if (_extraColors[GSThemeHighlightedState] == nil)
{
_extraColors[GSThemeHighlightedState]
= [[NSColorList alloc] initWithName: @"ThemeExtraHighlighted"
fromFile: nil];
}
if (_extraColors[GSThemeSelectedState] == nil)
{
_extraColors[GSThemeSelectedState]
= [[NSColorList alloc] initWithName: @"ThemeExtraSelected"
fromFile: nil];
}
if (_info == nil)
{
_info = [NSMutableDictionary new];
}
/*
* Make sure the info plist contains a GSThemeDomain dictionary and
* it is mutable so we can alter it.
*/
_defs = [_info objectForKey: @"GSThemeDomain"];
if (_defs == nil)
{
_defs = (NSMutableDictionary *)[NSDictionary dictionary];
}
_defs = [_defs mutableCopy];
[_info setObject: _defs forKey: @"GSThemeDomain"];
RELEASE(_defs);
/* Ensure that the loaded color list has the full set of system colors.
*/
keys = [systemColorList allKeys];
for (i = 0; i < [keys count]; i++)
{
NSString *key = [keys objectAtIndex: i];
if ([_colors colorWithKey: key] == nil)
{
[_colors setColor: [systemColorList colorWithKey: key] forKey: key];
}
}
/* Store in working directory.
*/
[_colors writeToFile:
[_rsrc stringByAppendingPathComponent: @"ThemeColors.clr"]];
[_extraColors[GSThemeNormalState] writeToFile:
[_rsrc stringByAppendingPathComponent: @"ThemeExtraColors.clr"]];
[_extraColors[GSThemeHighlightedState] writeToFile:
[_rsrc stringByAppendingPathComponent: @"ThemeExtraHighlightedColors.clr"]];
[_extraColors[GSThemeSelectedState] writeToFile:
[_rsrc stringByAppendingPathComponent: @"ThemeExtraSelectedColors.clr"]];
[_info writeToFile: [_rsrc stringByAppendingPathComponent:
@"Info-gnustep.plist"] atomically: NO];
/* If there is no icon for the theme, set a default one.
*/
if ([_info objectForKey: @"GSThemeIcon"] == nil)
{
NSString *s;
s = [[NSBundle mainBundle] pathForResource: @"Thematic" ofType: @"png"];
[self setResource: s forKey: @"GSThemeIcon"];
[window setDocumentEdited: NO];
}
_theme = [[GSTheme alloc] initWithBundle: [NSBundle bundleWithPath: _work]];
[_theme setName: [[self name] stringByDeletingPathExtension]];
[NSBundle loadNibNamed: @"ThemeDocument" owner: self];
[window setFrameUsingName: @"Document"];
[window setFrameAutosaveName: @"Document"];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(notified:)
name: nil
object: window];
[window setDelegate: self];
/* Move all the subviews from the Gorm file content view to our
* document view, and replace the original content view with it.
*/
frame = [[window contentView] frame];
frame.origin = NSZeroPoint;
view = [[ThemeDocumentView alloc] initWithFrame: frame];
[view setOwner: self];
enumerator = [[[window contentView] subviews] objectEnumerator];
while ((old = [enumerator nextObject]) != nil)
{
[view addSubview: old];
}
[window setContentView: view];
RELEASE(view);
[self setPath: path];
{
NSAttributedString *s;
s = [[NSAttributedString alloc] initWithString: @"This raises the theme images inspector, which displays all the images used in drawing standard GUI elements (excluding images used for tiling), double-clicking any image will oproduce an open panel, allowing you to specify a replacement image to be used by your theme."];
[[NSHelpManager sharedHelpManager] setContextHelp: s forObject: imagesView];
RELEASE(s);
}
[colorsView setToolTip: _(@"system colors")];
[imagesView setToolTip: _(@"system images")];
[menusView setToolTip: _(@"menu settings")];
[windowsView setToolTip: _(@"window settings")];
[extraView setToolTip: _(@"general information")];
[previewView setToolTip: _(@"preview image")];
[window makeKeyAndOrderFront: self];
/* Here we can perform any extra setup needed for the different views
*/
enumerator = [[view subviews] objectEnumerator];
while ((old = [enumerator nextObject]) != nil)
{
if ([old isKindOfClass: [NSScrollView class]])
{
NSEnumerator *e = [[old subviews] objectEnumerator];
NSScrollView *sv = (NSScrollView*)old;
NSTextView *tv = [sv documentView];
NSSize sz = [[sv contentView] bounds].size;
/* This next horrible code is to ensure that the document inside
* the scroll view is big enough so that the scrollers display.
*/
sz.width *= 5;
sz.height *= 5;
[tv setHorizontallyResizable: NO];
[tv setVerticallyResizable: NO];
[tv setFrameSize: sz];
[sv setAutohidesScrollers: NO];
[sv setHasHorizontalScroller: YES];
[sv setHasVerticalScroller: YES];
while ((old = [e nextObject]) != nil)
{
if ([old isKindOfClass: [NSScroller class]])
{
[(NSScroller*)old setEnabled: YES];
}
}
}
else if ([old isKindOfClass: [NSScroller class]])
{
[(NSScroller*)old setEnabled: YES];
}
}
RELEASE(pool);
return self;
}
- (NSString*) name
{
return _name;
}
- (void) notified: (NSNotification*)n
{
NSString *name = [n name];
if ([name isEqualToString: NSWindowDidBecomeMainNotification]
|| [name isEqualToString: NSWindowDidBecomeKeyNotification])
{
[[AppController sharedController] selectDocument: self];
}
else if ([name isEqualToString: NSWindowWillCloseNotification])
{
[self close];
}
// NSLog(@"Received %@ from %@", name, [n object]);
}
- (NSString*) path
{
return _path;
}
- (void) saveDocument: (id)sender
{
/* Make the document window the key windoe so that any unsaved changes in
* the inspector are saved too.
*/
[window makeKeyWindow];
if ([window isDocumentEdited] == YES)
{
if (_path == nil)
{
[self saveDocumentAs: (id)sender];
}
else
{
[self saveToPath: _path];
[window setDocumentEdited: NO];
}
}
}
- (NSString*) saveDirectory
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDir;
NSString *base;
base = [NSSearchPathForDirectoriesInDomains
(NSAllLibrariesDirectory, NSUserDomainMask, YES) lastObject];
if (base != nil)
{
base = [base stringByAppendingPathComponent: @"Themes"];
if ([mgr fileExistsAtPath: base isDirectory: &isDir] == NO
|| isDir == NO)
{
int ret;
ret = NSRunAlertPanel(_(@"Warning"),
_(@"Your personal theme directory (%@) does not exist"),
_(@"Create it"), _(@"Ignore"), nil, base);
if (ret == 1)
{
if ([mgr createDirectoryAtPath: base attributes: nil] == NO)
{
NSRunAlertPanel(_(@"Alert"),
_(@"Unable to create directory at %@"),
_(@"OK"), nil, nil, base);
base = nil;
}
}
else
{
base = nil;
}
}
}
if (base == nil)