summaryrefslogtreecommitdiffstats
path: root/Liaison/ViewOptionsController.m
blob: 21f4c4747cf149fbcbce4a428786cbbb1f970873 (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
#import "ViewOptionsController.h"

#import "FileTableDelegate.h"

@implementation ViewOptionsController
- (id)init
{
    self = [super init];

    theShownColumns = [[NSMutableArray alloc] init];

    return self;
}

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

- (void)toggledButton: (id)sender
{
    NSButtonCell *senderCell;
    NSMutableArray *colOrder;
    NSMutableDictionary *listPrefs;
    NSString *colID;

    senderCell = [sender selectedCell];
    listPrefs = [theFileDelegate listPrefs];
    colOrder = [listPrefs objectForKey: @"columnOrder"];
    colID = [theShownColumns objectAtIndex: [senderCell tag]];
    if (colID != nil) {
        if ([senderCell state] == 1) {
            [theFileDelegate showColumnWithIdentifier: colID];
            [colOrder addObject: colID];
        } else {
            [theFileDelegate removeColumnWithIdentifier: colID];
            [colOrder removeObject: colID];
        }
    }

    [theFileDelegate setListPrefs: listPrefs];
}

- (void)sizeWindowToFit
{
    NSRect contentRect, headerRect, windowFrame, oldWindowFrame;

    oldWindowFrame = [NSWindow contentRectForFrameRect: [theWindow frame]
                                             styleMask: [theWindow styleMask]];

    [layoutMatrix sizeToCells];
    contentRect = [theContentView frame];
    contentRect.size = [layoutMatrix bounds].size;
    headerRect = [theHeaderField frame];
    
    windowFrame.origin = oldWindowFrame.origin;
    windowFrame.size.width = MAX(2*contentRect.origin.x + contentRect.size.width,
                                 headerRect.origin.x + headerRect.size.width + 20);
    windowFrame.size.height = headerRect.size.height + contentRect.size.height;
    windowFrame = [NSWindow frameRectForContentRect: windowFrame
                                          styleMask: [theWindow styleMask]];
    [theWindow setFrame: windowFrame display: YES animate: NO];

    [theHeaderField setFrameOrigin:
        NSMakePoint(headerRect.origin.x, contentRect.size.height)];
    [theContentView setFrame: contentRect];
}

- (IBAction)showWindow: (id)sender
{
    NSDictionary *browserColumns;
    NSEnumerator *columnEnum;
    NSMutableArray *optionCol;
    NSString *identifier;
    NSRect tmpRect;
    unsigned int numRows;
    int cellTag;

    layoutMatrix = [[NSMatrix alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
    [layoutMatrix autorelease];
    [layoutMatrix setMode: NSTrackModeMatrix];
    [layoutMatrix setCellSize: NSMakeSize(100.0, 20.0)];
    [layoutMatrix setDrawsBackground: YES];

    cellTag = 0;
    browserColumns = [theFileDelegate browserColumns];
    numRows = ceil(sqrt([browserColumns count]));
    optionCol = [NSMutableArray array];
    columnEnum = [browserColumns keyEnumerator];
    while ((identifier = [columnEnum nextObject]) != nil) {
        LiBrowserColumn *col;

        col = [theFileDelegate columnForIdentifier: identifier];
        if (col != nil) {
            NSButtonCell *checkBox;
            NSTableColumn *tableCol;
            BOOL checked;

            tableCol = [[theFileDelegate tableView] tableColumnWithIdentifier: identifier];
            if (tableCol == nil)
                checked = 0;
            else
                checked = 1;

            checkBox = [[[NSButtonCell alloc] init] autorelease];
            [checkBox setButtonType: NSSwitchButton];
            [checkBox setTitle: [col name]];
            [checkBox setTag: cellTag];
            [checkBox setTarget: self];
            [checkBox setAction: @selector(toggledButton:)];
            [checkBox setState: checked];

            [theShownColumns addObject: identifier];

            [optionCol addObject: checkBox];
            if ([optionCol count] == numRows) {
                if ([layoutMatrix numberOfColumns] == 0) {
                    unsigned int j;

                    [layoutMatrix addColumn];
                    for (j = [layoutMatrix numberOfRows]; j < numRows; j++) {
                        [layoutMatrix addRowWithCells:
                            [NSArray arrayWithObject: [optionCol objectAtIndex: j]]];
                    }
                    [layoutMatrix putCell: [optionCol objectAtIndex: 0]
                                    atRow: 0 column: 0];
                } else {
                    [layoutMatrix addColumnWithCells: optionCol];
                }
                [optionCol removeAllObjects];
            }
        }

        cellTag++;
    }

    if ([optionCol count] > 0) {
        while ([optionCol count] < numRows)
            [optionCol addObject: [[[NSCell alloc] init] autorelease]];

        [layoutMatrix addColumnWithCells: optionCol];
    }

    tmpRect = [layoutMatrix bounds];
    [theContentView setFrameSize: tmpRect.size];
    [theContentView addSubview: layoutMatrix];
    [self sizeWindowToFit];

    [theWindow makeKeyAndOrderFront: self];
}
@synthesize theShownColumns;
@synthesize theFileDelegate;
@synthesize theWindow;
@synthesize theContentView;
@synthesize theHeaderField;
@synthesize layoutMatrix;
@end