File: gtk1trayicon.inc

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (428 lines) | stat: -rw-r--r-- 12,557 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
{%MainUnit gtkwsextctrls.pp}
{
 gtk1trayicon.pas

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

 Authors: Felipe Monteiro de Carvalho and Andrew Haines

 Special thanks for: Danny Milosavljevic and the Lazarus Team

 Gtk1 specific code. Works on gnome also.
}

{ TGtkWSCustomTrayIcon }

type

  { TGtk1TrayIconHandle }

  TGtk1TrayIconHandle = class(TObject)
  private
    plug: PGtkWidget;
    drawingarea: PGtkWidget;
    fDisplay: PDisplay;
    fcanvas: TCanvas;
    fWindow: TWindow;
    fScreenID: longint;
    fTrayParent: TWindow;
    //fOwner: TComponent;
    fEmbedded: Boolean;
    //fMsgCount: Integer;
    fTrayIcon: TCustomTrayIcon;
    function GetCanvas: TCanvas;
    function NotifyExpose(Event: PGdkEventExpose; Widget: PGtkWidget): Boolean; cdecl;
    function NotifyMouseMove(Event: PGdkEventMotion; Widget: PGtkWidget): Boolean; cdecl;
    function NotifyMouseDown(Event: PGdkEventButton; Widget: PGtkWidget): Boolean; cdecl;
    function NotifyMouseUp(Event: PGdkEventButton; Widget: PGtkWidget): Boolean; cdecl;
    procedure PaintForm(Sender: TObject);
    function  Send_Message(window: TWindow; msg: Integer;data1, data2,data3: Integer): boolean;
  public
    destructor Destroy; override;
    procedure CreateForm(id: Integer);
    function  GetPosition: TPoint;
    procedure Hide;
    procedure SetEmbedded;
    procedure SetMinSize(AWidth, AHeight: Integer);
    function  TrayParent(UseCachedValue: Boolean = True): TWindow;
  public
    property Canvas: TCanvas read GetCanvas;
  end;

const
  SYSTEM_TRAY_REQUEST_DOCK   = 0;
  SYSTEM_TRAY_BEGIN_MESSAGE  = 1;
  SYSTEM_TRAY_CANCEL_MESSAGE = 2;
  
// Temp ErrorHandler
function TempX11ErrorHandler(Display:PDisplay; ErrorEv:PXErrorEvent):longint;cdecl;
begin
  WriteLn('Error: ' + IntToStr(ErrorEv^.error_code));
  Result:=0;
end;

function TGtk1TrayIconHandle.GetCanvas: TCanvas;
begin
  if Assigned(FCanvas) then Exit(FCanvas);
  
  Result := TCanvas.Create;
  Result.Handle:= GetDC(HWND(drawingarea));
  FCanvas := Result;
end;

function TGtk1TrayIconHandle.NotifyExpose(Event: PGdkEventExpose;
  Widget: PGtkWidget): Boolean; cdecl;
begin
  Result := False;

  PaintForm(fTrayIcon);
end;

function TGtk1TrayIconHandle.NotifyMouseMove(Event: PGdkEventMotion;
  Widget: PGtkWidget): Boolean; cdecl;
begin
  Result := False;

  if Assigned(fTrayIcon.OnMouseMove) then
     fTrayIcon.OnMouseMove(fTrayIcon, [], Trunc(Event^.x), Trunc(Event^.y));
end;

function TGtk1TrayIconHandle.NotifyMouseDown(Event: PGdkEventButton;
  Widget: PGtkWidget): Boolean; cdecl;
var
  Button: TMouseButton;
begin
  Result := False;

  case  Event^.button of
    GDK_RIGHTBUTTON: Button := mbRight;
    GDK_MIDDLEBUTTON: Button := mbMiddle;
    GDK_LEFTBUTTON: Button := mbLeft;
  end;
  
  if Assigned(fTrayIcon.OnMouseDown) then
    fTrayIcon.OnMouseDown(fTrayIcon, Button, [], Trunc(Event^.x), Trunc(Event^.y));
end;

function TGtk1TrayIconHandle.NotifyMouseUp(Event: PGdkEventButton;
  Widget: PGtkWidget): Boolean; cdecl;
var
  Button: TMouseButton;
begin
  Result := False;
  
  case  Event^.button of
    3: Button := mbRight;
    2: Button := mbMiddle;
    1: Button := mbLeft;
  end;

  if Button = mbLeft then
    case gdk_event_get_type(Event) of
       GDK_BUTTON_PRESS:
         if Assigned(fTrayIcon.OnClick) then
           fTrayIcon.OnClick(fTrayIcon);
       GDK_2BUTTON_PRESS:
         if Assigned(fTrayIcon.OnDblClick) then
           fTrayIcon.OnDblClick(fTrayIcon);
    end;

  { Just using GetPosition to get the screen position and then add
    Event^.x and Event^.y to it won't work. It seams that this will
    cause a small difference with Mouse.CursorPos, and using
    TPopupMenu.PopUp will result in a wrong position for the menu }
  if (Button = mbRight) and (fTrayIcon.PopUpMenu <> nil) then
    fTrayIcon.PopUpMenu.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);

  if Assigned(fTrayIcon.OnMouseUp) then
    fTrayIcon.OnMouseUp(fTrayIcon, Button, [], Trunc(Event^.x), Trunc(Event^.y));
end;

{*******************************************************************
*  TGtk1TrayIconHandle.Send_Message ()
*
*  DESCRIPTION:    Sends a message to the X client
*
*******************************************************************}
function TGtk1TrayIconHandle.Send_Message(window: TWindow; msg: Integer;data1, data2,data3: Integer): boolean;
var
  Ev: TXEvent;
//  fmt: Integer;
begin
  FillChar(Ev, SizeOf(TXEvent), $0);

  ev.xclient._type := ClientMessage;
  ev.xclient.window := window;
  ev.xclient.message_type := XInternAtom (fDisplay, '_NET_SYSTEM_TRAY_OPCODE', False );
  ev.xclient.format := 32;
  ev.xclient.data.l[0] := CurrentTime;
  ev.xclient.data.l[1] := msg;
  ev.xclient.data.l[2] := data1;
  ev.xclient.data.l[3] := data2;
  ev.xclient.data.l[4] := data3;

  XSendEvent(fDisplay, fTrayParent, False, NoEventMask, @ev);
  XSync(fDisplay, False);
  Result := false;//(untrap_errors() = 0);
end;

function TGtk1TrayIconHandle.TrayParent(UseCachedValue: Boolean = True): TWindow;
var
  buf: array[0..32] of char;
  selection_atom: TAtom;
begin
  if (fTrayParent <> 0) and UseCachedValue then
    Exit(fTrayParent);
  fDisplay := gdk_display;
  fScreenID := gdk_screen;
  //fScreenID := XScreenNumberOfScreen(fScreen); // and it's number
  XGrabServer(fDisplay);
  
  buf :=  PChar('_NET_SYSTEM_TRAY_S' + IntToStr(fScreenID));
  selection_atom := XInternAtom(fDisplay, buf, false);
  fTrayParent := XGetSelectionOwner(fDisplay, selection_atom);

  XUngrabServer(fDisplay);

  Result := fTrayParent;
end;

destructor TGtk1TrayIconHandle.Destroy;
begin
  if Assigned(FCanvas) and FCanvas.HandleAllocated then
  begin
    ReleaseDC(HWND(drawingarea), fcanvas.Handle);
    FCanvas.Free;
  end;
  if Assigned(drawingarea) then
  begin
    g_signal_handlers_destroy(G_OBJECT(drawingarea));
    gtk_widget_destroy(drawingarea);
  end;
  if Assigned(plug) then
    gtk_widget_destroy(plug);

  inherited Destroy;
end;

{*******************************************************************
*  TGtk1TrayIconHandle.SetEmbedded ()
*
*  DESCRIPTION:
*
*******************************************************************}
procedure TGtk1TrayIconHandle.SetEmbedded;
var
  old_error: TXErrorHandler;
begin
  fEmbedded := False;
  if TrayParent = None then
    Exit;

  // so we have a TWindow
  gtk_widget_realize(plug);

  old_error := XSetErrorHandler(@TempX11ErrorHandler);
  Sleep(80);
  xsync(fdisplay,true);
  
  XGrabServer(fDisplay);
  XSelectInput(fDisplay, TrayParent, StructureNotifyMask);
  XUngrabServer(fDisplay);
  XFlush(fDisplay);
  
  fWindow := GDK_WINDOW_XWINDOW (Pointer(PGtkWidget(plug)^.window));

  Send_Message(TrayParent, SYSTEM_TRAY_REQUEST_DOCK, fWindow, 0, 0);

  GTK_WIDGET_SET_FLAGS(plug,GTK_VISIBLE);
  GTK_WIDGET_SET_FLAGS(plug,GTK_MAPPED);
  
  gtk_widget_show_all(plug);

  XSetErrorHandler(old_error);
  fEmbedded:=True;
end;

procedure TGtk1TrayIconHandle.Hide;
begin
  gtk_widget_hide_all(drawingarea);
  fEmbedded := False;
end;

{*******************************************************************
*  TGtk1TrayIconHandle.CreateForm ()
*
*  DESCRIPTION:
*
*******************************************************************}
procedure TGtk1TrayIconHandle.CreateForm(id: Integer);
begin
  plug := gtk_plug_new(0);
  drawingarea := gtk_event_box_new;
  
  gtk_container_add(GTK_CONTAINER(plug), drawingarea);
  
  //gtk_widget_add_events(drawingarea, GDK_MOTION_NOTIFY);
  
  gtk_signal_connect_object_after(G_OBJECT(drawingarea), 'expose-event', TGtkSignalFunc(@TGtk1TrayIconHandle.NotifyExpose), G_OBJECT(Self));
  gtk_signal_connect_object(G_OBJECT(drawingarea), 'motion-notify-event', TGtkSignalFunc(@TGtk1TrayIconHandle.NotifyMouseMove), G_OBJECT(Self));
  gtk_signal_connect_object(G_OBJECT(drawingarea), 'button-press-event', TGtkSignalFunc(@TGtk1TrayIconHandle.NotifyMouseDown), G_OBJECT(Self));
  gtk_signal_connect_object(G_OBJECT(drawingarea), 'button-release-event', TGtkSignalFunc(@TGtk1TrayIconHandle.NotifyMouseUp), G_OBJECT(Self));
  //

  fEmbedded := False;
  GetCanvas;
end;

{*******************************************************************
*  TGtk1TrayIconHandle.GetPosition ()
*
*  DESCRIPTION:    Returns the (x, y) position of the icon on the screen
*
*******************************************************************}
function TGtk1TrayIconHandle.GetPosition: TPoint;
var
  WindowHandle: PGDKWindow;
begin
  Result := Point(0, 0);

  if not Assigned(plug) then Exit;

  WindowHandle := plug^.window;
  
  if not Assigned(WindowHandle) then Exit;
  
  gdk_window_get_origin(WindowHandle, @Result.X, @Result.Y);
end;

{*******************************************************************
*  TGtk1TrayIconHandle.SetMinSize ()
*
*  DESCRIPTION:    Attemps to avoid problems on Gnome
*
*******************************************************************}
procedure TGtk1TrayIconHandle.SetMinSize(AWidth, AHeight: Integer);
begin
  gtk_widget_set_usize(drawingarea, AWidth, AHeight);
end;

{*******************************************************************
*  TGtk1TrayIconHandle.PaintForm ()
*
*  DESCRIPTION:    Paint method of the Icon Window
*
*******************************************************************}
procedure TGtk1TrayIconHandle.PaintForm(Sender: TObject);
begin
  if fTrayIcon.ShowIcon then Canvas.Draw(0, 0, fTrayIcon.Icon);

  if Assigned(fTrayIcon.OnPaint) then fTrayIcon.OnPaint(Self);
end;

{*******************************************************************
*  TGtkWSCustomTrayIcon.Hide ()
*
*  DESCRIPTION:    Hides the main tray icon of the program
*
*  PARAMETERS:     None
*
*  RETURNS:        True if sucessfull, otherwise False
*
*******************************************************************}
class function TGtkWSCustomTrayIcon.Hide(const ATrayIcon: TCustomTrayIcon): Boolean;
var
  TrayIconHandle: TGtk1TrayIconHandle;
begin
  Result := False;

  TrayIconHandle := TGtk1TrayIconHandle(ATrayIcon.Handle);
  TrayIconHandle.Free;
  
  ATrayIcon.Handle := 0;
  
  Result := True;
end;

{*******************************************************************
*  TGtkWSCustomTrayIcon.Show ()
*
*  DESCRIPTION:    Shows the main tray icon of the program
*
*  PARAMETERS:     None
*
*  RETURNS:        True if sucessfull, otherwise False
*
*******************************************************************}
class function TGtkWSCustomTrayIcon.Show(const ATrayIcon: TCustomTrayIcon): Boolean;
var
  TrayIconHandle: TGtk1TrayIconHandle;
begin
  Result := False;

  TrayIconHandle := TGtk1TrayIconHandle.Create;
  TrayIconHandle.fTrayIcon := ATrayIcon;
  
  ATrayIcon.Handle := PtrInt(TrayIconHandle);
    
  TrayIconHandle.CreateForm(0);
  TrayIconHandle.SetMinSize(ATrayIcon.Icon.Width, ATrayIcon.Icon.Height);
  TrayIconHandle.SetEmbedded;
  
  Result := True;
end;

{*******************************************************************
*  TGtkWSCustomTrayIcon.InternalUpdate ()
*
*  DESCRIPTION:    Makes modifications to the Icon while running
*                  i.e. without hiding it and showing again
*
*******************************************************************}
class procedure TGtkWSCustomTrayIcon.InternalUpdate(const ATrayIcon: TCustomTrayIcon);
var
  TrayIconHandle: TGtk1TrayIconHandle;
begin
  TrayIconHandle := TGtk1TrayIconHandle(ATrayIcon.Handle);

  if not Assigned(TrayIconHandle) then Exit;
end;

{*******************************************************************
*  TGtkWSCustomTrayIcon.GetPosition ()
*
*  DESCRIPTION:    Returns the position of the tray icon on the display.
*                  This function is utilized to show message boxes near
*                  the icon
*
*******************************************************************}
class function TGtkWSCustomTrayIcon.GetPosition(const ATrayIcon: TCustomTrayIcon): TPoint;
var
  TrayIconHandle: TGtk1TrayIconHandle;
begin
  Result := Point(0, 0);

  TrayIconHandle := TGtk1TrayIconHandle(ATrayIcon.Handle);

  if not Assigned(TrayIconHandle) then Exit;

  Result := TrayIconHandle.GetPosition;
end;

class function TGtkWSCustomTrayIcon.GetCanvas(const ATrayIcon: TCustomTrayIcon): TCanvas;
var
  TrayIconHandle: TGtk1TrayIconHandle;
begin
  TrayIconHandle := TGtk1TrayIconHandle(ATrayIcon.Handle);

  if not Assigned(TrayIconHandle) then Exit(ATrayIcon.Icon.Canvas);
  
  Result := TrayIconHandle.Canvas;
end;