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 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
|
{%MainUnit gtk2int.pas}
{ $Id$ }
{******************************************************************************
All GTK2 interface communication implementations.
Initial Revision : Sat Jan 17 19:00:00 2004
!! Keep alphabetical !!
Support routines go to gtk2proc.pp
******************************************************************************
Implementation
******************************************************************************
*****************************************************************************
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.
*****************************************************************************
}
//##apiwiz##sps## // Do not remove
function TGtk2WidgetSet.CreateRubberBand(const ARect: TRect; const ABrush: HBrush): HWND;
var
Widget: PGtkWidget absolute Result;
dx, dy: integer;
Pixmap: PGdkPixmap;
gc: PGdkGC;
AColor: TGdkColor;
begin
dx := ARect.Right - ARect.Left;
dy := ARect.Bottom - ARect.Top;
if dx < 0 then
dx := 0;
if dy < 0 then
dy := 0;
// rubber band is just a window without a title
Result := {%H-}HWND(gtk_window_new(GTK_WINDOW_POPUP));
gtk_window_set_default_size({%H-}PGtkWindow(Result), dx, dy);
gtk_widget_set_uposition(Widget, ARect.Left, ARect.Top);
gtk_widget_set_app_paintable(Widget, True);
gtk_widget_realize(Widget);
gdk_window_set_decorations(Widget^.window, 0);
gdk_window_set_functions(Widget^.window, GDK_FUNC_RESIZE or GDK_FUNC_CLOSE);
gtk_window_set_opacity({%H-}PGtkWindow(Result), 0.25);
if ABrush = 0 then
SetWidgetColor(Widget, clNone, clGradientActiveCaption, [GTK_STATE_NORMAL])
else
if {%H-}PGDIObject(ABrush)^.GDIBrushFill = GDK_SOLID then
SetWidgetColor(Widget, clNone, {%H-}PGDIObject(ABrush)^.GDIBrushColor.ColorRef, [GTK_STATE_NORMAL])
else
begin
Pixmap := gdk_pixmap_new(Widget^.window, dx, dy, -1);
gc := gdk_gc_new(Pixmap);
AColor := AllocGDKColor(clWhite);
gdk_gc_set_foreground(gc, @AColor);
gdk_gc_set_fill(gc, {%H-}PGDIObject(ABrush)^.GDIBrushFill);
case {%H-}PGDIObject(ABrush)^.GDIBrushFill of
GDK_TILED: gdk_gc_set_tile(gc, {%H-}PGDIObject(ABrush)^.GDIBrushPixMap);
GDK_STIPPLED: gdk_gc_set_stipple(gc, {%H-}PGDIObject(ABrush)^.GDIBrushPixMap);
end;
gdk_draw_rectangle(Pixmap, gc, -1, 0, 0, dx, dy);
gdk_gc_unref(gc);
gdk_window_set_back_pixmap(Widget^.window, Pixmap, False);
g_object_unref(Pixmap);
end;
gtk_widget_show(Widget);
end;
procedure TGtk2WidgetSet.DestroyRubberBand(ARubberBand: HWND);
begin
gtk_widget_destroy({%H-}PGtkWidget(ARubberBand));
end;
procedure TGtk2WidgetSet.DrawDefaultDockImage(AOldRect, ANewRect: TRect;
AOperation: TDockImageOperation);
const
LineWidth = 2;
var
Mask: PGdkBitmap;
gc: PGdkGC;
dx, dy: integer;
AColor: TGdkColor;
{$ifdef GTK_2_10}
Colormap: PGdkColormap;
Screen: PGdkScreen;
{$endif}
begin
dx := ANewRect.Right - ANewRect.Left;
dy := ANewRect.Bottom - ANewRect.Top;
if dx < 0 then
dx := 0;
if dy < 0 then
dy := 0;
if FDockImage = nil then
begin
// dock image is just a window without title
FDockImage := gtk_window_new(GTK_WINDOW_POPUP);
gtk_window_set_default_size(PGtkWindow(FDockImage),
dx, dy);
gtk_widget_realize(FDockImage);
gdk_window_set_decorations(FDockImage^.window, 0);
gdk_window_set_functions(FDockImage^.window, GDK_FUNC_RESIZE or GDK_FUNC_CLOSE);
SetWidgetColor(FDockImage, clNone, clGradientActiveCaption, [GTK_STATE_NORMAL]);
{$ifdef GTK_2_10}
// attemp to make window semi-transparent
Screen := gtk_widget_get_screen(FDockImage);
Colormap := gdk_screen_get_rgba_colormap(Screen);
if (Colormap <> nil) and gdk_screen_is_composited(Screen) then
gtk_widget_set_colormap(FDockImage, Colormap);
{$endif}
end;
gdk_window_move_resize(FDockImage^.window, ANewRect.Left, ANewRect.Top,
dx, dy);
if (dx > 0) and (dy > 0) then
begin
// create a hole inside window
Mask := gdk_pixmap_new(nil, dx, dy, 1);
gc := gdk_gc_new(Mask);
AColor.pixel := 1;
gdk_gc_set_foreground(gc, @AColor);
gdk_draw_rectangle(Mask, gc, 1, 0, 0, dx, dy);
AColor.pixel := 0;
gdk_gc_set_foreground(gc, @AColor);
gdk_draw_rectangle(Mask, gc, 1, LineWidth, LineWidth,
dx - LineWidth * 2, dy - LineWidth * 2);
gdk_gc_unref(gc);
gtk_widget_shape_combine_mask(FDockImage, Mask, 0, 0);
gdk_pixmap_unref(Mask);
end;
case AOperation of
disShow: gtk_widget_show(FDockImage);
disHide: gtk_widget_hide(FDockImage);
end;
end;
procedure TGtk2WidgetSet.DrawGrid(DC: HDC; const R: TRect; DX, DY: Integer);
var
X, Y: Integer;
W, H: Integer;
SavedDC: Integer;
begin
SavedDC := SaveDC(DC);
try
W := (R.Right - R.Left - 1) div DX;
H := (R.Bottom - R.Top - 1) div DY;
// remove rows from clip rect
for Y := 0 to H do
begin
ExcludeClipRect(DC, R.Left, R.Top + Y * DY + 1, R.Right + 1, R.Top + (Y + 1) * DY);
end;
// draw vertical lines cross excluded rows -> only grid cross points painted
for X := 0 to W do
begin
if MoveToEx(DC, R.Left + X * DX, R.Top, nil) then
LineTo(DC, R.Left + X * DX, R.Bottom + 1);
end;
finally
RestoreDC(DC, SavedDC);
end;
end;
{------------------------------------------------------------------------------
function TGtk2WidgetSet.ExtUTF8Out(DC: HDC; X, Y: Integer; Options: Longint;
Rect: PRect; Str: PChar; Count: Longint; Dx: PInteger): Boolean;
As ExtTextOut except that Str is treated as UTF8
------------------------------------------------------------------------------}
function TGtk2WidgetSet.ExtUTF8Out(DC: HDC; X, Y: Integer; Options: Longint;
Rect: PRect; Str: PChar; Count: Longint; Dx: PInteger): Boolean;
begin
// all fonts are UTF-8 under gtk2 => no mapping needed
Result:=ExtTextOut(DC,X,Y,Options,Rect,Str,Count,Dx);
end;
function TGtk2WidgetSet.TextUTF8Out(DC: HDC; X, Y: Integer; Str: PChar; Count: Longint): Boolean;
begin
// all fonts are UTF-8 under gtk2 => no mapping needed
Result := TextOut(DC, X, Y, Str, Count);
end;
{------------------------------------------------------------------------------
function TGtk2WidgetSet.FontIsMonoSpace(Font: HFont): boolean;
True if font characters have all the same width.
------------------------------------------------------------------------------}
function TGtk2WidgetSet.FontIsMonoSpace(Font: HFont): boolean;
begin
Result:=IsValidGDIObject(Font)
and FontIsMonoSpaceFont({%H-}PGdiObject(Font)^.GDIFontObject);
end;
{------------------------------------------------------------------------------
Function: GetAcceleratorString
Params: AVKey:
AShiftState:
Returns:
------------------------------------------------------------------------------}
function TGtk2WidgetSet.GetAcceleratorString(const AVKey: Byte;
const AShiftState: TShiftState): String;
begin
Result:=inherited GetAcceleratorString(AVKey,AShiftState);
end;
{------------------------------------------------------------------------------
Function: RawImage_CreateBitmap
Params: ARawImage:
ABitmap:
AMask:
ASkipMask: When set, no mask is created
Returns:
------------------------------------------------------------------------------}
function TGtk2WidgetSet.RawImage_CreateBitmaps(const ARawImage: TRawImage; out
ABitmap, AMask: HBitmap; ASkipMask: boolean): boolean;
var
GdiObject: PGDIObject absolute ABitmap;
GdiMaskObject: PGDIObject absolute AMask;
Desc: TRawImageDescription absolute ARawImage.Description;
ImgData: Pointer absolute ARawImage.Data;
ImgMask: Pointer absolute ARawImage.Mask;
ImgWidth: Cardinal absolute ARawImage.Description.Width;
ImgHeight: Cardinal absolute ARawImage.Description.Height;
ImgDepth: Byte absolute ARawImage.Description.Depth;
ImgDataSize: PtrUInt absolute ARawImage.DataSize;
Drawable: PGdkDrawable;
Pixbuf, TmpPixBuf: PGdkPixbuf;
GC: PGdkGC;
Visual: PGdkVisual;
GdkImage: PGdkImage;
RowStride: Cardinal;
Ridx, Gidx, Bidx, Aidx: Byte;
Data: Pointer;
Src, Dst, SrcRowPtr, DstRowPtr: PByte;
x, y: Cardinal;
CreateWithAlpha: boolean;
ADivResult, ARemainder: DWord;
begin
Result := False;
ABitmap := 0;
AMask := 0;
if ImgWidth = 0 then Exit;
if ImgHeight = 0 then Exit;
CreateWithAlpha := True;
try
{$IFDEF VerboseRawImage}
DebugLn('TGtk2WidgetSet.CreateBitmapFromRawImage A ',
' ASkipMask='+dbgs(ASkipMask),
' Depth='+dbgs(Desc.Depth),
' Width='+dbgs(Desc.Width),
' Height='+dbgs(Desc.Height),
' Data='+DbgS(ARawImage.Data),
' DataSize='+dbgs(ARawImage.DataSize)+
' Mask='+DbgS(ARawImage.Mask)+
' MaskSize='+dbgs(ARawImage.MaskSize)+
' Palette='+DbgS(ARawImage.Palette)+
' PaletteSize='+dbgs(ARawImage.PaletteSize)+
' BitsPerPixel='+dbgs(Desc.BitsPerPixel)+
'');
{$ENDIF}
// ToDo: check description
GdiObject := NewGDIObject(gdiBitmap);
GdiObject^.GDIBitmapType := gbPixmap;
GdiObject^.Depth := ImgDepth;
// create Pixmap from data
if ImgDepth = 1 then
begin
// create a GdkBitmap
if ImgData <> nil then
begin
Drawable := gdk_bitmap_create_from_data(nil, ImgData, ImgWidth, ImgHeight);
//gtk2 crashes if we create mask on gdkbitmap.issue #21673
ASkipMask := True;
end else
Drawable := gdk_pixmap_new(nil, ImgWidth, ImgHeight, 1);
GdiObject^.GDIBitmapObject := Drawable;
GdiObject^.GDIBitmapType := gbBitmap;
end else
begin
if (ImgData <> nil) and (ImgDepth = 32)
then begin
case Desc.LineEnd of
rileQWordBoundary: begin
RowStride := ImgWidth;
if ImgWidth and 1 <> 0 then Inc(RowStride);
RowStride := RowStride shl 2;
end;
rileDQWordBoundary: begin
RowStride := ImgWidth shr 1;
if ImgWidth and 3 <> 0 then Inc(RowStride);
RowStride := RowStride shl 3;
end;
else
RowStride := ImgWidth shl 2;
end;
// check if the pixels are in order, pixbuf expects them in R-G-B-A
Desc.GetRGBIndices(Ridx, Gidx, Bidx, AIdx);
if (Ridx <> 0) or (Gidx <> 1) or (Bidx <> 2) or (AIdx <> 3) then
begin
// put components in right order
GetMem(Data, ImgDataSize);
DstRowPtr := Data;
SrcRowPtr := ImgData;
y := ImgHeight;
while y > 0 do
begin
Src := SrcRowPtr;
Dst := DstRowPtr;
x := ImgWidth;
while x > 0 do
begin
Dst[0] := Src[Ridx];
Dst[1] := Src[Gidx];
Dst[2] := Src[Bidx];
Dst[3] := Src[Aidx];
Inc(Src, 4);
Inc(Dst, 4);
Dec(x);
end;
Inc(SrcRowPtr, Rowstride);
Inc(DstRowPtr, Rowstride);
Dec(y);
end;
end else
begin
// components are in place
// gtkPixbuf doesn't like invalid dataSize/MaskSize < 32. issue #8553.
if (ARawImage.MaskSize > 0) and (ImgDepth = 32) then
begin
// seem that gdkPixbuf does not like many of our masks
ADivResult := 0;
ARemainder := 0;
DivMod(ARawImage.DataSize, ARawImage.MaskSize, ADivResult, ARemainder);
CreateWithAlpha := (ARemainder = 0) and ARawImage.IsMasked(True);
{$IFDEF VerboseRawImage}
if not CreateWithAlpha then
DebugLn('TGtk2WidgetSet.CreateBitmapFromRawImage B WARNING: This image have invalid DataSize / MaskSize.');
{$ENDIF}
end;
Data := ImgData;
end;
TmpPixBuf := gdk_pixbuf_new_from_data(Data, GDK_COLORSPACE_RGB, CreateWithAlpha,
8, ImgWidth, ImgHeight, RowStride, nil, nil);
// we need to copy our pixbuf into a new one to allow data deallocation
Pixbuf := gdk_pixbuf_copy(TmpPixBuf);
gdk_pixbuf_unref(TmpPixBuf);
GdiObject^.GDIBitmapType := gbPixbuf;
GdiObject^.GDIPixbufObject := Pixbuf;
if Data <> ImgData
then FreeMem(Data);
GdiObject^.visual := gdk_visual_get_system();
gdk_visual_ref(GdiObject^.visual);
//DbgDumpPixbuf(Pixbuf, 'CreateBitmaps (32)');
end
else begin
// check if the depth is supported
Visual := gdk_visual_get_best_with_depth(Min(ImgDepth, 24));
// try some alternative (I'm not sure if we should fail here instead)
// if we don't have a visual we cannot draw anyway
//if Visual = nil
//then Visual := gdk_visual_get_best;
if Visual = nil
then Exit; // this depth is not supported
Drawable := gdk_pixmap_new(nil, ImgWidth, ImgHeight, Visual^.depth);
// create a GdkPixmap
if ImgData <> nil
then begin
{ The gdk_pixmap_create_from_data creates only a two-color pixmap so we can not use it }
GdkImage := gdk_image_new(GDK_IMAGE_FASTEST, Visual, ImgWidth, ImgHeight);
{$ifdef VerboseRawImage}
//DebugLn('TGtk2WidgetSet.CreateBitmapFromRawImage GdkImage: ',
// ' BytesPerLine=',dbgs(GdkImage^.bpl),
// ' BitsPerPixel=',dbgs(GetPGdkImageBitsPerPixel(GdkImage)),
// ' ByteOrder=',dbgs(ord(GdkImage^.byte_order)),
// '');
{$endif}
if ARawImage.Description.BitsPerPixel <> GetGdkImageBitsPerPixel(GdkImage)
then begin
DebugLn('TGtk2WidgetSet.CreateBitmapFromRawImage GdkImage: ',
' BytesPerLine=',dbgs(GdkImage^.bpl),
' BitsPerPixel=',dbgs(GetGdkImageBitsPerPixel(GdkImage)),
' ByteOrder=',dbgs(ord(GdkImage^.byte_order)),
' Visual^.depth=',dbgs(Visual^.depth),
' ImgDepth=',dbgs(ImgDepth),
' ARawImage.Description.BitsPerPixel=',dbgs(ARawImage.Description.BitsPerPixel),
'');
RaiseGDBException('TGtk2WidgetSet.CreateBitmapFromRawImage Incompatible BitsPerPixel');
end;
if ImgDataSize <> GdkImage^.bpl * ImgHeight
then begin
RaiseGDBException('TGtk2WidgetSet.CreateBitmapFromRawImage Incompatible DataSize');
end;
System.Move(ImgData^, GdkImage^.mem^, ImgDataSize);
if ImgDepth = 1
then CheckGdkImageBitOrder(GdkImage, GdkImage^.mem, ImgDataSize);
GC := gdk_gc_new(Drawable);
gdk_draw_image(Drawable, GC, GdkImage, 0, 0, 0, 0, ImgWidth, ImgHeight);
gdk_gc_unref(GC);
gdk_image_destroy(GdkImage);
//DbgDumpPixmap(Drawable, 'CreateBitmaps');
end;
GdiObject^.GDIPixmapObject.Image := Drawable;
GdiObject^.Visual := gdk_window_get_visual(Drawable);
gdk_visual_ref(GdiObject^.Visual);
end;
end;
if ASkipMask
then begin
Result := True;
Exit;
end;
// create mask
{$IFDEF VerboseRawImage}
DebugLn('TGtk2WidgetSet.CreateBitmapFromRawImage creating mask .. ');
{$ENDIF}
if ARawImage.IsMasked(False)
then Drawable := gdk_bitmap_create_from_data(nil, ImgMask, ImgWidth, ImgHeight)
else begin
Drawable := gdk_pixmap_new(nil, ImgWidth, ImgHeight, 1);
// clear drawable, the contents of a new pixmap are indefined
GC := gdk_gc_new(Drawable);
gdk_draw_rectangle(Drawable, GC, 1, 0, 0, ImgWidth, ImgHeight);
gdk_gc_unref(GC);
end;
GdiMaskObject := NewGDIObject(gdiBitmap);
GdiMaskObject^.Depth := 1;
GdiMaskObject^.GDIBitmapType := gbBitmap;
GdiMaskObject^.GDIBitmapObject := Drawable;
//DbgDumpBitmap(Drawable, 'CreateBitmaps - Mask');
Result := True;
except
DeleteObject(ABitmap);
ABitmap := 0;
DeleteObject(AMask);
AMask := 0;
end;
end;
{------------------------------------------------------------------------------
Function: RawImage_DescriptionFromBitmap
Params: Bitmap: HBITMAP;
Desc: PRawImageDescription
Returns: boolean;
------------------------------------------------------------------------------}
function TGtk2WidgetSet.RawImage_DescriptionFromBitmap(ABitmap: HBITMAP; out ADesc: TRawImageDescription): boolean;
var
GDIObject: PGDIObject absolute ABitmap;
begin
Result := False;
if not IsValidGDIObject(ABitmap)
then begin
DebugLn('WARNING: [TGtk2WidgetSet.GetBitmapRawImageDescription] invalid Bitmap!');
exit;
end;
case GDIObject^.GDIBitmapType of
gbBitmap:
Result := RawImage_DescriptionFromDrawable(ADesc,
GdiObject^.GDIBitmapObject, False);
gbPixmap:
Result := RawImage_DescriptionFromDrawable(ADesc,
GdiObject^.GDIPixmapObject.Image, GdiObject^.GDIPixmapObject.Mask <> nil);
gbPixbuf:
Result := RawImage_DescriptionFromPixbuf(ADesc, GdiObject^.GDIPixbufObject);
else
DebugLn('WARNING: [TGtk2WidgetSet.RawImage_DescriptionFromBitmap] Unknown GDIBitmapType');
Exit;
end;
end;
{------------------------------------------------------------------------------
function RawImage_DescriptionFromDevice
Params: DC: HDC;
Desc: PRawImageDescription
Returns: boolean;
Retrieves the information about the structure of the supported image data.
------------------------------------------------------------------------------}
function TGtk2WidgetSet.RawImage_DescriptionFromDevice(ADC: HDC; out
ADesc: TRawImageDescription): Boolean;
var
DevCon: TGtkDeviceContext absolute ADC;
Drawable: PGdkDrawable;
UseAlpha: Boolean;
begin
UseAlpha := False;
if IsValidDC(ADC)
then begin
Drawable := DevCon.Drawable;
if DevCon.CurrentBitmap <> nil
then begin
case DevCon.CurrentBitmap^.GDIBitmapType of
gbBitmap: Drawable := DevCon.CurrentBitmap^.GDIBitmapObject;
gbPixmap: begin
Drawable := DevCon.CurrentBitmap^.GDIPixmapObject.Image;
UseAlpha := DevCon.CurrentBitmap^.GDIPixmapObject.Mask <> nil;
end;
gbPixbuf: begin
Result := RawImage_DescriptionFromPixbuf(ADesc, DevCon.CurrentBitmap^.GDIPixbufObject);
Exit;
end;
end;
end;
end
else
Drawable := nil;
Result := RawImage_DescriptionFromDrawable(ADesc, Drawable, UseAlpha);
end;
{------------------------------------------------------------------------------
Function: RawImage_QueryDescription
Params: AFlags:
ADesc:
Returns:
------------------------------------------------------------------------------}
function TGtk2WidgetSet.RawImage_QueryDescription(AFlags: TRawImageQueryFlags; var ADesc: TRawImageDescription): Boolean;
var
Desc: TRawImageDescription;
begin
Desc.Init;
Result := RawImage_DescriptionFromDrawable(Desc, nil, riqfAlpha in AFlags);
if not Result then Exit;
if not (riqfUpdate in AFlags) then
ADesc.Init;
// if there's mask gtk2 assumes it's rgba (not XBM format).issue #12362
if (riqfUpdate in AFlags) and (riqfMono in AFlags) and (riqfMask in AFlags) then
AFlags := AFlags - [riqfMono] + [riqfRgb];
if riqfMono in AFlags then
begin
ADesc.Format := ricfGray;
ADesc.Depth := 1;
ADesc.BitOrder := Desc.MaskBitOrder;
ADesc.ByteOrder := riboLSBFirst;
ADesc.LineOrder := Desc.LineOrder;
ADesc.LineEnd := Desc.MaskLineEnd;
ADesc.BitsPerPixel := Desc.MaskBitsPerPixel;
ADesc.RedPrec := 1;
ADesc.RedShift := Desc.MaskShift;
// in theory only redshift is used, but if someone reads it as color thsi works too.
ADesc.GreenPrec := 1;
ADesc.GreenShift := Desc.MaskShift;
ADesc.BluePrec := 1;
ADesc.BlueShift := Desc.MaskShift;
end
else if riqfGrey in AFlags
then begin
ADesc.Format := ricfGray;
ADesc.Depth := 8;
ADesc.BitOrder := Desc.BitOrder;
ADesc.ByteOrder := Desc.ByteOrder;
ADesc.LineOrder := Desc.LineOrder;
ADesc.LineEnd := Desc.LineEnd;
ADesc.BitsPerPixel := 8;
ADesc.RedPrec := 8;
ADesc.RedShift := 0;
end
else
if riqfRGB in AFlags then
begin
ADesc.Format := ricfRGBA;
ADesc.Depth := Desc.Depth;
ADesc.BitOrder := Desc.BitOrder;
ADesc.ByteOrder := Desc.ByteOrder;
ADesc.LineOrder := Desc.LineOrder;
ADesc.LineEnd := Desc.LineEnd;
ADesc.BitsPerPixel := Desc.BitsPerPixel;
ADesc.RedPrec := Desc.RedPrec;
ADesc.RedShift := Desc.RedShift;
ADesc.GreenPrec := Desc.GreenPrec;
ADesc.GreenShift := Desc.GreenShift;
ADesc.BluePrec := Desc.BluePrec;
ADesc.BlueShift := Desc.BlueShift;
end;
if riqfAlpha in AFlags then
begin
ADesc.AlphaPrec := Desc.AlphaPrec;
ADesc.AlphaShift := Desc.AlphaShift;
end;
if riqfMask in AFlags then
begin
ADesc.MaskBitsPerPixel := Desc.MaskBitsPerPixel;
ADesc.MaskShift := Desc.MaskShift;
ADesc.MaskLineEnd := Desc.MaskLineEnd;
ADesc.MaskBitOrder := Desc.MaskBitOrder;
end;
(*
//TODO
if riqfPalette in AFlags
then begin
ADesc.PaletteColorCount := Desc.PaletteColorCount;
ADesc.PaletteBitsPerIndex := Desc.PaletteBitsPerIndex;
ADesc.PaletteShift := Desc.PaletteShift;
ADesc.PaletteLineEnd := Desc.PaletteLineEnd;
ADesc.PaletteBitOrder := Desc.PaletteBitOrder;
ADesc.PaletteByteOrder := Desc.PaletteByteOrder;
end;
*)
end;
{------------------------------------------------------------------------------
function TGtk2WidgetSet.GetRawImageFromBitmap(SrcBitmap, SrcMaskBitmap: HBITMAP;
const SrcRect: TRect; var NewRawImage: TRawImage): boolean; override;
------------------------------------------------------------------------------}
function TGtk2WidgetSet.RawImage_FromBitmap(out ARawImage: TRawImage; ABitmap, AMask: HBITMAP; ARect: PRect): Boolean;
var
GdiBitmap: PGDIObject absolute ABitmap;
GdiMask: PGDIObject absolute AMask;
Drawable: PGdkDrawable;
Bitmap: PGdkBitmap;
begin
Result := false;
{$IFDEF VerboseRawImage}
DebugLn('TGtk2WidgetSet.GetRawImageFromBitmap A');
{$ENDIF}
ARawImage.Init;
if not IsValidGDIObject(ABitmap)
then begin
DebugLn('WARNING: [TGtk2WidgetSet.RawImage_FromBitmap] invalid Bitmap!');
exit;
end;
if (AMask <> 0) and not IsValidGDIObject(AMask)
then begin
DebugLn('WARNING: [TGtk2WidgetSet.RawImage_FromBitmap] invalid Mask');
exit;
end;
try
// get rawimage for Bitmap
case GdiBitmap^.GDIBitmapType of
gbBitmap: begin
Drawable := GdiBitmap^.GDIBitmapObject;
Bitmap := nil;
end;
gbPixmap: begin
Drawable := GdiBitmap^.GDIPixmapObject.Image;
Bitmap := GdiBitmap^.GDIPixmapObject.Mask;
end;
gbPixbuf: begin
Result := RawImage_FromPixbuf(ARawImage, GdiBitmap^.GDIPixbufObject, ARect);
Exit;
end;
else
DebugLn('WARNING: [TGtk2WidgetSet.RawImage_FromBitmap] Unknown GDIBitmapType');
Exit;
end;
{$IFDEF VerboseRawImage}
DebugLn('TGtk2WidgetSet.RawImage_FromBitmap A GdkPixmap=',DbgS(Drawable),' SrcMaskBitmap=',DbgS(Bitmap));
{$ENDIF}
//DbgDumpPixmap(Drawable, 'RawImage_FromBitmap - drawable');
//DbgDumpBitmap(Bitmap, 'RawImage_FromBitmap - alpha');
Result := RawImage_FromDrawable(ARawImage, Drawable, Bitmap, ARect);
if Result and (AMask <> 0)
then begin
if GdiMask^.GDIBitmapType <> gbBitmap
then begin
DebugLn('WARNING: [TGtk2WidgetSet.RawImage_FromBitmap] Unsupported GDIBitmapType for mask');
Exit;
end;
Bitmap := GdiMask^.GDIBitmapObject;
RawImage_AddMask(ARawImage, Bitmap, ARect);
//DbgDumpBitmap(Bitmap, 'RawImage_FromBitmap - mask');
end
else
ARawImage.Description.MaskBitsPerPixel := 0;
if not Result
then DebugLn('WARNING: [TGtk2WidgetSet.RawImage_FromBitmap] unable to GetRawImageFromGdkWindow Image');
except
ARawImage.FreeData;
end;
end;
{------------------------------------------------------------------------------
function TGtk2WidgetSet.GetRawImageFromDevice(SrcDC: HDC; const SrcRect: TRect;
var NewRawImage: TRawImage): boolean;
------------------------------------------------------------------------------}
function TGtk2WidgetSet.RawImage_FromDevice(out ARawImage: TRawImage; ADC: HDC;
const ARect: TRect): Boolean;
var
DevCtx: TGtkDeviceContext absolute ADC;
DCOrigin: TPoint;
R: TRect;
Drawable: PGdkDrawable;
begin
Result := False;
if not IsValidDC(ADC)
then begin
DebugLn('WARNING: TGtk2WidgetSet.GetRawImageFromDevice invalid SrcDC');
Exit(False);
end;
DCOrigin := DevCtx.Offset;
{$IFDEF VerboseRawImage}
DebugLn('TGtk2WidgetSet.GetRawImageFromDevice A DCOrigin=',dbgs(DCOrigin.X),',',dbgs(DCOrigin.Y),' SrcRect=',dbgs(ARect.Left),',',dbgs(ARect.Top),',',dbgs(ARect.Right),',',dbgs(ARect.Bottom));
{$ENDIF}
R := ARect;
LPtoDP(ADC, R, 2);
Types.OffSetRect(R, DCOrigin.x, DCOrigin.y);
Drawable := DevCtx.Drawable;
if Drawable = nil then
// get screen shot
Drawable := gdk_screen_get_root_window(gdk_screen_get_default);
Result := RawImage_FromDrawable(ARawImage, Drawable, nil, @R);
end;
{------------------------------------------------------------------------------
Function: GetControlConstraints
Params: Constraints: TObject
Returns: true on success
Updates the constraints object (e.g. TSizeConstraints) with interface specific
bounds.
------------------------------------------------------------------------------}
function TGtk2WidgetSet.GetControlConstraints(Constraints: TObject): boolean;
var
SizeConstraints: TSizeConstraints absolute Constraints;
Widget: PGtkWidget;
MinWidth: Integer;
MinHeight: Integer;
MaxWidth: Integer;
MaxHeight: Integer;
begin
Result := True;
if Constraints is TSizeConstraints then
begin
MinWidth := 1;
MinHeight := 1;
MaxWidth := 0;
MaxHeight := 0;
if (SizeConstraints.Control=nil) then exit;
if SizeConstraints.Control is TScrollBar then begin
// TScrollBar
if TScrollBar(SizeConstraints.Control).Kind=sbHorizontal then begin
Widget:=GetStyleWidget(lgsHorizontalScrollbar);
MinHeight:=Widget^.requisition.Height;
MaxHeight:=MinHeight;
end else begin
Widget:=GetStyleWidget(lgsVerticalScrollbar);
MinWidth:=Widget^.requisition.Width;
MaxWidth:=MinWidth;
end;
//DebugLn('TGtk2WidgetSet.GetControlConstraints A '+dbgs(MinWidth)+','+dbgs(MinHeight),' ',dbgs(TScrollBar(SizeConstraints.Control).Kind=sbHorizontal),' ',TScrollBar(SizeConstraints.Control).Name);
end
else if SizeConstraints.Control is TCustomSplitter then begin
// TCustomSplitter
if TCustomSplitter(SizeConstraints.Control).ResizeAnchor in [akTop,akBottom] then
begin
Widget:=GetStyleWidget(lgsHorizontalPaned);
MinHeight:=Widget^.requisition.Height;
MaxHeight:=MinHeight;
end else begin
Widget:=GetStyleWidget(lgsVerticalPaned);
MinWidth:=Widget^.requisition.Width;
MaxWidth:=MinWidth;
end;
end
else if SizeConstraints.Control is TCustomMemo then begin
// TCustomMemo
Widget:=GetStyleWidget(lgsHorizontalScrollbar);
MinHeight:=Widget^.requisition.Height+20;
Widget:=GetStyleWidget(lgsVerticalScrollbar);
MinWidth:=Widget^.requisition.Width+20;
end
else if SizeConstraints.Control is TCustomTrackBar then begin
// TCustomTrackBar
if TCustomTrackBar(SizeConstraints.Control).Orientation=trHorizontal then
begin
Widget:=GetStyleWidget(lgsHScale);
gtk_scale_set_draw_value(PGtkScale(Widget),
TCustomTrackBar(SizeConstraints.Control).TickStyle <> tsNone);
gtk_widget_size_request(Widget, @Widget^.Requisition);
MinHeight:=Widget^.requisition.height;
end else begin
Widget:=GetStyleWidget(lgsVScale);
gtk_scale_set_draw_value(PGtkScale(Widget),
TCustomTrackBar(SizeConstraints.Control).TickStyle <> tsNone);
gtk_widget_size_request(Widget, @Widget^.Requisition);
MinWidth:=Widget^.requisition.width;
end;
//DebugLn(['TGtk2WidgetSet.GetControlConstraints ',DbgSName(SizeConstraints.Control),' ',MinWidth,',',MinHeight]);
end;
SizeConstraints.SetInterfaceConstraints(MinWidth,MinHeight,
MaxWidth,MaxHeight);
end;
end;
{------------------------------------------------------------------------------
function TGtk2WidgetSet.GetLCLOwnerObject(Handle: HWnd): TObject;
------------------------------------------------------------------------------}
function TGtk2WidgetSet.GetLCLOwnerObject(Handle: HWnd): TObject;
begin
if Handle<>0 then
Result:=GetNearestLCLObject({%H-}PGtkWidget(Handle))
else
Result:=nil;
end;
function PromptUserBoxClosed(Widget : PGtkWidget; {%H-}Event : PGdkEvent;
data: gPointer) : GBoolean; cdecl;
var
ModalResult : PtrUInt;
begin
{ We were requested by window manager to close so return EscapeResult}
if PInteger(data)^ = 0 then
begin
ModalResult:= {%H-}PtrUInt(g_object_get_data(PGObject(Widget), 'modal_result'));
{ Don't allow to close if we don't have a default return value }
Result:= (ModalResult = 0);
if not Result then PInteger(data)^:= ModalResult
else DebugLn('Do not close !!!');
end else Result:= false;
end;
function PromptUserButtonClicked(Widget : PGtkWidget; data: gPointer) : GBoolean; cdecl;
begin
PInteger(data)^ := {%H-}PtrUInt(g_object_get_data(PGObject(Widget), 'modal_result'));
Result := False;
end;
function gtk_message_dialog_get_message_area(Dialog:PGtkMessageDialog):PGtkWidget; cdecl; external gtklib;
procedure set_message_text(Dialog:PGtkMessageDialog;const msg: string;const is_pango_markup:boolean=false);
var
ma:PGtkWidget;
mainList:PgList;
begin
if is_pango_markup then
gtk_message_dialog_set_markup(Dialog, PGChar(msg))
else
begin
ma:=gtk_message_dialog_get_message_area(Dialog);
MainList := gtk_container_get_children(PGtkContainer(ma));
if Assigned(MainList) then
begin
gtk_label_set_label(PGtkLabel(MainList^.data),PGChar(msg));
g_list_free(MainList);
end;
end;
end;
function TGtk2WidgetSet.AskUser(const DialogCaption, DialogMessage: string; DialogType:
LongInt; Buttons: TDialogButtons; HelpCtx: Longint): LongInt;
const
ButtonResults : array[mrNone..mrYesToAll] of Longint = (
-1, idButtonOK, idButtonCancel, idButtonAbort, idButtonRetry,
idButtonIgnore, idButtonYes, idButtonNo, idButtonAll, idButtonNoToAll,
idButtonYesToAll);
var
Dialog: PGtkWidget;
function ResponseID(const AnID: Integer): Integer;
begin
case AnID of
idButtonOK : Result := GTK_RESPONSE_OK;
idButtonCancel : Result := GTK_RESPONSE_CANCEL;
idButtonHelp : Result := GTK_RESPONSE_HELP;
idButtonYes : Result := GTK_RESPONSE_YES;
idButtonNo : Result := GTK_RESPONSE_NO;
idButtonClose : Result := GTK_RESPONSE_CLOSE;
idButtonAbort : Result := GTK_RESPONSE_REJECT;
idButtonRetry : Result := GTK_RESPONSE_LCL_RETRY;
idButtonIgnore : Result := GTK_RESPONSE_LCL_IGNORE;
idButtonAll : Result := GTK_RESPONSE_LCL_ALL;
idButtonNoToAll : Result := GTK_RESPONSE_LCL_NOTOALL;
idButtonYesToAll : Result := GTK_RESPONSE_LCL_YESTOALL;
else
Result:=AnID;
end;
end;
procedure CreateButton(const ALabel : String; const AResponse: Integer;
const AImageHint: Integer = -1);
var
NewButton: PGtkWidget;
BitmapHandle, MaskHandle: HBitmap;
GDIObject: PGDIObject;
Pixbuf: PGdkPixbuf;
Mask: PGdkBitmap;
Img: PGtkWidget;
begin
NewButton := gtk_dialog_add_button(PGtkDialog(Dialog),
PgChar(Ampersands2Underscore(ALabel)), AResponse);
gtk_button_set_use_underline(PGtkButton(NewButton), True);
if AImageHint >= 0 then
begin
if ThemeServices.GetStockImage(AImageHint, BitmapHandle, MaskHandle) then
begin
GDIObject := {%H-}PGDIObject(BitmapHandle);
Mask := nil;
Pixbuf := nil;
if GDIObject^.GDIBitmapType = gbPixbuf then
Pixbuf := GDIObject^.GDIPixbufObject
else
Mask := CreateGdkMaskBitmap(BitmapHandle, MaskHandle);
Img := gtk_image_new;
if Pixbuf <> nil then
gtk_image_set_from_pixbuf(PGtkImage(Img), Pixbuf)
else
gtk_image_set_from_pixmap(PGtkImage(Img), GDIObject^.GDIPixmapObject.Image, Mask);
gtk_button_set_image(PGtkButton(NewButton), Img);
if Mask <> nil then
g_object_unref(Mask);
DeleteObject(BitmapHandle);
DeleteObject(MaskHandle);
end;
end;
end;
var
Btn: PGtkButton;
BtnId: Longint;
GtkDialogType: TGtkMessageType;
BtnIdx: Integer;
DefaultID, CancelID: Integer;
X: Integer;
MainList,ChildList: PGList;
Title: String;
ActiveWindow: HWND;
BtnResult: LongInt;
DlgBtn: TDialogButton;
ADialogResult: Integer;
Btns: TGtkButtonsType;
begin
Result := mrNone;
ReleaseCapture;
if (Length(DialogMessage)>1000) then
begin
Result:=inherited;
exit;
end;
ADialogResult := mrCancel;
case DialogType of
idDialogWarning: GtkDialogType := GTK_MESSAGE_WARNING;
idDialogError: GtkDialogType := GTK_MESSAGE_ERROR;
idDialogInfo : GtkDialogType := GTK_MESSAGE_INFO;
idDialogConfirm : GtkDialogType := GTK_MESSAGE_QUESTION;
else
GtkDialogType := GTK_MESSAGE_INFO;
end;
Btns := GTK_BUTTONS_NONE;
DefaultId := 0;
CancelId := -1;
for X := 0 to Buttons.Count - 1 do
begin
DlgBtn:=Buttons[X];
if (Buttons.DefaultButton=DlgBtn)
or ((Buttons.DefaultButton=nil) and DlgBtn.Default) then
DefaultID := X;
if (Buttons.CancelButton=DlgBtn)
or ((Buttons.CancelButton=nil) and DlgBtn.Cancel)
then begin
CancelID := X;
ADialogResult := DlgBtn.ModalResult;
end;
end;
Dialog := gtk_message_dialog_new({$IFDEF HASX}PGtkWindow(GetDesktopWidget){$ELSE}nil{$ENDIF},
GTK_DIALOG_MODAL, GtkDialogType, Btns,
nil);
set_message_text(PGtkMessageDialog(Dialog), PGChar(DialogMessage));
g_signal_connect(PGtkObject(Dialog), 'delete-event',
TGtkSignalFunc(@PromptUserBoxClosed),
@ADialogResult);
if Btns = GTK_BUTTONS_NONE then
begin
// gtk2 have reverted buttons eg. No, Yes
for BtnIdx := Buttons.Count - 1 downto 0 do
begin
with Buttons[BtnIdx] do
if (ModalResult >= Low(ButtonResults)) and (ModalResult <= High(ButtonResults)) then
begin
BtnID := ButtonResults[ModalResult];
case BtnID of
idButtonOK : CreateButton(Caption, GTK_RESPONSE_OK, BtnID);
idButtonCancel : CreateButton(Caption, GTK_RESPONSE_CANCEL, BtnID);
idButtonHelp : CreateButton(Caption, GTK_RESPONSE_HELP, BtnID);
idButtonYes : CreateButton(Caption, GTK_RESPONSE_YES, BtnID);
idButtonNo : CreateButton(Caption, GTK_RESPONSE_NO, BtnID);
idButtonClose : CreateButton(Caption, GTK_RESPONSE_CLOSE, BtnID);
idButtonAbort : CreateButton(Caption, GTK_RESPONSE_REJECT, BtnID);
idButtonRetry : CreateButton(Caption, GTK_RESPONSE_LCL_RETRY, BtnID);
idButtonIgnore : CreateButton(Caption, GTK_RESPONSE_LCL_IGNORE, BtnID);
idButtonAll : CreateButton(Caption, GTK_RESPONSE_LCL_ALL, BtnID);
idButtonNoToAll : CreateButton(Caption, GTK_RESPONSE_LCL_NOTOALL, BtnID);
idButtonYesToAll : CreateButton(Caption, GTK_RESPONSE_LCL_YESTOALL, BtnID);
end;
end else
CreateButton(Caption, GTK_RESPONSE_NONE, BtnID); // user defined buttons
end;
end;
MainList := gtk_container_children(PGtkContainer(PGtkDialog(Dialog)^.action_area));
ChildList := MainList;
BtnIdx := 0;
while ChildList <> nil do
begin
if (ChildList^.Data <> nil) then
begin
if GTK_IS_BUTTON(ChildList^.Data) then
begin
Btn := PGtkButton(ChildList^.Data);
DlgBtn := Buttons[BtnIdx];
BtnID := -1;
BtnResult:=DlgBtn.ModalResult;
if (BtnResult>=Low(ButtonResults)) and (BtnResult<=High(ButtonResults)) then
BtnID := ButtonResults[DlgBtn.ModalResult]
else
BtnID := DlgBtn.ModalResult;
if (BtnIdx=CancelID) then
g_object_set_data(PGObject(Dialog), 'modal_result', {%H-}Pointer(PtrInt(DlgBtn.ModalResult)));
X := DlgBtn.ModalResult;
g_object_set_data(PGObject(Btn), 'modal_result',
{%H-}Pointer(PtrInt(X)));
g_signal_connect(PGtkObject(Btn), 'clicked',
TGtkSignalFunc(@PromptUserButtonClicked), @ADialogResult);
if DefaultID = BtnIdx then
begin
gtk_dialog_set_default_response(PGtkDialog(Dialog), BtnID);
gtk_widget_grab_focus(PgtkWidget(Btn));
X := DlgBtn.ModalResult;
if CancelID<0 then
g_object_set_data(PGObject(Dialog), 'modal_result',
{%H-}Pointer(PtrInt(X)));
end;
inc(BtnIdx);
end;
end;
ChildList := g_list_next(ChildList);
end;
if MainList <> nil then
g_list_free(MainList);
if DialogCaption <> '' then
gtk_window_set_title(PGtkWindow(Dialog), PGChar(DialogCaption))
else
begin
Title := '';
case DialogType of
idDialogWarning: Title := rsMtWarning;
idDialogError: Title := rsMtError;
idDialogInfo : Title := rsMtInformation;
idDialogConfirm : Title := rsMtConfirmation;
end;
gtk_window_set_title(PGtkWindow(Dialog), PGChar(Title));
end;
if (gtk_major_version = 2) and (gtk_minor_version <= 12) then
begin
ActiveWindow := GetActiveWindow;
if ActiveWindow <> 0 then
gtk_window_set_transient_for(PGtkWindow(Dialog), {%H-}PGtkWindow(ActiveWindow));
end;
gtk_dialog_run(PGtkDialog(Dialog));
gtk_widget_destroy(Dialog);
Result := ADialogResult;
end;
function TGtk2WidgetSet.PromptUser(const DialogCaption: string;
const DialogMessage: string; DialogType: LongInt; Buttons: PLongInt;
ButtonCount: LongInt; DefaultIndex: LongInt; EscapeResult: LongInt): LongInt;
var
Btn: PGtkButton;
Dialog: PGtkWidget;
ADialogResult: Integer;
GtkDialogType: TGtkMessageType;
Btns: TGtkButtonsType;
BtnIdx: Integer;
DefaultID: Integer;
X: Integer;
MainList,ChildList: PGList;
Title: String;
ActiveWindow: HWND;
QuotedMessage: Pgchar;
procedure CreateButton(const ALabel : String; const AResponse: Integer);
var
NewButton: PGtkButton;
begin
NewButton := PGtkButton(gtk_dialog_add_button(PGtkDialog(Dialog),
PgChar(Ampersands2Underscore(ALabel)), AResponse));
gtk_button_set_use_underline(NewButton, True);
end;
function tr(UseWidgetStr: boolean; const TranslatedStr, WidgetStr: String): string;
begin
if UseWidgetStr then
Result:=WidgetStr
else
Result:=TranslatedStr;
end;
function ResponseID(const AnID: Integer): Integer;
begin
case AnID of
idButtonOK : Result := GTK_RESPONSE_OK;
idButtonCancel : Result := GTK_RESPONSE_CANCEL;
idButtonHelp : Result := GTK_RESPONSE_HELP;
idButtonYes : Result := GTK_RESPONSE_YES;
idButtonNo : Result := GTK_RESPONSE_NO;
idButtonClose : Result := GTK_RESPONSE_CLOSE;
idButtonAbort : Result := GTK_RESPONSE_REJECT;
idButtonRetry : Result := GTK_RESPONSE_LCL_RETRY;
idButtonIgnore : Result := GTK_RESPONSE_LCL_IGNORE;
idButtonAll : Result := GTK_RESPONSE_LCL_ALL;
idButtonNoToAll : Result := GTK_RESPONSE_LCL_NOTOALL;
idButtonYesToAll : Result := GTK_RESPONSE_LCL_YESTOALL;
end;
end;
begin
Result := -1;
ReleaseCapture;
ADialogResult := EscapeResult;
case DialogType of
idDialogWarning: GtkDialogType := GTK_MESSAGE_WARNING;
idDialogError: GtkDialogType := GTK_MESSAGE_ERROR;
idDialogInfo : GtkDialogType := GTK_MESSAGE_INFO;
idDialogConfirm : GtkDialogType := GTK_MESSAGE_QUESTION;
else
GtkDialogType := GTK_MESSAGE_INFO;
end;
Btns := GTK_BUTTONS_NONE;
DefaultId := 0;
for X := 0 to ButtonCount - 1 do
begin
if X = DefaultIndex then
DefaultID := Buttons[X];
end;
Dialog := gtk_message_dialog_new({$IFDEF HASX}PGtkWindow(GetDesktopWidget){$ELSE}nil{$ENDIF},
GTK_DIALOG_MODAL, GtkDialogType, Btns,
nil);
// Can't pass message string to gtk_message_dialog_new, as % chars are interpreted
// gtk_message_dialog_set_markup interpets HTML, so we need to quote that
QuotedMessage := g_markup_escape_text(PGChar(DialogMessage), Length(DialogMessage));
gtk_message_dialog_set_markup(PGtkMessageDialog(Dialog), QuotedMessage);
g_free(QuotedMessage);
g_signal_connect(PGtkObject(Dialog), 'delete-event',
TGtkSignalFunc(@PromptUserBoxClosed),
@ADialogResult);
if Btns = GTK_BUTTONS_NONE then
begin
// gtk2 have reverted buttons eg. No, Yes
for BtnIdx := ButtonCount-1 downto 0 do
begin
case Buttons[BtnIdx] of
idButtonOK : CreateButton(tr(rsmbOK='&OK',rsmbOK, 'gtk-ok'), GTK_RESPONSE_OK);
idButtonCancel : CreateButton(tr(rsmbCancel='Cancel',rsmbCancel,'gtk-cancel'), GTK_RESPONSE_CANCEL);
idButtonHelp : CreateButton(tr(rsmbHelp='&Help',rsmbHelp,'gtk-help'), GTK_RESPONSE_HELP);
idButtonYes : CreateButton(tr(rsmbYes='&Yes',rsmbYes,'gtk-yes'), GTK_RESPONSE_YES);
idButtonNo : CreateButton(tr(rsmbNo='&No',rsmbNo,'gtk-no'), GTK_RESPONSE_NO);
idButtonClose : CreateButton(tr(rsmbClose='&Close',rsmbClose,'gtk-close'), GTK_RESPONSE_CLOSE);
idButtonAbort : CreateButton(rsMBAbort, GTK_RESPONSE_REJECT);
idButtonRetry : CreateButton(rsMBRetry, GTK_RESPONSE_LCL_RETRY);
idButtonIgnore : CreateButton(rsMBIgnore, GTK_RESPONSE_LCL_IGNORE);
idButtonAll : CreateButton(rsMbAll, GTK_RESPONSE_LCL_ALL);
idButtonNoToAll : CreateButton(rsMBNoToAll, GTK_RESPONSE_LCL_NOTOALL);
idButtonYesToAll : CreateButton(rsMBYesToAll, GTK_RESPONSE_LCL_YESTOALL);
end;
end;
end;
MainList := gtk_container_children(PGtkContainer(PGtkDialog(Dialog)^.action_area));
ChildList := MainList;
BtnIdx := 0;
while ChildList <> nil do
begin
if (ChildList^.Data <> nil) then
begin
if GTK_IS_BUTTON(ChildList^.Data) then
begin
Btn := PGtkButton(ChildList^.Data);
if Buttons[BtnIdx] = idButtonCancel then
g_object_set_data(PGObject(Dialog), 'modal_result', Pointer(idButtonCancel));
X := Buttons[BtnIdx];
g_object_set_data(PGObject(Btn), 'modal_result',
{%H-}Pointer(PtrInt(X)));
g_signal_connect(PGtkObject(Btn), 'clicked',
TGtkSignalFunc(@PromptUserButtonClicked), @ADialogResult);
if DefaultID = Buttons[BtnIdx] then
begin
gtk_dialog_set_default_response(PGtkDialog(Dialog), ResponseID(Buttons[BtnIdx]));
X := Buttons[BtnIdx];
g_object_set_data(PGObject(Dialog), 'modal_result',
{%H-}Pointer(PtrInt(X)));
end;
inc(BtnIdx);
end;
end;
ChildList := g_list_next(ChildList);
end;
if MainList <> nil then
g_list_free(MainList);
if DialogCaption <> '' then
gtk_window_set_title(PGtkWindow(Dialog), PGChar(DialogCaption))
else
begin
Title := '';
case DialogType of
idDialogWarning: Title := rsMtWarning;
idDialogError: Title := rsMtError;
idDialogInfo : Title := rsMtInformation;
idDialogConfirm : Title := rsMtConfirmation;
end;
gtk_window_set_title(PGtkWindow(Dialog), PGChar(Title));
end;
ActiveWindow := GetActiveWindow;
if ActiveWindow <> 0 then
gtk_window_set_transient_for(PGtkWindow(Dialog), {%H-}PGtkWindow(ActiveWindow));
// the following line keeps the old behaviour making the dialog appear
// at screen center instead of at ActiveWindow center
gtk_window_set_position(PGtkWindow(Dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run(PGtkDialog(Dialog));
gtk_widget_destroy(Dialog);
Result := ADialogResult;
end;
function TGtk2WidgetSet.SetComboMinDropDownSize(Handle: HWND; MinItemsWidth,
MinItemsHeight, MinItemCount: integer): boolean;
var
p: PGtkWidget;
Menu: PGtkWidget;
Requisition: TGtkRequisition;
begin
Result:=True;
p := GetWidgetInfo({%H-}Pointer(Handle))^.CoreWidget;
Menu := PGtkWidget(g_object_get_data(G_OBJECT(p), 'Menu'));
if Menu<>nil then begin
Requisition.width := MinItemsWidth;
Requisition.height := MinItemsHeight * MinItemCount;
gtk_widget_size_request(Menu, @Requisition);
end;
end;
//##apiwiz##eps## // Do not remove, no wizard declaration after this line
function waithandle_iocallback({%H-}source: PGIOChannel; condition: TGIOCondition;
data: gpointer): gboolean; cdecl;
var
lEventHandler: PWaitHandleEventHandler absolute data;
begin
//debugln('waithandle_iocallback lEventHandler=',HexStr(Cardinal(lEventHandler),8));
lEventHandler^.OnEvent(lEventHandler^.UserData, condition);
Result := true;
end;
function TGtk2WidgetSet.AddEventHandler(AHandle: TLCLHandle; AFlags: dword;
AEventHandler: TWaitHandleEvent; AData: PtrInt): PEventHandler;
var
giochannel: pgiochannel;
lEventHandler: PWaitHandleEventHandler;
begin
if AEventHandler = nil then exit;
New(lEventHandler);
giochannel := g_io_channel_unix_new(AHandle);
lEventHandler^.Handle := AHandle;
lEventHandler^.UserData := AData;
lEventHandler^.GIOChannel := giochannel;
lEventHandler^.OnEvent := AEventHandler;
lEventHandler^.GSourceID := g_io_add_watch(giochannel,
AFlags, @waithandle_iocallback, lEventHandler);
//debugln('TGtk2WidgetSet.AddEventHandler lEventHandler=',HexStr(Cardinal(lEventHandler),8),' AHandle=',dbgs(lEventHandler^.Handle));
lEventHandler^.PrevHandler := nil;
lEventHandler^.NextHandler := FWaitHandles;
if FWaitHandles <> nil then
FWaitHandles^.PrevHandler := lEventHandler;
FWaitHandles := lEventHandler;
Result := lEventHandler;
end;
procedure TGtk2WidgetSet.RemoveEventHandler(var AHandler: PEventHandler);
var
lEventHandler: PWaitHandleEventHandler absolute AHandler;
begin
if AHandler = nil then exit;
g_source_remove(lEventHandler^.GSourceID);
{ channel will be freed with ref count drops to 0 }
g_io_channel_unref(lEventHandler^.GIOChannel);
if lEventHandler^.PrevHandler = nil then
FWaitHandles := lEventHandler^.NextHandler
else
lEventHandler^.PrevHandler^.NextHandler := lEventHandler^.NextHandler;
if lEventHandler^.NextHandler <> nil then
lEventHandler^.NextHandler^.PrevHandler := lEventHandler^.PrevHandler;
//debugln('TGtk2WidgetSet.RemoveEventHandler lEventHandler=',HexStr(Cardinal(lEventHandler),8),' AHandle=',dbgs(lEventHandler^.Handle));
Dispose(lEventHandler);
AHandler := nil;
end;
procedure TGtk2WidgetSet.SetEventHandlerFlags(AHandler: PEventHandler; NewFlags: dword);
var
lEventHandler: PWaitHandleEventHandler absolute AHandler;
begin
if AHandler = nil then exit;
g_source_remove(lEventHandler^.GSourceID);
lEventHandler^.GSourceID := g_io_add_watch(lEventHandler^.GIOChannel,
NewFlags, @waithandle_iocallback, lEventHandler);
//debugln('TGtk2WidgetSet.SetEventHandlerFlags lEventHandler=',HexStr(Cardinal(lEventHandler),8),' AHandle=',dbgs(lEventHandler^.Handle));
end;
procedure TGtk2WidgetSet.SetRubberBandRect(const ARubberBand: HWND; const ARect: TRect);
begin
if ARubberBand = 0 then
exit;
with ARect do
gdk_window_move_resize({%H-}PGtkWidget(ARubberBand)^.window, Left,
Top, Right - Left, Bottom - Top);
end;
type
PPipeEventInfo = ^TPipeEventInfo;
TPipeEventInfo = record
Handler: PEventHandler;
UserData: PtrInt;
OnEvent: TPipeEvent;
end;
function TGtk2WidgetSet.AddPipeEventHandler(AHandle: TLCLHandle;
AEventHandler: TPipeEvent; AData: PtrInt): PPipeEventHandler;
var
lPipeEventInfo: PPipeEventInfo;
begin
if AEventHandler = nil then exit;
New(lPipeEventInfo);
lPipeEventInfo^.UserData := AData;
lPipeEventInfo^.OnEvent := AEventHandler;
lPipeEventInfo^.Handler := AddEventHandler(AHandle, G_IO_IN or G_IO_HUP or G_IO_OUT,
@HandlePipeEvent, {%H-}PtrUInt(lPipeEventInfo));
Result := lPipeEventInfo;
end;
procedure TGtk2WidgetSet.HandlePipeEvent(AData: PtrInt; AFlags: dword);
var
lPipeEventInfo: PPipeEventInfo absolute AData;
lReasons: TPipeReasons;
begin
lReasons := [];
if AFlags and G_IO_IN = G_IO_IN then
Include(lReasons, prDataAvailable);
if AFlags and G_IO_OUT = G_IO_OUT then
Include(lReasons, prCanWrite);
if AFlags and G_IO_HUP = G_IO_HUP then
Include(lReasons, prBroken);
lPipeEventInfo^.OnEvent(lPipeEventInfo^.UserData, lReasons);
end;
procedure TGtk2WidgetSet.RemovePipeEventHandler(var AHandler: PPipeEventHandler);
var
lPipeEventInfo: PPipeEventInfo absolute AHandler;
begin
if AHandler = nil then exit;
RemoveEventHandler(lPipeEventInfo^.Handler);
Dispose(lPipeEventInfo);
AHandler := nil;
end;
{$ifdef UNIX}
function TGtk2WidgetSet.AddProcessEventHandler(AHandle: TLCLHandle;
AEventHandler: TChildExitEvent; AData: PtrInt): PProcessEventHandler;
var
lHandler: PChildSignalEventHandler;
begin
if AEventHandler = nil then exit(nil);
New(lHandler);
lHandler^.PID := TPid(AHandle);
lHandler^.UserData := AData;
lHandler^.OnEvent := AEventHandler;
lHandler^.PrevHandler := nil;
lHandler^.NextHandler := FChildSignalHandlers;
if FChildSignalHandlers <> nil then
FChildSignalHandlers^.PrevHandler := lHandler;
FChildSignalHandlers := lHandler;
Result := lHandler;
end;
procedure TGtk2WidgetSet.RemoveProcessEventHandler(var AHandler: PProcessEventHandler);
var
lHandler: PChildSignalEventHandler absolute AHandler;
begin
if AHandler = nil then exit;
if lHandler^.PrevHandler = nil then
FChildSignalHandlers := lHandler^.NextHandler
else
lHandler^.PrevHandler^.NextHandler := lHandler^.NextHandler;
if lHandler^.NextHandler <> nil then
lHandler^.NextHandler^.PrevHandler := lHandler^.PrevHandler;
Dispose(lHandler);
AHandler := nil;
end;
{$else}
{$IFDEF VerboseGtkToDos}{$warning TGtk2WidgetSet.RemoveProcessEventHandler and TGtk2WidgetSet.AddProcessEventHandler not implemented on this OS}{$ENDIF}
//PChildSignalEventHandler is only defined on unix
function TGtk2WidgetSet.AddProcessEventHandler(AHandle: THandle;
AEventHandler: TChildExitEvent; AData: PtrInt): PProcessEventHandler;
begin
Result := nil;
end;
procedure TGtk2WidgetSet.RemoveProcessEventHandler(var AHandler: PProcessEventHandler);
begin
end;
{$endif}
|