filedlg.mm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // Copyright: (c) Ryan Norton
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx.h".
  17. #include "wx/wxprec.h"
  18. #if wxUSE_FILEDLG
  19. #include "wx/filedlg.h"
  20. #ifndef WX_PRECOMP
  21. #include "wx/msgdlg.h"
  22. #include "wx/app.h"
  23. #endif
  24. #include "wx/filename.h"
  25. #include "wx/cocoa/autorelease.h"
  26. #include "wx/cocoa/string.h"
  27. #include "wx/modalhook.h"
  28. #import <AppKit/NSOpenPanel.h>
  29. #import <AppKit/NSSavePanel.h>
  30. #import <Foundation/NSArray.h>
  31. // ============================================================================
  32. // implementation
  33. // ============================================================================
  34. IMPLEMENT_CLASS(wxCocoaFileDialog, wxFileDialogBase)
  35. // ----------------------------------------------------------------------------
  36. // wxFileDialog
  37. // ----------------------------------------------------------------------------
  38. wxFileDialog::wxFileDialog(wxWindow *parent,
  39. const wxString& message,
  40. const wxString& defaultDir,
  41. const wxString& defaultFileName,
  42. const wxString& wildCard,
  43. long style,
  44. const wxPoint& pos,
  45. const wxSize& sz,
  46. const wxString& name)
  47. : wxFileDialogBase(parent, message, defaultDir, defaultFileName,
  48. wildCard, style, pos, sz, name)
  49. {
  50. wxTopLevelWindows.Append(this);
  51. wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
  52. if ( parent )
  53. parent->AddChild(this);
  54. m_cocoaNSWindow = nil;
  55. m_cocoaNSView = nil;
  56. //Init the wildcard array
  57. m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
  58. //If the user requests to save - use a NSSavePanel
  59. //else use a NSOpenPanel
  60. if (HasFlag(wxFD_SAVE))
  61. {
  62. SetNSPanel([NSSavePanel savePanel]);
  63. [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
  64. [GetNSSavePanel() setPrompt:@"Save"];
  65. [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
  66. [GetNSSavePanel() setCanSelectHiddenExtension:YES];
  67. // Cached as per-app in obj-c
  68. // [GetNSSavePanel() setExtensionHidden:YES];
  69. //
  70. // NB: Note that only Panther supports wildcards
  71. // with save dialogs - not that wildcards in save
  72. // dialogs are all that useful, anyway :)
  73. //
  74. }
  75. else //m_dialogStyle & wxFD_OPEN
  76. {
  77. SetNSPanel([NSOpenPanel openPanel]);
  78. [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
  79. [(NSOpenPanel*)m_cocoaNSWindow setAllowsMultipleSelection:(HasFlag(wxFD_MULTIPLE))];
  80. [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
  81. [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:YES];
  82. [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:NO];
  83. [GetNSSavePanel() setPrompt:@"Open"];
  84. //convert wildcards - open panel only takes file extensions -
  85. //no actual wildcards here :)
  86. size_t lastwcpos = 0;
  87. bool bDescription = true;
  88. size_t i;
  89. for(i = wildCard.find('|');
  90. i != wxString::npos;
  91. i = wildCard.find('|', lastwcpos+1))
  92. {
  93. size_t oldi = i;
  94. if(!bDescription)
  95. {
  96. bDescription = !bDescription;
  97. //work backwards looking for a period
  98. while(i != lastwcpos && wildCard[--i] != '.') {}
  99. if(i == lastwcpos)
  100. {
  101. //no extension - can't use this wildcard
  102. lastwcpos = oldi;
  103. continue;
  104. }
  105. [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
  106. }
  107. else
  108. bDescription = !bDescription;
  109. lastwcpos = oldi;
  110. }
  111. if (!bDescription)
  112. {
  113. //get last wildcard
  114. size_t oldi = wildCard.length();
  115. i = oldi;
  116. //work backwards looking for a period
  117. while(i != lastwcpos && wildCard[--i] != '.') {}
  118. if(i != lastwcpos)
  119. [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
  120. }
  121. if ([m_wildcards count] == 0)
  122. {
  123. [m_wildcards release];
  124. m_wildcards = nil;
  125. }
  126. }
  127. }
  128. wxFileDialog::~wxFileDialog()
  129. {
  130. [m_wildcards release];
  131. }
  132. void wxFileDialog::GetPaths(wxArrayString& paths) const
  133. {
  134. paths.Empty();
  135. wxString dir(m_dir);
  136. if ( m_dir.Last() != wxT('\\') )
  137. dir += wxT('\\');
  138. size_t count = m_fileNames.GetCount();
  139. for ( size_t n = 0; n < count; n++ )
  140. {
  141. if (wxFileName(m_fileNames[n]).IsAbsolute())
  142. paths.Add(m_fileNames[n]);
  143. else
  144. paths.Add(dir + m_fileNames[n]);
  145. }
  146. }
  147. void wxFileDialog::GetFilenames(wxArrayString& files) const
  148. {
  149. files = m_fileNames;
  150. }
  151. void wxFileDialog::SetPath(const wxString& path)
  152. {
  153. wxString ext;
  154. wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
  155. if ( !ext.empty() )
  156. m_fileName << wxT('.') << ext;
  157. }
  158. int wxFileDialog::ShowModal()
  159. {
  160. WX_HOOK_MODAL_DIALOG();
  161. wxAutoNSAutoreleasePool thePool;
  162. m_fileNames.Empty();
  163. int nResult;
  164. if (HasFlag(wxFD_SAVE))
  165. {
  166. nResult = [GetNSSavePanel()
  167. runModalForDirectory:wxNSStringWithWxString(m_dir)
  168. file:wxNSStringWithWxString(m_fileName)];
  169. if (nResult == NSOKButton)
  170. {
  171. m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
  172. m_path = m_fileNames[0];
  173. }
  174. }
  175. else //m_dialogStyle & wxFD_OPEN
  176. {
  177. nResult = [(NSOpenPanel*)m_cocoaNSWindow
  178. runModalForDirectory:wxNSStringWithWxString(m_dir)
  179. file:wxNSStringWithWxString(m_fileName)
  180. types:m_wildcards];
  181. if (nResult == NSOKButton)
  182. {
  183. for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
  184. {
  185. m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
  186. }
  187. m_path = m_fileNames[0];
  188. }
  189. }
  190. return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
  191. }
  192. #endif // wxUSE_FILEDLG