pidgin/pidgin

closing merged branch
port-changes-from-branch-2.x.y-to-default
2020-02-03, Gary Kramlich
2f836435c33c
closing merged branch
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02111-1301, USA.
*/
/*
* GtkTicker Copyright 2000 Syd Logan
*/
#include "gtkticker.h"
#include <gtk/gtk.h>
struct _GtkTicker
{
GtkContainer container;
guint interval; /* how often to scootch */
gint spacing; /* inter-child horizontal spacing */
guint scootch; /* how many pixels to move each scootch */
gint timer; /* timer object */
gint total; /* total width of widgets */
gint width; /* width of containing window */
gboolean dirty;
GList *children;
};
static void gtk_ticker_compute_offsets (GtkTicker *ticker);
static void gtk_ticker_class_init (GtkTickerClass *klass);
static void gtk_ticker_init (GtkTicker *ticker);
static void gtk_ticker_map (GtkWidget *widget);
static void gtk_ticker_realize (GtkWidget *widget);
static void gtk_ticker_get_preferred_width (GtkWidget *widget,
gint *minimal_width,
gint *natural_width);
static void gtk_ticker_get_preferred_height (GtkWidget *widget,
gint *minimal_height,
gint *natural_height);
static void gtk_ticker_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static void gtk_ticker_add_real (GtkContainer *container,
GtkWidget *widget);
static void gtk_ticker_remove_real (GtkContainer *container,
GtkWidget *widget);
static void gtk_ticker_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data);
static GType gtk_ticker_child_type (GtkContainer *container);
static GtkContainerClass *parent_class = NULL;
G_DEFINE_DYNAMIC_TYPE(GtkTicker, gtk_ticker, GTK_TYPE_CONTAINER);
/* This exists solely because the above macro makes gtk_ticker_register_type
* static. */
void
gtk_ticker_register(PurplePlugin *plugin)
{
gtk_ticker_register_type(G_TYPE_MODULE(plugin));
}
static void gtk_ticker_finalize(GObject *object) {
gtk_ticker_stop_scroll(GTK_TICKER(object));
G_OBJECT_CLASS(parent_class)->finalize(object);
}
static void gtk_ticker_class_init (GtkTickerClass *class)
{
GObjectClass *gobject_class;
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
gobject_class = (GObjectClass*) class;
widget_class = (GtkWidgetClass*) class;
container_class = (GtkContainerClass*) class;
parent_class = g_type_class_ref (GTK_TYPE_CONTAINER);
gobject_class->finalize = gtk_ticker_finalize;
widget_class->map = gtk_ticker_map;
widget_class->realize = gtk_ticker_realize;
widget_class->get_preferred_width = gtk_ticker_get_preferred_width;
widget_class->get_preferred_height = gtk_ticker_get_preferred_height;
widget_class->size_allocate = gtk_ticker_size_allocate;
container_class->add = gtk_ticker_add_real;
container_class->remove = gtk_ticker_remove_real;
container_class->forall = gtk_ticker_forall;
container_class->child_type = gtk_ticker_child_type;
}
static void
gtk_ticker_class_finalize(G_GNUC_UNUSED GtkTickerClass *klass)
{
}
static GType gtk_ticker_child_type (GtkContainer *container)
{
return GTK_TYPE_WIDGET;
}
static void gtk_ticker_init (GtkTicker *ticker)
{
gtk_widget_set_has_window (GTK_WIDGET (ticker), TRUE);
ticker->interval = (guint) 200;
ticker->scootch = (guint) 2;
ticker->children = NULL;
ticker->timer = 0;
ticker->dirty = TRUE;
}
GtkWidget* gtk_ticker_new (void)
{
return GTK_WIDGET(g_object_new(GTK_TYPE_TICKER, NULL));
}
static void gtk_ticker_put (GtkTicker *ticker, GtkWidget *widget)
{
GtkTickerChild *child_info;
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER (ticker));
g_return_if_fail (widget != NULL);
child_info = g_new(GtkTickerChild, 1);
child_info->widget = widget;
child_info->x = 0;
gtk_widget_set_parent(widget, GTK_WIDGET (ticker));
ticker->children = g_list_append (ticker->children, child_info);
if (gtk_widget_get_realized (GTK_WIDGET (ticker)))
gtk_widget_realize (widget);
if (gtk_widget_get_visible (GTK_WIDGET (ticker)) &&
gtk_widget_get_visible (widget))
{
if (gtk_widget_get_mapped (GTK_WIDGET (ticker)))
gtk_widget_map (widget);
gtk_widget_queue_resize (GTK_WIDGET (ticker));
}
}
void gtk_ticker_set_interval (GtkTicker *ticker, gint interval)
{
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER (ticker));
if ( interval < 0 )
interval = 200;
ticker->interval = interval;
}
guint gtk_ticker_get_interval (GtkTicker *ticker)
{
g_return_val_if_fail (ticker != NULL, -1);
g_return_val_if_fail (GTK_IS_TICKER (ticker), -1);
return ticker->interval;
}
void gtk_ticker_set_scootch (GtkTicker *ticker, gint scootch)
{
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER (ticker));
if (scootch <= 0)
scootch = 2;
ticker->scootch = scootch;
ticker->dirty = TRUE;
}
guint gtk_ticker_get_scootch (GtkTicker *ticker )
{
g_return_val_if_fail (ticker != NULL, -1);
g_return_val_if_fail (GTK_IS_TICKER (ticker), -1);
return ticker->scootch;
}
void gtk_ticker_set_spacing (GtkTicker *ticker, gint spacing )
{
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER (ticker));
if ( spacing < 0 )
spacing = 0;
ticker->spacing = spacing;
ticker->dirty = TRUE;
}
static int ticker_timeout(gpointer data)
{
GtkTicker *ticker = (GtkTicker *) data;
if (gtk_widget_get_visible (GTK_WIDGET (ticker)))
gtk_widget_queue_resize (GTK_WIDGET (ticker));
return( TRUE );
}
void gtk_ticker_start_scroll(GtkTicker *ticker)
{
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER (ticker));
if ( ticker->timer != 0 )
return;
ticker->timer = g_timeout_add(ticker->interval, ticker_timeout, ticker);
}
void gtk_ticker_stop_scroll(GtkTicker *ticker)
{
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER (ticker));
if ( ticker->timer == 0 )
return;
g_source_remove(ticker->timer);
ticker->timer = 0;
}
guint gtk_ticker_get_spacing (GtkTicker *ticker )
{
g_return_val_if_fail (ticker != NULL, -1);
g_return_val_if_fail (GTK_IS_TICKER (ticker), -1);
return ticker->spacing;
}
static void gtk_ticker_map (GtkWidget *widget)
{
GtkTicker *ticker;
GtkTickerChild *child;
GList *children;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_TICKER (widget));
gtk_widget_set_mapped (widget, TRUE);
ticker = GTK_TICKER (widget);
children = ticker->children;
while (children)
{
child = children->data;
children = children->next;
if (gtk_widget_get_visible (child->widget) &&
!gtk_widget_get_mapped (child->widget))
gtk_widget_map (child->widget);
}
gdk_window_show (gtk_widget_get_window (widget));
}
static void gtk_ticker_realize (GtkWidget *widget)
{
GdkWindowAttr attributes;
gint attributes_mask;
GdkWindow *window;
GtkStyleContext *context;
GtkAllocation allocation;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_TICKER (widget));
gtk_widget_set_realized (widget, TRUE);
attributes.window_type = GDK_WINDOW_CHILD;
gtk_widget_get_allocation (widget, &allocation);
attributes.x = allocation.x;
attributes.y = allocation.y;
attributes.width = allocation.width;
attributes.height = allocation.height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gtk_widget_get_visual (widget);
attributes.event_mask = gtk_widget_get_events (widget);
attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask);
gtk_widget_set_window (widget, window);
gdk_window_set_user_data (window, widget);
context = gtk_widget_get_style_context(widget);
gtk_style_context_add_class(context, GTK_STYLE_CLASS_BACKGROUND);
gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
gtk_style_context_set_background(context, window);
}
static void
gtk_ticker_get_preferred_width (GtkWidget *widget,
gint *minimal_width,
gint *natural_width)
{
GtkTicker *ticker;
GtkTickerChild *child;
GList *children;
gint child_min_width, child_nat_width;
gint width;
guint border_width;
ticker = GTK_TICKER (widget);
*minimal_width = width = 0;
children = ticker->children;
while (children)
{
child = children->data;
children = children->next;
if (gtk_widget_get_visible (child->widget))
{
gtk_widget_get_preferred_width (child->widget, &child_min_width, &child_nat_width);
width += child_nat_width + ticker->spacing;
*minimal_width = MAX(*minimal_width, child_min_width);
}
}
if ( width > ticker->spacing )
width -= ticker->spacing;
border_width = gtk_container_get_border_width (GTK_CONTAINER (ticker));
width += border_width * 2;
*natural_width = width;
}
static void
gtk_ticker_get_preferred_height (GtkWidget *widget,
gint *minimal_height,
gint *natural_height)
{
GtkTicker *ticker;
GtkTickerChild *child;
GList *children;
gint child_min_height, child_nat_height;
gint height;
guint border_width;
ticker = GTK_TICKER (widget);
height = 0;
children = ticker->children;
while (children)
{
child = children->data;
children = children->next;
if (gtk_widget_get_visible (child->widget))
{
gtk_widget_get_preferred_height (child->widget, &child_min_height, &child_nat_height);
height = MAX (height, child_nat_height);
}
}
border_width = gtk_container_get_border_width (GTK_CONTAINER (ticker));
height += border_width * 2;
*minimal_height = *natural_height = height;
}
static void gtk_ticker_compute_offsets (GtkTicker *ticker)
{
GtkAllocation allocation;
GtkTickerChild *child;
GtkRequisition child_requisition;
GList *children;
guint16 border_width;
g_return_if_fail (ticker != NULL);
g_return_if_fail (GTK_IS_TICKER(ticker));
border_width = gtk_container_get_border_width (GTK_CONTAINER (ticker));
gtk_widget_get_allocation (GTK_WIDGET (ticker), &allocation);
ticker->width = allocation.width;
ticker->total = 0;
children = ticker->children;
while (children) {
child = children->data;
child->x = 0;
if (gtk_widget_get_visible (child->widget)) {
gtk_widget_get_preferred_size (child->widget, NULL, &child_requisition);
child->offset = ticker->total;
ticker->total +=
child_requisition.width + border_width + ticker->spacing;
}
children = children->next;
}
ticker->dirty = FALSE;
}
static void gtk_ticker_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkTicker *ticker;
GtkAllocation a;
GtkTickerChild *child;
GtkAllocation child_allocation;
GtkRequisition child_requisition;
GList *children;
guint16 border_width;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_TICKER(widget));
g_return_if_fail (allocation != NULL);
ticker = GTK_TICKER (widget);
gtk_widget_get_allocation (GTK_WIDGET (ticker), &a);
if ( a.width != ticker->width )
ticker->dirty = TRUE;
if ( ticker->dirty == TRUE ) {
gtk_ticker_compute_offsets( ticker );
}
gtk_widget_set_allocation (widget, allocation);
if (gtk_widget_get_realized (widget))
gdk_window_move_resize (gtk_widget_get_window (widget),
allocation->x,
allocation->y,
allocation->width,
allocation->height);
border_width = gtk_container_get_border_width (GTK_CONTAINER (ticker));
children = ticker->children;
while (children)
{
child = children->data;
child->x -= ticker->scootch;
if (gtk_widget_get_visible (child->widget)) {
gtk_widget_get_preferred_size (child->widget, NULL, &child_requisition);
child_allocation.width = child_requisition.width;
child_allocation.x = child->offset + border_width + child->x;
if ( ( child_allocation.x + child_allocation.width ) < allocation->x ) {
if ( ticker->total >= allocation->width ) {
child->x += allocation->x + allocation->width + ( ticker->total - ( allocation->x + allocation->width ) );
}
else {
child->x += allocation->x + allocation->width;
}
}
child_allocation.y = border_width;
child_allocation.height = child_requisition.height;
gtk_widget_size_allocate (child->widget, &child_allocation);
}
children = children->next;
}
}
void gtk_ticker_add(GtkTicker *ticker, GtkWidget *widget)
{
gtk_ticker_add_real( GTK_CONTAINER( ticker ), widget );
ticker->dirty = TRUE;
}
void gtk_ticker_remove(GtkTicker *ticker, GtkWidget *widget)
{
gtk_ticker_remove_real( GTK_CONTAINER( ticker ), widget );
ticker->dirty = TRUE;
}
static void gtk_ticker_add_real(GtkContainer *container, GtkWidget *widget)
{
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_TICKER (container));
g_return_if_fail (widget != NULL);
gtk_ticker_put(GTK_TICKER (container), widget);
}
static void gtk_ticker_remove_real(GtkContainer *container, GtkWidget *widget)
{
GtkTicker *ticker;
GtkTickerChild *child;
GList *children;
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_TICKER (container));
g_return_if_fail (widget != NULL);
ticker = GTK_TICKER (container);
children = ticker->children;
while (children)
{
child = children->data;
if (child->widget == widget)
{
gboolean was_visible = gtk_widget_get_visible (widget);
gtk_widget_unparent (widget);
ticker->children = g_list_remove_link (ticker->children, children);
g_list_free (children);
g_free (child);
if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
gtk_widget_queue_resize (GTK_WIDGET (container));
break;
}
children = children->next;
}
}
static void gtk_ticker_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
GtkTicker *ticker;
GtkTickerChild *child;
GList *children;
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_TICKER (container));
g_return_if_fail (callback != NULL);
ticker = GTK_TICKER (container);
children = ticker->children;
while (children)
{
child = children->data;
children = children->next;
(* callback) (child->widget, callback_data);
}
}