The R Project SVN R

Rev

Rev 46784 | Rev 50851 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 46784 Rev 46842
Line 92... Line 92...
92
    settext(c, wtitle);
92
    settext(c, wtitle);
93
}
93
}
94
 
94
 
95
/*** FILE MANAGEMENT FUNCTIONS ***/
95
/*** FILE MANAGEMENT FUNCTIONS ***/
96
 
96
 
-
 
97
FILE *R_wfopen(const wchar_t *filename, const wchar_t *mode);
-
 
98
 
97
static void editor_load_file(editor c, const char *name, int enc)
99
static void editor_load_file(editor c, const char *name, int enc)
98
{
100
{
99
    textbox t = getdata(c);
101
    textbox t = getdata(c);
100
    EditorData p = getdata(t);
102
    EditorData p = getdata(t);
101
    FILE *f;
103
    FILE *f;
102
    char *buffer = NULL, tmp[MAX_PATH+50];
104
    char *buffer = NULL, tmp[MAX_PATH+50];
-
 
105
    const char *sname;
103
    long num = 1, bufsize;
106
    long num = 1, bufsize;
104
 
107
 
-
 
108
    if(enc == CE_UTF8) {
-
 
109
	wchar_t wname[MAX_PATH+1];
-
 
110
	Rf_utf8towcs(wname, name, MAX_PATH+1);
-
 
111
	f = R_wfopen(wname, L"r");
105
    /* we checked that file could be read in the caller */
112
	sname = reEnc(name, CE_UTF8, CE_NATIVE, 3);
-
 
113
    } else {
106
    f = R_fopen(name, "r");
114
	f = R_fopen(name, "r");
-
 
115
	sname = name;
-
 
116
    }
107
    if (f == NULL) return;
117
    if (f == NULL) {
-
 
118
	snprintf(tmp, MAX_PATH+50, 
-
 
119
		 G_("unable to open file %s for reading"), sname);
-
 
120
	R_ShowMessage(tmp);
-
 
121
	return;
-
 
122
    }
108
    p->file = 1;
123
    p->file = 1;
109
    strncpy(p->filename, name, MAX_PATH+1);
124
    strncpy(p->filename, name, MAX_PATH+1);
110
    bufsize = 0;
125
    bufsize = 0;
111
    while (num > 0) {
126
    while (num > 0) {
112
	buffer = realloc(buffer, bufsize + 3000 + 1);
127
	buffer = realloc(buffer, bufsize + 3000 + 1);
Line 114... Line 129...
114
	if (num >= 0) {
129
	if (num >= 0) {
115
	    bufsize += num;
130
	    bufsize += num;
116
	    buffer[bufsize] = '\0';
131
	    buffer[bufsize] = '\0';
117
	}
132
	}
118
	else {
133
	else {
119
	    snprintf(tmp, MAX_PATH+50, G_("Could not read from file '%s'"),
134
	    snprintf(tmp, MAX_PATH+50, 
120
		     name);
135
		     G_("Could not read from file '%s'"), sname);
121
	    askok(tmp);
136
	    askok(tmp);
122
	}
137
	}
123
    }
138
    }
124
    setlimittext(t, 2 * strlen(buffer));
139
    setlimittext(t, 2 * strlen(buffer));
125
    settext(t, buffer);
140
    settext(t, buffer);
126
    gsetmodified(t, 0);
141
    gsetmodified(t, 0);
127
    free(buffer);
142
    free(buffer);
128
    fclose(f);
143
    fclose(f);
129
}
144
}
130
 
145
 
131
static void editor_save_file(editor c, const char *name)
146
static void editor_save_file(editor c, const char *name, int enc)
132
{
147
{
133
    textbox t = getdata(c);
148
    textbox t = getdata(c);
134
    FILE *f;
149
    FILE *f;
135
    char buf[MAX_PATH+30];
150
    char buf[MAX_PATH+30];
-
 
151
    const char *sname;
-
 
152
 
136
    if (name == NULL)
153
    if (name == NULL)
137
	return;
154
	return;
138
    else {
155
    else {
-
 
156
	if(enc == CE_UTF8) {
-
 
157
	    wchar_t wname[MAX_PATH+1];
-
 
158
	    Rf_utf8towcs(wname, name, MAX_PATH+1);
-
 
159
	    sname = reEnc(name, CE_UTF8, CE_NATIVE, 3);
-
 
160
	    f = R_wfopen(wname, L"w");
-
 
161
	} else {
-
 
162
	    sname = name;
139
	f = R_fopen(name, "w");
163
	    f = R_fopen(sname, "w");
-
 
164
	}
140
	if (f == NULL) {
165
	if (f == NULL) {
141
	    snprintf(buf, MAX_PATH+30, G_("Could not save file '%s'"), name);
166
	    snprintf(buf, MAX_PATH+30, G_("Could not save file '%s'"), sname);
142
	    askok(buf);
167
	    askok(buf);
143
	    return;
168
	    return;
144
	}
169
	}
145
	fprintf(f, "%s", gettext(t));
170
	fprintf(f, "%s", gettext(t));
146
	fclose(f);
171
	fclose(f);
Line 149... Line 174...
149
 
174
 
150
static void editorsaveas(editor c)
175
static void editorsaveas(editor c)
151
{
176
{
152
    textbox t = getdata(c);
177
    textbox t = getdata(c);
153
    EditorData p = getdata(t);
178
    EditorData p = getdata(t);
154
    char *current_name = (p->file ? p->filename : "");
-
 
155
    char *name;
179
    wchar_t *wname;
-
 
180
 
156
    setuserfilter("R files (*.R)\0*.R\0S files (*.q, *.ssc, *.S)\0*.q;*.ssc;*.S\0All files (*.*)\0*.*\0\0");
181
    setuserfilterW(L"R files (*.R)\0*.R\0S files (*.q, *.ssc, *.S)\0*.q;*.ssc;*.S\0All files (*.*)\0*.*\0\0");
157
    name = askfilesave(G_("Save script as"), current_name);
182
    wname = askfilesaveW(G_("Save script as"), "");
158
    if (name == NULL)
183
    if (wname == NULL)
159
	return;
184
	return;
160
    else {
185
    else {
-
 
186
	char name[4*MAX_PATH+1];
-
 
187
	const char *tname;
-
 
188
	wcstoutf8(name, wname, MAX_PATH);
-
 
189
	tname = reEnc(name, CE_UTF8, CE_NATIVE, 3);
161
	editor_save_file(c, name);
190
	editor_save_file(c, name, CE_UTF8);
162
	p->file = 1;
191
	p->file = 1;
163
	strncpy(p->filename, name, MAX_PATH+1);
192
	strncpy(p->filename, tname, MAX_PATH+1);
164
	gsetmodified(t, 0);
193
	gsetmodified(t, 0);
165
	editor_set_title(c, name);
194
	editor_set_title(c, tname);
166
	show(c);
195
	show(c);
167
    }
196
    }
168
}
197
}
169
 
198
 
170
static void menueditorsaveas(control m)
199
static void menueditorsaveas(control m)
Line 177... Line 206...
177
static void editorsave(editor c)
206
static void editorsave(editor c)
178
{
207
{
179
    textbox t = getdata(c);
208
    textbox t = getdata(c);
180
    EditorData p = getdata(t);
209
    EditorData p = getdata(t);
181
    if (p->file) {  /* save existing file without prompt */
210
    if (p->file) {  /* save existing file without prompt */
182
	const char *current_name = p->filename;
-
 
183
	editor_save_file(c, current_name);
211
	editor_save_file(c, p->filename, CE_UTF8);
184
	gsetmodified(t, 0);
212
	gsetmodified(t, 0);
185
    }
213
    }
186
    /* if new file then prompt for name to save as */
214
    /* if new file then prompt for name to save as */
187
    else editorsaveas(c);
215
    else editorsaveas(c);
188
}
216
}
Line 336... Line 364...
336
    editornew();
364
    editornew();
337
}
365
}
338
 
366
 
339
static void editoropen(const char *default_name)
367
static void editoropen(const char *default_name)
340
{
368
{
341
    char *name;
369
    wchar_t *wname;
-
 
370
    char name[4*MAX_PATH];
-
 
371
    const char* title;
-
 
372
 
342
    int i; textbox t; EditorData p;
373
    int i; textbox t; EditorData p;
343
    setuserfilter("R files (*.R)\0*.R\0S files (*.q, *.ssc, *.S)\0*.q;*.ssc;*.S\0All files (*.*)\0*.*\0\0");
374
    setuserfilterW(L"R files (*.R)\0*.R\0S files (*.q, *.ssc, *.S)\0*.q;*.ssc;*.S\0All files (*.*)\0*.*\0\0");
344
    name = askfilename("Open script", default_name); /* returns NULL if open dialog cancelled */
375
    wname = askfilenameW(G_("Open script"), default_name); /* returns NULL if open dialog cancelled */
345
    if (name) {
376
    if (wname) {
-
 
377
	wcstoutf8(name, wname, MAX_PATH);
346
	/* check if file is already open in an editor. If so, close and open again */
378
	/* check if file is already open in an editor. If so, close and open again */
347
	for (i = 0; i < neditors; ++i) {
379
	for (i = 0; i < neditors; ++i) {
348
	    t = getdata(REditors[i]);
380
	    t = getdata(REditors[i]);
349
	    p = getdata(t);
381
	    p = getdata(t);
350
	    if (!strcmp (name, p->filename)) {
382
	    if (!strcmp (name, p->filename)) {
351
		editorclose(REditors[i]);
383
		editorclose(REditors[i]);
352
		break;
384
		break;
353
	    }
385
	    }
354
	}
386
	}
-
 
387
	title = reEnc(name, CE_UTF8, CE_NATIVE, 3);
355
	Rgui_Edit(name, CE_NATIVE, name, 0);
388
	Rgui_Edit(name, CE_UTF8, title, 0);
356
    }
389
    }
357
}
390
}
358
 
391
 
359
void menueditoropen(control m)
392
void menueditoropen(control m)
360
{
393
{
Line 796... Line 829...
796
   file. If calling from fix() or edit(), then don't send events to
829
   file. If calling from fix() or edit(), then don't send events to
797
   the console until editor is closed.  */
830
   the console until editor is closed.  */
798
 
831
 
799
#include <unistd.h>
832
#include <unistd.h>
800
 
833
 
801
/* FIXME UTF-8 */
-
 
802
int Rgui_Edit(const char *filename, int enc, const char *title,
834
int Rgui_Edit(const char *filename, int enc, const char *title,
803
	      int modal)
835
	      int modal)
804
{
836
{
805
    editor c;
837
    editor c;
806
    EditorData p;
838
    EditorData p;
Line 813... Line 845...
813
    if (!c) {
845
    if (!c) {
814
	R_ShowMessage(G_("Unable to create editor window"));
846
	R_ShowMessage(G_("Unable to create editor window"));
815
	return 1;
847
	return 1;
816
    }
848
    }
817
    if (strlen(filename) > 0) {
849
    if (strlen(filename) > 0) {
818
	if (!access(filename, R_OK))
-
 
819
	    editor_load_file(c, filename, enc);
850
	editor_load_file(c, filename, enc);
820
	else
-
 
821
	    R_ShowMessage(G_("Unable to open file for reading"));
-
 
822
	editor_set_title(c, title);
851
	editor_set_title(c, title);
823
    }
852
    }
824
    else {
853
    else {
825
	editor_set_title(c, G_("Untitled"));
854
	editor_set_title(c, G_("Untitled"));
826
    }
855
    }