Theo, your code on my 0.9.28.2-2 GTK gets closer. It selects the item under the right-click. But for some reason the routines fired off by the popup menu are seeing the 'old' selection. (For completeness, the popup menu brings up an 'Edit' item, the handler of which calls:
procedure TMainForm.OpenSelectedSourceFile();
begin
if self.sources_filelistbox.SelCount = 1 then
self.OpenInEditor(self.sources_filelistbox.FileName)
else
MessageDlg ('No file selected', 'You must first select a file from the Sources list.', mtError,
[mbOk],0)
end;
where my TFileListBox is called sources_filelistbox)
Thinking that what was happening was that the popup handling was taking place before the mouse down handling, I disabled the popup menu from the Object Inspector and tried popping it up manually:
procedure TMainForm.sources_filelistboxMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var item:integer;
begin
if Button = mbRight then
begin
item:=self.sources_filelistbox.ItemAtPos(Point(X,Y),true);
if item>-1 then
begin
self.sources_filelistbox.Selected[item]:=true;
sources_popupmenu.PopUp();
end;
end;
end;
This also makes the popup routines think they are working with the 'old' selection (?!) and introduces another oddity: Only every odd numbered right click brings up the popup. In other words, the first right click selects the item under the pointer and pops up the menu, the second only selects an item, the third selects the item and pops up the menu, the fourth only selects an item, etc.
I also trued a small variation of the above that does NOT select the clicked item:
procedure TMainForm.sources_filelistboxMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var item:integer;
begin
if Button = mbRight then
begin
item:=self.sources_filelistbox.ItemAtPos(Point(X,Y),true);
if item>-1 then
begin
//self.sources_filelistbox.Selected[item]:=true;
sources_popupmenu.PopUp();
end;
end;
end;
This fixes the 'old selection' problem but not the 'every other right click' problem.
I am confused but suspect I am missing something pretty basic here.