gtkrc.c 54.9 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/>.
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 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#include "config.h"
26 27 28

#include <locale.h>
#ifdef HAVE_UNISTD_H
29
#include <unistd.h>
30
#endif
31
#include <sys/stat.h>
32
#ifdef HAVE_SYS_PARAM_H
33
#include <sys/param.h>
34
#endif
35
#include <fcntl.h>
Elliot Lee's avatar
Elliot Lee committed
36
#include <string.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39

40
#define GDK_DISABLE_DEPRECATION_WARNINGS
41

Owen Taylor's avatar
Owen Taylor committed
42
#include <glib.h>
43
#include <glib/gstdio.h>
44
#include "gdk/gdk.h"
Owen Taylor's avatar
Owen Taylor committed
45

46
#include "gtkversion.h"
Elliot Lee's avatar
Elliot Lee committed
47
#include "gtkrc.h"
48
#include "gtkstyle.h"
Tim Janik's avatar
Tim Janik committed
49
#include "gtkbindings.h"
50
#include "gtkintl.h"
51
#include "gtkiconfactory.h"
52
#include "gtkmain.h"
53
#include "gtkmodules.h"
54
#include "gtkmodulesprivate.h"
55
#include "gtkprivate.h"
56
#include "gtksettingsprivate.h"
57
#include "gtkwidgetpath.h"
58
#include "gtkwindow.h"
Elliot Lee's avatar
Elliot Lee committed
59

Tor Lillqvist's avatar
Tor Lillqvist committed
60 61 62 63
#ifdef G_OS_WIN32
#include <io.h>
#endif

64 65 66 67 68 69 70 71 72 73

/**
 * SECTION:gtkrc
 * @Short_description: Deprecated routines for handling resource files
 * @Title: Resource Files
 *
 * GTK+ provides resource file mechanism for configuring
 * various aspects of the operation of a GTK+ program
 * at runtime.
 *
74 75
 * > In GTK+ 3.0, resource files have been deprecated and replaced by
 * > CSS-like style sheets, which are understood by #GtkCssProvider.
76
 *
77
 * # Default Files #
78
 *
79 80 81 82
 * An application can cause GTK+ to parse a specific RC
 * file by calling gtk_rc_parse(). In addition to this,
 * certain files will be read at the end of gtk_init().
 * Unless modified, the files looked for will be
83 84 85 86
 * `SYSCONFDIR/gtk-2.0/gtkrc`
 * and `.gtkrc-3.0` in the users home directory.
 * (`SYSCONFDIR` defaults to
 * `/usr/local/etc`. It can be changed with the
87
 * `--prefix` or `--sysconfdir` options when
88 89
 * configuring GTK+.)
 *
90
 * The set of these “default” files
91 92 93
 * can be retrieved with gtk_rc_get_default_files()
 * and modified with gtk_rc_add_default_file() and
 * gtk_rc_set_default_files().
94
 * Additionally, the `GTK2_RC_FILES` environment variable
95 96
 * can be set to a #G_SEARCHPATH_SEPARATOR_S-separated list of files
 * in order to overwrite the set of default files at runtime.
97 98 99
 *
 * # Locale Specific Files # {#locale-specific-rc}
 *
100 101
 * For each RC file, in addition to the file itself, GTK+ will look for
 * a locale-specific file that will be parsed after the main file.
102
 * For instance, if `LANG` is set to `ja_JP.ujis`,
103 104
 * when loading the default file `~/.gtkrc` then GTK+ looks
 * for `~/.gtkrc.ja_JP` and `~/.gtkrc.ja`,
105 106
 * and parses the first of those that exists.
 *
107
 * # Pathnames and Patterns #
108
 *
109 110
 * A resource file defines a number of styles and key bindings and
 * attaches them to particular widgets. The attachment is done
111 112
 * by the `widget`, `widget_class`,
 * and `class` declarations. As an example
113 114
 * of such a statement:
 *
115
 * |[
116
 * widget "mywindow.*.GtkEntry" style "my-entry-class"
117
 * ]|
118
 *
119
 * attaches the style `"my-entry-class"` to all
120
 * widgets  whose “widget path” matches the
121
 * “pattern” `"mywindow.*.GtkEntry"`.
122
 * That is, all #GtkEntry widgets which are part of a #GtkWindow named
123
 * `"mywindow"`.
124 125
 *
 * The patterns here are given in the standard shell glob syntax.
126 127
 * The `"?"` wildcard matches any character, while
 * `"*"` matches zero or more of any character.
128
 * The three types of matching are against the widget path, the
129
 * “class path” and the class hierarchy. Both the
130
 * widget path and the class path consist of a `"."`
131 132 133 134 135 136
 * separated list of all the parents of the widget and the widget itself
 * from outermost to innermost. The difference is that in the widget path,
 * the name assigned by gtk_widget_set_name() is used if present, otherwise
 * the class name of the widget, while for the class path, the class name is
 * always used.
 *
137 138 139
 * Since GTK+ 2.10, `widget_class` paths can also contain <classname>
 * substrings, which are matching the class with the given name and any
 * derived classes. For instance,
140
 * |[
141
 * widget_class "*<GtkMenuItem>.GtkLabel" style "my-style"
142
 * ]|
143 144
 * will match #GtkLabel widgets which are contained in any kind of menu item.
 *
145 146 147 148
 * So, if you have a #GtkEntry named `"myentry"`, inside of a horizontal
 * box in a window named `"mywindow"`, then the widget path is:
 * `"mywindow.GtkHBox.myentry"` while the class path is:
 * `"GtkWindow.GtkHBox.GtkEntry"`.
149 150 151 152
 *
 * Matching against class is a little different. The pattern match is done
 * against all class names in the widgets class hierarchy (not the layout
 * hierarchy) in sequence, so the pattern:
153
 * |[
154
 * class "GtkButton" style "my-style"
155
 * ]|
156 157 158 159 160 161 162
 * will match not just #GtkButton widgets, but also #GtkToggleButton and
 * #GtkCheckButton widgets, since those classes derive from #GtkButton.
 *
 * Additionally, a priority can be specified for each pattern, and styles
 * override other styles first by priority, then by pattern type and then
 * by order of specification (later overrides earlier). The priorities
 * that can be specified are (highest to lowest):
163
 *
164
 * - `highest`
165
 *
166
 * - `rc`
167
 *
168
 * - `theme`
169
 *
170
 * - `application`
171
 *
172
 * - `gtk`
173
 *
174
 * - `lowest`
175
 *
176 177
 * `rc` is the default for styles
 * read from an RC file, `theme`
178
 * is the default for styles read from theme RC files,
179
 * `application`
180
 * should be used for styles an application sets
181
 * up, and `gtk` is used for styles
182
 * that GTK+ creates internally.
183
 *
184
 * # Theme gtkrc Files #
185
 *
186
 * Theme RC files are loaded first from under the `~/.themes/`,
187
 * then from the directory from gtk_rc_get_theme_dir(). The files looked at will
188
 * be `gtk-3.0/gtkrc`.
189 190 191
 *
 * When the application prefers dark themes
 * (see the #GtkSettings:gtk-application-prefer-dark-theme property for details),
192 193
 * `gtk-3.0/gtkrc-dark` will be loaded first, and if not present
 * `gtk-3.0/gtkrc` will be loaded.
194 195 196
 *
 * # Optimizing RC Style Matches #
 *
197 198 199 200 201 202 203 204 205 206 207 208
 * Everytime a widget is created and added to the layout hierarchy of a #GtkWindow
 * ("anchored" to be exact), a list of matching RC styles out of all RC styles read
 * in so far is composed.
 * For this, every RC style is matched against the widgets class path,
 * the widgets name path and widgets inheritance hierarchy.
 * As a consequence, significant slowdown can be caused by utilization of many
 * RC styles and by using RC style patterns that are slow or complicated to match
 * against a given widget.
 * The following ordered list provides a number of advices (prioritized by
 * effectiveness) to reduce the performance overhead associated with RC style
 * matches:
 *
209
 * 1. Move RC styles for specific applications into RC files dedicated to those
210 211 212 213
 *   applications and parse application specific RC files only from
 *   applications that are affected by them.
 *   This reduces the overall amount of RC styles that have to be considered
 *   for a match across a group of applications.
214
 *
215
 * 2.  Merge multiple styles which use the same matching rule, for instance:
216
 *   |[
217 218 219 220
 *      style "Foo" { foo_content }
 *      class "X" style "Foo"
 *      style "Bar" { bar_content }
 *      class "X" style "Bar"
221
 *   ]|
222
 *   is faster to match as:
223
 *   |[
224 225
 *      style "FooBar" { foo_content bar_content }
 *      class "X" style "FooBar"
226
 *   ]|
227 228
 *
 * 3. Use of wildcards should be avoided, this can reduce the individual RC style
229
 *   match to a single integer comparison in most cases.
230 231
 *
 * 4. To avoid complex recursive matching, specification of full class names
232 233
 *   (for `class` matches) or full path names (for
 *   `widget` and `widget_class` matches)
234
 *   is to be preferred over shortened names
235
 *   containing `"*"` or `"?"`.
236 237
 *
 * 5. If at all necessary, wildcards should only be used at the tail or head
238 239
 *   of a pattern. This reduces the match complexity to a string comparison
 *   per RC style.
240
 *
241 242 243 244
 * 6. When using wildcards, use of `"?"` should be preferred
 *   over `"*"`. This can reduce the matching complexity from
 *   O(n^2) to O(n). For example `"Gtk*Box"` can be turned into
 *   `"Gtk?Box"` and will still match #GtkHBox and #GtkVBox.
245
 *
246 247
 * 7. The use of `"*"` wildcards should be restricted as much
 *   as possible, because matching `"A*B*C*RestString"` can
248
 *   result in matching complexities of O(n^2) worst case.
249
 *
250
 * # Toplevel Declarations #
251
 *
252
 * An RC file is a text file which is composed of a sequence
253 254
 * of declarations. `“#”` characters delimit comments and
 * the portion of a line after a `“#”` is ignored when parsing
255 256 257 258
 * an RC file.
 *
 * The possible toplevel declarations are:
 *
259
 * * `binding name
260
 *      { ... }`
261 262 263
 *
 *    Declares a binding set.
 *
264 265 266
 * * `class pattern
 *           [ style | binding ][ : priority ]
 *           name`
267 268 269 270
 *
 *    Specifies a style or binding set for a particular
 *      branch of the inheritance hierarchy.
 *
271
 * * `include filename`
272 273
 *
 *    Parses another file at this point. If
274
 *         filename is not an absolute filename,
275 276 277
 *         it is searched in the directories of the currently open RC files.
 *
 *    GTK+ also tries to load a
278
 *         [locale-specific variant][locale-specific-rc] of
279 280
 *         the included file.
 *
281
 * * `module_path path`
282 283
 *
 *    Sets a path (a list of directories separated
284
 *       by colons) that will be searched for theme engines referenced in
285 286
 *       RC files.
 *
287
 * * `pixmap_path path`
288 289
 *
 *    Sets a path (a list of directories separated
290
 *       by colons) that will be searched for pixmaps referenced in
291 292
 *       RC files.
 *
293
 * * `im_module_file pathname`
294 295
 *
 *    Sets the pathname for the IM modules file. Setting this from RC files
296
 *       is deprecated; you should use the environment variable `GTK_IM_MODULE_FILE`
297 298
 *       instead.
 *
299 300
 * * `style name [ =
 *     parent ] { ... }`
301 302 303
 *
 *    Declares a style.
 *
304 305 306
 * * `widget pattern
 *           [ style | binding ][ : priority ]
 *           name`
307 308 309 310
 *
 *      Specifies a style or binding set for a particular
 *      group of widgets by matching on the widget pathname.
 *
311 312 313
 * * `widget_class pattern
 *           [ style | binding ][ : priority ]
 *           name`
314 315 316 317
 *
 *      Specifies a style or binding set for a particular
 *      group of widgets by matching on the class pathname.
 *
318
 * * setting = value
319
 *
320
 *    Specifies a value for a [setting][GtkSettings].
321
 *         Note that settings in RC files are overwritten by system-wide settings
322 323 324 325
 *         (which are managed by an XSettings manager on X11).
 *
 * # Styles #
 *
326
 * A RC style is specified by a `style`
327
 * declaration in a RC file, and then bound to widgets
328 329
 * with a `widget`, `widget_class`,
 * or `class` declaration. All styles
330
 * applying to a particular widget are composited together
331 332 333
 * with `widget` declarations overriding
 * `widget_class` declarations which, in
 * turn, override `class` declarations.
334 335 336
 * Within each type of declaration, later declarations override
 * earlier ones.
 *
337
 * Within a `style` declaration, the possible
338 339
 * elements are:
 *
340
 * * `bg[state] = color`
341 342 343
 *
 *   Sets the color used for the background of most widgets.
 *
344
 * * `fg[state] = color`
345 346 347
 *
 *   Sets the color used for the foreground of most widgets.
 *
348
 * * `base[state] = color`
349
 *
350 351
 *          Sets the color used for the background of widgets displaying
 *          editable text. This color is used for the background
352
 *          of, among others, #GtkTextView, #GtkEntry.
353
 *
354 355
 * * `text[state] =
 *       color`
356
 *
357
 *          Sets the color used for foreground of widgets using
358
 *          `base` for the background color.
359
 *
360
 * * `xthickness =
361
 *       number`
362
 *
363 364
 *          Sets the xthickness, which is used for various horizontal padding
 *          values in GTK+.
365
 *
366
 * * `ythickness =
367
 *       number`
368
 *
369 370
 *          Sets the ythickness, which is used for various vertical padding
 *          values in GTK+.
371
 *
372 373
 * * `bg_pixmap[state] =
 *       pixmap`
374
 *
375 376 377
 *          Sets a background pixmap to be used in place of the `bg` color
 *          (or for #GtkText, in place of the `base` color. The special
 *          value `"<parent>"` may be used to indicate that the widget should
378
 *          use the same background pixmap as its parent. The special value
379
 *          `"<none>"` may be used to indicate no background pixmap.
380

381
 * * `font = font`
382
 *
383 384
 *          Starting with GTK+ 2.0, the “font”  and “fontset” 
 *          declarations are ignored; use “font_name”  declarations instead.
385
 *
386
 * * `fontset = font`
387
 *
388 389
 *          Starting with GTK+ 2.0, the “font”  and “fontset” 
 *          declarations are ignored; use “font_name”  declarations instead.
390
 *
391
 * * `font_name = font`
392
 *
393
 *          Sets the font for a widget. font must be
394
 *          a Pango font name, e.g. “Sans Italic 10” .
395 396
 *          For details about Pango font names, see
 *          pango_font_description_from_string().
397
 *
398
 * * `stock["stock-id"] = { icon source specifications }`
399
 *
400
 *         Defines the icon for a stock item.
401
 *
402
 * * `color["color-name"] = color specification`
403
 *
404 405
 *         Since 2.10, this element can be used to defines symbolic colors. See below for
 *         the syntax of color specifications.
406
 *
407 408
 * * `engine "engine" { engine-specific
 * settings }`
409
 *
410
 *         Defines the engine to be used when drawing with this style.
411
 *
412
 * * `class::property = value`
413
 *
414
 *         Sets a [style property][style-properties] for a widget class.
415 416 417 418
 *
 * The colors and background pixmaps are specified as a function of the
 * state of the widget. The states are:
 *
419
 * * `NORMAL`
420
 *
421
 *         A color used for a widget in its normal state.
422
 *
423
 * * `ACTIVE`
424
 *
425
 *         A variant of the `NORMAL` color used when the
426 427 428 429
 *         widget is in the %GTK_STATE_ACTIVE state, and also for
 *         the trough of a ScrollBar, tabs of a NoteBook
 *         other than the current tab and similar areas.
 *         Frequently, this should be a darker variant
430
 *         of the `NORMAL` color.
431
 *
432
 * * `PRELIGHT`
433
 *
434 435 436 437
 *         A color used for widgets in the %GTK_STATE_PRELIGHT state. This
 *         state is the used for Buttons and MenuItems
 *         that have the mouse cursor over them, and for
 *         their children.
438
 *
439
 * * `SELECTED`
440
 *
441 442 443
 *         A color used to highlight data selected by the user.
 *         for instance, the selected items in a list widget, and the
 *         selection in an editable widget.
444
 *
445
 * * `INSENSITIVE`
446
 *
447 448 449
 *         A color used for the background of widgets that have
 *         been set insensitive with gtk_widget_set_sensitive().
 *
450 451
 * ## Color Format ## {#color-format}
 *
452
 * Colors can be specified as a string containing a color name (GTK+ knows
453
 * all names from the X color database `/usr/lib/X11/rgb.txt`),
454 455 456 457
 * in one of the hexadecimal forms `#rrrrggggbbbb`,
 * `#rrrgggbbb`, `#rrggbb`,
 * or `#rgb`, where `r`,
 * `g` and `b` are
458
 * hex digits, or they can be specified as a triplet
459 460
 * `{ r, g,
 * b}`, where `r`,
461
 * `g` and `b` are either integers in
462 463 464
 * the range 0-65535 or floats in the range 0.0-1.0.
 *
 * Since 2.10, colors can also be specified by refering to a symbolic color, as
465
 * follows: `@color-name`, or by using expressions to combine
466
 * colors. The following expressions are currently supported:
467
 *
468
 * * mix (factor, color1, color2)
469
 *
470 471 472 473 474
 *         Computes a new color by mixing color1 and
 *         color2. The factor
 *         determines how close the new color is to color1.
 *         A factor of 1.0 gives pure color1, a factor of
 *         0.0 gives pure color2.
475
 *
476
 * * shade (factor, color)
477
 *
478 479
 *         Computes a lighter or darker variant of color.
 *         A factor of 1.0 leaves the color unchanged, smaller
480
 *         factors yield darker colors, larger factors yield lighter colors.
481
 *
482
 * * lighter (color)
483
 *
484
 *         This is an abbreviation for
485
 *         `shade (1.3, color)`.
486
 *
487
 * * darker (color)
488
 *
489
 *         This is an abbreviation for
490
 *         `shade (0.7, color)`.
491 492 493
 *
 * Here are some examples of color expressions:
 *
494
 * |[
495 496
 *  mix (0.5, "red", "blue")
 *  shade (1.5, mix (0.3, "#0abbc0", { 0.3, 0.5, 0.9 }))
497
 *  lighter (@foreground)
498
 * ]|
499
 *
500
 * In a `stock` definition, icon sources are specified as a
501 502 503
 * 4-tuple of image filename or icon name, text direction, widget state, and size, in that
 * order.  Each icon source specifies an image filename or icon name to use with a given
 * direction, state, and size. Filenames are specified as a string such
504
 * as `"itemltr.png"`, while icon names (looked up
505
 * in the current icon theme), are specified with a leading
506 507
 * `@`, such as `@"item-ltr"`.
 * The `*` character can be used as a
508
 * wildcard, and if direction/state/size are omitted they default to
509
 * `*`. So for example, the following specifies different icons to
510 511
 * use for left-to-right and right-to-left languages:
 *
512
 * |[<!-- language="C" -->
513 514 515 516 517
 * stock["my-stock-item"] =
 * {
 *   { "itemltr.png", LTR, *, * },
 *   { "itemrtl.png", RTL, *, * }
 * }
518
 * ]|
519 520 521
 *
 * This could be abbreviated as follows:
 *
522
 * |[<!-- language="C" -->
523 524 525 526 527
 * stock["my-stock-item"] =
 * {
 *   { "itemltr.png", LTR },
 *   { "itemrtl.png", RTL }
 * }
528
 * ]|
529 530 531
 *
 * You can specify custom icons for specific sizes, as follows:
 *
532
 * |[<!-- language="C" -->
533 534 535 536 537 538
 * stock["my-stock-item"] =
 * {
 *   { "itemmenusize.png", *, *, "gtk-menu" },
 *   { "itemtoolbarsize.png", *, *, "gtk-large-toolbar" }
 *   { "itemgeneric.png" } // implicit *, *, * as a fallback
 * }
539
 * ]|
540
 *
541 542 543
 * The sizes that come with GTK+ itself are `"gtk-menu"`,
 * `"gtk-small-toolbar"`, `"gtk-large-toolbar"`,
 * `"gtk-button"`, `"gtk-dialog"`. Applications
544 545
 * can define other sizes.
 *
546
 * It’s also possible to use custom icons for a given state, for example:
547
 *
548
 * |[<!-- language="C" -->
549 550 551 552 553 554
 * stock["my-stock-item"] =
 * {
 *   { "itemprelight.png", *, PRELIGHT },
 *   { "iteminsensitive.png", *, INSENSITIVE },
 *   { "itemgeneric.png" } // implicit *, *, * as a fallback
 * }
555
 * ]|
556 557 558 559
 *
 * When selecting an icon source to use, GTK+ will consider text direction most
 * important, state second, and size third. It will select the best match based on
 * those criteria. If an attribute matches exactly (e.g. you specified
560
 * `PRELIGHT` or specified the size), GTK+ won’t modify the image;
561 562
 * if the attribute matches with a wildcard, GTK+ will scale or modify the image to
 * match the state and size the user requested.
563 564 565
 *
 * # Key bindings #
 *
566 567 568 569
 * Key bindings allow the user to specify actions to be
 * taken on particular key presses. The form of a binding
 * set declaration is:
 *
570
 * |[
571 572 573
 * binding name {
 *   bind key {
 *     signalname (param, ...)
574 575 576 577
 *     ...
 *   }
 *   ...
 * }
578
 * ]|
579
 *
580 581
 * `key` is a string consisting of a series of modifiers followed by
 * the name of a key. The modifiers can be:
582
 *
583
 * - `<alt>`
584
 *
585
 * - `<ctl>`
586
 *
587
 * - `<control>`
588
 *
589
 * - `<meta>`
590
 *
591
 * - `<hyper>`
592
 *
593
 * - `<super>`
594
 *
595
 * - `<mod1>`
596
 *
597
 * - `<mod2>`
598
 *
599
 * - `<mod3>`
600
 *
601
 * - `<mod4>`
602
 *
603
 * - `<mod5>`
604
 *
605
 * - `<release>`
606
 *
607
 * - `<shft>`
608
 *
609
 * - `<shift>`
610
 *
611 612
 * `<shft>` is an alias for `<shift>`, `<ctl>` is an alias for
 * `<control>`, and `<alt>` is an alias for `<mod1>`.
613
 *
614 615 616 617 618 619
 * The action that is bound to the key is a sequence of signal names
 * (strings) followed by parameters for each signal. The signals must
 * be action signals. (See g_signal_new()). Each parameter can be a
 * float, integer, string, or unquoted string representing an enumeration
 * value. The types of the parameters specified must match the types of
 * the parameters of the signal.
620 621 622 623 624 625 626 627 628
 *
 * Binding sets are connected to widgets in the same manner as styles,
 * with one difference: Binding sets override other binding sets first
 * by pattern type, then by priority and then by order of specification.
 * The priorities that can be specified and their default values are the
 * same as for styles.
 */


629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
enum 
{
  PATH_ELT_PSPEC,
  PATH_ELT_UNRESOLVED,
  PATH_ELT_TYPE
};

typedef struct
{
  gint type;
  union 
  {
    GType         class_type;
    gchar        *class_name;
    GPatternSpec *pspec;
  } elt;
} PathElt;

647
typedef struct {
648
  GSList *color_hashes;
649 650 651
} GtkRcStylePrivate;

#define GTK_RC_STYLE_GET_PRIVATE(obj) ((GtkRcStylePrivate *) gtk_rc_style_get_instance_private ((GtkRcStyle *) (obj)))
652

653 654 655
static void        gtk_rc_style_finalize             (GObject         *object);
static void        gtk_rc_style_real_merge           (GtkRcStyle      *dest,
                                                      GtkRcStyle      *src);
656 657 658 659
static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle      *rc_style);
static GtkStyle*   gtk_rc_style_real_create_style    (GtkRcStyle      *rc_style);
static gint	   gtk_rc_properties_cmp	     (gconstpointer    bsearch_node1,
						      gconstpointer    bsearch_node2);
660

661 662 663 664
static void	   insert_rc_property		     (GtkRcStyle      *style,
						      GtkRcProperty   *property,
						      gboolean         replace);

665

666
static const GScannerConfig gtk_rc_scanner_config =
667 668
{
  (
669
   " \t\r\n"
670 671 672
   )			/* cset_skip_characters */,
  (
   "_"
673
   G_CSET_a_2_z
674 675 676
   G_CSET_A_2_Z
   )			/* cset_identifier_first */,
  (
677 678
   G_CSET_DIGITS
   "-_"
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
   G_CSET_a_2_z
   G_CSET_A_2_Z
   )			/* cset_identifier_nth */,
  ( "#\n" )		/* cpair_comment_single */,
  
  TRUE			/* case_sensitive */,
  
  TRUE			/* skip_comment_multi */,
  TRUE			/* skip_comment_single */,
  TRUE			/* scan_comment_multi */,
  TRUE			/* scan_identifier */,
  FALSE			/* scan_identifier_1char */,
  FALSE			/* scan_identifier_NULL */,
  TRUE			/* scan_symbols */,
  TRUE			/* scan_binary */,
  TRUE			/* scan_octal */,
  TRUE			/* scan_float */,
  TRUE			/* scan_hex */,
  TRUE			/* scan_hex_dollar */,
698
  TRUE			/* scan_string_sq */,
699 700 701
  TRUE			/* scan_string_dq */,
  TRUE			/* numbers_2_int */,
  FALSE			/* int_2_float */,
702
  FALSE			/* identifier_2_string */,
703 704
  TRUE			/* char_2_token */,
  TRUE			/* symbol_2_token */,
705
  FALSE			/* scope_0_fallback */,
706
};
707

708
static GHashTable *realized_style_ht = NULL;
Elliot Lee's avatar
Elliot Lee committed
709

Owen Taylor's avatar
Owen Taylor committed
710 711
static gchar *im_module_file = NULL;

712
static gchar **gtk_rc_default_files = NULL;
713

714 715
/* RC file handling */

Owen Taylor's avatar
Owen Taylor committed
716 717
static gchar *
gtk_rc_make_default_dir (const gchar *type)
718
{
719 720
  const gchar *var;
  gchar *path;
721

Matthias Clasen's avatar
Matthias Clasen committed
722
  var = g_getenv ("GTK_EXE_PREFIX");
723

724
  if (var)
725
    path = g_build_filename (var, "lib", "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
726
  else
727
    path = g_build_filename (_gtk_get_libdir (), "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
728

729 730 731
  return path;
}

732 733 734 735
/**
 * gtk_rc_get_im_module_path:
 *
 * Obtains the path in which to look for IM modules. See the documentation
736
 * of the `GTK_PATH`
737 738 739
 * environment variable for more details about looking up modules. This
 * function is useful solely for utilities supplied with GTK+ and should
 * not be used by applications under normal circumstances.
740
 *
Matthias Clasen's avatar
Matthias Clasen committed
741 742 743
 * Returns: (type filename): a newly-allocated string containing the
 *    path in which to look for IM modules.
 *
744
 * Deprecated: 3.0: Use #GtkCssProvider instead.
745
 */
746
gchar *
Owen Taylor's avatar
Owen Taylor committed
747 748
gtk_rc_get_im_module_path (void)
{
749 750 751
  gchar **paths = _gtk_get_module_path ("immodules");
  gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
  g_strfreev (paths);
Owen Taylor's avatar
Owen Taylor committed
752

753
  return result;
Owen Taylor's avatar
Owen Taylor committed
754 755
}

756 757 758 759
/**
 * gtk_rc_get_im_module_file:
 *
 * Obtains the path to the IM modules file. See the documentation
760
 * of the `GTK_IM_MODULE_FILE`
761
 * environment variable for more details.
762
 *
Matthias Clasen's avatar
Matthias Clasen committed
763 764 765
 * Returns: (type filename): a newly-allocated string containing the
 *    name of the file listing the IM modules available for loading
 *
766
 * Deprecated: 3.0: Use #GtkCssProvider instead.
jacob berkman's avatar
jacob berkman committed
767
 */
Owen Taylor's avatar
Owen Taylor committed
768 769 770
gchar *
gtk_rc_get_im_module_file (void)
{
771 772 773 774 775
  const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
  gchar *result = NULL;

  if (var)
    result = g_strdup (var);
Owen Taylor's avatar
Owen Taylor committed
776 777 778 779

  if (!result)
    {
      if (im_module_file)
780
        result = g_strdup (im_module_file);
Owen Taylor's avatar
Owen Taylor committed
781
      else
782
        result = gtk_rc_make_default_dir ("immodules.cache");
Owen Taylor's avatar
Owen Taylor committed
783 784
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
785
  return result;
Owen Taylor's avatar
Owen Taylor committed
786 787
}

788 789 790 791 792 793 794 795 796 797 798
/**
 * gtk_rc_get_theme_dir:
 *
 * Returns the standard directory in which themes should
 * be installed. (GTK+ does not actually use this directory
 * itself.)
 *
 * Returns: The directory (must be freed with g_free()).
 *
 * Deprecated: 3.0: Use #GtkCssProvider instead.
 */
Owen Taylor's avatar
Owen Taylor committed
799
gchar *
Hans Breuer's avatar
Hans Breuer committed
800
gtk_rc_get_theme_dir (void)
801
{
802 803
  const gchar *var;
  gchar *path;
804

Matthias Clasen's avatar
Matthias Clasen committed
805
  var = g_getenv ("GTK_DATA_PREFIX");
806

807
  if (var)
808
    path = g_build_filename (var, "share", "themes", NULL);
809
  else
810
    path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
811

812 813 814
  return path;
}

815 816
/**
 * gtk_rc_get_module_dir:
817
 *
818 819
 * Returns a directory in which GTK+ looks for theme engines.
 * For full information about the search for theme engines,
820
 * see the docs for `GTK_PATH` in [Running GTK+ Applications][gtk-running].
821
 *
822
 * return value: (type filename): the directory. (Must be freed with g_free())
823 824
 *
 * Deprecated: 3.0: Use #GtkCssProvider instead.
825
 **/
Owen Taylor's avatar
Owen Taylor committed
826
gchar *
Hans Breuer's avatar
Hans Breuer committed
827
gtk_rc_get_module_dir (void)
Owen Taylor's avatar
Owen Taylor committed
828 829 830 831
{
  return gtk_rc_make_default_dir ("engines");
}

832 833
/**
 * gtk_rc_add_default_file:
834 835
 * @filename: (type filename): the pathname to the file. If @filename
 *    is not absolute, it is searched in the current directory.
836
 *
837 838
 * Adds a file to the list of files to be parsed at the
 * end of gtk_init().
839 840
 *
 * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
841
 **/
842
void
843
gtk_rc_add_default_file (const gchar *filename)
844 845 846
{
}

847 848
/**
 * gtk_rc_set_default_files:
849 850
 * @filenames: (array zero-terminated=1) (element-type filename): A
 *     %NULL-terminated list of filenames.
851
 *
852
 * Sets the list of files that GTK+ will read at the
Matthias Clasen's avatar
Matthias Clasen committed
853
 * end of gtk_init().
854 855
 *
 * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
856
 **/
857
void
858
gtk_rc_set_default_files (gchar **filenames)
859 860 861
{
}

862 863
/**
 * gtk_rc_get_default_files:
864
 *
865
 * Retrieves the current list of RC files that will be parsed
Matthias Clasen's avatar
Matthias Clasen committed
866
 * at the end of gtk_init().
867
 *
868
 * Returns: (transfer none) (array zero-terminated=1) (element-type filename):
869 870 871
 *      A %NULL-terminated array of filenames.  This memory is owned
 *     by GTK+ and must not be freed by the application.  If you want
 *     to store this information, you should make a copy.
872 873
 *
 * Deprecated:3.0: Use #GtkStyleContext instead
874
 **/
875 876 877 878 879 880
gchar **
gtk_rc_get_default_files (void)
{
  return gtk_rc_default_files;
}

881 882 883 884 885 886 887 888
/**
 * gtk_rc_parse_string:
 * @rc_string: a string to parse.
 *
 * Parses resource information directly from a string.
 *
 * Deprecated: 3.0: Use #GtkCssProvider instead.
 */
Elliot Lee's avatar
Elliot Lee committed
889
void
890
gtk_rc_parse_string (const gchar *rc_string)
Elliot Lee's avatar
Elliot Lee committed
891
{
892
  g_return_if_fail (rc_string != NULL);
893 894
}

895 896 897 898 899 900 901 902 903
/**
 * gtk_rc_parse:
 * @filename: the filename of a file to parse. If @filename is not absolute, it
 *  is searched in the current directory.
 *
 * Parses a given resource file.
 *
 * Deprecated: 3.0: Use #GtkCssProvider instead.
 */
904
void
905 906
gtk_rc_parse (const gchar *filename)
{
907
  g_return_if_fail (filename != NULL);
908 909
}

910 911
/* Handling of RC styles */

912
G_DEFINE_TYPE_WITH_PRIVATE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
913

914 915 916
static void
gtk_rc_style_init (GtkRcStyle *style)
{
917
  GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
918 919 920
  guint i;

  style->name = NULL;
921 922
  style->font_desc = NULL;

923 924 925 926 927 928 929 930 931 932 933 934 935
  for (i = 0; i < 5; i++)
    {
      static const GdkColor init_color = { 0, 0, 0, 0, };

      style->bg_pixmap_name[i] = NULL;
      style->color_flags[i] = 0;
      style->fg[i] = init_color;
      style->bg[i] = init_color;
      style->text[i] = init_color;
      style->base[i] = init_color;
    }
  style->xthickness = -1;
  style->ythickness = -1;
936
  style->rc_properties = NULL;
937

938
  style->rc_style_lists = NULL;
939
  style->icon_factories = NULL;
940 941

  priv->color_hashes = NULL;
942
}
943

944 945
static void
gtk_rc_style_class_init (GtkRcStyleClass *klass)
946
{
947
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
948

949
  object_class->finalize = gtk_rc_style_finalize;
950 951

  klass->parse = NULL;
952
  klass->create_rc_style = gtk_rc_style_real_create_rc_style;
953 954
  klass->merge = gtk_rc_style_real_merge;
  klass->create_style = gtk_rc_style_real_create_style;
955 956
}

957 958
static void
gtk_rc_style_finalize (GObject *object)
959
{
960 961
  GSList *tmp_list1, *tmp_list2;
  GtkRcStyle *rc_style;
962
  GtkRcStylePrivate *rc_priv;
963
  gint i;
964

965
  rc_style = GTK_RC_STYLE (object);
966 967
  rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);

968
  g_free (rc_style->name);
969 970
  if (rc_style->font_desc)
    pango_font_description_free (rc_style->font_desc);
971

972
  for (i = 0; i < 5; i++)
973
    g_free (rc_style->bg_pixmap_name[i]);
974

975 976 977 978 979 980 981 982
  /* Now remove all references to this rc_style from
   * realized_style_ht
   */
  tmp_list1 = rc_style->rc_style_lists;
  while (tmp_list1)
    {
      GSList *rc_styles = tmp_list1->data;
      GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
Manish Singh's avatar
Manish Singh committed
983
      g_object_unref (style);
984 985 986

      /* Remove the list of styles from the other rc_styles
       * in the list
987
       */
988 989 990 991 992 993
      tmp_list2 = rc_styles;
      while (tmp_list2)
        {
          GtkRcStyle *other_style = tmp_list2->data;

          if (other_style != rc_style)
994 995
            other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
							      rc_styles);
996 997
          tmp_list2 = tmp_list2->next;
        }
998

999 1000 1001 1002
      /* And from the hash table itself
       */
      g_hash_table_remove (realized_style_ht, rc_styles);
      g_slist_free (rc_styles);
1003

1004 1005 1006
      tmp_list1 = tmp_list1->next;
    }
  g_slist_free (rc_style->rc_style_lists);
1007

1008 1009
  if (rc_style->rc_properties)
    {
1010
      for (i = 0; i < rc_style->rc_properties->len; i++)
1011
	{
1012
	  GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1013 1014 1015 1016

	  g_free (node->origin);
	  g_value_unset (&node->value);
	}
1017
      g_array_free (rc_style->rc_properties, TRUE);
1018 1019 1020
      rc_style->rc_properties = NULL;
    }

Matthias Clasen's avatar
Matthias Clasen committed
1021 1022
  g_slist_free_full (rc_style->icon_factories, g_object_unref);
  g_slist_free_full (rc_priv->color_hashes, (GDestroyNotify)g_hash_table_unref);
1023

Matthias Clasen's avatar
Matthias Clasen committed
1024
  G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1025
}
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/**
 * gtk_rc_style_new:
 *
 * Creates a new #GtkRcStyle with no fields set and
 * a reference count of 1.
 *
 * Returns: the newly-created #GtkRcStyle
 *
 * Deprecated: 3.0: Use #GtkCssProvider instead.
 */
1037
GtkRcStyle *
1038
gtk_rc_style_new (void)
1039 1040
{
  GtkRcStyle *style;
1041

1042
  style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1043

1044 1045
  return style;
}
1046

1047 1048 1049
/**
 * gtk_rc_style_copy:
 * @orig: the style to copy
1050
 *
Matthias Clasen's avatar
Matthias Clasen committed
1051
 * Makes a copy of the specified #GtkRcStyle. This function
Matthias Clasen's avatar
Matthias Clasen committed
1052
 * will correctly copy an RC style that is a member of a class
1053
 * derived from #GtkRcStyle.
1054
 *
1055
 * Returns: (transfer full): the resulting #GtkRcStyle
1056 1057
 *
 * Deprecated: 3.0: Use #GtkCssProvider instead.
1058 1059 1060 1061 1062 1063
 **/
GtkRcStyle *
gtk_rc_style_copy (GtkRcStyle *orig)
{
  GtkRcStyle *style;

1064
  g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1065
  
1066
  style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1067 1068 1069 1070 1071
  GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);

  return style;
}

1072
static GtkRcStyle *
1073
gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1074
{
1075
  return g_object_new (G_OBJECT_TYPE (style), NULL);
1076 1077
}

1078 1079 1080 1081 1082 1083 1084
static gint
gtk_rc_properties_cmp (gconstpointer bsearch_node1,
		       gconstpointer bsearch_node2)
{
  const GtkRcProperty *prop1 = bsearch_node1;
  const GtkRcProperty *prop2 = bsearch_node2;

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  if (prop1->type_name == prop2->type_name)
    return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
  else
    return prop1->type_name < prop2->type_name ? -1 : 1;
}

static void
insert_rc_property (GtkRcStyle    *style,
		    GtkRcProperty *property,
		    gboolean       replace)
{
  guint i;
  GtkRcProperty *new_property = NULL;
  GtkRcProperty key = { 0, 0, NULL, { 0, }, };

  key.type_name = property->type_name;
  key.property_name = property->property_name;

  if (!style->rc_properties)
    style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));

  i = 0;
  while (i < style->rc_properties->len)
    {
      gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1110

1111 1112 <