1#import "drawundo.h"
2
3@implementation PerformTextGraphicsChange
4
5static id editText = nil;
6static id editWindow = nil;
7
8- initGraphic:aGraphic view:aGraphicView
9{
10    [super init];
11
12    if (editText == nil) {
13	editText = [[DrawSpellText alloc] initWithFrame:(NSRect){{0,0},{0,0}}];
14	[editText setRichText:YES];
15    }
16
17    if (editWindow == nil) {
18	editWindow = [[NSWindow alloc] init];
19    }
20
21    graphic = aGraphic;
22    graphicView = aGraphicView;
23    textChange = nil;
24
25    return self;
26}
27
28- (void)dealloc
29{
30   [textChange release];
31   [super dealloc];
32}
33
34- (NSString *)changeName
35{
36    return [textChange changeName];
37}
38
39- (void)undoChange
40{
41    NSRect bounds;
42
43    [self loadGraphic];
44    [textChange undoChange];
45    [self unloadGraphic];
46    bounds = [graphic bounds];
47    [graphicView cache:bounds];
48    [[graphicView window] flushWindow];
49
50    [super undoChange];
51}
52
53- (void)redoChange
54{
55    NSRect bounds;
56
57    [self loadGraphic];
58    [textChange redoChange];
59    [self unloadGraphic];
60    bounds = [graphic bounds];
61    [graphicView cache:bounds];
62    [[graphicView window] flushWindow];
63
64    [super redoChange];
65}
66
67- (BOOL)incorporateChange:aChange
68{
69    if (textChange == nil) {
70	textChange = aChange;
71	return YES;
72    } else {
73	return NO;
74    }
75}
76
77- (void)loadGraphic
78{
79    NSRect graphicBounds;
80    [editText replaceCharactersInRange:NSMakeRange(0, [[editText string] length]) withRTF:[graphic richTextData]];
81    graphicBounds = [graphic bounds];
82    [editText setFrame:graphicBounds];
83    [editWindow setNextResponder:graphicView]; /* so changes can find our */
84                                               /* change manager          */
85    [[editWindow contentView] addSubview:editText];
86    [editText selectAll:self];
87}
88
89- (void)unloadGraphic
90{
91    [editWindow setNextResponder:nil];
92    [editText removeFromSuperview];
93    [editText setSelectedRange:NSMakeRange(0, 0)];
94    [graphic setFont:[editText font]];
95    [graphic setRichTextData:[editText RTFFromRange:NSMakeRange(0, [[editText string] length])]];
96}
97
98- (NSText *)editText
99{
100    return editText;
101}
102
103@end
104