GTK# Quick Reference

This quick reference lists useful classes and methods that you can use to develop simple programs in GTK# 2.0. For more information, see the official documentation.

All properties and methods below are public unless otherwise specified.

== Index ==

== Cairo namespace ==

Context : IDisposable

A Context is used for drawing graphics.

void Arc (double x, double y, double radius, double angle1, double angle2);

Add an arc from 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 ();

Close the current path, returning to its starting point.

void Fill ();

Fill in the current path with the current color.

Surface getTarget();

Get the Surface underlying this context.

void LineTo (double x, double y);
void LineTo (PointD p);

Add a line from the current point to the given point.

double LineWidth { set; get; }

Set or get the width of the stroke.

void MoveTo (double x, double y);
void MoveTo (PointD p);

Begin a new path at the given point.

void Paint();

Paint the current source everywhere in the the current clip region. Typically you will use this method for drawing an image that you have selected using CairoHelper.SetSourcePixbuf().

void Rectangle (double x, double y, double width, double height);
void Rectangle (Rectangle rectangle);

Add a rectangle to the current path.

void Scale (double sx, double sy);

Add a scaling transformation to the context's transformation matrix.

void SelectFontFace (string family, FontSlant slant, FontWeight weight);

Select a font to use for drawing text.

void SetFontSize (double scale);

Set the font size to use for drawing text.

void SetSourceRGB (double red, double green, double blue);

Set the color to be used for drawing to a Cairo context. (To set the color to a GDK color, see SetSourceColor, below.)

void ShowText (string text);

Draw text at the current position.

void Stroke ();

Draw the current path without filling it.

TextExtents TextExtents (string text);

Get a TextExtents object with information about the size of the given text in the currently selected font.

void Translate (double tx, double ty);

Add a translation to the context's transformation matrix.

PointD

A PointD represents an (x, y) point.

PointD (double x, double y);

Construct a point from its x and y components.

Rectangle

A Rectangle represents a rectangle on the (x,y) plane.

Rectangle (double x, double y, double width, double height);

Construct a Rectangle.

double Height { get; }
double Width { get; }
double X { get; }
double Y { get; }

Retrive the components of a Rectangle.

Surface : IDisposable

A Surface is part of a Cairo context. You should dispose a context's Surface when you are finished with the context.

TextExtents

A TextExtents structure represents layout information about a particular piece of text. Be aware that the values in this structure are sizes in user-space coordinates (i.e. screen pixels), independent of any coordinate transformation in the graphics context.

double Height { get; }

The height of the text.

double Width { get; }

The width of the text.

double XBearing { get; }

The horizontal distance from the origin (the point specified by MoveTo) to the leftmost part of the text glyphs.

double YBearing { get; }

The vertical distance from the origin (the point specified by MoveTo) to the topmost part of the text glyphs. Typically this will be negative.

== Gdk namespace ==

CairoHelper

static Cairo.Context Create (Gdk.Window window);

Create a Cairo context to draw on the given GDK window. When you are done drawing with the context, you should call Dispose() both on the context and on its underlying Surface.

static void SetSourceColor (Cairo.Context context, Color color);

Set the color to be used for drawing to the given Cairo context.

static void SetSourcePixbuf (Cairo.Context context, Pixbuf pixbuf, double x, double y);

Select a 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.

Color

Color (byte red, byte green, byte blue);

Construct a color from the given red, green and blue components.

static bool Parse (string name, ref Color color);

Parse a given name, placing the result in the given Color. You can pass any X11 color name such as "red", "cornflower" or "dark slate gray". Alternatively, you can pass a string containing the red, green and blue components in hexadecimal; for example, "#3050b2" specifies the components red = 3016, green = 5016 and blue = b216. Returns true if the parse succeeded.

Event

An event such as a key press or mouse click. There are subclasses for particular event types.

EventButton : Event

An event representing a mouse button click.

EventType Type { get; }

One of the following values indicating the type of 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 : Event

An event representing a key press on the keyboard.

Key Key { get; }

The key that the user pressed or released.

EventMask

enum EventMask

An enum that has one value for each possible event type. Values include

EventMotion : Event

An event representing a mouse move.

double X { get; }
double Y { get; }

The X and Y coordinates of the mouse pointer, relative to the window.

Key

An enum with one value for each physical key on a keyboard; for example, Key.Left and Key.Right are the left and right arrow keys. See the full list of values here.

Pixbuf

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 (string filename);

Read a Pixbuf from a file in JPG, PNG or TIFF format. Throws an exception if the file is not found.

Window

A GDK window is a rectangular region on the screen. This is a lower-level object than a GTK window. To draw on a GDK window, use CairoHelper.Create to create a Cairo context.

== GLib namespace ==

Timeout

static uint Add (uint interval, TimeoutHandler handler);

Request that the given TimeoutHandler be called at regular intervals of interval milliseconds.

TimeoutHandler

delegate bool TimeoutHandler ();

Return true if calls to this TimeoutHandler should continue, or false to cancel the calls.

== Gtk namespace ==

Application

static void Init ();

Initialize GTK. Call this before calling any other GTK methods in your application.

static void Run ();

Run the application's main loop.

static void Quit ();

Quit the application.

Button : Widget

Button (string label);

Construct a Button with the given label.

event EventHandler Clicked;

Called when the user has clicked this button.

DrawingArea : Widget

A DrawingArea is a canvas where the program can draw graphics. Use a Cairo context to draw on a DrawingArea.

void AddEvents (int events);

Ask to receive the given event(s), which should be cast from a Gdk.EventMask.

Gdk.Window GdkWindow { get; }

Get the GDK window underlying this widget.

void ModifyBg (StateType state, Gdk.Color color);

Modify the background color of this widget. Usually you will pass StateType.Normal as the first argument.

void QueueDraw ();

Ask for an ExposeEvent to be sent to redraw the widget.

events

event ButtonPressEventHandler ButtonPressEvent;

Called when a mouse button has been pressed. The event handler should have this signature:

delegate void ButtonPressEventHandler (object o, ButtonPressEventArgs args);

args.Event is a Gdk.EventButton with information about the event.

event ButtonReleaseEventHandler ButtonReleaseEvent;

Called when a mouse button has been released. The event handler should have this signature:

delegate void ButtonReleaseEventHandler (object o, ButtonReleaseEventArgs args)

args.Event is a Gdk.EventButton with information about the event.

event ExposeEventHandler ExposeEvent;

Called to ask the widget to repaint itself. The event handler should have this signature:

delegate void ExposeEventHandler (object o, ExposeEventArgs args);

Typically the widget will allocate a Cairo context and will use it to draw graphics:

Context c = CairoHelper.Create(GdkWindow);
...
c.GetTarget().Dispose();
c.Dispose();

Return true to indicate that the event has been handled.

event MotionNotifyEventHandler MotionNotifyEvent;

Called when the mouse has moved. The event handler should have this signature:

delegate void MotionNotifyEventHandler (object o, MotionNotifyEventArgs args);

args.Event is a Gdk.EventMotion with information about the event.

Entry : Widget

An Entry represents a text field used for entering and/or displaying text.

event EventHandler Changed;

An event that is raised whenever the text in this Entry changes.

bool IsEditable { get; set; }

True if the user can edit the text in this Entry.

string Text { get; set; }

The text in this Entry.

Label : Widget

A Label is a fixed piece of text, typically used as a label for some other widget.

Label (string s)

Create a new Label with the given string inside it.

Table : Widget

A Table lays out child widgets in rows and columns.

Table (uint rows, uint columns, bool homogeneous);

Construct a Table with the given number of rows and columns.

If homogeneous is true, all table cells will have the same size. If it is false, rows and columns may have various sizes. In that case, each individual row and column will be as large as necessary to hold all the cells it contains.

Attach (Widget widget, uint left, uint right, uint top, uint bottom);

Add a child widget to this Table. The widget may occupy any number of rows and columns. Specifically, the widget will occupy rows top through bottom - 1, and columns left through right – 1. This means that the widget will be (bottom - top) rows high and (right - left) columns wide.

Widget

void ShowAll ();

Show this widget and any widgets it contains.

Window : Widget

A window that appears on the user's desktop.

Window (string title);

Construct a window with the given title. The window will not initially be visible; you must call ShowAll() to make it appear.

void Add (Widget widget);

Add the given widget to this window.

void Resize (int width, int height);

Resize a window (just as if the user had resized it manually).

events

event DeleteEventHandler DeleteEvent;

Called when the user has clicked a window's close button. The event handler should have this signature:

delegate void DeleteEventHandler (object o, DeleteEventArgs args);

event KeyPressEventHandler KeyPressEvent;

Called when the user has pressed a key on the keyboard. The event handler should have this signature:

delegate void KeyPressEventHandler (object o, KeyPressEventArgs args);

args.Event is a Gdk.EventKey with information about the event.

event KeyReleaseEventHandler KeyReleaseEvent;

Called when the user has released a key on the keyboard. The event handler should have this signature:

delegate void KeyReleaseEventHandler (object o, KeyReleaseEventArgs args);

args.Event is a Gdk.EventKey with information about the event.