PureBasic Forum
http://forums.purebasic.com/english/

#Gadget or GadgetID - convert automatically to GadgetID
http://forums.purebasic.com/english/viewtopic.php?f=12&t=58482
Page 1 of 1

Author:  EfDschoma [ Fri Feb 21, 2014 12:05 pm ]
Post subject:  #Gadget or GadgetID - convert automatically to GadgetID

I found the following Trick to handle automatically conversion to GadgetID, but this doesn't work in all situations reliable
Code:
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:
    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

Author:  va!n [ Fri Feb 21, 2014 1:48 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

Do a check with IsGadget()

Author:  EfDschoma [ Fri Feb 21, 2014 2:33 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

va!n wrote:
Do a check with IsGadget()

Is not the same, because: I created a gadget with Nr 422 and called test -->
Code:
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

Author:  srod [ Fri Feb 21, 2014 2:54 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

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

Code:
  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:
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

Author:  EfDschoma [ Fri Feb 21, 2014 4:35 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

[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:
; 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

Author:  srod [ Fri Feb 21, 2014 4:57 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

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:
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.

Author:  ts-soft [ Fri Feb 21, 2014 5:17 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

srod wrote:
... and this method will be as safe as houses.

<offtopic>
Image
</offtopic>

Author:  srod [ Fri Feb 21, 2014 5:21 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

:lol:

Author:  EfDschoma [ Fri Feb 21, 2014 6:30 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

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.

Author:  srod [ Fri Feb 21, 2014 6:32 pm ]
Post subject:  Re: #Gadget or GadgetID - convert automatically to GadgetID

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.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/