Monotouch.Dialog not sizing cells appropriately with StyledMultilineElement? - c#

I'm using Xamarin and Monotouch.Dialog to show some text comments in a UIPopoverControl. If the text being shown in a StyledMultilineElement is of any great length then the Caption and the text is being shown further down (see image below). This problem gets worse if your text string is really big, say about 20,000 characters, because you get a huge scrolling cell with no text in it at all. Simplified version of the code I'm using in my UITapGestureRecognizer below.
Am I doing something wrong?
Is there a workaround?
I did have a look at the source of the StyledMultilineElement here and am not sure if the issue is with UITableView.StringSize method.
Any known issues with UITableView.StringSize?
Thanks,
Gavin
Sample code:
RootElement root = new RootElement("Title") { UnevenRows = true };
var section = new Section("Section Heading");
// Get a value string that is approx 1,000 chars long
var value = GetLongString();
var element = new StyledMultilineElement("Caption", value, UITableViewCellStyle.Subtitle)
{
LineBreakMode = UILineBreakMode.WordWrap,
Font = UIFont.FromName("HelveticaNeue", 16f),
SubtitleFont = UIFont.FromName("HelveticaNeue", 16f)
};
section.Add(element);
root.Add(new List<Section>() { section });
var viewController = new DialogViewController(root);
var navbarController = new UINavigationController(viewController);
_contactPopup = new UIPopoverController(navbarController);
_contactPopup.PresentFromRect(cell.Bounds, cell, UIPopoverArrowDirection.Any, true);
Edit:
Found the issues page on git hub and added issue there too Git Hub Issue #225

Related

LibreOffice Calc C# SDK: program to insert images into cells, stuck trying to create XGraphic

Background: I'm trying to write a program to insert an image into a cell of a spreadsheet. LibreOffice recently changed how this is done, and all the samples I could find use the old method which no longer works.
Technically I know that you can't "insert" an image into a cell and that such an image is an overlay on a DrawPage that sits on top of the spreadsheet to "decorate" it.
One of the first steps in doing this (the new way) is to create an XGraphic object which contains the image. The process is to create an XGraphicProvider and call it with MediaProperties that specify the image file URL to be loaded. I have a program that is supposed to do this but the resulting XGraphic is null. The LO SDK gives pretty much no information when you do something wrong; it just doesn't work.
Here is the code I have, with all the headers removed:
// addpic
// add picture to spreadsheet - debug version
class OpenOfficeApp {
[STAThread]
static void Main(string[] args) {
bool lreadonly;
string pqfile;
string pqURL;
string pqpic;
pqfile = "file:///D:/Documents/NSexeye/ODS%20File%20Access/"+
"addpix/addpic.ods";
pqpic = "addpic2";
pqURL = pqpic+".jpg";
lreadonly = false;
Console.WriteLine("Using: "+pqfile);
// get the desktop
XComponentContext XCC = uno.util.Bootstrap.bootstrap();
XMultiComponentFactory XMCF =
(XMultiComponentFactory)XCC.getServiceManager();
XMultiServiceFactory XMSF = (XMultiServiceFactory)XCC.getServiceManager();
XComponentLoader XCL =
(XComponentLoader)XMSF.createInstance("com.sun.star.frame.Desktop");
// open the spreadsheet
PropertyValue[] pPV = new PropertyValue[2];
pPV[0] = new PropertyValue();
pPV[0].Name = "Hidden";
pPV[0].Value = new uno.Any(true);
pPV[1] = new PropertyValue();
pPV[1].Name = "ReadOnly";
if (lreadonly) pPV[1].Value = new uno.Any(true);
else pPV[1].Value = new uno.Any(false);
XComponent XCo = XCL.loadComponentFromURL(pqfile,"_blank",0,pPV);
// create graphic object containing image
object oGP = XMCF.createInstanceWithContext(
"com.sun.star.graphic.GraphicProvider",XCC);
if (oGP == null) {
Console.WriteLine("oGP is null. Aborting.");
return;
}
XGraphicProvider XGP = (XGraphicProvider)oGP;
if (XGP == null) {
Console.WriteLine("XGP is null. Aborting.");
return;
}
pPV = new PropertyValue[1];
pPV[0] = new PropertyValue();
pPV[0].Name = "URL";
pPV[0].Value = new uno.Any(pqURL);
Console.WriteLine("Creating XGraphic containing "+pqURL);
XGraphic XG = XGP.queryGraphic(pPV);
// *** XG is null here
if (XG == null) {
Console.WriteLine("XG is null. Aborting.");
return;
}
// ... lots of stuff to be added here
// save and close the spreadsheet
XModifiable XM = (XModifiable)XCo;
XM.setModified(true);
XStorable XSt = (XStorable)XCo;
XSt.store();
XCloseable XCl = (XCloseable)XCo;
XCl.close(true);
// terminate LibreOffice
// *** I want this to not terminate it if something else is open
XDesktop XD = (XDesktop)XCL;
if (XD != null) XD.terminate();
}
}
I get a null for the XGraphic, in the place indicated in the comments. I don't know if the call to create it is failing, or if one of the earlier steps of the process are incorrect.
My goal here, in addition to getting my program working, is to create a sample program showing how to add an image to a Calc spreadsheet cell, and to manipulate such images. There are a fair number of people asking questions about this and none of the examples I've found will work. I think a good working sample will be of value.
I've spent a lot of time searching for information and code samples for this, with nothing that helps. I've tried to find ways to verify the validity of the XGraphicProvider interface with no luck. I've run out of things to try.
I'm hoping someone who knows about the LibreOffice SDK can take a look and maybe see what I'm doing wrong.
Update: I figured out what I was doing wrong: I was passing a bare filename in the "URL" property to XGraphicProvider. It has to be the same format (starting with "file:///") as the spreadsheet's file name specification.
Now I'm stuck with another property problem. The XGraphic has to be specified as a parameter to the GraphicObjectShape's Graphic property, but the setPropertyValue() function requires that it be a uno.Any type. I can't figure out how to specify an interface name like XGraphic as a uno.Any.
Here is the piece of code that won't compile, complaining that it can't convert an XGraphic to a uno.Any, in the first setPropertyValue call:
// set image XGraphic
XPropertySet XPS = (XPropertySet)XS;
XPS.setPropertyValue("Graphic",XG);
XPS.setPropertyValue("Name",new uno.Any(pqpic));
XG is an XGraphic type. Using "new uno.Any(XG)" doesn't work either, giving a similar compiler error.
After trying unsuccessfully for a few hours to get the latest LO SDK up and running, let me offer some untested ideas.
First of all, here is some working Basic code, no doubt similar to what you're translating from. The important line is oShape.Graphic = oProvider.queryGraphic(Props()).
oDoc = ThisComponent
oSheet = oDoc.CurrentController.ActiveSheet
pqURL = "file:///C:/Users/JimK/Desktop/addpic.jpg"
oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Dim Props(0) as new com.sun.star.beans.PropertyValue
Props(0).Name= "URL"
Props(0).Value = pqURL
oShape.Graphic = oProvider.queryGraphic(Props())
oCell = oSheet.getCellByPosition(5,5)
oShape.Name = oCell.AbsoluteName + "##" + Props(0).Value
oShape.Anchor = oCell
oSheet.DrawPage.add(oShape)
'Resize
w = oShape.Graphic.Size.Width
h = oShape.Graphic.Size.Height
wcl = oCell.Size.Width
hcl = oCell.Size.Height
If w<>0 and h<>0 then
oCell.String=""
Dim Size as new com.sun.star.awt.Size
Size.Width = wcl
Size.Height = h*wcl/w
If Size.Height > hcl then
Size.Width = hcl*w/h
Size.Height = hcl
Endif
oShape.setSize(Size)
oShape.setPosition(oCell.Position)
erase oShape
Else
oShape.dispose()
Endif
Now, how to translate this to C#? It looks like you may need to explicitly specify the type. In the SDK example, there are calls like this.
xFieldProp.setPropertyValue(
"Orientation",
new uno.Any(
typeof (unoidl.com.sun.star.sheet.DataPilotFieldOrientation),
unoidl.com.sun.star.sheet.DataPilotFieldOrientation.DATA ) );
So in your case, something like this:
XPS.setPropertyValue(
"Graphic"
new uno.Any(
typeof(unoidl.com.sun.star.graphic.XGraphic),
XG));
Alternatively, follow the suggestion here: set GraphicURL, which should load the image and set Graphic for you.

How do you add thumbnails to an embed message in DSharpPlus?

I have been bashing my head against the wall for 8 hours now, trying to figure out how to add thumbnails to embeded messages. I've been following this tutorial that uses ThumbnailURL, something that doesn't exist in the context he's using it for. Instead I just have Thumbnail which does not take a string, but rather an EmbedThumbnail which I can't access at all.
var avatarEmbed = new DiscordEmbedBuilder
{
Title = "Enter the game.",
Thumbnail = ctx.Client.CurrentUser.AvatarUrl,
Color = DiscordColor.Azure,
};
Please don't send links to docs and tutorials. I've tried. I've really tried. But this just seems like an issue that is very recent and therefore resources on it would be next to none. Also the docs don't seem to have example code, making it all more frustrating.
Not sure about your version but I use the latest nightly build.
and I do it like this
var builder = new DiscordEmbedBuilder
{
Title = "Title Here",
Color = DiscordColor.Azure
};
builder.WithThumbnail(ctx.Client.CurrentUser.AvatarUrl);
but you can also do it this way
var builder = new DiscordEmbedBuilder
{
Title = "Title Here",
Color = DiscordColor.Azure,
Thumbnail = new DiscordEmbedBuilder.EmbedThumbnail
{
Url = ctx.Client.CurrentUser.AvatarUrl
}
};

Getting ligatures working in SharpDX/DirectWrite

I'm having trouble getting ligatures to show up in DirectWrite code. I'm using a ligature-rich font and am basically applying every ligature tag I can get my hands on:
var typography = new Typography(dwriteFactory);
var ligatureTags = new[]
{
FontFeatureTag.StandardLigatures,
FontFeatureTag.StylisticAlternates,
FontFeatureTag.DiscretionaryLigatures,
FontFeatureTag.RequiredLigatures,
FontFeatureTag.ContextualLigatures,
FontFeatureTag.HistoricalLigatures
};
foreach (var tag in ligatureTags)
{
FontFeature ff;
ff.NameTag = tag;
ff.Parameter = DisableLigatures ? 0 : 1;
typography.AddFontFeature(ff);
}
textLayout.SetTypography(typography, EntireRange(text));
From the above code, StylisticAlternates and DiscretionaryLigatures work. But, whatever I do, StandardLigatures do not work.
Can anyone give suggestions as to why this wouldn't be working?

Remove top margin from cover page of PDF generated using TuesPechkin

I am using TuesPechkIn to convert my html to PDF. I am setting the configuration as follows:
My question is, can I remove the top margin from the first page of the PDF only? (Basically its the cover page which includes a banner image). Is there any way to implement this.
var document = new HtmlToPdfDocument
{
GlobalSettings =
{
DocumentTitle = "My Report",
PaperSize = PaperKind.A4,
Margins =
{
Top = 1.25, //Need to make it as 0 for the first page
Left = 0.00,
Right = 0.00,
Bottom = 1.25,
Unit = TuesPechkin.Unit.Centimeters
},
PageOffset = 2
},
Objects =
{
new ObjectSettings
{
HtmlText = htmlText,
FooterSettings = footerSettings
}
}
};
return converter.Convert(document);
A work around would be to remove that image in JavaScript. I was having the same issue on an element in the last bottom page an basically what I did was:
var element = document.getElementById("someID");
element.parentNode.removeChild(element);
put that code when the window loads and add it to your htmlText, it will remove the element for you before it generates the PDF.
To do this you need to enable js by simply adding to your Objects :
WebSettings =
{
EnableJavascript = true
}
Hope it helps you :)

Replacing InnerText of a TextBox

Using the MS OpenXml Sdk I have been able to copy a templatestream to a resultstream and append dynamic text(w.p>>w.r>>w.t) at the end of the body using the following code:
var templateStream = File.OpenRead(templatePath);
templateStream.CopyTo(resultStream);
using (var resultPackage = WordprocessingDocument.Open(resultStream, true))
{
var document = resultPackage.MainDocumentPart.Document;
var body = document.Body;
// Add new text.
var para = body.AppendChild(new Paragraph());
var run = para.AppendChild(new Run());
run.AppendChild(new Text(firstName));
document.Save();
}
My next logical step was to then replace the innertext of a textbox in the resultStream with the firstName as in the code below.
// replacing code in using statement from above
var document = resultPackage.MainDocumentPart.Document;
var textbox = document.Descendants<TextBox>().First();
const string firstNametag = "<<IH.FirstName>>";
if (textbox.InnerText.Contains(firstNametag))
{
var textboxContent = textbox.Elements<TextBoxContent>().First();
textboxContent.RemoveAllChildren();
var paragraph = textboxContent.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
run.AppendChild(new Text(firstName));
}
document.Save();
In the first example and with some additional code the result stream is appropriately serialized to a docx and the firstName is appended to the end of the body when viewed in Word. In the second example though the textbox and its contents remain the same even though further examination in the debugger showed the textboxContent's children reflecting the changes made above.
I am new to OpenXML development so if there is anything obvious please point it out.
Oh wow, I don't even want to think I understand the whole picture around this but here's a quick stab at it. Someone with more openXml experience please chime in...
Turns out when you create a textbox in word on a docx the document.xml file get's the following markup:
<w.r>
<mc.AlertnateContent>
<mc.Choice Requires="wps">
<wps:txbx>
<w:txbxContent>
<w:r>
<w.t>
Text Goes Here
</w.t>
</w.r>
</w:txbxContent>
</wps:txbx>
</mc.Choice>
<mc.Fallback>
<v.textbox>
<w:txbxContent>
<w:r>
<w.t>
Text Goes Here
</w.t>
</w.r>
</w:txbxContent>
</v.textbox>
</mc.Fallback>
</mc.AlertnateContent>
</w.r>
Notice the mc.AlternateContent, mc.Choice, and mc.Fallback tags. What the heck are these??? Someone put it this way on a blog article I came across -
"As I understand it - but don't take my word as gospel - AlternateContent can
appear anywhere and provides a mechanism for including enhanced
functionality if the consuming application can handle it, along with a
fallback if it can't."
-Tony Jollans - http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f8a5c277-7049-48c2-a295-199d2914f4ba/
In my case I was only modifying the fallback textbox(v.txbx not wps.txbx) because of my mishap in assuming Resharper was right in asking me to import the DocumentFormat.OpenXml.Vml namespace for my dependency on the TextBox object. Not sure why there isn't a Textbox definition in one of my already included namespaces, DocumentFormat.OpenXml.Packaging or DocumentFormat.OpenXml.Wordprocessing but that's beyond the scope of this question. Needless to say, upon realizing this and updating my code to look for the common w.txbxContent for the two I achieved what I wanted to do.
Here's the updated code with some refactoring, call the ReplaceTag method in the using statement from the original question and supply a model object instead of a string. Also, use the tagToValueSelector dictionary for convenience.
private void ReplaceTags(Document document, SomeModel model)
{
var textBoxContents = document.Descendants<TextBoxContent>().ToList();
foreach (var textBoxContent in textBoxContents)
{
ReplaceTag(textBoxContent, model);
}
}
private void ReplaceTag(TextBoxContent textBoxContent, SomeModel model)
{
var tag = textBoxContent.InnerText.Trim();
if (!tagsTomValues.ContainsKey(tag)) return;
var valueSelector = tagsTomValues[tag];
textBoxContent.RemoveAllChildren();
var paragraph = textBoxContent.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
run.AppendChild(new Text(valueSelector(model)));
}
// called in the ctor
private static void IntializeTags(IDictionary<string, Func<SomeModel, string>> dictionary)
{
dictionary.Add("<<IH.Name>>", m => string.Format("{0} {1}", m.FirstName, m.LastName));
}
Happy openXmling :)

Categories

Resources