Saturday, March 21, 2020

Double Click for TListView

Implementing On Item Click / Double Click for TListView Delphis TListView control displays a list of items in columns with column headers and sub-items, or vertically or horizontally, with small or large icons. As do most Delphi controls, the TListView exposes the OnClick and OnDblClick (OnDoubleClick) events. Unfortunately, if you need to know what item was clicked or double clicked you cannot simply handle the OnClick / OnDblClick events to get the clicked item. The OnClick (OnDblClick) event for the TListView is fired whenever the user clicks the control - that is whenever the click occurs somewhere inside the client area of the control. The user can click inside the list view, BUT miss any of the items. Whats more, since list view can change its display depending on the ViewStyle property, the user might have clicked on an item, on an item caption, on an item icon, nowhere, on an item state icon, etc. Note: the ViewStyle property determines how items are displayed in the list view: the items can be displayed as a set of movable icons, or as columns of text. ListView.On Item Click ListView.On Item Double Click To be able to locate the clicked (if there is one) item when the OnClick event for the list view is fired, you need to determine what elements of the list view lie under the point specified by the X and Y parameters - that is the location of the mouse at the moment of click. The TListiews GetHitTestInfoAt function returns information about the specified point in the list view’s client area. To make sure the item was clicked (or double clicked) you need to call the GetHitTestInfoAt and react only if the click event occurred on an actual item. Heres an example implementation of the ListView1s OnDblClick event: //handles ListView1s On Double Click procedure TForm.ListView1DblClick(Sender: TObject) ; var   Ã‚  hts : THitTests;   Ã‚  ht : THitTest;   Ã‚  sht : string;   Ã‚  ListViewCursosPos : TPoint;   Ã‚  selectedItem : TListItem; begin   Ã‚  //position of the mouse cursor related to ListView   Ã‚  ListViewCursosPos : ListView1.ScreenToClient(Mouse.CursorPos) ;   Ã‚  //double click where?   Ã‚  hts : ListView1.GetHitTestInfoAt(ListViewCursosPos.X, ListViewCursosPos.Y) ;   Ã‚  //debug hit test   Ã‚  Caption : ;   Ã‚  for ht in hts do   Ã‚  begin   Ã‚  Ã‚  Ã‚  sht : GetEnumName(TypeInfo(THitTest), Integer(ht)) ;   Ã‚  Ã‚  Ã‚  Caption : Format(%s %s | ,[Caption, sht]) ;   Ã‚  end;   Ã‚  //locate the double-clicked item   Ã‚  if hts [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] then   Ã‚  begin   Ã‚  Ã‚  Ã‚  selectedItem : ListView1.Selected;   Ã‚  Ã‚  Ã‚  //do something with the double clicked item!   Ã‚  Ã‚  Ã‚  Caption : Format(DblClcked : %s,[selectedItem.Caption]) ;   Ã‚  end; end; In the OnDblClick (or OnClick) event handler, read the GetHitTestInfoAt function by providing it with the location of the mouse inside the control. To get the location of the mouse related to the list view, the ScreenToClient function is used to convert a point (mouse X and Y) in screen coordinates to local, or client area, coordinates. The GetHitTestInfoAt return a value of THitTests type. The THitTests is a set of THitTest enumerated values. The THitTest enumeration values, with their description, are: htAbove - above the client area.htBelow - below the client area.htNowhere - inside the control, but not on an item.htOnItem - on an item, its text, or its bitmap.htOnButton - on a button.htOnIcon - on an icon.htOnIndent - on the indented area of an item.htOnLabel - on a label.htOnRight - on the right side of an item.htOnStateIcon - on a state icon or bitmap associated with an item.htToLeft - to the left of the client area.htToRight - to the right of the client area. If the result of the call to GetHitTestInfoAt is a subset (Delphi sets!) of [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] you can be sure the user clicked on the item (or on its icon / state icon). Finally, if the above is true, read the Selected property of the list view, it returns the first selected item (if multiple can be selected) in the list view. Do something with the clicked / double clicked / selected item ... Be sure to download the full source code to explore the code and learn by adopting it.

Thursday, March 5, 2020

Best Guess on the Year ‘Romeo and Juliet’ Was Written

Best Guess on the Year ‘Romeo and Juliet’ Was Written Although there is no record of when Shakespeare actually wrote Romeo and Juliet, it was first performed in 1594 or 1595. It is likely that Shakespeare wrote the play shortly before its premiere performance. But while  Romeo and Juliet  is one of Shakespeares most famous plays, the storyline is not entirely his own. So, who wrote the original Romeo and Juliet and when?   Italian Origins The origins of Romeo and Juliet are convoluted, but many people trace it back to an old Italian tale based on the lives of two lovers who tragically died for each other in Verona, Italy in 1303. Some say the lovers, although not from the Capulet and Montague families, were real people.   While this may as well be true, there is no clear record of such a tragedy occurring in Verona in 1303. Tracing it back, the year seems to be proposed by the City of Verona Tourist Site, most likely in order to boost touristic appeal.   Capulet and Montague Families The Capulet and Montague families were most likely based on the Cappelletti and Montecchi families, which did exist in Italy during the 14th century. While the term family is used, Cappelletti and Montecchi were not the names of private families but rather local political bands. In modern terms, perhaps the word clan or faction is more accurate. The Montecchi was a merchant family that competed with other families for power and influence in Verona. But there is no record of a rivalry between them and the Cappelletti. Actually, the Cappelletti family was based in Cremona. Early Text Versions of Romeo and Juliet In 1476, the Italian poet, Masuccio Salernitano, wrote a story titled Mariotto e Gianozza. The story takes place in Siena and centers around two lovers who are secretly married against the wishes of their families and end up dying for each other due to a tragic miscommunication. In 1530, Luigi da Porta published Giulietta e Romeo,  which was based on Salernitanos story. Every aspect of the plot is the same. The only differences are that Porta changed the names of the lovers and the setting location, Verona rather than Siena. Also, Porta added the ball scene in the beginning, where Giulietta and Romeo meet and has Giuletta  commit suicide by stabbing herself with a dagger rather than wasting away like in Salernitanos version. English Translations Portas Italian story was translated in 1562 by Arthur Brooke, who published the English version under the title The Tragical History of Romeus and Juliet. William Painter retold the story in prose in his 1567 publication, Palace of Pleasure. It is most likely that William Shakespeare read these English versions of the story and was thus inspired to pen Romeo and Juliet.