summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/common/entry/dialog_darwin.mm
blob: 97e60e5fce6b94092bf75a0609bb5e2ae3757197 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright 2019-2019 Attila Kocsis. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "entry_p.h"
#if BX_PLATFORM_OSX

#include <bx/allocator.h>
#include <bx/filepath.h>
#include <bx/string.h>
#include <bx/readerwriter.h>
#include <bx/process.h>
#include <bx/semaphore.h>

#import <AppKit/AppKit.h>

#include "dialog.h"

class Split
{
public:
	Split(const bx::StringView& _str, char _ch)
	: m_str(_str)
	, m_token(_str.getPtr(), bx::strFind(_str, _ch).getPtr() )
	, m_ch(_ch)
	{
	}
	
	bx::StringView next()
	{
		bx::StringView result = m_token;
		m_token = bx::strTrim(
							  bx::StringView(m_token.getTerm()+1
											 , bx::strFind(bx::StringView(m_token.getTerm()+1, m_str.getTerm() ), m_ch).getPtr() )
							  , " \t\n"
							  );
		return result;
	}
	
	bool isDone() const
	{
		return m_token.isEmpty();
	}
	
private:
	const bx::StringView& m_str;
	bx::StringView m_token;
	char m_ch;
};


bool openFileSelectionDialog(
							 bx::FilePath& _inOutFilePath
							 , FileSelectionDialogType::Enum _type
							 , const bx::StringView& _title
							 , const bx::StringView& _filter
							 )
{
	NSMutableArray* fileTypes = [NSMutableArray arrayWithCapacity:10];
	bx::Error err;
	
	for (bx::LineReader lr(_filter); !lr.isDone() && err.isOk();)
	{
		const bx::StringView line = lr.next();
		const bx::StringView sep  = bx::strFind(line, '|');
		
		if (!sep.isEmpty() )
		{
			for (Split split(bx::strTrim(bx::StringView(sep.getPtr()+1, line.getTerm() ), " "), ' '); !split.isDone() && err.isOk();)
			{
				const bx::StringView token = split.next();
				
				if ( token.getLength() >= 3 && token.getPtr()[0] == '*'
					&& token.getPtr()[1] == '.' && bx::isAlphaNum(token.getPtr()[2]) )
				{
					NSString* extension = [[NSString alloc] initWithBytes:token.getPtr()+2 length:token.getLength()-2 encoding:NSASCIIStringEncoding];
					[fileTypes addObject: extension];
				}
			}
		}
	}
	
	__block NSString* fileName = nil;
	bx::Semaphore semaphore;
	bx::Semaphore* psemaphore = &semaphore;
	
	CFRunLoopPerformBlock([[NSRunLoop mainRunLoop] getCFRunLoop],
						  kCFRunLoopCommonModes,
				    ^{
						NSSavePanel* panel = nil;
						
						if ( FileSelectionDialogType::Open == _type)
						{
							NSOpenPanel* openPanel = [NSOpenPanel openPanel];
							openPanel.canChooseFiles = TRUE;
							openPanel.allowsMultipleSelection = FALSE;
							openPanel.canChooseDirectories = FALSE;
							panel = openPanel;
						}
						else
						{
							panel = [NSSavePanel savePanel];
						}
						
						panel.message = [[NSString alloc] initWithBytes:_title.getPtr() length:_title.getLength() encoding:NSASCIIStringEncoding];
						panel.directoryURL = [NSURL URLWithString:@(_inOutFilePath.getCPtr())];
						panel.allowedFileTypes = fileTypes;
						
						if ([panel runModal] == NSModalResponseOK)
						{
							NSURL* url = [panel URL];
							if (nil != url)
							{
								fileName = [url path];
								[fileName retain];
							}
					   	}
						[panel close];
						psemaphore->post();
				   });

	semaphore.wait();

	if ( fileName != nil )
	{
		_inOutFilePath.set([fileName UTF8String]);
		[fileName release];
		return true;
	}
	
	return false;
}

#endif