Mark menu item as checked

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

redfield
Posts: 58
Joined: Thu Apr 04, 2019 1:41 pm

Mark menu item as checked

Post by redfield » Tue Oct 15, 2019 9:05 pm

Hey Experts,
I have created a menu with the menu builder and in the builder noticed an option to mark an item as checked. When I select it, a !c appears in front of the item and in the application the item appears as checked. But I don't want it permanently checked but only when the item has been clicked. I tried coding it with the property the mark but without success. Is it actually possible to code this?

Klaus
Posts: 11771
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Mark menu item as checked

Post by Klaus » Tue Oct 15, 2019 9:33 pm

Hi redfield,

please check this LC lesson: http://lessons.livecode.com/m/2592/l/12 ... -menu-item


Best

Klaus

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4394
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Mark menu item as checked

Post by richmond62 » Tue Oct 15, 2019 9:41 pm

Being "clever" I tried this:

Code: Select all

on menuPick itemPicked
   set the checkmark of itemPicked to true
end menuPick
But that was because I was silly and didn't read THIS first:
-
Screenshot 2019-10-15 at 23.39.38.png
-
Mind you: it might "be lovely" if it were re-introduced into LiveCode.

bogs
Posts: 4641
Joined: Sat Feb 25, 2017 10:45 pm

Re: Mark menu item as checked

Post by bogs » Tue Oct 15, 2019 9:50 pm

LOL Klaus, I was just starting to type
Of course it is, you might try menuItem and ...
when you popped up the link :P :P :P

@Richmond - I agree that sure would be easier heh.
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4394
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Mark menu item as checked

Post by richmond62 » Tue Oct 15, 2019 10:06 pm

Screenshot 2019-10-16 at 0.04.52.png
Screenshot 2019-10-16 at 0.04.52.png (6.93 KiB) Viewed 1466 times
oink.jpg
oink.jpg (7.35 KiB) Viewed 1466 times
-
Seems a slightly long-winded way of doing something that seems to have been done in a simpler way in HyperCard.

Klaus
Posts: 11771
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Mark menu item as checked

Post by Klaus » Wed Oct 16, 2019 11:27 am

richmond62 wrote:
Tue Oct 15, 2019 10:06 pm
Seems a slightly long-winded way of doing something that seems to have been done in a simpler way in HyperCard.
Post this as a feature request here: https://quality.livecode.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4394
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Mark menu item as checked

Post by richmond62 » Wed Oct 16, 2019 6:36 pm

https://quality.livecode.com/show_bug.cgi?id=22413

"There we are."

Which must rank as one of the most ridiculous and meaningless phrases there is in English. 8)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3352
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Mark menu item as checked

Post by bn » Fri Oct 18, 2019 4:14 pm

Here are some code snippets that may help in handling popUp, pullDown, and Menu buttons

This allows for multiple choices in above buttons

put this into the script of above buttons

use

Code: Select all

set the toggleChoice of me to pItem
to toggle the checkmark

use

Code: Select all

put the ChoiceIsActive of me into tIsActive
to check the state of the menu item.


here is an complete example

Code: Select all

on menuPick pItem
   local tIsActive
   
   ## toggle choice on/off of multiple items of 
   ## a menu button, a pop up button or a pulldown button
   
   set the toggleChoice of me to pItem
   
   ## check the state of the choice if you need to know
   put the ChoiceIsActive of me into tIsActive
   
   -- more code
end menuPick


setProp toggleChoice pItem
   local tText, tLineNum, tPrefix
   put "!c" into tPrefix
   put the text of the target into tText
   
   put lineOffset(tPrefix & pItem, tText) into tLineNum
   if tLineNum is 0 then
      put lineOffset(pItem, tText) into tLineNum
   end if
   if char 1 to 2 of line tLineNum of tText is tPrefix then
      put empty into char 1 to 2 of line tLineNum of tText
   else
      put tPrefix before line tLineNum of tText
   end if
   set the text of the target to tText
end toggleChoice

getProp ChoiceIsActive
   return line (the menuHistory of the target) of the text of the Target begins with "!c"
end ChoiceIsActive
You can put the setter and getter (setProp toggleChoice, getProp ChoiceIsActive) either into the same script or into a script in the message path. Typically a card script or the stack script. These are then accessible to all buttons that use "set the toggleChoice of me to pItem" and "put the ChoiceIsActive of me into tIsActive".

Kind regards
Bernd

bogs
Posts: 4641
Joined: Sat Feb 25, 2017 10:45 pm

Re: Mark menu item as checked

Post by bogs » Fri Oct 18, 2019 4:16 pm

OOooo, that is nice Bernd!
Image

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3352
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Mark menu item as checked

Post by bn » Fri Oct 18, 2019 4:19 pm

Here are some code snippets that may help in handling popUp, pullDown, and Menu buttons

This allows for a single choice in above buttons

Code: Select all

on menuPick pItem
   
   set the checkChoice of me to pItem
   
end menuPick

setProp checkChoice pItem
   local tText
   put the text of me into tText
   replace "!c" with empty in tText
   put "!c" before line (lineOffset(pItem, tText)) of tText
   set the text of me to tText
end checkChoice
Again you can put the handler "setProp checkChoice" further along in the message path so it is accessible from mutliple buttons of the same setup.

Kind regards
Bernd

bogs
Posts: 4641
Joined: Sat Feb 25, 2017 10:45 pm

Re: Mark menu item as checked

Post by bogs » Fri Oct 18, 2019 4:47 pm

Nice! I had started down that path, but couldn't work out the text part :oops:
Image

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3352
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Mark menu item as checked

Post by bn » Fri Oct 18, 2019 4:48 pm

this are two helper handlers for Multiple Choices buttons
it lets you find out the state of the button with mulitple choices and returns the state of the currently clicked item

Code: Select all

getProp ChoiceIsActive
   return line (the menuHistory of the target) of the text of the Target begins with "!c"
end ChoiceIsActive

and this lists the active (checked) choices of a multiple choices button

Code: Select all

getProp activeChoices
   local tText, tChoices
   put the text of the target into tText
   repeat for each line aChoice in tText
      if aChoice begins with "!c" then 
         put aChoice & cr after tChoices
      end if
   end repeat
   delete char -1 of tChoices -- a return
   return tChoices
end activeChoices
a complete script would look like this

Code: Select all

on menuPick pItem
   local tIsActive, tActiveChoices
   
   set the toggleChoice of me to pItem
   
   ## check the state of the current choice
   ## returns true if checked else false
   put the ChoiceIsActive of me into tIsActive
   
   ## returns a list of items that are currently checked
   put the activeChoices of me into tActiveChoices
   
   -- put the current state of a choice and the
   -- checked choices of a multiple choices button
   put tIsActive &cr & tActiveChoices && the milliseconds
   -- more code
end menuPick

setProp toggleChoice pItem
   local tText, tLineNum, tPrefix
   put "!c" into tPrefix
   put the text of the target into tText
   
   put lineOffset(tPrefix & pItem, tText) into tLineNum
   if tLineNum is 0 then
      put lineOffset(pItem, tText) into tLineNum
   end if
   if char 1 to 2 of line tLineNum of tText is tPrefix then
      put empty into char 1 to 2 of line tLineNum of tText
   else
      put tPrefix before line tLineNum of tText
   end if
   set the text of the target to tText
end toggleChoice

getProp ChoiceIsActive
   return line (the menuHistory of the target) of the text of the Target begins with "!c"
end ChoiceIsActive

getProp activeChoices
   local tText, tChoices
   put the text of the target into tText
   repeat for each line aChoice in tText
      if aChoice begins with "!c" then 
         put aChoice & cr after tChoices
      end if
   end repeat
   delete char -1 of tChoices -- a return
   return tChoices
end activeChoices
again the custom properties getters can be put further in the message path.
Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3352
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Mark menu item as checked

Post by bn » Sat Oct 19, 2019 12:27 am

here is a stack that sums it all up

It has some refinements over the scripts I posted above. Please use this stack.

The card script contains the working code for the buttons which just invoke that code.

Kind regards
Bernd
Attachments
set check marks checkmarks Menu III.livecode.zip
(2.42 KiB) Downloaded 31 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4394
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Mark menu item as checked

Post by richmond62 » Sat Oct 19, 2019 9:44 am

Had the way things were done in HyperCard card not been removed
all this long, complicated discussion would not have been necessary.

I assume (?) that the reason the HyperCard way was removed was for
a good reason . . . .

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7670
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Mark menu item as checked

Post by FourthWorld » Sat Oct 19, 2019 2:58 pm

richmond62 wrote:
Sat Oct 19, 2019 9:44 am
Had the way things were done in HyperCard card not been removed...
When was it ever present in LiveCode?

Of all the xTalk dialects I've used, I've never seen any of them claim to be a true superset of HyperTalk. Each has at least least some HyperTalk syntax implemented differently or not at all. To the best of my recollection this has been true of SuperCard, OMO, Plus, Gain, Toolbook, and others in addition to LiveCode.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”