summaryrefslogtreecommitdiffhomepage
path: root/mixgtk/mixgtk_fontsel.c
blob: ac5c88108050c1fc0558d7380cd2fcc6cbc9010c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
/* -*-c-*- -------------- mixgtk_fontsel.c :
 * Implementation of the functions declared in mixgtk_fontsel.h
 * ------------------------------------------------------------------
 * Copyright (C) 2001, 2004, 2006, 2007, 2008, 2019 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <mixlib/mix.h>
#include "mixgtk_widgets.h"
#include "mixgtk_config.h"
#include "mixgtk_mixvm.h"
#include "mixgtk_mixal.h"
#include "mixgtk_cmd_dispatcher.h"
#include "mixgtk_device.h"
#include "mixgtk_fontsel.h"

static GtkFontChooserDialog *fontsel_dialog_ = NULL;
#define FONTSEL_ GTK_FONT_CHOOSER (fontsel_dialog_)

static const gchar *keys_[MIX_FONT_NO] = {
  "MIX.font", "Prompt.font", "Log.font", "MIXAL.font", "Devices.font",
  "Symbols.font", "Default.font"
};

static const gchar *default_font_ = "Monospace 10";

/* initialise the font selection dialog */
static void
init_fontsel_ (void)
{
  fontsel_dialog_ =
    GTK_FONT_SELECTION_DIALOG
    (mixgtk_widget_factory_get_dialog (MIXGTK_FONTSEL_DIALOG));
  g_assert (fontsel_dialog_ != NULL);
}

void
mixgtk_fontsel_set_font (mixgtk_font_t f, GtkWidget *w)
{
  const gchar *font;
  PangoFontDescription *font_desc;

  if (f >= MIX_FONT_NO || w == NULL) return;

  font = mixgtk_config_get (keys_[f]);
  if (!font)
    {
      mixgtk_config_update (keys_[f], default_font_);
      font = mixgtk_config_get (keys_[f]);
      g_assert (font);
    }

  font_desc = pango_font_description_from_string (font);

  if (font_desc)
    {
      gtk_widget_modify_font (w, font_desc);
      gtk_widget_queue_draw (w);
      pango_font_description_free (font_desc);
    }
  else
    mixgtk_config_remove (keys_[f]);

}

gboolean
mixgtk_fontsel_query_font (mixgtk_font_t f, GtkWidget **w, size_t no)
{
  gint result = GTK_RESPONSE_APPLY;
  const gchar *current;
  gboolean ret = FALSE;

  if (f >= MIX_FONT_NO) return FALSE;
  if (!fontsel_dialog_) init_fontsel_ ();

  current = mixgtk_config_get (keys_[f]);
  gtk_font_chooser_set_font (FONTSEL_, current? current : default_font_);

  while (result == GTK_RESPONSE_APPLY)
    {
      result = gtk_dialog_run (GTK_DIALOG (fontsel_dialog_));
      if (result != GTK_RESPONSE_CANCEL)
        {
          gint k;
          mixgtk_config_update
            (keys_[f],
             gtk_font_chooser_get_font (FONTSEL_));
          for (k = 0; k < no; ++k)
            mixgtk_fontsel_set_font (f, w[k]);
          ret = TRUE;
        }
    }
  gtk_widget_hide (GTK_WIDGET (fontsel_dialog_));
  return ret;
}

void
on_all_fonts_activate (void)
{
  gint result = GTK_RESPONSE_APPLY;
  const gchar *current;

  if (!fontsel_dialog_) init_fontsel_ ();

  current = mixgtk_config_get (keys_[MIX_FONT_DEFAULT]);
  gtk_font_chooser_set_font (FONTSEL_, current? current : default_font_);

  while (result == GTK_RESPONSE_APPLY)
    {
      result = gtk_dialog_run (GTK_DIALOG (fontsel_dialog_));
      if (result != GTK_RESPONSE_CANCEL)
        {
          gint k;
          current = gtk_font_chooser_get_font (FONTSEL_);

          for (k = 0; k < MIX_FONT_NO; ++k)
            mixgtk_config_update (keys_[k], current);

          mixgtk_mixal_update_fonts ();
          mixgtk_mixvm_update_fonts ();
          mixgtk_device_update_fonts ();
          mixgtk_cmd_dispatcher_update_fonts ();
        }
    }
  gtk_widget_hide (GTK_WIDGET (fontsel_dialog_));
}