Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Add a new multiline TextArea widget #928

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
48 changes: 34 additions & 14 deletions TestApps/Samples/Samples/TextEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,42 @@ public TextEntries ()
te5.PlaceholderText = "Placeholder text";
PackStart (te5);

TextEntry te6 = new TextEntry ();
te6.Text = "I should have" + Environment.NewLine + "multiple lines!";
te6.PlaceholderText = "Placeholder text";
te6.MultiLine = true;
te6.MinHeight = 40;
PackStart (te6);
HBox hbox1 = new HBox ();

TextArea ta1 = new TextArea ();
ta1.Text = "I should have" + Environment.NewLine + "multiple lines and be centered!";
ta1.PlaceholderText = "Placeholder text";
ta1.TextAlignment = Alignment.Center;
ta1.MinHeight = 40;
ta1.Activated += (sender, e) => MessageDialog.ShowMessage ("Activated");
hbox1.PackStart (ta1, true);

TextArea ta2 = new TextArea ();
ta2.Text = "I should have multiple lines," + Environment.NewLine + "no frame and should wrap on words!";
ta2.PlaceholderText = "Placeholder text";
ta2.ShowFrame = false;
ta2.Wrap = WrapMode.Word;
hbox1.PackStart (ta2, true);

PackStart (hbox1);

TextArea ta3 = new TextArea ();
ta3.Text = "I should have\nmultiple lines,\nwrap on words,\n and scroll\nvertically ";
ta3.PlaceholderText = "Placeholder text";
ta3.Wrap = WrapMode.Word;
var scrollTa3 = new ScrollView (ta3);
scrollTa3.HorizontalScrollPolicy = ScrollPolicy.Never;
PackStart (scrollTa3);

try {
SearchTextEntry te7 = new SearchTextEntry ();
te7.PlaceholderText = "Type to search ...";
PackStart (te7);

SearchTextEntry te8 = new SearchTextEntry ();
te8.PlaceholderText = "I should have no frame";
te8.ShowFrame = false;
PackStart (te8);
SearchTextEntry ts1 = new SearchTextEntry ();
ts1.PlaceholderText = "Type to search ...";
PackStart (ts1);

SearchTextEntry ts2 = new SearchTextEntry ();
ts2.PlaceholderText = "I should have no frame";
ts2.ShowFrame = false;
PackStart (ts2);
} catch (InvalidOperationException ex) {
Console.WriteLine (ex);
}
Expand Down
2 changes: 2 additions & 0 deletions Xwt.Gtk/Xwt.Gtk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@
<Compile Include="Xwt.GtkBackend\AccessibleBackend.cs" />
<Compile Include="Xwt.GtkBackend\PopupWindowBackend.cs" />
<Compile Include="Xwt.GtkBackend\UtilityWindowBackend.cs" />
<Compile Include="Xwt.GtkBackend\TextAreaBackend.cs" />
<Compile Include="Xwt.GtkBackend\TextAreaBackendGtk2.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Xwt.Gtk/Xwt.Gtk3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@
<Compile Include="Xwt.GtkBackend\AccessibleBackend.cs" />
<Compile Include="Xwt.GtkBackend\PopupWindowBackend.cs" />
<Compile Include="Xwt.GtkBackend\UtilityWindowBackend.cs" />
<Compile Include="Xwt.GtkBackend\TextAreaBackend.cs" />
<Compile Include="Xwt.GtkBackend\TextAreaBackendGtk3.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions Xwt.Gtk/Xwt.GtkBackend/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ public static GridLines ToXwtValue (this Gtk.TreeViewGridLines value)
throw new InvalidOperationException("Invalid TreeViewGridLines value: " + value);
}

public static Gtk.Justification ToGtkJustification (this Alignment value)
{
switch (value) {
case Alignment.Start: return Gtk.Justification.Left;
case Alignment.Center: return Gtk.Justification.Center;
case Alignment.End: return Gtk.Justification.Right;
}
throw new InvalidOperationException("Invalid Alignment value: " + value);
}

public static Alignment ToXwtValue (this Gtk.Justification value)
{
switch (value) {
case Gtk.Justification.Left: return Alignment.Start;
case Gtk.Justification.Center: return Alignment.Center;
case Gtk.Justification.Right: return Alignment.End;
}
return Alignment.Start; // return Start for unknown and Fill
}

public static float ToGtkAlignment(this Alignment alignment)
{
switch(alignment) {
Expand Down
14 changes: 14 additions & 0 deletions Xwt.Gtk/Xwt.GtkBackend/Gtk2Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ public static void RenderPlaceholderText (this Gtk.Entry entry, Gtk.ExposeEventA
RenderPlaceholderText_internal (entry, args, placeHolderText, ref layout, entry.Xalign, 0.5f, 1, 0);
}

public static void RenderPlaceholderText (this Gtk.TextView textView, Gtk.ExposeEventArgs args, string placeHolderText, ref Pango.Layout layout)
{
if (args.Event.Window != textView.GetWindow (Gtk.TextWindowType.Text))
return;
if (textView.Buffer.Text.Length > 0)
return;
float xalign = 0;
switch (textView.Justification) {
case Gtk.Justification.Center: xalign = 0.5f; break;
case Gtk.Justification.Right: xalign = 1; break;
}
RenderPlaceholderText_internal (textView, args, placeHolderText, ref layout, xalign, 0.0f, 3, 0);
}

static void RenderPlaceholderText_internal (Gtk.Widget widget, Gtk.ExposeEventArgs args, string placeHolderText, ref Pango.Layout layout, float xalign, float yalign, int xpad, int ypad)
{
if (layout == null) {
Expand Down
31 changes: 31 additions & 0 deletions Xwt.Gtk/Xwt.GtkBackend/Gtk3Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// THE SOFTWARE.

using Xwt.Backends;
using Xwt.CairoBackend;
using System;
using System.Runtime.InteropServices;
using Gtk;
Expand Down Expand Up @@ -239,6 +240,36 @@ public static double GetSliderPosition (this Gtk.Scale scale)
scale.GetSliderRange (out start, out end);
return start + ((end - start) / 2);
}

public static void RenderPlaceholderText (this Gtk.TextView textView, Cairo.Context cr, string placeHolderText, ref Pango.Layout layout)
{
if (textView.Buffer.Text.Length > 0 || textView.HasFocus)
return;
float xalign = 0;
switch (textView.Justification) {
case Gtk.Justification.Center: xalign = 0.5f; break;
case Gtk.Justification.Right: xalign = 1; break;
}
RenderPlaceholderText_internal (textView, cr, placeHolderText, ref layout, xalign, 0.0f, 3, 0);
}

static void RenderPlaceholderText_internal (Gtk.Widget widget, Cairo.Context cr, string placeHolderText, ref Pango.Layout layout, float xalign, float yalign, int xpad, int ypad)
{
if (layout == null) {
layout = new Pango.Layout (widget.PangoContext);
layout.FontDescription = widget.PangoContext.FontDescription.Copy ();
}
int width, height;
layout.SetText (placeHolderText);
layout.GetPixelSize (out width, out height);
int x = xpad + (int)((widget.AllocatedWidth - width) * xalign);
int y = ypad + (int)((widget.AllocatedHeight - height) * yalign);
Xwt.Drawing.Color color_a = widget.StyleContext.GetBackgroundColor (Gtk.StateFlags.Normal).ToXwtValue ();
Xwt.Drawing.Color color_b = widget.StyleContext.GetColor (Gtk.StateFlags.Normal).ToXwtValue ();
cr.SetSourceColor (color_b.BlendWith (color_a, 0.5).ToCairoColor ());
cr.MoveTo (x, y);
Pango.CairoHelper.ShowLayout (cr, layout);
}
}
}

1 change: 1 addition & 0 deletions Xwt.Gtk/Xwt.GtkBackend/GtkEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public override void InitializeBackends ()
RegisterBackend<IDesignerSurfaceBackend, DesignerSurfaceBackend> ();
RegisterBackend<IMenuButtonBackend, MenuButtonBackend> ();
RegisterBackend<ITextEntryBackend, TextEntryBackend> ();
RegisterBackend<ITextAreaBackend, TextAreaBackend> ();
RegisterBackend<IToggleButtonBackend, ToggleButtonBackend> ();
RegisterBackend<IImageViewBackend, ImageViewBackend> ();
RegisterBackend<IAlertDialogBackend, AlertDialogBackend> ();
Expand Down
Loading