void format_send_to_gui(TEXT_DEST_REC *dest, const char *text)
{
	THEME_REC *theme;
	char *dup, *str, *ptr, type;
	int fgcolor, bgcolor;
	int flags;

	theme = window_get_theme(dest->window);

	dup = str = g_strdup(text);

	flags = 0; fgcolor = theme->default_color; bgcolor = -1;
	while (*str != '\0') {
		type = '\0';
		for (ptr = str; *ptr != '\0'; ptr++) {
			if (IS_COLOR_CODE(*ptr) || *ptr == '\n') {
				type = *ptr;
				*ptr++ = '\0';
				break;
			}
		}

		if (type == 7) {
			/* bell */
			if (settings_get_bool("bell_beeps"))
				signal_emit("beep", 0);
		} else if (type == 4 && *ptr == FORMAT_STYLE_CLRTOEOL) {
			/* clear to end of line */
			flags |= GUI_PRINT_FLAG_CLRTOEOL;
		}

		if (*str != '\0' || (flags & GUI_PRINT_FLAG_CLRTOEOL)) {
			/* send the text to gui handler */
			signal_emit_id(signal_gui_print_text, 6, dest->window,
				       GINT_TO_POINTER(fgcolor),
				       GINT_TO_POINTER(bgcolor),
				       GINT_TO_POINTER(flags), str,
				       dest);
			flags &= ~(GUI_PRINT_FLAG_INDENT|GUI_PRINT_FLAG_CLRTOEOL);
		}

		if (type == '\n') {
			format_newline(dest->window);
			fgcolor = theme->default_color;
			bgcolor = -1;
			flags &= GUI_PRINT_FLAG_INDENT|GUI_PRINT_FLAG_MONOSPACE;
		}

		if (*ptr == '\0')
			break;

		switch (type)
		{
		case 2:
			/* bold */
			if (!hide_text_style)
				flags ^= GUI_PRINT_FLAG_BOLD;
			break;
		case 3:
			/* MIRC color */
			get_mirc_color((const char **) &ptr,
					hide_colors ? NULL : &fgcolor,
					hide_colors ? NULL : &bgcolor);
			if (!hide_colors)
				flags |= GUI_PRINT_FLAG_MIRC_COLOR;
			break;
		case 4:
			/* user specific colors */
			flags &= ~GUI_PRINT_FLAG_MIRC_COLOR;
			switch (*ptr) {
			case FORMAT_STYLE_BLINK:
				flags ^= GUI_PRINT_FLAG_BLINK;
				break;
			case FORMAT_STYLE_UNDERLINE:
				flags ^= GUI_PRINT_FLAG_UNDERLINE;
				break;
			case FORMAT_STYLE_BOLD:
				flags ^= GUI_PRINT_FLAG_BOLD;
				break;
			case FORMAT_STYLE_REVERSE:
				flags ^= GUI_PRINT_FLAG_REVERSE;
				break;
			case FORMAT_STYLE_ITALIC:
				flags ^= GUI_PRINT_FLAG_ITALIC;
				break;
			case FORMAT_STYLE_MONOSPACE:
				flags ^= GUI_PRINT_FLAG_MONOSPACE;
				break;
			case FORMAT_STYLE_INDENT:
				flags |= GUI_PRINT_FLAG_INDENT;
				break;
			case FORMAT_STYLE_DEFAULTS:
				fgcolor = theme->default_color;
				bgcolor = -1;
				flags &= GUI_PRINT_FLAG_INDENT|GUI_PRINT_FLAG_MONOSPACE;
				break;
			case FORMAT_STYLE_CLRTOEOL:
				break;
			case FORMAT_COLOR_EXT1:
				fgcolor = 0x10 + *++ptr - FORMAT_COLOR_NOCHANGE;
				flags &= ~GUI_PRINT_FLAG_COLOR_24_FG;
				break;
			case FORMAT_COLOR_EXT1_BG:
				bgcolor = 0x10 + *++ptr - FORMAT_COLOR_NOCHANGE;
				flags &= ~GUI_PRINT_FLAG_COLOR_24_BG;
				break;
			case FORMAT_COLOR_EXT2:
				fgcolor = 0x60 + *++ptr - FORMAT_COLOR_NOCHANGE;
				flags &= ~GUI_PRINT_FLAG_COLOR_24_FG;
				break;
			case FORMAT_COLOR_EXT2_BG:
				bgcolor = 0x60 + *++ptr - FORMAT_COLOR_NOCHANGE;
				flags &= ~GUI_PRINT_FLAG_COLOR_24_BG;
				break;
			case FORMAT_COLOR_EXT3:
				fgcolor = 0xb0 + *++ptr - FORMAT_COLOR_NOCHANGE;
				flags &= ~GUI_PRINT_FLAG_COLOR_24_FG;
				break;
			case FORMAT_COLOR_EXT3_BG:
				bgcolor = 0xb0 + *++ptr - FORMAT_COLOR_NOCHANGE;
				flags &= ~GUI_PRINT_FLAG_COLOR_24_BG;
				break;
#ifdef TERM_TRUECOLOR
			case FORMAT_COLOR_24:
				unformat_24bit_color(&ptr, 1, &fgcolor, &bgcolor, &flags);
				break;
#endif
			default:
				if (*ptr != FORMAT_COLOR_NOCHANGE) {
					flags &= ~GUI_PRINT_FLAG_COLOR_24_FG;
					fgcolor = *ptr==(char)0xff ? -1 : (unsigned char) *ptr-'0';
				}
				if (ptr[1] == '\0')
					break;

				ptr++;
				if (*ptr != FORMAT_COLOR_NOCHANGE) {
					flags &= ~GUI_PRINT_FLAG_COLOR_24_BG;
					bgcolor = *ptr==(char)0xff ? -1 : *ptr-'0';
				}
			}
			if (*ptr == '\0')
				break;

			ptr++;
			break;
		case 6:
			/* blink */
			if (!hide_text_style)
				flags ^= GUI_PRINT_FLAG_BLINK;
			break;
		case 15:
			/* remove all styling */
			fgcolor = theme->default_color;
			bgcolor = -1;
			flags &= GUI_PRINT_FLAG_INDENT|GUI_PRINT_FLAG_MONOSPACE;
			break;
		case 22:
			/* reverse */
			if (!hide_text_style)
				flags ^= GUI_PRINT_FLAG_REVERSE;
			break;
		case 29:
			/* italic */
			if (!hide_text_style)
				flags ^= GUI_PRINT_FLAG_ITALIC;
			break;
		case 31:
			/* underline */
			if (!hide_text_style)
				flags ^= GUI_PRINT_FLAG_UNDERLINE;
			break;
		case 27:
			/* ansi color code */
			ptr = (char *)
				get_ansi_color(theme, ptr,
					       hide_colors ? NULL : &fgcolor,
					       hide_colors ? NULL : &bgcolor,
					       hide_colors ? NULL : &flags);
			break;
		}

		str = ptr;
	}

	g_free(dup);
}