/usr/share/gtk-sharp3-examples/CustomScrollableWidget.cs is in gtk-sharp3-examples 2.99.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | using GLib;
using Gtk;
using System;
class CustomScrollableWidgetTest {
public static int Main (string[] args)
{
Gtk.Application.Init ();
Window win = new Window ("Custom Scrollable Widget Test");
win.DeleteEvent += new DeleteEventHandler (OnQuit);
VPaned paned = new VPaned ();
ScrolledWindow scroll = new ScrolledWindow ();
scroll.HscrollbarPolicy = PolicyType.Automatic;
scroll.VscrollbarPolicy = PolicyType.Automatic;
var cw = new DerivedScrollableWidget<string> ("This one label that is repeated");
scroll.Add (cw);
paned.Pack1 (scroll, true, false);
scroll = new ScrolledWindow ();
scroll.HscrollbarPolicy = PolicyType.Automatic;
scroll.VscrollbarPolicy = PolicyType.Automatic;
var cw2 = new DerivedScrollableWidget<object> ("Another label that is repeated");
scroll.Add (cw2);
paned.Pack2 (scroll, true, false);
win.Add (paned);
win.DefaultWidth = 200;
win.DefaultHeight = 200;
win.ShowAll ();
Gtk.Application.Run ();
return 0;
}
static void OnQuit (object sender, DeleteEventArgs args)
{
Gtk.Application.Quit ();
}
}
abstract class CustomBase : Widget
{
public CustomBase () : base ()
{ }
}
class DerivedScrollableWidget<T> : CustomScrollableWidget<T>
{
public DerivedScrollableWidget (string label) : base (label)
{ }
}
class CustomScrollableWidget<T> : CustomBase, IScrollableImplementor {
private int num_rows = 20;
private string label;
private Pango.Layout layout;
public CustomScrollableWidget (string custom_label) : base ()
{
label = custom_label;
layout = null;
HasWindow = false;
}
private Pango.Layout Layout {
get {
if (layout == null)
layout = CreatePangoLayout (label);
return layout;
}
}
private Gdk.Rectangle ContentArea {
get {
Gdk.Rectangle area;
area.X = Allocation.X;
area.Y = Allocation.Y;
int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
area.Width = layoutWidth;
area.Height = layoutHeight * num_rows;
return area;
}
}
protected override bool OnDrawn (Cairo.Context cr)
{
int layout_x = - HadjustmentValue;
int layout_y = - VadjustmentValue;
int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
for (int i = 0; i < num_rows; i++) {
Layout.SetText (String.Format ("{0} {1}", label, i));
StyleContext.RenderLayout (cr, layout_x, layout_y, Layout);
layout_y += layoutHeight;
}
return base.OnDrawn (cr);
}
protected override void OnSizeAllocated (Gdk.Rectangle allocation)
{
base.OnSizeAllocated (allocation);
if (hadjustment != null) {
hadjustment.PageSize = allocation.Width;
hadjustment.PageIncrement = allocation.Width;
UpdateAdjustments ();
}
if (vadjustment != null) {
vadjustment.PageSize = allocation.Height;
vadjustment.PageIncrement = allocation.Height;
UpdateAdjustments ();
}
}
private Adjustment hadjustment;
public Adjustment Hadjustment {
get { return hadjustment; }
set {
if (value == hadjustment) {
return;
}
hadjustment = value;
if (hadjustment == null) {
return;
}
hadjustment.ValueChanged += OnHadjustmentChanged;
UpdateAdjustments ();
}
}
private Adjustment vadjustment;
public Adjustment Vadjustment {
get { return vadjustment; }
set {
if (value == vadjustment) {
return;
}
vadjustment = value;
if (vadjustment == null) {
return;
}
vadjustment.ValueChanged += OnVadjustmentChanged;
UpdateAdjustments ();
}
}
private int HadjustmentValue {
get { return hadjustment == null ? 0 : (int)hadjustment.Value; }
}
private int VadjustmentValue {
get { return vadjustment == null ? 0 : (int)vadjustment.Value; }
}
public Gtk.ScrollablePolicy HscrollPolicy {
get; set;
}
public Gtk.ScrollablePolicy VscrollPolicy {
get; set;
}
private void UpdateAdjustments ()
{
int layoutWidth, layoutHeight;
Layout.GetPixelSize (out layoutWidth, out layoutHeight);
if (hadjustment != null) {
hadjustment.Upper = ContentArea.Width;
hadjustment.StepIncrement = 10.0;
if (hadjustment.Value + hadjustment.PageSize > hadjustment.Upper) {
hadjustment.Value = hadjustment.Upper - hadjustment.PageSize;
}
if (hadjustment.Upper > 0 && hadjustment.Upper < hadjustment.PageSize) {
hadjustment.Upper = hadjustment.PageSize;
}
hadjustment.Change ();
}
if (vadjustment != null) {
vadjustment.Upper = ContentArea.Height;
vadjustment.StepIncrement = layoutHeight;
if (vadjustment.Value + vadjustment.PageSize > vadjustment.Upper) {
vadjustment.Value = vadjustment.Upper - vadjustment.PageSize;
}
if (vadjustment.Upper > 0 && vadjustment.Upper < vadjustment.PageSize) {
vadjustment.Upper = vadjustment.PageSize;
}
vadjustment.Change ();
}
}
private void OnHadjustmentChanged (object o, EventArgs args)
{
UpdateAdjustments ();
QueueDraw ();
}
private void OnVadjustmentChanged (object o, EventArgs args)
{
UpdateAdjustments ();
QueueDraw ();
}
}
|