User Tools

Site Tools


k15:k15.5:start

15.5 Class DesktopWatcher (gb. desktop)

The class DesktopWatcher (gb.desktop) implements an object that monitors events of the window manager.

15.5.1 Properties

The DesktopWatcher class has only one property: RootWindow (Boolean) and returns True if only the RootWindow is to be monitored or sets the Root-Window as the window to be monitored with True.

15.5.2 Events

The class does not have methods - but eight events. This is understandable, because the class has mainly supervising tasks.

Event Description
ActiveWindow ()The event is triggered when the current active window has changed.
Change ()The event is triggered when the current virtual desktop has changed.
Count ()The event is triggered when the number of virtual desktops has changed.
Geometry ()The event is triggered when the size of a desktop has changed.
WindowGeometry (W As DesktopWindow)The event is triggered when the specified window has been resized or moved.
WindowIcon (W As DesktopWindow)The event is triggered when the window icon has changed.
WindowName (W As DesktopWindow)The event is triggered when the window's name or displayed name has changed.
WindowState (W As DesktopWindow)The event is triggered when the status of the specified window has changed. State can have multiple values.
Windows ()The event is triggered if the list of windows has changed because a window has been destroyed or created, or if the order in the existing list has changed.

Table 15.5.2.1: Overview of selected events of the class DesktopWatcher

15.5.3 Project

The presented project was developed in 2012 by Richard Walker as WindowExplorer. The adaptation is limited to the demonstration of the interaction of the classes DesktopWindow, Desktop → chapter 15.1 and DesktopWatcher. The source code has been extended by some functions and requires that the program 'wmctrl' be installed.

' Gambas class file
 
Private aDTWindowsList As DesktopWindow[]
Private aDTWindowsList2 As Integer[]
Public DTWatcher As DesktopWatcher
 
Public Sub _new()
  DTWatcher = New DesktopWatcher(True) As "MyDTWatcher"
  txbPatternBox.Text = "*"
End ' _new()
 
Public Sub Form_Open()
  FMain.Center
 
  GetWindowInfo()
  GetDesktopInfo()
  GetWindowsList()
End ' Form_Open()
 
Public Sub GetWindowsList()
  Dim iCount As Integer
 
  txaWindowList.Clear
  aDTWindowsList = Desktop_FindWindow(txbPatternBox.Text)
  lblWindowCount.Text = aDTWindowsList.Count
  For iCount = 0 To aDTWindowsList.Count - 1
      txaWindowList.Text &= Str(iCount + 1) & "\t" & (1 + aDTWindowsList[iCount].Desktop) 
      txaWindowList.Text &= "      " & aDTWindowsList[iCount].Id & "\t"
      txaWindowList.Text &= aDTWindowsList[iCount].Name & gb.NewLine
  Next ' iCount
  aDTWindowsList = Null
 
End ' btnGetWindowsList1_Click()
 
Public Function GetCurrentDesktopName() As String
  Dim sOutput, sZeile, sElement As String
  Dim aMatrix As String[]
 
' Output of virtual desktop names (VDesktop)
  Exec ["wmctrl", "-d"] To sOutput  
  For Each sZeile In Split(sOutput, gb.NewLine) 
      If InStr(sZeile, "*") Then ' The current desktop is indicated by a *.
         aMatrix = Split(sZeile, " ") 
         Return aMatrix[aMatrix.Max]
      Endif ' InStr(sZeile, "*") ?
  Next ' FOR EACH sZeile   
End ' Function GetDesktopName()
 
Public Sub GetDesktopInfo()
  lblDesktopCount.Text = Str(Desktop.Count)
  lblDTCurrentValue.Text = Str(Desktop.Current + 1)
  lblDesktopType.Text = Desktop.Type
  lblCurrentDesktopName.Text = GetCurrentDesktopName()
End ' GetDesktopInfo()
 
Public Sub GetActiveWindow()
  Dim DTWindow As DesktopWindow
 
  lblActiveWindowValue.Text = Str(Desktop.ActiveWindow)
  DTWindow = New DesktopWindow(Val(lblActiveWindowValue.Text))
  lblActiveWindowName.Text = DTWindow.Name  
End ' GetActiveWindow()
 
Public Sub GetWindowInfo()
  lblRootWindowID.Text = Str(Desktop.RootWindow)
  GetActiveWindow()
End ' GetWindowInfo()
 
Private Function Desktop_FindWindow(sPattern As String) As DesktopWindow[]
  Dim DTWindow As DesktopWindow
  Dim DTWList As New DesktopWindow[]
 
  For Each DTWindow In Desktop.Windows ' Desktop.Windows contains the list of all windows
      If DTWindow.Name Like sPattern Then
         DTWList.Add(DTWindow)
      Endif ' DTWindow.Name Like sPattern ?
  Next ' DTWindow 
  Return DTWList
End ' Function Desktop_FindWindow(..)
 
Public Function SetTime() As String
  Return Format(Now(), "hh:nn:ss") & "\t"  
End ' SetTime()
 
Public Sub MyDTWatcher_ActiveWindow()
  txaDTWatcherEvents.Text &= SetTime() & "* The active window has changed!" & gb.NewLine
  GetActiveWindow()
End ' MyWindowWatcher_ActiveWindow()
 
Public Sub MyDTWatcher_Windows()
  txaDTWatcherEvents.Text &= SetTime() & "~ The window list has changed." & gb.NewLine
  GetWindowsList()
End ' MyDTWatcher_Windows()
 
Public Sub btnRefresh_Click()
  GetWindowInfo()
  GetDesktopInfo()
  GetWindowsList()
End ' btnRefresh_Click()
 
Public Sub btnEnde_Click()
  FMain.Close
End ' btnEnde_Click()

B1

Figure 15.5.3.1: WindowExplorer with changed search pattern

It is remarkable how much information the program displays. Figure 15.5.3.1 and after moving the program window to another (virtual) desktop so that the current desktop can be queried and displayed again with number and name.

You could add these six procedures to WindowExplorer. The source text must be extended, since you must provide the values of the parameters of the first three procedures.

Public Sub MyDTWatcher_WindowGeometry(w As DesktopWindow)
  txaDTWatcherEvents.Text &= SetTime() & w.Name & ": VDesktop-Geometrie geändert." & gb.NewLine
End ' MyDTWatcher_WindowGeometry(w As DesktopWindow)

Public Sub MyDTWatcher_WindowIcon(w As DesktopWindow)
  txaDTWatcherEvents.Text &= SetTime() & w.Name & ": VDesktop-Icon geändert." & gb.NewLine
End ' MyDTWatcher_WindowGeometry(w As DesktopWindow)

Public Sub MyDTWatcher_WindowName(w As DesktopWindow)
  txaDTWatcherEvents.Text &= SetTime() & w.Name & ": VDesktop-Name geändert." & gb.NewLine
End ' MyDTWatcher_WindowName(w As DesktopWindow)

Public Sub MyDTWatcher_Change()
  txaDTWatcherEvents.Text &= SetTime() & "Aktueller VDesktop hat sich geändert." & gb.NewLine
End ' MyDTWatcher_Change()

Public Sub MyDTWatcher_Count()
  txaDTWatcherEvents.Text &= SetTime() & "Die VDesktop-Anzahl hat sich geändert." & gb.NewLine
End ' MyDTWatcher_Count()

Public Sub MyDTWatcher_Geometry()
  txaDTWatcherEvents.Text &= SetTime() & "Die VDesktop-Geometrie hat sich geändert." & gb.NewLine
End ' MyDTWatcher_Geometry()

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k15/k15.5/start.txt · Last modified: 28.09.2023 by emma

Page Tools