sample: Port all existing GtkDemo samples to the new APIs

The following samples crash because of an issue with the new Drawn
handler: ColorSelection, DrawingArea and Pixbuf.
The rest seem to work OK.
This commit is contained in:
Bertrand Lorentz 2011-06-13 18:00:08 +02:00
parent 9d7eec2eca
commit 3a2f01c534
13 changed files with 85 additions and 122 deletions

View file

@ -192,7 +192,7 @@ namespace GtkDemo
protected override bool OnWindowStateEvent (Gdk.EventWindowState evt) protected override bool OnWindowStateEvent (Gdk.EventWindowState evt)
{ {
if ((evt.ChangedMask & (Gdk.WindowState.Maximized | Gdk.WindowState.Fullscreen)) != 0) if ((evt.ChangedMask & (Gdk.WindowState.Maximized | Gdk.WindowState.Fullscreen)) != 0)
statusbar.HasResizeGrip = (evt.NewWindowState & (Gdk.WindowState.Maximized | Gdk.WindowState.Fullscreen)) == 0; HasResizeGrip = (evt.NewWindowState & (Gdk.WindowState.Maximized | Gdk.WindowState.Fullscreen)) == 0;
return false; return false;
} }

View file

@ -14,7 +14,7 @@ namespace GtkDemo
[Demo ("Color Selection", "DemoColorSelection.cs")] [Demo ("Color Selection", "DemoColorSelection.cs")]
public class DemoColorSelection : Gtk.Window public class DemoColorSelection : Gtk.Window
{ {
private Gdk.Color color; private Gdk.RGBA color;
private Gtk.DrawingArea drawingArea; private Gtk.DrawingArea drawingArea;
public DemoColorSelection () : base ("Color Selection") public DemoColorSelection () : base ("Color Selection")
@ -30,19 +30,22 @@ namespace GtkDemo
vbox.PackStart (frame, true, true, 0); vbox.PackStart (frame, true, true, 0);
drawingArea = new DrawingArea (); drawingArea = new DrawingArea ();
drawingArea.ExposeEvent += new ExposeEventHandler (ExposeEventCallback); drawingArea.Drawn += new DrawnHandler (DrawnCallback);
// set a minimum size // set a minimum size
drawingArea.SetSizeRequest (200,200); drawingArea.SetSizeRequest (200,200);
// set the color // set the color
color = new Gdk.Color (0, 0, 0xff); color.Red = 0;
drawingArea.ModifyBg (StateType.Normal, color); color.Green = 0;
color.Blue = 1;
color.Alpha = 1;
drawingArea.OverrideBackgroundColor (StateFlags.Normal, color);
frame.Add (drawingArea); frame.Add (drawingArea);
Alignment alignment = new Alignment (1.0f, 0.5f, 0.0f, 0.0f); Alignment alignment = new Alignment (1.0f, 0.5f, 0.0f, 0.0f);
Button button = new Button ("_Change the above color"); Button button = new Button ("_Change the above color");
button.Clicked += new EventHandler (ChangeColorCallback); button.Clicked += new EventHandler (ChangeColorCallback);
alignment.Add (button); alignment.Add (button);
vbox.PackStart (alignment); vbox.PackStart (alignment, false, false, 0);
ShowAll (); ShowAll ();
} }
@ -53,17 +56,15 @@ namespace GtkDemo
return true; return true;
} }
// Expose callback for the drawing area // Drawn callback for the drawing area
private void ExposeEventCallback (object o, ExposeEventArgs args) private void DrawnCallback (object o, DrawnArgs args)
{ {
EventExpose eventExpose = args.Event; Cairo.Context cr = args.Cr;
Gdk.Window window = eventExpose.Window;
Rectangle area = eventExpose.Area; Gdk.RGBA rgba = StyleContext.GetBackgroundColor (StateFlags.Normal);
cr.SetSourceRGBA (rgba.Red, rgba.Green, rgba.Blue, rgba.Alpha);
cr.Paint ();
window.DrawRectangle (drawingArea.Style.BackgroundGC (StateType.Normal),
true,
area.X, area.Y,
area.Width, area.Height);
args.RetVal = true; args.RetVal = true;
} }
@ -71,13 +72,13 @@ namespace GtkDemo
{ {
using (ColorSelectionDialog colorSelectionDialog = new ColorSelectionDialog ("Changing color")) { using (ColorSelectionDialog colorSelectionDialog = new ColorSelectionDialog ("Changing color")) {
colorSelectionDialog.TransientFor = this; colorSelectionDialog.TransientFor = this;
colorSelectionDialog.ColorSelection.PreviousColor = color; colorSelectionDialog.ColorSelection.SetPreviousRgba (color);
colorSelectionDialog.ColorSelection.CurrentColor = color; colorSelectionDialog.ColorSelection.CurrentRgba = color;
colorSelectionDialog.ColorSelection.HasPalette = true; colorSelectionDialog.ColorSelection.HasPalette = true;
if (colorSelectionDialog.Run () == (int) ResponseType.Ok) { if (colorSelectionDialog.Run () == (int) ResponseType.Ok) {
Gdk.Color selected = colorSelectionDialog.ColorSelection.CurrentColor; Gdk.RGBA selected = colorSelectionDialog.ColorSelection.CurrentRgba;
drawingArea.ModifyBg (StateType.Normal, selected); drawingArea.OverrideBackgroundColor (StateFlags.Normal, selected);
} }
colorSelectionDialog.Hide (); colorSelectionDialog.Hide ();

View file

@ -94,7 +94,7 @@ namespace GtkDemo
HBox hbox = new HBox (false, 8); HBox hbox = new HBox (false, 8);
hbox.BorderWidth = 8; hbox.BorderWidth = 8;
dialog.VBox.PackStart (hbox, false, false, 0); dialog.ContentArea.PackStart (hbox, false, false, 0);
Image stock = new Image (Stock.DialogQuestion, IconSize.Dialog); Image stock = new Image (Stock.DialogQuestion, IconSize.Dialog);
hbox.PackStart (stock, false, false, 0); hbox.PackStart (stock, false, false, 0);

View file

@ -22,7 +22,7 @@ namespace GtkDemo
[Demo ("Drawing Area", "DemoDrawingArea.cs")] [Demo ("Drawing Area", "DemoDrawingArea.cs")]
public class DemoDrawingArea : Gtk.Window public class DemoDrawingArea : Gtk.Window
{ {
private Pixmap pixmap = null; private Cairo.Surface surface = null;
public DemoDrawingArea () : base ("Drawing Area") public DemoDrawingArea () : base ("Drawing Area")
{ {
@ -45,7 +45,7 @@ namespace GtkDemo
// set a minimum size // set a minimum size
da.SetSizeRequest (100,100); da.SetSizeRequest (100,100);
frame.Add (da); frame.Add (da);
da.ExposeEvent += new ExposeEventHandler (CheckerboardExpose); da.Drawn += new DrawnHandler (CheckerboardDrawn);
// Create the scribble area // Create the scribble area
label = new Label ("<u>Scribble area</u>"); label = new Label ("<u>Scribble area</u>");
@ -62,7 +62,7 @@ namespace GtkDemo
frame.Add (da); frame.Add (da);
// Signals used to handle backing pixmap // Signals used to handle backing pixmap
da.ExposeEvent += new ExposeEventHandler (ScribbleExpose); da.Drawn += new DrawnHandler (ScribbleDrawn);
da.ConfigureEvent += new ConfigureEventHandler (ScribbleConfigure); da.ConfigureEvent += new ConfigureEventHandler (ScribbleConfigure);
// Event signals // Event signals
@ -84,25 +84,21 @@ namespace GtkDemo
return true; return true;
} }
private void CheckerboardExpose (object o, ExposeEventArgs args) private void CheckerboardDrawn (object o, DrawnArgs args)
{ {
const int CheckSize = 10; const int CheckSize = 10;
const int Spacing = 2; const int Spacing = 2;
DrawingArea da = o as DrawingArea; Widget widget = o as Widget;
Cairo.Context cr = args.Cr;
// It would be a bit more efficient to keep these
// GC's around instead of recreating on each expose, but
// this is the lazy/slow way.
Gdk.GC gc1 = new Gdk.GC (da.GdkWindow);
gc1.RgbFgColor = new Gdk.Color (117, 0, 117);
Gdk.GC gc2 = new Gdk.GC (da.GdkWindow);
gc2.RgbFgColor = new Gdk.Color (255, 255, 255);
int i, j, xcount, ycount; int i, j, xcount, ycount;
Gdk.Rectangle alloc = da.Allocation;
// At the start of a draw handler, a clip region has been set on
// the Cairo context, and the contents have been cleared to the
// widget's background color.
Rectangle alloc = widget.Allocation;
// Start redrawing the Checkerboard // Start redrawing the Checkerboard
xcount = 0; xcount = 0;
i = Spacing; i = Spacing;
@ -110,13 +106,12 @@ namespace GtkDemo
j = Spacing; j = Spacing;
ycount = xcount % 2; // start with even/odd depending on row ycount = xcount % 2; // start with even/odd depending on row
while (j < alloc.Height) { while (j < alloc.Height) {
Gdk.GC gc;
if (ycount % 2 != 0) if (ycount % 2 != 0)
gc = gc1; cr.SetSourceRGB (0.45777, 0, 0.45777);
else else
gc = gc2; cr.SetSourceRGB (1, 1, 1);
da.GdkWindow.DrawRectangle (gc, true, i, j, // If we're outside the clip, this will do nothing.
CheckSize, CheckSize); cr.Rectangle (i, j, CheckSize, CheckSize);
j += CheckSize + Spacing; j += CheckSize + Spacing;
++ycount; ++ycount;
@ -130,33 +125,29 @@ namespace GtkDemo
args.RetVal = true; args.RetVal = true;
} }
private void ScribbleExpose (object o, ExposeEventArgs args) private void ScribbleDrawn (object o, DrawnArgs args)
{ {
Widget widget = o as Widget; Cairo.Context cr = args.Cr;
Gdk.Window window = widget.GdkWindow;
Rectangle area = args.Event.Area;
// We use the "ForegroundGC" for the widget since it already exists, cr.SetSourceSurface (surface, 0, 0);
// but honestly any GC would work. The only thing to worry about cr.Paint ();
// is whether the GC has an inappropriate clip region set.
window.DrawDrawable (widget.Style.ForegroundGC (StateType.Normal),
pixmap,
area.X, area.Y,
area.X, area.Y,
area.Width, area.Height);
} }
// Create a new pixmap of the appropriate size to store our scribbles // Create a new surface of the appropriate size to store our scribbles
private void ScribbleConfigure (object o, ConfigureEventArgs args) private void ScribbleConfigure (object o, ConfigureEventArgs args)
{ {
Widget widget = o as Widget; Widget widget = o as Widget;
Rectangle allocation = widget.Allocation;
pixmap = new Pixmap (widget.GdkWindow, allocation.Width, allocation.Height, -1); if (surface != null)
surface.Destroy ();
// Initialize the pixmap to white var allocation = widget.Allocation;
pixmap.DrawRectangle (widget.Style.WhiteGC, true, 0, 0,
allocation.Width, allocation.Height); surface = widget.Window.CreateSimilarSurface (Cairo.Content.Color, allocation.Width, allocation.Height);
var cr = new Cairo.Context (surface);
cr.Paint ();
((IDisposable)cr).Dispose ();
// We've handled the configure event, no need for further processing. // We've handled the configure event, no need for further processing.
args.RetVal = true; args.RetVal = true;
@ -166,7 +157,7 @@ namespace GtkDemo
{ {
// paranoia check, in case we haven't gotten a configure event // paranoia check, in case we haven't gotten a configure event
if (pixmap == null) if (surface == null)
return; return;
// This call is very important; it requests the next motion event. // This call is very important; it requests the next motion event.
@ -192,19 +183,21 @@ namespace GtkDemo
// Draw a rectangle on the screen // Draw a rectangle on the screen
private void DrawBrush (Widget widget, double x, double y) private void DrawBrush (Widget widget, double x, double y)
{ {
Rectangle update_rect = new Rectangle ((int)x - 3, (int)y - 3, 6, 6); var update_rect = new Gdk.Rectangle ((int)x - 3, (int)y - 3, 6, 6);
var cr = new Cairo.Context (surface);
// Paint to the pixmap, where we store our state cr.Fill ();
pixmap.DrawRectangle (widget.Style.BlackGC, true, Gdk.CairoHelper.Rectangle (cr, update_rect);
update_rect.X, update_rect.Y,
update_rect.Width, update_rect.Height); ((IDisposable)cr).Dispose ();
widget.GdkWindow.InvalidateRect (update_rect, false);
widget.Window.InvalidateRect (update_rect, false);
} }
private void ScribbleButtonPress (object o, ButtonPressEventArgs args) private void ScribbleButtonPress (object o, ButtonPressEventArgs args)
{ {
// paranoia check, in case we haven't gotten a configure event // paranoia check, in case we haven't gotten a configure event
if (pixmap == null) if (surface == null)
return; return;
EventButton ev = args.Event; EventButton ev = args.Event;

View file

@ -18,7 +18,7 @@ namespace GtkDemo
VBox vbox = new VBox (false, 5); VBox vbox = new VBox (false, 5);
vbox.BorderWidth = 5; vbox.BorderWidth = 5;
this.VBox.PackStart (vbox, true, true, 0); this.ContentArea.PackStart (vbox, true, true, 0);
Label label = new Label ("Completion demo, try writing <b>total</b> or <b>gnome</b> for example."); Label label = new Label ("Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
label.UseMarkup = true; label.UseMarkup = true;

View file

@ -17,7 +17,7 @@ namespace GtkDemo
Resizable = false; Resizable = false;
VBox vbox = new VBox (false, 5); VBox vbox = new VBox (false, 5);
this.VBox.PackStart (vbox, true, true, 0); this.ContentArea.PackStart (vbox, true, true, 0);
vbox.BorderWidth = 5; vbox.BorderWidth = 5;
vbox.PackStart (new Label ("Expander demo. Click on the triangle for details."), false, false, 0); vbox.PackStart (new Label ("Expander demo. Click on the triangle for details."), false, false, 0);

View file

@ -184,7 +184,7 @@ namespace GtkDemo
view.WindowToBufferCoords (TextWindowType.Widget, (int) args.Event.X, (int) args.Event.Y, out x, out y); view.WindowToBufferCoords (TextWindowType.Widget, (int) args.Event.X, (int) args.Event.Y, out x, out y);
SetCursorIfAppropriate (view, x, y); SetCursorIfAppropriate (view, x, y);
view.GdkWindow.GetPointer (out x, out y, out state); view.Window.GetPointer (out x, out y, out state);
} }
// Also update the cursor image if the window becomes visible // Also update the cursor image if the window becomes visible

View file

@ -170,7 +170,7 @@ namespace GtkDemo
{ {
Gdk.Pixbuf pixbuf = pixbufLoader.Pixbuf; Gdk.Pixbuf pixbuf = pixbufLoader.Pixbuf;
pixbuf.Fill (0xaaaaaaff); pixbuf.Fill (0xaaaaaaff);
progressiveImage.FromPixbuf = pixbuf; progressiveImage.Pixbuf = pixbuf;
} }
void ProgressiveUpdatedCallback (object obj, AreaUpdatedArgs args) void ProgressiveUpdatedCallback (object obj, AreaUpdatedArgs args)

View file

@ -180,8 +180,8 @@ namespace GtkDemo
scrolledWindow.Add (textView); scrolledWindow.Add (textView);
if (IsSource) { if (IsSource) {
FontDescription fontDescription = FontDescription.FromString ("Courier 12"); FontDescription fontDescription = FontDescription.FromString ("monospace");
textView.ModifyFont (fontDescription); textView.OverrideFont (fontDescription);
textView.WrapMode = Gtk.WrapMode.None; textView.WrapMode = Gtk.WrapMode.None;
} else { } else {
// Make it a bit nicer for text // Make it a bit nicer for text

View file

@ -71,23 +71,13 @@ namespace GtkDemo
} }
// Expose callback for the drawing area // Expose callback for the drawing area
void Expose (object o, ExposeEventArgs args) void DrawnCallback (object o, DrawnArgs args)
{ {
Widget widget = (Widget) o; Cairo.Context cr = args.Cr;
Gdk.Rectangle area = args.Event.Area;
byte[] pixels;
int rowstride;
rowstride = frame.Rowstride; Gdk.CairoHelper.SetSourcePixbuf (cr, frame, 0, 0);
pixels = new byte[(frame.Height - area.Y) * rowstride]; cr.Paint ();
IntPtr src = (IntPtr)(frame.Pixels.ToInt64 () + rowstride * area.Y + area.X * 3);
Marshal.Copy (src, pixels, 0, pixels.Length);
widget.GdkWindow.DrawRgbImageDithalign (widget.Style.BlackGC,
area.X, area.Y, area.Width, area.Height,
Gdk.RgbDither.Normal,
pixels, rowstride,
area.X, area.Y);
args.RetVal = true; args.RetVal = true;
} }
@ -152,7 +142,7 @@ namespace GtkDemo
frame = new Pixbuf (Colorspace.Rgb, false, 8, backWidth, backHeight); frame = new Pixbuf (Colorspace.Rgb, false, 8, backWidth, backHeight);
drawingArea = new DrawingArea (); drawingArea = new DrawingArea ();
drawingArea.ExposeEvent += new ExposeEventHandler (Expose); drawingArea.Drawn += new DrawnHandler (DrawnCallback);
Add (drawingArea); Add (drawingArea);
timeoutId = GLib.Timeout.Add (FrameDelay, new GLib.TimeoutHandler(timeout)); timeoutId = GLib.Timeout.Add (FrameDelay, new GLib.TimeoutHandler(timeout));

View file

@ -33,7 +33,7 @@ namespace GtkDemo
Resizable = false; Resizable = false;
VBox vbox = new VBox (false, 5); VBox vbox = new VBox (false, 5);
this.VBox.PackStart (vbox, true, true, 0); this.ContentArea.PackStart (vbox, true, true, 0);
vbox.BorderWidth = 5; vbox.BorderWidth = 5;
sizeGroup = new SizeGroup (SizeGroupMode.Horizontal); sizeGroup = new SizeGroup (SizeGroupMode.Horizontal);
@ -76,7 +76,7 @@ namespace GtkDemo
// Convenience function to create a combo box holding a number of strings // Convenience function to create a combo box holding a number of strings
private ComboBox CreateComboBox (string [] strings) private ComboBox CreateComboBox (string [] strings)
{ {
ComboBox combo = ComboBox.NewText (); ComboBoxText combo = new ComboBoxText ();
foreach (string str in strings) foreach (string str in strings)
combo.AppendText (str); combo.AppendText (str);

View file

@ -71,7 +71,7 @@ namespace GtkDemo
textView.AddChildAtAnchor (button, buttonAnchor); textView.AddChildAtAnchor (button, buttonAnchor);
button.ShowAll (); button.ShowAll ();
ComboBox combo = ComboBox.NewText (); ComboBoxText combo = new ComboBoxText ();
combo.AppendText ("Option 1"); combo.AppendText ("Option 1");
combo.AppendText ("Option 2"); combo.AppendText ("Option 2");
combo.AppendText ("Option 3"); combo.AppendText ("Option 3");
@ -154,20 +154,6 @@ namespace GtkDemo
tag.Background = "red"; tag.Background = "red";
buffer.TagTable.Add (tag); buffer.TagTable.Add (tag);
// The C gtk-demo passes NULL for the drawable param, which isn't
// multi-head safe, so it seems bad to allow it in the C# API.
// But the Window isn't realized at this point, so we can't get
// an actual Drawable from it. So we kludge for now.
Pixmap stipple = Pixmap.CreateBitmapFromData (Gdk.Screen.Default.RootWindow, gray50_bits, gray50_width, gray50_height);
tag = new TextTag ("background_stipple");
tag.BackgroundStipple = stipple;
buffer.TagTable.Add (tag);
tag = new TextTag ("foreground_stipple");
tag.ForegroundStipple = stipple;
buffer.TagTable.Add (tag);
tag = new TextTag ("big_gap_before_line"); tag = new TextTag ("big_gap_before_line");
tag.PixelsAboveLines = 30; tag.PixelsAboveLines = 30;
buffer.TagTable.Add (tag); buffer.TagTable.Add (tag);
@ -278,16 +264,9 @@ namespace GtkDemo
buffer.Insert (ref insertIter, " or "); buffer.Insert (ref insertIter, " or ");
buffer.InsertWithTagsByName (ref insertIter, "a red background", "red_background"); buffer.InsertWithTagsByName (ref insertIter, "a red background", "red_background");
buffer.Insert (ref insertIter, " or even "); buffer.Insert (ref insertIter, " or even ");
buffer.InsertWithTagsByName (ref insertIter, "a stippled red background", buffer.InsertWithTagsByName (ref insertIter, "a blue foreground on red background",
"red_background",
"background_stipple");
buffer.Insert (ref insertIter, " or ");
buffer.InsertWithTagsByName (ref insertIter,
"a stippled blue foreground on solid red background",
"blue_foreground", "blue_foreground",
"red_background", "red_background");
"foreground_stipple");
buffer.Insert (ref insertIter, " (select that to read it) can be used.\n\n"); buffer.Insert (ref insertIter, " (select that to read it) can be used.\n\n");
buffer.InsertWithTagsByName (ref insertIter, "Underline, strikethrough, and rise. ", "heading"); buffer.InsertWithTagsByName (ref insertIter, "Underline, strikethrough, and rise. ", "heading");
@ -389,9 +368,9 @@ namespace GtkDemo
// Event box is to add a black border around each child view // Event box is to add a black border around each child view
EventBox eventBox = new EventBox (); EventBox eventBox = new EventBox ();
Gdk.Color color = new Gdk.Color (); Gdk.RGBA color = new Gdk.RGBA ();
Gdk.Color.Parse ("black", ref color); color.Parse ("black");
eventBox.ModifyBg (StateType.Normal, color); eventBox.OverrideBackgroundColor (StateFlags.Normal, color);
Alignment align = new Alignment (0.5f, 0.5f, 1.0f, 1.0f); Alignment align = new Alignment (0.5f, 0.5f, 1.0f, 1.0f);
align.BorderWidth = 1; align.BorderWidth = 1;

View file

@ -1,4 +1,4 @@
# SUBDIRS = GtkDemo pixmaps valtest opaquetest gio gtk-gio SUBDIRS = GtkDemo #pixmaps valtest opaquetest gio gtk-gio
if ENABLE_DOTNET if ENABLE_DOTNET
DOTNET_TARGETS=drawing-sample.exe DOTNET_TARGETS=drawing-sample.exe