gchar *gp_locale_from_utf8 (const gchar *src) { gsize read, wrote; GError *err = NULL; gchar *ret; if (gretl_is_ascii(src)) { return NULL; } /* let glib figure out what the target locale is */ ret = g_locale_from_utf8(src, -1, &read, &wrote, &err); if (err) { errbox(err->message); g_error_free(err); } return ret; }
int prn_to_clipboard (PRN *prn, int fmt) { char *buf = gretl_print_steal_buffer(prn); char *modbuf = NULL; int rtf_format = 0; int err = 0; if (buf == NULL || *buf == '\0') { errbox(_("Copy buffer was empty")); return 0; } if (!OpenClipboard(NULL)) { errbox(_("Cannot open the clipboard")); return 1; } if (fmt == GRETL_FORMAT_RTF || fmt == GRETL_FORMAT_RTF_TXT) { rtf_format = 1; } EmptyClipboard(); err = maybe_post_process_buffer(buf, fmt, W_COPY, &modbuf); if (!err) { HGLOBAL winclip; LPTSTR ptr; unsigned clip_format; gunichar2 *ubuf = NULL; char *winbuf; glong wrote = 0; size_t sz; winbuf = modbuf != NULL ? modbuf : buf; if (!rtf_format && !gretl_is_ascii(winbuf)) { /* for Windows clipboard, recode UTF-8 to UTF-16 */ ubuf = g_utf8_to_utf16(winbuf, -1, NULL, &wrote, NULL); } if (ubuf != NULL) { sz = (wrote + 1) * sizeof(gunichar2); } else { sz = strlen(winbuf) + 1; } winclip = GlobalAlloc(GMEM_MOVEABLE, sz); ptr = GlobalLock(winclip); if (ubuf != NULL) { memcpy(ptr, ubuf, sz); } else { memcpy(ptr, winbuf, sz); } GlobalUnlock(winclip); if (ubuf != NULL) { clip_format = CF_UNICODETEXT; } else if (rtf_format) { clip_format = RegisterClipboardFormat("Rich Text Format"); } else if (fmt == GRETL_FORMAT_CSV) { clip_format = RegisterClipboardFormat("CSV"); } else { clip_format = CF_TEXT; } SetClipboardData(clip_format, winclip); if (ubuf != NULL) { g_free(ubuf); } } CloseClipboard(); free(buf); free(modbuf); return err; }