1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3 
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #include "toolutil.h"
27 #include "msgbox.h"
28 #include "filebox.h"
29 #include <string.h>
30 #include "macros.h"
31 
32 #ifndef WIN32
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <pwd.h>
36 #endif
37 
38 
open_dialog_fn(const char * mode,const char * title,const char * filter,GfxDomain * domain,GfxSurface * gfx,const Font * largefont,const Font * smallfont,const char * deffilename,char * filename,int filename_size)39 char * open_dialog_fn(const char *mode, const char *title, const char *filter, GfxDomain *domain, GfxSurface *gfx, const Font *largefont, const Font *smallfont, const char *deffilename, char *filename, int filename_size)
40 {
41 	if (deffilename)
42 		strncpy(filename, deffilename, filename_size - 1);
43 	else
44 		strcpy(filename, "");
45 
46 	if (filebox(title, mode[0] == 'w' ? FB_SAVE : FB_OPEN, filename, filename_size - 1, filter, domain, gfx, largefont, smallfont) == FB_OK)
47 	{
48 		return filename;
49 	}
50 	else
51 		return NULL;
52 }
53 
54 
open_dialog(const char * mode,const char * title,const char * filter,GfxDomain * domain,GfxSurface * gfx,const Font * largefont,const Font * smallfont,const char * deffilename)55 FILE *open_dialog(const char *mode, const char *title, const char *filter, GfxDomain *domain, GfxSurface *gfx, const Font *largefont, const Font *smallfont, const char *deffilename)
56 {
57 	char filename[5000];
58 
59 	if (open_dialog_fn(mode, title, filter, domain, gfx, largefont, smallfont, deffilename, filename, sizeof(filename)))
60 	{
61 		FILE * f = fopen(filename, mode);
62 		if (!f) msgbox(domain, gfx, largefont, "Could not open file", MB_OK);
63 		return f;
64 	}
65 	else
66 		return NULL;
67 }
68 
69 
confirm(GfxDomain * domain,GfxSurface * gfx,const Font * font,const char * msg)70 int confirm(GfxDomain *domain, GfxSurface *gfx, const Font *font, const char *msg)
71 {
72 	return msgbox(domain, gfx, font, msg, MB_YES|MB_NO) == MB_YES; // MessageBox(0, msg, "Confirm", MB_YESNO) == IDYES;
73 }
74 
confirm_ync(GfxDomain * domain,GfxSurface * gfx,const Font * font,const char * msg)75 int confirm_ync(GfxDomain *domain, GfxSurface *gfx, const Font *font, const char *msg)
76 {
77 	int r = msgbox(domain, gfx, font, msg, MB_YES|MB_NO|MB_CANCEL);
78 
79 	if (r == MB_YES)
80 	{
81 		return 1;
82 	}
83 	if (r == MB_NO)
84 	{
85 		return -1;
86 	}
87 
88 	return 0;
89 
90 }
91 
92 
expand_tilde(const char * path)93 char * expand_tilde(const char * path)
94 {
95 	if (path[0] != '~') return NULL;
96 
97 #ifndef WIN32
98 	const char *rest = strchr(path, '/');
99 #else
100 	const char *rest = strchr(path, '/');
101 	if (!rest) rest = strchr(path, '\\');
102 #endif
103 
104 	size_t rest_len = 0;
105 
106 	if (rest != NULL)
107 	{
108 		rest_len = strlen(rest);
109 	}
110 
111 	const char *homedir = NULL;
112 
113 #ifndef WIN32
114 	struct passwd *pw = getpwuid(getuid());
115 
116 	if (pw)
117 		homedir = pw->pw_dir;
118 #else
119 	homedir = getenv("USERPROFILE");
120 #endif
121 
122 	char * final = malloc(strlen(homedir) + rest_len + 2);
123 
124 	strcpy(final, homedir);
125 
126 	if (rest) strcat(final, rest);
127 
128 	return final;
129 }
130