Added First version of ComboBoxes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@4862 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
gvandyk 1999-09-09 20:06:52 +00:00
parent 890b701d28
commit 7e3e7b84a9
11 changed files with 1379 additions and 2 deletions

View file

@ -1,3 +1,13 @@
1999-09-09 Gerrit van Dyk <gerritvd@decillion.net>
* Headers/NSComboBox.h: Added NSComboBox
* Headers/NSComboBoxCell.h: Add NSComboBoxCell
* Headers/AppKit.h: Added NSComboBox,NSComboBoxCell
* Source/NSComboBox.m: Added NSComboBox.
* Source/NSComboBoxCell.m: Added NSComboBoxCell
* Source/GSComboSupport.hm: Private Classes used by ComboBoxes
* Images/NSComboArrow.tiff: down arrow on comboboxes
1999-09-07 Michael Hanni <mhanni@sprintmail.com>
* Source/NSTabView.m: some modifications to make tabs on the
@ -92,7 +102,7 @@ Sun Aug 22 11:52:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSView.m: ([-viewWillMoveToWindow:]) fixed to call method
in subviews when new window is nil.
1999-08-22 Gerrit van Dyk <gerritvd@decimax.com>
1999-08-22 Gerrit van Dyk <gerritvd@decillion.net>
* Headers/NSProgressIndicator.h
AppKit.h: Added ProgressIndicator

View file

@ -56,9 +56,11 @@
// Controls
@class NSControl, NSButton, NSTextField, NSScroller, NSBox, NSForm, NSMatrix;
@class NSPopUpButton, NSProgressIndicator, NSSlider, NSBrowser, NSForm;
@class NSComboBox;
// Cells
@class NSCell, NSActionCell, NSButtonCell, NSTextFieldCell, NSFormCell;
@class NSSliderCell, NSMenuCell, NSBrowserCell, NSFormCell;
@class NSComboBoxCell;
@class NSEvent, NSCursor;
@class NSColor, NSColorList, NSColorPanel, NSColorPicker, NSColorWell;
@class NSImage, NSImageCell, NSImageRep, NSBitmapImageRep, NSCachedImageRep;
@ -89,6 +91,8 @@
#include <AppKit/NSColorPicker.h>
#include <AppKit/NSColorPicking.h>
#include <AppKit/NSColorWell.h>
#include <AppKit/NSComboBox.h>
#include <AppKit/NSComboBoxCell.h>
#include <AppKit/NSControl.h>
#include <AppKit/NSCursor.h>
#include <AppKit/NSCustomImageRep.h>

View file

@ -0,0 +1,101 @@
/*
NSComboBox.h
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Gerrit van Dyk <gerritvd@decillion.net>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _GNUstep_H_NSComboBox
#define _GNUstep_H_NSComboBox
#import <Foundation/NSNotification.h>
#import <AppKit/NSTextField.h>
@interface NSComboBox : NSTextField
{
}
- (BOOL)hasVerticalScroller;
- (void)setHasVerticalScroller:(BOOL)flag;
- (NSSize)intercellSpacing;
- (void)setIntercellSpacing:(NSSize)aSize;
- (float)itemHeight;
- (void)setItemHeight:(float)itemHeight;
- (int)numberOfVisibleItems;
- (void)setNumberOfVisibleItems:(int)visibleItems;
- (void)reloadData;
- (void)noteNumberOfItemsChanged;
- (BOOL)usesDataSource;
- (void)setUsesDataSource:(BOOL)flag;
- (void)scrollItemAtIndexToTop:(int)index;
- (void)scrollItemAtIndexToVisible:(int)index;
- (void)selectItemAtIndex:(int)index;
- (void)deselectItemAtIndex:(int)index;
- (int)indexOfSelectedItem;
- (int)numberOfItems;
/* These two methods can only be used when usesDataSource is YES */
- (id)dataSource;
- (void)setDataSource:(id)aSource;
/* These methods can only be used when usesDataSource is NO */
- (void)addItemWithObjectValue:(id)object;
- (void)addItemsWithObjectValues:(NSArray *)objects;
- (void)insertItemWithObjectValue:(id)object atIndex:(int)index;
- (void)removeItemWithObjectValue:(id)object;
- (void)removeItemAtIndex:(int)index;
- (void)removeAllItems;
- (void)selectItemWithObjectValue:(id)object;
- (id)itemObjectValueAtIndex:(int)index;
- (id)objectValueOfSelectedItem;
- (int)indexOfItemWithObjectValue:(id)object;
- (NSArray *)objectValues;
@end
@interface NSObject (NSComboBoxDataSource)
- (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index;
- (unsigned int)comboBox:(NSComboBox *)aComboBox
indexOfItemWithStringValue:(NSString *)string;
@end
@interface NSObject (NSComboBoxNotifications)
- (void)comboBoxWillPopUp:(NSNotification *)notification;
- (void)comboBoxWillDismiss:(NSNotification *)notification;
- (void)comboBoxSelectionDidChange:(NSNotification *)notification;
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
@end
NSString *NSComboBoxWillPopUpNotification;
NSString *NSComboBoxWillDismissNotification;
NSString *NSComboBoxSelectionDidChangeNotification;
NSString *NSComboBoxSelectionIsChangingNotification;
#endif /* _GNUstep_H_NSComboBox */

View file

@ -0,0 +1,108 @@
/*
NSComboBoxCell.h
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Gerrit van Dyk <gerritvd@decillion.net>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _GNUstep_H_NSComboBoxCell
#define _GNUstep_H_NSComboBoxCell
#import <AppKit/NSTextFieldCell.h>
#import <AppKit/NSTableView.h>
@class NSButtonCell,NSScrollView;
@interface NSComboBoxCell : NSTextFieldCell
{
id _delegate;
id _dataSource;
NSButtonCell *_buttonCell;
NSMutableArray *_popUpList;
BOOL _usesDataSource;
BOOL _hasVerticalScroller;
int _visibleItems;
NSSize _intercellSpacing;
float _itemHeight;
// Should be private but because of copying we can't
NSView *_popView;
@private;
BOOL _canPop;
NSRect _popRect;
NSEvent *_mUpEvent;
}
- (BOOL)hasVerticalScroller;
- (void)setHasVerticalScroller:(BOOL)flag;
- (NSSize)intercellSpacing;
- (void)setIntercellSpacing:(NSSize)aSize;
- (float)itemHeight;
- (void)setItemHeight:(float)itemHeight;
- (int)numberOfVisibleItems;
- (void)setNumberOfVisibleItems:(int)visibleItems;
- (void)reloadData;
- (void)noteNumberOfItemsChanged;
- (BOOL)usesDataSource;
- (void)setUsesDataSource:(BOOL)flag;
- (void)scrollItemAtIndexToTop:(int)index;
- (void)scrollItemAtIndexToVisible:(int)index;
- (void)selectItemAtIndex:(int)index;
- (void)deselectItemAtIndex:(int)index;
- (int)indexOfSelectedItem;
- (int)numberOfItems;
/* These two methods can only be used when usesDataSource is YES */
- (id)dataSource;
- (void)setDataSource:(id)aSource;
/* These methods can only be used when usesDataSource is NO */
- (void)addItemWithObjectValue:(id)object;
- (void)addItemsWithObjectValues:(NSArray *)objects;
- (void)insertItemWithObjectValue:(id)object atIndex:(int)index;
- (void)removeItemWithObjectValue:(id)object;
- (void)removeItemAtIndex:(int)index;
- (void)removeAllItems;
- (void)selectItemWithObjectValue:(id)object;
- (id)itemObjectValueAtIndex:(int)index;
- (id)objectValueOfSelectedItem;
- (int)indexOfItemWithObjectValue:(id)object;
- (NSArray *)objectValues;
@end
@interface NSObject (NSComboBoxCellDataSource)
- (int)numberOfItemsInComboBoxCell:(NSComboBoxCell *)comboBoxCell;
- (id)comboBoxCell:(NSComboBoxCell *)aComboBoxCell
objectValueForItemAtIndex:(int)index;
- (unsigned int)comboBoxCell:(NSComboBoxCell *)aComboBoxCell
indexOfItemWithStringValue:(NSString *)string;
@end
#endif /* _GNUstep_H_NSComboBoxCell */

View file

@ -88,7 +88,8 @@ common_Root_Sparc.tiff \
common_Root_Sparc2.tiff \
common_Root_Apple.tiff \
GNUstep.tiff \
nsmapping.strings
nsmapping.strings \
NSComboArrow.tiff
-include GNUmakefile.preamble

BIN
Images/NSComboArrow.tiff Normal file

Binary file not shown.

View file

@ -57,6 +57,8 @@ NSColorList.m \
NSColorPanel.m \
NSColorPicker.m \
NSColorWell.m \
NSComboBox.m \
NSComboBoxCell.m \
NSControl.m \
NSCursor.m \
NSCustomImageRep.m \
@ -123,6 +125,7 @@ NSTextView.m \
NSView.m \
NSWindow.m \
NSWorkspace.m \
GSComboSupport.m \
GSTextStorage.m \
GSTrackingRect.m \
GSServicesManager.m \
@ -158,6 +161,8 @@ AppKit/NSColorPanel.h \
AppKit/NSColorPicker.h \
AppKit/NSColorPicking.h \
AppKit/NSColorWell.h \
AppKit/NSComboBox.h \
AppKit/NSComboBoxCell.h \
AppKit/NSControl.h \
AppKit/NSCursor.h \
AppKit/NSCustomImageRep.h \

65
Source/GSComboSupport.h Normal file
View file

@ -0,0 +1,65 @@
/*
GSComboSupport.h
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Gerrit van Dyk <gerritvd@decillion.net>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _GNUstep_H_GSComboSupport
#define _GNUstep_H_GSComboSupport
#import <AppKit/NSBrowser.h>
#import <AppKit/NSWindow.h>
@class NSArray,NSComboBoxCell;
@interface GSComboWindow : NSWindow
{
NSBrowser *browser;
@private;
NSArray *list;
id _cell;
NSPoint _point;
float _width;
BOOL _stopped;
BOOL _shouldOrder;
BOOL _shouldNotify;
}
+ (GSComboWindow *)defaultPopUp;
- (NSMatrix *)matrix;
- (NSSize)popUpSize;
- (NSSize)popUpCellSizeForPopUp:(NSComboBoxCell *)aCell;
- (void)popUpCell:(NSComboBoxCell *)aCell
popUpAt:(NSPoint)aPoint
width:(float)aWidth;
- (void)runModalPopUp;
- (void)runLoop;
@end
@interface GSPopUpActionBrowser : NSBrowser
@end
#endif /* _GNUstep_H_GSComboSupport */

321
Source/GSComboSupport.m Normal file
View file

@ -0,0 +1,321 @@
/*
GSComboSupport.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Gerrit van Dyk <gerritvd@decillion.net>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import <AppKit/AppKit.h>
#import "GSComboSupport.h"
@implementation GSComboWindow
+ (GSComboWindow *)defaultPopUp
{
static GSComboWindow *gsWindow = nil;
if (!gsWindow)
gsWindow = [[self alloc] initWithContentRect:NSMakeRect(0,0,100,100)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES];
return gsWindow;
}
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
NSBox *box;
self = [super initWithContentRect:contentRect
styleMask:aStyle
backing:bufferingType
defer:flag];
box = [[[NSBox alloc] initWithFrame:NSMakeRect(0,0,100,100)] autorelease];
[box setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[box setBorderType:NSLineBorder];
[box setTitlePosition:NSNoTitle];
[box setContentViewMargins:NSMakeSize(1,1)];
[box sizeToFit];
[self setContentView:box];
browser = [[[GSPopUpActionBrowser alloc]
initWithFrame:NSMakeRect(0,0,100,100)] autorelease];
[browser setMaxVisibleColumns:1];
[browser setTitled:NO];
[browser setHasHorizontalScroller:NO];
[browser setTarget:self];
[browser setAction: @selector(selectItem:)];
[browser setDelegate:self];
// [browser setRefusesFirstResponder:YES];
[browser setAutoresizingMask:NSViewWidthSizable | NSViewWidthSizable];
[browser setAllowsEmptySelection:NO];
[browser setAllowsMultipleSelection:NO];
[box setContentView:browser];
return self;
}
- (void)dealloc
{
// Browser was not retained so don't release it
[super dealloc];
}
- (NSMatrix *)matrix { return [browser matrixInColumn:0]; }
- (NSSize)popUpSize
{
float itemHeight;
float cellSpacing;
if (!_cell)
return NSZeroSize;
if (![_cell isKindOfClass:[NSComboBoxCell class]])
[NSEvent raise:@"GSComboWindow" format:@"Cell not a NSComboBoxCell"];
itemHeight = [_cell itemHeight];
cellSpacing = [_cell intercellSpacing].height;
if (itemHeight <= 0)
itemHeight = [[self matrix] cellSize].height;
if (cellSpacing <= 0)
cellSpacing = [[self matrix] intercellSpacing].height;
return NSMakeSize(2.0 + [NSScroller scrollerWidth] + 100.0,
2.0 + (itemHeight * [_cell numberOfVisibleItems]) +
(cellSpacing * [_cell numberOfVisibleItems]));
}
- (NSSize)popUpCellSizeForPopUp:(NSComboBoxCell *)aCell
{
NSSize size;
_cell = aCell;
size = [self popUpSize];
size.height += 4.0;
size.width += 4.0;
_cell = nil;
return size;
}
- (void)popUpCell:(NSComboBoxCell *)aCell
popUpAt:(NSPoint)aPoint
width:(float)aWidth
{
NSRect rect;
rect.size = [self popUpCellSizeForPopUp:aCell];
_cell = aCell;
_width = aWidth;
_point = aPoint;
rect.size.width = _width;
rect.origin.x = _point.x;
rect.origin.y = _point.y;
[self setFrame:rect display:NO];
[_cell reloadData];
[browser loadColumnZero];
// [self enableKeyEquivalentForDefaultButtonCell];
[self runModalPopUp];
_cell = nil;
}
- (void)runModalPopUp
{
NSWindow *onWindow;
NSEvent *event;
NSException *exception = nil;
onWindow = [[_cell controlView] window];
[self setLevel:[onWindow level]];
[self orderWindow:NSWindowAbove relativeTo:[onWindow windowNumber]];
while ((event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate dateWithTimeIntervalSinceNow:0]
inMode:NSDefaultRunLoopMode
dequeue:NO]))
{
if ([event type] == NSAppKitDefined ||
[event type] == NSSystemDefined ||
[event type] == NSApplicationDefined ||
[event windowNumber] == [self windowNumber])
break;
[NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
}
[self makeKeyAndOrderFront:nil];
_shouldOrder = YES;
NS_DURING
[self runLoop];
NS_HANDLER
exception = localException;
NS_ENDHANDLER;
if (onWindow && _shouldOrder)
{
[onWindow makeKeyWindow];
[onWindow orderFrontRegardless];
}
if ([self isVisible])
[self orderOut:nil];
if (exception)
[exception raise];
}
- (void)runLoop
{
NSEvent *event;
int cnt;
NSAutoreleasePool *pool;
BOOL kDown;
cnt = 0;
pool = [[NSAutoreleasePool alloc] init];
_stopped = NO;
_shouldNotify = YES;
while (!_stopped)
{
kDown = NO;
cnt++;
if (cnt >= 5)
{
[pool release];
pool = [[NSAutoreleasePool alloc] init];
cnt = 0;
}
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:NO];
if (event)
{
if ([event type] == NSAppKitDefined ||
[event type] == NSSystemDefined ||
[event type] == NSApplicationDefined ||
[event windowNumber] == [self windowNumber])
{
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[NSApp sendEvent:event];
if ([event type] == NSKeyDown)
kDown = YES;
}
else if ([event type] == NSMouseMoved ||
[event type] == NSLeftMouseDragged ||
[event type] == NSRightMouseDragged ||
[event type] == NSMouseEntered ||
[event type] == NSMouseExited ||
[event type] == NSCursorUpdate)
{
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[NSApp sendEvent:event];
}
else
_stopped = YES;
}
}
_shouldNotify = NO;
if (kDown)
while ((event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:NO]))
{
if ([event windowNumber] != [self windowNumber])
break;
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[NSApp sendEvent:event];
if ([event type] == NSKeyUp)
break;
}
[pool release];
}
- (BOOL)canBecomeKeyWindow { return YES; }
- (BOOL)worksWhenModal { return NO; }
// Target/Action of Browser
- (void)selectItem:(id)sender
{
if (_cell && [_cell isKindOfClass:[NSComboBoxCell class]])
{
[_cell setStringValue:[[sender selectedCell] stringValue]];
_stopped = YES;
}
}
// Browser Delegate Methods
- (int)browser:(NSBrowser *)sender numberOfRowsInColumn:(int)column
{
if (!(_cell && [_cell isKindOfClass:[NSComboBoxCell class]]))
return 0;
[list release];
list = [[_cell objectValues] retain];
return [list count];
return 0;
}
- (void)browser:(NSBrowser *)sender willDisplayCell:(id)aCell
atRow:(int)row column:(int)column
{
[aCell setStringValue:[list objectAtIndex:row]];
[aCell setLeaf:YES];
}
@end
@implementation GSPopUpActionBrowser
- (BOOL)sendAction:(SEL)theAction to:(id)theTarget
{
// This Jippo is there because the browser does not want to send
// the action if we came from a modal panel
if (theTarget && theAction && [theTarget respondsToSelector:theAction])
{
[theTarget performSelector:theAction withObject:self];
return YES;
}
return [super sendAction:theAction to:theTarget];
}
@end

230
Source/NSComboBox.m Normal file
View file

@ -0,0 +1,230 @@
/*
NSComboBox.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Gerrit van Dyk <gerritvd@decillion.net>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import <AppKit/AppKit.h>
@interface NSObject(MouseUpping)
- (NSEvent *)_mouseUpEvent;
@end
@implementation NSComboBox
+ (void)initialize
{
if (self == [NSComboBox class])
[self setCellClass:[NSComboBoxCell class]];
}
- (NSComboBoxCell *)comboCell
{
return (NSComboBoxCell *)[self cell];
}
- (BOOL)hasVerticalScroller
{
return [[self comboCell] hasVerticalScroller];
}
- (void)setHasVerticalScroller:(BOOL)flag
{
[[self comboCell] setHasVerticalScroller:flag];
}
- (NSSize)intercellSpacing
{
return [[self comboCell] intercellSpacing];
}
- (void)setIntercellSpacing:(NSSize)aSize
{
[[self comboCell] setIntercellSpacing:aSize];
}
- (float)itemHeight
{
return [[self comboCell] itemHeight];
}
- (void)setItemHeight:(float)itemHeight
{
[[self comboCell] setItemHeight:itemHeight];
}
- (int)numberOfVisibleItems
{
return [[self comboCell] numberOfVisibleItems];
}
- (void)setNumberOfVisibleItems:(int)visibleItems
{
[[self comboCell] setNumberOfVisibleItems:visibleItems];
}
- (void)reloadData
{
[[self comboCell] reloadData];
}
- (void)noteNumberOfItemsChanged
{
[[self comboCell] noteNumberOfItemsChanged];
}
- (BOOL)usesDataSource
{
return [[self comboCell] usesDataSource];
}
- (void)setUsesDataSource:(BOOL)flag
{
[[self comboCell] setUsesDataSource:flag];
}
- (void)scrollItemAtIndexToTop:(int)index
{
[[self comboCell] scrollItemAtIndexToTop:index];
}
- (void)scrollItemAtIndexToVisible:(int)index
{
[[self comboCell] scrollItemAtIndexToVisible:index];
}
- (void)selectItemAtIndex:(int)index
{
[[self comboCell] selectItemAtIndex:index];
}
- (void)deselectItemAtIndex:(int)index
{
[[self comboCell] deselectItemAtIndex:index];
}
- (int)indexOfSelectedItem
{
return [[self comboCell] indexOfSelectedItem];
}
- (int)numberOfItems
{
return [[self comboCell] numberOfItems];
}
- (id)dataSource
{
return [[self comboCell] dataSource];
}
- (void)setDataSource:(id)aSource
{
[[self comboCell] setDataSource:aSource];
}
- (void)addItemWithObjectValue:(id)object
{
[[self comboCell] addItemWithObjectValue:object];
}
- (void)addItemsWithObjectValues:(NSArray *)objects
{
[[self comboCell] addItemsWithObjectValues:objects];
}
- (void)insertItemWithObjectValue:(id)object atIndex:(int)index
{
[[self comboCell] insertItemWithObjectValue:object atIndex:index];
}
- (void)removeItemWithObjectValue:(id)object
{
[[self comboCell] removeItemWithObjectValue:object];
}
- (void)removeItemAtIndex:(int)index
{
[[self comboCell] removeItemAtIndex:index];
}
- (void)removeAllItems
{
[[self comboCell] removeAllItems];
}
- (void)selectItemWithObjectValue:(id)object
{
[[self comboCell] selectItemWithObjectValue:object];
}
- (id)itemObjectValueAtIndex:(int)index
{
return [[self comboCell] itemObjectValueAtIndex:index];
}
- (id)objectValueOfSelectedItem
{
return [[self comboCell] objectValueOfSelectedItem];
}
- (int)indexOfItemWithObjectValue:(id)object
{
return [[self comboCell] indexOfItemWithObjectValue:object];
}
- (NSArray *)objectValues
{
return [[self comboCell] objectValues];
}
// Overridden
- (void)mouseDown:(NSEvent *)theEvent
{
id aCell;
NSEvent *cEvent;
aCell = [self cell];
[aCell trackMouse:theEvent inRect:[self bounds]
ofView:self untilMouseUp:YES];
if ([aCell respondsToSelector: @selector(_mouseUpEvent)])
cEvent = [aCell _mouseUpEvent];
else
cEvent = nil;
if ([aCell isSelectable])
{
if (!cEvent)
cEvent = [NSApp currentEvent];
if ([cEvent type] == NSLeftMouseUp &&
([cEvent windowNumber] == [[self window] windowNumber]))
[NSApp postEvent:cEvent atStart:NO];
[super mouseDown:theEvent];
}
}
@end
NSString *NSComboBoxWillPopUpNotification = @"NSComboBoxWillPopUpNotification";
NSString *NSComboBoxWillDismissNotification = @"NSComboBoxWillDismissNotification";
NSString *NSComboBoxSelectionDidChangeNotification = @"NSComboBoxSelectionDidChangeNotification";
NSString *NSComboBoxSelectionIsChangingNotification = @"NSComboBoxSelectionIsChangingNotification";

532
Source/NSComboBoxCell.m Normal file
View file

@ -0,0 +1,532 @@
/*
NSComboBoxCell.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Gerrit van Dyk <gerritvd@decillion.net>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import <AppKit/AppKit.h>
#import "GSComboSupport.h"
@interface NSComboBoxCell(_Private_)
- (void)_createButtonCell;
- (void)_didClick:(id)sender;
- (NSImage *)_buttonImage;
- (GSComboWindow *)_popUp;
@end
@implementation NSComboBoxCell
- (id)initTextCell:(NSString *)aString
{
self = [super initTextCell:aString];
_delegate = nil;
_dataSource = nil;
_buttonCell = nil;
_popUpList = [[NSMutableArray array] retain];
_usesDataSource = NO;
_visibleItems = 10;
_intercellSpacing = NSMakeSize(0.0,0.0);
_itemHeight = 14;
_popView = nil;
_canPop = NO;
_popRect = NSZeroRect;
_mUpEvent = nil;
[self _createButtonCell];
return self;
}
- (void)dealloc
{
[_delegate release];
[_dataSource release];
[_buttonCell release];
[_popUpList release];
[super dealloc];
}
- (BOOL)hasVerticalScroller { return _hasVerticalScroller; }
- (void)setHasVerticalScroller:(BOOL)flag
{
_hasVerticalScroller = flag;
}
- (NSSize)intercellSpacing { return _intercellSpacing; }
- (void)setIntercellSpacing:(NSSize)aSize
{
_intercellSpacing = aSize;
}
- (float)itemHeight { return _itemHeight; }
- (void)setItemHeight:(float)itemHeight
{
if (itemHeight > 14)
_itemHeight = itemHeight;
}
- (int)numberOfVisibleItems { return _visibleItems; }
- (void)setNumberOfVisibleItems:(int)visibleItems
{
if (_visibleItems > 10)
_visibleItems = visibleItems;
}
- (void)reloadData
{
}
- (void)noteNumberOfItemsChanged
{
}
- (BOOL)usesDataSource { return _usesDataSource; }
- (void)setUsesDataSource:(BOOL)flag
{
_usesDataSource = flag;
}
- (void)scrollItemAtIndexToTop:(int)index
{
}
- (void)scrollItemAtIndexToVisible:(int)index
{
}
- (void)selectItemAtIndex:(int)index
{
}
- (void)deselectItemAtIndex:(int)index
{
}
- (int)indexOfSelectedItem
{
return 0;
}
- (int)numberOfItems
{
SEL selector;
if (_usesDataSource)
{
if (!_dataSource)
NSLog(@"No DataSource Specified");
else
{
if ([[self controlView] isKindOfClass:[NSComboBox class]])
{
selector = @selector(numberOfItemsInComboBox:);
if ([_dataSource respondsToSelector:selector])
return [_dataSource numberOfItemsInComboBox:
(NSComboBox *)[self controlView]];
}
else
{
selector = @selector(numberOfItemsInComboBoxCell:);
if ([_dataSource respondsToSelector:selector])
return [_dataSource numberOfItemsInComboBoxCell:self];
}
}
}
else
return [_popUpList count];
return 0;
}
- (id)dataSource { return _dataSource; }
- (void)setDataSource:(id)aSource
{
if (_dataSource != aSource)
{
[_dataSource release];
_dataSource = [aSource retain];
}
}
- (void)addItemWithObjectValue:(id)object
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
else
[_popUpList addObject:object];
}
- (void)addItemsWithObjectValues:(NSArray *)objects
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
else
[_popUpList addObjectsFromArray:objects];
}
- (void)insertItemWithObjectValue:(id)object atIndex:(int)index
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
else
[_popUpList insertObject:object atIndex:index];
}
- (void)removeItemWithObjectValue:(id)object
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
else
[_popUpList removeObject:object];
}
- (void)removeItemAtIndex:(int)index
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
else
[_popUpList removeObjectAtIndex:index];
}
- (void)removeAllItems
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
else
[_popUpList removeAllObjects];
}
- (void)selectItemWithObjectValue:(id)object
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
}
- (id)itemObjectValueAtIndex:(int)index
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
return nil;
}
- (id)objectValueOfSelectedItem
{
if (_usesDataSource)
NSLog(@"Method Invalid: ComboBox uses dataSource");
return nil;
}
- (int)indexOfItemWithObjectValue:(id)object
{
if (_usesDataSource)
{
NSLog(@"Method Invalid: ComboBox uses dataSource");
return 0;
}
return [_popUpList indexOfObject:object];
}
- (NSArray *)_dataSourceObjectValues
{
NSMutableArray *array = nil;
id obj;
SEL selector;
int i,cnt;
if (!_dataSource)
NSLog(@"No DataSource Specified");
else
{
cnt = [self numberOfItems];
if ([[self controlView] isKindOfClass:[NSComboBox class]])
{
obj = [self controlView];
selector = @selector(comboBox:objectValueForItemAtIndex:);
if ([_dataSource respondsToSelector:selector])
{
array = [NSMutableArray array];
for (i=0;i<cnt;i++)
[array addObject:[_dataSource comboBox:obj
objectValueForItemAtIndex:i]];
}
}
else
{
obj = self;
selector = @selector(comboBoxCell:indexOfItemWithStringValue:);
if ([_dataSource respondsToSelector:selector])
{
array = [NSMutableArray array];
for (i=0;i<cnt;i++)
[array addObject:[_dataSource comboBoxCell:obj
objectValueForItemAtIndex:i]];
}
}
}
return array;
}
- (NSArray *)objectValues
{
if (_usesDataSource)
return [self _dataSourceObjectValues];
return _popUpList;
}
- (void)performPopUsingSelector:(SEL)aSelector
inRect:(NSRect)cellFrame
ofView:(NSView *)controlView
{
_canPop = YES;
_popRect = cellFrame;
_popView = [controlView retain];
[self performSelector:aSelector withObject:self];
[_popView release];
_popView = nil;
_canPop = NO;
_popRect = NSZeroRect;
}
#define CBButtonWidth 18
#define CBFrameWidth 2
- (NSRect)textCellFrameFromRect:(NSRect)cellRect
{
return NSMakeRect(NSMinX(cellRect),
NSMinY(cellRect),
NSWidth(cellRect)-CBButtonWidth,
NSHeight(cellRect));
}
- (NSRect)buttonCellFrameFromRect:(NSRect)cellRect
{
return NSMakeRect(NSMaxX(cellRect)-CBButtonWidth,
NSMinY(cellRect)+CBFrameWidth,
CBButtonWidth,
NSHeight(cellRect)-(CBFrameWidth*2.0));
}
- (void)didClick:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView
{
NSPoint point;
point = [theEvent locationInWindow];
point = [controlView convertPoint:point fromView:nil];
if (NSPointInRect(point,[self buttonCellFrameFromRect:cellFrame]))
{
[_buttonCell setCellAttribute:NSCellHighlighted to:1];
[controlView displayRect:cellFrame];
// [[NSDPSContext currentContext] flush];
// [[controlView window] display];
[self performPopUsingSelector: @selector(_didClick:)
inRect:cellFrame
ofView:controlView];
[_buttonCell setCellAttribute:NSCellHighlighted to:0];
[controlView displayRect:cellFrame];
// [[NSDPSContext currentContext] flush];
// [[controlView window] display];
}
}
// Overridden
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// if ([[NSDPSContext currentContext] isDrawingToScreen])
// {
[super drawWithFrame:[self textCellFrameFromRect:cellFrame]
inView:controlView];
[_buttonCell drawWithFrame:[self buttonCellFrameFromRect:cellFrame]
inView:controlView];
// }
// else
// [super drawWithFrame:cellFrame inView:controlView];
}
- (void)highlight:(BOOL)flag
withFrame:(NSRect)cellFrame
inView:(NSView *)controlView
{
// if ([[NSDPSContext currentContext] isDrawingToScreen])
// {
[super highlight:flag
withFrame:[self textCellFrameFromRect:cellFrame]
inView:controlView];
[_buttonCell highlight:flag
withFrame:[self buttonCellFrameFromRect:cellFrame]
inView:controlView];
// }
// else
// [super highlight:flag withFrame:cellFrame inView:controlView];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject
start:(int)selStart length:(int)selLength
{
[super selectWithFrame:[self textCellFrameFromRect:aRect]
inView:controlView
editor:textObj delegate:anObject
start:selStart length:selLength];
}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject
event:(NSEvent *)theEvent
{
[super editWithFrame:[self textCellFrameFromRect:aRect]
inView:controlView
editor:textObj delegate:anObject
event:theEvent];
}
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView untilMouseUp:(BOOL)flag
{
NSEvent *nEvent;
BOOL rValue;
NSPoint point;
rValue = [super trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:flag];
nEvent = [NSApp currentEvent];
if ([theEvent type] == NSLeftMouseDown &&
[nEvent type] == NSLeftMouseUp)
{
point = [controlView convertPoint:[theEvent locationInWindow]
fromView:nil];
if (NSPointInRect(point,cellFrame))
{
point = [controlView convertPoint:[nEvent locationInWindow]
fromView:nil];
if (NSPointInRect(point,cellFrame))
[self didClick:nEvent inRect:cellFrame ofView:controlView];
}
}
_mUpEvent = nEvent;
return rValue;
}
- (void)resetCursorRect:(NSRect)cellFrame inView:(NSView *)controlView
{
[super resetCursorRect:[self textCellFrameFromRect:cellFrame]
inView:controlView];
}
- (void)setEnabled:(BOOL)flag
{
[_buttonCell setEnabled:flag];
[super setEnabled:flag];
}
// NSCoding
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
}
- (id)initWithCoder:(NSCoder *)coder
{
return [super initWithCoder:coder];
}
@end
@implementation NSComboBoxCell(_Private_)
- (void)_createButtonCell
{
NSImage *image;
image = [self _buttonImage];
_buttonCell = [[NSButtonCell alloc] initImageCell:image];
[_buttonCell setImagePosition:NSImageOnly];
[_buttonCell setButtonType:NSMomentaryPushButton];
[_buttonCell setHighlightsBy:NSPushInCellMask];
[_buttonCell setBordered:YES];
[_buttonCell setTarget:self];
[_buttonCell setAction: @selector(_didClick:)];
}
- (void)_didClick:(id)sender
{
NSSize size;
NSPoint point,oldPoint;
if (![self isEnabled])
return;
if (![self controlView])
control_view = _popView;
size = [[self _popUp] popUpCellSizeForPopUp:self];
if (size.width == 0 || size.height == 0)
return;
point = _popRect.origin;
if ([_popView isFlipped])
point.y += NSHeight(_popRect);
point = [_popView convertPoint:point toView:nil];
point.y -= 1.0;
point = [[_popView window] convertBaseToScreen:point];
point.y -= size.height;
if (point.y >= 0)
goto popUp;
oldPoint = point;
point = _popRect.origin;
if (![_popView isFlipped])
point.y += NSHeight(_popRect);
point = [[_popView window] convertBaseToScreen:point];
if (point.y > NSHeight([[[_popView window] screen] frame]))
point = oldPoint;
if (point.y+size.height > NSHeight([[[_popView window] screen] frame]))
point.y = NSHeight([[[_popView window] screen] frame]) - size.height;
popUp:
if (point.x+size.width > NSWidth([[[_popView window] screen] frame]))
point.x = NSWidth([[[_popView window] screen] frame]) - size.width;
if (point.x < 0.0)
point.x = 0.0;
[[self _popUp] popUpCell:self popUpAt:point width:NSWidth(_popRect)];
}
- (NSImage *)_buttonImage
{
return [NSImage imageNamed: @"NSComboArrow"];
}
- (NSEvent *)_mouseUpEvent
{
return _mUpEvent;
_mUpEvent = nil;
}
- (GSComboWindow *)_popUp
{
return [GSComboWindow defaultPopUp];
}
@end