1/* -*- mode:objc; coding:utf-8; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- */
2/*
3  Copyright (c) 2003-2005 MacUIM contributors, All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8
9  1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11  2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14  3. Neither the name of authors nor the names of its contributors
15     may be used to endorse or promote products derived from this software
16     without specific prior written permission.
17
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
22  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  SUCH DAMAGE.
29*/
30
31#import "Debug.h"
32#import "UimCustomChoice.h"
33#import "UimCustomChoiceController.h"
34
35
36@implementation UimCustomChoice
37
38- (id)initWithCustom:(struct uim_custom *)aCustom
39{
40  if (!(self = [super initWithCustom:aCustom]))
41    return nil;
42
43  if (!(controller = [[UimCustomChoiceController alloc] init])) {
44    [self release];
45    return nil;
46  }
47  [controller retain];
48
49  [self displayCustom];
50  [self setAction];
51
52  return self;
53}
54
55- (void)displayCustom
56{
57  int i;
58
59  [[(UimCustomChoiceController *) controller button] removeAllItems];
60  for (i = 0;
61       custom->range->as_choice.valid_items &&
62       custom->range->as_choice.valid_items[i];
63       i++) {
64    [[(UimCustomChoiceController *) controller button] addItemWithTitle:[NSString stringWithUTF8String:custom->range->as_choice.valid_items[i]->label]];
65    if (!strcmp(custom->range->as_choice.valid_items[i]->symbol,
66                custom->value->as_choice->symbol))
67      [[(UimCustomChoiceController *) controller button] selectItemAtIndex:i];
68  }
69  [[(UimCustomChoiceController *) controller button] setEnabled:custom->is_active];
70  [[(UimCustomChoiceController *) controller button] sizeToFit];
71  [[(UimCustomChoiceController *) controller view] setNeedsDisplay:YES];
72}
73
74- (void)setAction
75{
76  [[(UimCustomChoiceController *) controller button] setTarget:self];
77  [[(UimCustomChoiceController *) controller button] setAction:@selector(click:)];
78}
79
80- (void)setDefault
81{
82  free(custom->value->as_choice->symbol);
83  free(custom->value->as_choice->label);
84  free(custom->value->as_choice->desc);
85  custom->value->as_choice->symbol =
86    strdup(custom->default_value->as_choice->symbol);
87  custom->value->as_choice->label =
88    strdup(custom->default_value->as_choice->label);
89  custom->value->as_choice->desc =
90    strdup(custom->default_value->as_choice->desc);
91  uim_custom_set(custom);
92}
93
94#pragma mark -
95
96//
97// Actions
98//
99
100- (IBAction)click:(id)sender
101{
102  int index = [[(UimCustomChoiceController *) controller button] indexOfSelectedItem];
103
104  free(custom->value->as_choice->symbol);
105  free(custom->value->as_choice->label);
106  free(custom->value->as_choice->desc);
107  custom->value->as_choice->symbol =
108    strdup(custom->range->as_choice.valid_items[index]->symbol);
109  custom->value->as_choice->label =
110    strdup(custom->range->as_choice.valid_items[index]->label);
111  custom->value->as_choice->desc =
112    strdup(custom->range->as_choice.valid_items[index]->desc);
113  uim_custom_set(custom);
114
115  if ([self isValidDelegateForSelector:@selector(uimCustomModified:)])
116    [[self delegate] performSelector:@selector(uimCustomModified:)
117                          withObject:self];
118}
119
120@end
121