textArea growByContent doesnt work in layer layout - codenameone

I used layer layout to keep the title ie textArea on top of button image. But i cannot grow textArea size. Also the padding and margin on the textArea is not working as well.
Image btnIcon = URLImage.createToStorage(placeholder, "homeIcon" + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE);
int width = placeholder.getWidth();
int height = placeholder.getHeight();
homeButton.setPreferredSize(new Dimension(width, height));
homeButton.getAllStyles().setBgImage(btnIcon);
TextArea buttonTitle = new TextArea(title);
buttonTitle.setRows(1);
buttonTitle.setUIID("smallLabel");
buttonTitle.getAllStyles().setPadding(0, 0, 0, 0);
buttonTitle.getAllStyles().setMargin(0, 4, 0, 4);
buttonTitle.setPreferredW(screenWidth / 3 - 30);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setIsScrollVisible(false);
buttonTitle.setGrowLimit(2);
Container container1 = new Container();
container1.setUIID("buttonTitleContainer");
container1.getAllStyles().setMargin(0, 5, 0, 0);
container1.getAllStyles().setBgTransparency(0);
container1.add(LayeredLayout.encloseIn(homeButton, FlowLayout.encloseRightBottom(buttonTitle)));
middleContainer.addComponent(BorderLayout.CENTER, container1);
How can i grow the textArea row as required and set the padding margin of the textarea.
Update:
i commented setPreferredW() and added calcPreferredSize() to set the dimension but how can i set the height dynamically??
TextArea buttonTitle = new TextArea(title){
#Override
protected Dimension calcPreferredSize() {
Dimension d = new Dimension(screenWidth/3-10, 40);
return d; //To change body of generated methods, choose Tools | Templates.
}
};
buttonTitle.setUIID("smallLabel");
buttonTitle.getAllStyles().setAlignment(Label.LEFT);
buttonTitle.setRows(1);
buttonTitle.getAllStyles().setPadding(0, 0, 0, 0);
buttonTitle.getAllStyles().setMargin(0, 0, 0, 4);
// buttonTitle.setPreferredW(screenWidth / 3 - 30);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setIsScrollVisible(false);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
the design is somewhat like below:
Current screenshot:
As you can see, there is only one word displayed and the textArea always has only 1 row and doesnt grow

The problem isn't in the LayeredLayout it's due to the fact that you called the deprecated setPreferredW.
Notice that setPreferredW also limits the height.

Related

Is it possible to turn off antialiasing when using ImageBrush tiling?

Is it possible to turn off anti-aliasing in WPF when using an ImageBrush?
Given the following code:
var handleImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/myimage.png"));
var imageBrush = new ImageBrush(handleImage);
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.Stretch = Stretch.Uniform;
imageBrush.Viewport = new Rect(0, 0, _handleImage.Width, _handleImage.Height);
imageBrush.ViewportUnits = BrushMappingMode.Absolute;
imageBrush.TileMode = TileMode.Tile;
drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, width, height));
Gives me something like:
But I'm expecting:
WPF's default antialiasing makes it look terrible. I've tried UseLayoutRounding=true, SnapsToDevicePixels=true, RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality),
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor) RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified); on the window. The only one that changes any visual difference is BitmapScalingMode.NearestNeighbor however it still looks odd and the tiling overlaps itself.
EDIT: Download full working sample: WpfImageBrushExample.zip
The problem here was that the BitmapImage width and height were different fractional numbers - in this case it was 5.333 x 6x666 instead of the expected 4 x 5 pixels. If I use _handleImage.PixelWidth and _handleImage.PixelHeight the problem is fixed and I don't get weird aliasing under any of those rendering options set.
var handleImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/myimage.png"));
var imageBrush = new ImageBrush(handleImage);
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.Stretch = Stretch.Uniform;
imageBrush.Viewport = new Rect(0, 0, _handleImage.PixelWidth, _handleImage.PixelHeight);
imageBrush.ViewportUnits = BrushMappingMode.Absolute;
imageBrush.TileMode = TileMode.Tile;
drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, width, height));
Produces:

Image magnify Glass on windows form

i am trying to use glass magnifier on PictureEdit(devexpress) control in windows form
we have set PictureEdit.SizeMode =Squeeze . its important. in our application i am already using Squeeze size mode.
i am getting problem of calculation of mouse location , for draw rect image onto enalged panel.
here is code of PartialMag_Paint event of enalaged panel. in which we show zoomed imaged of mouse pointed area
int srcx = (PartailMagImageView.Location.X + PartailMagImageView.Width / 2);
int srcy = (PartailMagImageView.Location.Y + PartailMagImageView.Height / 2);
e.Graphics.DrawImage(this.Image
, new System.Drawing.Rectangle(0, 0, PartailMagImageView.Width, PartailMagImageView.Height)
, new System.Drawing.Rectangle(srcx - ZoomOutRate / 2, srcy - ZoomOutRate / 2, ZoomOutRate, ZoomOutRate)
, GraphicsUnit.Pixel);
Pen mypen = new Pen(Color.Black, 7);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.DrawEllipse(mypen, 0, 0, PartailMagImageView.Width - 7, PartailMagImageView.Height - 7);
if any one need to see complete code sample. i am attaching sample in dropbox link
https://www.dropbox.com/s/mogmshuiimtvhk7/ImageMagnifyingWindowsForm.zip?dl=0
PictureEdit has the ViewportToImage and ImageToViewport methods allowing you to convert viewport coordinates to the source image coordinates and vice versa.

Scaling encodedImage

How can we scale encodedImage with respect to width and the height just scale with proportion to width? We can scaleWidth of image but there is no scaledEncodedWidth. Is there a way to scale width of encoded image and height scale with respect to the width?
code:
homePlaceholder = (EncodedImage) r.getImage("placeholder.png");
homePlaceholder = homePlaceholder.scaledEncoded(screenWidth / 3, screenWidth / 3);
//the following doesnt work as well
homePlaceholder = homePlaceholder.scaledEncoded(screenWidth / 3, -1);
I hope replacing EncodedImage with Image will solve your problem as shown in following code
Image homePlaceholder =(Image) r.getImage("placeholder.png");
homePlaceholder = homePlaceholder.scaledWidth(screenWidth / 3);
EncodedImage encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder , true)

WPF Rectangle movement with Animation

I have a requirement of moving dynamically created rectangles dynamically.
I am almost done with my implementation, and if I write the code below for each of the rectangle dynamically, I am able to achieve the desired result.
The code below moves the desired rectangle by 50 pixels on X Axis.
TranslateTransform translateTransform1 = new TranslateTransform(50, 0); aRectangle.RenderTransform = translateTransform1;
The problem is I want this to be animated. The code below is written to have the same rectanle moved with animation, but gives entirelly different result all together. Any help will be much appreciated. I want it to manage through code as my rectangles are going to be dynamic.
TranslateTransform translateTransform1 = new TranslateTransform(50, 0);
Duration duration = new Duration(new TimeSpan(0, 0, 0, 1, 0));
DoubleAnimation anim = new DoubleAnimation(30, duration);
translateTransform1.BeginAnimation(TranslateTransform.XProperty, anim);
aRectangle.RenderTransform = translateTransform1;
Try
Duration duration = new Duration(new TimeSpan(0, 0, 0, 1, 0));
DoubleAnimation anim = new DoubleAnimation(30, duration);
aRectangle.RenderTransform = new TranslateTransform();
aRectangle.BeginAnimation(TranslateTransform.XProperty, anim);

DataGridView row: Semi-transparent selection or row border on selection

I have a DataGridView where the background of each row is different depending on the data bound item. Though, when I select a row, I can no longer see its original background color.
To solve this, I have thought of two solutions:
I can make the selections semi-transparent, making it possible to see if two selected rows have different background colors.
Or; I can remove the selection colors entirely, and draw a border around the selected rows.
What option is easier and how can I do this?
It's a WinForm app.
Edit: I ended up using some of your code, adrift
private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (dgv.Rows[e.RowIndex].Selected)
{
var row = dgv.Rows[e.RowIndex];
var bgColor = row.DefaultCellStyle.BackColor;
row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(bgColor.R * 5 / 6, bgColor.G * 5 / 6, bgColor.B * 5 / 6);
}
}
This gives the impression of a semi-transparent selection color. Thanks for your help!
If you want to draw a border around selected rows, you can use the DataGridView.RowPostPaintEvent, and to 'clear' the selection colors, you can use the DataGridViewCellStyle.SelectionBackColor and DataGridViewCellStyle.SelectionForeColor properties.
For example, if I set the row cell style like this
row.DefaultCellStyle.BackColor = Color.LightBlue;
row.DefaultCellStyle.SelectionBackColor = Color.LightBlue;
row.DefaultCellStyle.SelectionForeColor = dataGridView1.ForeColor;
I can add this code to the RowPostPaintEvent
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Selected)
{
using (Pen pen = new Pen(Color.Red))
{
int penWidth = 2;
pen.Width = penWidth;
int x = e.RowBounds.Left + (penWidth / 2);
int y = e.RowBounds.Top + (penWidth / 2);
int width = e.RowBounds.Width - penWidth;
int height = e.RowBounds.Height - penWidth;
e.Graphics.DrawRectangle(pen, x, y, width, height);
}
}
}
and a selected row will display like this:

Resources