comparison src/gtkimhtml.c @ 9573:c8e0426c63ed

[gaim-migrate @ 10416] Make the right-click->save image for IM images use the 2.4 file chooser. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 22 Jul 2004 03:23:12 +0000
parents b6971539f7af
children fd32ae5ee373
comparison
equal deleted inserted replaced
9572:d066c21dcbef 9573:c8e0426c63ed
2691 im_image->width = gdk_pixbuf_get_width(img); 2691 im_image->width = gdk_pixbuf_get_width(img);
2692 im_image->height = gdk_pixbuf_get_height(img); 2692 im_image->height = gdk_pixbuf_get_height(img);
2693 im_image->mark = NULL; 2693 im_image->mark = NULL;
2694 im_image->filename = filename ? g_strdup(filename) : NULL; 2694 im_image->filename = filename ? g_strdup(filename) : NULL;
2695 im_image->id = id; 2695 im_image->id = id;
2696 im_image->filesel = NULL;
2696 2697
2697 g_object_ref(img); 2698 g_object_ref(img);
2698 return GTK_IMHTML_SCALABLE(im_image); 2699 return GTK_IMHTML_SCALABLE(im_image);
2699 } 2700 }
2700 2701
2722 gtk_image_set_from_pixbuf(image->image, new_image); 2723 gtk_image_set_from_pixbuf(image->image, new_image);
2723 g_object_unref(G_OBJECT(new_image)); 2724 g_object_unref(G_OBJECT(new_image));
2724 } 2725 }
2725 } 2726 }
2726 2727
2727 static void write_img_to_file(GtkWidget *w, GtkFileSelection *sel) 2728 static void
2728 { 2729 image_save_yes_cb(GtkIMHtmlImage *image, const char *filename)
2729 const gchar *filename = gtk_file_selection_get_filename(sel); 2730 {
2730 gchar *dirname;
2731 GtkIMHtmlImage *image = g_object_get_data(G_OBJECT(sel), "GtkIMHtmlImage");
2732 gchar *type = NULL; 2731 gchar *type = NULL;
2733 GError *error = NULL; 2732 GError *error = NULL;
2734 #if GTK_CHECK_VERSION(2,2,0) 2733 #if GTK_CHECK_VERSION(2,2,0)
2735 GSList *formats = gdk_pixbuf_get_formats(); 2734 GSList *formats = gdk_pixbuf_get_formats();
2736 #else 2735 #else
2736 GtkIMHtmlImage *image = g_object_get_data(G_OBJECT(sel), "GtkIMHtmlImage");
2737 char *basename = g_path_get_basename(filename); 2737 char *basename = g_path_get_basename(filename);
2738 char *ext = strrchr(basename, '.'); 2738 char *ext = strrchr(basename, '.');
2739 #endif 2739 #endif
2740 2740
2741 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { 2741 gtk_widget_destroy(image->filesel);
2742 /* append a / if needed */ 2742 image->filesel = NULL;
2743 if (filename[strlen(filename) - 1] != '/') {
2744 dirname = g_strconcat(filename, "/", NULL);
2745 } else {
2746 dirname = g_strdup(filename);
2747 }
2748 gtk_file_selection_set_filename(sel, dirname);
2749 g_free(dirname);
2750 return;
2751 }
2752 2743
2753 #if GTK_CHECK_VERSION(2,2,0) 2744 #if GTK_CHECK_VERSION(2,2,0)
2754 while(formats){ 2745 while (formats) {
2755 GdkPixbufFormat *format = formats->data; 2746 GdkPixbufFormat *format = formats->data;
2756 gchar **extensions = gdk_pixbuf_format_get_extensions(format); 2747 gchar **extensions = gdk_pixbuf_format_get_extensions(format);
2757 gpointer p = extensions; 2748 gpointer p = extensions;
2758 2749
2759 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){ 2750 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){
2768 extensions++; 2759 extensions++;
2769 } 2760 }
2770 2761
2771 g_strfreev(p); 2762 g_strfreev(p);
2772 2763
2773 if(type) 2764 if (type)
2774 break; 2765 break;
2775 2766
2776 formats = formats->next; 2767 formats = formats->next;
2777 } 2768 }
2778 2769
2779 g_slist_free(formats); 2770 g_slist_free(formats);
2780 #else 2771 #else
2781 /* this is really ugly code, but I think it will work */ 2772 /* this is really ugly code, but I think it will work */
2782 if(ext) { 2773 if (ext) {
2783 ext++; 2774 ext++;
2784 if(!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg")) 2775 if (!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg"))
2785 type = g_strdup("jpeg"); 2776 type = g_strdup("jpeg");
2786 else if(!g_ascii_strcasecmp(ext, "png")) 2777 else if (!g_ascii_strcasecmp(ext, "png"))
2787 type = g_strdup("png"); 2778 type = g_strdup("png");
2788 } 2779 }
2789 2780
2790 g_free(basename); 2781 g_free(basename);
2791 #endif 2782 #endif
2792 2783
2793 /* If I can't find a valid type, I will just tell the user about it and then assume 2784 /* If I can't find a valid type, I will just tell the user about it and then assume
2794 it's a png */ 2785 it's a png */
2795 if(!type){ 2786 if (!type){
2796 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, 2787 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
2797 _("Unable to guess the image type based on the file extension supplied. Defaulting to PNG.")); 2788 _("Unable to guess the image type based on the file extension supplied. Defaulting to PNG."));
2798 type = g_strdup("png"); 2789 type = g_strdup("png");
2799 } 2790 }
2800 2791
2801 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL); 2792 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL);
2802 2793
2803 if(error){ 2794 if (error){
2804 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, 2795 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
2805 _("Error saving image: %s"), error->message); 2796 _("Error saving image: %s"), error->message);
2806 g_error_free(error); 2797 g_error_free(error);
2807 } 2798 }
2808 2799
2809 g_free(type); 2800 g_free(type);
2810 } 2801 }
2811 2802
2812 static void gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image) 2803 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2813 { 2804 static void
2814 GtkWidget *sel = gtk_file_selection_new(_("Save Image")); 2805 image_save_check_if_exists_cb(GtkWidget *widget, gint response, GtkIMHtmlImage *image)
2815 2806 {
2816 if (image->filename) 2807 gchar *filename;
2817 gtk_file_selection_set_filename(GTK_FILE_SELECTION(sel), image->filename); 2808
2818 g_object_set_data(G_OBJECT(sel), "GtkIMHtmlImage", image); 2809 if (response != GTK_RESPONSE_ACCEPT) {
2819 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", 2810 gtk_widget_destroy(widget);
2820 G_CALLBACK(write_img_to_file), sel); 2811 image->filesel = NULL;
2821 2812 return;
2822 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", 2813 }
2823 G_CALLBACK(gtk_widget_destroy), sel); 2814
2824 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->cancel_button), "clicked", 2815 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
2825 G_CALLBACK(gtk_widget_destroy), sel); 2816 #else /* FILECHOOSER */
2826 2817 static void
2827 gtk_widget_show(sel); 2818 image_save_check_if_exists_cb(GtkWidget *button, GtkIMHtmlImage *image)
2819 {
2820 gchar *filename;
2821
2822 filename = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(image->filesel)));
2823
2824 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
2825 gchar *dirname;
2826 /* append a / is needed */
2827 if (filename[strlen(filename) - 1] != G_DIR_SEPARATOR) {
2828 dirname = g_strconcat(filename, G_DIR_SEPARATOR_S, NULL);
2829 } else {
2830 dirname = g_strdup(filename);
2831 }
2832 gtk_file_selection_set_filename(image->filesel, dirname);
2833 g_free(dirname);
2834 g_free(filename);
2835 return;
2836 }
2837 #endif /* FILECHOOSER */
2838
2839 /*
2840 * XXX - We should probably prompt the user to determine if they really
2841 * want to overwrite the file or not. However, I don't feel like doing
2842 * that, so we're just always going to overwrite if the file exists.
2843 */
2844 /*
2845 if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
2846 } else
2847 image_save_yes_cb(image, filename);
2848 */
2849
2850 image_save_yes_cb(image, filename);
2851
2852 g_free(filename);
2853 }
2854
2855 #if !GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2856 static void
2857 image_save_cancel_cb(GtkIMHtmlImage *image)
2858 {
2859 gtk_widget_destroy(image->filesel);
2860 image->filesel = NULL;
2861 }
2862 #endif /* FILECHOOSER */
2863
2864 static void
2865 gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image)
2866 {
2867 if (image->filesel != NULL) {
2868 gtk_window_present(GTK_WINDOW(image->filesel));
2869 return;
2870 }
2871
2872 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
2873 image->filesel = gtk_file_chooser_dialog_new(_("Save Image"),
2874 NULL,
2875 GTK_FILE_CHOOSER_ACTION_SAVE,
2876 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2877 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
2878 NULL);
2879 gtk_dialog_set_default_response(GTK_DIALOG(image->filesel), GTK_RESPONSE_ACCEPT);
2880 if (image->filename != NULL)
2881 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(image->filesel), image->filename);
2882 g_signal_connect(G_OBJECT(GTK_FILE_CHOOSER(image->filesel)), "response",
2883 G_CALLBACK(image_save_check_if_exists_cb), image);
2884 #else /* FILECHOOSER */
2885 image->filesel = gtk_file_selection_new(_("Save Image"));
2886 if (image->filename != NULL)
2887 gtk_file_selection_set_filename(GTK_FILE_SELECTION(image->filesel), image->filename);
2888 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->ok_button), "clicked",
2889 G_CALLBACK(image_save_check_if_exists_cb), image);
2890 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->ok_button), "clicked",
2891 G_CALLBACK(image_save_cancel_cb), image);
2892 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->cancel_button), "clicked",
2893 G_CALLBACK(image_save_cancel_cb), image);
2894 #endif /* FILECHOOSER */
2895
2896 gtk_widget_show(image->filesel);
2828 } 2897 }
2829 2898
2830 static gboolean gtk_imhtml_image_clicked(GtkWidget *w, GdkEvent *event, GtkIMHtmlImage *image) 2899 static gboolean gtk_imhtml_image_clicked(GtkWidget *w, GdkEvent *event, GtkIMHtmlImage *image)
2831 { 2900 {
2832 GdkEventButton *event_button = (GdkEventButton *) event; 2901 GdkEventButton *event_button = (GdkEventButton *) event;
2864 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; 2933 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale;
2865 2934
2866 g_object_unref(image->pixbuf); 2935 g_object_unref(image->pixbuf);
2867 if (image->filename) 2936 if (image->filename)
2868 g_free(image->filename); 2937 g_free(image->filename);
2938 if (image->filesel)
2939 gtk_widget_destroy(image->filesel);
2869 g_free(scale); 2940 g_free(scale);
2870 } 2941 }
2871 2942
2872 void gtk_imhtml_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) 2943 void gtk_imhtml_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter)
2873 { 2944 {