Skip to content

Commit

Permalink
#605: Ctrl-V should not paste text twice (#606)
Browse files Browse the repository at this point in the history
* fix bug

* add one more test
  • Loading branch information
lidaobing authored Jun 23, 2024
1 parent 8653a31 commit 0b47207
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/iptux/DialogBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,13 +803,7 @@ void DialogBase::OnPasteClipboard(DialogBase*, GtkTextView* textview) {
buffer = gtk_text_view_get_buffer(textview);
gtk_text_buffer_get_iter_at_mark(buffer, &iter,
gtk_text_buffer_get_insert(buffer));
if (gtk_clipboard_wait_is_text_available(clipboard)) {
gchar* text = gtk_clipboard_wait_for_text(clipboard);
if (text) {
gtk_text_buffer_insert(buffer, &iter, text, -1);
g_free(text);
}
} else if (gtk_clipboard_wait_is_image_available(clipboard)) {
if (gtk_clipboard_wait_is_image_available(clipboard)) {
GdkPixbuf* pixbuf = gtk_clipboard_wait_for_image(clipboard);
if (pixbuf) {
gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
Expand Down
2 changes: 1 addition & 1 deletion src/iptux/DialogPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void DialogPeer::onRequestSharedResources(void*, void*, DialogPeer& self) {

void DialogPeer::onPaste(void*, void*, DialogPeer* self) {
GtkTextView* textview = GTK_TEXT_VIEW(self->inputTextviewWidget);
DialogBase::OnPasteClipboard(self, textview);
g_signal_emit_by_name(textview, "paste-clipboard");
}

void DialogPeer::insertPicture() {
Expand Down
3 changes: 3 additions & 0 deletions src/iptux/DialogPeerTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Application.h"
#include "UiHelper.h"
#include "gtest/gtest.h"

#include "iptux-utils/TestHelper.h"
Expand All @@ -23,10 +24,12 @@ TEST(DialogPeer, Constructor) {
GroupInfo* grpinf = app->getCoreThread()->GetPalRegularItem(pal.get());
grpinf->buffer = gtk_text_buffer_new(NULL);
DialogPeer* dlgpr = new DialogPeer(app, grpinf);
ASSERT_EQ(igtk_text_buffer_get_text(grpinf->getInputBuffer()), "");

auto clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_text(clipboard, "hello world", -1);
do_action(dlgpr, "paste");
ASSERT_EQ(igtk_text_buffer_get_text(grpinf->getInputBuffer()), "hello world");

GError* error = NULL;
auto pixbuf =
Expand Down
9 changes: 9 additions & 0 deletions src/iptux/UiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,13 @@ GtkImage* igtk_image_new_with_size(const char* filename,
return GTK_IMAGE(gtk_image_new_from_pixbuf(pixbuf));
}

string igtk_text_buffer_get_text(GtkTextBuffer* buffer) {
GtkTextIter start, end;
gtk_text_buffer_get_bounds(buffer, &start, &end);
char* res1 = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
string res(res1);
g_free(res1);
return res;
}

} // namespace iptux
1 change: 1 addition & 0 deletions src/iptux/UiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ GSList* selection_data_get_path(GtkSelectionData* data);
* @return GtkImage* null if failed
*/
GtkImage* igtk_image_new_with_size(const char* filename, int width, int height);
std::string igtk_text_buffer_get_text(GtkTextBuffer* buffer);

/**
* @brief only used for test, after call this, pop_info, pop_warning,
Expand Down

0 comments on commit 0b47207

Please sign in to comment.