December 04, 2020, 11:15:12 AM

Author Topic: [bmx] MaxGui Program Flow via sub hooking by Shagwana [ 1+ years ago ]  (Read 485 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : MaxGui Program Flow via sub hooking
Author : Shagwana
Posted : 1+ years ago

Description : This is a working example of how to install a master hook, that monitors events and passes desired events to registered routines (-> OnEvent()).

I post it here as it might be usefull to someone :)


Code :
Code: BlitzMax
  1. '
  2. ' Example of using one master hook, to send sorted events to
  3. ' registered gadgets.
  4. '
  5. ' Also an example of how to control program flow in a more efficent manner.
  6. '
  7.  
  8. SuperStrict
  9.  
  10.  
  11. Type TMasterEventHook
  12.  
  13.   'Master sub hook list of intrested gadgets
  14.   Global tSubHookIntrestedList:TList=Null    'Starts off empty
  15.  
  16.   Method New()
  17.     If tSubHookIntrestedList:TList=Null
  18.       Print "Sub hook list created"
  19.       tSubHookIntrestedList:TList=CreateList()
  20.       EndIf
  21.     End Method
  22.  
  23.   'Add the master hooking method in
  24.   Function InstallMasterHook()
  25.     Print "Hook installed"
  26.     AddHook EmitEventHook,TMasterEventHook.ProcessEvents
  27.     End Function
  28.  
  29.  
  30.   'All events pass to here
  31.   Function ProcessEvents:Object(iId:Int,tData:Object,tContext:Object)
  32.  
  33.     If tData=Null Then Return Null     'Nothing here to work on
  34.     Local tEv:TEvent=TEvent(tData)     'Convert into an event
  35.    
  36.     'Scan the list, see if we have a source
  37.     For Local tSH:TSubEventHook = EachIn tSubHookIntrestedList:TList
  38.       If tSH.pWidget = tEv.Source
  39.         tSH.OnEvent(tEv)    'Send the event to the proper place
  40.         Return Null
  41.         EndIf
  42.       Next
  43. '    Print tEv.Source.tostring()+" -> "+tEv.tostring()
  44.  
  45.     Return Null     'Presume we want to capture events!
  46.     End Function
  47.  
  48.  
  49.   End Type
  50.  
  51.  
  52.  
  53. Type TSubEventHook Extends TMasterEventHook
  54.  
  55.   Method New()
  56.     Print "Sub hook in"
  57.     ListAddLast TMasterEventHook.tSubHookIntrestedList,Self
  58.     End Method
  59.  
  60.   Field pWidget:TGadget=Null             'Events of this gadget needed
  61.  
  62.   Method OnEvent(tEv:TEvent) Abstract    '----> coded someplace else
  63.  
  64.   End Type
  65.  
  66.  
  67.  
  68.  
  69. '_/ code \____________________________________________________________________________________________________
  70.  
  71.  
  72.  
  73. Type TWin1Code Extends TSubEventHook  
  74.  
  75.   'Register a gadget to this code lump ....
  76.   Function RegisterGadget:TWin1Code(tG:TGadget)
  77.     Local tSH:TWin1Code = New TWin1Code
  78.     tSH.pWidget = tG
  79.     Return tSH:TWin1Code
  80.     End Function
  81.  
  82.   'Event processing for this gadget
  83.   Method OnEvent(tEv:TEvent)
  84.     Print "TWin 1 Code -> "+tEv.tostring()
  85.  
  86.     'Event handle...
  87.     Select tEv.Id
  88.       Case EVENT_WINDOWCLOSE
  89.       End    'Quit this program on window close
  90.       End Select
  91.  
  92.     End Method
  93.  
  94.   End Type
  95.  
  96.  
  97.  
  98.  
  99.  
  100. Type TBut1Code Extends TSubEventHook  
  101.  
  102.   'Register a gadget to this code lump ....
  103.   Function RegisterGadget:TBut1Code(tG:TGadget)
  104.     Local tSH:TBut1Code = New TBut1Code
  105.     tSH.pWidget = tG
  106.     Return tSH:TBut1Code
  107.     End Function
  108.  
  109.   'Event processing for this gadget
  110.   Method OnEvent(tEv:TEvent)
  111.     Print "TBut 1 Code -> "+tEv.tostring()
  112.     End Method
  113.  
  114.   End Type
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. Type TBut2Code Extends TSubEventHook  
  122.  
  123.   'Register a gadget to this code lump ....
  124.   Function RegisterGadget:TBut2Code(tG:TGadget)
  125.     Local tSH:TBut2Code = New TBut2Code
  126.     tSH.pWidget = tG
  127.     Return tSH:TBut2Code
  128.     End Function
  129.  
  130.  
  131.   'Event processing for this gadget
  132.   Method OnEvent(tEv:TEvent)
  133.     Print "TBut 2 Code -> "+tEv.tostring()
  134.     End Method
  135.  
  136.   End Type
  137.  
  138.  
  139.  
  140.  
  141.  
  142. Type TBut3Code Extends TSubEventHook  
  143.  
  144.   'Register a gadget to this code lump ....
  145.   Function RegisterGadget:TBut3Code(tG:TGadget)
  146.     Local tSH:TBut3Code = New TBut3Code
  147.     tSH.pWidget = tG
  148.     Return tSH:TBut3Code
  149.     End Function
  150.  
  151.   'Event processing for this gadget
  152.   Method OnEvent(tEv:TEvent)
  153.     Print "TBut 3 Code -> "+tEv.tostring()
  154.     End Method
  155.  
  156.   End Type
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. Type TBut4Code Extends TSubEventHook  
  164.  
  165.   'Register a gadget to this code lump ....
  166.   Function RegisterGadget:TBut4Code(tG:TGadget)
  167.     Local tSH:TBut4Code = New TBut4Code
  168.     tSH.pWidget = tG
  169.     Return tSH:TBut4Code
  170.     End Function
  171.  
  172.   'Event processing for this gadget
  173.   Method OnEvent(tEv:TEvent)
  174.     Print "TBut 4 Code -> "+tEv.tostring()
  175.     End Method
  176.  
  177.   End Type
  178.  
  179.  
  180.  
  181.  
  182. Type TBut5Code Extends TSubEventHook  
  183.  
  184.   'Register a gadget to this code lump ....
  185.   Function RegisterGadget:TBut5Code(tG:TGadget)
  186.     Local tSH:TBut5Code = New TBut5Code
  187.     tSH.pWidget = tG
  188.     Return tSH:TBut5Code
  189.     End Function
  190.  
  191.   'Event processing for this gadget
  192.   Method OnEvent(tEv:TEvent)
  193.     Print "TBut 5 Code -> "+tEv.tostring()
  194.     End Method
  195.  
  196.   End Type
  197.  
  198.  
  199.  
  200.  
  201.  
  202. 'Gui building ...
  203. Global pWin1:TGadget=CreateWindow("Supersize me!",500,100,400,400,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS)
  204. Global pBut1:TGadget=CreateButton("But1",40,40,80,26,pWin1)
  205. Global pBut2:TGadget=CreateButton("But2",40,120,80,26,pWin1)
  206. Global pPan1:TGadget=CreatePanel(180,50,200,200,pWin1,PANEL_BORDER)
  207. Global pBut3:TGadget=CreateButton("But3",40,10,80,26,pPan1)
  208. Global pPan2:TGadget=CreatePanel(20,300,240,80,pWin1,PANEL_BORDER)
  209. Global pBut4:TGadget=CreateButton("But4",40,20,80,26,pPan2)
  210. Global pBut5:TGadget=CreateButton("But5",140,40,80,26,pPan2)
  211. SetMinWindowSize pWin1,200,200
  212.  
  213.  
  214. 'Install sub event hooks for given gadgets ....
  215. Local tWin1Logic:TWin1Code = TWin1Code.RegisterGadget( pWin1 )
  216. Local tBut1Logic:TBut1Code = TBut1Code.RegisterGadget( pBut1 )
  217. Local tBut2Logic:TBut2Code = TBut2Code.RegisterGadget( pBut2 )
  218. Local tBut3Logic:TBut3Code = TBut3Code.RegisterGadget( pBut3 )
  219. Local tBut4Logic:TBut4Code = TBut4Code.RegisterGadget( pBut4 )
  220. Local tBut5Logic:TBut5Code = TBut5Code.RegisterGadget( pBut5 )
  221.  
  222.  
  223. 'Master hook routine install
  224. TMasterEventHook.InstallMasterHook()
  225.  
  226. 'Main loopage
  227. Repeat
  228.   WaitEvent
  229.   Forever
  230. End


Comments :


wedoe(Posted 1+ years ago)

 I know I will need this one soon, thanks :oD


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal