gtklabel.c 189 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25
#include "config.h"
26

27
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
28
#include <string.h>
29

Elliot Lee's avatar
Elliot Lee committed
30
#include "gtklabel.h"
31
#include "gtkaccellabel.h"
32
#include "gtkdnd.h"
33
#include "gtkmarshalers.h"
34
#include "gtkpango.h"
35
#include "gtkwindow.h"
36
#include "gtkclipboard.h"
37
#include "gtkimagemenuitem.h"
38
#include "gtkintl.h"
39
#include "gtkseparatormenuitem.h"
40
#include "gtktextutil.h"
41
#include "gtkmain.h"
42
#include "gtkmenuitem.h"
43
#include "gtkmenushellprivate.h"
44
#include "gtknotebook.h"
45
#include "gtkstock.h"
46
#include "gtkbindings.h"
47
#include "gtkbuildable.h"
48 49 50
#include "gtkimage.h"
#include "gtkshow.h"
#include "gtktooltip.h"
51
#include "gtkprivate.h"
52
#include "gtktypebuiltins.h"
53
#include "gtkmain.h"
54

55
#include "a11y/gtklabelaccessible.h"
56

Chun-wei Fan's avatar
Chun-wei Fan committed
57 58 59 60 61
/* this is in case rint() is not provided by the compiler, 
 * such as in the case of C89 compilers, like MSVC
 */
#include "fallback-c89.c"

62 63 64 65 66 67 68
/**
 * SECTION:gtklabel
 * @Short_description: A widget that displays a small to medium amount of text
 * @Title: GtkLabel
 *
 * The #GtkLabel widget displays a small amount of text. As the name
 * implies, most labels are used to label another widget such as a
69
 * #GtkButton, a #GtkMenuItem, or a #GtkComboBox.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
 *
 * <refsect2 id="GtkLabel-BUILDER-UI">
 * <title>GtkLabel as GtkBuildable</title>
 * <para>
 * The GtkLabel implementation of the GtkBuildable interface supports a
 * custom &lt;attributes&gt; element, which supports any number of &lt;attribute&gt;
 * elements. the &lt;attribute&gt; element has attributes named name, value,
 * start and end and allows you to specify #PangoAttribute values for this label.
 *
 * <example>
 * <title>A UI definition fragment specifying Pango attributes</title>
 * <programlisting><![CDATA[
 * <object class="GtkLabel">
 *   <attributes>
 *     <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
 *     <attribute name="background" value="red" start="5" end="10"/>"
 *   </attributes>
 * </object>
 * ]]></programlisting>
 * </example>
 * The start and end attributes specify the range of characters to which the
 * Pango attribute applies. If start and end are not specified, the attribute is
 * applied to the whole text. Note that specifying ranges does not make much
 * sense with translatable attributes. Use markup embedded in the translatable
 * content instead.
 * </para>
 * </refsect2>
 * <refsect2>
 * <title>Mnemonics</title>
 * <para>
 * Labels may contain <firstterm>mnemonics</firstterm>. Mnemonics are
 * underlined characters in the label, used for keyboard navigation.
 * Mnemonics are created by providing a string with an underscore before
 * the mnemonic character, such as <literal>"_File"</literal>, to the
 * functions gtk_label_new_with_mnemonic() or
 * gtk_label_set_text_with_mnemonic().
 *
 * Mnemonics automatically activate any activatable widget the label is
 * inside, such as a #GtkButton; if the label is not inside the
 * mnemonic's target widget, you have to tell the label about the target
 * using gtk_label_set_mnemonic_widget(). Here's a simple example where
 * the label is inside a button:
 *
 * <informalexample>
 * <programlisting>
 *   // Pressing Alt+H will activate this button
 *   button = gtk_button_new (<!-- -->);
 *   label = gtk_label_new_with_mnemonic ("_Hello");
 *   gtk_container_add (GTK_CONTAINER (button), label);
 * </programlisting>
 * </informalexample>
 *
 * There's a convenience function to create buttons with a mnemonic label
 * already inside:
 *
 * <informalexample>
 * <programlisting>
 *   // Pressing Alt+H will activate this button
 *   button = gtk_button_new_with_mnemonic ("_Hello");
 * </programlisting>
 * </informalexample>
 *
 * To create a mnemonic for a widget alongside the label, such as a
 * #GtkEntry, you have to point the label at the entry with
 * gtk_label_set_mnemonic_widget():
 *
 * <informalexample>
 * <programlisting>
 *   // Pressing Alt+H will focus the entry
 *   entry = gtk_entry_new (<!-- -->);
 *   label = gtk_label_new_with_mnemonic ("_Hello");
 *   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
 * </programlisting>
 * </informalexample>
 * </para>
 * </refsect2>
 * <refsect2>
 * <title>Markup (styled text)</title>
 * <para>
 * To make it easy to format text in a label (changing colors, fonts,
 * etc.), label text can be provided in a simple <link
 * linkend="PangoMarkupFormat">markup format</link>.
 * Here's how to create a label with a small font:
 *
 * <informalexample>
 * <programlisting>
 *   label = gtk_label_new (NULL);
 *   gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");
 * </programlisting>
 * </informalexample>
 *
 * (See <link
 * linkend="PangoMarkupFormat">complete documentation</link> of available
 * tags in the Pango manual.)
 *
 * The markup passed to gtk_label_set_markup() must be valid; for example,
 * literal &lt;, &gt; and &amp; characters must be escaped as \&lt;,
 * \gt;, and \&amp;. If you pass text obtained from the user, file,
 * or a network to gtk_label_set_markup(), you'll want to escape it with
 * g_markup_escape_text() or g_markup_printf_escaped().
 *
 * Markup strings are just a convenient way to set the #PangoAttrList on
 * a label; gtk_label_set_attributes() may be a simpler way to set
 * attributes in some cases. Be careful though; #PangoAttrList tends to
 * cause internationalization problems, unless you're applying attributes
 * to the entire string (i.e. unless you set the range of each attribute
 * to [0, %G_MAXINT)). The reason is that specifying the start_index and
 * end_index for a #PangoAttribute requires knowledge of the exact string
 * being displayed, so translations will cause problems.
 * </para>
 * </refsect2>
 * <refsect2>
 * <title>Selectable labels</title>
 * Labels can be made selectable with gtk_label_set_selectable().
 * Selectable labels allow the user to copy the label contents to
 * the clipboard. Only labels that contain useful-to-copy information
 * &mdash; such as error messages &mdash; should be made selectable.
 * </refsect2>
 * <refsect2 id="label-text-layout">
 * <title>Text layout</title>
 * <para>
 * A label can contain any number of paragraphs, but will have
 * performance problems if it contains more than a small number.
 * Paragraphs are separated by newlines or other paragraph separators
 * understood by Pango.
 *
 * Labels can automatically wrap text if you call
 * gtk_label_set_line_wrap().
 *
 * gtk_label_set_justify() sets how the lines in a label align
 * with one another. If you want to set how the label as a whole
 * aligns in its available space, see gtk_misc_set_alignment().
 *
 * The #GtkLabel:width-chars and #GtkLabel:max-width-chars properties
 * can be used to control the size allocation of ellipsized or wrapped
 * labels. For ellipsizing labels, if either is specified (and less
 * than the actual text size), it is used as the minimum width, and the actual
 * text size is used as the natural width of the label. For wrapping labels,
 * width-chars is used as the minimum width, if specified, and max-width-chars
 * is used as the natural width. Even if max-width-chars specified, wrapping
 * labels will be rewrapped to use all of the available width.
 *
 * <note><para>
 * Note that the interpretation of #GtkLabel:width-chars and
 * #GtkLabel:max-width-chars has changed a bit with the introduction of
 * <link linkend="geometry-management">width-for-height geometry management.</link>
 * </para></note>
 * </para>
 * </refsect2>
 * <refsect2>
 * <title>Links</title>
 * <para>
 * Since 2.18, GTK+ supports markup for clickable hyperlinks in addition
 * to regular Pango markup. The markup for links is borrowed from HTML, using the
 * <tag>a</tag> with href and title attributes. GTK+ renders links similar to the
 * way they appear in web browsers, with colored, underlined text. The title
 * attribute is displayed as a tooltip on the link. An example looks like this:
 *
 * <informalexample><programlisting>
 * gtk_label_set_markup (label, "Go to the <a href="http://www.gtk.org" title="&lt;i&gt;Our&lt;/i&gt; website">GTK+ website</a> for more...");
 * </programlisting></informalexample>
 *
 * It is possible to implement custom handling for links and their tooltips with
 * the #GtkLabel::activate-link signal and the gtk_label_get_current_uri() function.
 * </para>
 * </refsect2>
 */

238
struct _GtkLabelPrivate
239
{
240 241 242 243 244
  GtkLabelSelectionInfo *select_info;
  GtkWidget *mnemonic_widget;
  GtkWindow *mnemonic_window;

  PangoAttrList *attrs;
Benjamin Otte's avatar
Benjamin Otte committed
245
  PangoAttrList *markup_attrs;
246
  PangoLayout   *layout;
247

248 249 250
  gchar   *label;
  gchar   *text;

251 252
  gdouble  angle;

253
  guint    mnemonics_visible  : 1;
254 255 256 257 258 259 260 261 262 263 264
  guint    jtype              : 2;
  guint    wrap               : 1;
  guint    use_underline      : 1;
  guint    use_markup         : 1;
  guint    ellipsize          : 3;
  guint    single_line_mode   : 1;
  guint    have_transform     : 1;
  guint    in_click           : 1;
  guint    wrap_mode          : 3;
  guint    pattern_set        : 1;
  guint    track_links        : 1;
265 266 267 268 269 270

  guint    mnemonic_keyval;

  gint     width_chars;
  gint     max_width_chars;
};
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

/* Notes about the handling of links:
 *
 * Links share the GtkLabelSelectionInfo struct with selectable labels.
 * There are some new fields for links. The links field contains the list
 * of GtkLabelLink structs that describe the links which are embedded in
 * the label. The active_link field points to the link under the mouse
 * pointer. For keyboard navigation, the 'focus' link is determined by
 * finding the link which contains the selection_anchor position.
 * The link_clicked field is used with button press and release events
 * to ensure that pressing inside a link and releasing outside of it
 * does not activate the link.
 *
 * Links are rendered with the link-color/visited-link-color colors
 * that are determined by the style and with an underline. When the mouse
 * pointer is over a link, the pointer is changed to indicate the link,
 * and the background behind the link is rendered with the base[PRELIGHT]
 * color. While a button is pressed over a link, the background is rendered
 * with the base[ACTIVE] color.
 *
 * Labels with links accept keyboard focus, and it is possible to move
 * the focus between the embedded links using Tab/Shift-Tab. The focus
 * is indicated by a focus rectangle that is drawn around the link text.
 * Pressing Enter activates the focussed link, and there is a suitable
 * context menu for links that can be opened with the Menu key. Pressing
 * Control-C copies the link URI to the clipboard.
 *
 * In selectable labels with links, link functionality is only available
 * when the selection is empty.
 */
typedef struct
{
  gchar *uri;
  gchar *title;     /* the title attribute, used as tooltip */
  gboolean visited; /* get set when the link is activated; this flag
                     * gets preserved over later set_markup() calls
                     */
  gint start;       /* position of the link in the PangoLayout */
  gint end;
} GtkLabelLink;
311

312 313 314 315 316
struct _GtkLabelSelectionInfo
{
  GdkWindow *window;
  gint selection_anchor;
  gint selection_end;
317
  GtkWidget *popup_menu;
318 319 320 321

  GList *links;
  GtkLabelLink *active_link;

322 323 324
  gint drag_start_x;
  gint drag_start_y;

325
  guint in_drag      : 1;
326
  guint select_words : 1;
327 328
  guint selectable   : 1;
  guint link_clicked : 1;
329
};
Elliot Lee's avatar
Elliot Lee committed
330

331 332 333 334
enum {
  MOVE_CURSOR,
  COPY_CLIPBOARD,
  POPULATE_POPUP,
335
  ACTIVATE_LINK,
336
  ACTIVATE_CURRENT_LINK,
337 338
  LAST_SIGNAL
};
339

340
enum {
341 342 343 344 345 346 347 348
  PROP_0,
  PROP_LABEL,
  PROP_ATTRIBUTES,
  PROP_USE_MARKUP,
  PROP_USE_UNDERLINE,
  PROP_JUSTIFY,
  PROP_PATTERN,
  PROP_WRAP,
349
  PROP_WRAP_MODE,
350
  PROP_SELECTABLE,
351
  PROP_MNEMONIC_KEYVAL,
352 353
  PROP_MNEMONIC_WIDGET,
  PROP_CURSOR_POSITION,
354
  PROP_SELECTION_BOUND,
355
  PROP_ELLIPSIZE,
356
  PROP_WIDTH_CHARS,
357
  PROP_SINGLE_LINE_MODE,
358
  PROP_ANGLE,
359 360
  PROP_MAX_WIDTH_CHARS,
  PROP_TRACK_VISITED_LINKS
361 362
};

363 364 365
/* When rotating ellipsizable text we want the natural size to request 
 * more to ensure the label wont ever ellipsize in an allocation of full natural size.
 * */
366
#define ROTATION_ELLIPSIZE_PADDING 2
367

368 369
static guint signals[LAST_SIGNAL] = { 0 };

370 371 372
static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };

373 374 375
static void gtk_label_set_property      (GObject          *object,
					 guint             prop_id,
					 const GValue     *value,
376
					 GParamSpec       *pspec);
377 378 379
static void gtk_label_get_property      (GObject          *object,
					 guint             prop_id,
					 GValue           *value,
380
					 GParamSpec       *pspec);
381
static void gtk_label_finalize          (GObject          *object);
382
static void gtk_label_destroy           (GtkWidget        *widget);
383 384
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
385 386
static void gtk_label_state_flags_changed   (GtkWidget        *widget,
                                             GtkStateFlags     prev_state);
387
static void gtk_label_style_updated     (GtkWidget        *widget);
388 389
static void gtk_label_direction_changed (GtkWidget        *widget,
					 GtkTextDirection  previous_dir);
390 391
static gint gtk_label_draw              (GtkWidget        *widget,
                                         cairo_t          *cr);
392 393
static gboolean gtk_label_focus         (GtkWidget         *widget,
                                         GtkDirectionType   direction);
Elliot Lee's avatar
Elliot Lee committed
394

395 396 397 398
static void gtk_label_realize           (GtkWidget        *widget);
static void gtk_label_unrealize         (GtkWidget        *widget);
static void gtk_label_map               (GtkWidget        *widget);
static void gtk_label_unmap             (GtkWidget        *widget);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
399 400 401 402 403 404 405

static gboolean gtk_label_button_press      (GtkWidget        *widget,
					     GdkEventButton   *event);
static gboolean gtk_label_button_release    (GtkWidget        *widget,
					     GdkEventButton   *event);
static gboolean gtk_label_motion            (GtkWidget        *widget,
					     GdkEventMotion   *event);
406 407 408
static gboolean gtk_label_leave_notify      (GtkWidget        *widget,
                                             GdkEventCrossing *event);

409
static void     gtk_label_grab_focus        (GtkWidget        *widget);
410

411 412 413 414 415
static gboolean gtk_label_query_tooltip     (GtkWidget        *widget,
                                             gint              x,
                                             gint              y,
                                             gboolean          keyboard_tip,
                                             GtkTooltip       *tooltip);
416 417 418 419 420 421 422 423 424 425 426 427

static void gtk_label_set_text_internal          (GtkLabel      *label,
						  gchar         *str);
static void gtk_label_set_label_internal         (GtkLabel      *label,
						  gchar         *str);
static void gtk_label_set_use_markup_internal    (GtkLabel      *label,
						  gboolean       val);
static void gtk_label_set_use_underline_internal (GtkLabel      *label,
						  gboolean       val);
static void gtk_label_set_uline_text_internal    (GtkLabel      *label,
						  const gchar   *str);
static void gtk_label_set_pattern_internal       (GtkLabel      *label,
428 429
				                  const gchar   *pattern,
                                                  gboolean       is_mnemonic);
430
static void gtk_label_set_markup_internal        (GtkLabel      *label,
431 432 433
						  const gchar   *str,
						  gboolean       with_uline);
static void gtk_label_recalculate                (GtkLabel      *label);
434 435
static void gtk_label_hierarchy_changed          (GtkWidget     *widget,
						  GtkWidget     *old_toplevel);
436 437
static void gtk_label_screen_changed             (GtkWidget     *widget,
						  GdkScreen     *old_screen);
438
static gboolean gtk_label_popup_menu             (GtkWidget     *widget);
439

440 441
static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
442 443 444
static void gtk_label_ensure_select_info  (GtkLabel *label);
static void gtk_label_clear_select_info   (GtkLabel *label);
static void gtk_label_update_cursor       (GtkLabel *label);
445
static void gtk_label_clear_layout        (GtkLabel *label);
446
static void gtk_label_ensure_layout       (GtkLabel *label);
447 448 449 450
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);

451

452 453 454 455
static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
					     gboolean           group_cycling);
static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
					     guint              last_key);
456 457 458 459 460 461
static void     gtk_label_drag_data_get     (GtkWidget         *widget,
					     GdkDragContext    *context,
					     GtkSelectionData  *selection_data,
					     guint              info,
					     guint              time);

462 463 464 465 466 467 468 469 470 471 472 473 474 475
static void     gtk_label_buildable_interface_init     (GtkBuildableIface *iface);
static gboolean gtk_label_buildable_custom_tag_start   (GtkBuildable     *buildable,
							GtkBuilder       *builder,
							GObject          *child,
							const gchar      *tagname,
							GMarkupParser    *parser,
							gpointer         *data);

static void     gtk_label_buildable_custom_finished    (GtkBuildable     *buildable,
							GtkBuilder       *builder,
							GObject          *child,
							const gchar      *tagname,
							gpointer          user_data);

476

477
static void connect_mnemonics_visible_notify    (GtkLabel   *label);
478 479 480 481
static gboolean      separate_uline_pattern     (const gchar  *str,
                                                 guint        *accel_key,
                                                 gchar       **new_str,
                                                 gchar       **pattern);
482 483


484
/* For selectable labels: */
485 486 487 488 489 490 491 492 493 494 495 496
static void gtk_label_move_cursor        (GtkLabel        *label,
					  GtkMovementStep  step,
					  gint             count,
					  gboolean         extend_selection);
static void gtk_label_copy_clipboard     (GtkLabel        *label);
static void gtk_label_select_all         (GtkLabel        *label);
static void gtk_label_do_popup           (GtkLabel        *label,
					  GdkEventButton  *event);
static gint gtk_label_move_forward_word  (GtkLabel        *label,
					  gint             start);
static gint gtk_label_move_backward_word (GtkLabel        *label,
					  gint             start);
497

498 499
/* For links: */
static void          gtk_label_clear_links      (GtkLabel  *label);
500 501 502
static gboolean      gtk_label_activate_link    (GtkLabel    *label,
                                                 const gchar *uri);
static void          gtk_label_activate_current_link (GtkLabel *label);
503 504
static GtkLabelLink *gtk_label_get_current_link (GtkLabel  *label);
static void          gtk_label_get_link_colors  (GtkWidget  *widget,
505 506
                                                 GdkColor   *link_color,
                                                 GdkColor   *visited_link_color);
507 508
static void          emit_activate_link         (GtkLabel     *label,
                                                 GtkLabelLink *link);
509

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
static GtkSizeRequestMode gtk_label_get_request_mode                (GtkWidget           *widget);
static void               gtk_label_get_preferred_width             (GtkWidget           *widget,
                                                                     gint                *minimum_size,
                                                                     gint                *natural_size);
static void               gtk_label_get_preferred_height            (GtkWidget           *widget,
                                                                     gint                *minimum_size,
                                                                     gint                *natural_size);
static void               gtk_label_get_preferred_width_for_height  (GtkWidget           *widget,
                                                                     gint                 height,
                                                                     gint                *minimum_width,
                                                                     gint                *natural_width);
static void               gtk_label_get_preferred_height_for_width  (GtkWidget           *widget,
                                                                     gint                 width,
                                                                     gint                *minimum_height,
                                                                     gint                *natural_height);
525

526 527 528 529
static GtkBuildableIface *buildable_parent_iface = NULL;

G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
530
						gtk_label_buildable_interface_init))
Elliot Lee's avatar
Elliot Lee committed
531

532 533 534 535 536 537 538 539 540 541
static void
add_move_binding (GtkBindingSet  *binding_set,
		  guint           keyval,
		  guint           modmask,
		  GtkMovementStep step,
		  gint            count)
{
  g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
  
  gtk_binding_entry_add_signal (binding_set, keyval, modmask,
542
				"move-cursor", 3,
Manish Singh's avatar
Manish Singh committed
543
				G_TYPE_ENUM, step,
544
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
545
				G_TYPE_BOOLEAN, FALSE);
546 547 548

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
549
				"move-cursor", 3,
Manish Singh's avatar
Manish Singh committed
550
				G_TYPE_ENUM, step,
551
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
552
				G_TYPE_BOOLEAN, TRUE);
553 554
}

555
static void
Elliot Lee's avatar
Elliot Lee committed
556 557
gtk_label_class_init (GtkLabelClass *class)
{
558
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
559
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
560
  GtkBindingSet *binding_set;
561

562 563
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
564
  gobject_class->finalize = gtk_label_finalize;
565

566
  widget_class->destroy = gtk_label_destroy;
567
  widget_class->size_allocate = gtk_label_size_allocate;
568
  widget_class->state_flags_changed = gtk_label_state_flags_changed;
569
  widget_class->style_updated = gtk_label_style_updated;
570
  widget_class->query_tooltip = gtk_label_query_tooltip;
571
  widget_class->direction_changed = gtk_label_direction_changed;
572
  widget_class->draw = gtk_label_draw;
573 574 575 576 577 578 579
  widget_class->realize = gtk_label_realize;
  widget_class->unrealize = gtk_label_unrealize;
  widget_class->map = gtk_label_map;
  widget_class->unmap = gtk_label_unmap;
  widget_class->button_press_event = gtk_label_button_press;
  widget_class->button_release_event = gtk_label_button_release;
  widget_class->motion_notify_event = gtk_label_motion;
580
  widget_class->leave_notify_event = gtk_label_leave_notify;
581
  widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
582
  widget_class->screen_changed = gtk_label_screen_changed;
583
  widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
584
  widget_class->drag_data_get = gtk_label_drag_data_get;
585
  widget_class->grab_focus = gtk_label_grab_focus;
586 587
  widget_class->popup_menu = gtk_label_popup_menu;
  widget_class->focus = gtk_label_focus;
588 589 590 591 592
  widget_class->get_request_mode = gtk_label_get_request_mode;
  widget_class->get_preferred_width = gtk_label_get_preferred_width;
  widget_class->get_preferred_height = gtk_label_get_preferred_height;
  widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
593 594 595

  class->move_cursor = gtk_label_move_cursor;
  class->copy_clipboard = gtk_label_copy_clipboard;
596 597
  class->activate_link = gtk_label_activate_link;

Matthias Clasen's avatar
Matthias Clasen committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611
  /**
   * GtkLabel::move-cursor:
   * @entry: the object which received the signal
   * @step: the granularity of the move, as a #GtkMovementStep
   * @count: the number of @step units to move
   * @extend_selection: %TRUE if the move should extend the selection
   *
   * The ::move-cursor signal is a
   * <link linkend="keybinding-signals">keybinding signal</link>
   * which gets emitted when the user initiates a cursor movement.
   * If the cursor is not visible in @entry, this signal causes
   * the viewport to be moved instead.
   *
   * Applications should not connect to it, but may emit it with
612
   * g_signal_emit_by_name() if they need to control the cursor
Matthias Clasen's avatar
Matthias Clasen committed
613 614 615 616 617 618 619 620 621 622 623 624
   * programmatically.
   *
   * The default bindings for this signal come in two variants,
   * the variant with the Shift modifier extends the selection,
   * the variant without the Shift modifer does not.
   * There are too many key combinations to list them all here.
   * <itemizedlist>
   * <listitem>Arrow keys move by individual characters/lines</listitem>
   * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
   * <listitem>Home/End keys move to the ends of the buffer</listitem>
   * </itemizedlist>
   */
625
  signals[MOVE_CURSOR] = 
626
    g_signal_new (I_("move-cursor"),
Manish Singh's avatar
Manish Singh committed
627 628 629 630 631 632 633 634 635
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
		  G_TYPE_NONE, 3,
		  GTK_TYPE_MOVEMENT_STEP,
		  G_TYPE_INT,
		  G_TYPE_BOOLEAN);
Matthias Clasen's avatar
Matthias Clasen committed
636 637 638 639 640 641 642 643 644 645 646

   /**
   * GtkLabel::copy-clipboard:
   * @label: the object which received the signal
   *
   * The ::copy-clipboard signal is a
   * <link linkend="keybinding-signals">keybinding signal</link>
   * which gets emitted to copy the selection to the clipboard.
   *
   * The default binding for this signal is Ctrl-c.
   */ 
647
  signals[COPY_CLIPBOARD] =
648
    g_signal_new (I_("copy-clipboard"),
Manish Singh's avatar
Manish Singh committed
649 650 651 652 653 654
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
655
  
Matthias Clasen's avatar
Matthias Clasen committed
656 657 658 659 660 661 662 663 664 665 666 667
  /**
   * GtkLabel::populate-popup:
   * @label: The label on which the signal is emitted
   * @menu: the menu that is being populated
   *
   * The ::populate-popup signal gets emitted before showing the
   * context menu of the label. Note that only selectable labels
   * have context menus.
   *
   * If you need to add items to the context menu, connect
   * to this signal and append your menuitems to the @menu.
   */
668
  signals[POPULATE_POPUP] =
669
    g_signal_new (I_("populate-popup"),
Manish Singh's avatar
Manish Singh committed
670 671 672 673 674 675 676 677
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_MENU);

678
    /**
679 680
     * GtkLabel::activate-current-link:
     * @label: The label on which the signal was emitted
681
     *
682
     * A <link linkend="keybinding-signals">keybinding signal</link>
683 684 685 686 687 688 689
     * which gets emitted when the user activates a link in the label.
     *
     * Applications may also emit the signal with g_signal_emit_by_name()
     * if they need to control activation of URIs programmatically.
     *
     * The default bindings for this signal are all forms of the Enter key.
     *
690 691 692 693
     * Since: 2.18
     */
    signals[ACTIVATE_CURRENT_LINK] =
      g_signal_new_class_handler ("activate-current-link",
694
                                  G_TYPE_FROM_CLASS (gobject_class),
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
                                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                  G_CALLBACK (gtk_label_activate_current_link),
                                  NULL, NULL,
                                  _gtk_marshal_VOID__VOID,
                                  G_TYPE_NONE, 0);

    /**
     * GtkLabel::activate-link:
     * @label: The label on which the signal was emitted
     * @uri: the URI that is activated
     *
     * The signal which gets emitted to activate a URI.
     * Applications may connect to it to override the default behaviour,
     * which is to call gtk_show_uri().
     *
710 711 712 713 714 715
     * Returns: %TRUE if the link has been activated
     *
     * Since: 2.18
     */
    signals[ACTIVATE_LINK] =
      g_signal_new ("activate-link",
716
                    G_TYPE_FROM_CLASS (gobject_class),
717
                    G_SIGNAL_RUN_LAST,
718 719
                    G_STRUCT_OFFSET (GtkLabelClass, activate_link),
                    _gtk_boolean_handled_accumulator, NULL,
720 721
                    _gtk_marshal_BOOLEAN__STRING,
                    G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
722

Manish Singh's avatar
Manish Singh committed
723
  g_object_class_install_property (gobject_class,
724 725
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
726 727
                                                        P_("Label"),
                                                        P_("The text of the label"),
728
                                                        "",
729
                                                        GTK_PARAM_READWRITE));
730 731 732
  g_object_class_install_property (gobject_class,
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
733 734
						       P_("Attributes"),
						       P_("A list of style attributes to apply to the text of the label"),
735
						       PANGO_TYPE_ATTR_LIST,
736
						       GTK_PARAM_READWRITE));
737 738
  g_object_class_install_property (gobject_class,
                                   PROP_USE_MARKUP,
Matthias Clasen's avatar
x  
Matthias Clasen committed
739
                                   g_param_spec_boolean ("use-markup",
740 741
							 P_("Use markup"),
							 P_("The text of the label includes XML markup. See pango_parse_markup()"),
742
                                                        FALSE,
743
                                                        GTK_PARAM_READWRITE));
744 745
  g_object_class_install_property (gobject_class,
                                   PROP_USE_UNDERLINE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
746
                                   g_param_spec_boolean ("use-underline",
747 748
							 P_("Use underline"),
							 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
749
                                                        FALSE,
750
                                                        GTK_PARAM_READWRITE));
751 752 753 754

  g_object_class_install_property (gobject_class,
				   PROP_JUSTIFY,
                                   g_param_spec_enum ("justify",
755 756
                                                      P_("Justification"),
                                                      P_("The alignment of the lines in the text of the label relative to each other. This does NOT affect the alignment of the label within its allocation. See GtkMisc::xalign for that"),
757 758
						      GTK_TYPE_JUSTIFICATION,
						      GTK_JUSTIFY_LEFT,
759
                                                      GTK_PARAM_READWRITE));
760 761 762 763

  g_object_class_install_property (gobject_class,
                                   PROP_PATTERN,
                                   g_param_spec_string ("pattern",
764 765
                                                        P_("Pattern"),
                                                        P_("A string with _ characters in positions correspond to characters in the text to underline"),
766
                                                        NULL,
767
                                                        GTK_PARAM_WRITABLE));
768 769 770 771

  g_object_class_install_property (gobject_class,
                                   PROP_WRAP,
                                   g_param_spec_boolean ("wrap",
772 773
                                                        P_("Line wrap"),
                                                        P_("If set, wrap lines if the text becomes too wide"),
774
                                                        FALSE,
775
                                                        GTK_PARAM_READWRITE));
776 777 778
  /**
   * GtkLabel:wrap-mode:
   *
779 780 781
   * If line wrapping is on (see the #GtkLabel:wrap property) this controls 
   * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which 
   * means wrap on word boundaries.
782 783 784 785 786 787 788 789 790 791 792
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
                                   PROP_WRAP_MODE,
                                   g_param_spec_enum ("wrap-mode",
						      P_("Line wrap mode"),
						      P_("If wrap is set, controls how linewrapping is done"),
						      PANGO_TYPE_WRAP_MODE,
						      PANGO_WRAP_WORD,
						      GTK_PARAM_READWRITE));
793 794 795
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTABLE,
                                   g_param_spec_boolean ("selectable",
796 797
                                                        P_("Selectable"),
                                                        P_("Whether the label text can be selected with the mouse"),
798
                                                        FALSE,
799
                                                        GTK_PARAM_READWRITE));
800
  g_object_class_install_property (gobject_class,
801
                                   PROP_MNEMONIC_KEYVAL,
Matthias Clasen's avatar
x  
Matthias Clasen committed
802
                                   g_param_spec_uint ("mnemonic-keyval",
803 804
						      P_("Mnemonic key"),
						      P_("The mnemonic accelerator key for this label"),
805 806
						      0,
						      G_MAXUINT,
807
						      GDK_KEY_VoidSymbol,
808
						      GTK_PARAM_READABLE));
809 810
  g_object_class_install_property (gobject_class,
                                   PROP_MNEMONIC_WIDGET,
Matthias Clasen's avatar
x  
Matthias Clasen committed
811
                                   g_param_spec_object ("mnemonic-widget",
812 813
							P_("Mnemonic widget"),
							P_("The widget to be activated when the label's mnemonic "
814
							  "key is pressed"),
815
							GTK_TYPE_WIDGET,
816
							GTK_PARAM_READWRITE));
817

818 819
  g_object_class_install_property (gobject_class,
                                   PROP_CURSOR_POSITION,
Matthias Clasen's avatar
x  
Matthias Clasen committed
820
                                   g_param_spec_int ("cursor-position",
821 822
                                                     P_("Cursor Position"),
                                                     P_("The current position of the insertion cursor in chars"),
823 824 825
                                                     0,
                                                     G_MAXINT,
                                                     0,
826
                                                     GTK_PARAM_READABLE));
827 828 829
  
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTION_BOUND,
Matthias Clasen's avatar
x  
Matthias Clasen committed
830
                                   g_param_spec_int ("selection-bound",
831 832
                                                     P_("Selection Bound"),
                                                     P_("The position of the opposite end of the selection from the cursor in chars"),
833 834 835
                                                     0,
                                                     G_MAXINT,
                                                     0,
836
                                                     GTK_PARAM_READABLE));
837
  
838 839 840
  /**
   * GtkLabel:ellipsize:
   *
841 842
   * The preferred place to ellipsize the string, if the label does 
   * not have enough room to display the entire string, specified as a 
843
   * #PangoEllipsizeMode. 
844
   *
845 846 847 848 849 850 851
   * Note that setting this property to a value other than 
   * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests 
   * only enough space to display the ellipsis "...". In particular, this 
   * means that ellipsizing labels do not work well in notebook tabs, unless 
   * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
   * to set a label's width are gtk_widget_set_size_request() and
   * gtk_label_set_width_chars().
852 853 854
   *
   * Since: 2.6
   */
855 856 857 858
  g_object_class_install_property (gobject_class,
				   PROP_ELLIPSIZE,
                                   g_param_spec_enum ("ellipsize",
                                                      P_("Ellipsize"),
859
                                                      P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
860 861
						      PANGO_TYPE_ELLIPSIZE_MODE,
						      PANGO_ELLIPSIZE_NONE,
862
                                                      GTK_PARAM_READWRITE));
863 864 865

  /**
   * GtkLabel:width-chars:
866
   *
867
   * The desired width of the label, in characters. If this property is set to
868 869 870 871 872
   * -1, the width will be calculated automatically.
   *
   * See the section on <link linkend="label-text-layout">text layout</link>
   * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
   * determine the width of ellipsized and wrapped labels.
873
   *
874 875 876 877
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_WIDTH_CHARS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
878
                                   g_param_spec_int ("width-chars",
879
                                                     P_("Width In Characters"),
880 881 882 883
                                                     P_("The desired width of the label, in characters"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
884
                                                     GTK_PARAM_READWRITE));
885
  
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  /**
   * GtkLabel:single-line-mode:
   * 
   * Whether the label is in single line mode. In single line mode,
   * the height of the label does not depend on the actual text, it
   * is always set to ascent + descent of the font. This can be an
   * advantage in situations where resizing the label because of text 
   * changes would be distracting, e.g. in a statusbar.
   *
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_SINGLE_LINE_MODE,
                                   g_param_spec_boolean ("single-line-mode",
                                                        P_("Single Line Mode"),
                                                        P_("Whether the label is in single line mode"),
                                                        FALSE,
903
                                                        GTK_PARAM_READWRITE));
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

  /**
   * GtkLabel:angle:
   * 
   * The angle that the baseline of the label makes with the horizontal,
   * in degrees, measured counterclockwise. An angle of 90 reads from
   * from bottom to top, an angle of 270, from top to bottom. Ignored
   * if the label is selectable, wrapped, or ellipsized.
   *
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_ANGLE,
                                   g_param_spec_double ("angle",
							P_("Angle"),
							P_("Angle at which the label is rotated"),
							0.0,
							360.0,
							0.0, 
923
							GTK_PARAM_READWRITE));
924
  
925 926 927
  /**
   * GtkLabel:max-width-chars:
   * 
928
   * The desired maximum width of the label, in characters. If this property 
929 930 931 932 933 934
   * is set to -1, the width will be calculated automatically.
   *
   * See the section on <link linkend="label-text-layout">text layout</link>
   * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
   * determine the width of ellipsized and wrapped labels.
   *
935 936 937 938
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_MAX_WIDTH_CHARS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
939
                                   g_param_spec_int ("max-width-chars",
940 941
                                                     P_("Maximum Width In Characters"),
                                                     P_("The desired maximum width of the label, in characters"),
942 943 944
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
945
                                                     GTK_PARAM_READWRITE));
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962

  /**
   * GtkLabel:track-visited-links:
   *
   * Set this property to %TRUE to make the label track which links
   * have been clicked. It will then apply the ::visited-link-color
   * color, instead of ::link-color.
   *
   * Since: 2.18
   */
  g_object_class_install_property (gobject_class,
                                   PROP_TRACK_VISITED_LINKS,
                                   g_param_spec_boolean ("track-visited-links",
                                                         P_("Track visited links"),
                                                         P_("Whether visited links should be tracked"),
                                                         TRUE,
                                                         GTK_PARAM_READWRITE));
963 964 965 966 967 968 969
  /*
   * Key bindings
   */

  binding_set = gtk_binding_set_by_class (class);

  /* Moving the insertion point */
970
  add_move_binding (binding_set, GDK_KEY_Right, 0,
971 972
		    GTK_MOVEMENT_VISUAL_POSITIONS, 1);
  
973
  add_move_binding (binding_set, GDK_KEY_Left, 0,
974 975
		    GTK_MOVEMENT_VISUAL_POSITIONS, -1);

976
  add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
977 978
		    GTK_MOVEMENT_VISUAL_POSITIONS, 1);
  
979
  add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
980 981
		    GTK_MOVEMENT_VISUAL_POSITIONS, -1);
  
982
  add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
983 984
		    GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
  
985
  add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
986 987
		    GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
  
988
  add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
989 990
		    GTK_MOVEMENT_WORDS, 1);

991
  add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
992 993
		    GTK_MOVEMENT_WORDS, -1);

994
  add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
995 996
		    GTK_MOVEMENT_WORDS, 1);

997
  add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
998 999
		    GTK_MOVEMENT_WORDS, -1);

1000
  /* select all */
1001
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1002
				"move-cursor", 3,
1003 1004 1005 1006
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);

1007
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1008
				"move-cursor", 3,
1009 1010 1011 1012
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);

1013
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1014
				"move-cursor", 3,
1015 1016 1017 1018
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);

1019
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1020
				"move-cursor", 3,
1021 1022 1023 1024 1025
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);

  /* unselect all */
1026
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1027
				"move-cursor", 3,
1028 1029 1030 1031
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);

1032
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1033
				"move-cursor", 3,
1034 1035 1036
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);
1037

1038
  add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
1039 1040
		    GTK_MOVEMENT_WORDS, 1);

1041
  add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
1042 1043
		    GTK_MOVEMENT_WORDS, -1);

1044
  add_move_binding (binding_set, GDK_KEY_Home, 0,
1045 1046
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);

1047
  add_move_binding (binding_set, GDK_KEY_End, 0,
1048 1049
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);

1050
  add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1051 1052
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);

1053
  add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1054 1055
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
  
1056
  add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1057 1058
		    GTK_MOVEMENT_BUFFER_ENDS, -1);

1059
  add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1060 1061
		    GTK_MOVEMENT_BUFFER_ENDS, 1);

1062
  add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1063 1064
		    GTK_MOVEMENT_BUFFER_ENDS, -1);

1065
  add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1066 1067 1068
		    GTK_MOVEMENT_BUFFER_ENDS, 1);

  /* copy */
1069
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1070
				"copy-clipboard", 0);
1071

1072
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1073
				"activate-current-link", 0);
1074
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1075
				"activate-current-link", 0);
1076
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1077
				"activate-current-link", 0);
1078

1079
  g_type_class_add_private (class, sizeof (GtkLabelPrivate));
1080 1081

  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LABEL_ACCESSIBLE);
Elliot Lee's avatar
Elliot Lee committed
1082 1083
}

1084 1085 1086 1087
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
1088
			GParamSpec   *pspec)
1089
{
1090
  GtkLabel *label = GTK_LABEL (object);
1091

1092
  switch (prop_id)
1093
    {
1094
    case PROP_LABEL:
1095
      gtk_label_set_label (label, g_value_get_string (value));
1096 1097 1098 1099 1100
      break;
    case PROP_ATTRIBUTES:
      gtk_label_set_attributes (label, g_value_get_boxed (value));
      break;
    case PROP_USE_MARKUP:
1101
      gtk_label_set_use_markup (label, g_value_get_boolean (value));
1102
      break;
1103
    case PROP_USE_UNDERLINE:
1104
      gtk_label_set_use_underline (label, g_value_get_boolean (value));
1105
      break;
1106 1107
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
1108
      break;
1109 1110 1111 1112 1113 1114
    case PROP_PATTERN:
      gtk_label_set_pattern (label, g_value_get_string (value));
      break;
    case PROP_WRAP:
      gtk_label_set_line_wrap (label, g_value_get_boolean (value));
      break;	  
1115 1116 1117
    case PROP_WRAP_MODE:
      gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
      break;	  
1118 1119
    case PROP_SELECTABLE:
      gtk_label_set_selectable (label, g_value_get_boolean (value));
1120
      break;	  
1121 1122 1123
    case PROP_MNEMONIC_WIDGET:
      gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
      break;
1124 1125 1126
    case PROP_ELLIPSIZE:
      gtk_label_set_ellipsize (label, g_value_get_enum (value));
      break;
1127 1128 1129
    case PROP_WIDTH_CHARS:
      gtk_label_set_width_chars (label, g_value_get_int (value));
      break;
1130 1131 1132
    case PROP_SINGLE_LINE_MODE:
      gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
      break;	  
1133 1134
    case PROP_ANGLE:
      gtk_label_set_angle (label, g_value_get_double (value));
1135 1136 1137 1138
      break;
    case PROP_MAX_WIDTH_CHARS:
      gtk_label_set_max_width_chars (label, g_value_get_int (value));
      break;
1139 1140 1141
    case PROP_TRACK_VISITED_LINKS:
      gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
      break;
Tim Janik's avatar
Tim Janik committed
1142
    default:
1143
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
1144
      break;
1145 1146 1147
    }
}

1148 1149 1150 1151
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
1152
			GParamSpec  *pspec)
1153
{
1154
  GtkLabel *label = GTK_LABEL (object);
1155
  GtkLabelPrivate *priv = label->priv;
1156

1157
  switch (prop_id)
1158
    {
1159
    case PROP_LABEL:
1160
      g_value_set_string (value, priv->label);
1161 1162
      break;
    case PROP_ATTRIBUTES:
1163
      g_value_set_boxed (value, priv->attrs);
1164 1165
      break;
    case PROP_USE_MARKUP:
1166
      g_value_set_boolean (value, priv->use_markup);
1167 1168
      break;
    case PROP_USE_UNDERLINE:
1169
      g_value_set_boolean (value, priv->use_underline);
1170
      break;
1171
    case PROP_JUSTIFY:
1172
      g_value_set_enum (value, priv->jtype);
1173
      break;
1174
    case PROP_WRAP:
1175
      g_value_set_boolean (value, priv->wrap);
1176
      break;
1177
    case PROP_WRAP_MODE:
1178
      g_value_set_enum (value, priv->wrap_mode);
1179
      break;
1180 1181 1182
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
1183
    case PROP_MNEMONIC_KEYVAL:
1184
      g_value_set_uint (value, priv->mnemonic_keyval);
1185
      break;
1186
    case PROP_MNEMONIC_WIDGET:
1187
      g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1188
      break;
1189
    case PROP_CURSOR_POSITION:
1190
      g_value_set_int (value, _gtk_label_get_cursor_position (label));
1191 1192
      break;
    case PROP_SELECTION_BOUND:
1193
      g_value_set_int (value, _gtk_label_get_selection_bound (label));
1194
      break;
1195
    case PROP_ELLIPSIZE:
1196
      g_value_set_enum (value, priv->ellipsize);
1197
      break;
1198
    case PROP_WIDTH_CHARS:
1199
      g_value_set_int (value, gtk_label_get_width_chars (label));
1200
      break;
1201 1202 1203
    case PROP_SINGLE_LINE_MODE:
      g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
      break;
1204 1205 1206
    case PROP_ANGLE:
      g_value_set_double (value, gtk_label_get_angle (label));
      break;
1207 1208 1209
    case PROP_MAX_WIDTH_CHARS:
      g_value_set_int (value, gtk_label_get_max_width_chars (label));
      break;
1210 1211 1212
    case PROP_TRACK_VISITED_LINKS:
      g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
      break;
1213
    default:
1214
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1215 1216 1217 1218
      break;
    }
}

1219
static void
Elliot Lee's avatar
Elliot Lee committed
1220 1221
gtk_label_init (GtkLabel *label)
{
1222
  GtkLabelPrivate *priv;
1223 1224 1225

  label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
                                             GTK_TYPE_LABEL,
1226
                                             GtkLabelPrivate);
1227
  priv = label->priv;
1228

1229
  gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1230 1231

  priv->width_chars = -1;
1232
  priv->max_width_chars = -1;
1233
  priv->label = NULL;
1234

1235 1236 1237 1238
  priv->jtype = GTK_JUSTIFY_LEFT;
  priv->wrap = FALSE;
  priv->wrap_mode = PANGO_WRAP_WORD;
  priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1239

1240 1241 1242 1243
  priv->use_underline = FALSE;
  priv->use_markup = FALSE;
  priv->pattern_set = FALSE;
  priv->track_links = TRUE;
1244

1245
  priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1246 1247 1248
  priv->layout = NULL;
  priv->text = NULL;
  priv->attrs = NULL;
1249

1250 1251
  priv->mnemonic_widget = NULL;
  priv->mnemonic_window = NULL;
1252

1253 1254
  priv->mnemonics_visible = TRUE;

1255
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
1256 1257
}

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

static void
gtk_label_buildable_interface_init (GtkBuildableIface *iface)
{
  buildable_parent_iface = g_type_interface_peek_parent (iface);

  iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
  iface->custom_finished = gtk_label_buildable_custom_finished;
}

typedef struct {
  GtkBuilder    *builder;
  GObject       *object;
  PangoAttrList *attrs;
} PangoParserData;

Matthias Clasen's avatar
Matthias Clasen committed
1274
static PangoAttribute *
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
attribute_from_text (GtkBuilder   *builder,
		     const gchar  *name, 
		     const gchar  *value,
		     GError      **error)
{
  PangoAttribute *attribute = NULL;
  PangoAttrType   type;
  PangoLanguage  *language;
  PangoFontDescription *font_desc;
  GdkColor       *color;
Javier Jardon's avatar
Javier Jardon committed
1285
  GValue          val = G_VALUE_INIT;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

  if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
    return NULL;

  type = g_value_get_enum (&val);
  g_value_unset (&val);

  switch (type)
    {
      /* PangoAttrLanguage */
    case PANGO_ATTR_LANGUAGE:
      if ((language = pango_language_from_string (value)))
	{
	  attribute = pango_attr_language_new (language);
	  g_value_init (&val, G_TYPE_INT);
	}
      break;
      /* PangoAttrInt */
    case PANGO_ATTR_STYLE:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
	attribute = pango_attr_style_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_WEIGHT:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
	attribute = pango_attr_weight_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_VARIANT:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
	attribute = pango_attr_variant_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_STRETCH:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
	attribute = pango_attr_stretch_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_UNDERLINE:
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_UNDERLINE, value, &val, NULL))
	attribute = pango_attr_underline_new (g_value_get_enum (&val));
      else
        {
          /* XXX: allow boolean for backwards compat, so ignore error */
          /* Deprecate this somehow */
          g_value_unset (&val);
          if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
            attribute = pango_attr_underline_new (g_value_get_boolean (&val));
        }
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
      break;
    case PANGO_ATTR_STRIKETHROUGH:	
      if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
	attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
      break;
    case PANGO_ATTR_GRAVITY:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
	attribute = pango_attr_gravity_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_GRAVITY_HINT:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT, 
					      value, &val, error))
	attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
      break;
      /* PangoAttrString */	  
    case PANGO_ATTR_FAMILY:
      attribute = pango_attr_family_new (value);
      g_value_init (&val, G_TYPE_INT);
      break;

      /* PangoAttrSize */	  
    case PANGO_ATTR_SIZE:
      if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
					      value, &val, error))
	attribute = pango_attr_size_new (g_value_get_int (&val));
      break;
    case PANGO_ATTR_ABSOLUTE_SIZE:
      if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
					      value, &val, error))
	attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
      break;
    
      /* PangoAttrFontDesc */
    case PANGO_ATTR_FONT_DESC:
      if ((font_desc = pango_font_description_from_string (value)))
	{
	  attribute = pango_attr_font_desc_new (font_desc);
	  pango_font_description_free (font_desc);
	  g_value_init (&val, G_TYPE_INT);
	}
      break;

      /* PangoAttrColor */
    case PANGO_ATTR_FOREGROUND<