Commit c818ee74 authored by Christian Persch's avatar Christian Persch

widget: Add font-scale property

To implement zoom, add font-scale property.

Conflicts:
	src/vte-private.h
	src/vte.c
	src/vteapp.c
parent 916a5cb9
......@@ -18,6 +18,8 @@ vte_terminal_paste_clipboard
vte_terminal_copy_primary
vte_terminal_paste_primary
vte_terminal_set_size
vte_terminal_set_font_scale
vte_terminal_get_font_scale
vte_terminal_set_audible_bell
vte_terminal_get_audible_bell
vte_terminal_set_visible_bell
......@@ -43,7 +45,6 @@ vte_terminal_get_cursor_blink_mode
vte_terminal_set_cursor_blink_mode
vte_terminal_set_scrollback_lines
vte_terminal_set_font
vte_terminal_set_font_from_string
vte_terminal_get_font
vte_terminal_get_has_selection
vte_terminal_set_word_chars
......
......@@ -111,6 +111,9 @@ G_BEGIN_DECLS
#define VTE_COLOR_SOURCE_ESCAPE 0
#define VTE_COLOR_SOURCE_API 1
#define VTE_FONT_SCALE_MIN (.25)
#define VTE_FONT_SCALE_MAX (4.)
#define I_(string) (g_intern_static_string(string))
typedef enum {
......@@ -351,7 +354,9 @@ struct _VteTerminalPrivate {
/* Data used when rendering the text which does not require server
* resources and which can be kept after unrealizing. */
PangoFontDescription *unscaled_font_desc;
PangoFontDescription *fontdesc;
gdouble font_scale;
gboolean fontdirty;
glong char_ascent;
glong char_descent;
......
......@@ -168,7 +168,8 @@ enum {
PROP_SCROLL_ON_OUTPUT,
PROP_WINDOW_TITLE,
PROP_WORD_CHARS,
PROP_VISIBLE_BELL
PROP_VISIBLE_BELL,
PROP_FONT_SCALE
};
/* these static variables are guarded by the GDK mutex */
......@@ -7512,8 +7513,38 @@ vte_terminal_ensure_font (VteTerminal *terminal)
}
}
/**
* vte_terminal_set_font:
static void
vte_terminal_update_font(VteTerminal *terminal)
{
VteTerminalPrivate *pvt = terminal->pvt;
PangoFontDescription *desc;
gdouble size;
desc = pango_font_description_copy(pvt->unscaled_font_desc);
size = pango_font_description_get_size(desc);
if (pango_font_description_get_size_is_absolute(desc)) {
pango_font_description_set_absolute_size(desc, pvt->font_scale * size);
} else {
pango_font_description_set_size(desc, pvt->font_scale * size);
}
if (pvt->fontdesc) {
pango_font_description_free(pvt->fontdesc);
}
pvt->fontdesc = desc;
pvt->fontdirty = TRUE;
pvt->has_fonts = TRUE;
/* Set the drawing font. */
if (gtk_widget_get_realized (&terminal->widget)) {
vte_terminal_ensure_font (terminal);
}
}
/*
* _vte_terminal_set_font:
* @terminal: a #VteTerminal
* @font_desc: (allow-none): a #PangoFontDescription for the desired font, or %NULL
*
......@@ -7559,7 +7590,8 @@ vte_terminal_set_font(VteTerminal *terminal,
"Using default monospace font.\n");
}
same_desc = pvt->fontdesc && pango_font_description_equal (pvt->fontdesc, desc);
same_desc = pvt->unscaled_font_desc &&
pango_font_description_equal (pvt->unscaled_font_desc, desc);
/* Note that we proceed to recreating the font even if the description
* are the same. This is because maybe screen
......@@ -7567,64 +7599,73 @@ vte_terminal_set_font(VteTerminal *terminal,
* detected at font creation time and respected.
*/
g_object_freeze_notify(object);
/* Free the old font description and save the new one. */
if (terminal->pvt->fontdesc != NULL) {
pango_font_description_free(terminal->pvt->fontdesc);
if (terminal->pvt->unscaled_font_desc != NULL) {
pango_font_description_free(terminal->pvt->unscaled_font_desc);
}
pvt->fontdesc = desc;
pvt->fontdirty = TRUE;
pvt->has_fonts = TRUE;
terminal->pvt->unscaled_font_desc = desc /* adopted */;
vte_terminal_update_font(terminal);
if (!same_desc)
g_object_notify(object, "font-desc");
}
/* Set the drawing font. */
if (gtk_widget_get_realized (&terminal->widget)) {
vte_terminal_ensure_font (terminal);
}
/**
* vte_terminal_get_font:
* @terminal: a #VteTerminal
*
* Queries the terminal for information about the fonts which will be
* used to draw text in the terminal.
*
* Returns: (transfer none): a #PangoFontDescription describing the font the terminal is
* currently using to render text
*/
const PangoFontDescription *
vte_terminal_get_font(VteTerminal *terminal)
{
g_return_val_if_fail(VTE_IS_TERMINAL(terminal), NULL);
g_object_thaw_notify(object);
return terminal->pvt->unscaled_font_desc;
}
/**
* vte_terminal_set_font_from_string:
* vte_terminal_set_font_scale:
* @terminal: a #VteTerminal
* @name: (type utf8): a pango font description in string form
* @scale: the font scale
*
* A convenience function which converts @name into a #PangoFontDescription and
* passes it to vte_terminal_set_font().
* Sets the terminal's font scale to @scale.
*
* Since: 0.30
*/
void
vte_terminal_set_font_from_string(VteTerminal *terminal,
const char *name)
vte_terminal_set_font_scale(VteTerminal *terminal,
gdouble scale)
{
PangoFontDescription *font_desc = NULL;
g_return_if_fail(VTE_IS_TERMINAL(terminal));
g_return_if_fail(name != NULL);
g_return_if_fail(scale >= VTE_FONT_SCALE_MIN && scale <= VTE_FONT_SCALE_MAX);
if (name)
font_desc = pango_font_description_from_string(name);
vte_terminal_set_font(terminal, font_desc);
pango_font_description_free(font_desc);
terminal->pvt->font_scale = CLAMP(scale, VTE_FONT_SCALE_MIN, VTE_FONT_SCALE_MAX);
vte_terminal_update_font(terminal);
g_object_notify(G_OBJECT(terminal), "font-scale");
}
/**
* vte_terminal_get_font:
* vte_terminal_get_font_scale:
* @terminal: a #VteTerminal
*
* Queries the terminal for information about the fonts which will be
* used to draw text in the terminal.
* Returns: the terminal's font scale
*
* Returns: (transfer none): a #PangoFontDescription describing the font the terminal is
* currently using to render text
* Since: 0.30
*/
const PangoFontDescription *
vte_terminal_get_font(VteTerminal *terminal)
gdouble
vte_terminal_get_font_scale(VteTerminal *terminal)
{
g_return_val_if_fail(VTE_IS_TERMINAL(terminal), NULL);
return terminal->pvt->fontdesc;
g_return_val_if_fail(VTE_IS_TERMINAL(terminal), 1.);
return terminal->pvt->font_scale;
}
/* Read and refresh our perception of the size of the PTY. */
......@@ -8216,6 +8257,8 @@ vte_terminal_init(VteTerminal *terminal)
pvt->background_alpha = 1.;
pvt->selection_block_mode = FALSE;
pvt->unscaled_font_desc = pvt->fontdesc = NULL;
pvt->font_scale = 1.;
pvt->has_fonts = FALSE;
pvt->alternate_screen_scroll = TRUE;
......@@ -8528,6 +8571,7 @@ vte_terminal_finalize(GObject *object)
{
GtkWidget *widget = GTK_WIDGET (object);
VteTerminal *terminal = VTE_TERMINAL (object);
VteTerminalPrivate *pvt = terminal->pvt;
GtkClipboard *clipboard;
GtkSettings *settings;
struct vte_match_regex *regex;
......@@ -8544,6 +8588,9 @@ vte_terminal_finalize(GObject *object)
_vte_iso2022_state_free(terminal->pvt->iso2022);
/* Free the font description. */
if (pvt->unscaled_font_desc != NULL) {
pango_font_description_free(pvt->unscaled_font_desc);
}
if (terminal->pvt->fontdesc != NULL) {
pango_font_description_free(terminal->pvt->fontdesc);
}
......@@ -10761,6 +10808,9 @@ vte_terminal_get_property (GObject *object,
case PROP_VISIBLE_BELL:
g_value_set_boolean (value, vte_terminal_get_visible_bell (terminal));
break;
case PROP_FONT_SCALE:
g_value_set_double (value, vte_terminal_get_font_scale (terminal));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
......@@ -10847,6 +10897,9 @@ vte_terminal_set_property (GObject *object,
case PROP_VISIBLE_BELL:
vte_terminal_set_visible_bell (terminal, g_value_get_boolean (value));
break;
case PROP_FONT_SCALE:
vte_terminal_set_font_scale (terminal, g_value_get_double (value));
break;
/* Not writable */
case PROP_CURRENT_DIRECTORY_URI:
......@@ -11620,6 +11673,22 @@ vte_terminal_class_init(VteTerminalClass *klass)
VTE_DEFAULT_EMULATION,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* VteTerminal:font-scale:
*
* The terminal's font scale.
*
* Since: 0.30
*/
g_object_class_install_property
(gobject_class,
PROP_AUDIBLE_BELL,
g_param_spec_double ("font-scale", NULL, NULL,
VTE_FONT_SCALE_MIN,
VTE_FONT_SCALE_MAX,
1.,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* VteTerminal:encoding:
*
......
......@@ -91,6 +91,9 @@ struct _VteTerminalClass {
void (*resize_window)(VteTerminal* terminal, guint width, guint height);
void (*move_window)(VteTerminal* terminal, guint x, guint y);
/* FIXMEchpe: should these return gboolean and have defaul thandlers
* settings the "scale" property?
*/
void (*increase_font_size)(VteTerminal* terminal);
void (*decrease_font_size)(VteTerminal* terminal);
......@@ -220,6 +223,10 @@ void vte_terminal_select_none(VteTerminal *terminal);
void vte_terminal_set_size(VteTerminal *terminal,
glong columns, glong rows);
void vte_terminal_set_font_scale(VteTerminal *terminal,
gdouble scale);
gdouble vte_terminal_get_font_scale(VteTerminal *terminal);
/* Set various on-off settings. */
void vte_terminal_set_audible_bell(VteTerminal *terminal, gboolean is_audible);
gboolean vte_terminal_get_audible_bell(VteTerminal *terminal);
......@@ -270,7 +277,6 @@ void vte_terminal_set_scrollback_lines(VteTerminal *terminal, glong lines);
/* Set or retrieve the current font. */
void vte_terminal_set_font(VteTerminal *terminal,
const PangoFontDescription *font_desc);
void vte_terminal_set_font_from_string(VteTerminal *terminal, const char *name);
const PangoFontDescription *vte_terminal_get_font(VteTerminal *terminal);
void vte_terminal_set_allow_bold(VteTerminal *terminal, gboolean allow_bold);
gboolean vte_terminal_get_allow_bold(VteTerminal *terminal);
......
......@@ -346,12 +346,11 @@ move_window(GtkWidget *widget, guint x, guint y, gpointer data)
}
static void
adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch)
adjust_font_size(GtkWidget *widget, gpointer data, gdouble factor)
{
VteTerminal *terminal;
PangoFontDescription *desired;
gdouble scale;
glong char_width, char_height;
gint newsize;
gint columns, rows, owidth, oheight;
/* Read the screen dimensions in cells. */
......@@ -366,16 +365,8 @@ adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch)
owidth -= char_width * columns;
oheight -= char_height * rows;
/* Calculate the new font size. */
desired = pango_font_description_copy(vte_terminal_get_font(terminal));
newsize = pango_font_description_get_size(desired) / PANGO_SCALE;
newsize += howmuch;
pango_font_description_set_size(desired,
CLAMP(newsize, 4, 144) * PANGO_SCALE);
/* Change the font, then resize the window so that we have the same
* number of rows and columns. */
vte_terminal_set_font(terminal, desired);
scale = vte_terminal_get_font_scale(terminal);
vte_terminal_set_font_scale(terminal, scale * factor);
/* This above call will have changed the char size! */
char_width = vte_terminal_get_char_width (terminal);
......@@ -384,20 +375,18 @@ adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch)
gtk_window_resize(GTK_WINDOW(data),
columns * char_width + owidth,
rows * char_height + oheight);
pango_font_description_free(desired);
}
static void
increase_font_size(GtkWidget *widget, gpointer data)
{
adjust_font_size(widget, data, 1);
adjust_font_size(widget, data, 1.2);
}
static void
decrease_font_size(GtkWidget *widget, gpointer data)
{
adjust_font_size(widget, data, -1);
adjust_font_size(widget, data, 1. / 1.2);
}
static gboolean
......@@ -1040,7 +1029,11 @@ main(int argc, char **argv)
/* Set the default font. */
if (font) {
vte_terminal_set_font_from_string(terminal, font);
PangoFontDescription *desc;
desc = pango_font_description_from_string(font);
vte_terminal_set_font(terminal, desc);
pango_font_description_free(desc);
}
/* Match "abcdefg". */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment