1997-11-24 22:37:52 +00:00
|
|
|
/* 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
|
1998-04-13 02:02:47 +00:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1997-11-24 22:37:52 +00:00
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "gtklabel.h"
|
|
|
|
|
|
|
|
|
1998-01-17 07:52:38 +00:00
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_LABEL,
|
|
|
|
ARG_JUSTIFY
|
|
|
|
};
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
static void gtk_label_class_init (GtkLabelClass *klass);
|
|
|
|
static void gtk_label_init (GtkLabel *label);
|
1998-01-17 07:52:38 +00:00
|
|
|
static void gtk_label_set_arg (GtkLabel *label,
|
|
|
|
GtkArg *arg,
|
|
|
|
guint arg_id);
|
|
|
|
static void gtk_label_get_arg (GtkLabel *label,
|
|
|
|
GtkArg *arg,
|
|
|
|
guint arg_id);
|
1998-01-30 23:47:09 +00:00
|
|
|
static void gtk_label_finalize (GtkObject *object);
|
1997-11-24 22:37:52 +00:00
|
|
|
static void gtk_label_size_request (GtkWidget *widget,
|
|
|
|
GtkRequisition *requisition);
|
|
|
|
static gint gtk_label_expose (GtkWidget *widget,
|
|
|
|
GdkEventExpose *event);
|
1998-03-29 02:44:43 +00:00
|
|
|
static void gtk_label_state_changed (GtkWidget *widget,
|
|
|
|
guint previous_state);
|
|
|
|
static void gtk_label_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *previous_style);
|
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
static GtkMiscClass *parent_class = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
guint
|
1998-05-03 22:41:32 +00:00
|
|
|
gtk_label_get_type (void)
|
1997-11-24 22:37:52 +00:00
|
|
|
{
|
|
|
|
static guint label_type = 0;
|
|
|
|
|
|
|
|
if (!label_type)
|
|
|
|
{
|
|
|
|
GtkTypeInfo label_info =
|
|
|
|
{
|
|
|
|
"GtkLabel",
|
|
|
|
sizeof (GtkLabel),
|
|
|
|
sizeof (GtkLabelClass),
|
|
|
|
(GtkClassInitFunc) gtk_label_class_init,
|
|
|
|
(GtkObjectInitFunc) gtk_label_init,
|
1998-01-17 07:52:38 +00:00
|
|
|
(GtkArgSetFunc) gtk_label_set_arg,
|
|
|
|
(GtkArgGetFunc) gtk_label_get_arg,
|
1997-11-24 22:37:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
label_type = gtk_type_unique (gtk_misc_get_type (), &label_info);
|
1998-05-14 21:13:18 +00:00
|
|
|
gtk_type_set_chunk_alloc (label_type, 32);
|
1997-11-24 22:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return label_type;
|
|
|
|
}
|
|
|
|
|
1998-01-08 19:02:40 +00:00
|
|
|
static void
|
1997-11-24 22:37:52 +00:00
|
|
|
gtk_label_class_init (GtkLabelClass *class)
|
|
|
|
{
|
|
|
|
GtkObjectClass *object_class;
|
|
|
|
GtkWidgetClass *widget_class;
|
|
|
|
|
|
|
|
object_class = (GtkObjectClass*) class;
|
|
|
|
widget_class = (GtkWidgetClass*) class;
|
|
|
|
|
|
|
|
parent_class = gtk_type_class (gtk_misc_get_type ());
|
|
|
|
|
1998-02-19 07:18:42 +00:00
|
|
|
gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
|
|
|
|
gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_ENUM, GTK_ARG_READWRITE, ARG_JUSTIFY);
|
1998-01-17 07:52:38 +00:00
|
|
|
|
1998-01-30 23:47:09 +00:00
|
|
|
object_class->finalize = gtk_label_finalize;
|
1997-11-24 22:37:52 +00:00
|
|
|
|
|
|
|
widget_class->size_request = gtk_label_size_request;
|
|
|
|
widget_class->expose_event = gtk_label_expose;
|
1998-03-29 02:44:43 +00:00
|
|
|
widget_class->style_set = gtk_label_style_set;
|
|
|
|
widget_class->state_changed = gtk_label_state_changed;
|
1997-11-24 22:37:52 +00:00
|
|
|
}
|
|
|
|
|
1998-01-30 23:47:09 +00:00
|
|
|
static void
|
|
|
|
gtk_label_set_arg (GtkLabel *label,
|
|
|
|
GtkArg *arg,
|
|
|
|
guint arg_id)
|
1998-01-17 07:52:38 +00:00
|
|
|
{
|
|
|
|
switch (arg_id)
|
|
|
|
{
|
|
|
|
case ARG_LABEL:
|
|
|
|
gtk_label_set (label, GTK_VALUE_STRING (*arg));
|
|
|
|
break;
|
|
|
|
case ARG_JUSTIFY:
|
|
|
|
gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
|
|
|
|
break;
|
1998-01-21 23:03:11 +00:00
|
|
|
default:
|
|
|
|
arg->type = GTK_TYPE_INVALID;
|
|
|
|
break;
|
1998-01-17 07:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gtk_label_get_arg (GtkLabel *label,
|
|
|
|
GtkArg *arg,
|
|
|
|
guint arg_id)
|
|
|
|
{
|
|
|
|
switch (arg_id)
|
|
|
|
{
|
|
|
|
case ARG_LABEL:
|
|
|
|
GTK_VALUE_STRING (*arg) = g_strdup (label->label);
|
|
|
|
break;
|
|
|
|
case ARG_JUSTIFY:
|
|
|
|
GTK_VALUE_ENUM (*arg) = label->jtype;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
arg->type = GTK_TYPE_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-08 19:02:40 +00:00
|
|
|
static void
|
1997-11-24 22:37:52 +00:00
|
|
|
gtk_label_init (GtkLabel *label)
|
|
|
|
{
|
|
|
|
GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
|
|
|
|
|
|
|
|
label->label = NULL;
|
|
|
|
label->row = NULL;
|
|
|
|
label->jtype = GTK_JUSTIFY_CENTER;
|
1998-04-13 00:20:17 +00:00
|
|
|
label->needs_clear = 0;
|
1998-02-20 05:46:48 +00:00
|
|
|
|
|
|
|
gtk_label_set (label, "");
|
1997-11-24 22:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget*
|
|
|
|
gtk_label_new (const char *str)
|
|
|
|
{
|
|
|
|
GtkLabel *label;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str != NULL, NULL);
|
|
|
|
|
|
|
|
label = gtk_type_new (gtk_label_get_type ());
|
|
|
|
|
|
|
|
gtk_label_set (label, str);
|
|
|
|
|
|
|
|
return GTK_WIDGET (label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_label_set (GtkLabel *label,
|
|
|
|
const char *str)
|
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
|
|
|
|
g_return_if_fail (label != NULL);
|
|
|
|
g_return_if_fail (GTK_IS_LABEL (label));
|
|
|
|
g_return_if_fail (str != NULL);
|
|
|
|
|
|
|
|
if (label->label)
|
|
|
|
g_free (label->label);
|
|
|
|
label->label = g_strdup (str);
|
|
|
|
|
|
|
|
if (label->row)
|
|
|
|
g_slist_free (label->row);
|
|
|
|
label->row = NULL;
|
|
|
|
label->row = g_slist_append (label->row, label->label);
|
|
|
|
p = label->label;
|
|
|
|
while ((p = strchr(p, '\n')))
|
|
|
|
label->row = g_slist_append (label->row, ++p);
|
|
|
|
|
|
|
|
if (GTK_WIDGET_VISIBLE (label))
|
|
|
|
{
|
|
|
|
if (GTK_WIDGET_MAPPED (label))
|
|
|
|
gdk_window_clear_area (GTK_WIDGET (label)->window,
|
|
|
|
GTK_WIDGET (label)->allocation.x,
|
|
|
|
GTK_WIDGET (label)->allocation.y,
|
|
|
|
GTK_WIDGET (label)->allocation.width,
|
|
|
|
GTK_WIDGET (label)->allocation.height);
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (label));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_label_set_justify (GtkLabel *label, GtkJustification jtype)
|
|
|
|
{
|
|
|
|
g_return_if_fail (label != NULL);
|
|
|
|
g_return_if_fail (GTK_IS_LABEL (label));
|
|
|
|
|
|
|
|
if ((GtkJustification) label->jtype != jtype)
|
|
|
|
{
|
|
|
|
label->jtype = jtype;
|
|
|
|
|
|
|
|
if (GTK_WIDGET_VISIBLE (label))
|
|
|
|
{
|
|
|
|
if (GTK_WIDGET_MAPPED (label))
|
|
|
|
gdk_window_clear_area (GTK_WIDGET (label)->window,
|
|
|
|
GTK_WIDGET (label)->allocation.x,
|
|
|
|
GTK_WIDGET (label)->allocation.y,
|
|
|
|
GTK_WIDGET (label)->allocation.width,
|
|
|
|
GTK_WIDGET (label)->allocation.height);
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (label));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_label_get (GtkLabel *label,
|
|
|
|
char **str)
|
|
|
|
{
|
|
|
|
g_return_if_fail (label != NULL);
|
|
|
|
g_return_if_fail (GTK_IS_LABEL (label));
|
|
|
|
g_return_if_fail (str != NULL);
|
|
|
|
|
|
|
|
*str = label->label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1998-01-30 23:47:09 +00:00
|
|
|
gtk_label_finalize (GtkObject *object)
|
1997-11-24 22:37:52 +00:00
|
|
|
{
|
|
|
|
GtkLabel *label;
|
1998-01-30 23:47:09 +00:00
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
g_return_if_fail (object != NULL);
|
|
|
|
g_return_if_fail (GTK_IS_LABEL (object));
|
1998-01-30 23:47:09 +00:00
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
label = GTK_LABEL (object);
|
1998-01-30 23:47:09 +00:00
|
|
|
|
1997-11-24 22:37:52 +00:00
|
|
|
g_free (label->label);
|
|
|
|
g_slist_free (label->row);
|
1998-01-30 23:47:09 +00:00
|
|
|
|
|
|
|
(* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
|
1997-11-24 22:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_label_size_request (GtkWidget *widget,
|
|
|
|
GtkRequisition *requisition)
|
|
|
|
{
|
|
|
|
GtkLabel *label;
|
|
|
|
GSList *row;
|
|
|
|
gint width;
|
|
|
|
|
|
|
|
g_return_if_fail (widget != NULL);
|
|
|
|
g_return_if_fail (GTK_IS_LABEL (widget));
|
|
|
|
g_return_if_fail (requisition != NULL);
|
|
|
|
|
|
|
|
label = GTK_LABEL (widget);
|
|
|
|
|
|
|
|
row = label->row;
|
|
|
|
width = 0;
|
|
|
|
while (row)
|
|
|
|
{
|
|
|
|
if (row->next)
|
|
|
|
width = MAX (width,
|
1998-04-07 23:29:04 +00:00
|
|
|
gdk_text_width (GTK_WIDGET (label)->style->font,
|
|
|
|
row->data,
|
1998-04-15 04:00:45 +00:00
|
|
|
(gchar*) row->next->data - (gchar*) row->data - 1));
|
1997-11-24 22:37:52 +00:00
|
|
|
else
|
|
|
|
width = MAX (width, gdk_string_width (GTK_WIDGET (label)->style->font, row->data));
|
|
|
|
row = row->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
requisition->width = width + label->misc.xpad * 2;
|
|
|
|
requisition->height = ((GTK_WIDGET (label)->style->font->ascent +
|
|
|
|
GTK_WIDGET (label)->style->font->descent + 2) *
|
|
|
|
g_slist_length(label->row) +
|
|
|
|
label->misc.ypad * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gtk_label_expose (GtkWidget *widget,
|
|
|
|
GdkEventExpose *event)
|
|
|
|
{
|
|
|
|
GtkLabel *label;
|
|
|
|
GtkMisc *misc;
|
|
|
|
GSList *row;
|
|
|
|
gint state;
|
|
|
|
gint offset;
|
|
|
|
gint len;
|
|
|
|
gint maxl;
|
|
|
|
gint x, y;
|
|
|
|
|
|
|
|
g_return_val_if_fail (widget != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
|
|
|
|
g_return_val_if_fail (event != NULL, FALSE);
|
|
|
|
|
|
|
|
if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
|
|
|
|
{
|
|
|
|
label = GTK_LABEL (widget);
|
|
|
|
misc = GTK_MISC (widget);
|
|
|
|
|
|
|
|
state = widget->state;
|
|
|
|
|
1997-11-25 06:17:49 +00:00
|
|
|
maxl = widget->requisition.width - misc->xpad * 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GC Clipping
|
1997-11-24 22:37:52 +00:00
|
|
|
*/
|
1997-11-25 06:17:49 +00:00
|
|
|
gdk_gc_set_clip_rectangle (widget->style->white_gc, &event->area);
|
|
|
|
gdk_gc_set_clip_rectangle (widget->style->fg_gc[state], &event->area);
|
|
|
|
|
1998-04-13 00:20:17 +00:00
|
|
|
/* We clear the whole allocation here so that if a partial
|
|
|
|
* expose is triggered we don't just clear part and mess up
|
|
|
|
* when the queued redraw comes along. (There will always
|
|
|
|
* be a complete queued redraw when the needs_clear flag
|
|
|
|
* is set.)
|
|
|
|
*/
|
|
|
|
if (label->needs_clear)
|
|
|
|
{
|
|
|
|
gdk_window_clear_area (widget->window,
|
|
|
|
widget->allocation.x,
|
|
|
|
widget->allocation.y,
|
|
|
|
widget->allocation.width,
|
|
|
|
widget->allocation.height);
|
|
|
|
|
|
|
|
label->needs_clear = FALSE;
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:17:49 +00:00
|
|
|
x = widget->allocation.x + misc->xpad +
|
|
|
|
(widget->allocation.width - widget->requisition.width)
|
|
|
|
* misc->xalign + 0.5;
|
|
|
|
|
|
|
|
y = (widget->allocation.y * (1.0 - misc->yalign) +
|
|
|
|
(widget->allocation.y + widget->allocation.height -
|
|
|
|
(widget->requisition.height - misc->ypad * 2)) *
|
|
|
|
misc->yalign + widget->style->font->ascent) + 1.5;
|
1997-11-24 22:37:52 +00:00
|
|
|
|
1997-11-25 06:17:49 +00:00
|
|
|
row = label->row;
|
|
|
|
while (row && row->next)
|
1997-11-24 22:37:52 +00:00
|
|
|
{
|
1998-04-07 23:29:04 +00:00
|
|
|
len = (gchar*) row->next->data - (gchar*) row->data - 1;
|
1997-11-25 06:17:49 +00:00
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
if (label->jtype == GTK_JUSTIFY_CENTER)
|
|
|
|
offset = (maxl - gdk_text_width (widget->style->font, row->data, len)) / 2;
|
|
|
|
|
|
|
|
else if (label->jtype == GTK_JUSTIFY_RIGHT)
|
|
|
|
offset = (maxl - gdk_text_width (widget->style->font, row->data, len));
|
|
|
|
|
|
|
|
if (state == GTK_STATE_INSENSITIVE)
|
|
|
|
gdk_draw_text (widget->window, widget->style->font,
|
|
|
|
widget->style->white_gc,
|
|
|
|
offset + x + 1, y + 1, row->data, len);
|
|
|
|
|
|
|
|
gdk_draw_text (widget->window, widget->style->font,
|
|
|
|
widget->style->fg_gc[state],
|
|
|
|
offset + x, y, row->data, len);
|
|
|
|
row = row->next;
|
|
|
|
y += widget->style->font->ascent + widget->style->font->descent + 2;
|
1997-11-24 22:37:52 +00:00
|
|
|
}
|
1997-11-25 06:17:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* COMMENT: we can avoid gdk_text_width() calls here storing in label->row
|
|
|
|
* the widths of the rows calculated in gtk_label_set.
|
|
|
|
* Once we have a wrapping interface we can support GTK_JUSTIFY_FILL.
|
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
if (label->jtype == GTK_JUSTIFY_CENTER)
|
|
|
|
offset = (maxl - gdk_string_width (widget->style->font, row->data)) / 2;
|
|
|
|
|
|
|
|
else if (label->jtype == GTK_JUSTIFY_RIGHT)
|
|
|
|
offset = (maxl - gdk_string_width (widget->style->font, row->data));
|
|
|
|
|
|
|
|
if (state == GTK_STATE_INSENSITIVE)
|
|
|
|
gdk_draw_string (widget->window, widget->style->font,
|
|
|
|
widget->style->white_gc,
|
|
|
|
offset + x + 1, y + 1, row->data);
|
|
|
|
|
|
|
|
gdk_draw_string (widget->window, widget->style->font,
|
|
|
|
widget->style->fg_gc[state],
|
|
|
|
offset + x, y, row->data);
|
|
|
|
|
|
|
|
gdk_gc_set_clip_mask (widget->style->white_gc, NULL);
|
|
|
|
gdk_gc_set_clip_mask (widget->style->fg_gc[state], NULL);
|
1997-11-24 22:37:52 +00:00
|
|
|
|
1997-11-25 06:17:49 +00:00
|
|
|
}
|
1997-11-24 22:37:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1998-03-29 02:44:43 +00:00
|
|
|
static void
|
|
|
|
gtk_label_state_changed (GtkWidget *widget,
|
|
|
|
guint previous_state)
|
|
|
|
{
|
1998-05-04 02:10:36 +00:00
|
|
|
if (GTK_WIDGET_DRAWABLE (widget))
|
|
|
|
GTK_LABEL (widget)->needs_clear = 1;
|
1998-03-29 02:44:43 +00:00
|
|
|
}
|
1997-11-24 22:37:52 +00:00
|
|
|
|
1998-03-29 02:44:43 +00:00
|
|
|
static void
|
|
|
|
gtk_label_style_set (GtkWidget *widget,
|
|
|
|
GtkStyle *previous_style)
|
|
|
|
{
|
1998-05-04 02:10:36 +00:00
|
|
|
if (GTK_WIDGET_DRAWABLE (widget))
|
|
|
|
GTK_LABEL (widget)->needs_clear = 1;
|
1998-03-29 02:44:43 +00:00
|
|
|
}
|
1998-04-09 02:55:46 +00:00
|
|
|
|