Sync the CropArea-widget again.

Sync'ed to the latest commit of gnome-control-center (commit d511097).
This commit is contained in:
Niels De Graef 2017-06-27 09:34:39 +02:00
parent 9a55109d99
commit 67f3eb60d8
5 changed files with 115 additions and 101 deletions

View file

@ -58,7 +58,7 @@ contact-resources.c: $(top_srcdir)/data/contacts.gresource.xml $(resource_files)
gnome_contacts_SOURCES = \ gnome_contacts_SOURCES = \
$(vala_sources) \ $(vala_sources) \
um-crop-area.c \ cc-crop-area.c \
contact-resources.c \ contact-resources.c \
contacts-esd-setup.c \ contacts-esd-setup.c \
$(NULL) $(NULL)
@ -92,6 +92,6 @@ CLEANFILES = \
EXTRA_DIST = \ EXTRA_DIST = \
org.gnome.Contacts.gschema.xml \ org.gnome.Contacts.gschema.xml \
cheese-flash.h \ cheese-flash.h \
um-crop-area.h \ cc-crop-area.h \
contacts-esd-setup.h \ contacts-esd-setup.h \
$(NULL) $(NULL)

View file

@ -4,7 +4,7 @@
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
@ -26,9 +26,9 @@
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "um-crop-area.h" #include "cc-crop-area.h"
struct _UmCropAreaPrivate { struct _CcCropAreaPrivate {
GdkPixbuf *browse_pixbuf; GdkPixbuf *browse_pixbuf;
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
GdkPixbuf *color_shifted; GdkPixbuf *color_shifted;
@ -44,7 +44,7 @@ struct _UmCropAreaPrivate {
gdouble aspect; gdouble aspect;
}; };
G_DEFINE_TYPE (UmCropArea, um_crop_area, GTK_TYPE_DRAWING_AREA); G_DEFINE_TYPE (CcCropArea, cc_crop_area, GTK_TYPE_DRAWING_AREA);
static inline guchar static inline guchar
shift_color_byte (guchar b, shift_color_byte (guchar b,
@ -87,21 +87,27 @@ shift_colors (GdkPixbuf *pixbuf,
} }
static void static void
update_pixbufs (UmCropArea *area) update_pixbufs (CcCropArea *area)
{ {
gint width; gint width;
gint height; gint height;
GtkAllocation allocation; GtkAllocation allocation;
gdouble scale; gdouble scale;
GdkRGBA color; gint dest_width, dest_height;
guint32 pixel;
gint dest_x, dest_y, dest_width, dest_height;
GtkWidget *widget; GtkWidget *widget;
GtkStyleContext *context;
widget = GTK_WIDGET (area); widget = GTK_WIDGET (area);
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
context = gtk_widget_get_style_context (widget);
width = gdk_pixbuf_get_width (area->priv->browse_pixbuf);
height = gdk_pixbuf_get_height (area->priv->browse_pixbuf);
scale = allocation.height / (gdouble)height;
if (scale * width > allocation.width)
scale = allocation.width / (gdouble)width;
dest_width = width * scale;
dest_height = height * scale;
if (area->priv->pixbuf == NULL || if (area->priv->pixbuf == NULL ||
gdk_pixbuf_get_width (area->priv->pixbuf) != allocation.width || gdk_pixbuf_get_width (area->priv->pixbuf) != allocation.width ||
@ -111,31 +117,14 @@ update_pixbufs (UmCropArea *area)
area->priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, area->priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
gdk_pixbuf_get_has_alpha (area->priv->browse_pixbuf), gdk_pixbuf_get_has_alpha (area->priv->browse_pixbuf),
8, 8,
allocation.width, allocation.height); dest_width, dest_height);
gdk_pixbuf_fill (area->priv->pixbuf, 0x0);
gtk_style_context_get_background_color (context, gtk_style_context_get_state (context), &color);
pixel = (((gint)(color.red * 1.0)) << 16) |
(((gint)(color.green * 1.0)) << 8) |
((gint)(color.blue * 1.0));
gdk_pixbuf_fill (area->priv->pixbuf, pixel);
width = gdk_pixbuf_get_width (area->priv->browse_pixbuf);
height = gdk_pixbuf_get_height (area->priv->browse_pixbuf);
scale = allocation.height / (gdouble)height;
if (scale * width > allocation.width)
scale = allocation.width / (gdouble)width;
dest_width = width * scale;
dest_height = height * scale;
dest_x = (allocation.width - dest_width) / 2;
dest_y = (allocation.height - dest_height) / 2,
gdk_pixbuf_scale (area->priv->browse_pixbuf, gdk_pixbuf_scale (area->priv->browse_pixbuf,
area->priv->pixbuf, area->priv->pixbuf,
dest_x, dest_y, 0, 0,
dest_width, dest_height, dest_width, dest_height,
dest_x, dest_y, 0, 0,
scale, scale, scale, scale,
GDK_INTERP_BILINEAR); GDK_INTERP_BILINEAR);
@ -145,22 +134,31 @@ update_pixbufs (UmCropArea *area)
shift_colors (area->priv->color_shifted, -32, -32, -32, 0); shift_colors (area->priv->color_shifted, -32, -32, -32, 0);
if (area->priv->scale == 0.0) { if (area->priv->scale == 0.0) {
area->priv->crop.width = 2 * area->priv->base_width / scale; gdouble scale_to_80, scale_to_image, crop_scale;
area->priv->crop.height = 2 * area->priv->base_height / scale;
/* Scale the crop rectangle to 80% of the area, or less to fit the image */
scale_to_80 = MIN ((gdouble)gdk_pixbuf_get_width (area->priv->pixbuf) * 0.8 / area->priv->base_width,
(gdouble)gdk_pixbuf_get_height (area->priv->pixbuf) * 0.8 / area->priv->base_height);
scale_to_image = MIN ((gdouble)dest_width / area->priv->base_width,
(gdouble)dest_height / area->priv->base_height);
crop_scale = MIN (scale_to_80, scale_to_image);
area->priv->crop.width = crop_scale * area->priv->base_width / scale;
area->priv->crop.height = crop_scale * area->priv->base_height / scale;
area->priv->crop.x = (gdk_pixbuf_get_width (area->priv->browse_pixbuf) - area->priv->crop.width) / 2; area->priv->crop.x = (gdk_pixbuf_get_width (area->priv->browse_pixbuf) - area->priv->crop.width) / 2;
area->priv->crop.y = (gdk_pixbuf_get_height (area->priv->browse_pixbuf) - area->priv->crop.height) / 2; area->priv->crop.y = (gdk_pixbuf_get_height (area->priv->browse_pixbuf) - area->priv->crop.height) / 2;
} }
area->priv->scale = scale; area->priv->scale = scale;
area->priv->image.x = dest_x; area->priv->image.x = (allocation.width - dest_width) / 2;
area->priv->image.y = dest_y; area->priv->image.y = (allocation.height - dest_height) / 2;
area->priv->image.width = dest_width; area->priv->image.width = dest_width;
area->priv->image.height = dest_height; area->priv->image.height = dest_height;
} }
} }
static void static void
crop_to_widget (UmCropArea *area, crop_to_widget (CcCropArea *area,
GdkRectangle *crop) GdkRectangle *crop)
{ {
crop->x = area->priv->image.x + area->priv->crop.x * area->priv->scale; crop->x = area->priv->image.x + area->priv->crop.x * area->priv->scale;
@ -183,12 +181,12 @@ typedef enum {
} Location; } Location;
static gboolean static gboolean
um_crop_area_draw (GtkWidget *widget, cc_crop_area_draw (GtkWidget *widget,
cairo_t *cr) cairo_t *cr)
{ {
GdkRectangle crop; GdkRectangle crop;
gint width, height; gint width, height, ix, iy;
UmCropArea *uarea = UM_CROP_AREA (widget); CcCropArea *uarea = CC_CROP_AREA (widget);
if (uarea->priv->browse_pixbuf == NULL) if (uarea->priv->browse_pixbuf == NULL)
return FALSE; return FALSE;
@ -199,14 +197,17 @@ um_crop_area_draw (GtkWidget *widget,
height = gdk_pixbuf_get_height (uarea->priv->pixbuf); height = gdk_pixbuf_get_height (uarea->priv->pixbuf);
crop_to_widget (uarea, &crop); crop_to_widget (uarea, &crop);
gdk_cairo_set_source_pixbuf (cr, uarea->priv->color_shifted, 0, 0); ix = uarea->priv->image.x;
cairo_rectangle (cr, 0, 0, width, crop.y); iy = uarea->priv->image.y;
cairo_rectangle (cr, 0, crop.y, crop.x, crop.height);
cairo_rectangle (cr, crop.x + crop.width, crop.y, width - crop.x - crop.width, crop.height); gdk_cairo_set_source_pixbuf (cr, uarea->priv->color_shifted, ix, iy);
cairo_rectangle (cr, 0, crop.y + crop.height, width, height - crop.y - crop.height); cairo_rectangle (cr, ix, iy, width, crop.y - iy);
cairo_rectangle (cr, ix, crop.y, crop.x - ix, crop.height);
cairo_rectangle (cr, crop.x + crop.width, crop.y, width - crop.width - (crop.x - ix), crop.height);
cairo_rectangle (cr, ix, crop.y + crop.height, width, height - crop.height - (crop.y - iy));
cairo_fill (cr); cairo_fill (cr);
gdk_cairo_set_source_pixbuf (cr, uarea->priv->pixbuf, 0, 0); gdk_cairo_set_source_pixbuf (cr, uarea->priv->pixbuf, ix, iy);
cairo_rectangle (cr, crop.x, crop.y, crop.width, crop.height); cairo_rectangle (cr, crop.x, crop.y, crop.width, crop.height);
cairo_fill (cr); cairo_fill (cr);
@ -301,7 +302,7 @@ find_location (GdkRectangle *rect,
} }
static void static void
update_cursor (UmCropArea *area, update_cursor (CcCropArea *area,
gint x, gint x,
gint y) gint y)
{ {
@ -351,7 +352,8 @@ update_cursor (UmCropArea *area,
} }
if (cursor_type != area->priv->current_cursor) { if (cursor_type != area->priv->current_cursor) {
GdkCursor *cursor = gdk_cursor_new (cursor_type); GdkCursor *cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (area)),
cursor_type);
gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (area)), cursor); gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (area)), cursor);
g_object_unref (cursor); g_object_unref (cursor);
area->priv->current_cursor = cursor_type; area->priv->current_cursor = cursor_type;
@ -373,10 +375,10 @@ eval_radial_line (gdouble center_x, gdouble center_y,
} }
static gboolean static gboolean
um_crop_area_motion_notify_event (GtkWidget *widget, cc_crop_area_motion_notify_event (GtkWidget *widget,
GdkEventMotion *event) GdkEventMotion *event)
{ {
UmCropArea *area = UM_CROP_AREA (widget); CcCropArea *area = CC_CROP_AREA (widget);
gint x, y; gint x, y;
gint delta_x, delta_y; gint delta_x, delta_y;
gint width, height; gint width, height;
@ -634,10 +636,10 @@ um_crop_area_motion_notify_event (GtkWidget *widget,
} }
static gboolean static gboolean
um_crop_area_button_press_event (GtkWidget *widget, cc_crop_area_button_press_event (GtkWidget *widget,
GdkEventButton *event) GdkEventButton *event)
{ {
UmCropArea *area = UM_CROP_AREA (widget); CcCropArea *area = CC_CROP_AREA (widget);
GdkRectangle crop; GdkRectangle crop;
if (area->priv->browse_pixbuf == NULL) if (area->priv->browse_pixbuf == NULL)
@ -657,10 +659,10 @@ um_crop_area_button_press_event (GtkWidget *widget,
} }
static gboolean static gboolean
um_crop_area_button_release_event (GtkWidget *widget, cc_crop_area_button_release_event (GtkWidget *widget,
GdkEventButton *event) GdkEventButton *event)
{ {
UmCropArea *area = UM_CROP_AREA (widget); CcCropArea *area = CC_CROP_AREA (widget);
GdkRectangle crop; GdkRectangle crop;
if (area->priv->browse_pixbuf == NULL) if (area->priv->browse_pixbuf == NULL)
@ -680,9 +682,17 @@ um_crop_area_button_release_event (GtkWidget *widget,
} }
static void static void
um_crop_area_finalize (GObject *object) cc_crop_area_set_size_request (CcCropArea *area)
{ {
UmCropArea *area = UM_CROP_AREA (object); gtk_widget_set_size_request (GTK_WIDGET (area),
area->priv->base_width,
area->priv->base_height);
}
static void
cc_crop_area_finalize (GObject *object)
{
CcCropArea *area = CC_CROP_AREA (object);
if (area->priv->browse_pixbuf) { if (area->priv->browse_pixbuf) {
g_object_unref (area->priv->browse_pixbuf); g_object_unref (area->priv->browse_pixbuf);
@ -699,25 +709,25 @@ um_crop_area_finalize (GObject *object)
} }
static void static void
um_crop_area_class_init (UmCropAreaClass *klass) cc_crop_area_class_init (CcCropAreaClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->finalize = um_crop_area_finalize; object_class->finalize = cc_crop_area_finalize;
widget_class->draw = um_crop_area_draw; widget_class->draw = cc_crop_area_draw;
widget_class->button_press_event = um_crop_area_button_press_event; widget_class->button_press_event = cc_crop_area_button_press_event;
widget_class->button_release_event = um_crop_area_button_release_event; widget_class->button_release_event = cc_crop_area_button_release_event;
widget_class->motion_notify_event = um_crop_area_motion_notify_event; widget_class->motion_notify_event = cc_crop_area_motion_notify_event;
g_type_class_add_private (klass, sizeof (UmCropAreaPrivate)); g_type_class_add_private (klass, sizeof (CcCropAreaPrivate));
} }
static void static void
um_crop_area_init (UmCropArea *area) cc_crop_area_init (CcCropArea *area)
{ {
area->priv = (G_TYPE_INSTANCE_GET_PRIVATE ((area), UM_TYPE_CROP_AREA, area->priv = (G_TYPE_INSTANCE_GET_PRIVATE ((area), CC_TYPE_CROP_AREA,
UmCropAreaPrivate)); CcCropAreaPrivate));
gtk_widget_add_events (GTK_WIDGET (area), GDK_POINTER_MOTION_MASK | gtk_widget_add_events (GTK_WIDGET (area), GDK_POINTER_MOTION_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
@ -732,16 +742,18 @@ um_crop_area_init (UmCropArea *area)
area->priv->base_width = 48; area->priv->base_width = 48;
area->priv->base_height = 48; area->priv->base_height = 48;
area->priv->aspect = 1; area->priv->aspect = 1;
cc_crop_area_set_size_request (area);
} }
GtkWidget * GtkWidget *
um_crop_area_new (void) cc_crop_area_new (void)
{ {
return g_object_new (UM_TYPE_CROP_AREA, NULL); return g_object_new (CC_TYPE_CROP_AREA, NULL);
} }
GdkPixbuf * GdkPixbuf *
um_crop_area_get_picture (UmCropArea *area) cc_crop_area_get_picture (CcCropArea *area)
{ {
gint width, height; gint width, height;
@ -757,7 +769,7 @@ um_crop_area_get_picture (UmCropArea *area)
} }
void void
um_crop_area_set_picture (UmCropArea *area, cc_crop_area_set_picture (CcCropArea *area,
GdkPixbuf *pixbuf) GdkPixbuf *pixbuf)
{ {
int width; int width;
@ -791,20 +803,22 @@ um_crop_area_set_picture (UmCropArea *area,
} }
void void
um_crop_area_set_min_size (UmCropArea *area, cc_crop_area_set_min_size (CcCropArea *area,
gint width, gint width,
gint height) gint height)
{ {
area->priv->base_width = width; area->priv->base_width = width;
area->priv->base_height = height; area->priv->base_height = height;
cc_crop_area_set_size_request (area);
if (area->priv->aspect > 0) { if (area->priv->aspect > 0) {
area->priv->aspect = area->priv->base_width / (gdouble)area->priv->base_height; area->priv->aspect = area->priv->base_width / (gdouble)area->priv->base_height;
} }
} }
void void
um_crop_area_set_constrain_aspect (UmCropArea *area, cc_crop_area_set_constrain_aspect (CcCropArea *area,
gboolean constrain) gboolean constrain)
{ {
if (constrain) { if (constrain) {

View file

@ -17,49 +17,49 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _UM_CROP_AREA_H_ #ifndef _CC_CROP_AREA_H_
#define _UM_CROP_AREA_H_ #define _CC_CROP_AREA_H_
#include <glib-object.h> #include <glib-object.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define UM_TYPE_CROP_AREA (um_crop_area_get_type ()) #define CC_TYPE_CROP_AREA (cc_crop_area_get_type ())
#define UM_CROP_AREA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), UM_TYPE_CROP_AREA, \ #define CC_CROP_AREA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CC_TYPE_CROP_AREA, \
UmCropArea)) CcCropArea))
#define UM_CROP_AREA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), UM_TYPE_CROP_AREA, \ #define CC_CROP_AREA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CC_TYPE_CROP_AREA, \
UmCropAreaClass)) CcCropAreaClass))
#define UM_IS_CROP_AREA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), UM_TYPE_CROP_AREA)) #define CC_IS_CROP_AREA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CC_TYPE_CROP_AREA))
#define UM_IS_CROP_AREA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), UM_TYPE_CROP_AREA)) #define CC_IS_CROP_AREA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CC_TYPE_CROP_AREA))
#define UM_CROP_AREA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), UM_TYPE_CROP_AREA, \ #define CC_CROP_AREA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CC_TYPE_CROP_AREA, \
UmCropAreaClass)) CcCropAreaClass))
typedef struct _UmCropAreaClass UmCropAreaClass; typedef struct _CcCropAreaClass CcCropAreaClass;
typedef struct _UmCropArea UmCropArea; typedef struct _CcCropArea CcCropArea;
typedef struct _UmCropAreaPrivate UmCropAreaPrivate; typedef struct _CcCropAreaPrivate CcCropAreaPrivate;
struct _UmCropAreaClass { struct _CcCropAreaClass {
GtkDrawingAreaClass parent_class; GtkDrawingAreaClass parent_class;
}; };
struct _UmCropArea { struct _CcCropArea {
GtkDrawingArea parent_instance; GtkDrawingArea parent_instance;
UmCropAreaPrivate *priv; CcCropAreaPrivate *priv;
}; };
GType um_crop_area_get_type (void) G_GNUC_CONST; GType cc_crop_area_get_type (void) G_GNUC_CONST;
GtkWidget *um_crop_area_new (void); GtkWidget *cc_crop_area_new (void);
GdkPixbuf *um_crop_area_get_picture (UmCropArea *area); GdkPixbuf *cc_crop_area_get_picture (CcCropArea *area);
void um_crop_area_set_picture (UmCropArea *area, void cc_crop_area_set_picture (CcCropArea *area,
GdkPixbuf *pixbuf); GdkPixbuf *pixbuf);
void um_crop_area_set_min_size (UmCropArea *area, void cc_crop_area_set_min_size (CcCropArea *area,
gint width, gint width,
gint height); gint height);
void um_crop_area_set_constrain_aspect (UmCropArea *area, void cc_crop_area_set_constrain_aspect (CcCropArea *area,
gboolean constrain); gboolean constrain);
G_END_DECLS G_END_DECLS
#endif /* _UM_CROP_AREA_H_ */ #endif /* _CC_CROP_AREA_H_ */

View file

@ -48,7 +48,7 @@ public class Contacts.AvatarDialog : Dialog {
private FlowBox stock_thumbnail_grid; private FlowBox stock_thumbnail_grid;
[GtkChild] [GtkChild]
private Grid crop_page; private Grid crop_page;
private Um.CropArea crop_area; private Cc.CropArea crop_area;
[GtkChild] [GtkChild]
private Grid photobooth_page; private Grid photobooth_page;
[GtkChild] [GtkChild]
@ -260,7 +260,7 @@ public class Contacts.AvatarDialog : Dialog {
} }
private void set_crop_widget (Gdk.Pixbuf pixbuf) { private void set_crop_widget (Gdk.Pixbuf pixbuf) {
this.crop_area = new Um.CropArea (); this.crop_area = new Cc.CropArea ();
this.crop_area.set_vexpand (true); this.crop_area.set_vexpand (true);
this.crop_area.set_hexpand (true); this.crop_area.set_hexpand (true);
this.crop_area.set_min_size (48, 48); this.crop_area.set_min_size (48, 48);

View file

@ -35,8 +35,8 @@ namespace Contacts {
public static unowned Gtk.Widget get_icon_for_goa_account (string goa_id); public static unowned Gtk.Widget get_icon_for_goa_account (string goa_id);
} }
[CCode (cprefix = "Um", lower_case_cprefix = "um_", cheader_filename = "um-crop-area.h")] [CCode (cprefix = "Cc", lower_case_cprefix = "cc_", cheader_filename = "cc-crop-area.h")]
namespace Um { namespace Cc {
public class CropArea : Gtk.DrawingArea { public class CropArea : Gtk.DrawingArea {
[CCode (has_construct_function = false, type = "GtkWidget*")] [CCode (has_construct_function = false, type = "GtkWidget*")]
public CropArea (); public CropArea ();