1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
using GLib;
using Gtk;
using System;
class CustomWidgetTest {
public static int Main (string[] args)
{
Gtk.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 ();
Gtk.Application.Run ();
return 0;
}
static void OnQuit (object sender, DeleteEventArgs args)
{
Gtk.Application.Quit ();
}
}
class CustomWidget : Bin {
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;
HasWindow = false;
}
private Gdk.Pixbuf Icon {
get {
if (icon == null)
icon = RenderIconPixbuf (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 = RenderIconPixbuf (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 OnDrawn (Cairo.Context cr)
{
Gdk.Rectangle titleArea = TitleArea;
Gdk.CairoHelper.SetSourcePixbuf (cr, Icon, 0, 0);
cr.Paint ();
int layout_x = icon.Width + 1;
titleArea.Width -= icon.Width - 1;
int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
int layout_y = (titleArea.Height - layoutHeight) / 2;
StyleContext.RenderLayout (cr, layout_x, layout_y, Layout);
return base.OnDrawn (cr);
}
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 OnGetPreferredWidth (out int minimum_width, out int natural_width)
{
minimum_width = natural_width = (int)BorderWidth * 2 + Icon.Width + 1;
int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
if (Child != null && Child.Visible) {
int child_min_width, child_nat_width;
Child.GetPreferredWidth (out child_min_width, out child_nat_width);
minimum_width += Math.Max (layoutWidth, child_min_width);
natural_width += Math.Max (layoutWidth, child_nat_width);
} else {
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);
}
}
}
|