1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 18:07:59 +00:00
putty-source/unix/gtkfont.h
Simon Tatham 3214563d8e Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.

PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.

I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!

To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.

In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
 - the 'multisel' field in dialog.h's list box structure, for which
   the GTK front end in particular recognises a difference between 1
   and 2 but nearly everything else treats as boolean
 - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
   something about the specific location of the urgent pointer, but
   most clients only care about 0 vs 'something nonzero'
 - the return value of wc_match, where -1 indicates a syntax error in
   the wildcard.
 - the return values from SSH-1 RSA-key loading functions, which use
   -1 for 'wrong passphrase' and 0 for all other failures (so any
   caller which already knows it's not loading an _encrypted private_
   key can treat them as boolean)
 - term->esc_query, and the 'query' parameter in toggle_mode in
   terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
   but can also hold -1 for some other intervening character that we
   don't support.

In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
 - the return value of plug_accepting uses the POSIXish convention of
   0=success and nonzero=error; I think if I made it bool then I'd
   also want to reverse its sense, and that's a job for a separate
   piece of work.
 - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
   represent the default and alternate screens. There's no obvious
   reason why one of those should be considered 'true' or 'positive'
   or 'success' - they're just indices - so I've left it as int.

ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.

In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.

Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-03 13:45:00 +00:00

186 lines
6.0 KiB
C

/*
* Header file for gtkfont.c. Has to be separate from unix.h
* because it depends on GTK data types, hence can't be included
* from cross-platform code (which doesn't go near GTK).
*/
#ifndef PUTTY_GTKFONT_H
#define PUTTY_GTKFONT_H
/*
* We support two entirely different drawing systems: the old
* GDK1/GDK2 one which works on server-side X drawables, and the
* new-style Cairo one. GTK1 only supports GDK drawing; GTK3 only
* supports Cairo; GTK2 supports both, but deprecates GTK, so we only
* enable it if we aren't trying on purpose to compile without the
* deprecated functions.
*
* Our different font classes may prefer different drawing systems: X
* server-side fonts are a lot faster to draw with GDK, but for
* everything else we prefer Cairo, on general grounds of modernness
* and also in particular because its matrix-based scaling system
* gives much nicer results for double-width and double-height text
* when a scalable font is in use.
*/
#if !GTK_CHECK_VERSION(3,0,0) && !defined GDK_DISABLE_DEPRECATED
#define DRAW_TEXT_GDK
#endif
#if GTK_CHECK_VERSION(2,8,0)
#define DRAW_TEXT_CAIRO
#endif
#if GTK_CHECK_VERSION(3,0,0) || defined GDK_DISABLE_DEPRECATED
/*
* Where the facility is available, we prefer to render text on to a
* persistent server-side pixmap, and redraw windows by simply
* blitting rectangles of that pixmap into them as needed. This is
* better for performance since we avoid expensive font rendering
* calls where possible, and it's particularly good over a non-local X
* connection because the response to an expose event can now be a
* very simple rectangle-copy operation rather than a lot of fiddly
* drawing or bitmap transfer.
*
* However, GTK is deprecating the use of server-side pixmaps, so we
* have to disable this mode under some circumstances.
*/
#define NO_BACKING_PIXMAPS
#endif
/*
* Exports from gtkfont.c.
*/
struct UnifontVtable; /* contents internal to gtkfont.c */
typedef struct unifont {
const struct UnifontVtable *vt;
/*
* `Non-static data members' of the `class', accessible to
* external code.
*/
/*
* public_charset is the charset used when the user asks for
* `Use font encoding'.
*/
int public_charset;
/*
* Font dimensions needed by clients.
*/
int width, height, ascent, descent;
/*
* Indicates whether this font is capable of handling all glyphs
* (Pango fonts can do this because Pango automatically supplies
* missing glyphs from other fonts), or whether it would like a
* fallback font to cope with missing glyphs.
*/
bool want_fallback;
/*
* Preferred drawing API to use when this class of font is active.
* (See the enum below, in unifont_drawctx.)
*/
int preferred_drawtype;
} unifont;
/* A default drawtype, for the case where no font exists to make the
* decision with. */
#ifdef DRAW_TEXT_CAIRO
#define DRAW_DEFAULT_CAIRO
#define DRAWTYPE_DEFAULT DRAWTYPE_CAIRO
#elif defined DRAW_TEXT_GDK
#define DRAW_DEFAULT_GDK
#define DRAWTYPE_DEFAULT DRAWTYPE_GDK
#else
#error No drawtype available at all
#endif
/*
* Drawing context passed in to unifont_draw_text, which contains
* everything required to know where and how to draw the requested
* text.
*/
typedef struct unifont_drawctx {
enum {
#ifdef DRAW_TEXT_GDK
DRAWTYPE_GDK,
#endif
#ifdef DRAW_TEXT_CAIRO
DRAWTYPE_CAIRO,
#endif
DRAWTYPE_NTYPES
} type;
union {
#ifdef DRAW_TEXT_GDK
struct {
GdkDrawable *target;
GdkGC *gc;
} gdk;
#endif
#ifdef DRAW_TEXT_CAIRO
struct {
/* Need an actual widget, in order to backtrack to its X
* screen number when creating server-side pixmaps */
GtkWidget *widget;
cairo_t *cr;
cairo_matrix_t origmatrix;
#if GTK_CHECK_VERSION(3,22,0)
GdkWindow *gdkwin;
GdkDrawingContext *drawctx;
#endif
} cairo;
#endif
} u;
} unifont_drawctx;
unifont *unifont_create(GtkWidget *widget, const char *name,
bool wide, bool bold,
int shadowoffset, bool shadowalways);
void unifont_destroy(unifont *font);
void unifont_draw_text(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len,
bool wide, bool bold, int cellwidth);
/* Same as unifont_draw_text, but expects 'string' to contain one
* normal char plus combining chars, and overdraws them all in the
* same character cell. */
void unifont_draw_combining(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len,
bool wide, bool bold, int cellwidth);
/* Return a name that will select a bigger/smaller font than this one,
* or NULL if no such name is available. */
char *unifont_size_increment(unifont *font, int increment);
/*
* This function behaves exactly like the low-level unifont_create,
* except that as well as the requested font it also allocates (if
* necessary) a fallback font for filling in replacement glyphs.
*
* Return value is usable with unifont_destroy and unifont_draw_text
* as if it were an ordinary unifont.
*/
unifont *multifont_create(GtkWidget *widget, const char *name,
bool wide, bool bold,
int shadowoffset, bool shadowalways);
/*
* Unified font selector dialog. I can't be bothered to do a
* proper GTK subclassing today, so this will just be an ordinary
* data structure with some useful members.
*
* (Of course, these aren't the only members; this structure is
* contained within a bigger one which holds data visible only to
* the implementation.)
*/
typedef struct unifontsel {
void *user_data; /* settable by the user */
GtkWindow *window;
GtkWidget *ok_button, *cancel_button;
} unifontsel;
unifontsel *unifontsel_new(const char *wintitle);
void unifontsel_destroy(unifontsel *fontsel);
void unifontsel_set_name(unifontsel *fontsel, const char *fontname);
char *unifontsel_get_name(unifontsel *fontsel);
#endif /* PUTTY_GTKFONT_H */