source: tuxpaint-config/trunk/fuentes/src/macosx_dialog.m @ 220

Last change on this file since 220 was 220, checked in by mabarracus, 4 years ago

added tuxpaint-config sources from trusty

File size: 3.5 KB
Line 
1//
2//  macosx_dialog.m
3//  TuxPaint-Config
4//
5//  Created by Martin Fuhrer on 12/06/07.
6//  Copyright 2007 __MyCompanyName__. All rights reserved.
7//
8//  This program is free software; you can redistribute it and/or modify
9//  it under the terms of the GNU General Public License as published by
10//  the Free Software Foundation; either version 2 of the License, or
11//  (at your option) any later version.
12//
13//  This program is distributed in the hope that it will be useful,
14//  but WITHOUT ANY WARRANTY; without even the implied warranty of
15//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//  GNU General Public License for more details.
17//
18//  You should have received a copy of the GNU General Public License
19//  along with this program; if not, write to the Free Software
20//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21//  (See COPYING.txt)
22//
23
24#import "macosx_dialog.h"
25#import <Cocoa/Cocoa.h>
26#import <AppKit/NSOpenPanel.h>
27
28// this object waits for the open dialog to go away
29@interface ModalDelegate : NSObject
30{
31    BOOL isDismissed;
32    BOOL isCancelled;
33    NSString *path;
34}
35- (id)   init;
36- (void) dealloc;
37- (BOOL) wait;
38- (NSString*) path;
39- (void) reset;
40@end
41
42@implementation ModalDelegate
43
44- (id) init
45{
46    self = [ super init ];
47    [ self reset ];
48    return self;
49}
50
51- (void) dealloc
52{
53    [path release];   
54    [super dealloc];
55}
56
57- (BOOL) wait
58{
59    while (!isDismissed) {
60       
61        NSEvent *event;
62       
63        event = [ NSApp nextEventMatchingMask:NSAnyEventMask
64                                    untilDate:[ NSDate distantFuture ]
65                                       inMode: NSDefaultRunLoopMode dequeue:YES ];
66       
67        [ NSApp sendEvent:event ];
68    }
69   
70    return isCancelled;
71}
72
73- (void) reset
74{
75    isDismissed = NO;
76    isCancelled = NO;
77    path = @"";
78}
79
80- (NSString*) path
81{
82    return path;   
83}
84
85- (void)openPanelDidEnd:(NSOpenPanel *)panel
86            returnCode:(int)returnCode contextInfo:(void *)contextInfo
87{
88    isDismissed = YES;
89    if( returnCode == NSOKButton )
90    {
91        isCancelled = NO;
92        [ path release ];
93        path = [ [ panel filename ] retain ];
94    }
95    else
96    {
97        isCancelled = YES;
98    }
99}
100
101@end
102
103
104const char *choosePath(int files, int directories, const char* extension)
105{
106    NSApplicationLoad();  // initialize Cocoa
107   
108    NSString* dirPath = @"";
109    ModalDelegate* delegate = [ [ ModalDelegate alloc ] init ];
110   
111    NSMutableArray* fileTypes = [ [ NSMutableArray alloc ] init ];
112    if( extension != 0 )
113    {
114        [ fileTypes addObject:[ NSString stringWithCString:extension ] ];
115    }
116   
117    NSOpenPanel* openPanel = [ NSOpenPanel openPanel ];
118   
119    [ openPanel setCanChooseFiles:(files != 0) ];
120    [ openPanel setCanChooseDirectories:(directories != 0 ) ];
121   
122    // Display dialog as sheet (doesn't seem to work currently)
123    /*
124    [ openPanel beginSheetForDirectory:nil
125           file:nil
126           types:fileTypes
127           modalForWindow:[ NSApp mainWindow ]
128           modalDelegate:delegate
129           didEndSelector:@selector(openPanelDidEnd: returnCode: contextInfo:)
130           contextInfo:nil ];
131     
132    [ delegate wait ];
133    dirPath = [ delegate path ];
134    */
135
136    // Display dialog modally
137    int result = [ openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
138    if( result == NSOKButton )
139        dirPath = [ openPanel filename ];
140   
141    [ delegate release ];
142    [ fileTypes release ];
143   
144    const char* path = [ dirPath cString ];
145    return path;
146}
Note: See TracBrowser for help on using the repository browser.