Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/ui/ui-drv/cocoa/CustomDialog.m
blob: 5cac7df794abdb9e340f6892dee037997aa78dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *     XaoS, a fast portable realtime fractal zoomer 
 *                  Copyright (C) 1996 by
 *
 *      Jan Hubicka          (hubicka@paru.cas.cz)
 *      Thomas Marsh         (tmarsh@austin.ibm.com)
 *
 *    Cocoa Driver by J.B. Langston III (jb-langston@austin.rr.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#import "CustomDialog.h"

#ifdef HAVE_GETTEXT
#include <libintl.h>
#include <locale.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

#define MARGIN 20
#define SPACING 8

@implementation CustomDialog

#pragma mark Initialization

- (id)initWithContext:(struct uih_context *)myContext 
             menuItem:(CONST menuitem *)myItem 
               dialog:(CONST menudialog *)myDialog {
    
    NSRect windowRect		= NSMakeRect(200, 200, 300, 107);
    NSRect helpButtonRect	= NSMakeRect(20, 20, 25, 25);
    NSRect cancelButtonRect	= NSMakeRect(128, 20, 75, 25);
    NSRect okButtonRect		= NSMakeRect(210, 20, 75, 25);
    NSRect labelRect		= NSMakeRect(20, 67, 62, 17);
    NSRect controlRect		= NSMakeRect(90, 65, 190, 22);
    NSRect coordRect		= NSMakeRect(90, 65, 85, 22);
    
    self = [super initWithContentRect:windowRect
                            styleMask:NSTitledWindowMask
                              backing:NSBackingStoreBuffered
                                defer:YES];
    
    if (self) {
        context = myContext;
        item = myItem;
        dialog = myDialog;	
        
        [self setTitle:[NSString stringWithUTF8String:item->name]];
        
        controls = [[NSMutableDictionary alloc] initWithCapacity:10];
        
        NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:10];
        
        int maxLabelWidth = 0, maxControlWidth = 0, nitems = 0, i = 0;
        
        for (nitems = 0; dialog[nitems].question; nitems++);
        
        for (i = 0; i < nitems; i++) {
            NSTextField *label = [[NSTextField alloc] initWithFrame:labelRect];
            NSString *question = [NSString stringWithUTF8String:dialog[i].question];
            [label setEditable:NO];
            [label setBezeled:NO];
            [label setDrawsBackground:NO];
            [label setStringValue:question];
            
            [label sizeToFit];
            if ([label frame].size.width > maxLabelWidth)
                maxLabelWidth = [label frame].size.width;
            
            [[self contentView] addSubview:label];
            [labels addObject:label];
            [label release];
            
            switch (dialog[i].type) {
              case DIALOG_INT:
              case DIALOG_FLOAT:
              case DIALOG_STRING:
              case DIALOG_KEYSTRING:
              {
                  NSTextField *textField = [[NSTextField alloc] initWithFrame:controlRect];
                  [textField setEditable:YES];
                  [textField setBezeled:YES];
                  [textField setBezelStyle:NSTextFieldSquareBezel];
                  [[textField cell] setScrollable:YES];
                  switch (dialog[i].type) {
                    case DIALOG_INT:
                        [textField setIntValue:dialog[i].defint];
                        break;
                    case DIALOG_FLOAT:
                        [textField setDoubleValue:dialog[i].deffloat];
                        break;
                    case DIALOG_STRING:
                        [textField setStringValue:[NSString stringWithUTF8String:dialog[i].defstr]];
                        break;
                    case DIALOG_KEYSTRING:
                        [textField setStringValue:[NSString stringWithUTF8String:dialog[i].defstr]];
                        break;
                  }
                  
                  if ([textField frame].size.width > maxControlWidth)
                      maxControlWidth = [textField frame].size.width;
                  
                  [[self contentView] addSubview:textField];
                  [controls setValue:textField forKey:question];					
                  [textField release];
                  
                  break;
              }
              case DIALOG_IFILE:
              case DIALOG_OFILE:
              {
                  NSTextField *textField = [[NSTextField alloc] initWithFrame:controlRect];
                  [textField setEditable:NO];
                  [textField setBezeled:YES];
                  [textField setBezelStyle:NSTextFieldSquareBezel];
                  [[textField cell] setScrollable:YES];
                  [textField setStringValue:[NSString stringWithUTF8String:dialog[i].defstr]];
                  [[textField cell] setRepresentedObject:[NSString stringWithUTF8String:dialog[i].defstr]];
                  
                  if ([textField frame].size.width > maxControlWidth)
                      maxControlWidth = [textField frame].size.width;
                  
                  [[self contentView] addSubview:textField];
                  [controls setValue:textField forKey:question];					
                  
                  NSButton *chooseButton = [[NSButton alloc] initWithFrame:okButtonRect];
                  [chooseButton setTitle:[NSString stringWithUTF8String:_("Choose")]];
                  [chooseButton setButtonType:NSMomentaryPushInButton];
                  [chooseButton setBezelStyle:NSRoundedBezelStyle];
                  [chooseButton setTarget:self];
                  if (dialog[i].type == DIALOG_IFILE)
                      [chooseButton setAction:@selector(chooseInput:)];
                  else
                      [chooseButton setAction:@selector(chooseOutput:)];
                  [[chooseButton cell] setRepresentedObject:textField];
                  
                  [[self contentView] addSubview:chooseButton];
                  [controls setValue:chooseButton forKey:[question stringByAppendingString:@"choose"]];
                  
                  if ([textField frame].size.width + SPACING + [chooseButton frame].size.width > maxControlWidth)
                      maxControlWidth = [textField frame].size.width + SPACING + [chooseButton frame].size.width;
                  
                  [chooseButton release];
                  [textField release];
                  break;
              }
              case DIALOG_CHOICE:
              {
                  
                  NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:controlRect];
                  
                  NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
                  CONST char **str = (CONST char **) dialog[i].defstr;
                  int y;
                  for (y = 0; str[y] != NULL; y++)
                    {
                        NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithUTF8String:str[y]] action:nil keyEquivalent:@""];
                        [menu addItem:menuItem];
                        [menuItem release];
                    }
                  [popupButton setMenu:menu];
                  [menu release];
                  [popupButton selectItemAtIndex:dialog[i].defint];
                  
                  [popupButton sizeToFit];
                  if ([popupButton frame].size.width > maxControlWidth)
                      maxControlWidth = [popupButton frame].size.width;
                  
                  [[self contentView] addSubview:popupButton];
                  [controls setValue:popupButton forKey:question];					
                  [popupButton release];
                  
                  break;
              }
              case DIALOG_ONOFF:
              {
                  break;
              }
              case DIALOG_COORD:
              {
                  int coordWidth = 0;
                  NSTextField *textField = [[NSTextField alloc] initWithFrame:coordRect];
                  [textField setEditable:YES];
                  [textField setBezeled:YES];
                  [textField setBezelStyle:NSTextFieldSquareBezel];
                  [[textField cell] setScrollable:YES];
                  [textField setDoubleValue:dialog[i].deffloat];
                  
                  coordWidth += [textField frame].size.width;
                  
                  [[self contentView] addSubview:textField];
                  [controls setValue:textField forKey:question];
                  [textField release];
                  
                  textField = [[NSTextField alloc] initWithFrame:labelRect];
                  [textField setEditable:NO];
                  [textField setBezeled:NO];
                  [textField setDrawsBackground:NO];
                  [textField setStringValue:@"+"];
                  
                  [textField sizeToFit];
                  coordWidth += [textField frame].size.width;
                  
                  [[self contentView] addSubview:textField];
                  [controls setValue:textField forKey:[question stringByAppendingString:@"+"]];
                  [textField release];
                  
                  textField = [[NSTextField alloc] initWithFrame:coordRect];
                  [textField setEditable:YES];
                  [textField setBezeled:YES];
                  [textField setBezelStyle:NSTextFieldSquareBezel];
                  [[textField cell] setScrollable:YES];
                  [textField setDoubleValue:dialog[i].deffloat2];
                  
                  coordWidth += [textField frame].size.width;
                  
                  [[self contentView] addSubview:textField];
                  [controls setValue:textField forKey:[question stringByAppendingString:@"2"]];
                  [textField release];
                  
                  textField = [[NSTextField alloc] initWithFrame:labelRect];
                  [textField setEditable:NO];
                  [textField setBezeled:NO];
                  [textField setDrawsBackground:NO];
                  [textField setStringValue:@"i"];
                  
                  [textField sizeToFit];
                  coordWidth += [textField frame].size.width;
                  
                  [[self contentView] addSubview:textField];
                  [controls setValue:textField forKey:[question stringByAppendingString:@"i"]];
                  [textField release];
                  
                  if (coordWidth > maxControlWidth)
                      maxControlWidth = coordWidth;
              }
            }
            if (MARGIN + maxLabelWidth + SPACING + maxControlWidth + MARGIN > windowRect.size.width)
                windowRect.size.width = MARGIN + maxLabelWidth + SPACING + maxControlWidth + MARGIN;
        }
        
        controlRect.origin.x = labelRect.origin.x + maxLabelWidth + SPACING;
        
        for (i = nitems-1; i >= 0; i--) {
            NSTextField *label = [labels objectAtIndex:i];
            NSControl *control = [controls objectForKey:[label stringValue]];
            [label setFrameOrigin:labelRect.origin];
            
            [control setFrameOrigin:controlRect.origin];
            
            switch (dialog[i].type) {
              case DIALOG_IFILE:
              case DIALOG_OFILE:
              {
                  NSButton *chooseButton = [controls objectForKey:[[label stringValue] stringByAppendingString:@"choose"]];
                  [chooseButton setFrameOrigin:NSMakePoint([control frame].origin.x + [control frame].size.width + SPACING, [control frame].origin.y - 2)];
                  break;
              }
              case DIALOG_COORD:
              {
                  NSRect controlRect2 = controlRect;
                  controlRect2.origin.x += [control frame].size.width;
                  control = [controls objectForKey:[[label stringValue] stringByAppendingString:@"+"]];
                  [control setFrameOrigin:controlRect2.origin];
                  controlRect2.origin.x += [control frame].size.width;
                  control = [controls objectForKey:[[label stringValue] stringByAppendingString:@"2"]];
                  [control setFrameOrigin:controlRect2.origin];
                  controlRect2.origin.x += [control frame].size.width;
                  control = [controls objectForKey:[[label stringValue] stringByAppendingString:@"i"]];
                  [control setFrameOrigin:controlRect2.origin];
                  control = [controls objectForKey:[label stringValue]];
              }
            }
            
            labelRect.origin.y += SPACING + [control frame].size.height;
            controlRect.origin.y += SPACING + [control frame].size.height;
        }
        
        controlRect.origin.y += MARGIN;
        
        windowRect.size.height = controlRect.origin.y;
        
        okButtonRect.origin.x += windowRect.size.width - [self frame].size.width;
        cancelButtonRect.origin.x += windowRect.size.width - [self frame].size.width;
        
        NSButton *okButton = [[NSButton alloc] initWithFrame:okButtonRect];
        [okButton setTitle:[NSString stringWithUTF8String:_("OK")]];
        [okButton setButtonType:NSMomentaryPushInButton];
        [okButton setBezelStyle:NSRoundedBezelStyle];
        [okButton setKeyEquivalent:@"\r"];
        [okButton setTarget:self];
        [okButton setAction:@selector(execute:)];
        [okButton sizeToFit];
        [[self contentView] addSubview:okButton];
        
        NSButton *cancelButton = [[NSButton alloc] initWithFrame:cancelButtonRect];
        [cancelButton setTitle:[NSString stringWithUTF8String:_("Cancel")]];
        [cancelButton setButtonType:NSMomentaryPushInButton];
        [cancelButton setBezelStyle:NSRoundedBezelStyle];
        [cancelButton setTarget:self];
        [cancelButton setAction:@selector(cancel:)];
        [cancelButton sizeToFit];
        [[self contentView] addSubview:cancelButton];
        
        NSButton *helpButton = [[NSButton alloc] initWithFrame:helpButtonRect];
        [helpButton setTitle:@""];
        [helpButton setButtonType:NSMomentaryPushInButton];
        [helpButton setBezelStyle:NSHelpButtonBezelStyle];
        [helpButton setTarget:self];
        [helpButton setAction:@selector(help:)];
        [[self contentView] addSubview:helpButton];
        [helpButton release];
        
        okButtonRect.size.width = MAX([okButton frame].size.width + 2, [cancelButton frame].size.width) + 2 * SPACING;
        cancelButtonRect.size.width = okButtonRect.size.width;
        
        okButtonRect.origin.x = windowRect.size.width - MARGIN - okButtonRect.size.width;
        cancelButtonRect.origin.x = okButtonRect.origin.x - SPACING - cancelButtonRect.size.width;
        
        [okButton setFrame:okButtonRect];
        [cancelButton setFrame:cancelButtonRect];
        
        [cancelButton release];
        [okButton release];
        
        [self setContentSize:windowRect.size];
    }
    return self;
}

# pragma mark Targets

- (IBAction)execute:(id)sender {
    int nitems;
    for (nitems = 0; dialog[nitems].question; nitems++);
    params = malloc (sizeof (*params) * nitems);
    
    int i;
    for (i = 0; i < nitems; i++) {
        NSString *question = [NSString stringWithUTF8String:dialog[i].question];
        NSControl *control;
        switch (dialog[i].type) {
          case DIALOG_IFILE:
          case DIALOG_OFILE:
          case DIALOG_STRING:
          case DIALOG_KEYSTRING:
              control = [controls objectForKey:question];
              params[i].dstring = strdup ([[control stringValue] UTF8String]);
              break;
          case DIALOG_INT:
              control = [controls objectForKey:question];
              params[i].dint = [control intValue];
              break;
          case DIALOG_FLOAT:
              control = [controls objectForKey:question];
              params[i].number = [control floatValue];
              break;
          case DIALOG_COORD:
              control = [controls objectForKey:question];
              params[i].dcoord[0] = [control floatValue];
              control = [controls objectForKey:[question stringByAppendingString:@"2"]];
              params[i].dcoord[1] = [control floatValue];
              break;
          case DIALOG_CHOICE:
              control = [controls objectForKey:question];
              params[i].dint = [(NSPopUpButtonCell *)control indexOfSelectedItem];
              break;
        }
    }
    [NSApp stopModal];
}

- (IBAction)cancel:(id)sender {
    params = NULL;
    [NSApp stopModal];
}

- (IBAction)help:(id)sender {
    ui_help(item->shortname);
}

- (IBAction)chooseInput:(id)sender {
    NSTextField *target = [[sender cell] representedObject];
    NSString *extension = [[[target cell] representedObject] pathExtension];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
    
    int result = [oPanel runModalForDirectory:nil 
                                         file:nil 
                                        types:[NSArray arrayWithObject:extension]];
    
    if (result == NSOKButton)
        [target setStringValue:[oPanel filename]];
}

- (IBAction)chooseOutput:(id)sender {
    NSTextField *target = [[sender cell] representedObject];
    NSString *extension = [[[target cell] representedObject] pathExtension];
    NSSavePanel *sPanel = [NSSavePanel savePanel];
    [sPanel setRequiredFileType:extension];
    
    int result = [sPanel runModalForDirectory:nil file:[target stringValue]];
    if (result == NSOKButton)
        [target setStringValue:[sPanel filename]];
}

#pragma mark Accessors

- (dialogparam *)params {
    return params;
}

#pragma mark Deallocation

- (void)dealloc {
    [controls release];
    [super dealloc];
}
@end