Recent

Author Topic: [Solved!!!] Getting basic state-dependent properties for a component  (Read 1162 times)

heebiejeebies

  • New Member
  • *
  • Posts: 39
Hi everyone!  :)

I have asked this question before, buried in the depths of another question, but I didn't really get any useful answers - so I'll ask it again as the main question.

In my current scenario, I need to get the currently selected value of a PopupMenu.  To me, it would be logical that this would be something like "if PopupMenu1.SelectedItem = 'My item' then;...

But I have tried everything I can think of in place of 'SelectedItem' and I still can't find what the correct property name is.  The wiki always seems to give a million things that I'll never use, and of course it gives all the properties that you can easily see and change in the object inspector.  But it never seems to give the basic state-dependent properties like what item is selected from the PopupMenu.  Someone suggested I type the component name and a dot, then wait for a menu to appear offering all options.  Yes, the menu appeared - but I still can't see any option that gives me the currently selected item in the PopupMenu!

So my question is: surely there's a simple property name that represents the currently selected menu item in a popup menu, but more importantly so I don't have to annoy you guys every single time....

Surely there's a way I can figure these out for myself without having to ask??  And just in case you didn't read the whole post, that's evidently not the wiki!   :D

Thank you very much!  :)
« Last Edit: April 22, 2021, 10:14:46 am by heebiejeebies »
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

Zvoni

  • Hero Member
  • *****
  • Posts: 550
Re: Getting basic state-dependent properties for a component
« Reply #1 on: April 20, 2021, 02:12:16 pm »
IIRC, a popup-menu (as well as a regular menu)-item, has three ways to "act" (I'm not talking about menu-items which contain a submenu):
1) The regular way: Fire an Event
2) The Checkbox-Way: Turn option on or off
3) The Option-Button way: Out of a group only one is on, the others are off

example: On Windows, right-click on your desktop
a context-menu (also called a popup-menu) shows up.
The second to last entry should be something like "Display Options" (sorry, german Windows here "Anzeigeeinstellungen")
That would be an example for 1) --> It fires an event launching Display Options from control panel

Further, in that context-menu the first item should be "View" (german "Ansicht"), which has a "sub-menu", showing examples
for 2) --> "Show Desktop-Symbols"
for 3) --> "Big/Medium/Small Symbols"

For which one of those "ways" are you looking for a property?
« Last Edit: April 20, 2021, 02:16:27 pm by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Handoko

  • Hero Member
  • *****
  • Posts: 4214
  • My goal: build my own game engine using Lazarus
Re: Getting basic state-dependent properties for a component
« Reply #2 on: April 20, 2021, 02:18:47 pm »
Maybe a video tutorial can help:
https://www.youtube.com/watch?v=dcZd5HaiwyA

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 511
  • Professional amateur ;-P
Re: Getting basic state-dependent properties for a component
« Reply #3 on: April 20, 2021, 11:26:46 pm »
Hey heebiejeebies,

The question you ask: "In my current scenario, I need to get the currently selected value of a PopupMenu" doesn't make sense to me, so I'll need a bit more clarification.

A component that as the concept of a selected item is usually something with the name List on it, like a ListBox.
On a ListBox there is a property called ItemIndex that if it's value is different than -1, an Item of the List is selected.
It makes sense to maintain this selected state since selecting an item on a list is the first of any number of following actions.

To select a menu is a single action concept that does not need to maintain state for any other action.

This concept does not translate into menus, because when you select a menu item, it immediately fires the OnClick event and that's it, there's no maintained state.
If you want to know what menu item was selected, then you have two way:
  • If it was called via an action, the action passed(Sender) onto the Execute has a property called ActionComponent that will give you access to the menu item that triggered the action
  • If it was called via it's own OnClick event the Sender is the menu item itself.

So, it really begs the question: Why do you think that there should be a concept of a selected state after the action/event has been triggered?

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

heebiejeebies

  • New Member
  • *
  • Posts: 39
Re: Getting basic state-dependent properties for a component
« Reply #4 on: April 21, 2021, 01:10:21 am »
Thanks again everyone!  I realise now that I misinterpreted the purpose of the PopupMenu - I thought it was just a menu that popped up anywhere on the form and allowed you to select between various options.  Like the audio input selectors in Audacity, for example.  I was wondering why it wasn't appearing and thought that was another rabbit hole I'd have to go down! I managed to mock it up so that it pops up with a left-click on a TButton, and then whatever option you choose changes the caption of the TButton to that option.  Is that really the only way to do it though?  It looks a bit ugly and non-standard. I thought the other way was a reasonably standard UI item?  %)
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4454
Re: Getting basic state-dependent properties for a component
« Reply #5 on: April 21, 2021, 01:38:06 am »
most controls have a PopupMenu property.. you Assign what ever popup you design to that property...

When you RIGHT click on that control the menu will popup..

There is a OnPopUp event  you can use which carries the sender, that reports what control popped it.

Each MenuItem has a onClick..
The only true wisdom is knowing you know nothing

heebiejeebies

  • New Member
  • *
  • Posts: 39
Re: Getting basic state-dependent properties for a component
« Reply #6 on: April 21, 2021, 03:55:44 am »
Thanks Jamie.

So just to confirm, there's no way to do a simple, always-there popup menu like these in Lazarus?:

https://i.postimg.cc/4xym0k7f/audacity.png

Thanks guys
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

speter

  • Full Member
  • ***
  • Posts: 194
Re: Getting basic state-dependent properties for a component
« Reply #7 on: April 21, 2021, 05:25:08 am »
I am not sure, but you may be referring to a toolbar.

As others have explained a popup menu is generally something that appears in response to a right mouse button click, and displays a menu allowing the user to select a menu-item.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

heebiejeebies

  • New Member
  • *
  • Posts: 39
Re: Getting basic state-dependent properties for a component
« Reply #8 on: April 21, 2021, 07:53:56 am »
Not a toolbar or a coolbar.  I'm only talking about the pop-up menus in the middle of the Audacity toolbar that I linked.

Anyway, looks like there's no way to do it. Quite strange! But I'll work around it. Thanks everyone
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 4214
  • My goal: build my own game engine using Lazarus
Re: Getting basic state-dependent properties for a component
« Reply #9 on: April 21, 2021, 08:12:40 am »
I saw no popup menu in the screenshot you provided. Maybe what you meant is TComboBox.

heebiejeebies

  • New Member
  • *
  • Posts: 39
Re: Getting basic state-dependent properties for a component
« Reply #10 on: April 21, 2021, 09:12:10 am »
I saw no popup menu in the screenshot you provided. Maybe what you meant is TComboBox.

Right in the middle where it says "ALSA", "HDA Intel..." "2 (Stereo)" and "default".  These are pop up menus that are always visible, and accessible with the left mouse button.  I would have demonstrated them but Ubuntu won't take a screenshot while a menu is popped up.

You're so close with the combo box.  That's very nearly it, except that you can edit the options.  Mine needs to be just a read-only list, and unfortunately the ReadOnly property refuses to enable.  According to the wiki, that property is deprecated.
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 4214
  • My goal: build my own game engine using Lazarus
Re: Getting basic state-dependent properties for a component
« Reply #11 on: April 21, 2021, 10:07:18 am »
I'm not sure what you want but maybe you can try my custom popup:
https://forum.lazarus.freepascal.org/index.php/topic,37369.msg250939.html#msg250939

Zvoni

  • Hero Member
  • *****
  • Posts: 550
Re: Getting basic state-dependent properties for a component
« Reply #12 on: April 21, 2021, 10:18:17 am »
I think what he's looking for is ToolButton, which appears to be a member of a ToolBar

https://lazarus-ccr.sourceforge.io/docs/lcl/comctrls/ttoolbutton.html
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

lucamar

  • Hero Member
  • *****
  • Posts: 3894
Re: Getting basic state-dependent properties for a component
« Reply #13 on: April 21, 2021, 10:45:18 am »
You're so close with the combo box.  That's very nearly it, except that you can edit the options.  Mine needs to be just a read-only list, and unfortunately the ReadOnly property refuses to enable.  According to the wiki, that property is deprecated.

What you want is indeed a TComboBox with Style set to csDropDownList which shouldn't allow editing.

However it will neither look or behave like the Audacity one because Audacity uses wxWidget which is not available (AFAIK) for Lazarus/FPC; moreover, after some testing (Linux x86_64) I've discovered that whatever style you use you can always type something in the "edit" section, so the behaviour seems to be either widgetset-dependant or "buggy".

You can approximate what you want with a TToolButton associated to a TPopupMenu but then you'll have to keep track of the "selected item" through each item's OnClick. That's not as hard as it sounds: you can just use  a single OnClick handler for all menu items and assign inside it some property/variable depending on which item was actually clicked. If it's a property, its writer could then update the caption of the button and do whatever else you want it to do.

Of course, it's not easy as (almost) all that being done automagically but it's doable and not much code needs be added.

HTH!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zvoni

  • Hero Member
  • *****
  • Posts: 550
Re: Getting basic state-dependent properties for a component
« Reply #14 on: April 21, 2021, 11:09:37 am »
To finish this off:
are you looking for something like this?: https://blog.dummzeuch.de/2014/08/31/showing-a-dropdown-menu-when-clicking-a-button/
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

 

TinyPortal © 2005-2018