| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/cocoa/filedlg.mm |
| 3 | // Purpose: wxFileDialog for wxCocoa |
| 4 | // Author: Ryan Norton |
| 5 | // Modified by: |
| 6 | // Created: 2004-10-02 |
| 7 | // RCS-ID: $Id: filedlg.mm 40007 2006-07-05 13:10:46Z SC $ |
| 8 | // Copyright: (c) Ryan Norton |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #if wxUSE_FILEDLG |
| 24 | |
| 25 | #include "wx/filedlg.h" |
| 26 | |
| 27 | #ifndef WX_PRECOMP |
| 28 | #include "wx/msgdlg.h" |
| 29 | #include "wx/app.h" |
| 30 | #endif |
| 31 | |
| 32 | #include "wx/filename.h" |
| 33 | |
| 34 | #include "wx/osx/private.h" |
| 35 | |
| 36 | // ============================================================================ |
| 37 | // implementation |
| 38 | // ============================================================================ |
| 39 | |
| 40 | // Open Items: |
| 41 | // - support for old style MacOS creator / type combos |
| 42 | // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories) |
| 43 | |
| 44 | IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) |
| 45 | |
| 46 | wxFileDialog::wxFileDialog( |
| 47 | wxWindow *parent, const wxString& message, |
| 48 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, |
| 49 | long style, const wxPoint& pos, const wxSize& sz, const wxString& name) |
| 50 | : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | |
| 55 | NSArray* GetTypesFromFilter( const wxString filter ) |
| 56 | { |
| 57 | NSMutableArray* types = nil; |
| 58 | if ( !filter.empty() ) |
| 59 | { |
| 60 | wxArrayString names ; |
| 61 | wxArrayString extensions; |
| 62 | |
| 63 | wxString filter2(filter) ; |
| 64 | int filterIndex = 0; |
| 65 | bool isName = true ; |
| 66 | wxString current ; |
| 67 | |
| 68 | for ( unsigned int i = 0; i < filter2.length() ; i++ ) |
| 69 | { |
| 70 | if ( filter2.GetChar(i) == wxT('|') ) |
| 71 | { |
| 72 | if ( isName ) |
| 73 | { |
| 74 | names.Add( current ) ; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | extensions.Add( current ) ; |
| 79 | ++filterIndex ; |
| 80 | } |
| 81 | |
| 82 | isName = !isName ; |
| 83 | current = wxEmptyString ; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | current += filter2.GetChar(i) ; |
| 88 | } |
| 89 | } |
| 90 | // we allow for compatibility reason to have a single filter expression (like *.*) without |
| 91 | // an explanatory text, in that case the first part is name and extension at the same time |
| 92 | |
| 93 | wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ; |
| 94 | if ( current.empty() ) |
| 95 | extensions.Add( names[filterIndex] ) ; |
| 96 | else |
| 97 | extensions.Add( current ) ; |
| 98 | if ( filterIndex == 0 || isName ) |
| 99 | names.Add( current ) ; |
| 100 | |
| 101 | ++filterIndex ; |
| 102 | |
| 103 | const size_t extCount = extensions.GetCount(); |
| 104 | for ( size_t i = 0 ; i < extCount; i++ ) |
| 105 | { |
| 106 | wxString extension = extensions[i]; |
| 107 | |
| 108 | // Remove leading '*' |
| 109 | if (extension.length() && (extension.GetChar(0) == '*')) |
| 110 | extension = extension.Mid( 1 ); |
| 111 | |
| 112 | // Remove leading '.' |
| 113 | if (extension.length() && (extension.GetChar(0) == '.')) |
| 114 | extension = extension.Mid( 1 ); |
| 115 | |
| 116 | if ( extension.IsEmpty() ) |
| 117 | { |
| 118 | if ( types != nil ) |
| 119 | [types release]; |
| 120 | return nil; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | if ( types == nil ) |
| 125 | types = [[NSMutableArray alloc] init]; |
| 126 | |
| 127 | wxCFStringRef cfext(extension); |
| 128 | [types addObject: (NSString*)cfext.AsNSString() ]; |
| 129 | #if 0 |
| 130 | // add support for classic fileType / creator here |
| 131 | wxUint32 fileType, creator; |
| 132 | // extension -> mactypes |
| 133 | #endif |
| 134 | } |
| 135 | } |
| 136 | return types; |
| 137 | } |
| 138 | |
| 139 | int wxFileDialog::ShowModal() |
| 140 | { |
| 141 | int result = wxID_CANCEL; |
| 142 | |
| 143 | NSSavePanel *panel = nil; |
| 144 | |
| 145 | wxCFStringRef cf( m_message ); |
| 146 | |
| 147 | wxCFStringRef dir( m_dir ); |
| 148 | wxCFStringRef file( m_fileName ); |
| 149 | |
| 150 | m_path = wxEmptyString; |
| 151 | m_fileNames.Clear(); |
| 152 | |
| 153 | if (HasFlag(wxFD_SAVE)) |
| 154 | { |
| 155 | NSSavePanel* sPanel = [NSSavePanel savePanel]; |
| 156 | // makes things more convenient: |
| 157 | [sPanel setCanCreateDirectories:YES]; |
| 158 | [sPanel setMessage:cf.AsNSString()]; |
| 159 | // if we should be able to descend into pacakges we must somehow |
| 160 | // be able to pass this in |
| 161 | [sPanel setTreatsFilePackagesAsDirectories:NO]; |
| 162 | [sPanel setCanSelectHiddenExtension:YES]; |
| 163 | |
| 164 | if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | if ( [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ] == NSOKButton ) |
| 169 | { |
| 170 | panel = sPanel; |
| 171 | result = wxID_OK; |
| 172 | |
| 173 | wxCFStringRef filename( [[sPanel filename] retain] ); |
| 174 | |
| 175 | m_path = filename.AsString(); |
| 176 | m_fileName = wxFileNameFromPath(m_path); |
| 177 | m_dir = wxPathOnly( m_path ); |
| 178 | } |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | NSArray* types = GetTypesFromFilter( m_wildCard ) ; |
| 183 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; |
| 184 | [oPanel setTreatsFilePackagesAsDirectories:NO]; |
| 185 | [oPanel setCanChooseDirectories:NO]; |
| 186 | [oPanel setResolvesAliases:YES]; |
| 187 | [oPanel setCanChooseFiles:YES]; |
| 188 | [oPanel setMessage:cf.AsNSString()]; |
| 189 | |
| 190 | if ( [oPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() types:types] == NSOKButton ) |
| 191 | { |
| 192 | panel = oPanel; |
| 193 | result = wxID_OK; |
| 194 | NSArray* filenames = [oPanel filenames]; |
| 195 | for ( size_t i = 0 ; i < [filenames count] ; ++ i ) |
| 196 | { |
| 197 | wxCFStringRef filename( [(NSString*) [filenames objectAtIndex:i] retain] ); |
| 198 | wxString fnstr = filename.AsString(); |
| 199 | m_paths.Add( fnstr ); |
| 200 | m_fileNames.Add( wxFileNameFromPath(fnstr) ); |
| 201 | if ( i == 0 ) |
| 202 | { |
| 203 | m_path = fnstr; |
| 204 | m_fileName = wxFileNameFromPath(fnstr); |
| 205 | m_dir = wxPathOnly( fnstr ); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | if ( types != nil ) |
| 210 | [types release]; |
| 211 | } |
| 212 | |
| 213 | return result; |
| 214 | } |
| 215 | |
| 216 | #endif // wxUSE_FILEDLG |