gtkrc.c 54.9 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Elliot Lee's avatar
Elliot Lee committed
12
13
14
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
15
16
17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19
20
21
22
23
24
25
26

/*
 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
 * 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/. 
 */

27
28
#include "config.h"

29
30
31
#include "glib.h"
#include "gdkconfig.h"

32
#ifdef GDK_WINDOWING_X11
33
#include <X11/Xlocale.h>	/* so we get the right setlocale */
34
35
36
#else
#include <locale.h>
#endif
Elliot Lee's avatar
Elliot Lee committed
37
#include <ctype.h>
38
#ifdef HAVE_UNISTD_H
39
#include <unistd.h>
40
#endif
41
#include <sys/stat.h>
42
#ifdef HAVE_SYS_PARAM_H
43
#include <sys/param.h>
44
#endif
45
#include <fcntl.h>
Elliot Lee's avatar
Elliot Lee committed
46
#include <string.h>
47
#include <stdio.h>
48
#include <stdlib.h>
49

Tor Lillqvist's avatar
Tor Lillqvist committed
50
51
52
53
#ifndef HAVE_LSTAT
#define lstat stat
#endif

Manish Singh's avatar
Manish Singh committed
54
#ifdef G_OS_WIN32
55
#include <windows.h>		/* For GetWindowsDirectory */
56
57
58
#include <io.h>
#endif

Elliot Lee's avatar
Elliot Lee committed
59
#include "gtkrc.h"
Tim Janik's avatar
Tim Janik committed
60
#include "gtkbindings.h"
61
#include "gtkthemes.h"
62
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
63
64

typedef struct _GtkRcSet    GtkRcSet;
65
typedef struct _GtkRcNode   GtkRcNode;
66
typedef struct _GtkRcFile   GtkRcFile;
67
typedef struct _GtkRcStylePrivate  GtkRcStylePrivate;
68

Elliot Lee's avatar
Elliot Lee committed
69
70
struct _GtkRcSet
{
Tim Janik's avatar
Tim Janik committed
71
72
  GtkPatternSpec pspec;
  GtkRcStyle	*rc_style;
Elliot Lee's avatar
Elliot Lee committed
73
74
};

75
76
77
78
79
80
81
struct _GtkRcFile
{
  time_t mtime;
  gchar *name;
  gchar *canonical_name;
  gboolean reload;
};
Elliot Lee's avatar
Elliot Lee committed
82

83
84
85
86
87
88
89
90
91
struct _GtkRcStylePrivate
{
  GtkRcStyle style;

  guint ref_count;
  /* list of RC style lists including this RC style */
  GSList *rc_style_lists;
};

92
93
94
static guint	   gtk_rc_style_hash		   (const char   *name);
static gint	   gtk_rc_style_compare		   (const char   *a,
						    const char   *b);
95
96
97
static guint	   gtk_rc_styles_hash		   (const GSList *rc_styles);
static gint	   gtk_rc_styles_compare	   (const GSList *a,
						    const GSList *b);
98
static GtkRcStyle* gtk_rc_style_find		   (const char   *name);
99
100
101
102
103
104
105
static GSList *    gtk_rc_styles_match             (GSList       *rc_styles,
						    GSList       *sets,
						    guint         path_length,
						    gchar        *path,
						    gchar        *path_reversed);
static GtkStyle *  gtk_rc_style_to_style           (GtkRcStyle   *rc_style);
static GtkStyle*   gtk_rc_style_init		   (GSList       *rc_styles);
106
107
108
static void        gtk_rc_parse_file               (const gchar  *filename,
						    gboolean      reload);

109
110
111
static void	   gtk_rc_parse_any		   (const gchar  *input_name,
						    gint	  input_fd,
						    const gchar  *input_string);
112
113
114
static guint	   gtk_rc_parse_statement	   (GScanner	 *scanner);
static guint	   gtk_rc_parse_style		   (GScanner	 *scanner);
static guint	   gtk_rc_parse_base		   (GScanner	 *scanner,
115
						    GtkRcStyle	 *style);
116
static guint	   gtk_rc_parse_bg		   (GScanner	 *scanner,
117
						    GtkRcStyle	 *style);
118
static guint	   gtk_rc_parse_fg		   (GScanner	 *scanner,
119
						    GtkRcStyle	 *style);
120
static guint	   gtk_rc_parse_text		   (GScanner	 *scanner,
121
						    GtkRcStyle	 *style);
122
static guint	   gtk_rc_parse_bg_pixmap	   (GScanner	 *scanner,
123
						    GtkRcStyle	 *rc_style);
124
static guint	   gtk_rc_parse_font		   (GScanner	 *scanner,
125
						    GtkRcStyle	 *rc_style);
126
static guint	   gtk_rc_parse_fontset		   (GScanner	 *scanner,
127
						    GtkRcStyle	 *rc_style);
128
129
static guint	   gtk_rc_parse_engine		   (GScanner	 *scanner,
						    GtkRcStyle	 *rc_style);
130
static guint	   gtk_rc_parse_pixmap_path	   (GScanner	 *scanner);
131
static void	   gtk_rc_parse_pixmap_path_string (gchar *pix_path);
132
133
static guint	   gtk_rc_parse_module_path	   (GScanner	 *scanner);
static void	   gtk_rc_parse_module_path_string (gchar *mod_path);
134
static guint	   gtk_rc_parse_path_pattern	   (GScanner     *scanner);
135
136
137
static void        gtk_rc_clear_hash_node          (gpointer   key, 
						    gpointer   data, 
						    gpointer   user_data);
138
139
140
static void        gtk_rc_clear_styles               (void);
static void        gtk_rc_append_default_pixmap_path (void);
static void        gtk_rc_append_default_module_path (void);
141
static void        gtk_rc_add_initial_default_files  (void);
142
143


144
static const GScannerConfig	gtk_rc_scanner_config =
145
146
{
  (
147
   " \t\r\n"
148
149
150
151
152
153
154
155
   )			/* cset_skip_characters */,
  (
   G_CSET_a_2_z
   "_"
   G_CSET_A_2_Z
   )			/* cset_identifier_first */,
  (
   G_CSET_a_2_z
156
   "_-0123456789"
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
   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 */,
175
  TRUE			/* scan_string_sq */,
176
177
178
  TRUE			/* scan_string_dq */,
  TRUE			/* numbers_2_int */,
  FALSE			/* int_2_float */,
179
  FALSE			/* identifier_2_string */,
180
181
  TRUE			/* char_2_token */,
  TRUE			/* symbol_2_token */,
182
  FALSE			/* scope_0_fallback */,
183
184
};

185
static const struct
Elliot Lee's avatar
Elliot Lee committed
186
{
187
  gchar *name;
188
  guint token;
189
} symbols[] = {
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
  { "include", GTK_RC_TOKEN_INCLUDE },
  { "NORMAL", GTK_RC_TOKEN_NORMAL },
  { "ACTIVE", GTK_RC_TOKEN_ACTIVE },
  { "PRELIGHT", GTK_RC_TOKEN_PRELIGHT },
  { "SELECTED", GTK_RC_TOKEN_SELECTED },
  { "INSENSITIVE", GTK_RC_TOKEN_INSENSITIVE },
  { "fg", GTK_RC_TOKEN_FG },
  { "bg", GTK_RC_TOKEN_BG },
  { "base", GTK_RC_TOKEN_BASE },
  { "text", GTK_RC_TOKEN_TEXT },
  { "font", GTK_RC_TOKEN_FONT },
  { "fontset", GTK_RC_TOKEN_FONTSET },
  { "bg_pixmap", GTK_RC_TOKEN_BG_PIXMAP },
  { "pixmap_path", GTK_RC_TOKEN_PIXMAP_PATH },
  { "style", GTK_RC_TOKEN_STYLE },
  { "binding", GTK_RC_TOKEN_BINDING },
  { "bind", GTK_RC_TOKEN_BIND },
  { "widget", GTK_RC_TOKEN_WIDGET },
  { "widget_class", GTK_RC_TOKEN_WIDGET_CLASS },
  { "class", GTK_RC_TOKEN_CLASS },
  { "lowest", GTK_RC_TOKEN_LOWEST },
  { "gtk", GTK_RC_TOKEN_GTK },
  { "application", GTK_RC_TOKEN_APPLICATION },
  { "rc", GTK_RC_TOKEN_RC },
  { "highest", GTK_RC_TOKEN_HIGHEST },
215
216
  { "engine", GTK_RC_TOKEN_ENGINE },
  { "module_path", GTK_RC_TOKEN_MODULE_PATH },
217
};
218

219
static const guint n_symbols = sizeof (symbols) / sizeof (symbols[0]);
Elliot Lee's avatar
Elliot Lee committed
220
221

static GHashTable *rc_style_ht = NULL;
222
static GHashTable *realized_style_ht = NULL;
223
224
static GSList *gtk_rc_sets_widget = NULL;
static GSList *gtk_rc_sets_widget_class = NULL;
225
static GSList *gtk_rc_sets_class = NULL;
Elliot Lee's avatar
Elliot Lee committed
226

227
228
229
230
#define GTK_RC_MAX_DEFAULT_FILES 128
static gchar *gtk_rc_default_files[GTK_RC_MAX_DEFAULT_FILES];
static gboolean gtk_rc_auto_parse = TRUE;

Elliot Lee's avatar
Elliot Lee committed
231
232
#define GTK_RC_MAX_PIXMAP_PATHS 128
static gchar *pixmap_path[GTK_RC_MAX_PIXMAP_PATHS];
233
234
#define GTK_RC_MAX_MODULE_PATHS 128
static gchar *module_path[GTK_RC_MAX_MODULE_PATHS];
Elliot Lee's avatar
Elliot Lee committed
235

236
237
238
239
240
/* A stack of directories for RC files we are parsing currently.
 * these are implicitely added to the end of PIXMAP_PATHS
 */
GSList *rc_dir_stack = NULL;

241
/* The files we have parsed, to reread later if necessary */
242
GSList *rc_files = NULL;
Elliot Lee's avatar
Elliot Lee committed
243

244
245
static GtkImageLoader image_loader = NULL;

246
247
248
/* RC file handling */


Manish Singh's avatar
Manish Singh committed
249
#ifdef G_OS_WIN32
250

251
gchar *
Manish Singh's avatar
Manish Singh committed
252
get_gtk_sysconf_directory (void)
253
254
255
256
257
{
  static gchar gtk_sysconf_dir[200];
  gchar win_dir[100];

  GetWindowsDirectory (win_dir, sizeof (win_dir));
258
  sprintf (gtk_sysconf_dir, "%s\\gtk+", win_dir);
259
260
261
262
  return gtk_sysconf_dir;
}

static gchar *
Manish Singh's avatar
Manish Singh committed
263
get_themes_directory (void)
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{
  /* We really should fetch this from the Registry. The GIMP
   * installation program stores the Themes installation
   * directory in HKLM\Software\GNU\GTk+\Themes\InstallDirectory.
   * Later.
   */
  static gchar themes_dir[200];

  sprintf (themes_dir, "%s\\themes", get_gtk_sysconf_directory ());
  return themes_dir;
}

#endif
 
278
279
280
281
282
gchar *
gtk_rc_get_theme_dir(void)
{
  gchar *var, *path;

Manish Singh's avatar
Manish Singh committed
283
#ifndef G_OS_WIN32
284
285
  var = getenv("GTK_DATA_PREFIX");
  if (var)
286
    path = g_strdup_printf("%s%s", var, "/share/themes");
287
  else
288
    path = g_strdup_printf("%s%s", GTK_DATA_PREFIX, "/share/themes");
289
290
291
#else
  path = g_strdup (get_themes_directory ());
#endif
292

293
294
295
296
297
298
299
300
  return path;
}

gchar *
gtk_rc_get_module_dir(void)
{
  gchar *var, *path;

Manish Singh's avatar
Manish Singh committed
301
#ifndef G_OS_WIN32
302
303
  var = getenv("GTK_EXE_PREFIX");
  if (var)
304
    path = g_strdup_printf("%s%s", var, "/lib/gtk/themes/engines");
305
  else
306
    path = g_strdup_printf("%s%s", GTK_EXE_PREFIX, "/lib/gtk/themes/engines");
307
308
309
#else
  path = g_strdup_printf ("%s%s", get_themes_directory (), "\\engines");
#endif
310

311
312
313
314
315
316
317
318
319
  return path;
}

static void
gtk_rc_append_default_pixmap_path(void)
{
  gchar *var, *path;
  gint n;

Manish Singh's avatar
Manish Singh committed
320
#ifndef G_OS_WIN32
321
322
  var = getenv("GTK_DATA_PREFIX");
  if (var)
323
    path = g_strdup_printf("%s%s", var, "/share/gtk/themes");
324
  else
325
    path = g_strdup_printf("%s%s", GTK_DATA_PREFIX, "/share/gtk/themes");
326
327
328
#else
  path = g_strdup (get_themes_directory ());
#endif      
329
330
331
  
  for (n = 0; pixmap_path[n]; n++) ;
  if (n >= GTK_RC_MAX_PIXMAP_PATHS - 1)
332
333
334
335
336
    {
      g_free (path);
      return;
    }
  pixmap_path[n++] = path;
337
338
339
340
341
342
343
344
345
346
347
348
349
  pixmap_path[n] = NULL;
}

static void
gtk_rc_append_default_module_path(void)
{
  gchar *var, *path;
  gint n;

  for (n = 0; module_path[n]; n++) ;
  if (n >= GTK_RC_MAX_MODULE_PATHS - 1)
    return;
  
Manish Singh's avatar
Manish Singh committed
350
#ifndef G_OS_WIN32
351
352
  var = getenv("GTK_EXE_PREFIX");
  if (var)
353
    path = g_strdup_printf("%s%s", var, "/lib/gtk/themes/engines");
354
  else
355
    path = g_strdup_printf("%s%s", GTK_EXE_PREFIX, "/lib/gtk/themes/engines");
356
357
358
359
#else
  path = g_strdup_printf ("%s%s", get_themes_directory (), "\\engines");
#endif
  module_path[n++] = path;
360

361
  var = g_get_home_dir ();
362
  if (var)
363
    {
Manish Singh's avatar
Manish Singh committed
364
#ifndef G_OS_WIN32
365
366
367
368
369
370
      path = g_strdup_printf ("%s%s", var, "/.gtk/lib/themes/engines");
#else
      path = g_strdup_printf ("%s%s", var, "\\_gtk\\themes\\engines");
#endif
      module_path[n++] = path;
    }
371
372
373
  module_path[n] = NULL;
}

374
375
376
377
378
379
380
381
382
383
384
385
386
387
static void
gtk_rc_add_initial_default_files (void)
{
  static gint init = FALSE;
  gchar *var, *str;
  gchar **files;
  gint i;

  if (init)
    return;
  
  gtk_rc_default_files[0] = NULL;
  init = TRUE;

388
  var = g_getenv("GTK_RC_FILES");
389
390
  if (var)
    {
391
      files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
392
393
394
395
396
397
      i=0;
      while (files[i])
	{
	  gtk_rc_add_default_file (files[i]);
	  i++;
	}
398
      g_strfreev (files);
399
400
401
    }
  else
    {
Manish Singh's avatar
Manish Singh committed
402
#ifndef G_OS_WIN32
403
404
405
406
      str = g_strdup (GTK_SYSCONFDIR G_DIR_SEPARATOR_S "gtk" G_DIR_SEPARATOR_S "gtkrc");
#else
      str = g_strdup_printf ("%s\\gtkrc", get_gtk_sysconf_directory ());
#endif
407
      gtk_rc_add_default_file (str);
408
      g_free (str);
409

410
411
412
413
414
415
416
      var = g_get_home_dir ();
      if (var)
	{
	  str = g_strdup_printf ("%s" G_DIR_SEPARATOR_S ".gtkrc", var);
	  gtk_rc_add_default_file (str);
	  g_free (str);
	}
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    }
}

void
gtk_rc_add_default_file (const gchar *file)
{
  guint n;
  
  gtk_rc_add_initial_default_files ();

  for (n = 0; gtk_rc_default_files[n]; n++) ;
  if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
    return;
  
  gtk_rc_default_files[n++] = g_strdup (file);
  gtk_rc_default_files[n] = NULL;
}

void
gtk_rc_set_default_files (gchar **files)
{
  gint i;

  gtk_rc_add_initial_default_files ();

  i = 0;
  while (gtk_rc_default_files[i])
    {
      g_free (gtk_rc_default_files[i]);
      i++;
    }
    
  gtk_rc_default_files[0] = NULL;
  gtk_rc_auto_parse = FALSE;

  i = 0;
  while (files[i] != NULL)
    {
      gtk_rc_add_default_file (files[i]);
      i++;
    }
}

gchar **
gtk_rc_get_default_files (void)
{
  gtk_rc_add_initial_default_files ();

  return gtk_rc_default_files;
}

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
 /* The following routine is based on _nl_normalize_codeset from
  * the GNU C library. Contributed by
  *
  * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  * Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
  * 
  * Normalize codeset name.  There is no standard for the codeset
  * names.  Normalization allows the user to use any of the common
  * names.
  */
 static char *
 _gtk_normalize_codeset (const char *codeset, int name_len)
 {
   int len = 0;
   int only_digit = 1;
   char *retval;
   char *wp;
   int cnt;
 
   for (cnt = 0; cnt < name_len; ++cnt)
     if (isalnum (codeset[cnt]))
       {
 	++len;
 
 	if (isalpha (codeset[cnt]))
 	  only_digit = 0;
       }
 
   retval = g_malloc ((only_digit ? 3 : 0) + len + 1);
 
   if (only_digit)
     {
       strcpy (retval, "iso");
       wp = retval + 3;
     }
   else
     wp = retval;
   
   for (cnt = 0; cnt < name_len; ++cnt)
     if (isalpha (codeset[cnt]))
       *wp++ = isupper(codeset[cnt]) ? tolower (codeset[cnt]) : codeset[cnt];
     else if (isdigit (codeset[cnt]))
       *wp++ = codeset[cnt];
   
   *wp = '\0';
 
   return retval;
 }
 
Elliot Lee's avatar
Elliot Lee committed
517
void
518
gtk_rc_init (void)
Elliot Lee's avatar
Elliot Lee committed
519
{
520
521
  static gchar *locale_suffixes[3];
  static gint n_locale_suffixes = 0;
522

523
  gint i, j;
524

525
  static gboolean initted = FALSE;
526

527
  if (!initted)
528
    {
529
530
531
532
533
534
535
536
537
      gint length;
      gchar *locale;
      gchar *p;

#ifdef G_OS_WIN32      
      locale = g_win32_getlocale ();
#else      
      locale = setlocale (LC_CTYPE, NULL);
#endif      
538
      
539
540
541
542
543
544
      initted = TRUE;

      pixmap_path[0] = NULL;
      module_path[0] = NULL;
      gtk_rc_append_default_pixmap_path();
      gtk_rc_append_default_module_path();
545
      
546
547
548
      gtk_rc_add_initial_default_files ();

      if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
549
	{
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
	  /* Determine locale-specific suffixes for RC files
	   *
	   * We normalize the charset into a standard form,
	   * which has all '-' and '_' characters removed,
	   * and is lowercase.
	   */
	  gchar *normalized_locale;

	  p = strchr (locale, '@');
	  length = p ? (p -locale) : strlen (locale);

	  p = strchr (locale, '.');
	  if (p)
	    {
	      gchar *tmp1 = g_strndup (locale, p - locale + 1);
	      gchar *tmp2 = _gtk_normalize_codeset (p + 1, length - (p - locale + 1));
	      
	      normalized_locale = g_strconcat (tmp1, tmp2, NULL);
	      g_free (tmp1);
	      g_free (tmp2);
						 
	      locale_suffixes[n_locale_suffixes++] = g_strdup (normalized_locale);
	      length = p - locale;
	    }
	  else
	    normalized_locale = g_strndup (locale, length);
	  
	  p = strchr (normalized_locale, '_');
	  if (p)
	    {
	      locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
	      length = p - normalized_locale;
	    }
	  
	  locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
585

586
587
	  g_free (normalized_locale);
	}
588
589
    }
  
590
591
592
  i = 0;
  while (gtk_rc_default_files[i] != NULL)
    {
593
594
595
596
597
598
599
600
601
602
      /* Try to find a locale specific RC file corresponding to
       * to parse before the default file.
       */
      for (j=n_locale_suffixes-1; j>=0; j--)
	{
	  gchar *name = g_strconcat (gtk_rc_default_files[i],
				     ".",
				     locale_suffixes[j],
				     NULL);
	  gtk_rc_parse (name);
603
	  g_free (name);
604
605
	}

606
607
608
      gtk_rc_parse (gtk_rc_default_files[i]);
      i++;
    }
609
}
Elliot Lee's avatar
Elliot Lee committed
610
611

void
612
gtk_rc_parse_string (const gchar *rc_string)
Elliot Lee's avatar
Elliot Lee committed
613
{
614
  g_return_if_fail (rc_string != NULL);
Elliot Lee's avatar
Elliot Lee committed
615

616
617
  gtk_rc_parse_any ("-", -1, rc_string);
}
Elliot Lee's avatar
Elliot Lee committed
618

619
620
static void
gtk_rc_parse_file (const gchar *filename, gboolean reload)
621
{
622
623
624
  GtkRcFile *rc_file = NULL;
  struct stat statbuf;
  GSList *tmp_list;
Elliot Lee's avatar
Elliot Lee committed
625

626
  g_return_if_fail (filename != NULL);
Elliot Lee's avatar
Elliot Lee committed
627

628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  tmp_list = rc_files;
  while (tmp_list)
    {
      rc_file = tmp_list->data;
      if (!strcmp (rc_file->name, filename))
	break;
      
      tmp_list = tmp_list->next;
    }

  if (!tmp_list)
    {
      rc_file = g_new (GtkRcFile, 1);
      rc_file->name = g_strdup (filename);
      rc_file->canonical_name = NULL;
      rc_file->mtime = 0;
      rc_file->reload = reload;

      rc_files = g_slist_append (rc_files, rc_file);
    }

  if (!rc_file->canonical_name)
    {
      /* Get the absolute pathname */

653
      if (g_path_is_absolute (rc_file->name))
654
655
656
657
	rc_file->canonical_name = rc_file->name;
      else
	{
	  GString *str;
658
659
660
661
662
663
	  gchar *cwd;

	  cwd = g_get_current_dir ();

	  str = g_string_new (cwd);
	  g_free (cwd);
664
	  g_string_append_c (str, G_DIR_SEPARATOR);
665
666
667
668
669
670
671
	  g_string_append (str, rc_file->name);
	  
	  rc_file->canonical_name = str->str;
	  g_string_free (str, FALSE);
	}
    }

672
  if (!lstat (rc_file->canonical_name, &statbuf))
673
674
    {
      gint fd;
675
      GSList *tmp_list;
676
677

      rc_file->mtime = statbuf.st_mtime;
678

679
680
681
      fd = open (rc_file->canonical_name, O_RDONLY);
      if (fd < 0)
	return;
Elliot Lee's avatar
Elliot Lee committed
682

683
684
685
686
687
      /* Temporarily push directory name for this file on
       * a stack of directory names while parsing it
       */
      rc_dir_stack = g_slist_prepend (rc_dir_stack,
 				      g_dirname (rc_file->canonical_name));
688
      gtk_rc_parse_any (filename, fd, NULL);
689
690
691
692
693
694
 
      tmp_list = rc_dir_stack;
      rc_dir_stack = rc_dir_stack->next;
 
      g_free (tmp_list->data);
      g_slist_free_1 (tmp_list);
Elliot Lee's avatar
Elliot Lee committed
695

696
697
      close (fd);
    }
Elliot Lee's avatar
Elliot Lee committed
698
699
}

700
void
701
702
703
704
705
706
707
gtk_rc_parse (const gchar *filename)
{
  g_return_if_fail (filename != NULL);

  gtk_rc_parse_file (filename, TRUE);
}

708
709
710
711
/* Handling of RC styles */

GtkRcStyle *
gtk_rc_style_new              (void)
712
{
713
  GtkRcStylePrivate *new_style;
714

715
  new_style = g_new0 (GtkRcStylePrivate, 1);
716
  new_style->ref_count = 1;
717

718
  return (GtkRcStyle *)new_style;
719
}
720

721
722
723
724
void      
gtk_rc_style_ref (GtkRcStyle  *rc_style)
{
  g_return_if_fail (rc_style != NULL);
725

726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  ((GtkRcStylePrivate *)rc_style)->ref_count++;
}

/* Like g_slist_remove, but remove all copies of data */
static GSList*
gtk_rc_slist_remove_all (GSList   *list,
			 gpointer  data)
{
  GSList *tmp;
  GSList *prev;

  prev = NULL;
  tmp = list;

  while (tmp)
    {
      if (tmp->data == data)
	{
	  if (list == tmp)
	    list = list->next;

	  if (prev) 
	    prev->next = tmp->next;

	  g_slist_free_1 (tmp);

	  if (prev)
	    tmp = prev->next;
	  else
	    tmp = list;
	}
      else
	{
	  prev = tmp;
	  tmp = tmp->next;
	}
    }

  return list;
765
}
766

767
768
769
void      
gtk_rc_style_unref (GtkRcStyle  *rc_style)
{
770
  GtkRcStylePrivate *private = (GtkRcStylePrivate *)rc_style;
771
  gint i;
772

773
  g_return_if_fail (rc_style != NULL);
774
  g_return_if_fail (private->ref_count > 0);
775

776
  private->ref_count--;
777

778
  if (private->ref_count == 0)
779
    {
780
781
      GSList *tmp_list1, *tmp_list2;
	
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
      if (rc_style->engine)
	{
	  rc_style->engine->destroy_rc_style (rc_style);
	  gtk_theme_engine_unref (rc_style->engine);
	}

      if (rc_style->name)
	g_free (rc_style->name);
      if (rc_style->fontset_name)
	g_free (rc_style->fontset_name);
      if (rc_style->font_name)
	g_free (rc_style->font_name);
      
      for (i=0 ; i<5 ; i++)
	if (rc_style->bg_pixmap_name[i])
	  g_free (rc_style->bg_pixmap_name[i]);
      
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
      /* Now remove all references to this rc_style from
       * realized_style_ht
       */
      tmp_list1 = private->rc_style_lists;
      while (tmp_list1)
	{
	  GSList *rc_styles = tmp_list1->data;
	  GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
	  gtk_style_unref (style);

	  /* Remove the list of styles from the other rc_styles
	   * in the list
	   */
	  tmp_list2 = rc_styles;
	  while (tmp_list2)
	    {
	      GtkRcStylePrivate *other_style = tmp_list2->data;
816

817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
	      if (other_style != private)
		other_style->rc_style_lists =
		  gtk_rc_slist_remove_all (other_style->rc_style_lists, rc_styles);
		  
	      tmp_list2 = tmp_list2->next;
	    }

	  /* And from the hash table itself
	   */
	  g_hash_table_remove (realized_style_ht, rc_styles);
	  g_slist_free (rc_styles);

	  tmp_list1 = tmp_list1->next;
	}
      g_slist_free (private->rc_style_lists);

      g_free (private);
    }
835
836
837
838
839
840
841
842
}

static void
gtk_rc_clear_hash_node (gpointer key, 
			gpointer data, 
			gpointer user_data)
{
  gtk_rc_style_unref (data);
843
844
}

845
static void
846
gtk_rc_free_rc_sets (GSList *slist)
847
{
848
849
850
851
852
853
854
  while (slist)
    {
      GtkRcSet *rc_set;

      rc_set = slist->data;
      gtk_pattern_spec_free_segs (&rc_set->pspec);
      g_free (rc_set);
855

856
857
858
859
860
861
862
      slist = slist->next;
    }
}

static void
gtk_rc_clear_styles (void)
{
863
864
  /* Clear out all old rc_styles */

865
866
867
868
869
870
871
  if (rc_style_ht)
    {
      g_hash_table_foreach (rc_style_ht, gtk_rc_clear_hash_node, NULL);
      g_hash_table_destroy (rc_style_ht);
      rc_style_ht = NULL;
    }

872
  gtk_rc_free_rc_sets (gtk_rc_sets_widget);
873
874
  g_slist_free (gtk_rc_sets_widget);
  gtk_rc_sets_widget = NULL;
875

876
  gtk_rc_free_rc_sets (gtk_rc_sets_widget_class);
877
878
  g_slist_free (gtk_rc_sets_widget_class);
  gtk_rc_sets_widget_class = NULL;
879

880
881
882
  gtk_rc_free_rc_sets (gtk_rc_sets_class);
  g_slist_free (gtk_rc_sets_class);
  gtk_rc_sets_class = NULL;
883
884
885
886
887
888
889
890
}

gboolean
gtk_rc_reparse_all (void)
{
  GSList *tmp_list;
  gboolean mtime_modified = FALSE;
  GtkRcFile *rc_file;
891

892
  struct stat statbuf;
893

894
895
896
897
  /* Check through and see if any of the RC's have had their
   * mtime modified. If so, reparse everything.
   */
  tmp_list = rc_files;
898
899
  while (tmp_list)
    {
900
901
      rc_file = tmp_list->data;
      
902
      if (!lstat (rc_file->name, &statbuf) && 
903
904
905
906
907
	  (statbuf.st_mtime > rc_file->mtime))
	{
	  mtime_modified = TRUE;
	  break;
	}
908
909
910
      
      tmp_list = tmp_list->next;
    }
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

  if (mtime_modified)
    {
      gtk_rc_clear_styles();

      tmp_list = rc_files;
      while (tmp_list)
	{
	  rc_file = tmp_list->data;
	  if (rc_file->reload)
	    gtk_rc_parse_file (rc_file->name, FALSE);
	  
	  tmp_list = tmp_list->next;
	}
    }

  return mtime_modified;
928
929
}

930
931
932
static GSList *
gtk_rc_styles_match (GSList       *rc_styles,
		     GSList	  *sets,
933
934
935
		     guint         path_length,
		     gchar        *path,
		     gchar        *path_reversed)
936
		     
937
938
939
940
941
942
943
944
945
{
  GtkRcSet *rc_set;

  while (sets)
    {
      rc_set = sets->data;
      sets = sets->next;

      if (gtk_pattern_match (&rc_set->pspec, path_length, path, path_reversed))
946
	rc_styles = g_slist_append (rc_styles, rc_set->rc_style);
947
948
    }
  
949
  return rc_styles;
950
951
}

Elliot Lee's avatar
Elliot Lee committed
952
953
954
GtkStyle*
gtk_rc_get_style (GtkWidget *widget)
{
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
  GtkRcStyle *widget_rc_style;
  GSList *rc_styles = NULL;

  static guint rc_style_key_id = 0;

  /* We allow the specification of a single rc style to be bound
   * tightly to a widget, for application modifications
   */
  if (!rc_style_key_id)
    rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");

  widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
					       rc_style_key_id);

  if (widget_rc_style)
970
    rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
971
  
972
  if (gtk_rc_sets_widget)
Elliot Lee's avatar
Elliot Lee committed
973
    {
Tim Janik's avatar
Tim Janik committed
974
975
976
977
      gchar *path, *path_reversed;
      guint path_length;

      gtk_widget_path (widget, &path_length, &path, &path_reversed);
978
      rc_styles = gtk_rc_styles_match (rc_styles, gtk_rc_sets_widget, path_length, path, path_reversed);
Tim Janik's avatar
Tim Janik committed
979
980
981
      g_free (path);
      g_free (path_reversed);
      
Elliot Lee's avatar
Elliot Lee committed
982
    }
983
  
984
  if (gtk_rc_sets_widget_class)
Elliot Lee's avatar
Elliot Lee committed
985
    {
Tim Janik's avatar
Tim Janik committed
986
987
988
989
      gchar *path, *path_reversed;
      guint path_length;

      gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
990
      rc_styles = gtk_rc_styles_match (rc_styles, gtk_rc_sets_widget_class, path_length, path, path_reversed);
Tim Janik's avatar
Tim Janik committed
991
992
      g_free (path);
      g_free (path_reversed);
Elliot Lee's avatar
Elliot Lee committed
993
    }
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009

  if (gtk_rc_sets_class)
    {
      GtkType type;

      type = GTK_OBJECT_TYPE (widget);
      while (type)
	{
	  gchar *path, *path_reversed;
	  guint path_length;

	  path = gtk_type_name (type);
	  path_length = strlen (path);
	  path_reversed = g_strdup (path);
	  g_strreverse (path_reversed);
	  
1010
	  rc_styles = gtk_rc_styles_match (rc_styles, gtk_rc_sets_class, path_length, path, path_reversed);
1011
1012
1013
1014
1015
	  g_free (path_reversed);
      
	  type = gtk_type_parent (type);
	}
    }
1016
  
1017
1018
1019
  if (rc_styles)
    return gtk_rc_style_init (rc_styles);

1020
  return NULL;
Elliot Lee's avatar
Elliot Lee committed
1021
1022
}

1023
1024
static GSList*
gtk_rc_add_rc_sets (GSList     *slist,
1025
		    GtkRcStyle *rc_style,
1026
		    const char *pattern)
Elliot Lee's avatar
Elliot Lee committed
1027
{
1028
  GtkRcStyle *new_style;
Elliot Lee's avatar
Elliot Lee committed
1029
  GtkRcSet *rc_set;
1030
  guint i;
1031
  
1032
1033
1034
1035
1036
  new_style = gtk_rc_style_new ();
  *new_style = *rc_style;
  new_style->name = g_strdup (rc_style->name);
  new_style->font_name = g_strdup (rc_style->font_name);
  new_style->fontset_name = g_strdup (rc_style->fontset_name);
1037
  
Elliot Lee's avatar
Elliot Lee committed
1038
  for (i = 0; i < 5; i++)
1039
    new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1040
  
Elliot Lee's avatar
Elliot Lee committed
1041
  rc_set = g_new (GtkRcSet, 1);
Tim Janik's avatar
Tim Janik committed
1042
  gtk_pattern_spec_init (&rc_set->pspec, pattern);
Elliot Lee's avatar
Elliot Lee committed
1043
  rc_set->rc_style = rc_style;
1044
  
1045
  return g_slist_prepend (slist, rc_set);
Elliot Lee's avatar
Elliot Lee committed
1046
1047
1048
}

void
1049
gtk_rc_add_widget_name_style (GtkRcStyle  *rc_style,
1050
			      const gchar *pattern)
Elliot Lee's avatar
Elliot Lee committed
1051
{
1052
  g_return_if_fail (rc_style != NULL);
1053
1054
  g_return_if_fail (pattern != NULL);

1055
  gtk_rc_sets_widget = gtk_rc_add_rc_sets (gtk_rc_sets_widget, rc_style, pattern);
1056
1057
1058
}

void
1059
gtk_rc_add_widget_class_style (GtkRcStyle  *rc_style,
1060
1061
			       const gchar *pattern)
{
1062
  g_return_if_fail (rc_style != NULL);
1063
1064
  g_return_if_fail (pattern != NULL);

1065
  gtk_rc_sets_widget_class = gtk_rc_add_rc_sets (gtk_rc_sets_widget_class, rc_style, pattern);
1066
1067
1068
}

void
1069
gtk_rc_add_class_style (GtkRcStyle  *rc_style,
1070
1071
			const gchar *pattern)
{
1072
  g_return_if_fail (rc_style != NULL);
1073
1074
  g_return_if_fail (pattern != NULL);

1075
  gtk_rc_sets_class = gtk_rc_add_rc_sets (gtk_rc_sets_class, rc_style, pattern);
Elliot Lee's avatar
Elliot Lee committed
1076
1077
}

1078
1079
1080
1081
1082
1083
1084
static void
gtk_rc_parse_any (const gchar  *input_name,
		  gint		input_fd,
		  const gchar  *input_string)
{
  GScanner *scanner;
  guint	   i;
1085
  gboolean done;
1086
  
1087
  scanner = g_scanner_new ((GScannerConfig *) &gtk_rc_scanner_config);
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
  
  if (input_fd >= 0)
    {
      g_assert (input_string == NULL);
      
      g_scanner_input_file (scanner, input_fd);
    }
  else
    {
      g_assert (input_string != NULL);
      
      g_scanner_input_text (scanner, input_string, strlen (input_string));
    }
1101
  scanner->input_name = input_name;
1102

1103
  g_scanner_freeze_symbol_table (scanner);
1104
  for (i = 0; i < n_symbols; i++)
1105
    g_scanner_add_symbol (scanner, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1106
  g_scanner_thaw_symbol_table (scanner);
1107
1108
1109
1110
  
  done = FALSE;
  while (!done)
    {
1111
1112
1113
      if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
	done = TRUE;
      else
1114
	{
1115
	  guint expected_token;
1116
	  
1117
1118
1119
1120
1121
1122
1123
1124
	  expected_token = gtk_rc_parse_statement (scanner);

	  if (expected_token != G_TOKEN_NONE)
	    {
	      gchar *symbol_name;
	      gchar *msg;
	      
	      msg = NULL;
1125
1126
	      symbol_name = NULL;
	      if (scanner->scope_id == 0)
1127
		{
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
		  /* if we are in scope 0, we know the symbol names
		   * that are associated with certaintoken values.
		   * so we look them up to make the error messages
		   * more readable.
		   */
		  if (expected_token > GTK_RC_TOKEN_INVALID &&
		      expected_token < GTK_RC_TOKEN_LAST)
		    {
		      for (i = 0; i < n_symbols; i++)
			if (symbols[i].token == expected_token)
			  msg = symbols[i].name;
		      if (msg)
			msg = g_strconcat ("e.g. `", msg, "'", NULL);
		    }
		  if (scanner->token > GTK_RC_TOKEN_INVALID &&
		      scanner->token < GTK_RC_TOKEN_LAST)
		    {
		      symbol_name = "???";
		      for (i = 0; i < n_symbols; i++)
			if (symbols[i].token == scanner->token)
			  symbol_name = symbols[i].name;
		    }
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
		}
	      g_scanner_unexp_token (scanner,
				     expected_token,
				     NULL,
				     "keyword",
				     symbol_name,
				     msg,
				     TRUE);
	      g_free (msg);
	      done = TRUE;
	    }
1161
1162
	}
    }
1163
  
1164
1165
  g_scanner_destroy (scanner);
}
Elliot Lee's avatar
Elliot Lee committed
1166

1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
static guint	   
gtk_rc_styles_hash (const GSList *rc_styles)
{
  guint result;
  
  result = 0;
  while (rc_styles)
    {
      result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
      rc_styles = rc_styles->next;
    }
  
  return result;
}

static gint	   
gtk_rc_styles_compare (const GSList *a,
		       const GSList *b)
{
  while (a && b)
    {
      if (a->data != b->data)
	return FALSE;
      a = a->next;
      b = b->next;
    }
  
  return (a == b);
}

Elliot Lee's avatar
Elliot Lee committed
1197
1198
1199
1200
static guint
gtk_rc_style_hash (const char *name)
{
  guint result;
1201
  
Elliot Lee's avatar
Elliot Lee committed
1202
1203
1204
  result = 0;
  while (*name)
    result += (result << 3) + *name++;
1205
  
Elliot Lee's avatar
Elliot Lee committed
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
  return result;
}

static gint
gtk_rc_style_compare (const char *a,
		      const char *b)
{
  return (strcmp (a, b) == 0);
}

static GtkRcStyle*
gtk_rc_style_find (const char *name)
{
1219
1220
1221
1222
  if (rc_style_ht)
    return g_hash_table_lookup (rc_style_ht, (gpointer) name);
  else
    return NULL;
Elliot Lee's avatar
Elliot Lee committed
1223
1224
}

1225
/* Assumes ownership of rc_style */
1226
static GtkStyle *
1227
gtk_rc_style_to_style (GtkRcStyle *rc_style)
Elliot Lee's avatar
Elliot Lee committed
1228
{
1229
  GtkStyle *style;
Elliot Lee's avatar
Elliot Lee committed
1230
1231
  GdkFont *old_font;
  gint i;
1232

1233
  style = gtk_style_new ();
1234

1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
  style->rc_style = rc_style;
  
  if (rc_style->fontset_name)
    {
      old_font = style->font;
      style->font = gdk_fontset_load (rc_style->fontset_name);
      if (style->font)
	gdk_font_unref (old_font);
      else
	style->font = old_font;
    }
  else if (rc_style->font_name)
    {
      old_font = style->font;
      style->font = gdk_font_load (rc_style->font_name);
      if (style->font)
	gdk_font_unref (old_font);
      else
	style->font = old_font;
    }
  
  for (i = 0; i < 5; i++)
    {
      if (rc_style->color_flags[i] & GTK_RC_FG)
	style->fg[i] = rc_style->fg[i];
      if (rc_style->color_flags[i] & GTK_RC_BG)
	style->bg[i] = rc_style->bg[i];
      if (rc_style->color_flags[i] & GTK_RC_TEXT)
	style->text[i] = rc_style->text[i];
      if (rc_style->color_flags[i] & GTK_RC_BASE)
	style->base[i] = rc_style->base[i];
    }
1267

1268
  if (rc_style->engine)
1269
    {
1270
1271
1272
1273
      style->engine = rc_style->engine;
      gtk_theme_engine_ref (style->engine);
      rc_style->engine->rc_style_to_style (style, rc_style);
    }
1274

1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
  return style;
}

/* Reuses or frees rc_styles */
static GtkStyle *
gtk_rc_style_init (GSList *rc_styles)
{
  gint i;

  GtkStyle *style = NULL;

  if (!realized_style_ht)
    realized_style_ht = g_hash_table_new ((GHashFunc)gtk_rc_styles_hash,
					   (GCompareFunc)gtk_rc_styles_compare);

  style = g_hash_table_lookup (realized_style_ht, rc_styles);
1291
1292

  if (!style)
Elliot Lee's avatar
Elliot Lee committed
1293
    {
1294
1295
      GtkRcStyle *proto_style;
      GSList *tmp_styles;
1296
      
1297
1298
      proto_style = gtk_rc_style_new ();

1299
      tmp_styles = rc_styles;