Page 1 of 2

Modul OwnGadget (PB5.2x)

Posted: Sun Jul 07, 2013 5:13 pm
by ts-soft
This Modul based of: http://www.purebasic.fr/english/viewtopic.php?p=140940

You can register your own gadget, for example API-Controls, Controls from ext. Libraries and so on.

The source uses the infos from the actual SDK.

In the moment only for windows!

Code: Select all

;======================================================================
; Module:          OwnGadget.pbi
;
; Author:          Thomas (ts-soft) Schulz
; based on Code by edel (Hallodri)
; Date:            Jul 08, 2013
; Version:         0.8
; Target Compiler: PureBasic 5.2+
; Target OS:       in the moment only windows
; License:         Free, unrestricted, no warranty whatsoever
;                  Use at your own risk
;======================================================================

; History:
; 0.7
; removed GetGadgetParent() (UseGadgetList(0) do the same)
; put RegisterGadget() to public

; 0.8
; fixed RegisterGadget

DeclareModule OwnGadget
  Structure GadgetVT
    GadgetType.l   
    SizeOf.l       
    GadgetCallback.i
    FreeGadget.i
    GetGadgetState.i
    SetGadgetState.i
    GetGadgetText.i
    SetGadgetText.i
    AddGadgetItem2.i
    AddGadgetItem3.i
    RemoveGadgetItem.i
    ClearGadgetItemList.i
    ResizeGadget.i
    CountGadgetItems.i
    GetGadgetItemState.i
    SetGadgetItemState.i
    GetGadgetItemText.i
    SetGadgetItemText.i
    OpenGadgetList2.i
    GadgetX.i
    GadgetY.i
    GadgetWidth.i
    GadgetHeight.i
    HideGadget.i
    AddGadgetColumn.i
    RemoveGadgetColumn.i
    GetGadgetAttribute.i
    SetGadgetAttribute.i
    GetGadgetItemAttribute2.i
    SetGadgetItemAttribute2.i
    SetGadgetColor.i
    GetGadgetColor.i
    SetGadgetItemColor2.i
    GetGadgetItemColor2.i
    SetGadgetItemData.i
    GetGadgetItemData.i
    GetRequiredSize.i
    SetActiveGadget.i
    GetGadgetFont.i
    SetGadgetFont.i
    SetGadgetItemImage.i
  EndStructure

  Declare RegisterGadget(ID.i, hWnd.i, *vt.GadgetVT = 0)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      Declare CreateGadget(ID.i, ClassName.s, Text.s, Style.l, x.i, y.i, w.i, h.i, ExStyle.l = 0, *vt.GadgetVT = 0)
  CompilerEndSelect
EndDeclareModule

Module OwnGadget
  EnableExplicit
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Import ""
  CompilerElse
  ImportC ""
  CompilerEndIf
    PB_Object_GetOrAllocateID(Objects.i, ID.i)
    PB_Gadget_RegisterGadget(ID.i, *Gadget, hWnd.i, *GadgetVT)
    PB_Gadget_Objects.i
  EndImport
  
  Structure Gadget
    Gadget.i
    *vt.GadgetVT
    UserData.i
    OldCallback.i
    Daten.i[4]
  EndStructure
  
  Procedure RegisterGadget(ID.i, hWnd.i, *vt.GadgetVT = 0)
    Protected *Gadget.Gadget
    Protected *vttmp.GadgetVT = AllocateMemory(SizeOf(GadgetVT))

    If Not *vttmp : ProcedureReturn #False : EndIf
          
    If Not hWnd Or ID < -1
      ProcedureReturn #False
    EndIf

    If *vt
      CopyMemory(*vt, *vttmp, SizeOf(GadgetVT))
      *vttmp\SizeOf = SizeOf(GadgetVT)
    EndIf
          
    *Gadget = PB_Object_GetOrAllocateID(PB_Gadget_Objects, ID)
    ProcedureReturn PB_Gadget_RegisterGadget(ID, *Gadget, hWnd, *vttmp)
    
  EndProcedure
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    Procedure CreateGadget(ID.i, ClassName.s, Text.s, Style.l, x.i, y.i, w.i, h.i, ExStyle.l = 0, *vt.GadgetVT = 0)
      Protected hWnd.i, Parent.i = UseGadgetList(0), hInstance.i = GetModuleHandle_(0) 
      
      hWnd  = CreateWindowEx_(ExStyle, ClassName, Text, Style, x, y, w, h, Parent, 0, hInstance, 0)
      SendMessage_(hWnd, #WM_SETFONT, GetGadgetFont(#PB_Default), 1)
      
      If Not hWnd : ProcedureReturn #False : EndIf
      
      ProcedureReturn RegisterGadget(ID, hWnd, *vt)
    EndProcedure
  CompilerEndSelect
EndModule

CompilerIf #PB_Compiler_IsMainFile
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    
      Enumeration
        #btn1
        #btn2
      EndEnumeration
      
      Procedure MyButton(Gadget, x, y, width, height, Text.s)
        ProcedureReturn OwnGadget::CreateGadget(Gadget, "Button", Text, #WS_CHILD | #WS_VISIBLE, x, y, width, height)
      EndProcedure
      
      Procedure btn1_event()
        Debug "Okay pressed"
      EndProcedure
      
      Procedure Btn3_event()
        End
      EndProcedure
      
      Define btn3
      OpenWindow(0, #PB_Ignore, #PB_Ignore, 190, 100, "example")
      MyButton(#btn1, 10, 10, 80, 25, "Okay")
      BindGadgetEvent(#btn1, @btn1_event())
      MyButton(#btn2, 100, 10, 80, 25, "")
      SetGadgetText(#btn2, "Cancel")
      btn3 = MyButton(#PB_Any, 60, 60, 80, 25, "End")
      GadgetToolTip(btn3, "Close Example!")
      BindGadgetEvent(btn3, @Btn3_event())
      
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow : Break
          Case #PB_Event_Gadget
            Select EventGadget()
              Case #btn2
                Debug "Cancel pressed"
            EndSelect
        EndSelect
      ForEver
  CompilerEndSelect
CompilerEndIf
Greetings - Thomas

Re: Modul OwnGadget (PB5.2x)

Posted: Sun Jul 07, 2013 9:27 pm
by luis
Pretty nice template :)

Thanks for sharing.

Re: Modul OwnGadget (PB5.2x)

Posted: Sun Jul 07, 2013 9:52 pm
by idle
Interesting as always, thanks.

Re: Modul OwnGadget (PB5.2x)

Posted: Mon Jul 08, 2013 9:55 am
by Kwai chang caine
Works great, thanks TsSoft 8)

Re: Modul OwnGadget (PB5.2x)

Posted: Wed Jul 24, 2013 5:29 pm
by eddy
@TSoft
Hi,
First, thanks for your GadgetVT structure. It's the most recent one that I've found.

Is it possible to define a custom DisableGadget(...) method ?

Re: Modul OwnGadget (PB5.2x)

Posted: Wed Jul 24, 2013 5:40 pm
by ts-soft
I can't see a entry in the vtTable, so i don't know!

Re: Modul OwnGadget (PB5.2x)

Posted: Sun Jul 28, 2013 8:13 am
by Bisonte
eddy wrote:@TSoft
Hi,
First, thanks for your GadgetVT structure. It's the most recent one that I've found.

Is it possible to define a custom DisableGadget(...) method ?
You can do it in the callback proc.

Like

Code: Select all

  Case #WM_ENABLE
    If wParam = #True
      ; DisableGadget(Gadget, #True) was used
    Else
      ; DisableGadget(Gadget, #False) was used
    EndIf

Re: Modul OwnGadget (PB5.2x)

Posted: Wed Jun 24, 2015 7:57 pm
by eddy
Is there SDK for linux or mac in PB 5.30 ?

Re: Modul OwnGadget (PB5.2x)

Posted: Wed Jun 24, 2015 9:06 pm
by mk-soft
No way.

Only an old Linux. For Mac no SDK for occupancy and construction of GadgetVT.
Already have all searched and inquired.

Re: Modul OwnGadget (PB5.2x)

Posted: Tue Jul 21, 2015 2:53 pm
by eddy
mk-soft wrote:No way.

Only an old Linux. For Mac no SDK for occupancy and construction of GadgetVT.
Already have all searched and inquired.
Such a pity. It would be cool to fully integrate custom gadgets into PB.
PB has now many canvas-based custom gadgets (tabbar, gauge, button, checkbox, splitter, color picker, etc...).

Re: Modul OwnGadget (PB5.2x)

Posted: Tue Jul 21, 2015 6:17 pm
by mk-soft
I had also thought so that you can use the standard functions.
http://www.purebasic.fr/english/viewtop ... 12&t=59057

Have it but not get MacOS to run.

Therefore, I now go the way to modules and interfaces
http://www.purebasic.fr/german/viewtopi ... =8&t=28903

Re: Modul OwnGadget (PB5.2x)

Posted: Tue Jul 21, 2015 6:49 pm
by Fred
I will add SDK stuff for OS X and Linux in the next release.

Re: Modul OwnGadget (PB5.2x)

Posted: Tue Jul 21, 2015 7:00 pm
by eddy
Fred wrote:I will add SDK stuff for OS X and Linux in the next release.
Great! :D
mk-soft wrote:I had also thought so that you can use the standard functions.
http://www.purebasic.fr/english/viewtop ... 12&t=59057

Have it but not get MacOS to run.

Therefore, I now go the way to modules and interfaces
http://www.purebasic.fr/german/viewtopi ... =8&t=28903
Thanks.
I'll study your code.

Re: Modul OwnGadget (PB5.2x)

Posted: Tue Jul 21, 2015 8:51 pm
by mk-soft
Very Great :D

Re: Modul OwnGadget (PB5.2x)

Posted: Thu Jul 23, 2015 7:29 pm
by eddy
Hi,
I took a quick look at PB SDK. There's no API to retrieve PB Window number of current parent window.

I tried UseGadgetList(0) but it returns PB WindowID value of current parent window.

Code: Select all

PostEvent(#PB_Event_Gadget, Window, Gadget, #PB_EventType_Change)