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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
{%MainUnit gtk2wsextctrls.pp}
{
gtk2trayicon.inc
*****************************************************************************
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
Gtk2 specific code.
}
{ TGtk2WSCustomTrayIcon }
type
{ TGtk2TrayIconHandle }
TGtk2TrayIconHandle = class
private
plug: PGtkWidget;
DrawingArea: PGtkWidget;
Tips: PGtkTooltips;
fEmbedded: Boolean;
fTrayIcon: TCustomTrayIcon;
{$ifdef UseStatusIcon}
FStatusIcon: PGtkStatusIcon;
{$endif}
{$ifdef HasGdk2X}
fDisplay: PDisplay;
fWindow: TWindow;
fScreen: PScreen;
fScreenID: longint;
fTrayParent: TWindow;
function SendMessage(window: TWindow; msg: Integer; data1, data2, data3: Integer): Boolean;
procedure SetEmbedded;
{$endif HasGdk2X}
public
constructor Create(const wsTrayIcon: TCustomTrayIcon);
destructor Destroy; override;
procedure Show;
function GetPosition: TPoint;
procedure Update(NewPixBuf: PGdkPixbuf; const NewHint: String);
end;
const
SYSTEM_TRAY_REQUEST_DOCK = 0;
//SYSTEM_TRAY_BEGIN_MESSAGE = 1;
//SYSTEM_TRAY_CANCEL_MESSAGE = 2;
{$ifdef HasGdk2X}
var
XError: Integer;
{*******************************************************************
* TempX11ErrorHandler ()
*
* DESCRIPTION: Temp ErrorHandler
*
* PARAMETERS: ?
*
* RETURNS: ?
*
*******************************************************************}
function TempX11ErrorHandler({%H-}Display:PDisplay; ErrorEv:PXErrorEvent):longint;cdecl;
begin
XError := ErrorEv^.error_code;
WriteLn('Error: ' + IntToStr(XError));
Result:=0;
end;
{*******************************************************************
* TGtk2TrayIconHandle.Send_Message ()
*
* DESCRIPTION: Sends a message to the X client
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
function TGtk2TrayIconHandle.SendMessage(window: TWindow; msg: Integer; data1, data2, data3: Integer): Boolean;
var
Ev: TXEvent;
begin
FillChar(Ev{%H-}, 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;
XError := 0;
XSendEvent(fDisplay, fTrayParent, False, NoEventMask, @ev);
XSync(fDisplay, False);
Result := XError = 0;
XError := 0;
end;
{*******************************************************************
* TGtk2TrayIconHandle.SetEmbedded ()
*
* DESCRIPTION: Docks the GtkPlug into the system tray
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
procedure TGtk2TrayIconHandle.SetEmbedded;
var
old_error: TXErrorHandler;
buf: array [0..32] of char;
selection_atom : TAtom;
begin
if fEmbedded then
Exit;
old_error := XSetErrorHandler(@TempX11ErrorHandler);
xsync(fdisplay,true);
buf := PChar('_NET_SYSTEM_TRAY_S' + IntToStr(fScreenID));
selection_atom := XInternAtom(fDisplay, buf, false);
XGrabServer(fDisplay);
fTrayParent := XGetSelectionOwner(fDisplay, selection_atom);
if fTrayParent <> None then
begin
XSelectInput(fDisplay, fTrayParent, StructureNotifyMask);
end;
XUngrabServer(fDisplay);
XFlush(fDisplay);
if fTrayParent <> None then
fEmbedded := SendMessage(fTrayParent, SYSTEM_TRAY_REQUEST_DOCK, fWindow, 0, 0);
XSetErrorHandler(old_error);
end;
{$endif HasGdk2X}
{*******************************************************************
* realize_cb ()
*
* DESCRIPTION: Callback function for the realize signal
* Sets the systray icon after the widget is realized
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
procedure realize_cb({%H-}widget: PGtkWidget; user_data: gpointer); cdecl;
var
wsTrayIcon: TCustomTrayIcon absolute user_data;
begin
with TGtk2TrayIconHandle(wsTrayIcon.Handle) do
begin
{$ifdef HasGdk2X}
fDisplay := GDK_WINDOW_XDISPLAY(plug^.window);
fWindow := GDK_WINDOW_XWINDOW(plug^.window);
{ Does not work
gdk_screen := gtk_widget_get_screen(plug);
fScreen := GDK_SCREEN_XSCREEN(gdk_screen); // get the real screen}
fScreen := XDefaultScreenOfDisplay(fDisplay);
fScreenID := XScreenNumberOfScreen(fScreen); // and it's number
SetEmbedded;
{$endif}
end;
end;
{*******************************************************************
* button_release_cb ()
*
* DESCRIPTION: Callback function for Mouse Click
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
function button_release_cb({%H-}widget: PGtkWidget; event: PGdkEventButton;
user_data: gpointer): gboolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := False;
case event^.button of
1:
begin
if Assigned(vwsTrayIcon.OnClick) then vwsTrayIcon.OnClick(vwsTrayIcon);
if Assigned(vwsTrayIcon.OnMouseUp) then
vwsTrayIcon.OnMouseUp(vwsTrayIcon, mbLeft, [], Round(event^.X), Round(event^.Y));
end;
2: if Assigned(vwsTrayIcon.OnMouseUp) then
vwsTrayIcon.OnMouseUp(vwsTrayIcon, mbMiddle, [], Round(event^.X), Round(event^.Y));
3:
begin
if Assigned(vwsTrayIcon.OnMouseUp) then
vwsTrayIcon.OnMouseUp(vwsTrayIcon, mbRight, [], Round(event^.X), Round(event^.Y));
if Assigned(vwsTrayIcon.PopUpMenu) then
vwsTrayIcon.PopUpMenu.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;
end;
end;
{*******************************************************************
* button_press_cb ()
*
* DESCRIPTION: Callback function for Mouse Click
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
function button_press_cb({%H-}widget: PGtkWidget; event: PGdkEventButton;
user_data: gpointer): gboolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := False;
if (event^._type = GDK_2BUTTON_PRESS) and Assigned(vwsTrayIcon.OnDblClick) then
vwsTrayIcon.OnDblClick(vwsTrayIcon)
else
begin
case event^.button of
1: if Assigned(vwsTrayIcon.OnMouseDown) then
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbLeft, [], Round(event^.X), Round(event^.Y));
2: if Assigned(vwsTrayIcon.OnMouseDown) then
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbMiddle, [], Round(event^.X), Round(event^.Y));
3: if Assigned(vwsTrayIcon.OnMouseDown) then
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbRight, [], Round(event^.X), Round(event^.Y));
end;
end;
end;
{*******************************************************************
* popup_cb ()
*
* DESCRIPTION: Callback function for the popup menu
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
function popup_cb({%H-}widget: PGtkWidget; user_data: gpointer): Boolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := True;
if Assigned(vwsTrayIcon.PopUpMenu) then
vwsTrayIcon.PopUpMenu.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;
{*******************************************************************
* motion_cb ()
*
* DESCRIPTION: Callback function for the OnMouseMove event
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
function motion_cb({%H-}widget: PGtkWidget; event: PGdkEventMotion; user_data: gpointer): Boolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := False;
if Assigned(vwsTrayIcon.OnMouseMove) then
vwsTrayIcon.OnMouseMove(vwsTrayIcon, [], Round(event^.X), Round(event^.Y));
end;
{$ifdef UseStatusIcon}
procedure activate_cb_statusicon({%H-}status_icon: PGtkStatusIcon; user_data: gpointer); cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
if Assigned(vwsTrayIcon.OnMouseDown) then
with Mouse.CursorPos do
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbLeft, [], X, Y);
end;
procedure popup_cb_statusicon({%H-}status_icon: PGtkStatusIcon; {%H-}button: guint;
{%H-}activate_time: guint; user_data: gpointer); cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
if Assigned(vwsTrayIcon.PopUpMenu) then
vwsTrayIcon.PopUpMenu.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;
function button_press_cb_statusicon({%H-}status_icon: PGtkStatusIcon;
event: PGdkEvent; user_data: gpointer): gboolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := False;
if (PGdkEventButton(event)^._type = GDK_2BUTTON_PRESS) and Assigned(vwsTrayIcon.OnDblClick) then
vwsTrayIcon.OnDblClick(vwsTrayIcon)
else
begin
case PGdkEventButton(event)^.button of
1: if Assigned(vwsTrayIcon.OnMouseDown) then
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbLeft, [], Round(PGdkEventButton(event)^.X), Round(PGdkEventButton(event)^.Y));
2: if Assigned(vwsTrayIcon.OnMouseDown) then
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbMiddle, [], Round(PGdkEventButton(event)^.X), Round(PGdkEventButton(event)^.Y));
3: if Assigned(vwsTrayIcon.OnMouseDown) then
vwsTrayIcon.OnMouseDown(vwsTrayIcon, mbRight, [], Round(PGdkEventButton(event)^.X), Round(PGdkEventButton(event)^.Y));
end;
end;
end;
function button_release_cb_statusicon({%H-}status_icon: PGtkStatusIcon;
event: PGdkEvent; user_data: gpointer): gboolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := False;
case PGdkEventButton(event)^.button of
1:
begin
if Assigned(vwsTrayIcon.OnClick) then vwsTrayIcon.OnClick(vwsTrayIcon);
if Assigned(vwsTrayIcon.OnMouseUp) then
vwsTrayIcon.OnMouseUp(vwsTrayIcon, mbLeft, [], Round(PGdkEventButton(event)^.X), Round(PGdkEventButton(event)^.Y));
end;
2: if Assigned(vwsTrayIcon.OnMouseUp) then
vwsTrayIcon.OnMouseUp(vwsTrayIcon, mbMiddle, [], Round(PGdkEventButton(event)^.X), Round(PGdkEventButton(event)^.Y));
3:
begin
if Assigned(vwsTrayIcon.OnMouseUp) then
vwsTrayIcon.OnMouseUp(vwsTrayIcon, mbRight, [], Round(PGdkEventButton(event)^.X), Round(PGdkEventButton(event)^.Y));
if Assigned(vwsTrayIcon.PopUpMenu) then
vwsTrayIcon.PopUpMenu.PopUp(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;
end;
end;
function query_tooltip_cb_statusicon({%H-}status_icon: PGtkStatusIcon;
x: guint; y: guint; {%H-}keyboard_mode: gboolean; {%H-}tooltip: gpointer{PGtkTooltip}; user_data: gpointer): gboolean; cdecl;
var
vwsTrayIcon: TCustomTrayIcon absolute user_data;
begin
Result := FALSE;
if Assigned(vwsTrayIcon.OnMouseMove) then
vwsTrayIcon.OnMouseMove(vwsTrayIcon, [], x, y);
end;
{$endif}
constructor TGtk2TrayIconHandle.Create(const wsTrayIcon: TCustomTrayIcon);
begin
fTrayIcon := wsTrayIcon;
{$ifdef UseStatusIcon}
FStatusIcon := nil;
if Available_GtkStatusIcon_2_10 then
begin
FStatusIcon := gtk_status_icon_new();
gtk_status_icon_set_from_pixbuf(FStatusIcon, {%H-}PGdkPixbuf(FTrayIcon.Icon.Handle));
{$IF FPC_FULLVERSION>30300}
gtk_status_icon_set_tooltip_text(FStatusIcon, PChar(FTrayIcon.Hint));
{$ELSE}
gtk_status_icon_set_tooltip(FStatusIcon, PChar(FTrayIcon.Hint));
{$ENDIF}
g_object_set(PGObject(FStatusIcon), 'has-tooltip', gTRUE, nil);
fEmbedded := gtk_status_icon_is_embedded(FStatusIcon);
g_signal_connect(FStatusIcon, 'activate', TGCallback(@activate_cb_statusicon), fTrayIcon);
g_signal_connect(FStatusIcon, 'popup-menu', TGCallback(@popup_cb_statusicon), fTrayIcon);
g_signal_connect(FStatusIcon, 'button-press-event', TGCallback(@button_press_cb_statusicon), fTrayIcon);
g_signal_connect(FStatusIcon, 'button-release-event', TGCallback(@button_release_cb_statusicon), fTrayIcon);
g_signal_connect(FStatusIcon, 'query-tooltip', TGCallback(@query_tooltip_cb_statusicon), fTrayIcon);
end
else
{$endif}
begin
// Creates the GtkPlug
plug := gtk_plug_new(0);
Tips := gtk_tooltips_new;
g_object_ref(Tips);
gtk_object_sink(GTK_OBJECT(Tips));
gtk_tooltips_set_tip(GTK_TOOLTIPS(Tips), plug, PChar(wsTrayIcon.Hint), '');
// Connects the signals
gtk_widget_add_events(plug, GDK_ALL_EVENTS_MASK);
g_signal_connect(plug, 'realize', TGCallback(@realize_cb), wsTrayIcon);
g_signal_connect(plug, 'popup-menu', TGCallback(@popup_cb), wsTrayIcon);
g_signal_connect(plug, 'motion-notify-event', TGCallback(@motion_cb), wsTrayIcon);
g_signal_connect(plug, 'button-press-event', TGCallback(@button_press_cb), wsTrayIcon);
g_signal_connect(plug, 'button-release-event', TGCallback(@button_release_cb), wsTrayIcon);
// Draws the icon
with wsTrayIcon do
begin
DrawingArea := gtk_image_new_from_pixbuf({%H-}PGdkPixbuf(Icon.Handle));
gtk_container_add(GTK_CONTAINER(plug), DrawingArea);
gtk_widget_show(DrawingArea);
end;
end;
end;
destructor TGtk2TrayIconHandle.Destroy;
begin
{$ifdef UseStatusIcon}
if FStatusIcon <> nil then
begin
gtk_status_icon_set_visible(FStatusIcon, False);
g_object_unref(FStatusIcon);
FStatusIcon := nil;
end
else
{$endif}
begin
gtk_widget_destroy(plug);
plug := nil;
g_object_unref(Tips);
Tips := nil;
end;
end;
procedure TGtk2TrayIconHandle.Show;
begin
{$ifdef UseStatusIcon}
if FStatusIcon <> nil then
gtk_status_icon_set_visible(FStatusIcon, True)
else
{$endif}
gtk_widget_show(plug);
end;
function TGtk2TrayIconHandle.GetPosition: TPoint;
var
{$ifdef UseStatusIcon}
AScreen: PGdkScreen;
AArea: TGdkRectangle;
AOrientation: TGtkOrientation;
{$endif}
WindowHandle: PGDKWindow;
begin
{$ifdef UseStatusIcon}
if FStatusIcon <> nil then
begin
{$T-} // PPGdkScreen = PGdkScreen; in FPC libs. @AScreen should be OK.
gtk_status_icon_get_geometry(FStatusIcon, @AScreen, @AArea, @AOrientation);
{$T+}
Result.x := AArea.x;
Result.y := AArea.y;
end
else
{$endif}
begin
if Assigned(plug) then
begin
WindowHandle := plug^.window;
if Assigned(WindowHandle) then
gdk_window_get_origin(WindowHandle, @Result.X, @Result.Y);
end;
end;
end;
procedure TGtk2TrayIconHandle.Update(NewPixBuf: PGdkPixbuf; const NewHint: String);
begin
{$ifdef UseStatusIcon}
if FStatusIcon <> nil then
begin
gtk_status_icon_set_from_pixbuf(FStatusIcon, NewPixBuf);
{$IF FPC_FULLVERSION>30300}
gtk_status_icon_set_tooltip_text(FStatusIcon, PChar(NewHint));
{$ELSE}
gtk_status_icon_set_tooltip(FStatusIcon, PChar(NewHint));
{$ENDIF}
g_object_set(PGObject(FStatusIcon), 'has-tooltip', gTRUE, nil);
end
else
{$endif}
begin
// Updates the tooltips
if Assigned(Tips) then
gtk_tooltips_set_tip(GTK_TOOLTIPS(Tips), plug, PChar(NewHint), '');
// Updates the icon
if Assigned(DrawingArea) then
gtk_image_set_from_pixbuf(GTK_IMAGE(DrawingArea), NewPixbuf);
end;
end;
{*******************************************************************
* TGtk2WSCustomTrayIcon.Hide ()
*
* DESCRIPTION: Hides the main tray icon of the program
*
* PARAMETERS: None
*
* RETURNS: True if sucessfull, otherwise False
*
*******************************************************************}
class function TGtk2WSCustomTrayIcon.Hide(const ATrayIcon: TCustomTrayIcon): Boolean;
begin
Result := False;
{ Free and nil the handle }
TGtk2TrayIconHandle(ATrayIcon.Handle).Free;
ATrayIcon.Handle := 0;
Result := True;
end;
{*******************************************************************
* TGtk2WSCustomTrayIcon.Show ()
*
* DESCRIPTION: Shows the main tray icon of the program
*
* PARAMETERS: None
*
* RETURNS: True if sucessfull, otherwise False
*
*******************************************************************}
class function TGtk2WSCustomTrayIcon.Show(const ATrayIcon: TCustomTrayIcon): Boolean;
var
TrayIconHandle: TGtk2TrayIconHandle;
begin
Result := False;
TrayIconHandle := TGtk2TrayIconHandle.Create(ATrayIcon);
ATrayIcon.Handle := HWND(TrayIconHandle);
{*******************************************************************
* Now shows the GtkPlug
*******************************************************************}
TrayIconHandle.Show;
{$ifdef UseStatusIcon}
Result := True;
{$else}
if TrayIconHandle.fEmbedded then
Result := True
else
Hide(ATrayIcon);
{$endif}
end;
{*******************************************************************
* TGtk2WSCustomTrayIcon.InternalUpdate ()
*
* DESCRIPTION: Makes modifications to the Icon while running
* i.e. without hiding it and showing again
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
class procedure TGtk2WSCustomTrayIcon.InternalUpdate(const ATrayIcon: TCustomTrayIcon);
begin
if ATrayIcon.Handle <> 0 then
TGtk2TrayIconHandle(ATrayIcon.Handle).Update({%H-}PGdkPixbuf(ATrayIcon.Icon.Handle), ATrayIcon.Hint);
end;
{*******************************************************************
* TGtk2WSCustomTrayIcon.GetPosition ()
*
* DESCRIPTION: Returns the position of the tray icon on the display.
* This function is utilized to show message boxes near
* the icon
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
class function TGtk2WSCustomTrayIcon.GetPosition(const ATrayIcon: TCustomTrayIcon): TPoint;
begin
Result := Point(0, 0);
if ATrayIcon.Handle <> 0 then
Result := TGtk2TrayIconHandle(ATrayIcon.Handle).GetPosition;
end;
|