]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WORadioButtonList.m
ffacf53982a43b9ff271d1bdfdbe873af8c73b74
[sope] / sope-appserver / NGObjWeb / DynamicElements / WORadioButtonList.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
24 @interface WORadioButtonList : WOInput
25 {
26   // WODynamicElement: extraAttributes
27   // WODynamicElement: otherTagString
28   // WOInput:    name
29   // WOInput:    value
30   // WOInput:    disabled
31 @protected
32   WOAssociation *list;
33   WOAssociation *item;
34   WOAssociation *index;
35   WOAssociation *selection;
36   WOAssociation *prefix;
37   WOAssociation *suffix;
38 }
39
40 @end /* WORadioButtonList */
41
42 #include "decommon.h"
43
44 // TODO: add support for template? (does WO provide this?)
45
46 @implementation WORadioButtonList
47
48 - (id)initWithName:(NSString *)_name
49   associations:(NSDictionary *)_config
50   template:(WOElement *)_c 
51 {
52   if ((self = [super initWithName:_name associations:_config template:_c])) {
53     self->list      = OWGetProperty(_config, @"list");
54     self->item      = OWGetProperty(_config, @"item");
55     self->index     = OWGetProperty(_config, @"index");
56     self->selection = OWGetProperty(_config, @"selection");
57     self->prefix    = OWGetProperty(_config, @"prefix");
58     self->suffix    = OWGetProperty(_config, @"suffix");
59   }
60   return self;
61 }
62
63 - (void)dealloc {
64   [self->list      release];
65   [self->item      release];
66   [self->index     release];
67   [self->selection release];
68   [self->prefix    release];
69   [self->suffix    release];
70   [super dealloc];
71 }
72
73 /* processing requests */
74
75 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
76   WOComponent *sComponent = [_ctx component];
77   unsigned idx;
78   NSArray  *array;
79   id       formValue;
80   
81   if ([self->disabled boolValueInComponent:sComponent])
82     return;
83   
84   formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)];
85   if (formValue == nil)
86     return;
87   
88   idx   = [formValue unsignedIntValue];
89   array = [self->list valueInComponent:sComponent];
90
91   if ([self->index isValueSettable])
92     [self->index setUnsignedIntValue:idx inComponent:sComponent];
93
94   if ([self->item isValueSettable])
95     [self->item setValue:[array objectAtIndex:idx] inComponent:sComponent];
96       
97   if ([self->selection isValueSettable]) {
98     [self->selection setValue:[array objectAtIndex:idx]
99                      inComponent:sComponent];
100   }
101 }
102
103 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
104   return nil;
105 }
106
107 /* generating response */
108
109 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
110   WOComponent *sComponent;
111   NSArray     *array;
112   unsigned    goCount;
113   unsigned    cnt;
114   NSString    *n;
115   id          sel;
116   BOOL        canSetIndex, canSetItem;
117   
118   sComponent = [_ctx component];
119   array      = [self->list valueInComponent:sComponent];
120   goCount    = [array count];
121
122   if (goCount <= 0)
123     return;
124
125   n   = OWFormElementName(self, _ctx);
126   sel = [self->selection valueInComponent:sComponent];
127
128   canSetIndex = [self->index isValueSettable];
129   canSetItem  = [self->item  isValueSettable];
130   
131   for (cnt = 0; cnt < goCount; cnt++) {
132     id object;
133     
134     object = [array objectAtIndex:cnt];
135
136     if (canSetIndex)
137       [self->index setUnsignedIntValue:cnt inComponent:sComponent];
138     
139     if (canSetItem)
140       [self->item setValue:object inComponent:sComponent];
141
142     if (self->prefix != nil) {
143       WOResponse_AddString(_response,
144                            [self->prefix stringValueInComponent:sComponent]);
145     }
146
147     /* add radio button */
148     {
149       WOResponse_AddCString(_response, "<input type=\"radio\" name=\"");
150       [_response appendContentHTMLAttributeValue:n];
151       WOResponse_AddCString(_response, "\" value=\"");
152       WOResponse_AddInt(_response, cnt);
153       WOResponse_AddCString(_response, "\"");
154       
155       if (sel == object || [sel isEqual:object]) {
156         WOResponse_AddCString(_response, 
157                               (_ctx->wcFlags.allowEmptyAttributes 
158                                ? " checked" : " checked=\"checked\""));
159       }
160       
161       if ([self->disabled boolValueInComponent:sComponent]) {
162         WOResponse_AddCString(_response, 
163                               (_ctx->wcFlags.allowEmptyAttributes
164                                ? " disabled" : " disabled=\"disabled\""));
165       }
166       
167       [self appendExtraAttributesToResponse:_response inContext:_ctx];
168       if (self->otherTagString != nil) {
169         NSString *s;
170         
171         s = [self->otherTagString stringValueInComponent:sComponent];
172         WOResponse_AddChar(_response, ' ');
173         WOResponse_AddString(_response, s);
174       }
175       WOResponse_AddEmptyCloseParens(_response, _ctx);
176   
177       // the value in a radio list is the string besides the button
178       if (self->value != nil) {
179         NSString *s;
180         
181         s = [self->value stringValueInComponent:sComponent];
182         WOResponse_AddHtmlString(_response, s);
183       }
184     }
185         
186     if (self->suffix != nil) {
187       WOResponse_AddString(_response,
188                            [self->suffix stringValueInComponent:sComponent]);
189     }
190   }
191 }
192
193 /* description */
194
195 - (NSString *)associationDescription {
196   NSMutableString *str;
197
198   str = [NSMutableString stringWithCapacity:128];
199   [str appendString:[super associationDescription]];
200   if (self->list)      [str appendFormat:@" list=%@",      self->list];
201   if (self->item)      [str appendFormat:@" item=%@",      self->item];
202   if (self->index)     [str appendFormat:@" index=%@",     self->index];
203   if (self->prefix)    [str appendFormat:@" prefix=%@",    self->prefix];
204   if (self->suffix)    [str appendFormat:@" suffix=%@",    self->suffix];
205   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
206
207   return str;
208 }
209
210 @end /* WORadioButtonList */