sample: Update CustomWidget sample and add it to the build

This commit is contained in:
Bertrand Lorentz 2011-07-02 22:26:58 +02:00
parent 75f9a8acb7
commit 2656a5220c
2 changed files with 44 additions and 43 deletions

View File

@ -5,7 +5,7 @@ using System;
class CustomWidgetTest { class CustomWidgetTest {
public static int Main (string[] args) public static int Main (string[] args)
{ {
Application.Init (); Gtk.Application.Init ();
Window win = new Window ("Custom Widget Test"); Window win = new Window ("Custom Widget Test");
win.DeleteEvent += new DeleteEventHandler (OnQuit); win.DeleteEvent += new DeleteEventHandler (OnQuit);
@ -30,18 +30,17 @@ class CustomWidgetTest {
win.Add (paned); win.Add (paned);
win.ShowAll (); win.ShowAll ();
Application.Run (); Gtk.Application.Run ();
return 0; return 0;
} }
static void OnQuit (object sender, DeleteEventArgs args) static void OnQuit (object sender, DeleteEventArgs args)
{ {
Application.Quit (); Gtk.Application.Quit ();
} }
} }
class CustomWidget : Bin { class CustomWidget : Bin {
internal static GType customWidgetGType;
private Gdk.Pixbuf icon; private Gdk.Pixbuf icon;
private string label; private string label;
private Pango.Layout layout; private Pango.Layout layout;
@ -54,13 +53,13 @@ class CustomWidget : Bin {
layout = null; layout = null;
stockid = Stock.Execute; stockid = Stock.Execute;
WidgetFlags |= WidgetFlags.NoWindow; HasWindow = false;
} }
private Gdk.Pixbuf Icon { private Gdk.Pixbuf Icon {
get { get {
if (icon == null) if (icon == null)
icon = RenderIcon (stockid, IconSize.Menu, ""); icon = RenderIconPixbuf (stockid, IconSize.Menu);
return icon; return icon;
} }
} }
@ -89,7 +88,7 @@ class CustomWidget : Bin {
} }
set { set {
stockid = value; stockid = value;
icon = RenderIcon (stockid, IconSize.Menu, ""); icon = RenderIconPixbuf (stockid, IconSize.Menu);
} }
} }
@ -108,41 +107,26 @@ class CustomWidget : Bin {
} }
} }
protected override bool OnExposeEvent (Gdk.EventExpose args) protected override bool OnDrawn (Cairo.Context cr)
{ {
Gdk.Rectangle exposeArea;
Gdk.Rectangle titleArea = TitleArea; Gdk.Rectangle titleArea = TitleArea;
if (args.Area.Intersect (titleArea, out exposeArea)) Gdk.CairoHelper.SetSourcePixbuf (cr, Icon, 0, 0);
GdkWindow.DrawPixbuf (Style.BackgroundGC (State), Icon, 0, 0, cr.Paint ();
titleArea.X, titleArea.Y, Icon.Width,
Icon.Height, Gdk.RgbDither.None, 0, 0);
titleArea.X += icon.Width + 1; int layout_x = icon.Width + 1;
titleArea.Width -= icon.Width - 1; titleArea.Width -= icon.Width - 1;
if (args.Area.Intersect (titleArea, out exposeArea)) { int layoutWidth, layoutHeight;
int layoutWidth, layoutHeight; Layout.GetPixelSize (out layoutWidth, out layoutHeight);
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
titleArea.Y += (titleArea.Height - layoutHeight) / 2; int layout_y = (titleArea.Height - layoutHeight) / 2;
Style.PaintLayout (Style, GdkWindow, State, StyleContext.RenderLayout (cr, layout_x, layout_y, Layout);
true, exposeArea, this, null,
titleArea.X, titleArea.Y, Layout);
}
return base.OnExposeEvent (args); return base.OnDrawn (cr);
} }
protected override void OnRealized ()
{
WidgetFlags |= WidgetFlags.Realized;
GdkWindow = ParentWindow;
Style = Style.Attach (GdkWindow);
}
protected override void OnSizeAllocated (Gdk.Rectangle allocation) protected override void OnSizeAllocated (Gdk.Rectangle allocation)
{ {
base.OnSizeAllocated (allocation); base.OnSizeAllocated (allocation);
@ -161,22 +145,39 @@ class CustomWidget : Bin {
} }
} }
protected override void OnSizeRequested (ref Requisition requisition) protected override void OnGetPreferredWidth (out int minimum_width, out int natural_width)
{ {
requisition.Width = requisition.Height = (int)BorderWidth * 2; minimum_width = natural_width = (int)BorderWidth * 2 + Icon.Width + 1;
requisition.Width += Icon.Width + 1;
int layoutWidth, layoutHeight; int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight); Layout.GetPixelSize (out layoutWidth, out layoutHeight);
requisition.Height += layoutHeight;
if (Child != null && Child.Visible) { if (Child != null && Child.Visible) {
Requisition childReq = Child.SizeRequest (); int child_min_width, child_nat_width;
requisition.Height += childReq.Height; Child.GetPreferredWidth (out child_min_width, out child_nat_width);
requisition.Width += Math.Max (layoutWidth, childReq.Width); minimum_width += Math.Max (layoutWidth, child_min_width);
natural_width += Math.Max (layoutWidth, child_nat_width);
} else { } else {
requisition.Width += layoutWidth; minimum_width += layoutWidth;
natural_width += layoutWidth;
}
}
protected override void OnGetPreferredHeight (out int minimum_height, out int natural_height)
{
minimum_height = natural_height = (int)BorderWidth * 2;
int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
minimum_height += layoutHeight;
natural_height += layoutHeight;
if (Child != null && Child.Visible) {
int child_min_height, child_nat_height;
Child.GetPreferredHeight (out child_min_height, out child_nat_height);
minimum_height += Math.Max (layoutHeight, child_min_height);
natural_height += Math.Max (layoutHeight, child_nat_height);
} }
} }
} }

View File

@ -8,7 +8,7 @@ DOTNET_TARGETS=
DOTNET_ASSEMBLY= DOTNET_ASSEMBLY=
endif endif
TARGETS = gtk-hello-world.exe button.exe calendar.exe subclass.exe menu.exe treeviewdemo.exe managedtreeviewdemo.exe nodeviewdemo.exe treemodeldemo.exe actions.exe spawn.exe assistant.exe registerprop.exe gexceptiontest.exe native-instantiation.exe polarfixed.exe cairo-sample.exe scribble.exe testdnd.exe custom-cellrenderer.exe #custom-widget.exescribble-xinput.exe $(DOTNET_TARGETS) TARGETS = gtk-hello-world.exe button.exe calendar.exe subclass.exe menu.exe treeviewdemo.exe managedtreeviewdemo.exe nodeviewdemo.exe treemodeldemo.exe actions.exe spawn.exe assistant.exe registerprop.exe gexceptiontest.exe native-instantiation.exe polarfixed.exe cairo-sample.exe scribble.exe testdnd.exe custom-cellrenderer.exe custom-widget.exe #scribble-xinput.exe $(DOTNET_TARGETS)
DEBUGS = $(addsuffix .mdb, $(TARGETS)) DEBUGS = $(addsuffix .mdb, $(TARGETS))