Changing StringGadget read-only attribute on the fly ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Changing StringGadget read-only attribute on the fly ?

Post by Shardik »

At least two Windows solutions to toggle the read only state of a StringGadget without the need to delete and recreate the StringGadget have already been posted long ago:
PB (2003): http://www.purebasic.fr/english/viewtop ... 28&start=1
Arctic Fox (2009): http://www.purebasic.fr/english/viewtop ... 00&start=1

The following code example demonstrates how to toggle the read only state of a StringGadget cross-platform on Linux, MacOS and Windows without the need to delete and recreate the StringGadget. I have tested this code successfully on these operating systems:
- MacOS X 10.6.8 (Snow Leopard)
- Ubuntu 12.04 LTS x64 with KDE
- Windows 7 x64 SP1

Code: Select all

OpenWindow(0, 270, 100, 300, 90, "Toggle ReadOnly attribute") 
StringGadget(0, 10, 15, 280, 25, "Status: Read and Write")
ButtonGadget(1, 70, 50, 150, 27, "Enable Read only")
ReadOnlyState = #False

Repeat 
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        If EventType() = #PB_EventType_LeftClick
          ReadOnlyState = ReadOnlyState ! 1

          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Linux
              gtk_editable_set_editable_(GadgetID(0), ReadOnlyState ! 1)
            CompilerCase #PB_OS_MacOS
              CocoaMessage(0, GadgetID(0), "setSelectable:", ReadOnlyState)
              CocoaMessage(0, GadgetID(0), "setEditable:", ReadOnlyState ! 1)
            CompilerCase #PB_OS_Windows
              SendMessage_(GadgetID(0), #EM_SETREADONLY, ReadOnlyState, 0)
          CompilerEndSelect

          If ReadOnlyState
            SetGadgetText(0, "Status: Read only")
            SetGadgetText(1, "Disable Read only")
          Else
            SetGadgetText(0, "Status: Read and Write")
            SetGadgetText(1, "Enable Read only")
          EndIf
        EndIf
      EndIf
  EndSelect
ForEver
User avatar
majikeyric
Enthusiast
Enthusiast
Posts: 181
Joined: Mon Oct 21, 2013 5:21 pm
Location: France
Contact:

Re: Changing StringGadget read-only attribute on the fly ?

Post by majikeyric »

Thanks for your quick and kind help :)

I like your cross platform solution Shardick

Yes, PB should handle this natively with SetGadgetAttribute() like with the editor gadget :?

SetGadgetAttribute() is supported in the EditorGadget help, at least in the french one...
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Changing StringGadget read-only attribute on the fly ?

Post by Thunder93 »

I feel favouritism is at play here... :lol:
majikeyric wrote:Yes, PB should handle this natively with SetGadgetAttribute() like with the editor gadget :?

SetGadgetAttribute() is supported in the EditorGadget help, at least in the french one...
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
majikeyric
Enthusiast
Enthusiast
Posts: 181
Joined: Mon Oct 21, 2013 5:21 pm
Location: France
Contact:

Re: Changing StringGadget read-only attribute on the fly ?

Post by majikeyric »

Thunder93 wrote:I feel favouritism is at play here... :lol:
yes :mrgreen: 8) :D
Post Reply