Skip to content

Commit

Permalink
flexible items in UMEToolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
itod committed Jul 24, 2010
1 parent ac42856 commit 2abf65c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/UMEKit/UMEBarButtonItem.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ typedef enum {
CGFloat width; CGFloat width;


UMEBarStyle barStyle; UMEBarStyle barStyle;

BOOL space;
BOOL flexible;
} }


- (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)target action:(SEL)action; - (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
Expand Down
2 changes: 2 additions & 0 deletions include/UMEKit/UMEToolbar.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
NSColor *tintColor; NSColor *tintColor;
BOOL translucent; BOOL translucent;
BOOL layoutDone; BOOL layoutDone;
NSMutableArray *flexibleItems;
//NSUInteger numNonSpaceItems;
} }


@property (nonatomic, assign) UMEBarStyle barStyle; // default is UMEBarStyleDefault (blue) @property (nonatomic, assign) UMEBarStyle barStyle; // default is UMEBarStyleDefault (blue)
Expand Down
1 change: 1 addition & 0 deletions src/UMEActivityBarButtonItemButtonCell.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSPoint inner; NSPoint inner;
NSPoint outer; NSPoint outer;
[NSBezierPath setDefaultLineCapStyle:NSRoundLineCapStyle]; [NSBezierPath setDefaultLineCapStyle:NSRoundLineCapStyle];
strokeWidth = strokeWidth < 0.1 ? 0.1 :strokeWidth;
[NSBezierPath setDefaultLineWidth:strokeWidth]; [NSBezierPath setDefaultLineWidth:strokeWidth];
if ([self isSpinning]) { if ([self isSpinning]) {
a = (270+(step* 30))*DEG2RAD; a = (270+(step* 30))*DEG2RAD;
Expand Down
23 changes: 18 additions & 5 deletions src/UMEBarButtonItem.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ - (void)sizeToFit;
- (void)layout; - (void)layout;
@property (nonatomic, retain) NSButton *button; @property (nonatomic, retain) NSButton *button;
@property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault @property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault
@property (nonatomic, getter=isSpace) BOOL space;
@property (nonatomic, getter=isFlexible) BOOL flexible;
@end @end


@implementation UMEBarButtonItem @implementation UMEBarButtonItem
Expand Down Expand Up @@ -55,9 +57,12 @@ - (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)
aTitle = NSLocalizedString(@"Add", @""); aTitle = NSLocalizedString(@"Add", @"");
break; break;
case UMEBarButtonSystemItemFlexibleSpace: case UMEBarButtonSystemItemFlexibleSpace:
self.space = YES;
self.flexible = YES;
imgPath = [b pathForImageResource:@""]; imgPath = [b pathForImageResource:@""];
break; break;
case UMEBarButtonSystemItemFixedSpace: case UMEBarButtonSystemItemFixedSpace:
self.space = YES;
imgPath = [b pathForImageResource:@""]; imgPath = [b pathForImageResource:@""];
break; break;
case UMEBarButtonSystemItemCompose: case UMEBarButtonSystemItemCompose:
Expand Down Expand Up @@ -134,11 +139,15 @@ - (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)
break; break;
} }


self = [self initWithTitle:aTitle style:aStyle target:t action:sel]; if (self.isSpace) {
if ([imgPath length]) { self = [self initWithCustomView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
self.image = [[[NSImage alloc] initWithContentsOfFile:imgPath] autorelease]; } else {
self = [self initWithTitle:aTitle style:aStyle target:t action:sel];
if ([imgPath length]) {
self.image = [[[NSImage alloc] initWithContentsOfFile:imgPath] autorelease];
}
[button setImagePosition:imgPos];
} }
[button setImagePosition:imgPos];
return self; return self;
} }


Expand Down Expand Up @@ -199,7 +208,9 @@ - (void)sizeToFit {
[button sizeToFit]; [button sizeToFit];
//[button setFrameOrigin:NSZeroPoint]; //[button setFrameOrigin:NSZeroPoint];
[customView setFrameSize:[button frame].size]; [customView setFrameSize:[button frame].size];
} else { } else {
if (self.isFlexible) return;

if ([customView respondsToSelector:@selector(sizeToFit)]) { if ([customView respondsToSelector:@selector(sizeToFit)]) {
[customView performSelector:@selector(sizeToFit)]; [customView performSelector:@selector(sizeToFit)];
} else { } else {
Expand Down Expand Up @@ -282,4 +293,6 @@ - (void)setCustomView:(NSView *)v {
@synthesize style; @synthesize style;
@synthesize width; @synthesize width;
@synthesize barStyle; @synthesize barStyle;
@synthesize space;
@synthesize flexible;
@end @end
5 changes: 4 additions & 1 deletion src/UMEBarButtonItemButtonCell.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@


@interface UMEBarButtonItem () @interface UMEBarButtonItem ()
@property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault @property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault
@property (nonatomic, getter=isSpace) BOOL space;
@end @end


@interface UMEBarButtonItemButtonCell () @interface UMEBarButtonItemButtonCell ()
Expand Down Expand Up @@ -83,7 +84,7 @@ - (void)dealloc {




- (BOOL)isOpaque { - (BOOL)isOpaque {
return YES; return NO;
} }




Expand Down Expand Up @@ -271,6 +272,8 @@ - (void)drawInteriorWithFrame:(NSRect)r inView:(NSView *)cv {
UMEBarButtonItemButton *button = (UMEBarButtonItemButton *)cv; UMEBarButtonItemButton *button = (UMEBarButtonItemButton *)cv;
UMEBarButtonItem *item = button.item; UMEBarButtonItem *item = button.item;


if (item.isSpace) return;

// if below the min width, just clear and return (dont draw borked background image) // if below the min width, just clear and return (dont draw borked background image)
CGFloat minWidth = MIN_WIDTH; CGFloat minWidth = MIN_WIDTH;
if (NSImageOnly == [self imagePosition]) { if (NSImageOnly == [self imagePosition]) {
Expand Down
56 changes: 53 additions & 3 deletions src/UMEToolbar.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
@interface UMEBarButtonItem () @interface UMEBarButtonItem ()
- (void)sizeToFit; - (void)sizeToFit;
@property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault @property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault
@property (nonatomic, getter=isFlexible) BOOL flexible;
@end @end


@interface UMEToolbar () @interface UMEToolbar ()
- (void)layoutItems; - (void)layoutItems;
@property (nonatomic, retain) NSMutableArray *flexibleItems;
@end @end


@implementation UMEToolbar @implementation UMEToolbar
Expand All @@ -34,10 +36,16 @@ - (id)initWithFrame:(NSRect)frame {
- (void)dealloc { - (void)dealloc {
self.items = nil; self.items = nil;
self.tintColor = nil; self.tintColor = nil;
self.flexibleItems = nil;
[super dealloc]; [super dealloc];
} }




- (void)awakeFromNib {

}


#pragma mark - #pragma mark -
#pragma mark NSView #pragma mark NSView


Expand Down Expand Up @@ -82,7 +90,11 @@ - (void)drawRect:(NSRect)dirtyRect {
} }


NSRect bounds = [self bounds]; NSRect bounds = [self bounds];
NSDrawThreePartImage(bounds, bgImg, bgImg, bgImg, NO, NSCompositeSourceOver, 1, YES); NSDrawThreePartImage(bounds, nil, bgImg, nil, NO, NSCompositeSourceOver, 1, YES);

[[NSColor darkGrayColor] setStroke];
CGFloat y = bounds.size.height - 1;
[NSBezierPath strokeLineFromPoint:NSMakePoint(0, y) toPoint:NSMakePoint(bounds.size.width, y)];
} }




Expand All @@ -96,11 +108,24 @@ - (void)setItems:(NSArray *)a animated:(BOOL)animated {


- (void)setItems:(NSArray *)a { - (void)setItems:(NSArray *)a {
if (a != items) { if (a != items) {
for (UMEBarButtonItem *item in items) {
[item.customView removeFromSuperview];
}

[items autorelease]; [items autorelease];
items = [a retain]; items = [a retain];


self.flexibleItems = [NSMutableArray array];
//numNonSpaceItems = 0;

for (UMEBarButtonItem *item in items) { for (UMEBarButtonItem *item in items) {
item.barStyle = barStyle; item.barStyle = barStyle;
// if (!item.isSpace) {
// numNonSpaceItems++;
// }
if (item.isFlexible) {
[flexibleItems addObject:item];
}
} }


[self layoutItems]; [self layoutItems];
Expand All @@ -112,16 +137,40 @@ - (void)setItems:(NSArray *)a {
#pragma mark Private #pragma mark Private


- (void)layoutItems { - (void)layoutItems {
NSRect bounds = [self bounds];

CGFloat x = ITEM_X; CGFloat x = ITEM_X;
CGFloat y = 0; CGFloat y = 0;
CGFloat w = 0; CGFloat w = 0;
CGFloat h = 0; CGFloat h = bounds.size.height - 2.0; // room for bottom line

CGFloat availWidth = bounds.size.width;
CGFloat nonFlexibleItemsTotalWidth = ITEM_X * 2; // left and right margin

// calc total non-flexible width, and figure how many items can be visible in avail width
for (UMEBarButtonItem *item in items) {
CGFloat currWidth = NSWidth([item.customView frame]) + ITEM_MARGIN;
if (!item.isFlexible) {
nonFlexibleItemsTotalWidth += currWidth;
}
}

NSUInteger flexibleItemCount = [flexibleItems count];
BOOL itemsTruncated = (nonFlexibleItemsTotalWidth > availWidth);

if (flexibleItemCount > 0) {
CGFloat flexibleItemWidth = itemsTruncated ? 0 : (availWidth - nonFlexibleItemsTotalWidth) / flexibleItemCount;
for (UMEBarButtonItem *flexibleItem in flexibleItems) {
NSRect frame = [flexibleItem.customView frame];
frame.size.width = flexibleItemWidth;
[flexibleItem.customView setFrame:frame];
}
}


for (UMEBarButtonItem *item in items) { for (UMEBarButtonItem *item in items) {
//[item sizeToFit]; //[item sizeToFit];
[self addSubview:item.customView]; [self addSubview:item.customView];
w = [item width]; w = [item width];
h = NSHeight([item.customView frame]);
[item.customView setFrame:NSMakeRect(x, y, w, h)]; [item.customView setFrame:NSMakeRect(x, y, w, h)];
x += w + ITEM_MARGIN; x += w + ITEM_MARGIN;
} }
Expand All @@ -133,4 +182,5 @@ - (void)layoutItems {
@synthesize items; @synthesize items;
@synthesize tintColor; @synthesize tintColor;
@synthesize translucent; @synthesize translucent;
@synthesize flexibleItems;
@end @end

0 comments on commit 2abf65c

Please sign in to comment.