示例#1
0
void gui_paste_from_clipboard(struct gui_window *g, int x, int y)
{
	/* This and the other clipboard code is heavily based on the RKRM examples */
	struct ContextNode *cn;
	ULONG rlen=0,error;
	struct CSet cset;
	LONG codeset = 0;
	char *clip;
	STRPTR readbuf = AllocVec(1024,MEMF_PRIVATE | MEMF_CLEAR);

	cset.CodeSet = 0;

	if(OpenIFF(iffh,IFFF_READ)) return;
	if(StopChunk(iffh,ID_FTXT,ID_CHRS)) return;
	if(StopChunk(iffh,ID_FTXT,ID_CSET)) return;

	while(1)
	{
		error = ParseIFF(iffh,IFFPARSE_SCAN);
		if(error == IFFERR_EOC) continue;
		else if(error) break;

		cn = CurrentChunk(iffh);

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CSET))
		{
			rlen = ReadChunkBytes(iffh,&cset,32);
			if(cset.CodeSet == 1) codeset = 106;
				else codeset = cset.CodeSet;
		}

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CHRS))
		{
			while((rlen = ReadChunkBytes(iffh,readbuf,1024)) > 0)
			{
				if(codeset == 0)
				{
					utf8_from_local_encoding(readbuf,rlen,&clip);
				}
				else
				{
					utf8_from_enc(readbuf,
						(const char *)ObtainCharsetInfo(DFCS_NUMBER,
										codeset, DFCS_MIMENAME),
						rlen, &clip);
				}

				browser_window_paste_text(g->shared->bw,clip,rlen,true);
			}
			if(rlen < 0) error = rlen;
		}
	}
	CloseIFF(iffh);
}
示例#2
0
char *ami_to_utf8_easy(const char *string)
{
	char *localtext;

	if(utf8_from_local_encoding(string,strlen(string),&localtext) == NSERROR_OK)
	{
		return localtext;
	}
	else
	{
		return strdup(string);
	}
}
示例#3
0
文件: clipboard.c 项目: ysei/NetSurf
char *ami_clipboard_cat_collection(struct CollectionItem *ci, LONG codeset, size_t *text_length)
{
	struct CollectionItem *ci_new = NULL, *ci_next, *ci_curr = ci;
	size_t len = 0;
	char *text = NULL, *p;

	/* Scan the collected chunks to find out the total size.
	 * If they are not in UTF-8, convert the chunks first and create a new CollectionItem list.
	 */
	do {
		switch(codeset) {
			case 106:
				len += ci_curr->ci_Size;
			break;
			
			case 0:
				if(ci_new) {
					ci_next->ci_Next = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_next->ci_Next;
				} else {
					ci_new = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_new;
				}
				
				utf8_from_local_encoding(ci_curr->ci_Data, ci_curr->ci_Size, (char **)&ci_next->ci_Data);
				ci_next->ci_Size = strlen(ci_next->ci_Data);
				len += ci_next->ci_Size;
			break;

			default:
				if(ci_new) {
					ci_next->ci_Next = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_next->ci_Next;
				} else {
					ci_new = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_new;
				}
				
				utf8_from_enc(ci_curr->ci_Data,
						(const char *)ObtainCharsetInfo(DFCS_NUMBER,
										codeset, DFCS_MIMENAME),
					      ci_curr->ci_Size, (char **)&ci_next->ci_Data, NULL);
				ci_next->ci_Size = strlen(ci_next->ci_Data);
				len += ci_next->ci_Size;
			break;
		}
	} while (ci_curr = ci_curr->ci_Next);

	text = malloc(len);

	if(text == NULL) return NULL;

	/* p points to the end of the buffer. This is because the chunks are
	 * in the list in reverse order. */
	p = text + len;

	if(ci_new) {
		ci_curr = ci_new;
	} else {
		ci_curr = ci;
	}

	do {
		p -= ci_curr->ci_Size;
		memcpy(p, ci_curr->ci_Data, ci_curr->ci_Size);
		ci_next = ci_curr->ci_Next;
		
		if(ci_new) {
			free(ci_curr->ci_Data);
			FreeVec(ci_curr);
		}
	} while (ci_curr = ci_next);

	*text_length = len;
	return text;
}