/* * CPApplication.j * AppKit * * Created by Ross Boucher. * Copyright 2008, 280 North, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ import import "CPView.j" import "CPButton.j" import "CPImage.j" import "CPImageView.j" import "CPColorPicker.j" import "CPColorPanel.j" import "CPTabView.j" import "CPTabViewItem.j" @implementation CPKulerColorPicker : CPColorPicker { CPView _contentView; CPTabView _tabView; CPScrollView _searchView; CPScrollView _popularView; CPView _searchView; DOMElement _FIXME_searchField; CPURLConnection _searchConnection; CPURLConnection _popularConnection } - (id)initWithPickerMask:(int)mask colorPanel:(CPColorPanel)owningColorPanel { return [super initWithPickerMask:mask colorPanel: owningColorPanel]; } -(id)initView { aFrame = CPRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight); _contentView = [[CPView alloc] initWithFrame:aFrame]; _tabView = [[CPTabView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(aFrame), CGRectGetHeight(aFrame) - 20)]; [_tabView setAutoresizingMask: CPViewWidthSizable | CPViewHeightSizable]; [_tabView setDelegate: self]; [_contentView addSubview: _tabView]; var label = [[CPButton alloc] initWithFrame:CPRectMake((CPColorPickerViewWidth-150)/2, CPColorPickerViewHeight-20, 150, 20)]; [label setAutoresizingMask: CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin]; [label setFont:[CPFont boldSystemFontOfSize:10.0]]; [label setTextColor: [CPColor whiteColor]]; [label setTitle: @"Powered by Adobe Kuler"]; [label setTarget: self]; [label setAction: @selector(openKulerLink:)]; [label setBordered: NO]; [_contentView addSubview: label]; var searchThemeView = [[_CPKulerThemeView alloc] initWithFrame: CPRectMake(0, 0, aFrame.size.width, 0)]; [searchThemeView setDelegate: self]; [searchThemeView setAutoresizingMask: CPViewWidthSizable]; _searchView = [[CPScrollView alloc] initWithFrame: CPRectMake(0, 0, aFrame.size.width, CPColorPickerViewHeight - 10)]; [_searchView setDocumentView: searchThemeView]; [_searchView setHasHorizontalScroller: NO]; [_searchView setAutoresizingMask: (CPViewWidthSizable | CPViewHeightSizable)]; [[_searchView verticalScroller] setControlSize:CPSmallControlSize]; [_searchView setAutohidesScrollers:YES]; var auxiliarySearchView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, CPColorPickerViewWidth, 26.0)]; [auxiliarySearchView setAutoresizingMask: (CPViewMinYMargin | CPViewWidthSizable)]; _FIXME_searchField = document.createElement("input"); if(!window.addEventListener) _FIXME_searchField.type = "text"; else _FIXME_searchField.type = "search"; _FIXME_searchField.style.position = "absolute"; _FIXME_searchField.style.width = "96%"; _FIXME_searchField.style.left = "2%"; _FIXME_searchField.style.top = "2px"; _FIXME_searchField.onkeypress = function(aDOMEvent) { aDOMEvent = aDOMEvent || window.event; if (aDOMEvent.keyCode == 13) { [self search]; if(aDOMEvent.preventDefault) aDOMEvent.preventDefault(); else if(aDOMEvent.stopPropagation) aDOMEvent.stopPropagation(); _FIXME_searchField.blur(); } }; auxiliarySearchView._DOMElement.appendChild(_FIXME_searchField); var popularThemeView = [[_CPKulerThemeView alloc] initWithFrame: CPRectMake(0, 0, aFrame.size.width, 0)]; [popularThemeView setDelegate: self]; [popularThemeView setAutoresizingMask: CPViewWidthSizable]; _popularView = [[CPScrollView alloc] initWithFrame: CPRectMake(0, 0, aFrame.size.width, CPColorPickerViewHeight)]; [_popularView setDocumentView: popularThemeView]; [_popularView setHasHorizontalScroller: NO]; [_popularView setAutoresizingMask: (CPViewWidthSizable | CPViewHeightSizable)]; [[_popularView verticalScroller] setControlSize:CPSmallControlSize]; [_popularView setAutohidesScrollers:YES]; var mostPopularItem = [[CPTabViewItem alloc] initWithIdentifier:@"Popular"]; var searchItem = [[CPTabViewItem alloc] initWithIdentifier:@"Search"]; [searchItem setLabel:@"Search"]; [searchItem setView:_searchView]; [searchItem setAuxiliaryView: auxiliarySearchView]; [mostPopularItem setLabel:@"Popular"]; [mostPopularItem setView: _popularView]; [_tabView addTabViewItem: mostPopularItem]; [_tabView addTabViewItem: searchItem]; [self popularThemes]; } - (void)openKulerLink:(id)sender { window.open("http://kuler.adobe.com"); } - (CPView)provideNewView:(BOOL)initialRequest { if (initialRequest) [self initView]; return _contentView; } - (int)currentMode { return CPKulerColorPickerMode; } - (BOOL)supportsMode:(int)mode { return (mode == CPKulerColorPickerMode) ? YES : NO; } -(void)search { var query = escape(_FIXME_searchField.value); if(query.replace("%20", "") == "") return; [_searchConnection cancel]; _searchConnection = nil; _searchConnection = [CPURLConnection connectionWithRequest: [CPURLRequest requestWithURL: BASE_URL + "kuler.php?mode=search&query="+query] delegate: self]; } -(void)popularThemes { [_popularConnection cancel]; _popularConnection = nil; _popularConnection = [CPURLConnection connectionWithRequest: [CPURLRequest requestWithURL: BASE_URL + "kuler.php?mode=popular"] delegate: self]; } /* -(void)topRatedThemes { _connection = [CPURLConnection connectionWithRequest: [CPURLRequest requestWithURL: BASE_URL + "kuler.php?mode=rating"] delegate:self]; } */ - (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data { var data = CPJSObjectCreateWithJSON(data); if(!data) var themes = []; else var themes = data.result; if(aConnection == _popularConnection) [[_popularView documentView] setThemes: themes]; if(aConnection == _searchConnection) [[_searchView documentView] setThemes: themes]; } - (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError { [self connectionDidFinishLoading:aConnection]; } -(void)connectionDidFinishLoading:(CPURLConnection)aConnection { } -(void)chooseColor:(CPColor)aColor { [[self colorPanel] setColor: aColor]; } @end @implementation _CPKulerThemeView : CPView { CPArray _themes; id _delegate; CPColor _alternateBGColor; } -(id)initWithFrame:(CPRect)aFrame { self = [super initWithFrame:aFrame]; _alternateBGColor = [CPColor colorWithCalibratedRed: 241.0/255.0 green: 245.0/255.0 blue: 250.0/255.0 alpha: 1.0]; return self; } -(id)delegate { return _delegate; } -(void)setDelegate:(id)delegate { _delegate = delegate; } -(CPArray)themes { return _themes; } -(void)setThemes:(CPArray)themes { _themes = themes; [self updateDisplay]; } -(void)updateDisplay { var width = [self frame].size.width; var subviews = [self subviews]; for(var i = [subviews count]-1; i >= 0; --i) [subviews[i] removeFromSuperview]; for(var i=0, count = [_themes count]; i