Fixed some typos in comments and messages.
[AROS.git] / workbench / tools / Edit / ClipLoc.c
blob491e7dc71734b72d53188bcc8acbf93f3ba32c93
1 /**********************************************************
2 ** **
3 ** $VER: ClipLoc.c 1.2 (10 dec 1999) **
4 ** RKM Locale library and clipboard support. **
5 ** **
6 ** © T.Pierron, C.Guilaume. Free software under **
7 ** terms of GNU public license. **
8 ** **
9 **********************************************************/
11 #include <libraries/gadtools.h>
12 #include <libraries/iffparse.h>
13 #include <libraries/locale.h>
14 #include <exec/memory.h>
15 #include <exec/io.h>
16 #include "Memory.h"
17 #include "ClipLoc.h"
18 #include "Version.h"
19 #include "Cursor.h"
20 #include "Utility.h"
21 #include "ProtoTypes.h"
23 #define CATCOMP_NUMBERS /* We will need the string id */
24 #define CATCOMP_STRINGS /* and the english string corresponding to the id */
25 #include "strings.h"
27 #define ID_FTXT MAKE_ID('F','T','X','T')
28 #define ID_CHRS MAKE_ID('C','H','R','S')
30 /** Main clipboard handle manage through iffparse.library **/
31 struct IFFHandle * clip = NULL;
33 /** All messages of JanoEditor **/
34 STRPTR JanoMessages[] = {
36 /* Global error messages */
37 ERR_BADOS_STR, ERR_NOASLREQ_STR,
38 ERR_NOMEM_STR, ERR_NOGUI_STR,
39 ERR_NONAME_STR, ERR_WRITECLIP_STR,
40 ERR_OPENCLIP_STR, ERR_LOADFILE_STR,
41 ERR_NOTXTINCLIP_STR, ERR_WRONG_TYPE_STR,
42 ERR_READCLIP_STR, ERR_NOBRACKET_STR,
43 ERR_NOT_FOUND_STR, ERR_LOADFONT_STR,
44 ERR_NOPREFEDITOR_STR, ERR_BADPREFSFILE_STR,
45 ERR_FILEMODIFIED_STR, ERR_SLC_STR,
46 ERR_NOSEARCHTEXT_STR, ERR_FILEEXISTS_STR,
47 ERR_OC_STR, WARN_RECORD_STR,
48 WARN_RECORDED_STR, WARN_REC_STR,
50 /* About messages */
51 MSG_ABOUT_STR, MSG_FORMATINFO_STR,
52 MSG_CONTINUE_STR, MSG_BYTE_STR,
53 MSG_BYTES_STR, MSG_LINE_STR,
54 MSG_LINES_STR,
56 /* Search window messages */
57 MSG_SEARCHWINDOW_STR, MSG_REPLACEWINDOW_STR, MSG_SEARCHSTRING_STR,
58 MSG_REPLACESTRING_STR, MSG_OPTCASE_STR, MSG_OPTWORDS_STR,
59 MSG_BUTTONREPLACE_STR, MSG_BUTTONREPALL_STR, MSG_BUTTONSEARCH_STR,
60 MSG_NEXTSEARCH_STR, MSG_USESEARCH_STR, MSG_CANCELSEARCH_STR, NULL,
61 MSG_REPLACEALL_STR, MSG_OCCURENCY_STR, MSG_OCCURENCIES_STR
64 /*** Open the clipboard using iffparse library***/
65 static BOOL CBOpen(ULONG unit)
67 extern struct Library * IFFParseBase;
69 if( IFFParseBase != NULL && (clip = (APTR) AllocIFF() ) )
71 if( (clip->iff_Stream = (IPTR) OpenClipboard(unit)) )
73 InitIFFasClip(clip);
75 return TRUE;
77 FreeIFF( clip );
79 ThrowError(Wnd, ErrMsg(ERR_OPENCLIP));
80 return FALSE;
83 /*** Close clipboard ***/
84 void CBClose( void )
86 if( clip )
88 CloseClipboard( (APTR) clip->iff_Stream );
89 FreeIFF(clip);
93 /*** Write strings to the clipboard.device ***/
94 BOOL CBWriteFTXT( LINE *stsel, struct cutcopypast *ccp )
96 LINE *ln;
97 long length;
99 /* If clipboard not already allocated, makes it now */
100 if( clip == NULL && !CBOpen(STD_CLIP_UNIT) ) return FALSE;
102 /* Compute how many chars are selected */
103 for(length=0, ln=stsel; ln && ln->flags; ln=ln->next)
105 length += ln->size;
106 /* In block selection mode, always add a newline */
107 if(ccp->select == COLUMN_TYPE) length++;
108 if(ln->flags & FIRSTSEL) length -= find_nbc(ln, ccp->startsel);
109 if(ln->flags & LASTSEL) length += find_nbc(ln, ccp->endsel)-ln->size;
110 else length++; /* New line */
112 if(length == 0) return FALSE;
114 if( !OpenIFF(clip, IFFF_WRITE) )
116 /* Let iffparse manage main chunk size */
117 if( !PushChunk(clip, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN) )
119 if( !PushChunk(clip, ID_FTXT, ID_CHRS, length) )
121 /* In columnar selection, use '\r' as eol */
122 char eol = (ccp->select == COLUMN_TYPE ? '\r':'\n'), add_eol;
124 /* Write the content of strings */
125 for(ln = stsel; ln && ln->flags; ln = ln->next)
127 STRPTR stream;
128 ULONG length;
129 stream = (STRPTR)ln->stream;
130 length = ln->size;
131 if(ln->flags & FIRSTSEL) {
132 register long rc = find_nbc(ln, ccp->startsel);
133 length -= rc; stream += rc;
135 if(ln->flags & LASTSEL) {
136 length += find_nbc(ln, ccp->endsel) - ln->size;
137 /* Add automatically a newline */
138 add_eol = (ccp->select == COLUMN_TYPE);
140 else add_eol = 1;
142 WriteChunkBytes(clip, stream, length);
143 if(add_eol)
144 WriteChunkBytes(clip, &eol, 1);
146 PopChunk(clip);
148 else ThrowError(Wnd, ErrMsg(ERR_NOMEM));
149 PopChunk(clip);
151 else ThrowError(Wnd, ErrMsg(ERR_NOMEM));
152 CloseIFF(clip);
154 return TRUE;
157 /*** Reads the next CHRS chunk from clipboard ***/
158 BOOL CBReadCHRS( void *jbuf, LINE *st, ULONG pos, LONG *nbl )
160 struct ContextNode * cn;
161 BOOL ret = FALSE;
163 /* If clipboard not already allocated, makes it now */
164 if( clip == NULL && !CBOpen(STD_CLIP_UNIT) ) return FALSE;
166 if( !OpenIFF(clip, IFFF_READ) )
168 if( !StopChunk(clip, ID_FTXT, ID_CHRS) )
170 if( !ParseIFF(clip, IFFPARSE_SCAN) )
172 cn = CurrentChunk(clip);
173 if( cn->cn_Type == ID_FTXT && cn->cn_ID == ID_CHRS && cn->cn_Size > 0 )
175 STRPTR buf;
176 ULONG size = cn->cn_Size;
178 if( (buf = (STRPTR) AllocVec(size, MEMF_PUBLIC)) )
180 UBYTE eol;
181 ReadChunkBytes(clip, buf, size);
183 /* What's kind of paste method shall we used? */
184 { register STRPTR s; register ULONG n;
185 for(s=buf,n=size; --n && *s!='\n' && *s!='\r'; s++);
186 eol = *s;
188 /* Add string to the buffer */
189 reg_group_by(jbuf);
190 if(eol == '\n') add_string(jbuf,st,pos,buf,size,nbl) ;
191 else add_block (jbuf,st,pos,buf,size,nbl) ;
192 reg_group_by(jbuf);
193 FreeVec(buf);
194 ret = TRUE;
196 else ThrowError(Wnd, ErrMsg(ERR_NOMEM));
198 else ThrowError(Wnd, ErrMsg(ERR_NOTXTINCLIP));
200 else ThrowError(Wnd, ErrMsg(ERR_NOTXTINCLIP));
202 else ThrowError(Wnd, ErrMsg(ERR_NOMEM));
203 CloseIFF(clip);
205 /* ThrowError(Wnd, ErrMsg(ERR_READCLIP)); */
206 return ret;
209 /************************************************
210 **** locale.library support by C.Guillaume. ****
211 ************************************************/
213 static APTR catalog = NULL;
215 /*** Localise all strings of the program ***/
216 void InitLocale(void)
218 if( (catalog = (APTR) OpenCatalogA(NULL, "System/Tools/Editor.catalog", NULL)) )
220 WORD n;
221 /* Translate menu strings */
222 { register struct NewMenu * nm;
223 register STRPTR str;
224 extern struct NewMenu newmenu[];
226 for(nm = newmenu, n = MSG_PROJECTTITLE; nm->nm_Type != NM_END; nm++)
228 if (nm->nm_Label != NM_BARLABEL)
230 str = (STRPTR) GetCatalogStr( catalog, n++, nm->nm_Label );
231 /* Change shortcut to user prefered one */
232 if (*str == 127) {
233 nm->nm_CommKey = str+1; str += 2;
234 nm->nm_Flags &= ~NM_COMMANDSTRING;
236 nm->nm_Label = str;
240 /* Translate all other messages */
241 { register STRPTR * str;
242 for(n = 0, str = JanoMessages; n < sizeof(JanoMessages)/sizeof(STRPTR); n++, str++)
243 if (*str != NULL)
244 *str = (STRPTR) GetCatalogStr( catalog, n + ERR_BADOS, *str );
249 /*** Free allocated ressource ***/
250 void CleanupLocale(void)
252 if (catalog != NULL) CloseCatalog(catalog);