The Desktop class of the component with the same name allows you to use desktop related routines based on the xdg-utils scripts of the Portland project. The number of properties and methods is manageable. Selected properties and methods are listed and described in a table:
Property | Type | Description |
---|---|---|
ActiveWindow | Integer | The active current top-level window is returned or set. |
Count | Integer | The number of virtual desktops is returned or set. |
Current | Integer | The number of the current desktop is returned or set. |
Passwords | _DesktopPasswords | Returns a virtual object. At present, neither properties (except key) nor methods are known. |
Path | String | Returns the path to the desktop directory |
RootWindow | Integer | Returns the X11 handle of the root window |
ScreenSaver | _Desktop_ScreenSaver | Manage the screensaver |
Showing | Boolean | With TRUE all windows are minimized and you can see the desktop |
Type | String | Returns the name of the current desktop (KDE, KDE4, GNOME, XFCE or “?” for an unknown desktop) |
Windows | _Desktop_Windows | Returns a virtual object representing a list of all top-level windows |
Table 15.1.1: Overview of properties of the Desktop class (gb.desktop)
The virtual class _Desktop_ScreenSaver allows you to manipulate the screen saver and has the following property:
_Desktop_ScreenSaver.Enabled → Returns TRUE if the screen saver is turned on.
The following is a list of the methods of the virtual class _Desktop_ScreenSaver and a short explanation of the action triggered:
For a brief description of the methods of the Desktop class, see the following table, which contains a detailed description of selected methods in other (sub)chapters:
N | Method | Description |
---|---|---|
1 | FindWindow ([ Title As String, Application As String, Role As String]) As Integer[] | Returns all X11 top-level windows in an array. |
2 | GetFileIcon (Path As String, Size As Integer[, Preview As Boolean]) As Picture | For preview equal to False an icon of type Picture is returned from the specified file. |
3 | Open (URL As String[, Wait AsBoolean]) | Opens a file or URL in the assigned standard application |
4 | OpenTerminal ([ Dir As String] | Opens a terminal in the current desktop environment. Optionally, a start directory can be specified. |
5 | RunAsRoot (Command As String) | Executes the specified command. The root password is requested in a terminal corresponding to the desktop. |
6 | SendKeys (Keys As String) | A keystroke or a combination of keys is generated. Keys is a list of keys to be generated one after the other. |
7 | SendMail (To As String[] Cc As String[], Bcc As String[], Subject As String, Body As String, Attachment As String] | An email is prepared with a recipient list, further recipient lists, the subject, the text content and an attachment. |
Table 15.1.2: Overview of methods of the class Desktop (gb. desktop)
(1)
The FindWindow(..) method returns an array of X11 window identifiers as a function value. The following applies to the optional parameters in the method:
Desktop.FindWindow ( [ Title As String, Application As String, Role As String ] ) As Integer[]
Title is the window title. This can be found in the X11 window property WM_NAME.Application is the window class. It is usually the name of the application that creates the window. It is stored in the X11 window property WM_CLASS, which is the role of the window. It is stored in the X11 window property WM_WINDOW_ROLE.
The arguments Title, Application and Role are regular expressions as defined in conjunction with the LIKE operator (→ chapter 19.6.3 Excursus LIKE and chapter 8.6 Special Operators).
[1] Public Sub GetDTWindowsList() [2] Dim iCount As Integer [3] Dim DTWindow As DesktopWindow [4] Dim aDTWindowsList As Integer[] [5] [6] txaWindowList.Clear [7] aDTWindowsList = Desktop.FindWindow(txbPatternBox.Text) [8] lblWindowCount.Text = aDTWindowsList.Count [9] For iCount = 0 To aDTWindowsList.Max [10] DTWindow = New DesktopWindow(aDTWindowsList[iCount]) [11] If DTWindow.Id = aDTWindowsList[iCount] Then [12] txaWindowList.Text &= Str(iCount + 1) & "\t" & (DTWindow.Desktop + 1) & " " & DTWindow.Id & "\t" & DTWindow.Name & gb.NewLine [13] Endif ' DTWindow.Id = aDTWindowsList[iCount] ? [14] Next ' iCount [15] [16] End ' GetDTWindowsList() [17] [18] Public Sub btnGetDTWList_Click() [19] GetDTWindowsList() [20] End ' btnGetDTWList_Click()
The following results could be displayed in a TextArea. First, the regular expression [DGM]* (→ line 7) was used as the search pattern (window name begins with D, G or M):
Nummer Desktop Desktop.ID Fenster-Name ---------------------------------------------------------------------------------------------- 1 3 52428816 DesktopWatcher 0.0.6 - Gambas 3 2 1 62914563 GHex 3 4 65011850 Mozilla Firefox 4 3 37748751 DesktopWatcher
The search pattern was then set to * and resulted in the following extended list of X11 top-level windows:
Nummer Desktop Desktop.ID Fenster-Name ---------------------------------------------------------------------------------------------- 1 0 23068676 Schreibtisch 2 3 52428816 DesktopWatcher 0.0.6 - Gambas 3 3 1 62914563 GHex 4 1 60817454 Unbenanntes Dokument 1 - gedit 5 2 48234565 15.1.odt - LibreOffice Writer 6 4 65011850 Mozilla Firefox 7 3 37748751 DesktopWatcher 8 0 14680084 Unteres Kanten-Panel, ausgedehnt 9 0 14680067 Oberes Kanten-Panel, ausgedehnt
(2)
The GetFileIcon(..)method returns the icon associated with the selected file:
' Gambas class file Public picFileIcon As Picture Public Sub Form_Open() picFileIcon = Desktop.GetFileIcon(User.Home &/ "DesktopWatcher.gambas", 32, False) ' Saving the icon in the global variables picFileIcon of type Picture PictureBox1.W = 72 PictureBox1.H = PictureBox1.W PictureBox1.Stretch = True PictureBox1.Picture = Desktop.GetFileIcon(User.Home &/ "Arbeitsfläche/GetFileIcon/GetFileIcon.gambas", 32, False) End ' Form_Open
(5)
With the RunAsRoot(…) method, many people hoped to be able to execute a command that required root privileges quite simply from within a Gambas program. Implementation in a project is simple:
Public Sub RunAsRoot(sCommand As String) Desktop.RunAsRoot(sCommand) End ' RunAsRoot(sCommand As String) Public Sub btnRunAsRoot_Click() RunAsRoot("apt-get install bluefish") End ' btnRunAsRoot_Click()
A terminal window opens and you are prompted to enter the root password:
Figure 15.1.1: Terminal
But currently (→ 30.11.2014) nothing happens after entering the root password. The terminal window closes and you return to the program window.
What it already is - at least under Ubuntu 12.04.
Internally, the instruction Exec() is used and the following command - here in connection with the command to install the program 'bluefish' - is used:
hans@linux:~$ /tmp/gambas.1000/10857/xdg-utils/xdg-su -c "apt-get install bluefish"
For the virtual class _Desktop_ScreenSaver as well as for the class Desktop with its methods, descriptions and the presentation of relevant source code excerpts are given in separate chapters:
Virtual class _Desktop_ScreenSaver to manipulate the screen saver Desktop.
Articles