Skip to content

Commit

Permalink
#604: cop/save image not work under Linux (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidaobing authored Jun 23, 2024
1 parent 0b47207 commit 8afe210
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
50 changes: 32 additions & 18 deletions src/iptux/DialogBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,28 @@ void DialogBase::OnPasteClipboard(DialogBase*, GtkTextView* textview) {
}
}

gboolean DialogBase::OnImageButtonPress(DialogBase*,
GdkEventButton event,
void DialogBase::afterWindowCreated() {
g_return_if_fail(!m_imagePopupMenu);

m_imagePopupMenu = GTK_MENU(gtk_menu_new());

GtkWidget* menu_item = gtk_menu_item_new_with_label(_("Save Image"));
gtk_menu_shell_append(GTK_MENU_SHELL(m_imagePopupMenu), menu_item);
g_signal_connect_swapped(menu_item, "activate",
G_CALLBACK(DialogBase::OnSaveImage), this);

menu_item = gtk_menu_item_new_with_label(_("Copy Image"));
gtk_menu_shell_append(GTK_MENU_SHELL(m_imagePopupMenu), menu_item);
g_signal_connect_swapped(menu_item, "activate",
G_CALLBACK(DialogBase::OnCopyImage), this);

gtk_menu_attach_to_widget(m_imagePopupMenu, GTK_WIDGET(getWindow()), NULL);
}

gboolean DialogBase::OnImageButtonPress(DialogBase* self,
GdkEventButton* event,
GtkEventBox* event_box) {
if (event.type != GDK_BUTTON_PRESS || event.button != 3) {
if (event->type != GDK_BUTTON_PRESS || event->button != 3) {
return FALSE;
}

Expand All @@ -824,20 +842,10 @@ gboolean DialogBase::OnImageButtonPress(DialogBase*,
LOG_ERROR("image not found in event box.");
return FALSE;
}
self->m_activeImage = GTK_IMAGE(image);

GtkWidget* menu = gtk_menu_new();
GtkWidget* menu_item = gtk_menu_item_new_with_label(_("Save Image"));
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
g_signal_connect_swapped(menu_item, "activate",
G_CALLBACK(DialogBase::OnSaveImage), image);
menu_item = gtk_menu_item_new_with_label(_("Copy Image"));
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
g_signal_connect_swapped(menu_item, "activate",
G_CALLBACK(DialogBase::OnCopyImage), image);
gtk_widget_show_all(menu);

gtk_menu_popup_at_pointer(GTK_MENU(menu), (GdkEvent*)&event);
g_signal_connect(menu, "hide", G_CALLBACK(gtk_widget_destroy), menu);
gtk_widget_show_all(GTK_WIDGET(self->m_imagePopupMenu));
gtk_menu_popup_at_pointer(self->m_imagePopupMenu, (GdkEvent*)event);
return TRUE;
}

Expand Down Expand Up @@ -872,7 +880,10 @@ void DialogBase::OnChatHistoryInsertChildAnchor(DialogBase* self,
gtk_widget_show_all(event_box);
}

void DialogBase::OnSaveImage(GtkImage* image) {
void DialogBase::OnSaveImage(DialogBase* self) {
GtkImage* image = self->m_activeImage;
g_return_if_fail(!!image);

const char* path =
(const char*)g_object_get_data(G_OBJECT(image), kObjectKeyImagePath);
if (!path) {
Expand Down Expand Up @@ -909,7 +920,10 @@ void DialogBase::OnSaveImage(GtkImage* image) {
gtk_widget_destroy(dialog);
}

void DialogBase::OnCopyImage(GtkImage* image) {
void DialogBase::OnCopyImage(DialogBase* self) {
GtkImage* image = self->m_activeImage;
g_return_if_fail(!!image);

GdkPixbuf* pixbuf = gtk_image_get_pixbuf(image);
if (!pixbuf) {
LOG_ERROR("Failed to create pixbuf from image.");
Expand Down
12 changes: 9 additions & 3 deletions src/iptux/DialogBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class DialogBase : public SessionAbstract, public sigc::trackable {
void InitSublayerGeneral();
void ClearSublayerGeneral();

void afterWindowCreated();

void ScrollHistoryTextview();
virtual void OnNewMessageComing();
void NotifyUser();
Expand Down Expand Up @@ -83,14 +85,14 @@ class DialogBase : public SessionAbstract, public sigc::trackable {
static void RemoveSelectedEnclosure(DialogBase* self);
static void OnPasteClipboard(DialogBase* self, GtkTextView* textview);
static gboolean OnImageButtonPress(DialogBase* self,
GdkEventButton event,
GdkEventButton* event,
GtkEventBox* eventbox);
static void OnChatHistoryInsertChildAnchor(DialogBase* self,
const GtkTextIter* location,
GtkTextChildAnchor* anchor,
GtkTextBuffer* buffer);
static void OnSaveImage(GtkImage* self);
static void OnCopyImage(GtkImage* self);
static void OnSaveImage(DialogBase* self);
static void OnCopyImage(DialogBase* self);

protected:
Application* app;
Expand All @@ -109,6 +111,10 @@ class DialogBase : public SessionAbstract, public sigc::trackable {
int64_t totalsendsize; // 总计待发送大小(包括已发送)
struct timeval lasktime; // 上一次更新UI的时间
guint timersend; // 发送文件界面更新计时器ID

private:
GtkMenu* m_imagePopupMenu = 0;
GtkImage* m_activeImage = 0;
};

} // namespace iptux
Expand Down
1 change: 1 addition & 0 deletions src/iptux/DialogGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ GtkWindow* DialogGroup::CreateMainWindow() {
};
g_action_map_add_action_entries(G_ACTION_MAP(window), win_entries,
G_N_ELEMENTS(win_entries), this);
afterWindowCreated();
return GTK_WINDOW(window);
}

Expand Down
1 change: 1 addition & 0 deletions src/iptux/DialogPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ GtkWindow* DialogPeer::CreateMainWindow() {
MainWindowSignalSetup(GTK_WINDOW(window));
g_signal_connect_swapped(GTK_WIDGET(window), "show",
G_CALLBACK(ShowDialogPeer), this);
afterWindowCreated();
return GTK_WINDOW(window);
}

Expand Down

0 comments on commit 8afe210

Please sign in to comment.