osxCocoaUtilities.mm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import "platformOSX/platformOSX.h"
  23. #import "fileDialog.h"
  24. #import "osxCocoaUtilities.h"
  25. @implementation NSSavePanel (TorqueFileDialogs)
  26. //-----------------------------------------------------------------------------
  27. - (void)setFilters:(StringTableEntry)filters
  28. {
  29. FileDialogFileTypeList fileTypes (filters);
  30. NSMutableArray *types = [NSMutableArray arrayWithCapacity:10];
  31. char *token = fileTypes.filterData;
  32. char *place = fileTypes.filterData;
  33. // scan the filter list until we hit a null.
  34. // when we see the separator '|', replace it with a null, and save the token
  35. // format is description|extension|description|extension
  36. bool isDesc = true;
  37. for (; *place; place++)
  38. {
  39. if (*place != '|')
  40. {
  41. continue;
  42. }
  43. *place = '\0';
  44. if (isDesc)
  45. {
  46. fileTypes.names.push_back(token);
  47. }
  48. else
  49. {
  50. // detect *.*
  51. if (dStrstr(token, "*.*"))
  52. {
  53. fileTypes.any = true;
  54. }
  55. fileTypes.exts.push_back([self getFileExtensionsFromString:token]);
  56. }
  57. isDesc = !isDesc;
  58. ++place;
  59. token = place;
  60. }
  61. fileTypes.exts.push_back([self getFileExtensionsFromString:token]);
  62. for (U32 i = 0; i < fileTypes.exts.size(); i++)
  63. {
  64. for (U32 j = 0; j < fileTypes.exts[i]->list.size(); j++)
  65. {
  66. char *ext = fileTypes.exts[i]->list[j];
  67. if (ext)
  68. {
  69. // MP: Added for 1.4.1
  70. // Passing *.*, *., .*, or just . does nothing on OS X 10.6
  71. // Manually adding the extensions supported by the engine for now
  72. if (dStrncmp(ext, "*.*", 3) == 0)
  73. {
  74. [types addObject:[NSString stringWithUTF8String:"png"]];
  75. [types addObject:[NSString stringWithUTF8String:"jpg"]];
  76. [types addObject:[NSString stringWithUTF8String:"eff"]];
  77. [types addObject:[NSString stringWithUTF8String:"lyr"]];
  78. [types addObject:[NSString stringWithUTF8String:"gui"]];
  79. [types addObject:[NSString stringWithUTF8String:"bmp"]];
  80. continue;
  81. }
  82. if (dStrncmp(ext, "*.", 2) == 0)
  83. {
  84. ext += 2;
  85. }
  86. [types addObject:[NSString stringWithUTF8String:ext]];
  87. }
  88. }
  89. }
  90. if ([types count] > 0)
  91. {
  92. [self setAllowedFileTypes:types];
  93. }
  94. if (fileTypes.any)
  95. {
  96. [self setAllowsOtherFileTypes:YES];
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. - (void)setAttributesFromData:(FileDialogData *)data
  101. {
  102. [self setCanCreateDirectories:YES];
  103. [self setCanSelectHiddenExtension:YES];
  104. [self setTreatsFilePackagesAsDirectories:YES];
  105. }
  106. //-----------------------------------------------------------------------------
  107. - (FileDialogFileExtList *)getFileExtensionsFromString:(char const *)filter
  108. {
  109. FileDialogFileExtList *list = new FileDialogFileExtList(filter);
  110. char *token = list->data;
  111. char *place = list->data;
  112. for (; *place; place++)
  113. {
  114. if (*place != ';')
  115. {
  116. continue;
  117. }
  118. *place = '\0';
  119. list->list.push_back(token);
  120. ++place;
  121. token = place;
  122. }
  123. // last token
  124. list->list.push_back(token);
  125. return list;
  126. }
  127. + (NSArray *)showSavePanel:(FileDialogData *)withData
  128. {
  129. U32 runResult;
  130. NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
  131. NSSavePanel *panel = [NSSavePanel savePanel];
  132. NSString *dir;
  133. if (dStrlen(withData->mDefaultPath) < 1)
  134. dir = [[NSString stringWithUTF8String:withData->mDefaultFile] stringByDeletingLastPathComponent];
  135. else
  136. dir = [NSString stringWithUTF8String:withData->mDefaultPath];
  137. [panel setDirectoryURL:[NSURL fileURLWithPath:dir]];
  138. [panel setFilters:withData->mFilters];
  139. runResult = (U32) [panel runModal];
  140. if (runResult != NSFileHandlingPanelCancelButton)
  141. [array addObject:[panel URL]];
  142. return array;
  143. }
  144. @end
  145. @implementation NSOpenPanel (TorqueFileDialogs)
  146. //-----------------------------------------------------------------------------
  147. - (void)setAttributesFromData:(FileDialogData *)data
  148. {
  149. bool chooseDir = (data->mStyle & FileDialogData::FDS_BROWSEFOLDER);
  150. [self setCanCreateDirectories:YES];
  151. [self setCanSelectHiddenExtension:YES];
  152. [self setTreatsFilePackagesAsDirectories:YES];
  153. [self setAllowsMultipleSelection:(data->mStyle & FileDialogData::FDS_MULTIPLEFILES)];
  154. [self setCanChooseFiles:!chooseDir];
  155. [self setCanChooseDirectories:chooseDir];
  156. if (chooseDir)
  157. {
  158. [self setPrompt:@"Choose"];
  159. [self setTitle:@"Choose Folder"];
  160. }
  161. }
  162. //-----------------------------------------------------------------------------
  163. + (NSArray *)showOpenPanel:(FileDialogData *)withData
  164. {
  165. U32 runResult;
  166. NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
  167. NSOpenPanel *panel = [NSOpenPanel openPanel];
  168. [panel setAttributesFromData:withData];
  169. NSString *dir;
  170. if (dStrlen(withData->mDefaultPath) < 1)
  171. dir = [[NSString stringWithUTF8String:withData->mDefaultFile] stringByDeletingLastPathComponent];
  172. else
  173. dir = [NSString stringWithUTF8String:withData->mDefaultPath];
  174. [panel setDirectoryURL:[NSURL fileURLWithPath:dir]];
  175. [panel setFilters:withData->mFilters];
  176. runResult = (U32) [panel runModal];
  177. if (runResult != NSFileHandlingPanelCancelButton)
  178. {
  179. if ([panel allowsMultipleSelection])
  180. [array addObjectsFromArray:[panel URLs]];
  181. else
  182. [array addObject:[panel URL]];
  183. }
  184. return array;
  185. }
  186. @end
  187. @implementation osxPopupMenuController
  188. @synthesize menu = _menu;
  189. @synthesize menuItem = _menuItem;
  190. @synthesize owner = _owner;
  191. -(id) init
  192. {
  193. self = [super init];
  194. if (self)
  195. {
  196. _owner = NULL;
  197. _menu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@"New Menu"];
  198. _menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"New Menu Item" action:NULL keyEquivalent:@""];
  199. [_menuItem setSubmenu:_menu];
  200. }
  201. return self;
  202. }
  203. -(void) dealloc
  204. {
  205. if (_menu)
  206. [_menu release];
  207. if (_menuItem)
  208. [_menuItem release];
  209. [super dealloc];
  210. }
  211. -(void)handleSelect:(id)sender
  212. {
  213. if(!_owner)
  214. {
  215. Con::errorf("osxPopupMenuController::handleSelect error: _owner is invalid");
  216. return;
  217. }
  218. // Get the title
  219. NSString* itemTitle = [sender title];
  220. // Get the index
  221. NSMenu* menu = [sender menu];
  222. S32 index = (S32)[menu indexOfItemWithTitle:itemTitle];
  223. _owner->handleSelect(index, [[_menu title] UTF8String]);
  224. }
  225. @end