Skip to content

Instantly share code, notes, and snippets.

@Alos
Created September 28, 2008 20:17
Show Gist options
  • Save Alos/13500 to your computer and use it in GitHub Desktop.
Save Alos/13500 to your computer and use it in GitHub Desktop.
/*The Music browser*/
@implementation SFBrowser : CPWindow
{
SFTable libraryTable;
}
/*Una bonita contructora*/
- (id)initWithSource:(CPArray)list{
//Creamos un HUD como panel para que pueda esta arriba de todas las demas ventanas
self = [[CPPanel alloc] initWithContentRect:CGRectMake(400, 50, 800, 500)styleMask:CPHUDBackgroundWindowMask|CPResizableWindowMask];
if (self)//pa ver si no somos null :P
{
console.log("Nueva ventana");
//le ponemos titulo al HUD lo centramos
[self setTitle:@"Music Browser"];
[self setFloatingPanel:YES];
[self setDelegate:self];
var contentView = [self contentView];
var bounds = [contentView bounds];
//para los titulos
var cmArray = [[CPArray alloc]init];
var theColor= [CPColor colorWithHexString:"99CCFF"];
var titleLabel =[[SFColumnModel alloc] initWithFrame:CGRectMake(0, 0, 300, 31) title:@"Name:" color:NULL];
var artistLabel =[[SFColumnModel alloc] initWithFrame:CGRectMake(301, 0, 200, 31) title:@"Artist:" color: NULL];
[cmArray addObject: artistLabel];
[cmArray addObject: titleLabel];
//a table
libraryTable = [[SFTable alloc] initWithColumnModel: cmArray model:list frame: bounds];
[contentView addSubview: libraryTable];
}
return self;
}
-(void)addItem:(CPObject)anObject{
[libraryTable addItem:anObject];
}
-(void)addList:(CPArray)aModel
{ console.log("addlist here!");
[libraryTable setModel: aModel];
}
@end
/*El modelo de la columna es responsable de saber de que colo y tamaño se tiene que pintar cada columna de la tabla*/
@implementation SFColumnModel : CPView
{
CPTextField columnTitle;
CPString name;
}
-(id)initWithFrame:(CPRect)aFrame title:(CPString)aTitle color:(CPColor)aColor{
self = [super initWithFrame: aFrame];
name=aTitle;
var title = [[CPTextField alloc]initWithFrame: aFrame];
[title setStringValue:aTitle]
[title setTextColor: [CPColor colorWithHexString:"33FF00"]];
[title setBackgroundColor: aColor];
[self addSubview: title];
return self;
}
-(CPTextField)columnTitle{
return columnTitle;
}
-(CPString)name{
return name;
}
@end
import <Foundation/CPObject.j>
@implementation SFTable : CPView
{
/*Para las tablas*/
CPCollectionView collectionView;
CPArray model;
/*Cosas para los titulos*/
CPArray columnModel
/*Cosas para las celdas*/
SFCell celdas;
}
-(void) initWithColumnModel:(CPArray)aColumnModel model:(CPArray)aModel frame:(CGRect)bounds{
self = [super initWithFrame:bounds];
[self setModel:aModel];
//para nuestro grid
collectionView = [[CPCollectionView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
//los scrolls por si son muchos
var scrollView = [[CPScrollView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
[scrollView setAutohidesScrollers: YES];
[scrollView setDocumentView: collectionView];
[[scrollView contentView] setBackgroundColor: NULL];
[scrollView setHasHorizontalScroller:NO]
[scrollView setAutoresizingMask: CPViewWidthSizable];
[scrollView setAutoresizesSubviews:YES];
//los items q representan los renglones
var listItem = [[CPCollectionViewItem alloc] init];
celdas = [[SFCell alloc] initWithFrame:CGRectMakeZero()];
[listItem setView: celdas];
[collectionView setItemPrototype: listItem];
[collectionView setMaxNumberOfColumns:1];
[collectionView setMinItemSize:CPSizeMake(CGRectGetWidth(bounds), 20)];
[collectionView setMaxItemSize:CPSizeMake(CGRectGetWidth(bounds), 20)];
[collectionView setContent: model];
[self addSubview:scrollView];
//la q esta arriba del Collectionview
var borderArriba = [[CPView alloc] initWithFrame:CGRectMake(0, 30 , CGRectGetWidth(bounds), 1)];
[borderArriba setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: borderArriba];
//la de arriba
var borderTop = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), 1)];
[borderTop setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: borderTop];
[self setColumnModel:aColumnModel];
return self;
}
-(void)setColumnModel:(CPArray)aColumnModel{
console.log("Setting the column model...");
columnModel = aColumnModel;
for(var i=0; i<[columnModel count];i++){
var thisColumn = [columnModel objectAtIndex:i];
[self addSubview: thisColumn];
if(i>0){
var border = [[CPView alloc] initWithFrame:CGRectMake(CGRectGetWidth([[columnModel objectAtIndex: i-1] bounds])+1 , 0, 1, CGRectGetHeight([self bounds]))];
[border setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[border setAutoresizingMask: CPViewWidthSizable];
[self addSubview: border];
}
}
}
-(void)setModel:(CPArray)aModel{
model = aModel;
[collectionView setContent: model];
[collectionView reloadContent];
}
/**
Adds an item to the table
@param the item to add to the table
*/
-(void)addItem():(CPObject)anItem{
[model addObject:anItem];
[collectionView setModel: model];
[collectionView reloadContent];
}
/**
@param anIndex the value where the item you want t remove is
*/
-(void)removeItem():(int)anIndex{
[model removeObjectAtIndex: anIndex];
[collectionView reloadContent];
}
/**
Returns the item that is currently selected
@return
*/
-(int)getSelectedItem{
return [[collectionView selectionIndexes] firstIndex];
}
/**
Removes selected items
*/
-(void)removeSelectedItems{
var indexes= [collectionView selectionIndexes];
var a = [indexes firstIndex];
[model removeObjectAtIndex: a];
[collectionView reloadContent];
}
-(CPIndexSet)getSelectedItems{
return [collectionView selectionIndexes];
}
@end
@implementation SFCell : CPView
{
CPTextField titleView;
CPTextField authorView;
CPView highlightView;
}
- (void)setRepresentedObject:(JSObject)anObject
{
if(!titleView)
{
titleView = [[CPTextField alloc] initWithFrame:CGRectInset( [self bounds], 4, 4)];
[titleView setFont: [CPFont systemFontOfSize: 12.0]];
[titleView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: titleView];
}
[titleView setStringValue: [anObject songTitle]];
[titleView sizeToFit];
[titleView setFrameOrigin: CGPointMake(5,0.0)];
if(!authorView)
{
authorView = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 4, 4)];
[authorView setFont: [CPFont systemFontOfSize: 12.0]];
[authorView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: authorView];
}
[authorView setStringValue: [anObject artist]];
[authorView sizeToFit];
[authorView setFrameOrigin: CGPointMake(251,0.0)];
}
- (void)setSelected:(BOOL)flag
{
if(!highlightView)
{
highlightView = [[CPView alloc] initWithFrame:CGRectCreateCopy([self bounds])];
[highlightView setBackgroundColor: [CPColor greenColor]];
}
if(flag)
{
[self addSubview:highlightView positioned:CPWindowBelow relativeTo: titleView];
[titleView setTextColor: [CPColor blackColor]];
[authorView setTextColor: [CPColor blackColor]];
}
else
{
[highlightView removeFromSuperview];
[titleView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[authorView setTextColor: [CPColor colorWithHexString:"33FF00"]];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment