summaryrefslogtreecommitdiffstats
path: root/Liaison/InspectorController.m
blob: 453574c428687fceabf63daf8b7add6c872cf8fc (plain)
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
#import "InspectorController.h"

#import "PluginManager.h"

@implementation InspectorController (WindowDelegate)
- (NSRect)windowWillUseStandardFrame: (NSWindow *)aWindow
                        defaultFrame: (NSRect)defaultFrame
{
    return [self minWindowFrame];
}
@end

@implementation InspectorController
- (id)init
{
    self = [super init];
    
    theInspectorViews = [[NSMutableDictionary alloc] init];
    
    return self;
}

- (void)dealloc
{
    [theInspectorViews release];
    [super dealloc];
}

- (LiInspectorView *)viewForDefault
{
    LiInspectorView *view;

    view = [[[LiInspectorView alloc] init] autorelease];
    [view setIdentifier: @"nothing"];
    [view setName: @"Nada"];
    [view setImage: nil];
    [view setIsHorizontallyResizable: NO];
    [view setIsVerticallyResizable: NO];
    [view setView: theDefaultTabView];
    [view setViewSize: [[view view] frame].size];

    return view;
}

- (void)awakeFromNib
{
    NSEnumerator *pluginEnum;
    NSObject <LiInspectorPlugin> *plugin;
    
    [self setFile: nil];

    // Load our default, "nothing's there" view.
    [theInspectorViews setObject: [self viewForDefault] forKey: @"DEFAULT"];
    
    // Load plug-in views.
    pluginEnum = [[[PluginManager defaultManager] inspectorPlugins] objectEnumerator];
    while ((plugin = [pluginEnum nextObject]) != nil) {
        LiInspectorView *view;
        NSEnumerator *viewEnum;

        viewEnum = [[plugin allInspectorViews] objectEnumerator];
        while ((view = [viewEnum nextObject]) != nil) {
            NSString *identifier;

            identifier = [NSString stringWithFormat: @"%@:%@",
                NSStringFromClass([plugin class]), [view identifier]];
            [theInspectorViews setObject: view forKey: identifier];
        }
    }
}

- (LiInspectorView *)inspectorViewForIdentifier: (NSString *)anIdentifier
{
    return [theInspectorViews objectForKey: anIdentifier];
}

- (void)showInspectorViewWithIdentifier: (NSString *)anIdentifier
{
    LiInspectorView *view;

    view = [self inspectorViewForIdentifier: anIdentifier];
    if (view != nil) {
        NSTabViewItem *tab;

        tab = [[NSTabViewItem alloc] initWithIdentifier: anIdentifier];
        [tab autorelease];
        [tab setLabel: [view name]];
        [tab setView: [view view]];
        [theTabView addTabViewItem: tab];
    }
}

- (void)removeInspectorViewWithIdentifier: (NSString *)anIdentifier
{
    LiInspectorView *view;

    view = [self inspectorViewForIdentifier: anIdentifier];
}

- (NSRect)minWindowFrame
{
    LiInspectorView *inspectorView;
    NSTabViewItem *tab;
    NSRect windowFrame;
    NSSize viewSize;
    float newHeight, newWidth;

    tab = [theTabView selectedTabViewItem];
    inspectorView = [self inspectorViewForIdentifier: [tab identifier]];

    viewSize = [inspectorView viewSize];
    newHeight = viewSize.height + 40;
    newWidth = viewSize.width;

    windowFrame = [NSWindow contentRectForFrameRect: [theWindow frame]
                                          styleMask: [theWindow styleMask]];
    windowFrame.origin.y += windowFrame.size.height;
    windowFrame.origin.y -= newHeight;
    windowFrame.size.height = newHeight;
    windowFrame.size.width = newWidth;

    windowFrame = [NSWindow frameRectForContentRect: windowFrame
                                          styleMask: [theWindow styleMask]];

    return windowFrame;
}

- (void)resizeWindow
{
    LiInspectorView *inspectorView;
    NSTabViewItem *tab;
    NSRect minWindowFrame, windowFrame;
    BOOL displayGrowBox = NO;

    tab = [theTabView selectedTabViewItem];
    inspectorView = [self inspectorViewForIdentifier: [tab identifier]];

    windowFrame = [theWindow frame];
    minWindowFrame = [self minWindowFrame];
    if ([inspectorView isVerticallyResizable] &&
        [inspectorView isHorizontallyResizable]) {
        // Resize nothing.
        if (windowFrame.size.width < minWindowFrame.size.width)
            windowFrame.size.width = minWindowFrame.size.width;
        if (windowFrame.size.height < minWindowFrame.size.height)
            windowFrame.size.height = minWindowFrame.size.height;
        
        displayGrowBox = YES;
    } else {
        if ([inspectorView isVerticallyResizable]) {
            // Resize the width.
            windowFrame.size.width = minWindowFrame.size.width;
            if (windowFrame.size.height < minWindowFrame.size.height)
                windowFrame.size.height = minWindowFrame.size.height;
            
            displayGrowBox = YES;
        } else if ([inspectorView isHorizontallyResizable]) {
            // Resize the height.
            windowFrame.origin.y = minWindowFrame.origin.y;
            windowFrame.size.height = minWindowFrame.size.height;
            if (windowFrame.size.width < minWindowFrame.size.width)
                windowFrame.size.width = minWindowFrame.size.width;
            
            displayGrowBox = YES;
        } else
            windowFrame = minWindowFrame;
    }
    [theWindow setFrame: windowFrame display: YES animate: YES];
    [theWindow setShowsResizeIndicator: displayGrowBox];
}

- (void)setFile: (LiFileHandle *)aFile
{
    NSEnumerator *pluginEnum, *tabEnum;
    NSMutableArray *shownTabs;
    NSString *identifier;
    NSTabViewItem *tab;
    NSObject <LiInspectorPlugin> *plugin;

    shownTabs = [NSMutableArray array];
    pluginEnum = [[[PluginManager defaultManager] inspectorPlugins] objectEnumerator];
    while ((plugin = [pluginEnum nextObject]) != nil) {
        LiInspectorView *view;
        NSEnumerator *viewEnum;

        viewEnum = [[plugin inspectorViewsForFile: aFile] objectEnumerator];
        while ((view = [viewEnum nextObject]) != nil) {
            identifier = [NSString stringWithFormat: @"%@:%@",
                NSStringFromClass([plugin class]), [view identifier]];
            [shownTabs addObject: identifier];
        }

        NS_DURING
            [plugin setFile: aFile];
        NS_HANDLER
            [LiLog logAsError:
           @"Inspector plugin: %@ couldn't handle '%@': %@, %@",
                NSStringFromClass([plugin class]),
                [aFile filename], [localException name],
                [localException reason]];
        NS_ENDHANDLER
    }

    tabEnum = [[theTabView tabViewItems] objectEnumerator];
    while ((tab = [tabEnum nextObject]) != nil) {
        if ([shownTabs containsObject: [tab identifier]])
            [shownTabs removeObject: [tab identifier]];
        else
            [theTabView removeTabViewItem: tab];
    }

    for (identifier in shownTabs) {
        [self showInspectorViewWithIdentifier: identifier];
    }

    if ([theTabView numberOfTabViewItems] == 0) {
        [LiLog logAsDebug: @"No tabs in view."];

        [self showInspectorViewWithIdentifier: @"DEFAULT"];
    }
}
@synthesize theWindow;
@synthesize theInspectorViews;
@synthesize theTabView;
@synthesize theDefaultTabView;
@synthesize theFile;
@end

@implementation InspectorController (TabViewDelegate)
- (void)tabView: (NSTabView *)tabView didSelectTabViewItem: (NSTabViewItem *)tabViewItem
{
    LiInspectorView *inspectorView;
    NSTabViewItem *tab;
    NSSize minSize, maxSize;

    tab = [theTabView selectedTabViewItem];
    inspectorView = [self inspectorViewForIdentifier: [tab identifier]];

    minSize = [self minWindowFrame].size;
    maxSize = NSMakeSize(800.0, 800.0);
    if ([inspectorView isHorizontallyResizable] == NO)
        maxSize.width = minSize.width;
    if ([inspectorView isVerticallyResizable] == NO)
        maxSize.height = minSize.height;

    [theWindow setMinSize: minSize];
    [theWindow setMaxSize: maxSize];
    [self resizeWindow];
}
@end