select.c
4.87 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270. Registro no INPI sob
* o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como select.c e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*/
#include "private.h"
#include <stdarg.h>
#ifdef WIN32
#include <gdk/gdkwin32.h>
#endif // WIN32
/*--[ Implement ]------------------------------------------------------------------------------------*/
/*
#if defined(_WIN32)
struct file {
OPENFILENAME ofn;
gboolean enabled;
char szName[260]; // buffer for file name
GtkFileChooserAction action;
BOOL ok;
};
static gpointer select_file(struct file *fl) {
switch(fl->action) {
case GTK_FILE_CHOOSER_ACTION_SAVE: // Receber arquivo
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms646839(v=vs.85).aspx
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms646829(v=vs.85).aspx#open_file
fl->ofn.Flags = OFN_OVERWRITEPROMPT | OFN_CREATEPROMPT | OFN_HIDEREADONLY;
fl->ok = GetSaveFileName(&fl->ofn);
break;
case GTK_FILE_CHOOSER_ACTION_OPEN: // Enviar arquivo
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms646928(v=vs.85).aspx
fl->ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
fl->ok = GetOpenFileName(&fl->ofn);
break;
}
fl->enabled = FALSE;
return 0;
}
#endif // _WIN32
*/
gchar * v3270ft_select_file(v3270ft *dialog, const gchar *title, const gchar *button, GtkFileChooserAction action, G_GNUC_UNUSED const gchar *filename, G_GNUC_UNUSED const gchar *filter, ...) {
gchar *rc = NULL;
#if GTK_CHECK_VERSION(3,20,0)
GtkFileChooserNative *native = gtk_file_chooser_native_new
(
title,
GTK_WINDOW(dialog),
action,
button,
_( "_Cancel" )
);
if(gtk_native_dialog_run(GTK_NATIVE_DIALOG (native)) == GTK_RESPONSE_ACCEPT) {
rc = g_strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native)));
}
g_object_unref(native);
#elif defined(_WIN32)
GThread * thd;
struct file fl;
GdkWindow * win = gtk_widget_get_window(GTK_WIDGET(dialog));
gtk_widget_set_sensitive(GTK_WIDGET(dialog),FALSE);
memset(&fl,0,sizeof(fl));
fl.ofn.lStructSize = sizeof(fl.ofn);
fl.ofn.hwndOwner = GDK_WINDOW_HWND(win);
fl.ofn.lpstrFile = fl.szName;
fl.ofn.lpstrTitle = title;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
fl.ofn.lpstrFile[0] = '\0';
fl.ofn.nMaxFile = sizeof(fl.szName);
// Monta lista de arquivos.
va_list args;
size_t ix = 0;
fl.ofn.lpstrFilter = (char *) g_malloc0(4096);
va_start (args, filter);
while(filter) {
filter = gettext(filter);
size_t sz = strlen(filter)+1;
if(ix+sz > 4095)
break;
debug("%s",filter);
memcpy(((char *) fl.ofn.lpstrFilter)+ix,filter,sz);
ix += sz;
filter = va_arg(args, const char *);
}
va_end (args);
debug("%s",fl.ofn.lpstrFilter);
fl.ofn.nFilterIndex = 1;
fl.ofn.lpstrInitialDir = NULL;
fl.ofn.nMaxFileTitle = 0;
// Guarda o valor atual
if(filename)
strncpy(fl.szName,filename,fl.ofn.nMaxFile);
fl.action = action;
thd = g_thread_new("GetFileName",(GThreadFunc) select_file, &fl);
fl.enabled = TRUE;
while(fl.enabled) {
g_main_context_iteration(NULL,TRUE);
}
g_thread_unref(thd);
if(fl.ok) {
rc = g_strdup(fl.szName);
}
g_free( ((char *) fl.ofn.lpstrFilter) );
gtk_widget_set_sensitive(GTK_WIDGET(dialog),TRUE);
#else
GtkWidget * chooser = gtk_file_chooser_dialog_new
(
title,
GTK_WINDOW(dialog),
action,
_("_Cancel" ), GTK_RESPONSE_CANCEL,
button, GTK_RESPONSE_ACCEPT,
NULL
);
if(filename && *filename)
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(chooser),filename);
if(gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_ACCEPT) {
rc = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
}
gtk_widget_destroy(chooser);
#endif // WIN32
return rc;
}