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 "UimCustomPathname.h"
33#import "UimCustomPathnameController.h"
34
35
36@implementation UimCustomPathname
37
38- (id)initWithCustom:(struct uim_custom *)aCustom
39{
40  if (!(self = [super initWithCustom:aCustom]))
41    return nil;
42
43  if (!(controller = [[UimCustomPathnameController 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  [[(UimCustomPathnameController *) controller field] setTitleWithMnemonic:[NSString stringWithUTF8String:custom->value->as_pathname->str]];
58  [[(UimCustomPathnameController *) controller field] setEnabled:custom->is_active];
59}
60
61- (void)setAction
62{
63  [[(UimCustomPathnameController *) controller button] setTarget:self];
64  [[(UimCustomPathnameController *) controller button] setAction:@selector(click:)];
65}
66
67- (void)setDefault
68{
69  free(custom->value->as_pathname->str);
70  custom->value->as_pathname->str =
71    strdup(custom->default_value->as_pathname->str);
72  uim_custom_set(custom);
73}
74
75#pragma mark -
76
77//
78// Actions
79//
80
81- (IBAction)click:(id)sender
82{
83  NSOpenPanel *openPanel = [NSOpenPanel openPanel];
84
85  [openPanel setCanChooseDirectories:YES];
86  [openPanel setCanChooseFiles:YES];
87
88  [openPanel beginSheetForDirectory:nil file:nil types:nil
89                     modalForWindow:[[NSApplication sharedApplication] keyWindow]
90                      modalDelegate:self
91                     didEndSelector:@selector(pathnameSelectDidEnd:returnCode:contextInfo:)
92                        contextInfo:nil];
93}
94
95- (void)pathnameSelectDidEnd:(NSOpenPanel *)sheet
96                  returnCode:(int)returnCode
97                 contextInfo:(void *)contextInfo
98{
99  if (!returnCode)
100    return;
101
102  free(custom->value->as_pathname->str);
103  custom->value->as_pathname->str = (char *) strdup([[sheet filename] UTF8String]);
104  uim_custom_set(custom);
105
106  if ([self isValidDelegateForSelector:@selector(uimCustomModified:)])
107    [[self delegate] performSelector:@selector(uimCustomModified:)
108                          withObject:self];
109}
110
111@end
112