I'm beginning to wonder if it's possible to save and restore the current disclosure state of the hierarchy of a NSOutlineView.

I solved this a while ago. Here is what I am doing:

I am saving in my model if an item is expanded or collapsed. To keep the model up-to-date, I am doing the following:

- In outlineViewItemDidExpand: I am setting my expanded flag in the model to YES. - In outlineViewItemWillCollapse: I am checking if the item is currently "visible" and only if it's visible, I set my expanded-flag to NO. To check if it is "visible" I am going up in the hierarchy and look if some of the parents is collapsed. If one is not expanded, I do not change the expanded flag. E.g. like so:

- (void)outlineViewItemWillCollapse:(NSNotification *)notification
{
ModelItem *item = [[notification userInfo] objectForKey:@"NSObject"];
    BOOL visible = YES;
    ModelItem *parent = [item parent];
    while (parent && parent != root_ item) {
        if (![parent getExpandedFlag]) {
            visible = NO;
            break;
        }
        parent = [parent parent];
    }
    if (visible) {
        [item setExpandedFlag:NO];
    }
}

Finally, to initialize the expansion state of the NSOutlineView items at startup, I am doing the following:

- Recursively walk through the hierarchy in the model.
- Get the expanded flag of the current item.
- Before getting down in the hierarchy, call expandItem: for the current item. - Now go down recursively by calling this exansion initialization routine for each child. - Finally (after returning from the recursion) call collapseItem: for the current item if its exanded flag (remembered in the second step) is set to NO.

Like so:

- (void) initExpansions:(ModelItem *)root
{
    ModelItem *child;
    BOOL expanded = [root getExpandedFlag];
    if (root != root_item) {
        [outlineView expandItem:root];
    }
    for("every child of root") {      // pseudo code!
        [self initExpansions:child];    // go down recursively
    }
    if (root != root_item && !expanded) {
        [outlineView collapseItem:root];
    }
}

Regards,
Mani
--
http://mani.de - friendly software
iVolume - listen to music freehand
LittleSecrets - the encrypted notepad

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to