<commit_before>/*
 * _CPPopUpList.j
 * AppKit
 *
 * Created by <NAME>.
 * Copyright (c) 2012, The Cappuccino Foundation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import "CPTableView.j"
@import "_CPPopUpListDataSource.j"


/*!
    Notification sent when the list is about to pop up. \c object is the _CPPopUpList.
*/
_CPPopUpListWillPopUpNotification = @"_CPPopUpListWillPopUpNotification";

/*!
    Notification sent when the list is about to be dismissed. \c object is the _CPPopUpList.
*/
_CPPopUpListWillDismissNotification = @"_CPPopUpListWillDismissNotification";

/*!
    Notification sent when the list is dismissed. \c object is the _CPPopUpList.
*/
_CPPopUpListDidDismissNotification = @"_CPPopUpListDidDismissNotification";

/*!
    Notification sent by when an item is selected. \c object is the _CPPopUpList.
    When this is received the list has already been dismissed and the dismiss notification has been sent.
*/
_CPPopUpListItemWasClickedNotification = @"_CPPopUpListItemWasClickedNotification";

/*!
    @ignore

    The minimum number of items that must be visible below the related field.
    If less than this number would be completely visible, and there is room for this many complete items
    above the field, the list is displayed above.
*/
var ListMinimumItems = 3;

/*! @ignore */
var ListColumnIdentifier = @"1";


/*!
    This class is a controller for a panel that can pop up and display a scrollable list of items in a CPTableView.
    It is used by CPComboBox to display the list of choices.

    This class requires a data source which must conform to the interface of _CPPopUpListDataSource.

    Objects of this class send the following notifications:

    _CPPopUpListWillPopUpNotification
    _CPPopUpListWillDismissNotification
    _CPPopUpListDidDismissNotification
    _CPPopUpListItemWasClickedNotification
*/
@implementation _CPPopUpList : CPObject
{
    _CPPopUpListDataSource  _dataSource;
    BOOL                    _itemWasClicked;
    BOOL                    _listWasClicked;
    int                     _listWidth;
    _CPPopUpPanel           _panel;
    CPScrollView            _scrollView;
    _CPPopUpTableView       _tableView;
    CPTableColumn           _tableColumn;
}

#pragma mark Creating and Displaying a List

/*!
    Creates a pop up list of choices that will display in a scrollable CPTableView.

    @param aDataSource    A subclass of _CPPopUpListDataSource
*/
- (id)initWithDataSource:(_CPPopUpListDataSource)aDataSource
{
    self = [super init];

    if (self)
    {
        [self setDataSource:aDataSource];
        _itemWasClicked = NO;
        _listWasClicked = NO;
        _listWidth = 0;

        _tableView = [self makeTableView];

        // Start with a default size, we will resize it later
        frame = CGRectMake(0, 0, 200, 200);

        _tableColumn = [[CPTableColumn alloc] initWithIdentifier:ListColumnIdentifier];
        [_tableColumn setWidth:CGRectGetWidth(frame) - [CPScroller scrollerWidth]];
        [_tableColumn setResizingMask:CPTableColumnAutoresizingMask];
        [_tableView addTableColumn:_tableColumn];

        _scrollView = [self makeScrollViewWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
        [_scrollView setDocumentView:_tableView];

        // This has to be done after setDocumentView so that the table knows which scroll view to update
        [_tableView setHeaderView:nil];

        _panel = [self makeListPanelWithFrame:frame];
        [[_panel contentView] addSubview:_scrollView];
        [_panel setInitialFirstResponder:_tableView];

        if ([_dataSource numberOfItemsInList:self] > 0)
            [_tableView selectRowIndexes:[CPIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
        else
            [_tableView setEnabled:NO];

        [_scrollView scrollToBeginningOfDocument:nil];
    }

    return self;
}

/*! @ignore */
- (CPPanel)makeListPanelWithFrame:(CGRect)aFrame
{
    var panel = [[_CPPopUpPanel alloc] initWithContentRect:aFrame styleMask:CPBorderlessWindowMask];

    [panel setTitle:@""];
    [panel setFloatingPanel:YES];
    [panel setBecomesKeyOnlyIfNeeded:YES];
    [panel setLevel:CPStatusWindowLevel];
    [panel setHasShadow:YES];
    [panel setShadowStyle:CPMenuWindowShadowStyle];
    [panel setDelegate:self];

    return panel;
}

/*! @ignore */
- (_CPPopUpTableView)makeTableView
{
    [self removeTableViewObservers];

    var table = [[_CPPopUpTableView alloc] initWithFrame:CGRectMakeZero()];

    [table setDelegate:self];
    [table setDataSource:self];
    [table setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
    [table setUsesAlternatingRowBackgroundColors:NO];
    [table setAllowsMultipleSelection:NO];
    [table setIntercellSpacing:CGSizeMake(3, 2)];
    [table setTarget:self];<commit_msg>use CPPopUpMenuWindowLevel for _CPPopUpList's window level<commit_after>/*
 * _CPPopUpList.j
 * AppKit
 *
 * Created by <NAME>.
 * Copyright (c) 2012, The Cappuccino Foundation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import "CPTableView.j"
@import "_CPPopUpListDataSource.j"


/*!
    Notification sent when the list is about to pop up. \c object is the _CPPopUpList.
*/
_CPPopUpListWillPopUpNotification = @"_CPPopUpListWillPopUpNotification";

/*!
    Notification sent when the list is about to be dismissed. \c object is the _CPPopUpList.
*/
_CPPopUpListWillDismissNotification = @"_CPPopUpListWillDismissNotification";

/*!
    Notification sent when the list is dismissed. \c object is the _CPPopUpList.
*/
_CPPopUpListDidDismissNotification = @"_CPPopUpListDidDismissNotification";

/*!
    Notification sent by when an item is selected. \c object is the _CPPopUpList.
    When this is received the list has already been dismissed and the dismiss notification has been sent.
*/
_CPPopUpListItemWasClickedNotification = @"_CPPopUpListItemWasClickedNotification";

/*!
    @ignore

    The minimum number of items that must be visible below the related field.
    If less than this number would be completely visible, and there is room for this many complete items
    above the field, the list is displayed above.
*/
var ListMinimumItems = 3;

/*! @ignore */
var ListColumnIdentifier = @"1";


/*!
    This class is a controller for a panel that can pop up and display a scrollable list of items in a CPTableView.
    It is used by CPComboBox to display the list of choices.

    This class requires a data source which must conform to the interface of _CPPopUpListDataSource.

    Objects of this class send the following notifications:

    _CPPopUpListWillPopUpNotification
    _CPPopUpListWillDismissNotification
    _CPPopUpListDidDismissNotification
    _CPPopUpListItemWasClickedNotification
*/
@implementation _CPPopUpList : CPObject
{
    _CPPopUpListDataSource  _dataSource;
    BOOL                    _itemWasClicked;
    BOOL                    _listWasClicked;
    int                     _listWidth;
    _CPPopUpPanel           _panel;
    CPScrollView            _scrollView;
    _CPPopUpTableView       _tableView;
    CPTableColumn           _tableColumn;
}

#pragma mark Creating and Displaying a List

/*!
    Creates a pop up list of choices that will display in a scrollable CPTableView.

    @param aDataSource    A subclass of _CPPopUpListDataSource
*/
- (id)initWithDataSource:(_CPPopUpListDataSource)aDataSource
{
    self = [super init];

    if (self)
    {
        [self setDataSource:aDataSource];
        _itemWasClicked = NO;
        _listWasClicked = NO;
        _listWidth = 0;

        _tableView = [self makeTableView];

        // Start with a default size, we will resize it later
        frame = CGRectMake(0, 0, 200, 200);

        _tableColumn = [[CPTableColumn alloc] initWithIdentifier:ListColumnIdentifier];
        [_tableColumn setWidth:CGRectGetWidth(frame) - [CPScroller scrollerWidth]];
        [_tableColumn setResizingMask:CPTableColumnAutoresizingMask];
        [_tableView addTableColumn:_tableColumn];

        _scrollView = [self makeScrollViewWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
        [_scrollView setDocumentView:_tableView];

        // This has to be done after setDocumentView so that the table knows which scroll view to update
        [_tableView setHeaderView:nil];

        _panel = [self makeListPanelWithFrame:frame];
        [[_panel contentView] addSubview:_scrollView];
        [_panel setInitialFirstResponder:_tableView];

        if ([_dataSource numberOfItemsInList:self] > 0)
            [_tableView selectRowIndexes:[CPIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
        else
            [_tableView setEnabled:NO];

        [_scrollView scrollToBeginningOfDocument:nil];
    }

    return self;
}

/*! @ignore */
- (CPPanel)makeListPanelWithFrame:(CGRect)aFrame
{
    var panel = [[_CPPopUpPanel alloc] initWithContentRect:aFrame styleMask:CPBorderlessWindowMask];

    [panel setTitle:@""];
    [panel setFloatingPanel:YES];
    [panel setBecomesKeyOnlyIfNeeded:YES];
    [panel setLevel:CPPopUpMenuWindowLevel];
    [panel setHasShadow:YES];
    [panel setShadowStyle:CPMenuWindowShadowStyle];
    [panel setDelegate:self];

    return panel;
}

/*! @ignore */
- (_CPPopUpTableView)makeTableView
{
    [self removeTableViewObservers];

    var table = [[_CPPopUpTableView alloc] initWithFrame:CGRectMakeZero()];

    [table setDelegate:self];
    [table setDataSource:self];
    [table setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
    [table setUsesAlternatingRowBackgroundColors:NO];
    [table setAllowsMultipleSelection:NO];
    [table setIntercellSpacing:CGSizeMake(3, 2)];
    [table setTarget:self];
{'fraction_non_alphanumeric': 0.07212317666126418, 'fraction_numerical': 0.0061228164955879705, 'mean_word_length': 3.682546374367622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2931727', 'n_tokens_mistral': 3214, 'n_tokens_neox': 3042, 'n_words': 1016}