]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOCheckBox.m
use %p for pointer formats, fixed gcc 4.1 warnings, minor code improvs
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOCheckBox.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "WOInput.h"
23 #include "decommon.h"
24
25 @interface WOCheckBox : WOInput
26 {
27   // WODynamicElement: extraAttributes
28   // WODynamicElement: otherTagString
29   // inherited: name
30   // inherited: value
31   // inherited: disabled
32 @protected
33   WOAssociation *selection;
34   WOAssociation *checked;
35 }
36
37 @end /* WOCheckBox */
38
39 @implementation WOCheckBox
40
41 - (id)initWithName:(NSString *)_name
42   associations:(NSDictionary *)_config
43   template:(WOElement *)_c
44 {
45   if ((self = [super initWithName:_name associations:_config template:_c])) {
46     self->selection = OWGetProperty(_config, @"selection");
47     self->checked   = OWGetProperty(_config, @"checked");
48   }
49   return self;
50 }
51
52 - (void)dealloc {
53   [self->selection release];
54   [self->checked   release];
55   [super dealloc];
56 }
57
58 /* handling requests */
59
60 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
61   /*
62     Checkboxes are special in their form-value handling. If the form is
63     submitted and the checkbox is checked, a 'YES' value is transferred in the
64     request. If the checkbox is not-checked, no value is transferred at all !
65   */
66   id formValue;
67   
68   if ([self->disabled boolValueInComponent:[_ctx component]])
69     return;
70
71   formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)];
72     
73   if ([self->checked isValueSettable]) {
74       [self->checked setBoolValue:formValue ? YES : NO
75                      inComponent:[_ctx component]];
76   }
77   
78   if ([self->value isValueSettable] && (formValue != nil))
79     [self->value setStringValue:formValue inComponent:[_ctx component]];
80 }
81
82 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
83   NSString *v;
84   BOOL     isChecked;
85   
86   if ([[_ctx request] isFromClientComponent])
87     return;
88
89   v         = [self->value   stringValueInComponent:[_ctx component]];
90   isChecked = [self->checked boolValueInComponent:[_ctx component]];
91     
92   WOResponse_AddCString(_response, "<input type=\"checkbox\" name=\"");
93   [_response appendContentHTMLAttributeValue:
94                OWFormElementName(self, _ctx)];
95   WOResponse_AddCString(_response, "\" value=\"");
96   [_response appendContentHTMLAttributeValue:
97                ([v isNotEmpty] ? v : (NSString *)@"1")];
98   WOResponse_AddCString(_response, "\"");
99   
100   if ([self->disabled boolValueInComponent:[_ctx component]]) {
101     WOResponse_AddCString(_response,
102                           (_ctx->wcFlags.allowEmptyAttributes
103                            ? " disabled" : " disabled=\"disabled\""));
104   }
105   
106   if (isChecked) {
107     WOResponse_AddCString(_response,
108                           (_ctx->wcFlags.allowEmptyAttributes 
109                            ? " checked" : " checked=\"checked\""));
110   }
111   
112   [self appendExtraAttributesToResponse:_response inContext:_ctx];
113   
114   if (self->otherTagString) {
115     WOResponse_AddChar(_response, ' ');
116     WOResponse_AddString(_response,
117                          [self->otherTagString stringValueInComponent:
118                                 [_ctx component]]);
119   }
120   
121   if (_ctx->wcFlags.xmlStyleEmptyElements) {
122     WOResponse_AddCString(_response, " />");
123   }
124   else {
125     WOResponse_AddCString(_response, ">");
126   }
127 }
128
129 /* description */
130
131 - (NSString *)associationDescription {
132   NSMutableString *str = nil;
133   str = [[NSMutableString alloc]
134                           initWithString:[super associationDescription]];
135
136   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
137   if (self->checked)   [str appendFormat:@" checked=%@", self->checked];
138
139   return [str autorelease];
140 }
141
142 @end /* WOCheckBox */