1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2004 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
15 GtkWidget
*url
, *get
, *width
, *height
;
19 url_changed_cb(ImageDlg
*idlg
) {
20 const char *text
= gtk_entry_get_text(GTK_ENTRY(idlg
->url
));
21 gtk_widget_set_sensitive(idlg
->get
, text
&& text
[0]);
22 gtk_dialog_set_response_sensitive(GTK_DIALOG(idlg
->dlg
),
23 GTK_RESPONSE_OK
, text
&& text
[0]);
27 size_cb(ImageDlg
*idlg
, gint width
, gint height
) {
29 text
= g_strdup_printf("%d", width
);
30 gtk_entry_set_text(GTK_ENTRY(idlg
->width
), text
);
32 text
= g_strdup_printf("%d", height
);
33 gtk_entry_set_text(GTK_ENTRY(idlg
->height
), text
);
38 get_url_cb(ImageDlg
*idlg
) {
39 GdkPixbufLoader
*loader
;
44 /* XXX suboptimal: we retrieve the entire image
45 * when we only need a few bytes. */
46 url
= gtk_entry_get_text(GTK_ENTRY(idlg
->url
));
47 img
= net_get_run(_("Retrieving Image..."), GTK_WINDOW(idlg
->dlg
), url
);
52 loader
= gdk_pixbuf_loader_new();
53 g_signal_connect_swapped(G_OBJECT(loader
), "size-prepared",
54 G_CALLBACK(size_cb
), idlg
);
55 if (!gdk_pixbuf_loader_write(loader
, (unsigned char*)img
->str
, img
->len
, &err
)) {
56 jam_warning(GTK_WINDOW(idlg
->dlg
),
57 _("Error loading image: %s."), err
->message
);
60 gdk_pixbuf_loader_close(loader
, NULL
);
61 g_object_unref(G_OBJECT(loader
));
65 make_dialog(ImageDlg
*idlg
, GtkWindow
*win
) {
66 GtkWidget
*vbox
, *hbox
, *dimbox
;
69 idlg
->dlg
= gtk_dialog_new_with_buttons(_("Insert Image"),
70 win
, GTK_DIALOG_MODAL
,
71 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
72 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
74 gtk_dialog_set_default_response(GTK_DIALOG(idlg
->dlg
),
76 g_signal_connect_swapped(G_OBJECT(idlg
->dlg
), "show",
77 G_CALLBACK(url_changed_cb
), idlg
);
79 sg
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
81 vbox
= gtk_vbox_new(FALSE
, 12);
83 idlg
->url
= gtk_entry_new();
84 g_signal_connect_swapped(G_OBJECT(idlg
->url
), "changed",
85 G_CALLBACK(url_changed_cb
), idlg
);
86 gtk_box_pack_start(GTK_BOX(vbox
),
87 labelled_box_new_sg(_("_URL:"), idlg
->url
, sg
),
90 hbox
= gtk_hbox_new(FALSE
, 6);
92 dimbox
= gtk_vbox_new(FALSE
, 6);
93 idlg
->width
= gtk_entry_new();
94 gtk_entry_set_width_chars(GTK_ENTRY(idlg
->width
), 6);
95 gtk_box_pack_start(GTK_BOX(dimbox
),
96 labelled_box_new_sg(_("_Width:"), idlg
->width
, sg
),
99 idlg
->height
= gtk_entry_new();
100 gtk_entry_set_width_chars(GTK_ENTRY(idlg
->height
), 6);
101 gtk_box_pack_start(GTK_BOX(dimbox
),
102 labelled_box_new_sg(_("_Height:"), idlg
->height
, sg
),
105 gtk_box_pack_start(GTK_BOX(hbox
), dimbox
, FALSE
, FALSE
, 0);
107 dimbox
= gtk_vbox_new(FALSE
, 6);
108 idlg
->get
= gtk_button_new_with_mnemonic(_("_Retrieve Dimensions from Network"));
109 g_signal_connect_swapped(G_OBJECT(idlg
->get
), "clicked",
110 G_CALLBACK(get_url_cb
), idlg
);
111 gtk_box_pack_start(GTK_BOX(dimbox
), idlg
->get
, TRUE
, FALSE
, 0);
113 gtk_box_pack_end(GTK_BOX(hbox
), dimbox
, FALSE
, FALSE
, 0);
115 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
117 jam_dialog_set_contents(GTK_DIALOG(idlg
->dlg
), vbox
);
121 is_image_link(const char *str
) {
124 return (g_str_has_prefix(str
, "http://") ||
125 g_str_has_prefix(str
, "ftp://"));
129 image_dialog_run(GtkWindow
*win
, JamDoc
*doc
) {
130 STACK(ImageDlg
, idlg
);
131 GtkTextBuffer
*buffer
;
132 GtkTextIter start
, end
;
133 char *sel
= NULL
, *url
= NULL
;
135 int width
= -1, height
= -1;
137 make_dialog(idlg
, win
);
139 buffer
= jam_doc_get_text_buffer(doc
);
140 if (gtk_text_buffer_get_selection_bounds(buffer
, &start
, &end
)) {
141 sel
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
142 if (is_image_link(sel
))
143 gtk_entry_set_text(GTK_ENTRY(idlg
->url
), sel
);
146 clip
= jam_clipboard_wait_for_text_timeout(
147 gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
),
149 if (is_image_link(clip
))
150 gtk_entry_set_text(GTK_ENTRY(idlg
->url
), clip
);
154 if (gtk_dialog_run(GTK_DIALOG(idlg
->dlg
)) == GTK_RESPONSE_OK
) {
156 gtk_text_buffer_delete(buffer
, &start
, &end
);
158 gtk_text_buffer_get_iter_at_mark(buffer
, &start
,
159 gtk_text_buffer_get_insert(buffer
));
161 url
= g_strdup(gtk_entry_get_text(GTK_ENTRY(idlg
->url
)));
163 gtk_text_buffer_insert(buffer
, &start
, "<img src='", -1);
164 gtk_text_buffer_insert(buffer
, &start
, url
, -1);
165 gtk_text_buffer_insert(buffer
, &start
, "'", -1);
168 data
= gtk_entry_get_text(GTK_ENTRY(idlg
->width
));
171 data
= gtk_entry_get_text(GTK_ENTRY(idlg
->height
));
175 char *data
= g_strdup_printf(" width='%d'", width
);
176 gtk_text_buffer_insert(buffer
, &start
, data
, -1);
180 char *data
= g_strdup_printf(" height='%d'", height
);
181 gtk_text_buffer_insert(buffer
, &start
, data
, -1);
184 gtk_text_buffer_insert(buffer
, &start
, " />", -1);
189 gtk_widget_destroy(idlg
->dlg
);