screenshot-dialog.c 9.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* screenshot-dialog.c - main GNOME Screenshot dialog
 *
 * Copyright (C) 2001-2006  Jonathan Blandford <jrb@alum.mit.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 */

20
#include <config.h>
Emmanuele Bassi's avatar
Emmanuele Bassi committed
21 22 23
#include <string.h>
#include <stdlib.h>

24
#include "screenshot-config.h"
25
#include "screenshot-dialog.h"
26
#include <glib/gi18n.h>
27
#include <gio/gio.h>
28

29
enum {
30 31
  TYPE_IMAGE_PNG,
  LAST_TYPE
32
};
33 34 35

static GtkTargetEntry drag_types[] =
{
36
  { "image/png", 0, TYPE_IMAGE_PNG },
37 38 39 40
};

struct ScreenshotDialog
{
41
  GtkBuilder *ui;
42 43 44
  GdkPixbuf *screenshot;
  GdkPixbuf *preview_image;
  GtkWidget *save_widget;
45 46 47
  GtkWidget *filename_entry;
  gint drag_x;
  gint drag_y;
48 49 50
};

static void
51 52 53
on_preview_draw (GtkWidget      *drawing_area,
                 cairo_t        *cr,
                 gpointer        data)
54 55
{
  ScreenshotDialog *dialog = data;
56
  GdkPixbuf *pixbuf = NULL;
57

58 59
  /* Stolen from GtkImage.  I really should just make the drawing area an
   * image some day */
60
  if (gtk_widget_get_state (drawing_area) != GTK_STATE_NORMAL)
61 62 63 64 65 66 67
    {
      GtkIconSource *source;

      source = gtk_icon_source_new ();
      gtk_icon_source_set_pixbuf (source, dialog->preview_image);
      gtk_icon_source_set_size (source, GTK_ICON_SIZE_SMALL_TOOLBAR);
      gtk_icon_source_set_size_wildcarded (source, FALSE);
68 69

      pixbuf = gtk_style_render_icon (gtk_widget_get_style (drawing_area),
70 71
				      source,
				      gtk_widget_get_direction (drawing_area),
72
				      gtk_widget_get_state (drawing_area),
73 74 75 76 77 78 79 80 81
				      (GtkIconSize) -1,
				      drawing_area,
				      "gtk-image");
      gtk_icon_source_free (source);
    }
  else
    {
      pixbuf = g_object_ref (dialog->preview_image);
    }
82 83 84 85

  gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
  cairo_paint (cr);

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  g_object_unref (pixbuf);
}

static gboolean
on_preview_button_press_event (GtkWidget      *drawing_area,
			       GdkEventButton *event,
			       gpointer        data)
{
  ScreenshotDialog *dialog = data;

  dialog->drag_x = (int) event->x;
  dialog->drag_y = (int) event->y;

  return FALSE;
}

static gboolean
on_preview_button_release_event (GtkWidget      *drawing_area,
				 GdkEventButton *event,
				 gpointer        data)
{
  ScreenshotDialog *dialog = data;

  dialog->drag_x = 0;
  dialog->drag_y = 0;

  return FALSE;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

static void
on_preview_configure_event (GtkWidget         *drawing_area,
			    GdkEventConfigure *event,
			    gpointer           data)
{
  ScreenshotDialog *dialog = data;

  if (dialog->preview_image)
    g_object_unref (G_OBJECT (dialog->preview_image));

  dialog->preview_image = gdk_pixbuf_scale_simple (dialog->screenshot,
						   event->width,
						   event->height,
						   GDK_INTERP_BILINEAR);
}

static void
drag_data_get (GtkWidget          *widget,
	       GdkDragContext     *context,
	       GtkSelectionData   *selection_data,
	       guint               info,
	       guint               time,
137
	       ScreenshotDialog   *dialog)
138
{
139 140
  if (info == TYPE_IMAGE_PNG)
    gtk_selection_data_set_pixbuf (selection_data, dialog->screenshot);
141
  else
142
    g_warning ("Unknown type %d", info);
143 144 145 146 147 148 149
}

static void
drag_begin (GtkWidget        *widget,
	    GdkDragContext   *context,
	    ScreenshotDialog *dialog)
{
150 151
  gtk_drag_set_icon_pixbuf (context, dialog->preview_image,
			    dialog->drag_x, dialog->drag_y);
152 153 154 155 156
}


ScreenshotDialog *
screenshot_dialog_new (GdkPixbuf *screenshot,
157
		       char      *initial_uri)
158 159 160 161 162
{
  ScreenshotDialog *dialog;
  GtkWidget *toplevel;
  GtkWidget *preview_darea;
  GtkWidget *aspect_frame;
163
  GtkWidget *file_chooser_box;
164 165 166
  gint width, height;
  char *current_folder;
  char *current_name;
167 168
  char *ext;
  gint pos;
169 170
  GFile *tmp_file;
  GFile *parent_file;
171
  guint res;
172

173 174
  tmp_file = g_file_new_for_uri (initial_uri);
  parent_file = g_file_get_parent (tmp_file);
175

176 177 178 179
  current_name = g_file_get_basename (tmp_file);
  current_folder = g_file_get_uri (parent_file);
  g_object_unref (tmp_file);
  g_object_unref (parent_file);
180 181 182

  dialog = g_new0 (ScreenshotDialog, 1);

183 184
  dialog-> ui = gtk_builder_new ();
  res = gtk_builder_add_from_file (dialog->ui, UIDIR "/gnome-screenshot.ui", NULL);
185 186
  dialog->screenshot = screenshot;

187
  if (res == 0)
188 189 190 191 192
    {
      GtkWidget *dialog;
      dialog = gtk_message_dialog_new (NULL, 0,
				       GTK_MESSAGE_ERROR,
				       GTK_BUTTONS_OK,
193
				       _("UI definition file for the screenshot program is missing.\n"
194
					 "Please check your installation of gnome-utils"));
195 196 197 198 199
      gtk_dialog_run (GTK_DIALOG (dialog));
      gtk_widget_destroy (dialog);
      exit (1);
    }

200 201
  gtk_builder_set_translation_domain (dialog->ui, GETTEXT_PACKAGE);

202 203 204 205 206 207
  width = gdk_pixbuf_get_width (screenshot);
  height = gdk_pixbuf_get_height (screenshot);

  width /= 5;
  height /= 5;

208 209 210 211 212
  toplevel = GTK_WIDGET (gtk_builder_get_object (dialog->ui, "toplevel"));
  aspect_frame = GTK_WIDGET (gtk_builder_get_object (dialog->ui, "aspect_frame"));
  preview_darea = GTK_WIDGET (gtk_builder_get_object (dialog->ui, "preview_darea"));
  dialog->filename_entry = GTK_WIDGET (gtk_builder_get_object (dialog->ui, "filename_entry"));
  file_chooser_box = GTK_WIDGET (gtk_builder_get_object (dialog->ui, "file_chooser_box"));
213

214
  dialog->save_widget = gtk_file_chooser_button_new (_("Select a folder"), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
215 216
  gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog->save_widget), FALSE);
  gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog->save_widget), current_folder);
217 218
  gtk_entry_set_text (GTK_ENTRY (dialog->filename_entry), current_name);

219
  gtk_box_pack_start (GTK_BOX (file_chooser_box), dialog->save_widget, TRUE, TRUE, 0);
220 221 222 223 224 225 226
  g_free (current_folder);

  gtk_widget_set_size_request (preview_darea, width, height);
  gtk_aspect_frame_set (GTK_ASPECT_FRAME (aspect_frame), 0.0, 0.5,
			gdk_pixbuf_get_width (screenshot)/
			(gfloat) gdk_pixbuf_get_height (screenshot),
			FALSE);
227
  g_signal_connect (preview_darea, "draw", G_CALLBACK (on_preview_draw), dialog);
228 229
  g_signal_connect (preview_darea, "button_press_event", G_CALLBACK (on_preview_button_press_event), dialog);
  g_signal_connect (preview_darea, "button_release_event", G_CALLBACK (on_preview_button_release_event), dialog);
230 231
  g_signal_connect (preview_darea, "configure_event", G_CALLBACK (on_preview_configure_event), dialog);

232 233 234 235 236
  gtk_drag_source_set (preview_darea,
		       GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
		       drag_types, G_N_ELEMENTS (drag_types),
		       GDK_ACTION_COPY);

237
  if (screenshot_config->take_window_shot)
238 239 240 241 242 243 244 245 246 247 248 249
    gtk_frame_set_shadow_type (GTK_FRAME (aspect_frame), GTK_SHADOW_NONE);
  else
    gtk_frame_set_shadow_type (GTK_FRAME (aspect_frame), GTK_SHADOW_IN);

  /* setup dnd */
  g_signal_connect (G_OBJECT (preview_darea), "drag_begin",
		    G_CALLBACK (drag_begin), dialog);
  g_signal_connect (G_OBJECT (preview_darea), "drag_data_get",
		    G_CALLBACK (drag_data_get), dialog);

  gtk_widget_show_all (toplevel);

250 251 252
  /* select the name of the file but leave out the extension if there's any;
   * the dialog must be realized for select_region to work
   */
253 254
  ext = g_utf8_strrchr (current_name, -1, '.');
  if (ext)
255
    pos = g_utf8_strlen (current_name, -1) - g_utf8_strlen (ext, -1);
256 257 258 259 260 261 262 263 264
  else
    pos = -1;

  gtk_editable_select_region (GTK_EDITABLE (dialog->filename_entry),
			      0,
			      pos);
  
  g_free (current_name);

265 266 267
  return dialog;
}

268 269 270 271 272 273
void
screenshot_dialog_focus_entry (ScreenshotDialog *dialog)
{
  gtk_widget_grab_focus (dialog->filename_entry);
}

274 275 276
GtkWidget *
screenshot_dialog_get_toplevel (ScreenshotDialog *dialog)
{
277
  return GTK_WIDGET (gtk_builder_get_object (dialog->ui, "toplevel"));
278 279 280 281 282
}

char *
screenshot_dialog_get_uri (ScreenshotDialog *dialog)
{
283 284
  gchar *folder, *file;
  gchar *uri;
285
  gchar *tmp;
286 287

  folder = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog->save_widget));
288 289 290
  tmp = screenshot_dialog_get_filename (dialog);
  file = g_uri_escape_string (tmp, NULL, FALSE);
  g_free (tmp);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  uri = g_build_filename (folder, file, NULL);

  g_free (folder);
  g_free (file);

  return uri;
}

char *
screenshot_dialog_get_folder (ScreenshotDialog *dialog)
{
  return gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog->save_widget));
}

char *
screenshot_dialog_get_filename (ScreenshotDialog *dialog)
{
308
  const gchar *file_name;
309
  gchar *tmp;
310
  GError *error;
311 312 313

  file_name = gtk_entry_get_text (GTK_ENTRY (dialog->filename_entry));

314 315 316 317 318 319 320 321 322 323 324 325
  error = NULL;
  tmp = g_filename_from_utf8 (file_name, -1, NULL, NULL, &error);
  if (error)
    {
      g_warning ("Unable to convert `%s' to valid UTF-8: %s\n"
                 "Falling back to default file.",
                 file_name,
                 error->message);
      g_error_free (error);
      tmp = g_strdup (_("Screenshot.png"));
    }

326
  return tmp;
327
}
328

329 330 331 332 333
void
screenshot_dialog_set_busy (ScreenshotDialog *dialog,
			    gboolean          busy)
{
  GtkWidget *toplevel;
334
  GdkWindow *window;
335 336

  toplevel = screenshot_dialog_get_toplevel (dialog);
337
  window = gtk_widget_get_window (toplevel);
338 339 340 341 342 343

  if (busy)
    {
      GdkCursor *cursor;
      /* Change cursor to busy */
      cursor = gdk_cursor_new (GDK_WATCH);
344
      gdk_window_set_cursor (window, cursor);
345 346 347 348
      gdk_cursor_unref (cursor);
    }
  else
    {
349
      gdk_window_set_cursor (window, NULL);
350 351 352 353 354 355
    }

  gtk_widget_set_sensitive (toplevel, ! busy);

  gdk_flush ();
}