From 3a2f01c534bdee1e13e66cc265c68eeeaf7fa94b Mon Sep 17 00:00:00 2001 From: Bertrand Lorentz Date: Mon, 13 Jun 2011 18:00:08 +0200 Subject: [PATCH] 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. --- sample/GtkDemo/DemoApplicationWindow.cs | 2 +- sample/GtkDemo/DemoColorSelection.cs | 37 ++++++----- sample/GtkDemo/DemoDialog.cs | 2 +- sample/GtkDemo/DemoDrawingArea.cs | 87 ++++++++++++------------- sample/GtkDemo/DemoEntryCompletion.cs | 2 +- sample/GtkDemo/DemoExpander.cs | 2 +- sample/GtkDemo/DemoHyperText.cs | 2 +- sample/GtkDemo/DemoImages.cs | 2 +- sample/GtkDemo/DemoMain.cs | 6 +- sample/GtkDemo/DemoPixbuf.cs | 22 ++----- sample/GtkDemo/DemoSizeGroup.cs | 4 +- sample/GtkDemo/DemoTextView.cs | 37 +++-------- sample/Makefile.am | 2 +- 13 files changed, 85 insertions(+), 122 deletions(-) diff --git a/sample/GtkDemo/DemoApplicationWindow.cs b/sample/GtkDemo/DemoApplicationWindow.cs index bc4df60aa..1d8462c56 100644 --- a/sample/GtkDemo/DemoApplicationWindow.cs +++ b/sample/GtkDemo/DemoApplicationWindow.cs @@ -192,7 +192,7 @@ namespace GtkDemo protected override bool OnWindowStateEvent (Gdk.EventWindowState evt) { 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; } diff --git a/sample/GtkDemo/DemoColorSelection.cs b/sample/GtkDemo/DemoColorSelection.cs index 1e8b90c87..d8f5f8b79 100644 --- a/sample/GtkDemo/DemoColorSelection.cs +++ b/sample/GtkDemo/DemoColorSelection.cs @@ -14,7 +14,7 @@ namespace GtkDemo [Demo ("Color Selection", "DemoColorSelection.cs")] public class DemoColorSelection : Gtk.Window { - private Gdk.Color color; + private Gdk.RGBA color; private Gtk.DrawingArea drawingArea; public DemoColorSelection () : base ("Color Selection") @@ -30,19 +30,22 @@ namespace GtkDemo vbox.PackStart (frame, true, true, 0); drawingArea = new DrawingArea (); - drawingArea.ExposeEvent += new ExposeEventHandler (ExposeEventCallback); + drawingArea.Drawn += new DrawnHandler (DrawnCallback); // set a minimum size drawingArea.SetSizeRequest (200,200); // set the color - color = new Gdk.Color (0, 0, 0xff); - drawingArea.ModifyBg (StateType.Normal, color); + color.Red = 0; + color.Green = 0; + color.Blue = 1; + color.Alpha = 1; + drawingArea.OverrideBackgroundColor (StateFlags.Normal, color); frame.Add (drawingArea); Alignment alignment = new Alignment (1.0f, 0.5f, 0.0f, 0.0f); Button button = new Button ("_Change the above color"); button.Clicked += new EventHandler (ChangeColorCallback); alignment.Add (button); - vbox.PackStart (alignment); + vbox.PackStart (alignment, false, false, 0); ShowAll (); } @@ -53,17 +56,15 @@ namespace GtkDemo return true; } - // Expose callback for the drawing area - private void ExposeEventCallback (object o, ExposeEventArgs args) + // Drawn callback for the drawing area + private void DrawnCallback (object o, DrawnArgs args) { - EventExpose eventExpose = args.Event; - Gdk.Window window = eventExpose.Window; - Rectangle area = eventExpose.Area; + Cairo.Context cr = args.Cr; + + 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; } @@ -71,13 +72,13 @@ namespace GtkDemo { using (ColorSelectionDialog colorSelectionDialog = new ColorSelectionDialog ("Changing color")) { colorSelectionDialog.TransientFor = this; - colorSelectionDialog.ColorSelection.PreviousColor = color; - colorSelectionDialog.ColorSelection.CurrentColor = color; + colorSelectionDialog.ColorSelection.SetPreviousRgba (color); + colorSelectionDialog.ColorSelection.CurrentRgba = color; colorSelectionDialog.ColorSelection.HasPalette = true; if (colorSelectionDialog.Run () == (int) ResponseType.Ok) { - Gdk.Color selected = colorSelectionDialog.ColorSelection.CurrentColor; - drawingArea.ModifyBg (StateType.Normal, selected); + Gdk.RGBA selected = colorSelectionDialog.ColorSelection.CurrentRgba; + drawingArea.OverrideBackgroundColor (StateFlags.Normal, selected); } colorSelectionDialog.Hide (); diff --git a/sample/GtkDemo/DemoDialog.cs b/sample/GtkDemo/DemoDialog.cs index 3bc74bb27..30c4a6c44 100644 --- a/sample/GtkDemo/DemoDialog.cs +++ b/sample/GtkDemo/DemoDialog.cs @@ -94,7 +94,7 @@ namespace GtkDemo HBox hbox = new HBox (false, 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); hbox.PackStart (stock, false, false, 0); diff --git a/sample/GtkDemo/DemoDrawingArea.cs b/sample/GtkDemo/DemoDrawingArea.cs index ea7721274..c8744e177 100644 --- a/sample/GtkDemo/DemoDrawingArea.cs +++ b/sample/GtkDemo/DemoDrawingArea.cs @@ -22,7 +22,7 @@ namespace GtkDemo [Demo ("Drawing Area", "DemoDrawingArea.cs")] public class DemoDrawingArea : Gtk.Window { - private Pixmap pixmap = null; + private Cairo.Surface surface = null; public DemoDrawingArea () : base ("Drawing Area") { @@ -45,7 +45,7 @@ namespace GtkDemo // set a minimum size da.SetSizeRequest (100,100); frame.Add (da); - da.ExposeEvent += new ExposeEventHandler (CheckerboardExpose); + da.Drawn += new DrawnHandler (CheckerboardDrawn); // Create the scribble area label = new Label ("Scribble area"); @@ -62,7 +62,7 @@ namespace GtkDemo frame.Add (da); // Signals used to handle backing pixmap - da.ExposeEvent += new ExposeEventHandler (ScribbleExpose); + da.Drawn += new DrawnHandler (ScribbleDrawn); da.ConfigureEvent += new ConfigureEventHandler (ScribbleConfigure); // Event signals @@ -84,25 +84,21 @@ namespace GtkDemo return true; } - private void CheckerboardExpose (object o, ExposeEventArgs args) + private void CheckerboardDrawn (object o, DrawnArgs args) { const int CheckSize = 10; const int Spacing = 2; - DrawingArea da = o as DrawingArea; - - // 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); + Widget widget = o as Widget; + Cairo.Context cr = args.Cr; 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 xcount = 0; i = Spacing; @@ -110,13 +106,12 @@ namespace GtkDemo j = Spacing; ycount = xcount % 2; // start with even/odd depending on row while (j < alloc.Height) { - Gdk.GC gc; if (ycount % 2 != 0) - gc = gc1; + cr.SetSourceRGB (0.45777, 0, 0.45777); else - gc = gc2; - da.GdkWindow.DrawRectangle (gc, true, i, j, - CheckSize, CheckSize); + cr.SetSourceRGB (1, 1, 1); + // If we're outside the clip, this will do nothing. + cr.Rectangle (i, j, CheckSize, CheckSize); j += CheckSize + Spacing; ++ycount; @@ -130,33 +125,29 @@ namespace GtkDemo args.RetVal = true; } - private void ScribbleExpose (object o, ExposeEventArgs args) + private void ScribbleDrawn (object o, DrawnArgs args) { - Widget widget = o as Widget; - Gdk.Window window = widget.GdkWindow; - Rectangle area = args.Event.Area; - - // We use the "ForegroundGC" for the widget since it already exists, - // but honestly any GC would work. The only thing to worry about - // 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); + Cairo.Context cr = args.Cr; + + cr.SetSourceSurface (surface, 0, 0); + cr.Paint (); } - // 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) { Widget widget = o as Widget; - Rectangle allocation = widget.Allocation; + + if (surface != null) + surface.Destroy (); - pixmap = new Pixmap (widget.GdkWindow, allocation.Width, allocation.Height, -1); + var allocation = widget.Allocation; - // Initialize the pixmap to white - 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. args.RetVal = true; @@ -166,7 +157,7 @@ namespace GtkDemo { // paranoia check, in case we haven't gotten a configure event - if (pixmap == null) + if (surface == null) return; // This call is very important; it requests the next motion event. @@ -192,19 +183,21 @@ namespace GtkDemo // Draw a rectangle on the screen 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); + + cr.Fill (); + Gdk.CairoHelper.Rectangle (cr, update_rect); - // Paint to the pixmap, where we store our state - pixmap.DrawRectangle (widget.Style.BlackGC, true, - update_rect.X, update_rect.Y, - update_rect.Width, update_rect.Height); - widget.GdkWindow.InvalidateRect (update_rect, false); + ((IDisposable)cr).Dispose (); + + widget.Window.InvalidateRect (update_rect, false); } private void ScribbleButtonPress (object o, ButtonPressEventArgs args) { // paranoia check, in case we haven't gotten a configure event - if (pixmap == null) + if (surface == null) return; EventButton ev = args.Event; diff --git a/sample/GtkDemo/DemoEntryCompletion.cs b/sample/GtkDemo/DemoEntryCompletion.cs index 65cee5e52..2f4dab7a0 100644 --- a/sample/GtkDemo/DemoEntryCompletion.cs +++ b/sample/GtkDemo/DemoEntryCompletion.cs @@ -18,7 +18,7 @@ namespace GtkDemo VBox vbox = new VBox (false, 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 total or gnome for example."); label.UseMarkup = true; diff --git a/sample/GtkDemo/DemoExpander.cs b/sample/GtkDemo/DemoExpander.cs index 192b369fd..f5016554c 100644 --- a/sample/GtkDemo/DemoExpander.cs +++ b/sample/GtkDemo/DemoExpander.cs @@ -17,7 +17,7 @@ namespace GtkDemo Resizable = false; VBox vbox = new VBox (false, 5); - this.VBox.PackStart (vbox, true, true, 0); + this.ContentArea.PackStart (vbox, true, true, 0); vbox.BorderWidth = 5; vbox.PackStart (new Label ("Expander demo. Click on the triangle for details."), false, false, 0); diff --git a/sample/GtkDemo/DemoHyperText.cs b/sample/GtkDemo/DemoHyperText.cs index c97f92b13..a657aaf20 100644 --- a/sample/GtkDemo/DemoHyperText.cs +++ b/sample/GtkDemo/DemoHyperText.cs @@ -184,7 +184,7 @@ namespace GtkDemo view.WindowToBufferCoords (TextWindowType.Widget, (int) args.Event.X, (int) args.Event.Y, out x, out 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 diff --git a/sample/GtkDemo/DemoImages.cs b/sample/GtkDemo/DemoImages.cs index 07ccae595..fb79f30e1 100644 --- a/sample/GtkDemo/DemoImages.cs +++ b/sample/GtkDemo/DemoImages.cs @@ -170,7 +170,7 @@ namespace GtkDemo { Gdk.Pixbuf pixbuf = pixbufLoader.Pixbuf; pixbuf.Fill (0xaaaaaaff); - progressiveImage.FromPixbuf = pixbuf; + progressiveImage.Pixbuf = pixbuf; } void ProgressiveUpdatedCallback (object obj, AreaUpdatedArgs args) diff --git a/sample/GtkDemo/DemoMain.cs b/sample/GtkDemo/DemoMain.cs index 36a4e0745..b2b39dd29 100644 --- a/sample/GtkDemo/DemoMain.cs +++ b/sample/GtkDemo/DemoMain.cs @@ -180,8 +180,8 @@ namespace GtkDemo scrolledWindow.Add (textView); if (IsSource) { - FontDescription fontDescription = FontDescription.FromString ("Courier 12"); - textView.ModifyFont (fontDescription); + FontDescription fontDescription = FontDescription.FromString ("monospace"); + textView.OverrideFont (fontDescription); textView.WrapMode = Gtk.WrapMode.None; } else { // Make it a bit nicer for text @@ -191,7 +191,7 @@ namespace GtkDemo } return scrolledWindow; - } + } private TreeStore FillTree () { diff --git a/sample/GtkDemo/DemoPixbuf.cs b/sample/GtkDemo/DemoPixbuf.cs index a1ae4a1b8..401f3263f 100644 --- a/sample/GtkDemo/DemoPixbuf.cs +++ b/sample/GtkDemo/DemoPixbuf.cs @@ -71,23 +71,13 @@ namespace GtkDemo } // Expose callback for the drawing area - void Expose (object o, ExposeEventArgs args) + void DrawnCallback (object o, DrawnArgs args) { - Widget widget = (Widget) o; - Gdk.Rectangle area = args.Event.Area; - byte[] pixels; - int rowstride; + Cairo.Context cr = args.Cr; + + Gdk.CairoHelper.SetSourcePixbuf (cr, frame, 0, 0); + cr.Paint (); - rowstride = frame.Rowstride; - pixels = new byte[(frame.Height - area.Y) * rowstride]; - 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; } @@ -152,7 +142,7 @@ namespace GtkDemo frame = new Pixbuf (Colorspace.Rgb, false, 8, backWidth, backHeight); drawingArea = new DrawingArea (); - drawingArea.ExposeEvent += new ExposeEventHandler (Expose); + drawingArea.Drawn += new DrawnHandler (DrawnCallback); Add (drawingArea); timeoutId = GLib.Timeout.Add (FrameDelay, new GLib.TimeoutHandler(timeout)); diff --git a/sample/GtkDemo/DemoSizeGroup.cs b/sample/GtkDemo/DemoSizeGroup.cs index 42776f2ee..8d04cd509 100644 --- a/sample/GtkDemo/DemoSizeGroup.cs +++ b/sample/GtkDemo/DemoSizeGroup.cs @@ -33,7 +33,7 @@ namespace GtkDemo Resizable = false; VBox vbox = new VBox (false, 5); - this.VBox.PackStart (vbox, true, true, 0); + this.ContentArea.PackStart (vbox, true, true, 0); vbox.BorderWidth = 5; sizeGroup = new SizeGroup (SizeGroupMode.Horizontal); @@ -76,7 +76,7 @@ namespace GtkDemo // Convenience function to create a combo box holding a number of strings private ComboBox CreateComboBox (string [] strings) { - ComboBox combo = ComboBox.NewText (); + ComboBoxText combo = new ComboBoxText (); foreach (string str in strings) combo.AppendText (str); diff --git a/sample/GtkDemo/DemoTextView.cs b/sample/GtkDemo/DemoTextView.cs index 7873dc0b8..af9f60b27 100644 --- a/sample/GtkDemo/DemoTextView.cs +++ b/sample/GtkDemo/DemoTextView.cs @@ -71,7 +71,7 @@ namespace GtkDemo textView.AddChildAtAnchor (button, buttonAnchor); button.ShowAll (); - ComboBox combo = ComboBox.NewText (); + ComboBoxText combo = new ComboBoxText (); combo.AppendText ("Option 1"); combo.AppendText ("Option 2"); combo.AppendText ("Option 3"); @@ -154,20 +154,6 @@ namespace GtkDemo tag.Background = "red"; 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.PixelsAboveLines = 30; buffer.TagTable.Add (tag); @@ -278,17 +264,10 @@ namespace GtkDemo buffer.Insert (ref insertIter, " or "); buffer.InsertWithTagsByName (ref insertIter, "a red background", "red_background"); buffer.Insert (ref insertIter, " or even "); - buffer.InsertWithTagsByName (ref insertIter, "a stippled 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", - "red_background", - "foreground_stipple"); - buffer.Insert (ref insertIter, " (select that to read it) can be used.\n\n"); + buffer.InsertWithTagsByName (ref insertIter, "a blue foreground on red background", + "blue_foreground", + "red_background"); + buffer.Insert (ref insertIter, " (select that to read it) can be used.\n\n"); 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 EventBox eventBox = new EventBox (); - Gdk.Color color = new Gdk.Color (); - Gdk.Color.Parse ("black", ref color); - eventBox.ModifyBg (StateType.Normal, color); + Gdk.RGBA color = new Gdk.RGBA (); + color.Parse ("black"); + eventBox.OverrideBackgroundColor (StateFlags.Normal, color); Alignment align = new Alignment (0.5f, 0.5f, 1.0f, 1.0f); align.BorderWidth = 1; diff --git a/sample/Makefile.am b/sample/Makefile.am index 8ef13e059..412e80fc7 100755 --- a/sample/Makefile.am +++ b/sample/Makefile.am @@ -1,4 +1,4 @@ -# SUBDIRS = GtkDemo pixmaps valtest opaquetest gio gtk-gio +SUBDIRS = GtkDemo #pixmaps valtest opaquetest gio gtk-gio if ENABLE_DOTNET DOTNET_TARGETS=drawing-sample.exe