I've been trying to modify a rich text field to display correctly in its half of the horizontal field.
The goal is this:
---------------------------
| address is | ***********|
| very long | ** IMAGE **|
| state, zip | ***********|
---------------------------
Where address is a single string separate from the city and zip.
I am modifying the address field like this:
RichTextField addrField = new RichTextField(address) {
public int getPreferredWidth() {
return 200;
}
protected void layout(int maxWidth,int maxHeight) {
super.layout(getPreferredWidth(),maxHeight);
setExtent(getPreferredWidth(), getHeight());
}
};
The results look like this:
-----------------------------
| address is ve| ***********|
| state, zip | ** IMAGE **|
| | ***********|
-----------------------------
where clearly the address is just going under the image. Both horizontal fields are static 200 pixels wide. It's not like the system wouldn't know where to wrap the address.
However, I have heard it is not easy to do this and is not done automatically.
I have had no success finding a direct answer online. I have found people saying you need to do it in a custom layout manager, some refer to the RichTextField API, which is of no use. But nobody actually mentions how to do it.
I understand that I may need to read character by character and set where the line breaks should happen. What I don't know is how exactly to do any of this. You can't just count characters and assume each is worth 5 pixels, and you shouldn't have to.
Surely there must be some way to achieve this in a way that makes sense.
Any suggestions?
I don't have an exact solution to your problem but in terms of measuring the width of text, if you have a reference to the Font object for the font being used in your field, you can call Font.getAdvance() to get the width of the specified text in pixels. This might help if you have to insert manual breaks in your text.
Related
The overall type structure and utilization in my current F# is working very well. However, I want to get some perspective if I am doing something incorrectly or following some kind of anti-pattern. I do find myself very often essentially expecting a particular type in particular logic that is pulling from a more general type that is a Discriminated Union unifying a bunch of distinct types that all follow layers of common processing.
Essentially I need particular versions of this function:
'GeneralDiscriminatedUnionType -> 'SpecificCaseType
I find myself repeating many statements like the following:
let checkPromptUpdated (PromptUpdated prompt) = prompt
This is the simplest way that I've found to this; however, every one of these has a valid compiler warning that makes sense that there could be a problem if the function is called with a different type than the expected. This is fair, but I so far have like 40 to 50 of these.
So I started trying the following out, which is actually better, because it would raise a valid exception with incorrect usage (both are the same):
let checkPromptUpdated input = match input with | PromptUpdated prompt -> prompt | _ -> invalidOp "Expecting Prompt"
let checkPromptUpdated = function | PromptUpdated prompt -> prompt | _ -> invalidOp "Expecting Prompt"
However, this looks a lot messier and I'm trying to find out if anyone has any suggestions prior to me doing this messiness all over.
Is there some way to apply this wider logic to a more general function that could then allow me to write this 50 to 100x in a cleaner and more direct and readable way?
This question is just a matter of trying to write cleaner code.
This is an example of a DU that I'm trying to write functions for to be able to pull the particular typed values from the cases:
type StateEvent =
| PromptUpdated of Prompt
| CorrectAnswerUpdated of CorrectAnswer
| DifficultyUpdated of Difficulty
| TagsUpdated of Tag list
| NotesUpdated of Notes
| AuthorUpdated of Author
If the checkPromptUpdated function only works on events that are of the PromptUpdated case, then I think the best design is that the function should be taking just a value of type Prompt (instead of a value of type StateEvent) as an argument:
let checkPromptUpdated prompt =
// do whatever checks you need using 'prompt'
Of course, this means that the pattern matching will get moved from this function to a function that calls it - or further - to a place where you actually receive StateEvent and need to handle all the other cases too. But that is exactly what you want - once you pattern match on the event, you can work with the more specific types like Prompt.
This works for me
let (TypeUWantToExtractFrom unwrappedValue) = wrappedValue
I would like to add the vertical pipes so that I can have my data table down there under "Examples" feature in Specflow. Anyone to give me any tip so I can go through it?. My scenario outline looks like:
#mytag
Scenario Outline: Check Noun Words
Given Access the AnalyzeStateless URL
And language code
And content of <Sentence>
And the Expected KeyWord <Expected KeyWords>
And the Expected Family ID <Expected FID>
And the index <Index>
When return the XML response
Then the keyword should contain <FamilyID>
Examples:
| Index | Sentence | Expected KeyWords | Expected FID |
| 1 | I need a personal credit card | personal | 92289 |
The "Examples" feature has been manually entered in above case. I have a thousand of rows on an excel file, any appropriate way to get all of the values in one go?
Have you looked at Specflow.Excel which allows you to keep your examples in your excel files?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In my VCL project, I have a TMemo with the following text (| is the caret):
| |
|Hello world | |
| |
|test |
| |
| |
When I press the Down button, the caret moves here:
| |
|Hello world |
|| |
|test |
| |
| |
What I need is for it to move here instead:
| |
|Hello world |
| | |
|test |
| |
| |
I think you would find it instructive to devise your own solution for this. The default behaviour for a TMemo responding to the Down key depends on how many characters there are on the next line. If there are at least as many on the next line as there are on the current line, the caret will stay in the same column number.
So a simple solution might be
When you detect an OnKeyDown event caused by the Down key, check the number of characters in the next line, and if it is fewer than the number of characters in the current line to the left of the caret, right-pad the line below with spaces until the numbers of characters are equal. The on-screen appearance will only exactly maintain the column position if the memo uses a fixed-point font; with a proportional font, the cursor will still "wiggle" a bit left and right because spaces are narrower than most other characters.
Of course, you would need to do this for the Up key too, and it's up to you whether you do similar for mouse clicks.
With a bit of googling you can easily find Delphi code to detect the current line and column number of the caret in a TMemo.
The TMemo in Delphi merely wraps the internal control within Windows itself - which does not natively support such functionality that you desire. It may be possible to modify it to behave in such manner, but easier to use something which is already designed for this. One largely popular control which can do this is the SynEdit which is geared towards code editors and syntax highlighting. It might do much more than you need, but it solves what you're looking for.
I'm reading about F# and looking at people's source code and I sometimes see
Type test =
| typeone
| typetwo
And sometimes i see
type test = typeone | typetwo
One of them has a pipe before and the one doesn't. At first I thought one was an enum vs discriminated Union but I THINK they are the same. Can someone explain the difference if there is any?
There is no difference. These notations are completely equivalent. The leading pipe character is optional.
Having this first pipe optional helps make the code look nicer in different circumstances. In particular, if my type has many cases, and each case has a lot of data, it makes sense to put them on separate lines. In this case, the leading pipe makes them look visually aligned, so that the reader perceives them as a single logical unit:
type Large =
| Case1 of int * string
| Case2 of bool
| SomeOtherCase
| FinalCase of SomeOtherType
On the other hand, if I only need two-three cases, I can put them on one line. In that case, the leading pipe only gets in the way, creating a feeling of clutter:
type QuickNSmall = One | Two | Three
There is no difference.
In the spec, the first | is optional.
The relevant bit of the spec is this:
union-type-cases:= '|'opt union-type-case '|' ... '|'
union-type-case
An enum would needs to give explicit values to the cases like
Type test =
| typeone = 1
| typetwo = 2
As already mentioned, the leading | is optional.
The examples in the other answers do not show this, so it is worth adding that you can omit it even for a multi-line discriminated union (and include it when defining a single line union):
type Large =
Case1 of int * string
| Case2 of bool
| SomeOtherCase
| FinalCase of SomeOtherType
type QuickNSmall = | One | Two | Three
I think most people just find these ugly (myself included!) and so they are usually written the way you see in the other answers.
I am testing out F# and using NUnit as my test library; I have discovered the use of double-back ticks to allow arbitrary method naming to make my method names even more human readable.
I was wondering, whether rightly or wrongly, if it is possible to parameterise the method names when using NUnit's TestCaseAttribute to change the method name, for example:
[<TestCase("1", 1)>]
[<TestCase("2", 2)>]
let ``Should return #expected when "#input" is supplied`` input expected =
...
This might not be exactly what you need, but if you want to go beyond unit testing, then TickSpec (a BDD framework using F#) has a nice feature where it lets you write parameterized scenarios based on back-tick methods that contain regular expressions as place holders.
For example, in Phil Trelford's blog post, he uses this to define tic-tac-toe scenario:
Scenario: Winning positions
Given a board layout:
| 1 | 2 | 3 |
| O | O | X |
| O | | |
| X | | X |
When a player marks X at <row> <col>
Then X wins
Examples:
| row | col |
| middle | right |
| middle | middle |
| bottom | middle |
The method that implements the When clause of the scenario is defined in F# using something like this:
let [<When>] ``a player marks (X|O) at (top|middle|bottom) (left|middle|right)``
(mark:string,row:Row,col:Col) =
let y = int row
let x = int col
Debug.Assert(System.String.IsNullOrEmpty(layout.[y].[x]))
layout.[y].[x] <- mark
This is a neat thing, but it might be an overkill if you just want to write a simple parameterized unit test - BDD is useful if you want to produce human readable specifications of different scenarios (and there are actually other people reading them!)
This is not possible.
The basic issue is that for every input and expected you need to create a unique function. You would then need to pick the correct function to call (or your stacktrace wouldn't make sense). As a result this is not possible.
Having said that if you hacked around with something like eval (which must exist inside fsi), it might be possible to create something like this, but it would be very slow.