Page 1 of 1

Changing StringGadget read-only attribute on the fly ?

Posted: Sat May 10, 2014 9:51 pm
by majikeyric
Hello everybody!

Is there a way like with the EditorGadget to change (on the fly) the read-only attribute of an already defined StringGadget ?

this is possible with an editor gadget:

Code: Select all

SetGadgetAttribute(#editor,#PB_Editor_ReadOnly,1)
but this is not with a string gadget:

Code: Select all

SetGadgetAttribute(#stringgadget,#PB_String_ReadOnly ,1)
Many thanks.

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

Posted: Sun May 11, 2014 3:11 am
by Thunder93
You could use the following on Windows. However, to me that would be beside the point. SetGadgetAttribute() should cover this in a cross-platform way.

Code: Select all

SendMessage_(GadgetID(#StringGadget), #EM_SETREADONLY, [1 - ReadOnly, 0 - Normal], 0)

In the PB Manual It states what it supports for attributes.
  • - SetGadgetAttribute() with the following attribute:
    • #PB_String_MaximumLength: Set the maximum number of characters which can be entered.

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

Posted: Sun May 11, 2014 3:21 am
by Thunder93
I suppose we should be happy that it works for EditorGadget. In the PB Manual there is no mentioning of SetGadgetAttribute() support. :shock:

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

Posted: Sun May 11, 2014 8:16 am
by Demivec
majikeyric wrote:Is there a way like with the EditorGadget to change (on the fly) the read-only attribute of an already defined StringGadget ?
You can do it by recreating the gadget on the fly.

Code: Select all

;initial gadget creation
StringGadget(#stringgadget, x, y, width, height, text$)

;Set Read-Only mode
StringGadget(#stringgadget, GadgetX(#stringgadget), GadgetY(#stringgadget),
             GadgetWidth(#stringgadget), GadgetHeight(#stringgadget),
             GetGadgetText(#stringgadget),#PB_String_ReadOnly)

;Turn off Read-Only mode
StringGadget(#stringgadget, GadgetX(#stringgadget), GadgetY(#stringgadget),
             GadgetWidth(#stringgadget), GadgetHeight(#stringgadget),
             GetGadgetText(#stringgadget))

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

Posted: Sun May 11, 2014 8:58 am
by PB
> You can do it by recreating the gadget on the fly

As long as you don't care about losing its colors, gadget data, tooltip,
disabled state, password flag, numeric flag, borderless flag, max length.

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

Posted: Sun May 11, 2014 9:02 am
by RASHAD
Windows Only
- No Cursor or Caret shown

Code: Select all

If OpenWindow(0,0,0,300,200,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  StringGadget(0,10,10,200,22,"Click the LMB on this window")
  ButtonGadget(1,10,170,80,24,"Read Only")
  ButtonGadget(2,100,170,80,24,"Writeable")
  
  ContainerGadget(5,0,0,0,0)
  CloseGadgetList()
  SetClassLongPtr_(GadgetID(5),#GCL_HBRBACKGROUND, GetStockObject_(#NULL_BRUSH))

  Repeat
   Select WaitWindowEvent()
     Case #PB_Event_CloseWindow
              Quit = 1
              
     Case #PB_Event_Gadget
          Select EventGadget()
              Case 1
                     ResizeGadget(5,10,10,200,22)
                     BringWindowToTop_(GadgetID(5))
              
              Case 2
                     ResizeGadget(5,0,0,0,0)
                     
          EndSelect
   EndSelect                   

  Until Quit = 1
EndIf


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

Posted: Sun May 11, 2014 9:27 am
by Demivec
PB wrote:> You can do it by recreating the gadget on the fly

As long as you don't care about losing its colors, gadget data, tooltip,
disabled state, password flag, numeric flag, borderless flag, max length.
Yes, it seems it was an idea that needed a little work. I tried some test code with it and I ran into some other unexpected problems as well. :oops:

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

Posted: Sun May 11, 2014 11:47 am
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

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

Posted: Sun May 11, 2014 4:00 pm
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...

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

Posted: Sun May 11, 2014 4:09 pm
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...

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

Posted: Sun May 11, 2014 4:28 pm
by majikeyric
Thunder93 wrote:I feel favouritism is at play here... :lol:
yes :mrgreen: 8) :D