]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WORadioButtonList.m
minor improvement to WOHttpAdaptor, bumped framework revisions
[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   formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)];
82   if (formValue == nil)
83     return;
84   
85   idx   = [formValue unsignedIntValue];
86   array = [self->list valueInComponent:sComponent];
87   
88   /* setup item/index */
89   
90   if ([self->index isValueSettable])
91     [self->index setUnsignedIntValue:idx inComponent:sComponent];
92
93   if ([self->item isValueSettable])
94     [self->item setValue:[array objectAtIndex:idx] inComponent:sComponent];
95   
96   /* now check whether the item is disabled/allowed as the selection */
97
98   if ([self->disabled boolValueInComponent:sComponent])
99     return;
100   
101   /* set selection if possible */
102   
103   if ([self->selection isValueSettable]) {
104     [self->selection setValue:[array objectAtIndex:idx]
105                      inComponent:sComponent];
106   }
107 }
108
109 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
110   return nil;
111 }
112
113 /* generating response */
114
115 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
116   WOComponent *sComponent;
117   NSArray     *array;
118   unsigned    goCount;
119   unsigned    cnt;
120   NSString    *n;
121   id          sel;
122   BOOL        canSetIndex, canSetItem;
123   
124   sComponent = [_ctx component];
125   array      = [self->list valueInComponent:sComponent];
126   goCount    = [array count];
127
128   if (goCount <= 0)
129     return;
130
131   n   = OWFormElementName(self, _ctx);
132   sel = [self->selection valueInComponent:sComponent];
133
134   canSetIndex = [self->index isValueSettable];
135   canSetItem  = [self->item  isValueSettable];
136   
137   for (cnt = 0; cnt < goCount; cnt++) {
138     id object;
139     
140     object = [array objectAtIndex:cnt];
141
142     if (canSetIndex)
143       [self->index setUnsignedIntValue:cnt inComponent:sComponent];
144     
145     if (canSetItem)
146       [self->item setValue:object inComponent:sComponent];
147
148     if (self->prefix != nil) {
149       WOResponse_AddString(_response,
150                            [self->prefix stringValueInComponent:sComponent]);
151     }
152
153     /* add radio button */
154     {
155       WOResponse_AddCString(_response, "<input type=\"radio\" name=\"");
156       [_response appendContentHTMLAttributeValue:n];
157       WOResponse_AddCString(_response, "\" value=\"");
158       WOResponse_AddInt(_response, cnt);
159       WOResponse_AddCString(_response, "\"");
160       
161       if (sel == object || [sel isEqual:object]) {
162         WOResponse_AddCString(_response, 
163                               (_ctx->wcFlags.allowEmptyAttributes 
164                                ? " checked" : " checked=\"checked\""));
165       }
166       
167       if ([self->disabled boolValueInComponent:sComponent]) {
168         WOResponse_AddCString(_response, 
169                               (_ctx->wcFlags.allowEmptyAttributes
170                                ? " disabled" : " disabled=\"disabled\""));
171       }
172       
173       [self appendExtraAttributesToResponse:_response inContext:_ctx];
174       if (self->otherTagString != nil) {
175         NSString *s;
176         
177         s = [self->otherTagString stringValueInComponent:sComponent];
178         WOResponse_AddChar(_response, ' ');
179         WOResponse_AddString(_response, s);
180       }
181       WOResponse_AddEmptyCloseParens(_response, _ctx);
182   
183       // the value in a radio list is the string besides the button
184       if (self->value != nil) {
185         NSString *s;
186         
187         s = [self->value stringValueInComponent:sComponent];
188         WOResponse_AddHtmlString(_response, s);
189       }
190     }
191         
192     if (self->suffix != nil) {
193       WOResponse_AddString(_response,
194                            [self->suffix stringValueInComponent:sComponent]);
195     }
196   }
197 }
198
199 /* description */
200
201 - (NSString *)associationDescription {
202   NSMutableString *str;
203
204   str = [NSMutableString stringWithCapacity:128];
205   [str appendString:[super associationDescription]];
206   if (self->list)      [str appendFormat:@" list=%@",      self->list];
207   if (self->item)      [str appendFormat:@" item=%@",      self->item];
208   if (self->index)     [str appendFormat:@" index=%@",     self->index];
209   if (self->prefix)    [str appendFormat:@" prefix=%@",    self->prefix];
210   if (self->suffix)    [str appendFormat:@" suffix=%@",    self->suffix];
211   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
212
213   return str;
214 }
215
216 @end /* WORadioButtonList */