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 ...@@ -18,6 +18,8 @@ vte_terminal_paste_clipboard
vte_terminal_copy_primary vte_terminal_copy_primary
vte_terminal_paste_primary vte_terminal_paste_primary
vte_terminal_set_size vte_terminal_set_size
vte_terminal_set_font_scale
vte_terminal_get_font_scale
vte_terminal_set_audible_bell vte_terminal_set_audible_bell
vte_terminal_get_audible_bell vte_terminal_get_audible_bell
vte_terminal_set_visible_bell vte_terminal_set_visible_bell
...@@ -43,7 +45,6 @@ vte_terminal_get_cursor_blink_mode ...@@ -43,7 +45,6 @@ vte_terminal_get_cursor_blink_mode
vte_terminal_set_cursor_blink_mode vte_terminal_set_cursor_blink_mode
vte_terminal_set_scrollback_lines vte_terminal_set_scrollback_lines
vte_terminal_set_font vte_terminal_set_font
vte_terminal_set_font_from_string
vte_terminal_get_font vte_terminal_get_font
vte_terminal_get_has_selection vte_terminal_get_has_selection
vte_terminal_set_word_chars vte_terminal_set_word_chars
......
...@@ -111,6 +111,9 @@ G_BEGIN_DECLS ...@@ -111,6 +111,9 @@ G_BEGIN_DECLS
#define VTE_COLOR_SOURCE_ESCAPE 0 #define VTE_COLOR_SOURCE_ESCAPE 0
#define VTE_COLOR_SOURCE_API 1 #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)) #define I_(string) (g_intern_static_string(string))
typedef enum { typedef enum {
...@@ -351,7 +354,9 @@ struct _VteTerminalPrivate { ...@@ -351,7 +354,9 @@ struct _VteTerminalPrivate {
/* Data used when rendering the text which does not require server /* Data used when rendering the text which does not require server
* resources and which can be kept after unrealizing. */ * resources and which can be kept after unrealizing. */
PangoFontDescription *unscaled_font_desc;
PangoFontDescription *fontdesc; PangoFontDescription *fontdesc;
gdouble font_scale;
gboolean fontdirty; gboolean fontdirty;
glong char_ascent; glong char_ascent;
glong char_descent; glong char_descent;
......
...@@ -168,7 +168,8 @@ enum { ...@@ -168,7 +168,8 @@ enum {
PROP_SCROLL_ON_OUTPUT, PROP_SCROLL_ON_OUTPUT,
PROP_WINDOW_TITLE, PROP_WINDOW_TITLE,
PROP_WORD_CHARS, PROP_WORD_CHARS,
PROP_VISIBLE_BELL PROP_VISIBLE_BELL,
PROP_FONT_SCALE
}; };
/* these static variables are guarded by the GDK mutex */ /* these static variables are guarded by the GDK mutex */
...@@ -7512,8 +7513,38 @@ vte_terminal_ensure_font (VteTerminal *terminal) ...@@ -7512,8 +7513,38 @@ vte_terminal_ensure_font (VteTerminal *terminal)
} }
} }
/** static void
* vte_terminal_set_font: 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 * @terminal: a #VteTerminal
* @font_desc: (allow-none): a #PangoFontDescription for the desired font, or %NULL * @font_desc: (allow-none): a #PangoFontDescription for the desired font, or %NULL
* *
...@@ -7559,72 +7590,82 @@ vte_terminal_set_font(VteTerminal *terminal, ...@@ -7559,72 +7590,82 @@ vte_terminal_set_font(VteTerminal *terminal,
"Using default monospace font.\n"); "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 /* Note that we proceed to recreating the font even if the description
* are the same. This is because maybe screen * are the same. This is because maybe screen
* font options were changed, or new fonts installed. Those will be * font options were changed, or new fonts installed. Those will be
* detected at font creation time and respected. * detected at font creation time and respected.
*/ */
g_object_freeze_notify(object);
/* Free the old font description and save the new one. */ /* Free the old font description and save the new one. */
if (terminal->pvt->fontdesc != NULL) { if (terminal->pvt->unscaled_font_desc != NULL) {
pango_font_description_free(terminal->pvt->fontdesc); pango_font_description_free(terminal->pvt->unscaled_font_desc);
} }
pvt->fontdesc = desc;
pvt->fontdirty = TRUE; terminal->pvt->unscaled_font_desc = desc /* adopted */;
pvt->has_fonts = TRUE;
vte_terminal_update_font(terminal);
if (!same_desc) if (!same_desc)
g_object_notify(object, "font-desc"); g_object_notify(object, "font-desc");
}
/* Set the drawing font. */ /**
if (gtk_widget_get_realized (&terminal->widget)) { * vte_terminal_get_font:
vte_terminal_ensure_font (terminal); * @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 * @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 * Sets the terminal's font scale to @scale.
* passes it to vte_terminal_set_font(). *
* Since: 0.30
*/ */
void void
vte_terminal_set_font_from_string(VteTerminal *terminal, vte_terminal_set_font_scale(VteTerminal *terminal,
const char *name) gdouble scale)
{ {
PangoFontDescription *font_desc = NULL; g_return_if_fail(VTE_IS_TERMINAL(terminal));
g_return_if_fail(VTE_IS_TERMINAL(terminal)); g_return_if_fail(scale >= VTE_FONT_SCALE_MIN && scale <= VTE_FONT_SCALE_MAX);
g_return_if_fail(name != NULL);
if (name) terminal->pvt->font_scale = CLAMP(scale, VTE_FONT_SCALE_MIN, VTE_FONT_SCALE_MAX);
font_desc = pango_font_description_from_string(name); vte_terminal_update_font(terminal);
vte_terminal_set_font(terminal, font_desc);
pango_font_description_free(font_desc); g_object_notify(G_OBJECT(terminal), "font-scale");
} }
/** /**
* vte_terminal_get_font: * vte_terminal_get_font_scale:
* @terminal: a #VteTerminal * @terminal: a #VteTerminal
* *
* Queries the terminal for information about the fonts which will be * Returns: the terminal's font scale
* used to draw text in the terminal.
* *
* Returns: (transfer none): a #PangoFontDescription describing the font the terminal is * Since: 0.30
* currently using to render text
*/ */
const PangoFontDescription * gdouble
vte_terminal_get_font(VteTerminal *terminal) vte_terminal_get_font_scale(VteTerminal *terminal)
{ {
g_return_val_if_fail(VTE_IS_TERMINAL(terminal), NULL); g_return_val_if_fail(VTE_IS_TERMINAL(terminal), 1.);
return terminal->pvt->fontdesc;
return terminal->pvt->font_scale;
} }
/* Read and refresh our perception of the size of the PTY. */ /* Read and refresh our perception of the size of the PTY. */
...@@ -8216,6 +8257,8 @@ vte_terminal_init(VteTerminal *terminal) ...@@ -8216,6 +8257,8 @@ vte_terminal_init(VteTerminal *terminal)
pvt->background_alpha = 1.; pvt->background_alpha = 1.;
pvt->selection_block_mode = FALSE; pvt->selection_block_mode = FALSE;
pvt->unscaled_font_desc = pvt->fontdesc = NULL;
pvt->font_scale = 1.;
pvt->has_fonts = FALSE; pvt->has_fonts = FALSE;
pvt->alternate_screen_scroll = TRUE; pvt->alternate_screen_scroll = TRUE;
...@@ -8528,6 +8571,7 @@ vte_terminal_finalize(GObject *object) ...@@ -8528,6 +8571,7 @@ vte_terminal_finalize(GObject *object)
{ {
GtkWidget *widget = GTK_WIDGET (object); GtkWidget *widget = GTK_WIDGET (object);
VteTerminal *terminal = VTE_TERMINAL (object); VteTerminal *terminal = VTE_TERMINAL (object);
VteTerminalPrivate *pvt = terminal->pvt;
GtkClipboard *clipboard; GtkClipboard *clipboard;
GtkSettings *settings; GtkSettings *settings;
struct vte_match_regex *regex; struct vte_match_regex *regex;
...@@ -8544,6 +8588,9 @@ vte_terminal_finalize(GObject *object) ...@@ -8544,6 +8588,9 @@ vte_terminal_finalize(GObject *object)
_vte_iso2022_state_free(terminal->pvt->iso2022); _vte_iso2022_state_free(terminal->pvt->iso2022);
/* Free the font description. */ /* Free the font description. */
if (pvt->unscaled_font_desc != NULL) {
pango_font_description_free(pvt->unscaled_font_desc);
}
if (terminal->pvt->fontdesc != NULL) { if (terminal->pvt->fontdesc != NULL) {
pango_font_description_free(terminal->pvt->fontdesc); pango_font_description_free(terminal->pvt->fontdesc);
} }
...@@ -10761,6 +10808,9 @@ vte_terminal_get_property (GObject *object, ...@@ -10761,6 +10808,9 @@ vte_terminal_get_property (GObject *object,
case PROP_VISIBLE_BELL: case PROP_VISIBLE_BELL:
g_value_set_boolean (value, vte_terminal_get_visible_bell (terminal)); g_value_set_boolean (value, vte_terminal_get_visible_bell (terminal));
break; break;
case PROP_FONT_SCALE:
g_value_set_double (value, vte_terminal_get_font_scale (terminal));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
...@@ -10847,6 +10897,9 @@ vte_terminal_set_property (GObject *object, ...@@ -10847,6 +10897,9 @@ vte_terminal_set_property (GObject *object,
case PROP_VISIBLE_BELL: case PROP_VISIBLE_BELL:
vte_terminal_set_visible_bell (terminal, g_value_get_boolean (value)); vte_terminal_set_visible_bell (terminal, g_value_get_boolean (value));
break; break;
case PROP_FONT_SCALE:
vte_terminal_set_font_scale (terminal, g_value_get_double (value));
break;
/* Not writable */ /* Not writable */
case PROP_CURRENT_DIRECTORY_URI: case PROP_CURRENT_DIRECTORY_URI:
...@@ -11619,7 +11672,23 @@ vte_terminal_class_init(VteTerminalClass *klass) ...@@ -11619,7 +11672,23 @@ vte_terminal_class_init(VteTerminalClass *klass)
g_param_spec_string ("emulation", NULL, NULL, g_param_spec_string ("emulation", NULL, NULL,
VTE_DEFAULT_EMULATION, VTE_DEFAULT_EMULATION,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); 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: * VteTerminal:encoding:
* *
......
...@@ -91,6 +91,9 @@ struct _VteTerminalClass { ...@@ -91,6 +91,9 @@ struct _VteTerminalClass {
void (*resize_window)(VteTerminal* terminal, guint width, guint height); void (*resize_window)(VteTerminal* terminal, guint width, guint height);
void (*move_window)(VteTerminal* terminal, guint x, guint y); 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 (*increase_font_size)(VteTerminal* terminal);
void (*decrease_font_size)(VteTerminal* terminal); void (*decrease_font_size)(VteTerminal* terminal);
...@@ -220,6 +223,10 @@ void vte_terminal_select_none(VteTerminal *terminal); ...@@ -220,6 +223,10 @@ void vte_terminal_select_none(VteTerminal *terminal);
void vte_terminal_set_size(VteTerminal *terminal, void vte_terminal_set_size(VteTerminal *terminal,
glong columns, glong rows); 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. */ /* Set various on-off settings. */
void vte_terminal_set_audible_bell(VteTerminal *terminal, gboolean is_audible); void vte_terminal_set_audible_bell(VteTerminal *terminal, gboolean is_audible);
gboolean vte_terminal_get_audible_bell(VteTerminal *terminal); gboolean vte_terminal_get_audible_bell(VteTerminal *terminal);
...@@ -270,7 +277,6 @@ void vte_terminal_set_scrollback_lines(VteTerminal *terminal, glong lines); ...@@ -270,7 +277,6 @@ void vte_terminal_set_scrollback_lines(VteTerminal *terminal, glong lines);
/* Set or retrieve the current font. */ /* Set or retrieve the current font. */
void vte_terminal_set_font(VteTerminal *terminal, void vte_terminal_set_font(VteTerminal *terminal,
const PangoFontDescription *font_desc); const PangoFontDescription *font_desc);
void vte_terminal_set_font_from_string(VteTerminal *terminal, const char *name);
const PangoFontDescription *vte_terminal_get_font(VteTerminal *terminal); const PangoFontDescription *vte_terminal_get_font(VteTerminal *terminal);
void vte_terminal_set_allow_bold(VteTerminal *terminal, gboolean allow_bold); void vte_terminal_set_allow_bold(VteTerminal *terminal, gboolean allow_bold);
gboolean vte_terminal_get_allow_bold(VteTerminal *terminal); gboolean vte_terminal_get_allow_bold(VteTerminal *terminal);
......
...@@ -346,12 +346,11 @@ move_window(GtkWidget *widget, guint x, guint y, gpointer data) ...@@ -346,12 +346,11 @@ move_window(GtkWidget *widget, guint x, guint y, gpointer data)
} }
static void static void
adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch) adjust_font_size(GtkWidget *widget, gpointer data, gdouble factor)
{ {
VteTerminal *terminal; VteTerminal *terminal;
PangoFontDescription *desired; gdouble scale;
glong char_width, char_height; glong char_width, char_height;
gint newsize;
gint columns, rows, owidth, oheight; gint columns, rows, owidth, oheight;
/* Read the screen dimensions in cells. */ /* Read the screen dimensions in cells. */
...@@ -366,16 +365,8 @@ adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch) ...@@ -366,16 +365,8 @@ adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch)
owidth -= char_width * columns; owidth -= char_width * columns;
oheight -= char_height * rows; oheight -= char_height * rows;
/* Calculate the new font size. */ scale = vte_terminal_get_font_scale(terminal);
desired = pango_font_description_copy(vte_terminal_get_font(terminal)); vte_terminal_set_font_scale(terminal, scale * factor);
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);
/* This above call will have changed the char size! */ /* This above call will have changed the char size! */
char_width = vte_terminal_get_char_width (terminal); char_width = vte_terminal_get_char_width (terminal);
...@@ -384,20 +375,18 @@ adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch) ...@@ -384,20 +375,18 @@ adjust_font_size(GtkWidget *widget, gpointer data, gint howmuch)
gtk_window_resize(GTK_WINDOW(data), gtk_window_resize(GTK_WINDOW(data),
columns * char_width + owidth, columns * char_width + owidth,
rows * char_height + oheight); rows * char_height + oheight);
pango_font_description_free(desired);
} }
static void static void
increase_font_size(GtkWidget *widget, gpointer data) increase_font_size(GtkWidget *widget, gpointer data)
{ {
adjust_font_size(widget, data, 1); adjust_font_size(widget, data, 1.2);
} }
static void static void
decrease_font_size(GtkWidget *widget, gpointer data) decrease_font_size(GtkWidget *widget, gpointer data)
{ {
adjust_font_size(widget, data, -1); adjust_font_size(widget, data, 1. / 1.2);
} }
static gboolean static gboolean
...@@ -907,7 +896,7 @@ main(int argc, char **argv) ...@@ -907,7 +896,7 @@ main(int argc, char **argv)
/* Create the terminal widget and add it to the scrolling shell. */ /* Create the terminal widget and add it to the scrolling shell. */
widget = vteapp_terminal_new(); widget = vteapp_terminal_new();
terminal = VTE_TERMINAL (widget); terminal = VTE_TERMINAL (widget);
if (!dbuffer) { if (!dbuffer) {
gtk_widget_set_double_buffered(widget, dbuffer); gtk_widget_set_double_buffered(widget, dbuffer);
} }
if (show_object_notifications) if (show_object_notifications)
...@@ -1040,7 +1029,11 @@ main(int argc, char **argv) ...@@ -1040,7 +1029,11 @@ main(int argc, char **argv)
/* Set the default font. */ /* Set the default font. */
if (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". */ /* 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