gaim/gaim

Parents 3ace3198893b
Children 8384ff051fa1
This fixes losing the HTML clipboard contents when the window is closed. Also make sure that the various clipboard initialization functions succeed before doing things that depend on them.
  • +79 -59
    src/gtkimhtml.c
  • --- a/src/gtkimhtml.c Thu May 05 09:08:38 2005 -0400
    +++ b/src/gtkimhtml.c Thu May 05 14:03:43 2005 -0400
    @@ -41,6 +41,7 @@
    #include <locale.h>
    #endif
    #ifdef _WIN32
    +#include <gdk/gdkwin32.h>
    #include <windows.h>
    #endif
    @@ -90,6 +91,7 @@
    static void hijack_menu_cb(GtkIMHtml *imhtml, GtkMenu *menu, gpointer data);
    static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, gpointer data);
    static void paste_plaintext_received_cb (GtkClipboard *clipboard, const gchar *text, gpointer data);
    +static void imhtml_paste_insert(GtkIMHtml *imhtml, const char *text, gboolean plaintext);
    /* POINT_SIZE converts from AIM font sizes to a point size scale factor. */
    #define MAX_FONT_SIZE 7
    @@ -215,6 +217,80 @@
    return ret;
    }
    +
    +static void clipboard_copy_html_win32(GtkIMHtml *imhtml) {
    + gchar *clipboard = clipboard_html_to_win32(imhtml->clipboard_html_string);
    + if (clipboard != NULL) {
    + HWND hwnd = GDK_WINDOW_HWND(GTK_WIDGET(imhtml)->window);
    + if (OpenClipboard(hwnd)) {
    + if (EmptyClipboard()) {
    + gint length = strlen(clipboard);
    + HGLOBAL hdata = GlobalAlloc(GMEM_MOVEABLE, length);
    + if (hdata != NULL) {
    + gchar *buffer = GlobalLock(hdata);
    + memcpy(buffer, clipboard, length);
    + GlobalUnlock(hdata);
    +
    + if (SetClipboardData(win_html_fmt, hdata) == NULL) {
    + gchar *err_msg =
    + g_win32_error_message(GetLastError());
    + gaim_debug_info("html clipboard",
    + "Unable to set clipboard data: %s\n",
    + err_msg ? err_msg : "Unknown Error");
    + g_free(err_msg);
    + }
    + }
    + }
    + CloseClipboard();
    + }
    + g_free(clipboard);
    + }
    +}
    +
    +static gboolean clipboard_paste_html_win32(GtkIMHtml *imhtml) {
    + gboolean pasted = FALSE;
    +
    + if (gtk_text_view_get_editable(GTK_TEXT_VIEW(imhtml))
    + && IsClipboardFormatAvailable(win_html_fmt)) {
    + gboolean error_reading_clipboard = FALSE;
    + HWND hwnd = GDK_WINDOW_HWND(GTK_WIDGET(imhtml)->window);
    +
    + if (OpenClipboard(hwnd)) {
    + HGLOBAL hdata = GetClipboardData(win_html_fmt);
    + if (hdata == NULL) {
    + error_reading_clipboard = TRUE;
    + } else {
    + char *buffer = GlobalLock(hdata);
    + if (buffer == NULL) {
    + error_reading_clipboard = TRUE;
    + } else {
    + char *text = clipboard_win32_to_html(
    + buffer);
    + imhtml_paste_insert(imhtml, text,
    + FALSE);
    + g_free(text);
    + pasted = TRUE;
    + }
    + GlobalUnlock(hdata);
    + }
    +
    + CloseClipboard();
    +
    + } else {
    + error_reading_clipboard = TRUE;
    + }
    +
    + if (error_reading_clipboard) {
    + gchar *err_msg = g_win32_error_message(GetLastError());
    + gaim_debug_info("html clipboard",
    + "Unable to read clipboard data: %s\n",
    + err_msg ? err_msg : "Unknown Error");
    + g_free(err_msg);
    + }
    + }
    +
    + return pasted;
    +}
    #endif
    static GtkSmileyTree*
    @@ -691,20 +767,7 @@
    #ifdef _WIN32
    /* We're going to still copy plain text, but let's toss the "HTML Format"
    we need into the windows clipboard now as well. */
    - HGLOBAL hdata;
    - gchar *clipboard = clipboard_html_to_win32(imhtml->clipboard_html_string);
    - gchar *buffer;
    - gint length = strlen(clipboard);
    - if(clipboard != NULL) {
    - OpenClipboard(NULL);
    - hdata = GlobalAlloc(GMEM_MOVEABLE, length);
    - buffer = GlobalLock(hdata);
    - memcpy(buffer, clipboard, length);
    - GlobalUnlock(hdata);
    - SetClipboardData(win_html_fmt, hdata);
    - CloseClipboard();
    - g_free(clipboard);
    - }
    + clipboard_copy_html_win32(imhtml);
    #endif
    g_signal_stop_emission_by_name(imhtml, "copy-clipboard");
    @@ -735,20 +798,7 @@
    #ifdef _WIN32
    /* We're going to still copy plain text, but let's toss the "HTML Format"
    we need into the windows clipboard now as well. */
    - HGLOBAL hdata;
    - gchar *clipboard = clipboard_html_to_win32(imhtml->clipboard_html_string);
    - gchar *buffer;
    - gint length = strlen(clipboard);
    - if(clipboard != NULL) {
    - OpenClipboard(NULL);
    - hdata = GlobalAlloc(GMEM_MOVEABLE, length);
    - buffer = GlobalLock(hdata);
    - memcpy(buffer, clipboard, length);
    - GlobalUnlock(hdata);
    - SetClipboardData(win_html_fmt, hdata);
    - CloseClipboard();
    - g_free(clipboard);
    - }
    + clipboard_copy_html_win32(imhtml);
    #endif
    if (imhtml->editable)
    @@ -851,37 +901,7 @@
    #ifdef _WIN32
    /* If we're on windows, let's see if we can get data from the HTML Format
    clipboard before we try to paste from the GTK buffer */
    - HGLOBAL hdata;
    - DWORD err;
    - char *buffer;
    - char *text;
    -
    - if (!gtk_text_view_get_editable(GTK_TEXT_VIEW(imhtml)))
    - return;
    -
    - if (IsClipboardFormatAvailable(win_html_fmt)) {
    - OpenClipboard(NULL);
    - hdata = GetClipboardData(win_html_fmt);
    - if (hdata == NULL) {
    - err = GetLastError();
    - gaim_debug_info("html clipboard", "error number %u! See http://msdn.microsoft.com/library/en-us/debug/base/system_error_codes.asp\n", err);
    - CloseClipboard();
    - return;
    - }
    - buffer = GlobalLock(hdata);
    - if (buffer == NULL) {
    - err = GetLastError();
    - gaim_debug_info("html clipboard", "error number %u! See http://msdn.microsoft.com/library/en-us/debug/base/system_error_codes.asp\n", err);
    - CloseClipboard();
    - return;
    - }
    - text = clipboard_win32_to_html(buffer);
    - GlobalUnlock(hdata);
    - CloseClipboard();
    -
    - imhtml_paste_insert(imhtml, text, FALSE);
    - g_free(text);
    - } else {
    + if (!clipboard_paste_html_win32(imhtml)) {
    #endif
    GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD);
    gtk_clipboard_request_contents(clipboard, gdk_atom_intern("text/html", FALSE),