GTK#
A closer look at Buttons
GTK# offers not just the normal button, but some other types,
derived from it.
Using Stock Icons
Up to now, we only created Buttons containing a label. It's
possible to create a Button from Stock Icon, which is very good for
consistency in Gnome apps:
Common look & feel: Toolbar with 4 Stock Icons.
Button stockbutton = Button.FromStock(stocktype);
Gtk# offers a wide range of good-looking, well designed and
accessible icons. Anopther benfit of Stock Icons is, that they are
available in many languages, and there is no need, to translate
them. stocktype must be one of these values:
Gtk.Stock.Add
Gtk.Stock.Apply
Gtk.Stock.Bold
Gtk.Stock.Cancel
Gtk.Stock.Cdrom
Gtk.Stock.Clear
Gtk.Stock.Close
Gtk.Stock.Convert
Gtk.Stock.Copy
Gtk.Stock.Cut
Gtk.Stock.Delete
Gtk.Stock.DialogError
Gtk.Stock.DialogInfo
Gtk.Stock.DialogQuestion
Gtk.Stock.DialogWarning
Gtk.Stock.StockDnd
Gtk.Stock.DndMultiple
Gtk.Stock.Execute
Gtk.Stock.FIND
Gtk.Stock.FIND_AND_REPLACE
Gtk.Stock.FLOPPY
Gtk.Stock.GOTO_BOTTOM
Gtk.Stock.GOTO_FIRST
Gtk.Stock.GOTO_LAST
Gtk.Stock.GOTO_TOP
Gtk.Stock.GO_BACK
Gtk.Stock.GO_DOWN
Gtk.Stock.GO_FORWARD
Gtk.Stock.GO_UP
Gtk.Stock.HELP
Gtk.Stock.HOME
Gtk.Stock.INDEX
Gtk.Stock.ITALIC
Gtk.Stock.JUMP_TO
Gtk.Stock.JUSTIFY_CENTER
Gtk.Stock.JUSTIFY_FILL
Gtk.Stock.JUSTIFY_LEFT
Gtk.Stock.JUSTIFY_RIGHT
Gtk.Stock.MISSING_IMAGE
Gtk.Stock.NEW
Gtk.Stock.NO
Gtk.Stock.OK
Gtk.Stock.OPEN
Gtk.Stock.PASTE
Gtk.Stock.PREFERENCES
Gtk.Stock.PRINT
Gtk.Stock.PRINT_PREVIEW
Gtk.Stock.PROPERTIES
Gtk.Stock.QUIT
Gtk.Stock.REDO
Gtk.Stock.REFRESH
Gtk.Stock.REMOVE
Gtk.Stock.REVERT_TO_SAVED
Gtk.Stock.SAVE
Gtk.Stock.SAVE_AS
Gtk.Stock.SELECT_COLOR
Gtk.Stock.SELECT_FONT
Gtk.Stock.SORT_ASCENDING
Gtk.Stock.SORT_DESCENDING
Gtk.Stock.SPELL_CHECK
Gtk.Stock.STOP
Gtk.Stock.STRIKETHROUGH
Gtk.Stock.UNDELETE
Gtk.Stock.UNDERLINE
Gtk.Stock.UNDO
Gtk.Stock.YES
Gtk.Stock.ZOOM_100
Gtk.Stock.ZOOM_FIT
Gtk.Stock.ZOOM_IN
Gtk.Stock.ZOOM_OUT
Custom Buttons
You can put pretty much everything in the button. A often used
example is a button with icon and label.
[TODO]
Toggle Buttons
ToggleButtons are similiar to the normal button, but when clicked,
may be depressed, and when you click again, they will pop back up.
Click again, and they will pop back down. When popped down, the
Property Active has the value "true".
using Gtk;
using GtkSharp;
using System;
using System.Drawing;
public class togglebuttons
{
public static void Main(string[] args)
{
Application.Init();
Window window = new Window("toggle buttons");
window.DeleteEvent += new DeleteEventHandler (delete_event);
/* Creating a new ToggleButton*/
ToggleButton togglebutton = new ToggleButton ("button1");
togglebutton.Clicked += new EventHandler (clickedCallback);
window.Add(togglebutton);
window.ShowAll();
Application.Run();
}
static void delete_event (object obj, DeleteEventArgs args)
{
Application.Quit();
}
static void clickedCallback (object obj, EventArgs args)
{
/* Check Active Property */
if (((ToggleButton) obj).Active)
Console.WriteLine ("ToggleButton clicked, I'm activating");
}
}
Check Buttons
Check Buttons are squares with text besides them. From a
programmers point of view, they work like ToggleButtons.
CheckButton cb1 = new CheckButton ("CheckButton 1");
Radio Buttons
[TODO]: Sample needs explanation.
using Gtk;
using GtkSharp;
using System;
using System.Drawing;
public class radiobuttons
{
static GLib.SList group = null;
static void delete_event (object obj, DeleteEventArgs args)
{
Application.Quit();
}
static void exitbutton_event (object obj, EventArgs args)
{
Application.Quit();
}
public static void Main(string[] args)
{
Application.Init();
Window window = new Window("radio buttons");
window.DeleteEvent += new DeleteEventHandler (delete_event);
window.BorderWidth = 0;
VBox box1 = new VBox (false, 0);
window.Add(box1);
box1.Show();
VBox box2 = new VBox (false, 10);
box2.BorderWidth = 10;
box1.PackStart(box2, true, true, 0);
box2.Show();
RadioButton radiobutton = new RadioButton (null, "button1");
box2.PackStart(radiobutton, true, true, 0);
radiobutton.Show();
group = radiobutton.Group;
RadioButton radiobutton2 = new RadioButton(group, "button2");
radiobutton2.Active = true;
box2.PackStart(radiobutton2, true, true, 0);
radiobutton2.Show();
RadioButton radiobutton3 = RadioButton.NewWithLabelFromWidget(radiobutton, "button3");
box2.PackStart(radiobutton3, true, true, 0);
radiobutton3.Show();
HSeparator separator = new HSeparator ();
box1.PackStart (separator,false, true, 0);
separator.Show();
VBox box3 = new VBox(false, 10);
box3.BorderWidth = 10;
box1.PackStart(box3,false, true, 0);
box3.Show();
Button button = new Button ("close");
button.Clicked += new EventHandler (exitbutton_event);
box3.PackStart(button, true, true, 0);
button.CanDefault = true;
button.GrabDefault();
button.Show();
window.ShowAll();
Application.Run();
}
}
Miscellaneous Widgets
This section describes some often used widgets, but is not a
complete reference. A more complete listing will be available in
the GTK# port of the GTK+ 2.0 tutorial or the GTK# documentation.
Labels
Creating a new Label...
Label label1 = new Label("Caption");
A new feature makes it possible, to use simple styles in label
captions. It's called GMarkup and is similiar to HTML:
Label label1 = new Label("A <span color=\"blue\">blue</span> <big>and</big> <b>bold</b> label");
This line is rendered like that:

The feature is possible, thanks to pango, that is discussed later.
The label text can be justified using:
label1.Justify = jtype;
jtype must be a value of the Justification enumeration:
Justification.Left
Justification.Right
Justification.Center
Justification.Fill
The label widget is also capable of line wrapping the text
automatically. This can be activated using:
label1.LineWrap = true;
The wrap argument takes a true or false value.
The text can be underlined:
label1.Pattern = "_________________________ _ _________ _ ______ __ _______ ___";
An example application, using labels can be viewed here:
label.cs
Using Tooltips
Tooltips are boxes with some text, that can be shown, when the
mouse moves over a widget.
Tooltips tooltip1 = new Tooltips();
tooltip1.SetTip(button1, "Click on this button to exit", null)
You can use the same tooltip object for all widgets. This makes
sense, because you can then enable/disable all tooltips easily:
tooltip1.Enable();
tooltip1.Disable();
Progress Bars
[TODO]
Statusbar
[TODO]
Color Selection
[TODO]
File Selection
The Gnome FileOpen/Close Dialog. [TODO]
Font Selection
[TODO]
Message Dialog
[TODO]
Tree View
[TODO]
Text View
[TODO]
Using Drag & Drop
Widget Overview
Widget Hierarchy
Here is the class hierarchy tree used to implement widgets:
GObject
|
GtkObject
+GtkWidget
| +GtkMisc
| | +GtkLabel
| | | `GtkAccelLabel
| | +GtkArrow
| | `GtkImage
| +GtkContainer
| | +GtkBin
| | | +GtkAlignment
| | | +GtkFrame
| | | | `GtkAspectFrame
| | | +GtkButton
| | | | +GtkToggleButton
| | | | | `GtkCheckButton
| | | | | `GtkRadioButton
| | | | `GtkOptionMenu
| | | +GtkItem
| | | | +GtkMenuItem
| | | | +GtkCheckMenuItem
| | | | | `GtkRadioMenuItem
| | | | +GtkImageMenuItem
| | | | +GtkSeparatorMenuItem
| | | | `GtkTearoffMenuItem
| | | +GtkWindow
| | | | +GtkDialog
| | | | | +GtkColorSelectionDialog
| | | | | +GtkFileSelection
| | | | | +GtkFontSelectionDialog
| | | | | +GtkInputDialog
| | | | | `GtkMessageDialog
| | | | `GtkPlug
| | | +GtkEventBox
| | | +GtkHandleBox
| | | +GtkScrolledWindow
| | | `GtkViewport
| | +GtkBox
| | | +GtkButtonBox
| | | | +GtkHButtonBox
| | | | `GtkVButtonBox
| | | +GtkVBox
| | | | +GtkColorSelection
| | | | +GtkFontSelection
| | | | `GtkGammaCurve
| | | `GtkHBox
| | | +GtkCombo
| | | `GtkStatusbar
| | +GtkFixed
| | +GtkPaned
| | | +GtkHPaned
| | | `GtkVPaned
| | +GtkLayout
| | +GtkMenuShell
| | | +GtkMenuBar
| | | `GtkMenu
| | +GtkNotebook
| | +GtkSocket
| | +GtkTable
| | +GtkTextView
| | +GtkToolbar
| | `GtkTreeView
| +GtkCalendar
| +GtkDrawingArea
| | `GtkCurve
| +GtkEditable
| | +GtkEntry
| | `GtkSpinButton
| +GtkRuler
| | +GtkHRuler
| | `GtkVRuler
| +GtkRange
| | +GtkScale
| | | +GtkHScale
| | | `GtkVScale
| | `GtkScrollbar
| | +GtkHScrollbar
| | `GtkVScrollbar
| +GtkSeparator
| | +GtkHSeparator
| | `GtkVSeparator
| +GtkInvisible
| +GtkPreview
| `GtkProgressBar
+GtkAdjustment
+GtkCellRenderer
| +GtkCellRendererPixbuf
| +GtkCellRendererText
| +GtkCellRendererToggle
+GtkItemFactory
+GtkTooltips
`GtkTreeViewColumn