Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 93b3d28

Browse files
committed
Abstracted the routine called for answer file/folder
1 parent 270d94b commit 93b3d28

File tree

3 files changed

+75
-105
lines changed

3 files changed

+75
-105
lines changed

engine/src/desktop-ans.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int MCA_folder(MCStringRef p_title, MCStringRef p_prompt, MCStringRef p_initial,
6060
MCPlatformWindowRef t_owner;
6161
t_owner = compute_sheet_owner(p_options);
6262

63-
MCPlatformBeginFolderDialog(t_owner, p_title, p_prompt, p_initial);
63+
MCPlatformBeginFolderOrFileDialog(kMCPlatformFileDialogKindFolder, t_owner, p_title, p_prompt, nil, 1, p_initial, false);
6464

6565
MCPlatformDialogResult t_result;
6666
MCAutoStringRef t_folder;
@@ -158,7 +158,7 @@ int MCA_file(MCStringRef p_title, MCStringRef p_prompt, MCStringRef p_filter, MC
158158
else
159159
t_kind = kMCPlatformFileDialogKindOpen;
160160

161-
MCPlatformBeginFileDialog(t_kind, t_owner, p_title, p_prompt, *t_types, t_types . Count(), p_initial);
161+
MCPlatformBeginFolderOrFileDialog(t_kind, t_owner, p_title, p_prompt, *t_types, t_types . Count(), p_initial, true);
162162

163163
MCPlatformDialogResult t_result;
164164
MCAutoStringRef t_file, t_type;
@@ -191,7 +191,7 @@ int MCA_file_with_types(MCStringRef p_title, MCStringRef p_prompt, MCStringRef *
191191
else
192192
t_kind = kMCPlatformFileDialogKindOpen;
193193

194-
MCPlatformBeginFileDialog(t_kind, t_owner, p_title, p_prompt, p_types, p_type_count, p_initial);
194+
MCPlatformBeginFolderOrFileDialog(t_kind, t_owner, p_title, p_prompt, p_types, p_type_count, p_initial, true);
195195

196196
MCPlatformDialogResult t_result;
197197
MCAutoStringRef t_file, t_type;
@@ -227,7 +227,7 @@ int MCA_ask_file(MCStringRef p_title, MCStringRef p_prompt, MCStringRef p_filter
227227
MCPlatformWindowRef t_owner;
228228
t_owner = compute_sheet_owner(p_options);
229229

230-
MCPlatformBeginFileDialog(kMCPlatformFileDialogKindSave, t_owner, p_title, p_prompt, *t_types, t_types . Count(), p_initial);
230+
MCPlatformBeginFolderOrFileDialog(kMCPlatformFileDialogKindSave, t_owner, p_title, p_prompt, *t_types, t_types . Count(), p_initial, true);
231231

232232
MCPlatformDialogResult t_result;
233233
MCAutoStringRef t_file, t_type;
@@ -254,7 +254,7 @@ int MCA_ask_file_with_types(MCStringRef p_title, MCStringRef p_prompt, MCStringR
254254
MCPlatformWindowRef t_owner;
255255
t_owner = compute_sheet_owner(p_options);
256256

257-
MCPlatformBeginFileDialog(kMCPlatformFileDialogKindSave, t_owner, p_title, p_prompt, p_types, p_type_count, p_initial);
257+
MCPlatformBeginFolderOrFileDialog(kMCPlatformFileDialogKindSave, t_owner, p_title, p_prompt, p_types, p_type_count, p_initial, true);
258258

259259
MCPlatformDialogResult t_result;
260260
MCAutoStringRef t_file, t_type;

engine/src/mac-dialog.mm

Lines changed: 68 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -204,48 +204,6 @@ static MCPlatformDialogResult MCPlatformEndOpenSaveDialog(void)
204204

205205
////////////////////////////////////////////////////////////////////////////////
206206

207-
void MCPlatformBeginFolderDialog(MCPlatformWindowRef p_owner, MCStringRef p_title, MCStringRef p_prompt, MCStringRef p_initial)
208-
{
209-
MCAutoStringRef t_initial_folder;
210-
211-
if (p_initial != nil)
212-
/* UNCHECKED */ folder_path_from_initial_path(p_initial, &t_initial_folder);
213-
214-
NSOpenPanel *t_panel;
215-
t_panel = [NSOpenPanel openPanel];
216-
// If we have both a title and a message, then 'title' is used on setTitle, and 'prompt' is used for message.
217-
// If there is no title, then on 10.11+ we must use the message field of the dialog; on <10.11 we must use the title field
218-
if (p_title != nil && MCStringGetLength(p_title) != 0)
219-
{
220-
[t_panel setTitle: [NSString stringWithMCStringRef: p_title]];
221-
[t_panel setMessage: [NSString stringWithMCStringRef: p_prompt]];
222-
}
223-
else
224-
{
225-
extern uint4 MCmajorosversion;
226-
if (MCmajorosversion >= 0x10B0)
227-
[t_panel setMessage: [NSString stringWithMCStringRef: p_prompt]];
228-
else
229-
[t_panel setTitle: [NSString stringWithMCStringRef: p_prompt]];
230-
231-
}
232-
[t_panel setPrompt: @"Choose"];
233-
[t_panel setCanChooseFiles: NO];
234-
[t_panel setCanChooseDirectories: YES];
235-
[t_panel setAllowsMultipleSelection: NO];
236-
237-
// MM-2012-03-01: [[ BUG 10046]] Make sure the "new folder" button is enabled for folder dialogs
238-
[t_panel setCanCreateDirectories: YES];
239-
240-
if ([t_panel respondsToSelector:@selector(isAccessoryViewDisclosed)])
241-
{
242-
// show accessory view when dialog opens
243-
[t_panel setAccessoryViewDisclosed: YES];
244-
}
245-
246-
MCMacPlatformBeginOpenSaveDialog(p_owner, t_panel, *t_initial_folder, nil);
247-
}
248-
249207
MCPlatformDialogResult MCPlatformEndFolderDialog(MCStringRef& r_selected_folder)
250208
{
251209
if (s_dialog_nesting -> result == kMCPlatformDialogResultContinue)
@@ -605,70 +563,82 @@ - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
605563

606564
////////////////////////////////////////////////////////////////////////////////
607565

608-
void MCPlatformBeginFileDialog(MCPlatformFileDialogKind p_kind, MCPlatformWindowRef p_owner, MCStringRef p_title, MCStringRef p_prompt, MCStringRef *p_types, uint4 p_type_count, MCStringRef p_initial)
566+
void MCPlatformBeginFolderOrFileDialog(MCPlatformFileDialogKind p_kind, MCPlatformWindowRef p_owner, MCStringRef p_title, MCStringRef p_prompt, MCStringRef *p_types, uint4 p_type_count, MCStringRef p_initial, bool p_is_file)
609567
{
610-
MCAutoStringRef t_initial_folder;
611-
if (p_initial != nil)
612-
/* UNCHECKED */ folder_path_from_initial_path(p_initial, &t_initial_folder);
613-
614-
MCAutoStringRef t_initial_file;
615-
if ((p_kind == kMCPlatformFileDialogKindSave) && p_initial != nil && !MCS_exists(p_initial, false))
616-
{
617-
uindex_t t_last_slash;
568+
MCAutoStringRef t_initial_folder;
569+
if (p_initial != nil)
570+
/* UNCHECKED */ folder_path_from_initial_path(p_initial, &t_initial_folder);
571+
572+
MCAutoStringRef t_initial_file;
573+
if ((p_kind == kMCPlatformFileDialogKindSave) && p_initial != nil && !MCS_exists(p_initial, false))
574+
{
575+
uindex_t t_last_slash;
618576
if (MCStringLastIndexOfChar(p_initial, '/', UINT32_MAX, kMCStringOptionCompareExact, t_last_slash))
619577
// SN-2014-08-11: [[ Bug 13143 ]] Take the right part: after the last slash, not before
620578
MCStringCopySubstring(p_initial, MCRangeMake(t_last_slash + 1, MCStringGetLength(p_initial) - t_last_slash - 1), &t_initial_file);
621579
else
622580
t_initial_file = p_initial;
623-
}
624-
625-
NSSavePanel *t_panel;
626-
t_panel = (p_kind == kMCPlatformFileDialogKindSave) ? [NSSavePanel savePanel] : [NSOpenPanel openPanel] ;
627-
628-
if (p_title != nil && !MCStringIsEmpty(p_title))
629-
{
630-
[t_panel setTitle: [NSString stringWithMCStringRef: p_title]];
631-
[t_panel setMessage: [NSString stringWithMCStringRef: p_prompt]];
632-
}
633-
else
634-
{
635-
extern uint4 MCmajorosversion;
636-
if (MCmajorosversion >= 0x10B0 && p_kind != kMCPlatformFileDialogKindSave)
637-
[t_panel setMessage: [NSString stringWithMCStringRef: p_prompt]];
638-
else
639-
[t_panel setTitle: [NSString stringWithMCStringRef: p_prompt]];
640-
}
641-
642-
// MW-2014-07-17: [[ Bug 12826 ]] If we have at least one type, add a delegate. Only add as
643-
// an accessory view if more than one type.
644-
MCFileDialogAccessoryView *t_accessory;
645-
if (p_type_count > 0)
581+
}
582+
583+
NSSavePanel *t_panel;
584+
t_panel = (p_kind == kMCPlatformFileDialogKindSave) ? [NSSavePanel savePanel] : [NSOpenPanel openPanel] ;
585+
586+
// If we have both a title and a message, then 'title' is used on setTitle, and 'prompt' is used for message.
587+
// If there is no title, then on 10.11+ we must use the message field of the dialog; on <10.11 we must use the title field
588+
if (p_title != nil && !MCStringIsEmpty(p_title))
646589
{
647-
t_accessory = [[MCFileDialogAccessoryView alloc] initWithPanel: t_panel];
648-
[t_accessory setTypes: p_types length: p_type_count];
649-
[t_accessory setLabel: @"Format:"];
650-
if (p_type_count > 1)
651-
[t_panel setAccessoryView: t_accessory];
652-
[t_panel setDelegate: t_accessory];
653-
}
590+
[t_panel setTitle: [NSString stringWithMCStringRef: p_title]];
591+
[t_panel setMessage: [NSString stringWithMCStringRef: p_prompt]];
592+
}
593+
else
594+
{
595+
extern uint4 MCmajorosversion;
596+
if (MCmajorosversion >= 0x10B0 && p_kind != kMCPlatformFileDialogKindSave)
597+
[t_panel setMessage: [NSString stringWithMCStringRef: p_prompt]];
598+
else
599+
[t_panel setTitle: [NSString stringWithMCStringRef: p_prompt]];
600+
}
654601

655-
if (p_kind != kMCPlatformFileDialogKindSave)
656-
{
657-
[(NSOpenPanel *)t_panel setCanChooseFiles: YES];
658-
[(NSOpenPanel *)t_panel setCanChooseDirectories: NO];
659-
[(NSOpenPanel *)t_panel setAllowsMultipleSelection: p_kind == kMCPlatformFileDialogKindOpenMultiple ? YES : NO];
660-
}
661-
// MM-2012-03-01: [[ BUG 10046]] Make sure the "new folder" button is enabled for save dialogs
662-
else
663-
[t_panel setCanCreateDirectories: YES];
664-
665-
if ([t_panel respondsToSelector:@selector(isAccessoryViewDisclosed)])
666-
{
667-
// show accessory view when dialog opens
668-
[t_panel setAccessoryViewDisclosed: YES];
669-
}
670-
671-
MCMacPlatformBeginOpenSaveDialog(p_owner, t_panel, *t_initial_folder, *t_initial_file);
602+
if (p_is_file)
603+
{
604+
// MW-2014-07-17: [[ Bug 12826 ]] If we have at least one type, add a delegate. Only add as
605+
// an accessory view if more than one type.
606+
MCFileDialogAccessoryView *t_accessory;
607+
if (p_type_count > 0)
608+
{
609+
t_accessory = [[MCFileDialogAccessoryView alloc] initWithPanel: t_panel];
610+
[t_accessory setTypes: p_types length: p_type_count];
611+
[t_accessory setLabel: @"Format:"];
612+
if (p_type_count > 1)
613+
[t_panel setAccessoryView: t_accessory];
614+
[t_panel setDelegate: t_accessory];
615+
}
616+
617+
if (p_kind != kMCPlatformFileDialogKindSave)
618+
{
619+
[(NSOpenPanel *)t_panel setCanChooseFiles: YES];
620+
[(NSOpenPanel *)t_panel setCanChooseDirectories: NO];
621+
[(NSOpenPanel *)t_panel setAllowsMultipleSelection: p_kind == kMCPlatformFileDialogKindOpenMultiple ? YES : NO];
622+
}
623+
// MM-2012-03-01: [[ BUG 10046]] Make sure the "new folder" button is enabled for save dialogs
624+
else
625+
[t_panel setCanCreateDirectories: YES];
626+
}
627+
else
628+
{
629+
[t_panel setPrompt: @"Choose"];
630+
[t_panel setCanChooseFiles: NO];
631+
[t_panel setCanChooseDirectories: YES];
632+
[t_panel setAllowsMultipleSelection: NO];
633+
}
634+
635+
if ([t_panel respondsToSelector:@selector(isAccessoryViewDisclosed)])
636+
{
637+
// show accessory view when dialog opens
638+
[t_panel setAccessoryViewDisclosed: YES];
639+
}
640+
641+
MCMacPlatformBeginOpenSaveDialog(p_owner, t_panel, *t_initial_folder, p_is_file ? *t_initial_file : nil);
672642
}
673643

674644
MCPlatformDialogResult MCPlatformEndFileDialog(MCPlatformFileDialogKind p_kind, MCStringRef &r_paths, MCStringRef &r_type)

engine/src/platform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,13 +939,13 @@ enum MCPlatformFileDialogKind
939939
kMCPlatformFileDialogKindSave,
940940
kMCPlatformFileDialogKindOpen,
941941
kMCPlatformFileDialogKindOpenMultiple,
942+
kMCPlatformFileDialogKindFolder,
942943
};
943944

944-
void MCPlatformBeginFolderDialog(MCPlatformWindowRef owner, MCStringRef p_title, MCStringRef p_message, MCStringRef p_initial);
945+
void MCPlatformBeginFolderOrFileDialog(MCPlatformFileDialogKind p_kind, MCPlatformWindowRef p_owner, MCStringRef p_title, MCStringRef p_prompt, MCStringRef *p_types, uint4 p_type_count, MCStringRef p_initial, bool p_is_file);
945946
MCPlatformDialogResult MCPlatformEndFolderDialog(MCStringRef & r_selected_folder);
946947

947948

948-
void MCPlatformBeginFileDialog(MCPlatformFileDialogKind p_kind, MCPlatformWindowRef p_owner, MCStringRef p_title, MCStringRef p_prompt, MCStringRef *p_types, uint4 p_type_count, MCStringRef p_initial);
949949
MCPlatformDialogResult MCPlatformEndFileDialog(MCPlatformFileDialogKind p_kind, MCStringRef& r_paths, MCStringRef& r_type);
950950

951951
void MCPlatformBeginColorDialog(MCStringRef p_title, const MCColor& p_color);

0 commit comments

Comments
 (0)