API 0.9.5
AppKit/CPCheckBox.j
Go to the documentation of this file.
00001 /*
00002  * CPCheckBox.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2009, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 CPCheckBoxImageOffset = 4.0;
00026 @implementation CPCheckBox : CPButton
00027 {
00028     id __doxygen__;
00029 }
00030 
00031 + (id)checkBoxWithTitle:(CPString)aTitle theme:(CPTheme)aTheme
00032 {
00033     return [self buttonWithTitle:aTitle theme:aTheme];
00034 }
00035 
00036 + (id)checkBoxWithTitle:(CPString)aTitle
00037 {
00038     return [self buttonWithTitle:aTitle];
00039 }
00040 
00041 + (CPString)defaultThemeClass
00042 {
00043     return @"check-box";
00044 }
00045 
00046 + (Class)_binderClassForBinding:(CPString)theBinding
00047 {
00048     if (theBinding === CPValueBinding)
00049         return [_CPCheckBoxValueBinder class];
00050 
00051     return [super _binderClassForBinding:theBinding];
00052 }
00053 
00054 - (id)initWithFrame:(CGRect)aFrame
00055 {
00056     self = [super initWithFrame:aFrame];
00057 
00058     if (self)
00059     {
00060         [self setHighlightsBy:CPContentsCellMask];
00061         [self setShowsStateBy:CPContentsCellMask];
00062 
00063         // Defaults?
00064         [self setImagePosition:CPImageLeft];
00065         [self setAlignment:CPLeftTextAlignment];
00066 
00067         [self setBordered:NO];
00068     }
00069 
00070     return self;
00071 }
00072 
00073 - (void)takeStateFromKeyPath:(CPString)aKeyPath ofObjects:(CPArray)objects
00074 {
00075     var count = objects.length,
00076         value = [objects[0] valueForKeyPath:aKeyPath] ? CPOnState : CPOffState;
00077 
00078     [self setAllowsMixedState:NO];
00079     [self setState:value];
00080 
00081     while (count-- > 1)
00082     {
00083         if (value !== ([objects[count] valueForKeyPath:aKeyPath] ? CPOnState : CPOffState))
00084         {
00085             [self setAllowsMixedState:YES];
00086             [self setState:CPMixedState];
00087         }
00088     }
00089 }
00090 
00091 - (void)takeValueFromKeyPath:(CPString)aKeyPath ofObjects:(CPArray)objects
00092 {
00093     [self takeStateFromKeyPath:aKeyPath ofObjects:objects];
00094 }
00095 
00096 @end
00097 @implementation _CPCheckBoxValueBinder : CPBinder
00098 {
00099     id __doxygen__;
00100 }
00101 
00102 - (void)setValueFor:(CPString)theBinding
00103 {
00104     var destination = [_info objectForKey:CPObservedObjectKey],
00105         keyPath = [_info objectForKey:CPObservedKeyPathKey],
00106         options = [_info objectForKey:CPOptionsKey],
00107         newValue = [destination valueForKeyPath:keyPath],
00108         isPlaceholder = CPIsControllerMarker(newValue);
00109 
00110     if (isPlaceholder)
00111     {
00112         switch (newValue)
00113         {
00114             case CPMultipleValuesMarker:
00115                 newValue = CPMixedState;
00116                 break;
00117 
00118             case CPNoSelectionMarker:
00119                 newValue = CPOffState;
00120                 break;
00121 
00122             case CPNotApplicableMarker:
00123                 if ([options objectForKey:CPRaisesForNotApplicableKeysBindingOption])
00124                     [CPException raise:CPGenericException reason:@"can't transform non applicable key on: "+_source+" value: "+newValue];
00125 
00126                 newValue = CPOffState;
00127                 break;
00128         }
00129 
00130         if (newValue === CPMixedState)
00131         {
00132             [_source setAllowsMixedState:YES];
00133         }
00134         else
00135         {
00136             // Cocoa will always set allowsMixedState to NO
00137             // This behavior will be fine for Cappuccino as well if we (like Cocoa)
00138             // default the CPConditionallySetsEnabledBindingOption to YES
00139             [_source setAllowsMixedState:NO];
00140         }
00141     }
00142 
00143     [_source setState:newValue];
00144 }
00145 
00146 @end
 All Classes Files Functions Variables Defines