This quick reference lists classes and methods that you can use to develop simple programs in GtkSharp, targeting GTK 3.
If you want to go beyond what's listed here, unfortunately there is no API documentation for GtkSharp for GTK 3. You can look at the Gtk# 2 documentation, since most GTK 2 classes and methods also exist in GTK 3. Or you could look at the GTK 3 API documentation for C or for Python, since most GTK class/method names in C# will be similar (especially to the Python-based API).
All properties and methods below are public unless otherwise specified.
Context
| PointD | Rectangle
| TextExtents
CairoHelper
| Pixbuf
|
Rectangle | RGBA
Application
|
Box
|
Button
|
Dialog
|
DrawingArea
|
FileChooserDialog
|
HBox
| Label |
Menu
| MenuBar | MenuItem
| VBox | Widget
| Window
Cairo is a library for drawing 2-dimensional graphics. GTK integrates closely with Cairo, and most GTK applications use Cairo for visual output.
A Context
is used for
drawing graphics. Typically you
will use a Context
object
to draw on a window in the
On
Drawn
handler of a Widget
subclass.
Be aware that methods such as Arc()
,
LineTo()
and Rectangle()
do not draw - they merely add a geometric shape to the current
path. After you call these methods, you must call either Stroke()
or Fill()
to draw to the output surface.
Surface
.void Arc (double x, double y, double radius,
double angle1, double angle2);
angle1
to angle2
to the current path, centered at (x, y). To draw a circle, pass
angle1
= 0.0 and angle2
= 2 * Math.PI
.void ClosePath ();
void Fill ();
void LineTo (double x, double y);
void
LineTo (PointD p);
double LineWidth { set; get; }
void
MoveTo (PointD p);
void Paint();
CairoHelper.SetSourcePixbuf()
.void Rectangle (double x, double y, double
width, double height);
void Rectangle (Rectangle rectangle);
void Scale (double sx, double sy);
void SelectFontFace (string family, FontSlant
slant, FontWeight weight);
family
should be a generic font family
such as "serif", "sans-serif" or "monospace",
or the name of a specific font family available on your system.
slant
should be FontSlant.Normal
or
FontSlant.Italic
.
weight
should be
FontWeight.Normal
or FontWeight.Bold
.
void SetFontSize (double
scale);
void SetSourceRGB (double red, double green,
double blue);
SetSourceColor
,
below.)Surface
as a source for
drawing at the position (x, y) in the destination. Typically
you will make a following call to Paint()
to perform the actual drawing operation.void ShowText (string text);
void Stroke ();
TextExtents TextExtents (string text);
TextExtents
object with
information about the size of the given text in the currently
selected font.void Translate (double tx, double ty);
An ImageSurface
is a
Cairo surface that holds pixel data in memory. You can draw onto an
ImageSurface
, and then later
use the ImageSurface
as
a source for other drawing operations.
ImageSurface
with the given
width and height. Typically
Format
will
be either Format.Rgb24
,
meaning 24-bit color with 8 bits for each color channel (red, green,
blue), or Format.Argb32
,
which is similar but also includes an 8-bit alpha (transparency)
channel.A PointD represents an (x, y) point. (The "D" stands for "double".)
PointD (double x, double
y);
A Rectangle represents a rectangle on the (x,y)
plane. Note that this class (Cairo.Rectangle
)
is distinct from Gdk.Rectangle
, which
appears below.
Any kind of surface on which Cairo can draw.
A TextExtents
structure represents layout information about a particular piece of
text. Be aware that the values in this structure are sizes in screen
pixels, independent of any coordinate transformation in the graphics
context.
MoveTo
before a call to ShowText
)
to the leftmost part of the text glyphs.
YBearing
is
negative, and XBearing
is
slightly negative:Here is how to draw a string s centered at the point (x, y):
TextExtents te = context.TextExtents(s); context.MoveTo(x - (te.Width / 2 + te.XBearing), y - (te.Height / 2 + te.YBearing)); context.ShowText(s);
GDK is a low-level library that sits underneath GTK.
This class contains a few static utility methods for working with Cairo. (There's also a CairoHelper class in the Gtk namespace, so type "Gdk.CairoHelper" to use this one.)
Pixbuf
to use for drawing to a
Cairo context at position (x, y). Typically you will follow this
with a call to context.Paint()
to draw
the image.A Pixbuf
holds an
image in memory. You can read a Pixbuf
from a file and use Cairo to render it to the display.
Pixbuf
from a file in JPG, PNG
or TIFF format. Throws an exception if the file is not found.Marshal.Copy
, or by using unsafe
C# language constructs that let you access C data directly.NChannels
* Width
, but may be a larger value if there are padding bytes
at the end of each row.type
can be
"jpeg" or "png". Return true if the save
succeeded.PixBuf
.
InterpType
is an interpolation type,
and can be e.g. InterpType.Bilinear
for
a reasonable balance between quality and speed, or InterpType.Hyper
which produces
a higher-quality image but is slower.A Rectangle
represents a rectangle on the (x,y) plane. Note that this class
(Gdk
.Rectangle
)
is distinct from Cairo
.Rectangle
,
which appears above.
Rectangle
.Rectangle
.An RGBA represents a color.
TimeoutHandler
be called at regular intervals of interval
milliseconds.TimeoutHandler
should continue, or false to cancel the calls.An HBox
or VBox
,
used for arranging child widgets.
Box
.expand
is true, the child widget
will expand to use as much space as it is given. If fill
is true, the child widget will request as much space as is
available. padding
is the size (in
pixels) of a border to place around the child widget.Button
with the given
label.A dialog box.
DialogFlags.Modal
to create a modal dialog. button_data indicates a set of buttons to
display at the bottom of the dialog. Each such
button is represented by a pair of values: a button
text string (typically
a stock string such as Stock.Add
)
followed
by a value to be returned if that button is selected
(typically a response code such as ResponseType.Ok
or ResponseType.Cancel
).ResponseType.Accept
or ResponseType.Cancel
, cast to an int
.Destroy()
to destroy it - otherwise it will remain visible on the screen.A DrawingArea
is a
canvas where the program can draw graphics. Use a Cairo context to
draw on a DrawingArea
.
DrawingArea
.A standard dialog box that prompts the user to select a file to be opened or created.
FileChooserDialog
.title
is a
string that will appear in the dialog box's title bar.parent
is the top-level window that
will own this dialog, typically the application's main window.action
is a FileChooserAction
,
an enumeration whose values include Open
(open mode: the user can only choose
an existing file) or Save
(save mode:
the user can either choose an existing file, or type in a new
filename).Stock.Open
or
Stock.Cancel
) and a response code to be
returned when that button is selected (e.g. ResponseType.Accept
or ResponseType.Cancel
).new FileChooserDialog( "Open...", this, FileChooserAction.Open, Stock.Cancel, ResponseType.Cancel, Stock.Open, ResponseType.Accept);
An HBox contains one or more widgets that are arranged in a horizontal row.
H
Box
.A widget that displays a text label, which is often a fixed string.
A top-level menu or submenu.
Menu
.Menu
.The menu bar that appears at the top of a window.
MenuBar
.A MenuItem
is an item
that appears in a menu. It may itself contain a submenu of
child MenuItem
objects.
MenuItem
with the given label.MenuItem
fires when the
user selects it.MenuItem
.A VBox contains one or more widgets that are arranged in a vertical column.
VBox
.A widget is a visual element such as a button, check box, scrollbar, or text area. (In Windows programming these are called controls, but "widget" is the usual Unix term.) A top-level window also counts as a widget.
Gdk.EventMask
, which is an
enumeration with one value for each possible event type,
including ButtonPressMask
,
ButtonReleaseMask
and PointerMotionMask.
AddEvents((int) (EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.PointerMotionMask));
Allocation
property is a
Gdk.Rectangle
representing the widget's
current size and position within its parent widget.Window
,
then Allocation.X
and Allocation.Y
will be 0, and Allocation.Width
and
Allocation.Height
will be the size of
the client area of the window.EventButton
object
has the following properties:ModifierType State { get; }
A value indicating which modifier key(s) (e.g.
Control, Shift) were held down as the mouse button was pressed.
ModifierType
is an enumeration including
values ControlMask
and ShiftMask
.
EventType Type { get; }
One of the following values indicating the type of click:
EventType.ButtonPress
EventType.TwoButtonPress
(a double click)
EventType.ThreeButtonPress
(a triple click)
Note that if the user
double-clicks the mouse, you will receive a ButtonPress
event for each click, and also a TwoButtonPress
event.
double X { get; }
double
Y { get; }
The X and Y coordinates of a button press, relative to the window.
EventKey
object has the following properties:Gdk.Key Key { get; }
The key that the user pressed or released. Gdk.Key
is an
enumeration with one value for each physical key on a keyboard. Some
useful values include Gdk.Key.Left
and
Gdk.Key.Right
(the left and right arrow
keys), Gdk.Key.space
, Gdk.Key.Return
and Gdk.Key.Delete
. See the full list of
values here.
EventMotion
object
has the following properties:double X { get; }
double Y { get; }
The X and Y coordinates of the mouse pointer, relative to the window.
Window
, this handler will be called
when the user has resized the window.OnDrawn
handler in the near
future.A window that appears on the user's desktop. (There's also a Window class in the Gdk namespace, so type "Gtk.Window" to use this one.)
ShowAll()
to make it appear.Window
. A
Window
can directly contain only a
single widget; to hold more widgets, add a container widget such as
a VBox
and place the child widgets in
that container.