From 03df165143a996f67252b3e459e766f40ac97aec Mon Sep 17 00:00:00 2001 From: Jeroen Zwartepoorte Date: Tue, 1 Jun 2004 18:31:20 +0000 Subject: [PATCH] 2004-06-01 Jeroen Zwartepoorte * sample/CustomWidget.cs: Add custom widget sample. * sample/Makefile.am: Idem. svn path=/trunk/gtk-sharp/; revision=28649 --- ChangeLog | 5 ++ sample/CustomWidget.cs | 182 +++++++++++++++++++++++++++++++++++++++++ sample/Makefile.am | 8 +- 3 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 sample/CustomWidget.cs diff --git a/ChangeLog b/ChangeLog index 62ea8608a..67a107e2d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2004-06-01 Jeroen Zwartepoorte + + * sample/CustomWidget.cs: Add custom widget sample. + * sample/Makefile.am: Idem. + 2004-06-01 Mike Kestner * generator/ObjectGen.cs : generate protected ctor () for all diff --git a/sample/CustomWidget.cs b/sample/CustomWidget.cs new file mode 100644 index 000000000..fa71d06b6 --- /dev/null +++ b/sample/CustomWidget.cs @@ -0,0 +1,182 @@ +using GLib; +using Gtk; +using System; + +class CustomWidgetTest { + public static int Main (string[] args) + { + Application.Init (); + Window win = new Window ("Custom Widget Test"); + win.DeleteEvent += new DeleteEventHandler (OnQuit); + + VPaned paned = new VPaned (); + CustomWidget cw = new CustomWidget (); + cw.Label = "This one contains a button"; + Button button = new Button ("Ordinary button"); + cw.Add (button); + paned.Pack1 (cw, true, false); + + cw = new CustomWidget (); + cw.Label = "And this one a TextView"; + cw.StockId = Stock.JustifyLeft; + ScrolledWindow sw = new ScrolledWindow (null, null); + sw.ShadowType = ShadowType.In; + sw.HscrollbarPolicy = PolicyType.Automatic; + sw.VscrollbarPolicy = PolicyType.Automatic; + TextView textView = new TextView (); + sw.Add (textView); + cw.Add (sw); + paned.Pack2 (cw, true, false); + + win.Add (paned); + win.ShowAll (); + Application.Run (); + return 0; + } + + static void OnQuit (object sender, DeleteEventArgs args) + { + Application.Quit (); + } +} + +class CustomWidget : Bin { + internal static GType customWidgetGType; + private Gdk.Pixbuf icon; + private string label; + private Pango.Layout layout; + private string stockid; + + public CustomWidget () : base () + { + icon = null; + label = "CustomWidget"; + layout = null; + stockid = Stock.Execute; + + Flags |= (int)WidgetFlags.NoWindow; + } + + private Gdk.Pixbuf Icon { + get { + if (icon == null) + icon = RenderIcon (stockid, IconSize.Menu, ""); + return icon; + } + } + + public string Label { + get { + return label; + } + set { + label = value; + Layout.SetText (label); + } + } + + private Pango.Layout Layout { + get { + if (layout == null) + layout = CreatePangoLayout (label); + return layout; + } + } + + public string StockId { + get { + return stockid; + } + set { + stockid = value; + icon = RenderIcon (stockid, IconSize.Menu, ""); + } + } + + private Gdk.Rectangle TitleArea { + get { + Gdk.Rectangle area; + area.X = Allocation.X + (int)BorderWidth; + area.Y = Allocation.Y + (int)BorderWidth; + area.Width = (Allocation.Width - 2 * (int)BorderWidth); + + int layoutWidth, layoutHeight; + Layout.GetPixelSize (out layoutWidth, out layoutHeight); + area.Height = Math.Max (layoutHeight, icon.Height); + + return area; + } + } + + protected override bool OnExposeEvent (Gdk.EventExpose args) + { + Gdk.Rectangle exposeArea; + Gdk.Rectangle titleArea = TitleArea; + + if (args.Area.Intersect (titleArea, out exposeArea)) + GdkWindow.DrawPixbuf (Style.BackgroundGC (State), Icon, 0, 0, + titleArea.X, titleArea.Y, Icon.Width, + Icon.Height, Gdk.RgbDither.None, 0, 0); + + titleArea.X += icon.Width + 1; + titleArea.Width -= icon.Width - 1; + + if (args.Area.Intersect (titleArea, out exposeArea)) { + int layoutWidth, layoutHeight; + Layout.GetPixelSize (out layoutWidth, out layoutHeight); + + titleArea.Y += (titleArea.Height - layoutHeight) / 2; + + Style.PaintLayout (Style, GdkWindow, State, + true, exposeArea, this, null, + titleArea.X, titleArea.Y, Layout); + } + + return base.OnExposeEvent (args); + } + + protected override void OnRealized () + { + Flags |= (int)WidgetFlags.Realized; + + GdkWindow = ParentWindow; + Style = Style.Attach (GdkWindow); + } + + protected override void OnSizeAllocated (Gdk.Rectangle allocation) + { + base.OnSizeAllocated (allocation); + + int bw = (int)BorderWidth; + + Gdk.Rectangle titleArea = TitleArea; + + if (Child != null) { + Gdk.Rectangle childAllocation; + childAllocation.X = allocation.X + bw; + childAllocation.Y = allocation.Y + bw + titleArea.Height; + childAllocation.Width = allocation.Width - 2 * bw; + childAllocation.Height = allocation.Height - 2 * bw - titleArea.Height; + Child.SizeAllocate (childAllocation); + } + } + + protected override void OnSizeRequested (ref Requisition requisition) + { + requisition.Width = requisition.Height = (int)BorderWidth * 2; + requisition.Width += Icon.Width + 1; + + int layoutWidth, layoutHeight; + Layout.GetPixelSize (out layoutWidth, out layoutHeight); + requisition.Height += layoutHeight; + + if (Child != null && Child.Visible) { + Requisition childReq = Child.SizeRequest (); + requisition.Height += childReq.Height; + + requisition.Width += Math.Max (layoutWidth, childReq.Width); + } else { + requisition.Width += layoutWidth; + } + } +} diff --git a/sample/Makefile.am b/sample/Makefile.am index 26b347dc8..8e2f8f741 100755 --- a/sample/Makefile.am +++ b/sample/Makefile.am @@ -24,7 +24,7 @@ VTE_TARGETS= VTE_ASSEMBLY= endif -TARGETS = custom-cellrenderer.exe gtk-hello-world.exe button.exe calendar.exe subclass.exe menu.exe size.exe scribble.exe treeviewdemo.exe managedtreeviewdemo.exe testdnd.exe drawing-sample.exe $(GNOME_TARGETS) $(GLADE_TARGETS) $(VTE_TARGETS) +TARGETS = custom-cellrenderer.exe gtk-hello-world.exe button.exe calendar.exe subclass.exe menu.exe size.exe scribble.exe treeviewdemo.exe managedtreeviewdemo.exe testdnd.exe drawing-sample.exe custom-widget.exe $(GNOME_TARGETS) $(GLADE_TARGETS) $(VTE_TARGETS) assemblies=../glib/glib-sharp.dll ../pango/pango-sharp.dll ../atk/atk-sharp.dll ../gdk/gdk-sharp.dll ../gtk/gtk-sharp.dll $(GNOME_ASSEMBLY) $(GLADE_ASSEMBLY) $(VTE_ASSEMBLY) references=$(addprefix -r , $(assemblies)) @@ -92,6 +92,9 @@ custom-cellrenderer.exe: $(srcdir)/CustomCellRenderer.cs $(assemblies) drawing-sample.exe: $(srcdir)/DrawingSample.cs $(srcdir)/sysdraw.cs $(assemblies) $(CSC) -debug+ -out:drawing-sample.exe $(references) $(srcdir)/DrawingSample.cs $(srcdir)/sysdraw.cs -r:System.Drawing +custom-widget.exe: $(srcdir)/CustomWidget.cs $(assemblies) + $(CSC) -debug+ -out:custom-widget.exe $(references) $(srcdir)/CustomWidget.cs + EXTRA_DIST = \ HelloWorld.cs \ GnomeHelloWorld.cs \ @@ -115,4 +118,5 @@ EXTRA_DIST = \ CustomCellRenderer.cs \ DrawingSample.cs \ sysdraw.cs \ - drawing-sample.exe.config + drawing-sample.exe.config \ + CustomWidget.cs