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
Expand Up @@ -56,6 +56,9 @@ typedef enum {
CGFloat width;

UMEBarStyle barStyle;

BOOL space;
BOOL flexible;
}

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

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

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

self = [self initWithTitle:aTitle style:aStyle target:t action:sel];
if ([imgPath length]) {
self.image = [[[NSImage alloc] initWithContentsOfFile:imgPath] autorelease];
if (self.isSpace) {
self = [self initWithCustomView:[[[NSView alloc] initWithFrame:NSZeroRect] 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;
}

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

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

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

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


- (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;
UMEBarButtonItem *item = button.item;

if (item.isSpace) return;

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

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

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


- (void)awakeFromNib {

}


#pragma mark -
#pragma mark NSView

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

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 {
if (a != items) {
for (UMEBarButtonItem *item in items) {
[item.customView removeFromSuperview];
}

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

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

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

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

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

CGFloat x = ITEM_X;
CGFloat y = 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) {
//[item sizeToFit];
[self addSubview:item.customView];
w = [item width];
h = NSHeight([item.customView frame]);
[item.customView setFrame:NSMakeRect(x, y, w, h)];
x += w + ITEM_MARGIN;
}
Expand All @@ -133,4 +182,5 @@ - (void)layoutItems {
@synthesize items;
@synthesize tintColor;
@synthesize translucent;
@synthesize flexibleItems;
@end

0 comments on commit 2abf65c

Please sign in to comment.