Mercurial > pidgin
comparison src/gtkimhtml.c @ 8681:e2e56231023c
[gaim-migrate @ 9434]
I think this just about perfects rich-text copy/paste. Tim's done a real good
job with gtkimhtml.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sat, 17 Apr 2004 17:39:15 +0000 |
parents | cc2ce209cc46 |
children | 140b0d020c43 |
comparison
equal
deleted
inserted
replaced
8680:f62ecbe87e92 | 8681:e2e56231023c |
---|---|
487 return FALSE; | 487 return FALSE; |
488 } | 488 } |
489 | 489 |
490 #if GTK_CHECK_VERSION(2,2,0) | 490 #if GTK_CHECK_VERSION(2,2,0) |
491 static void gtk_imhtml_clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, GtkIMHtml *imhtml) { | 491 static void gtk_imhtml_clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, GtkIMHtml *imhtml) { |
492 char *text; | |
492 GtkTextIter start, end; | 493 GtkTextIter start, end; |
493 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | 494 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); |
494 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | 495 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); |
495 char *text; | 496 |
496 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); | 497 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); |
497 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | 498 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); |
498 | 499 gboolean primary = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY) == clipboard; |
499 | 500 |
500 if (info == TARGET_HTML) { | 501 if (info == TARGET_HTML) { |
501 int len; | 502 int len; |
502 char *selection; | 503 char *selection; |
503 GString *str = g_string_new(NULL); | 504 GString *str = g_string_new(NULL); |
504 text = gtk_imhtml_get_markup_range(imhtml, &start, &end); | 505 if (primary) { |
506 text = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
507 } else | |
508 text = imhtml->clipboard_html_string; | |
505 | 509 |
506 /* Mozilla asks that we start our text/html with the Unicode byte order mark */ | 510 /* Mozilla asks that we start our text/html with the Unicode byte order mark */ |
507 str = g_string_append_unichar(str, 0xfeff); | 511 str = g_string_append_unichar(str, 0xfeff); |
508 str = g_string_append(str, text); | 512 str = g_string_append(str, text); |
509 str = g_string_append_unichar(str, 0x0000); | 513 str = g_string_append_unichar(str, 0x0000); |
510 selection = g_convert(str->str, str->len, "UCS-2", "UTF-8", NULL, &len, NULL); | 514 selection = g_convert(str->str, str->len, "UCS-2", "UTF-8", NULL, &len, NULL); |
511 gtk_selection_data_set (selection_data, gdk_atom_intern("text/html", FALSE), 16, selection, len); | 515 gtk_selection_data_set (selection_data, gdk_atom_intern("text/html", FALSE), 16, selection, len); |
512 g_string_free(str, TRUE); | 516 g_string_free(str, TRUE); |
513 g_free(selection); | 517 g_free(selection); |
514 } else { | 518 } else { |
515 text = gtk_imhtml_get_text(imhtml, &start, &end); | 519 if (primary) { |
520 text = gtk_imhtml_get_text(imhtml, &start, &end); | |
521 } else | |
522 text = imhtml->clipboard_text_string; | |
516 gtk_selection_data_set_text(selection_data, text, strlen(text)); | 523 gtk_selection_data_set_text(selection_data, text, strlen(text)); |
517 } | 524 } |
518 g_free(text); | 525 if (primary) /* This was allocated here */ |
519 } | 526 g_free(text); |
527 } | |
520 | 528 |
521 static void gtk_imhtml_primary_clipboard_clear(GtkClipboard *clipboard, GtkIMHtml *imhtml) | 529 static void gtk_imhtml_primary_clipboard_clear(GtkClipboard *clipboard, GtkIMHtml *imhtml) |
522 { | 530 { |
523 GtkTextIter insert; | 531 GtkTextIter insert; |
524 GtkTextIter selection_bound; | 532 GtkTextIter selection_bound; |
534 &insert); | 542 &insert); |
535 } | 543 } |
536 | 544 |
537 static void copy_clipboard_cb(GtkIMHtml *imhtml, gpointer unused) | 545 static void copy_clipboard_cb(GtkIMHtml *imhtml, gpointer unused) |
538 { | 546 { |
547 GtkTextIter start, end; | |
548 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | |
549 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
550 | |
551 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); | |
552 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
553 | |
539 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD), | 554 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD), |
540 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | 555 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), |
541 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | 556 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, |
542 (GtkClipboardClearFunc)NULL, G_OBJECT(imhtml)); | 557 (GtkClipboardClearFunc)NULL, G_OBJECT(imhtml)); |
558 | |
559 if (imhtml->clipboard_html_string) { | |
560 g_free(imhtml->clipboard_html_string); | |
561 g_free(imhtml->clipboard_text_string); | |
562 } | |
563 | |
564 imhtml->clipboard_html_string = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
565 imhtml->clipboard_text_string = gtk_imhtml_get_text(imhtml, &start, &end); | |
543 | 566 |
544 g_signal_stop_emission_by_name(imhtml, "copy-clipboard"); | 567 g_signal_stop_emission_by_name(imhtml, "copy-clipboard"); |
545 } | 568 } |
546 | 569 |
547 static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, gpointer data) | 570 static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, gpointer data) |
575 if (!imhtml->wbfo) | 598 if (!imhtml->wbfo) |
576 gtk_imhtml_close_tags(imhtml); | 599 gtk_imhtml_close_tags(imhtml); |
577 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, gtk_text_buffer_get_insert(imhtml->text_buffer)); | 600 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, gtk_text_buffer_get_insert(imhtml->text_buffer)); |
578 gtk_imhtml_insert_html_at_iter(imhtml, text, GTK_IMHTML_NO_NEWLINE, &iter); | 601 gtk_imhtml_insert_html_at_iter(imhtml, text, GTK_IMHTML_NO_NEWLINE, &iter); |
579 gtk_text_buffer_move_mark_by_name(imhtml->text_buffer, "insert", &iter); | 602 gtk_text_buffer_move_mark_by_name(imhtml->text_buffer, "insert", &iter); |
603 | |
604 g_free(text); | |
605 | |
580 } | 606 } |
581 | 607 |
582 | 608 |
583 static void paste_clipboard_cb(GtkIMHtml *imhtml, gpointer blah) | 609 static void paste_clipboard_cb(GtkIMHtml *imhtml, gpointer blah) |
584 { | 610 { |
660 | 686 |
661 for(scalables = imhtml->scalables; scalables; scalables = scalables->next) { | 687 for(scalables = imhtml->scalables; scalables; scalables = scalables->next) { |
662 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(scalables->data); | 688 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(scalables->data); |
663 scale->free(scale); | 689 scale->free(scale); |
664 } | 690 } |
691 | |
692 if (imhtml->clipboard_text_string) { | |
693 g_free(imhtml->clipboard_text_string); | |
694 g_free(imhtml->clipboard_html_string); | |
695 } | |
696 | |
665 | 697 |
666 g_list_free(imhtml->scalables); | 698 g_list_free(imhtml->scalables); |
667 G_OBJECT_CLASS(parent_class)->finalize (object); | 699 G_OBJECT_CLASS(parent_class)->finalize (object); |
668 } | 700 } |
669 | 701 |
780 | 812 |
781 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", | 813 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", |
782 G_CALLBACK(mark_set_so_update_selection_cb), imhtml); | 814 G_CALLBACK(mark_set_so_update_selection_cb), imhtml); |
783 | 815 |
784 gtk_widget_add_events(GTK_WIDGET(imhtml), GDK_LEAVE_NOTIFY_MASK); | 816 gtk_widget_add_events(GTK_WIDGET(imhtml), GDK_LEAVE_NOTIFY_MASK); |
817 | |
818 imhtml->clipboard_text_string = NULL; | |
819 imhtml->clipboard_html_string = NULL; | |
785 | 820 |
786 imhtml->tip = NULL; | 821 imhtml->tip = NULL; |
787 imhtml->tip_timer = 0; | 822 imhtml->tip_timer = 0; |
788 imhtml->tip_window = NULL; | 823 imhtml->tip_window = NULL; |
789 | 824 |