-
Notifications
You must be signed in to change notification settings - Fork 1
/
TWLayoutItemFactory.m
250 lines (198 loc) · 8.41 KB
/
TWLayoutItemFactory.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
/*
Copyright (C) 2013 Quentin Mathe
Author: Quentin Mathe <[email protected]>
Date: April 2013
License: Modified BSD (see COPYING)
*/
#import <IconKit/IconKit.h>
#import "TWLayoutItemFactory.h"
#import "TWEditorController.h"
#import "TWEditorContentController.h"
@implementation TWLayoutItemFactory
- (ETTextEditorLayout *) textLayout
{
return [ETTextEditorLayout layout];
}
- (ETOutlineLayout *) listLayout
{
ETOutlineLayout *layout = [ETOutlineLayout layout];
NSArray *headerNames = A(@"", @"Name", @"Content");
[layout setDisplayedProperties: A(@"icon", @"outlineValue")];
[layout setEditable: YES forProperty: @"name"];
[layout setEditable: YES forProperty: @"outlineValue"];
[layout setContentFont: [NSFont controlContentFontOfSize: 12]];
[[layout tableView] setUsesAlternatingRowBackgroundColors: YES];
[[layout columnForProperty: @"name"] setWidth: 180];
[[layout columnForProperty: @"outlineValue"] setWidth: 500];
[layout setSortable: YES];
NSUInteger n = [headerNames count];
for (NSUInteger i = 0; i < n; i++)
{
[layout setDisplayName: [headerNames objectAtIndex: i]
forProperty: [A(@"icon", @"name", @"outlineValue") objectAtIndex: i]];
}
return layout;
}
- (ETOutlineLayout *) columnLayout
{
return [ETBrowserLayout layout];
}
- (ETLayoutItem *) buttonWithIconNamed: (NSString *)aName target: (id)aTarget action: (SEL)anAction
{
NSImage *icon = [[IKIcon iconWithIdentifier: aName] image];
return [self buttonWithImage: icon target: aTarget action: anAction];
}
- (NSSize) defaultEditorSize
{
return NSMakeSize(900, 500);
}
- (NSSize) defaultBodySize
{
NSSize size = [self defaultEditorSize];
size.height -= [self defaultIconAndLabelBarHeight];
return size;
}
- (CGFloat) defaultSourceListWidth
{
return 0;
}
- (CGFloat) defaultContentViewWidth
{
return [self defaultBodySize].width - [self defaultSourceListWidth];
}
- (CGFloat) defaultInspectorWidth
{
return 400;
}
- (ETLayoutItemGroup *) editorWithRepresentedObject: (id)anObject editingContext: (COEditingContext *)aContext;
{
TWEditorController *controller = AUTORELEASE([TWEditorController new]);
[controller setPersistentObjectContext: aContext];
ETLayoutItemGroup *topBar = [self topBarWithController: controller];
ETLayoutItemGroup *body = [self bodyWithRepresentedObject: anObject controller: controller];
ETLayoutItemGroup *browser = [self itemGroupWithItems: A(topBar, body)];
[browser setAutoresizingMask: ETAutoresizingFlexibleWidth | ETAutoresizingFlexibleHeight];
[browser setController: controller];
/* For TWEditorController template lookup needs */
[browser setRepresentedObject: anObject];
[browser setShouldMutateRepresentedObject: NO];
[browser setLayout: [ETColumnLayout layout]];
[browser setSize: [self defaultEditorSize]];
return browser;
}
- (ETLayoutItemGroup *) bodyWithRepresentedObject: (id)anObject controller: (id)aController
{
ETLayoutItemGroup *contentView = [self contentViewWithRepresentedObject: anObject
controller: aController];
ETLayoutItemGroup *body = [self itemGroup];
[body setIdentifier: @"body"];
[body setAutoresizingMask: ETAutoresizingFlexibleWidth | ETAutoresizingFlexibleHeight];
[body setSize: [self defaultBodySize]]; // FIXME: Avoid negative size if -setSize: is not called
[body setLayout: [ETLineLayout layout]];
[body addItems: A(contentView)];
[aController setContentViewItem: contentView];
return body;
}
- (ETLayoutItem *) viewPopUpWithController: (id)aController
{
NSArray *choices = A(_(@"Text"), _(@"Outline"), _(@"Outline and Text"));
NSArray *templateLayouts =
A([self textLayout], [self listLayout], [NSNull null]);
ETLayoutItem *popUpItem = [self popUpMenuWithItemTitles: choices
representedObjects: templateLayouts
target: aController
action: @selector(changePresentationViewFromPopUp:)];
[aController setViewPopUpItem: popUpItem];
[[popUpItem view] selectItemAtIndex: 1];
return popUpItem;
}
- (ETLayoutItemGroup *) topBarWithController: (ETController *)aController
{
ETLayoutItemGroup *itemGroup = [self itemGroup];
ETLayoutItem *newItem = [self buttonWithIconNamed: @"list-add"
target: aController
action: @selector(insert:)];
ETLayoutItem *newChildItem = [self buttonWithIconNamed: @"list-add"
target: aController
action: @selector(add:)];
ETLayoutItem *removeItem = [self buttonWithIconNamed: @"list-remove"
target: aController
action: @selector(remove:)];
ETLayoutItem *searchFieldItem = [self searchFieldWithTarget: aController
action: @selector(search:)];
[itemGroup setIdentifier: @"topBar"];
[itemGroup setWidth: [self defaultEditorSize].width];
[itemGroup setHeight: [self defaultIconAndLabelBarHeight]];
[itemGroup setLayout: [ETLineLayout layout]];
// FIXME: [[itemGroup layout] setSeparatorTemplateItem: [self flexibleSpaceSeparator]];
[itemGroup addItems:
A([self barElementFromItem: newItem withLabel: _(@"New Item")],
[self barElementFromItem: newChildItem withLabel: _(@"New Child")],
[self barElementFromItem: removeItem withLabel: _(@"Remove")],
[self barElementFromItem: [self viewPopUpWithController: aController] withLabel: _(@"View")],
[self barElementFromItem: searchFieldItem withLabel: _(@"Search")])];
[itemGroup updateLayout];
return itemGroup;
}
- (ETLayoutItemGroup *) contentViewWithRepresentedObject: (id)anObject controller: (id)aController
{
ETLayoutItemGroup *itemGroup = [self itemGroupWithRepresentedObject: anObject];
[itemGroup setIdentifier: @"contentView"];
[itemGroup setAutoresizingMask: ETAutoresizingFlexibleWidth | ETAutoresizingFlexibleHeight];
[itemGroup setHeight: [self defaultBodySize].height];
// TODO: The width should be computed by the body layout
[itemGroup setWidth: [self defaultContentViewWidth]];
[itemGroup setHasVerticalScroller: YES];
[itemGroup setSource: itemGroup];
[itemGroup setShouldMutateRepresentedObject: YES];
[itemGroup setLayout: [self listLayout]];
[itemGroup setController: AUTORELEASE([TWEditorContentController new])];
[[itemGroup controller] setPersistentObjectContext: [aController persistentObjectContext]];
[itemGroup reload];
[aController setContentViewItem: itemGroup];
return itemGroup;
}
- (ETLayoutItemGroup *) inspectorWithObject: (id)anObject
size: (NSSize)aSize
controller: (ETController *)aController
{
ETLayoutItemGroup *itemGroup = [[ETModelDescriptionRenderer renderer] renderObject: anObject];
[itemGroup setIdentifier: @"inspector"];
[itemGroup setAutoresizingMask: ETAutoresizingFlexibleWidth | ETAutoresizingFlexibleHeight];
[itemGroup setSize: aSize];
return itemGroup;
}
@end
@implementation ETApplication (TypeWriter)
- (NSMenuItem *) viewMenuItem
{
TWLayoutItemFactory *itemFactory = [TWLayoutItemFactory factory];
NSMenuItem *menuItem = (id)[[self mainMenu] itemWithTitle: _(@"View")];
if (menuItem != nil)
return menuItem;
menuItem = [NSMenuItem menuItemWithTitle: _(@"View")
tag: 0
action: NULL];
NSMenu *menu = [menuItem submenu];
[menu addItemWithTitle: _(@"Text")
action: @selector(changePresentationViewFromMenuItem:)
keyEquivalent: @""];
[[menu lastItem] setRepresentedObject: [itemFactory textLayout]];
[menu addItemWithTitle: _(@"Outline")
action: @selector(changePresentationViewFromMenuItem:)
keyEquivalent: @""];
[[menu lastItem] setRepresentedObject: [itemFactory listLayout]];
[menu addItemWithTitle: _(@"Outline and Text")
action: @selector(changePresentationViewFromMenuItem:)
keyEquivalent: @""];
[[menu lastItem] setRepresentedObject: [NSNull null]];
[menu addItem: [NSMenuItem separatorItem]];
[menu addItemWithTitle: _(@"Show Inspector")
action: @selector(toggleInspector:)
keyEquivalent: @""];
[menu addItemWithTitle: _(@"New Inspector")
action: @selector(showInspectorInStandaloneWindow:)
keyEquivalent: @""];
return menuItem;
}
@end