Skip to content

Commit

Permalink
Added support for toolbars (and toolbar buttons).
Browse files Browse the repository at this point in the history
* Added toolbar element.
* Added barbuttonitem element.
* Added sample6.xml with a toolbar example.
  • Loading branch information
jonathanellis committed Mar 2, 2012
1 parent 27b55d9 commit cd88a9c
Show file tree
Hide file tree
Showing 25 changed files with 268 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Pegasus/PGAdapter.h
Expand Up @@ -23,7 +23,7 @@

@protocol PGAdapter <NSObject>

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes;
+ (id)internalViewWithAttributes:(NSDictionary *)attributes;
+ (NSString *)name;
+ (NSDictionary *)properties;

Expand Down
1 change: 1 addition & 0 deletions Pegasus/PGTranslators.h
Expand Up @@ -48,6 +48,7 @@
+ (UITableViewStyle)tableViewStyleWithString:(NSString *)string;
+ (UITableViewCellSeparatorStyle)tableViewCellSeparatorStyleWithString:(NSString *)string;
+ (UIButtonType)buttonTypeWithString:(NSString *)string;
+ (UIBarButtonItemStyle)buttonItemStyleWithString:(NSString *)string;

#pragma mark - Translators (Objects)
+ (NSString *)stringWithString:(NSString *)string;
Expand Down
8 changes: 8 additions & 0 deletions Pegasus/PGTranslators.m
Expand Up @@ -47,6 +47,7 @@ + (SEL)translatorForType:(NSString *)type {
// if ([type isEqualToString:@"UITableViewStyle"]) return @selector(tableViewStyleWithString:);
if ([type isEqualToString:@"UITableViewCellSeparatorStyle"]) return @selector(tableViewCellSeparatorStyleWithString:);
// if ([type isEqualToString:@"UIButtonType"]) return @selector(buttonTypeWithString:);
if ([type isEqualToString:@"UIBarButtonItemStyle"]) return @selector(buttonItemStyleWithString:);

// Objects:
if ([type isEqualToString:@"NSString"]) return @selector(stringWithString:);
Expand Down Expand Up @@ -207,6 +208,13 @@ + (UIButtonType)buttonTypeWithString:(NSString *)string {
return 0;
}

+ (UIBarButtonItemStyle)buttonItemStyleWithString:(NSString *)string {
if ([string isEqualToString:@"plain"]) return UIBarButtonItemStylePlain;
if ([string isEqualToString:@"bordered"]) return UIBarButtonItemStyleBordered;
if ([string isEqualToString:@"done"]) return UIBarButtonItemStyleDone;
return 0;
}

#pragma mark - Translators (Objects)

+ (NSString *)stringWithString:(NSString *)string {
Expand Down
2 changes: 2 additions & 0 deletions Pegasus/Pegasus.h
Expand Up @@ -43,6 +43,8 @@
#import "PGSwitch.h"
#import "PGTableView.h"
#import "PGTableViewCell.h"
#import "PGToolbar.h"
#import "PGBarButtonItem.h"

// Layouts
#import "PGLayout.h"
Expand Down
24 changes: 24 additions & 0 deletions Pegasus/Views/PGBarButtonItem.h
@@ -0,0 +1,24 @@
//
// PGBarButtonItem.h
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGView.h"

@interface PGBarButtonItem : PGView

@end
42 changes: 42 additions & 0 deletions Pegasus/Views/PGBarButtonItem.m
@@ -0,0 +1,42 @@
//
// PGBarButtonItem.m
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGBarButtonItem.h"

@implementation PGBarButtonItem

+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UIBarButtonItem alloc] init];
}

+ (NSString *)name {
return @"barbuttonitem";
}

+ (NSDictionary *)properties {

NSMutableDictionary *properties =[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"NSString", @"title",
@"UIBarButtonItemStyle", @"style",
nil];

return properties;
}

@end
4 changes: 2 additions & 2 deletions Pegasus/Views/PGButton.m
Expand Up @@ -21,8 +21,8 @@

@implementation PGButton

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
NSString *buttonTypeStr = [attributes objectForKey:@"buttonType"];
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
NSString *buttonTypeStr = [[attributes objectForKey:@"buttonType"] lowercaseString];
UIButtonType buttonType = [PGTranslators buttonTypeWithString:buttonTypeStr];
return [UIButton buttonWithType:buttonType];
}
Expand Down
2 changes: 1 addition & 1 deletion Pegasus/Views/PGImageView.m
Expand Up @@ -21,7 +21,7 @@

@implementation PGImageView

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UIImageView alloc] init];
}

Expand Down
2 changes: 1 addition & 1 deletion Pegasus/Views/PGLabel.m
Expand Up @@ -21,7 +21,7 @@

@implementation PGLabel

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UILabel alloc] init];
}

Expand Down
2 changes: 1 addition & 1 deletion Pegasus/Views/PGProgressView.m
Expand Up @@ -21,7 +21,7 @@

@implementation PGProgressView

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UIProgressView alloc] init];
}

Expand Down
2 changes: 1 addition & 1 deletion Pegasus/Views/PGScrollView.m
Expand Up @@ -21,7 +21,7 @@

@implementation PGScrollView

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UIScrollView alloc] init];
}

Expand Down
2 changes: 1 addition & 1 deletion Pegasus/Views/PGSwitch.m
Expand Up @@ -21,7 +21,7 @@

@implementation PGSwitch

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UISwitch alloc] init];
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions Pegasus/PGTableView.m → Pegasus/Views/PGTableView.m
Expand Up @@ -21,8 +21,8 @@

@implementation PGTableView

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
NSString *tableViewStyleStr = [attributes objectForKey:@"style"];
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
NSString *tableViewStyleStr = [[attributes objectForKey:@"style"] lowercaseString];
UITableViewStyle tableViewStyle = [PGTranslators tableViewStyleWithString:tableViewStyleStr];
return [[UITableView alloc] initWithFrame:CGRectZero style:tableViewStyle];
}
Expand Down
File renamed without changes.
Expand Up @@ -21,7 +21,7 @@

@implementation PGTableViewCell

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UITableViewCell alloc] init];
}

Expand Down
2 changes: 1 addition & 1 deletion Pegasus/Views/PGTextField.m
Expand Up @@ -21,7 +21,7 @@

@implementation PGTextField

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UITextField alloc] init];
}

Expand Down
24 changes: 24 additions & 0 deletions Pegasus/Views/PGToolbar.h
@@ -0,0 +1,24 @@
//
// UIToolbar.h
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGView.h"

@interface PGToolbar : PGView

@end
60 changes: 60 additions & 0 deletions Pegasus/Views/PGToolbar.m
@@ -0,0 +1,60 @@
//
// UIToolbar.m
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGToolbar.h"

@implementation PGToolbar

+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UIToolbar alloc] init];
}

+ (NSString *)name {
return @"toolbar";
}

+ (NSDictionary *)properties {

NSMutableDictionary *properties =[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"UIBarStyle", @"text",
@"BOOL", @"translucent",
@"UIColor", @"tintColor",
nil];

[properties addEntriesFromDictionary:[PGView properties]];

return properties;
}

- (void)addSubview:(PGView *)subview {
if ([subview isKindOfClass:[PGBarButtonItem class]]) {

UIToolbar *toolbar = (UIToolbar *)view;

NSMutableArray *items = [NSMutableArray arrayWithArray:toolbar.items];
[items addObject:subview.view];
toolbar.items = items;

} else {
[super addSubview:subview];
}
}


@end
4 changes: 2 additions & 2 deletions Pegasus/Views/PGView.h
Expand Up @@ -29,11 +29,11 @@
NSArray *groups;
PGLayout *layout;

UIView *view;
id view;
NSMutableArray *subviews;
}

@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) id view;

+ (PGView *)viewWithString:(NSString *)string;
+ (PGView *)viewWithContentsOfFile:(NSString *)file;
Expand Down
52 changes: 30 additions & 22 deletions Pegasus/Views/PGView.m
Expand Up @@ -57,6 +57,8 @@ + (PGView *)viewWithElement:(CXMLElement *)element {
@"PGProgressView",
@"PGTableView",
@"PGTableViewCell",
@"PGToolbar",
@"PGBarButtonItem",
nil];

// Search for class matching the tag name
Expand Down Expand Up @@ -110,27 +112,33 @@ - (id)initWithElement:(CXMLElement *)element {
}
}

// Finalize layout:
if (!layout) layout = [[PGLayout alloc] init];
layout.size = view.frame.size;

// Subviews:
for (CXMLElement *childElement in element.children) {
if ([childElement isKindOfClass:[CXMLElement class]]) {
PGView *subview = [PGView viewWithElement:childElement];

if (subview) {
[self addSubview:subview];
} else {
NSLog(@"Pegasus Error: No corresponding class for element '%@'. Ignoring!", childElement.name);
}
// If this is a view (as opposed to a barbutton item, for instance):
if ([view isKindOfClass:[UIView class]]) {

// Finalize layout:
if (!layout) layout = [[PGLayout alloc] init];

layout.size = ((UIView *)view).frame.size;

// Subviews:
for (CXMLElement *childElement in element.children) {
if ([childElement isKindOfClass:[CXMLElement class]]) {
PGView *subview = [PGView viewWithElement:childElement];

if (subview) {
[self addSubview:subview];
} else {
NSLog(@"Pegasus Error: No corresponding class for element '%@'. Ignoring!", childElement.name);
}

}
}

// Add views according to layout:
[layout addViewsToSuperview:view];

}

// Add views according to layout:
[layout addViewsToSuperview:view];

}
return self;
}
Expand Down Expand Up @@ -183,7 +191,7 @@ - (NSArray *)findViewsInGroup:(NSString *)subviewGroup {

#pragma mark - PGViewAdapter methods

+ (UIView *)internalViewWithAttributes:(NSDictionary *)attributes {
+ (id)internalViewWithAttributes:(NSDictionary *)attributes {
return [[UIView alloc] init];
}

Expand Down Expand Up @@ -229,13 +237,13 @@ - (void)setValue:(NSString *)string forVirtualProperty:(NSString *)propertyName
} else if ([propertyName isEqualToString:@"layout"]) {
layout = [PGLayout layoutWithString:string];
} else if ([propertyName isEqualToString:@"size"]) {
CGRect frame = view.frame;
CGRect frame = ((UIView *)view).frame;
frame.size = CGSizeFromString(string);
view.frame = frame;
((UIView *)view).frame = frame;
} else if ([propertyName isEqualToString:@"origin"]) {
CGRect frame = view.frame;
CGRect frame = ((UIView *)view).frame;
frame.origin = CGPointFromString(string);
view.frame = frame;
((UIView *)view).frame = frame;
}
}

Expand Down

0 comments on commit cd88a9c

Please sign in to comment.