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
|
#include "stdafx.h"
#include "Window.h"
#include "Container.h"
#include "Frame.h"
#include "App.h"
#include "Painter.h"
#include "Win32Dpi.h"
#include "GtkSignal.h"
#include "GtkEmpty.h"
#include "Env.h"
#include "Exception.h"
// We're using gtk_widget_override_font, as there is no better way of setting custom fonts on widgets...
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
namespace gui {
// Null is a failure code from eg. GetParent function, should be OK to use as invalid.
const Handle Window::invalid;
Window::Window() :
myHandle(invalid), myParent(null), myRoot(null),
myVisible(true), myEnabled(true), drawing(false), mouseInside(false),
gdkWindow(null), gTimer(null),
myPos(0, 0, 100, 100) /* large enough to not generate warnings in Gtk+ */ {
App *a = app(engine());
if (a->headless())
throw new (this) GuiError(S("Can not create windows when running in headless mode."));
myText = new (this) Str(L"");
myFont = a->defaultFont;
}
Window::~Window() {
if (myHandle != invalid) {
App *a = app(engine());
a->removeWindow(this);
}
}
ContainerBase *Window::parent() {
return myParent;
}
Frame *Window::rootFrame() {
return myRoot;
}
Handle Window::handle() const {
return myHandle;
}
Size Window::minSize() {
return Size();
}
void Window::attachParent(ContainerBase *parent) {
myParent = parent;
if (parent == this)
myRoot = as<Frame>(this);
else
myRoot = parent->myRoot;
}
void Window::detachParent() {
handle(invalid);
myRoot = null;
}
void Window::windowDestroyed() {}
void Window::parentCreated(nat id) {
assert(myParent);
myRoot = myParent->myRoot;
if (!created())
if (!create(myParent, id))
WARNING(L"Failed to create a window...");
}
void Window::handle(Handle handle) {
App *a = app(engine());
if (myHandle != invalid) {
if (myPainter)
myPainter->uiDetach();
if (myParent == null) {
// We're a frame (or orphaned).
destroyWindow(myHandle);
} else if (!myParent->created()) {
// Parent has already been destroyed. This means we're destroyed,
// by our parent, even though we have a handle.
// If we have a timer, we need to destroy that though.
clearTimer();
} else {
destroyWindow(myHandle);
}
// Notify we've been destroyed.
myHandle = invalid;
windowDestroyed();
// DestroyWindow sends WM_CLOSE, which may be handled by this class.
a->removeWindow(this);
}
myHandle = handle;
if (myHandle != invalid) {
a->addWindow(this);
if (myPainter)
myPainter->uiAttach(this);
}
}
Bool Window::onKey(Bool down, key::Key id, Modifiers modifiers) {
return false;
}
Bool Window::onChar(Nat id) {
return false;
}
Bool Window::onClick(Bool pressed, Point at, mouse::MouseButton button) {
return false;
}
Bool Window::onDblClick(Point at, mouse::MouseButton button) {
return false;
}
Bool Window::onMouseMove(Point at) {
return false;
}
void Window::onMouseEnter() {}
void Window::onMouseLeave() {}
Bool Window::onMouseVScroll(Point at, Int delta) {
return false;
}
Bool Window::onMouseHScroll(Point at, Int delta) {
return false;
}
Bool Window::visible() {
return myVisible;
}
Bool Window::enabled() {
return myEnabled;
}
Font *Window::font() {
return new (this) Font(*myFont);
}
void Window::resized(Size size) {
// Nothing here, override when needed.
}
void Window::resized() {
resized(pos().size());
}
void Window::onResize(Size size) {
resized(size);
}
void Window::painter(MAYBE(Painter *) p) {
if (myPainter)
myPainter->uiDetach();
Painter *old = myPainter;
myPainter = p;
if (old) {
if (!p)
detachPainter();
} else {
if (p)
attachPainter();
}
if (created() && myPainter)
myPainter->uiAttach(this);
}
MAYBE(Painter *) Window::painter() {
return myPainter;
}
void Window::onTimer() {}
#ifdef GUI_WIN32
void Window::destroyWindow(Handle handle) {
DestroyWindow(handle.hwnd());
}
MsgResult Window::onMessage(const Message &msg) {
switch (msg.msg) {
case WM_SIZE: {
Size s = pos().size();
if (myPainter) {
RECT r;
GetClientRect(handle().hwnd(), &r);
myPainter->uiResize(Size(Float(r.right), Float(r.bottom)), dpiScale(currentDpi()));
}
onResize(s);
return msgResult(0);
}
case WM_PAINT:
return onPaint();
case WM_ERASEBKGND: {
// Much like the default message proc, but respects 'windowColor'.
HWND wnd = handle().hwnd();
HDC dc = (HDC)msg.wParam;
HBRUSH brush;
COLORREF color;
windowBackground(brush, color);
RECT rect;
GetClientRect(wnd, &rect);
FillRect(dc, &rect, brush);
return msgResult(1);
}
case WM_TIMER: {
if (msg.wParam == (WPARAM)handle().hwnd()) {
onTimer();
return msgResult(0);
}
}
case WM_CTLCOLORSTATIC: {
HBRUSH brush;
COLORREF color;
windowBackground(brush, color);
HDC dc = HDC(msg.wParam);
SetBkColor(dc, color);
return msgResult((LRESULT)brush);
}
// TODO: Propagate mouse messages to parent windows?
case WM_LBUTTONDOWN:
if (onClick(true, dpiFromPx(currentDpi(), mousePos(msg)), mouse::left))
return msgResult(0);
break;
case WM_LBUTTONUP:
if (onClick(false, dpiFromPx(currentDpi(), mousePos(msg)), mouse::left))
return msgResult(0);
break;
case WM_LBUTTONDBLCLK:
onClick(true, dpiFromPx(currentDpi(), mousePos(msg)), mouse::left);
if (onDblClick(dpiFromPx(currentDpi(), mousePos(msg)), mouse::left))
return msgResult(0);
break;
case WM_MBUTTONDOWN:
if (onClick(true, dpiFromPx(currentDpi(), mousePos(msg)), mouse::middle))
return msgResult(0);
break;
case WM_MBUTTONUP:
if (onClick(false, dpiFromPx(currentDpi(), mousePos(msg)), mouse::middle))
return msgResult(0);
break;
case WM_MBUTTONDBLCLK:
onClick(true, dpiFromPx(currentDpi(), mousePos(msg)), mouse::middle);
if (onDblClick(dpiFromPx(currentDpi(), mousePos(msg)), mouse::middle))
return msgResult(0);
break;
case WM_RBUTTONDOWN:
if (onClick(true, dpiFromPx(currentDpi(), mousePos(msg)), mouse::right))
return msgResult(0);
break;
case WM_RBUTTONUP:
if (onClick(false, dpiFromPx(currentDpi(), mousePos(msg)), mouse::right))
return msgResult(0);
break;
case WM_RBUTTONDBLCLK:
onClick(true, dpiFromPx(currentDpi(), mousePos(msg)), mouse::right);
if (onDblClick(dpiFromPx(currentDpi(), mousePos(msg)), mouse::right))
return msgResult(0);
break;
case WM_MOUSEMOVE:
if (!mouseInside) {
TRACKMOUSEEVENT track;
track.cbSize = sizeof(track);
track.dwFlags = TME_LEAVE;
track.hwndTrack = handle().hwnd();
track.dwHoverTime = 0;
TrackMouseEvent(&track);
mouseInside = true;
onMouseEnter();
}
if (onMouseMove(dpiFromPx(currentDpi(), mousePos(msg))))
return msgResult(0);
break;
case WM_MOUSEWHEEL:
if (onMouseVScroll(dpiFromPx(currentDpi(), mouseAbsPos(handle(), msg)), GET_WHEEL_DELTA_WPARAM(msg.wParam)))
return msgResult(0);
break;
case WM_MOUSEHWHEEL:
if (onMouseHScroll(dpiFromPx(currentDpi(), mouseAbsPos(handle(), msg)), GET_WHEEL_DELTA_WPARAM(msg.wParam)))
return msgResult(0);
break;
case WM_MOUSELEAVE:
mouseInside = false;
onMouseLeave();
break;
}
return noResult();
}
MsgResult Window::beforeMessage(const Message &msg) {
switch (msg.msg) {
case WM_KEYUP:
case WM_SYSKEYUP:
if (onKey(false, keycode(msg.wParam), modifiers()))
return msgResult(0);
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (onKey(true, keycode(msg.wParam), modifiers()))
return msgResult(0);
break;
case WM_CHAR:
if (onChar(Nat(msg.wParam)))
return msgResult(0);
break;
}
return noResult();
}
void Window::afterMessage(const Message &msg) {}
void Window::visible(Bool show) {
myVisible = show;
if (created())
ShowWindow(handle().hwnd(), show ? TRUE : FALSE);
}
void Window::enabled(Bool enable) {
myEnabled = enable;
if (created())
EnableWindow(handle().hwnd(), enable ? TRUE : FALSE);
}
Str *Window::text() {
if (created()) {
Nat len = GetWindowTextLength(handle().hwnd());
GcArray<wchar> *src = runtime::allocArray<wchar>(engine(), &wcharArrayType, len + 1);
GetWindowText(handle().hwnd(), src->v, len + 1);
myText = (new (this) Str(src))->fromCrLf();
}
return myText;
}
void Window::text(Str *s) {
myText = s;
if (created())
SetWindowText(handle().hwnd(), s->toCrLf()->c_str());
}
Rect Window::pos() {
if (created()) {
// Note: We don't take client borders into account for anything but top-level frames.
RECT r;
HWND h = handle().hwnd();
GetWindowRect(h, &r);
POINT a = { r.left, r.top };
POINT b = { r.right, r. bottom };
if (myParent != this) {
ScreenToClient(myParent->handle().hwnd(), &a);
ScreenToClient(myParent->handle().hwnd(), &b);
}
myPos = dpiFromPx(currentDpi(), convert(a, b));
// The Edit control has an extra border (painted outside the control). Take that into account.
DWORD exStyle = GetWindowLong(h, GWL_EXSTYLE);
if (exStyle & WS_EX_CLIENTEDGE) {
myPos = myPos.grow(Size(1));
}
}
return myPos;
}
void Window::pos(Rect r) {
myPos = r;
if (created()) {
HWND h = handle().hwnd();
// The Edit control has an extra border (painted outside the control). Take that into account.
DWORD exStyle = GetWindowLong(h, GWL_EXSTYLE);
if (exStyle & WS_EX_CLIENTEDGE) {
r = r.shrink(Size(1));
}
// Note: We don't take client borders into account for anything but top-level frames.
RECT z = convert(dpiToPx(currentDpi(), r));
MoveWindow(h, z.left, z.top, z.right - z.left, z.bottom - z.top, TRUE);
}
}
void Window::focus() {
if (created())
SetFocus(handle().hwnd());
else if (Frame *f = rootFrame())
f->focus(this);
}
void Window::font(Font *font) {
if (font != myFont)
myFont = new (this) Font(*font);
if (created())
SendMessage(handle().hwnd(), WM_SETFONT, (WPARAM)font->handle(currentDpi()), TRUE);
}
void Window::update() {
if (created())
UpdateWindow(handle().hwnd());
}
void Window::repaint() {
if (created())
InvalidateRect(handle().hwnd(), NULL, FALSE);
}
void Window::attachPainter() {
if (!created())
return;
repaint();
}
void Window::detachPainter() {
if (!created())
return;
// We must ask really hard to make Windows repaint the window properly.
RedrawWindow(handle().hwnd(), NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
}
bool Window::onCommand(nat id) {
return false;
}
bool Window::onNotify(NMHDR *data) {
return false;
}
Nat Window::currentDpi() {
if (myRoot)
return myRoot->currentDpi();
else
return defaultDpi;
}
void Window::updateDpi(Bool move) {
// Update our position.
if (move)
pos(myPos);
// Update the font.
if (myFont)
font(myFont);
}
void Window::windowBackground(HBRUSH &brush, COLORREF &color) {
if (myParent && myParent != this) {
return myParent->windowBackground(brush, color);
} else {
// Avoid crashes and provide a somewhat sane defaults:
color = GetSysColor(gui::windowBackground);
brush = GetSysColorBrush(gui::windowBackground);
}
}
bool Window::create(ContainerBase *parent, nat id) {
return createEx(NULL, childFlags, 0, parent->handle().hwnd(), id);
}
bool Window::createEx(LPCTSTR className, DWORD style, DWORD exStyle, HWND parent) {
return createEx(className, style, exStyle, parent, 0, cNormal);
}
bool Window::createEx(LPCTSTR className, DWORD style, DWORD exStyle, HWND parent, nat id) {
return createEx(className, style, exStyle, parent, id, cNormal);
}
bool Window::createEx(LPCTSTR className, DWORD style, DWORD exStyle, HWND parent, nat id, CreateFlags flags) {
assert(handle() == invalid);
App *app = gui::app(engine());
if (className == null)
className = (LPCTSTR)app->windowClass();
if (cManualVisibility & ~flags) {
if (myVisible) {
style |= WS_VISIBLE;
} else {
style &= ~WS_VISIBLE;
}
}
if (!myEnabled)
style |= WS_DISABLED;
// Note: This was rewritten to not copy myPos.p0 into 'p' here. This seems to trigger an
// interesting bug(?) on the 64-bit compiler (VS2008, likely fixed since) where the position
// becomes completely corrupted.
Point p;
Size s;
if (WS_CHILD & ~style) {
RECT r = convert(myPos);
AdjustWindowRectEx(&r, style & ~WS_OVERLAPPED, FALSE, exStyle);
Rect c = convert(r);
p = c.p0;
s = c.size();
} else if (parent) {
Nat dpi = windowDpi(parent);
p = myPos.p0 * dpiScale(dpi);
s = myPos.size() * dpiScale(dpi);
} else {
p = myPos.p0;
s = myPos.size();
}
// Put everything in ints now, so that we can properly set CW_USEDEFAULT without potential
// rounding errors.
int x = (int)p.x;
int y = (int)p.y;
int width = (int)s.w;
int height = (int)s.h;
if (flags & cAutoPos) {
x = CW_USEDEFAULT;
y = 0;
if (parent != NULL) {
// Center the dialog over the parent.
RECT parentRect;
GetWindowRect(parent, &parentRect);
x = int(parentRect.left + (parentRect.right - parentRect.left) / 2 - s.w / 2);
y = int(parentRect.top + (parentRect.bottom - parentRect.top) / 2 - s.h / 2);
}
// Invalid size?
if (!myPos.size().valid()) {
width = CW_USEDEFAULT;
height = 0;
}
}
// Position controls before creation.
if (myPos.size().valid())
onResize(myPos.size());
HINSTANCE instance = app->instance();
LPCTSTR windowName = myText->toCrLf()->c_str();
app->preCreate(this);
HWND z = CreateWindowEx(exStyle, className, windowName, style,
x, y, width, height,
parent, (HMENU)id, instance, NULL);
if (z == NULL) {
app->createAborted(this);
return false;
}
if (flags & cWin32Subclass) {
app->subclass(z);
}
if (WS_CHILD & ~style) {
// Take DPI into account.
Nat dpi = windowDpi(z);
if (dpi != defaultDpi) {
RECT r = convert(Rect(Point(), myPos.size() * dpiScale(dpi)));
dpiAdjustWindowRectEx(&r, style & ~WS_OVERLAPPED, FALSE, exStyle, dpi);
s = convert(r).size();
SetWindowPos(z, NULL, 0, 0, int(s.w), int(s.h), SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
handle(z);
if (timerInterval != Duration()) {
setTimer(timerInterval);
}
SendMessage(handle().hwnd(), WM_SETFONT, (WPARAM)myFont->handle(currentDpi()), TRUE);
return true;
}
MsgResult Window::onPaint() {
if (myPainter) {
RepaintParams p;
myPainter->uiRepaint(&p);
ValidateRect(handle().hwnd(), NULL);
return msgResult(0);
} else {
return noResult();
}
}
void Window::setTimer(Duration interval) {
timerInterval = interval;
if (created()) {
// The HWND is used as a timer ID. That makes it unique. We can still use timer #0 for internal stuff.
SetTimer(handle().hwnd(), (UINT_PTR)handle().hwnd(), (UINT)interval.inMs(), NULL);
}
}
void Window::clearTimer() {
if (handle() != Handle())
KillTimer(handle().hwnd(), (UINT_PTR)handle().hwnd());
}
#endif
#ifdef GUI_GTK
class Timer {
public:
Timer(Duration interval, GtkWidget *widget, Engine &engine);
~Timer();
private:
GtkWidget *widget;
Engine *engine;
guint id;
static gboolean callback(gpointer user_data);
};
Timer::Timer(Duration interval, GtkWidget *widget, Engine &engine) :
widget(widget), engine(&engine),
id(g_timeout_add(interval.inMs(), &callback, this)) {}
Timer::~Timer() {
if (id) {
GSource *s = g_main_context_find_source_by_id(NULL, id);
if (s)
g_source_destroy(s);
id = 0;
}
}
gboolean Timer::callback(gpointer user_data) {
Timer *me = (Timer *)user_data;
App *app = gui::app(*me->engine);
Window *win = app->findWindow(Handle(me->widget));
win->onTimer();
return G_SOURCE_CONTINUE;
}
bool Window::create(ContainerBase *parent, nat id) {
// Create an event box so that we receive events for this window.
initWidget(parent, gtk_event_box_new());
return true;
}
void Window::initWidget(ContainerBase *parent, GtkWidget *widget) {
handle(widget);
parent->addChild(widget, myPos);
if (myVisible)
gtk_widget_show(widget);
else
gtk_widget_hide(widget);
if (!myEnabled)
gtk_widget_set_sensitive(widget, FALSE);
if (myFont != app(engine())->defaultFont)
gtk_widget_override_font(fontWidget(), myFont->desc());
initSignals(widget, drawWidget());
}
void Window::initSignals(GtkWidget *widget, GtkWidget *draw) {
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onKeyDown>::to(widget, "key-press-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onKeyUp>::to(widget, "key-release-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onButton>::to(widget, "button-press-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onButton>::to(widget, "button-release-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onMotion>::to(widget, "motion-notify-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onEnter>::to(widget, "enter-notify-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onLeave>::to(widget, "leave-notify-event", engine());
Signal<gboolean, Window, GdkEvent *>::Connect<&Window::onScroll>::to(widget, "scroll-event", engine());
Signal<void, Window, GdkRectangle *>::Connect<&Window::onSize>::to(widget, "size-allocate", engine());
Signal<void, Window>::Connect<&Window::onRealize>::to(draw, "realize", engine());
Signal<void, Window>::Connect<&Window::onUnrealize>::to(draw, "unrealize", engine());
Signal<gboolean, Window, cairo_t *>::Connect<&Window::onDraw>::to(draw, "draw", engine());
if (timerInterval != Duration())
setTimer(timerInterval);
}
void Window::destroyWindow(Handle handle) {
gtk_widget_destroy(handle.widget());
if (gTimer) {
delete gTimer;
gTimer = null;
}
}
gboolean Window::onKeyUp(GdkEvent *event) {
GdkEventKey &k = event->key;
return onKey(false, keycode(k), modifiers(k)) ? TRUE : FALSE;
}
gboolean Window::onKeyDown(GdkEvent *event) {
GdkEventKey &k = event->key;
bool ok = onKey(true, keycode(k), modifiers(k));
if (ok)
ok = onChar(k.keyval);
return ok ? TRUE : FALSE;
}
// Translate widget coordinates to window coordinates.
static void translateToWindow(GtkWidget *widget, gdouble &x, gdouble &y) {
if (!widget)
return;
// Note: This might not be entirely correct in nested structures.
// Note: Seems to work OK inside a tab control, which seems to consist of multiple windows. Not entirely sure though.
for (GtkWidget *at = widget; !gtk_widget_get_has_window(at); at = gtk_widget_get_parent(at)) {
GtkAllocation delta;
gtk_widget_get_allocation(at, &delta);
x += delta.x;
y += delta.y;
}
}
// Translate window coordinates to widget coordinates. Note: Due to how Gtk propagates events,
// it might be the case that 'eventWindow' is a child window to whatever window 'to' might be inside.
static bool translatePoint(GdkWindow *eventWindow, GtkWidget *to, gdouble x, gdouble y, Point &out) {
// Find a widget that has a window by traversing from 'to'.
GtkWidget *toParent = to;
while (!gtk_widget_get_has_window(toParent)) {
toParent = gtk_widget_get_parent(toParent);
if (!toParent) {
WARNING(L"Failed to find a parent widget with a window!");
return false;
}
}
GdkWindow *toWindow = gtk_widget_get_window(toParent);
// At this point, we know that 'toParent' is a parent of 'eventWindow'. Try to find it by
// traversing from 'eventWindow', and updating coordinates as we go. GdkWindow coordinates
// are relative to the parent window, in contrast to widgets.
while (eventWindow != toWindow) {
// Make coordinates relative the window's parent.
gint winX, winY;
gdk_window_get_position(eventWindow, &winX, &winY);
x += winX;
y += winY;
// Traverse.
eventWindow = gdk_window_get_effective_parent(eventWindow);
if (!eventWindow) {
// This happens as it seems like events for a window are passed to the dialog window
// first. As such, we should not warn in this case.
return false;
}
}
// Now, we have coordinates that are relative to the window owned by "toParent". Now we can
// just ask Gtk to convert coordinates for us!
gint xDelta, yDelta;
gtk_widget_translate_coordinates(toParent, to, 0, 0, &xDelta, &yDelta);
x += xDelta;
y += yDelta;
// Finally, we can ask Gtk about the allocation of 'to' and make sure that the coordinate is
// inside the rectangle.
GtkAllocation toAlloc;
gtk_widget_get_allocation(to, &toAlloc);
out.x = x;
out.y = y;
return out.x >= 0.0f && out.y >= 0.0f
&& out.x < toAlloc.width && out.y < toAlloc.height;
}
gboolean Window::onButton(GdkEvent *event) {
Point pt;
GdkEventButton &b = event->button;
if (!translatePoint(b.window, drawWidget(), b.x, b.y, pt))
return FALSE;
mouse::MouseButton button = mouse::MouseButton(b.button - 1);
bool ok = false;
if (button == mouse::left || button == mouse::middle || button == mouse::right) {
switch (b.type) {
case GDK_BUTTON_PRESS:
ok = onClick(true, pt, button);
break;
case GDK_BUTTON_RELEASE:
ok = onClick(false, pt, button);
break;
case GDK_2BUTTON_PRESS:
ok = onDblClick(pt, button);
break;
}
}
return ok ? TRUE : FALSE;
}
gboolean Window::onMotion(GdkEvent *event) {
Point pt;
GdkEventMotion &m = event->motion;
if (!translatePoint(m.window, drawWidget(), m.x, m.y, pt))
return FALSE;
return onMouseMove(pt) ? TRUE : FALSE;
}
gboolean Window::onEnter(GdkEvent *) {
onMouseEnter();
// No propagation.
return TRUE;
}
gboolean Window::onLeave(GdkEvent *) {
onMouseLeave();
// No propagation.
return TRUE;
}
gboolean Window::onScroll(GdkEvent *event) {
Point pt;
GdkEventScroll &s = event->scroll;
if (!translatePoint(s.window, drawWidget(), s.x, s.y, pt)) {
return FALSE;
}
const Int step = 120;
bool ok = false;
GdkScrollDirection direction;
gdouble dx = 0, dy = 0;
if (gdk_event_get_scroll_direction(event, &direction)) {
switch (s.direction) {
case GDK_SCROLL_UP:
ok = onMouseVScroll(pt, -step);
break;
case GDK_SCROLL_DOWN:
ok = onMouseVScroll(pt, step);
break;
case GDK_SCROLL_LEFT:
ok = onMouseHScroll(pt, -step);
break;
case GDK_SCROLL_RIGHT:
ok = onMouseHScroll(pt, step);
break;
}
} else if (gdk_event_get_scroll_deltas(event, &dx, &dy)) {
Int x(dx * step);
if (x != 0)
ok |= onMouseHScroll(pt, -x);
Int y(dy * step);
if (y != 0)
ok |= onMouseVScroll(pt, -y);
}
return ok ? TRUE : FALSE;
}
void Window::onSize(GdkRectangle *alloc) {
// Note: We're interested in forwarding the size of the drawing widget (if it differs from
// the root-widget of the window).
GtkWidget *draw = drawWidget();
GtkAllocation drawAlloc;
gtk_widget_get_allocation(draw, &drawAlloc);
// If we have our own window, resize that as well.
if (gdkWindow) {
// Modify the x and y coords if we're several layers down.
gdouble x = drawAlloc.x, y = drawAlloc.y;
translateToWindow(gtk_widget_get_parent(draw), x, y);
// gdk_window_move_resize(gdkWindow, alloc->x, alloc->y, alloc->width, alloc->height);
gdk_window_move_resize(gdkWindow, x, y, drawAlloc.width, drawAlloc.height);
}
Size s(drawAlloc.width, drawAlloc.height);
myPos.size(s);
// GTK sends us this signal quite often, so filter out dummy draws.
if (lastWidth != drawAlloc.width || lastHeight != drawAlloc.height) {
lastWidth = drawAlloc.width;
lastHeight = drawAlloc.height;
if (myPainter) {
int scale = gtk_widget_get_scale_factor(draw);
myPainter->uiResize(s * scale, scale);
}
onResize(s);
// Queue a repaint for this window.
gtk_widget_queue_draw(handle().widget());
// // Queue a repaint for this window. Otherwise, resizing windows that partially contain some
// // continuous rendering will not always be updated properly. We're probably in a race with
// // Gtk when we just update a part of the window.
// GdkWindow *window = gtk_widget_get_window(handle().widget());
// if (window) {
// gdk_window_invalidate_rect(window, NULL, false);
// }
}
}
bool Window::preExpose(GtkWidget *widget) {
// Do we have a painter?
if (myPainter && gdkWindow && widget == drawWidget()) {
// Does not seem to be needed right now, but this is where one can hook into paint
// events before Gtk+ gets hold of them!
// RepaintParams params = { gdkWindow, drawWidget(), NULL };
// myPainter->uiRepaint(¶ms);
// return true;
}
// Process normally.
return false;
}
void Window::updateGtkMinSize() {
// Nothing here.
}
gboolean Window::onDraw(cairo_t *ctx) {
GdkWindow *window = gdkWindow;
if (!window) {
GtkWidget *draw = drawWidget();
if (draw) {
window = gtk_widget_get_window(draw);
}
}
// Anything we should do at all?
if (!window)
return FALSE;
// Is this draw event for us?
if (!gtk_cairo_should_draw_window(ctx, window))
return FALSE;
// We will call ourselves in some cases.
if (drawing)
return FALSE;
// Do we have a painter?
if (myPainter) {
RepaintParams params = { window, drawWidget(), ctx };
myPainter->uiRepaint(¶ms);
// If we're using a native window, we need to inhibit the default behavior.
if (gdkWindow)
return TRUE;
return FALSE;
// This is not needed anymore. We're being kind to Gtk+ and are repainting through the
// provided cairo_t context.
// if (GDK_IS_WAYLAND_WINDOW(window)) {
// // This is a variant of what we're doing below.
// GtkWidget *me = drawWidget();
// GtkWidget *parentWidget = gtk_widget_get_parent(me);
// if (!parentWidget || !GTK_IS_CONTAINER(parentWidget)) {
// // If no parent exists, or the parent is not a container, we simply delegate to the
// // default behaviour. This should not happen in practice, but it is good to have a
// // decent fallback. Since the parent is not a container, the default implementation
// // will probably work anyway.
// return false;
// }
// GtkContainer *parent = GTK_CONTAINER(gtk_widget_get_parent(me));
// // It seems Gtk+ does not understand what we're doing and therefore miscalculates the x-
// // and y- positions of the layout during drawing. We correct this by using the offset in
// // the cairo_t.
// GtkAllocation position;
// gtk_widget_get_allocation(me, &position);
// // Since we have to pretend that the current child does not have a window when calling
// // 'gtk_container_propagate_draw' below, the current widget's offset will be applied twice.
// cairo_translate(ctx, -position.x, -position.y);
// drawing = true;
// gtk_container_propagate_draw(parent, me, ctx);
// drawing = false;
// }
// return TRUE;
}
// Do we have our own window and need to paint the background?
if (!gdkWindow)
return FALSE;
// Do we have a window and need to paint the background?
GtkWidget *me = drawWidget();
GtkWidget *parentWidget = gtk_widget_get_parent(me);
if (!parentWidget || !GTK_IS_CONTAINER(parentWidget)) {
// If no parent exists, or the parent is not a container, we simply delegate to the
// default behaviour. This should not happen in practice, but it is good to have a
// decent fallback. Since the parent is not a container, the default implementation
// will probably work anyway.
return FALSE;
}
GtkContainer *parent = GTK_CONTAINER(gtk_widget_get_parent(me));
// Fill with the background color we figured out earlier.
App *app = gui::app(engine());
Color bg = app->defaultBgColor;
cairo_set_source_rgba(ctx, bg.r, bg.g, bg.b, bg.a);
cairo_paint(ctx);
// It seems Gtk+ does not understand what we're doing and therefore miscalculates the x-
// and y- positions of the layout during drawing. We correct this by using the offset in
// the cairo_t.
GtkAllocation position;
gtk_widget_get_allocation(me, &position);
// Since we have to pretend that the current child does not have a window when calling
// 'gtk_container_propagate_draw' below, the current widget's offset will be applied twice.
cairo_translate(ctx, -position.x*2, -position.y*2);
// Call the original draw function here, with our translated context.
// typedef gboolean (*DrawFn)(GtkWidget *, cairo_t *);
// DrawFn original = GTK_WIDGET_GET_CLASS(me)->draw;
// if (original)
// (*original)(me, ctx);
// Call 'gtk_container_propagate_draw' to draw ourselves again. Remember that we are
// currently drawing so that we do not get into an infinite recursion when we are called
// again. We could call 'GTK_WIDGET_GET_CLASS(me)->draw' instead, but that function
// assumes that the cairo_t is properly transformed so that (0, 0) is at the top left of
// the widget. That is not the case now, due to the possible bug inside
// 'gtk_container_propagate_draw'.
gtk_widget_set_has_window(me, FALSE);
drawing = true;
gtk_container_propagate_draw(parent, me, ctx);
drawing = false;
gtk_widget_set_has_window(me, TRUE);
return TRUE;
}
bool Window::useNativeWindow() {
// By default, we don't need separate windows in the rendering model. This might, however,
// be expensive in cases where many widgets are overlaid the rendering area. For these
// cases, we do allow creating separate X windows by setting an environment variable.
// On Wayland, we don't want a separate window as it confuses the window manager.
if (GDK_IS_WAYLAND_WINDOW(gtk_widget_get_window(drawWidget())))
return false;
// Initialize the variable once. This is atomic and will only be executed once.
static const char *RENDER_X_WINDOW = getenv(ENV_RENDER_X_WINDOW);
// Check the environment variable.
if (RENDER_X_WINDOW) {
// It exists! We need to create a separate window in two cases:
// 1: we have a painter attached
// 2: our parent has a painter attached
if (myPainter)
return true;
if (myParent && myParent != this && myParent->myPainter)
return true;
}
return false;
}
void Window::onRealize() {
if (gdkWindow)
return;
if (!useNativeWindow()) {
setWindowMask(gtk_widget_get_window(drawWidget()));
// Make sure we are using double buffering when drawing to it in "composite" mode.
gtk_widget_set_double_buffered(drawWidget(), TRUE);
if (myPainter)
myPainter->uiAttach(this);
return;
}
// Create the window.
GdkWindowAttr attrs;
memset(&attrs, 0, sizeof(attrs));
GtkWidget *drawTo = drawWidget();
GdkWindow *oldWindow = gtk_widget_get_window(drawTo);
GdkWindow *parentWindow = gtk_widget_get_parent_window(drawTo);
if (oldWindow && parentWindow != oldWindow) {
WARNING(L"Using previously created windows could be bad.");
// This widget already has its own window. Use that.
gdkWindow = oldWindow;
if (myPainter)
gtk_widget_set_double_buffered(drawTo, FALSE);
if (myPainter)
myPainter->uiAttach(this);
return;
}
if (!oldWindow)
oldWindow = parentWindow;
GtkAllocation alloc;
gtk_widget_get_allocation(drawTo, &alloc);
gdouble x = 0, y = 0;
translateToWindow(drawTo, x, y);
attrs.x = x;
attrs.y = y;
attrs.width = alloc.width;
attrs.height = alloc.height;
attrs.event_mask = gtk_widget_get_events(drawTo) | GDK_EXPOSURE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK;
attrs.window_type = GDK_WINDOW_CHILD; // GDK_WINDOW_SUBSURFACE is nice on Wayland.
attrs.wclass = GDK_INPUT_OUTPUT;
// Probably good. We could use *_get_system_visual() instead.
attrs.visual = gdk_screen_get_rgba_visual(gdk_window_get_screen(oldWindow));
gdkWindow = gdk_window_new(oldWindow, &attrs, GDK_WA_X | GDK_WA_Y);
gdk_window_ensure_native(gdkWindow);
gdk_window_show_unraised(gdkWindow);
// Unref the old window.
if (oldWindow)
g_object_unref(oldWindow);
gtk_widget_set_has_window(drawTo, TRUE);
gtk_widget_set_window(drawTo, gdkWindow);
gdk_window_set_user_data(gdkWindow, drawTo);
if (myPainter) {
gtk_widget_set_double_buffered(drawTo, FALSE);
}
setWindowMask(gdkWindow);
if (myPainter)
myPainter->uiAttach(this);
}
void Window::onUnrealize() {
if (!gdkWindow)
return;
GtkWidget *drawTo = drawWidget();
// gtk_widget_set_window(drawTo, NULL);
gtk_widget_set_has_window(drawTo, FALSE);
if (myPainter)
myPainter->uiDetach();
gdkWindow = null;
}
void Window::setWindowMask(GdkWindow *window) {
if (window) {
int mask = gdk_window_get_events(window);
mask |= GDK_POINTER_MOTION_MASK;
mask |= GDK_BUTTON_MOTION_MASK;
mask |= GDK_BUTTON_PRESS_MASK;
mask |= GDK_BUTTON_RELEASE_MASK;
mask |= GDK_SCROLL_MASK;
mask |= GDK_SMOOTH_SCROLL_MASK;
mask |= GDK_LEAVE_NOTIFY_MASK;
gdk_window_set_events(window, GdkEventMask(mask));
}
}
static void recreate_widget(GtkWidget *widget) {
gtk_widget_unmap(widget);
gtk_widget_unrealize(widget);
gtk_widget_realize(widget);
gtk_widget_map(widget);
}
void Window::attachPainter() {
if (!created())
return;
if (gdkWindow)
return;
recreate_widget(drawWidget());
}
void Window::detachPainter() {
if (!created())
return;
if (!gdkWindow)
return;
recreate_widget(drawWidget());
}
Str *Window::text() {
return myText;
}
void Window::text(Str *str) {
myText = str;
}
Rect Window::pos() {
return myPos;
}
void Window::pos(Rect r) {
myPos = r;
if (created() && parent()) {
parent()->moveChild(handle().widget(), myPos);
}
}
void Window::visible(Bool v) {
myVisible = v;
if (created()) {
if (v)
gtk_widget_show(handle().widget());
else
gtk_widget_hide(handle().widget());
}
}
void Window::enabled(Bool v) {
myEnabled = v;
if (created()) {
gtk_widget_set_sensitive(handle().widget(), v ? TRUE : FALSE);
}
}
void Window::focus() {
if (created())
gtk_widget_grab_focus(handle().widget());
else if (Frame *f = rootFrame())
f->focus(this);
}
void Window::font(Font *font) {
myFont = new (this) Font(*font);
if (created())
gtk_widget_override_font(fontWidget(), myFont->desc());
}
GtkWidget *Window::fontWidget() {
return handle().widget();
}
GtkWidget *Window::drawWidget() {
return handle().widget();
}
void Window::update() {
if (created()) {
gtk_widget_queue_draw(handle().widget());
// GdkWindow *window = gtk_widget_get_window(handle().widget());
// if (window) {
// gdk_window_invalidate_rect(window, NULL, true);
// gdk_window_process_updates(window, true);
// }
}
}
void Window::repaint() {
if (created()) {
gtk_widget_queue_draw(handle().widget());
// GdkWindow *window = gtk_widget_get_window(handle().widget());
// if (window)
// gdk_window_invalidate_rect(window, NULL, true);
}
}
void Window::setTimer(Duration interval) {
timerInterval = interval;
if (created()) {
delete gTimer;
gTimer = null;
if (interval == Duration())
return;
gTimer = new Timer(interval, handle().widget(), engine());
}
}
void Window::clearTimer() {
if (gTimer)
delete gTimer;
gTimer = null;
}
#endif
}
|