#Gadget or GadgetID - convert automatically to GadgetID

Share your advanced PureBasic knowledge/code with the community.
User avatar
EfDschoma
User
User
Posts: 17
Joined: Fri Feb 21, 2014 11:46 am

#Gadget or GadgetID - convert automatically to GadgetID

Post by EfDschoma »

I found the following Trick to handle automatically conversion to GadgetID, but this doesn't work in all situations reliable

Code: Select all

If IsGadget(GadgetID) And Not IsWindow_(GadgetID)     ; If you've passed a #Gadget instead of a GadgetID(), it will be convert automatically.
      GadgetID = GadgetID(GadgetID)
   EndIf
Therefore i tried to find an alternative conversion

Code: Select all

    If Not GetDlgCtrlID_( GadgetID )    ; If the reverse value of GadgetID is no GadgetNr the GadgetID-Value is no GadgetID
      GadgetID = GadgetID( GadgetID )    ; Convert it
    EndIf
I think this works reliable, please give me a feedback
I may not know much, but I try a lot
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by va!n »

Do a check with IsGadget()
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
EfDschoma
User
User
Posts: 17
Joined: Fri Feb 21, 2014 11:46 am

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by EfDschoma »

va!n wrote:Do a check with IsGadget()
Is not the same, because: I created a gadget with Nr 422 and called test -->

Code: Select all

Procedure test ( GadgetID  )
  If  Not IsGadget( GadgetID )             ; produces a value 3211200 which is true
    GadgetID = GadgetID( GadgetID )    ; therefor the conversion will not be done
  EndIf
  SendMessage_( GadgetID, #EM_SHOWBALLOONTIP, 0, @Balloon )  ; gives me a failure

  but:
  If Not GetDlgCtrlID_( GadgetID )       ; realizes that this is not really a GadgetID
    GadgetID = GadgetID( GadgetID )   ; will be executed
  EndIf
  SendMessage_( GadgetID, #EM_SHOWBALLOONTIP, 0, @Balloon ) ; proceeds without failure
I may not know much, but I try a lot
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by srod »

I must be missing something here because it all works fine for me. Nothing is debugged with the following which is correct.

Code: Select all

  If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(422, 10, 10, 200, 20, "Standard Button")

    If Not IsGadget(422)
      Debug "Not return #True!"
    EndIf

    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
You seem to be making additional work for yourself here.

Can you give me an example of when the following fails? Not that I'd ever use it because it simply isn't needed inmo. If some clot passes a hWnd instead of a gadget# to one of your library procedures then they deserve the outcome, whatever that might be! :) If you use #PB_Any in particular then this really isn't needed. IsGadget() will suffice.

Code: Select all

If IsGadget(GadgetID) And Not IsWindow_(GadgetID)     ; If you've passed a #Gadget instead of a GadgetID(), it will be convert automatically.
      GadgetID = GadgetID(GadgetID)
   EndIf
I may look like a mule, but I'm not a complete ass.
User avatar
EfDschoma
User
User
Posts: 17
Joined: Fri Feb 21, 2014 11:46 am

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by EfDschoma »

[quote="srod"]I must be missing something here because it all works fine for me. Nothing is debugged with the following which is correct.
...
Thank you for your attention, here is my code. It seems that my other code uses by calling 'Show_StrGdgBalloon' GadgetNr or the GadgetID,
and i couldnt find the problem, so I will solve this here and be sure that i use GadgetID and
if i dont use 'If Not GetDlgCtrlID_( GadgetID )' my balloon on LostFocus will not appear

Code: Select all

; This code is copy and paste, so i have to thank to all guys who find a fragment of their own work here

#StrGdg_User = 422 : #wMain = 400

Structure __Struct_StrGdgBalloon
  StructSize.l
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64 : _pad1.l : CompilerEndIf
  pTitle.i
  pText.i
  Icon.l
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64 : _pad2.l : CompilerEndIf
EndStructure
  
Procedure CreateGadget( GadgetTyp, GadgetNr, x, y, w, h, Text.s="", Flags=0, gTitle.s="" ) 
  Define.l gTitleID, gTitleID_Width = 30, gTitleID_Height = 20
  Define.i gadPosX
  
  ; First, create the Gadget
  Select GadgetTyp                                                                
    Case #PB_GadgetType_Button    :  ButtonGadget(   GadgetNr, x, y, w, h, Text )
    Case #PB_GadgetType_ComboBox  :  ComboBoxGadget( GadgetNr, x, y, w, h )
    Case #PB_GadgetType_String    :  StringGadget(   GadgetNr, x, y, w, h, Text, Flags )
  EndSelect
  
  ;  Produce the StringGadget and the related TextGadget in one process     
  If gTitle <> ""
    gTitleID =  1000 + GadgetNr
    TextGadget(     gTitleID, gadPosX, y + ( h - 14 ) / 2, 130, 14, gTitle, #PB_Text_Right )  
    ResizeGadget(   gTitleID, x - gTitleID_Width - 10, y+(h-14)/2, gTitleID_Width, 14)
  EndIf 
  
EndProcedure

Procedure Hide_StrGdgBalloon( GadgetID )
  SendMessage_( GadgetID, #EM_HIDEBALLOONTIP, 0, 0 )
EndProcedure

Procedure Show_StrGdgBalloon( hWnd, GadgetID, Title.s, Text.s, IconFlag = #TTI_INFO, MilliSecs = 3000 )
               Title         = "   " + Title 
               Text          = #CRLF$ + Text + #CRLF$ + " "
  Protected   TitleBuffer.l = StringByteLength( Title, #PB_Unicode ) + 2
  Protected   TextBuffer.l  = StringByteLength( Text , #PB_Unicode ) + 2
  Protected   *Buffer       = AllocateMemory(   TitleBuffer + TextBuffer )
  Protected.i timerNr       = 123 
  Protected   Balloon.__Struct_StrGdgBalloon 
  ; I use this Show_StrGdgBalloon for different Gadgets and sometimes is the GadgetNr or the GadgetID delivered so will be sure that:
  If Not GetDlgCtrlID_( GadgetID )    ; If the reverse value of GadgetID is no GadgetNr the GadgetID-Value is no GadgetID
    ; Debug IsGadget( GadgetID )         in this situation IsGadget doesnt help  
    GadgetID = GadgetID( GadgetID )
  EndIf
  
  If OSVersion() < #PB_OS_Windows_Vista And IconFlag > #TTI_ERROR : IconFlag - 3 : EndIf
   
 If *Buffer
    Balloon\StructSize = SizeOf( __Struct_StrGdgBalloon )
    PokeS( *Buffer, Title, Len( Title ), #PB_Unicode)
    PokeS( *Buffer + TitleBuffer, Text, Len( Text ), #PB_Unicode )
    Balloon\pTitle     = *Buffer
    Balloon\pText      = *Buffer + TitleBuffer
    Balloon\Icon       = IconFlag
    SendMessage_( GadgetID, #EM_SHOWBALLOONTIP, 0, @Balloon )
    FreeMemory(   *Buffer )
  EndIf
  
  AddWindowTimer( hWnd, timerNr, MilliSecs )
  Repeat
    If WaitWindowEvent() = #PB_Event_Timer And EventTimer() = timerNr
      Hide_StrGdgBalloon( GadgetID )
      RemoveWindowTimer(  hWnd, timerNr )
      Break 
    EndIf
  ForEver
      
  Hide_StrGdgBalloon( GadgetID )
    
EndProcedure
  
If OpenWindow(   #wMain, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CreateGadget(  #PB_GadgetType_String, #StrGdg_User, 70, 10, 100, 20, "", 0, "User" )
  GadgetToolTip( #StrGdg_User,   "Enter user (at least 6 characters)"     )
  ButtonGadget(  1, 70, 35, 100, 20,    "Standard Button")

  If Not IsGadget(422)
    Debug "Not return #True!"
  EndIf

  Repeat
    wwEvent = WaitWindowEvent()
    If wwEvent = #PB_Event_Gadget And EventType() = #PB_EventType_LostFocus And EventGadget() = #StrGdg_User
      If Len( GetGadgetText( #StrGdg_User ) ) < 6 
        Show_StrGdgBalloon( #wMain, #StrGdg_User, "Error...", "Must be 6 chars at minimum", #TTI_ERROR, 2000 )
      EndIf
      
    EndIf
    
  Until wwEvent = #PB_Event_CloseWindow
EndIf
I may not know much, but I try a lot
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by srod »

Well firstly, these are functions being used in your own program, so why would you need to check if a gadget# or a gadgetID has been passed? Just code for one and stick to it. There really is no need to put these checks in... unless these routines are to be made available to others via a library for example.

Secondly, if I run the following code :

Code: Select all

a$=Space(100)
GetClassName_(422, @a$, 100)
Debug a$
I get a classname of "PrintCacheLocalConnectionListenerHiddenWindow"

There are loads of hidden windows scattered about which explains why your original Not IsWindow_() fails. You just happened to stumble upon what looks like a system generated window here. I have no idea what it is or quite why it has such a bizarre hWnd? Something I have not encountered before.

Thirdly... just stick to IsGadget(). Use #PB_Any when you create your gadgets and this method will be as safe as houses.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by ts-soft »

srod wrote:... and this method will be as safe as houses.
<offtopic>
Image
</offtopic>
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by srod »

:lol:
I may look like a mule, but I'm not a complete ass.
User avatar
EfDschoma
User
User
Posts: 17
Joined: Fri Feb 21, 2014 11:46 am

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by EfDschoma »

srod wrote::lol:
I found and bought purebasic a few days ago and I am very excited, although I have to rewrite many routines that I had in other programming languages ​​already. and I am even more excited by your feedback. Thank you for your help.
I may not know much, but I try a lot
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: #Gadget or GadgetID - convert automatically to GadgetID

Post by srod »

EfDschoma wrote:
srod wrote::lol:
I found and bought purebasic a few days ago and I am very excited, although I have to rewrite many routines that I had in other programming languages ​​already. and I am even more excited by your feedback. Thank you for your help.
You made a good choice. ;)

Welcome aboard.
I may look like a mule, but I'm not a complete ass.
Post Reply