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
|
/*
* This source file is part of libRocket, the HTML/CSS Interface Middleware
*
* For the latest information, see http://www.librocket.com
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "precompiled.h"
#include "../../Include/Rocket/Core.h"
#include "EventDispatcher.h"
#include "EventIterators.h"
#include "PluginRegistry.h"
#include "../../Include/Rocket/Core/StreamFile.h"
#include "../../Include/Rocket/Core/StreamMemory.h"
#include <algorithm>
#include <iterator>
namespace Rocket {
namespace Core {
const float DOUBLE_CLICK_TIME = 0.5f;
Context::Context(const String& name) : name(name), dimensions(0, 0), mouse_position(0, 0), clip_origin(-1, -1), clip_dimensions(-1, -1)
{
instancer = NULL;
// Initialise this to NULL; this will be set in Rocket::Core::CreateContext().
render_interface = NULL;
root = Factory::InstanceElement(NULL, "*", "#root", XMLAttributes());
root->SetId(name);
root->SetOffset(Vector2f(0, 0), NULL);
root->SetProperty(Z_INDEX, "0");
Element* element = Factory::InstanceElement(NULL, "body", "body", XMLAttributes());
cursor_proxy = dynamic_cast< ElementDocument* >(element);
if (cursor_proxy == NULL)
{
if (element != NULL)
element->RemoveReference();
}
else
cursor_proxy->context = this;
document_focus_history.push_back(root);
focus = root;
show_cursor = true;
drag_started = false;
drag_verbose = false;
drag_clone = NULL;
last_click_element = NULL;
last_click_time = 0;
}
Context::~Context()
{
PluginRegistry::NotifyContextDestroy(this);
UnloadAllDocuments();
UnloadAllMouseCursors();
ReleaseUnloadedDocuments();
if (cursor_proxy != NULL)
cursor_proxy->RemoveReference();
if (root != NULL)
root->RemoveReference();
if (instancer)
instancer->RemoveReference();
if (render_interface)
render_interface->RemoveReference();
}
// Returns the name of the context.
const String& Context::GetName() const
{
return name;
}
// Changes the dimensions of the screen.
void Context::SetDimensions(const Vector2i& _dimensions)
{
if (dimensions != _dimensions)
{
dimensions = _dimensions;
root->SetBox(Box(Vector2f((float) dimensions.x, (float) dimensions.y)));
root->DirtyLayout();
for (int i = 0; i < root->GetNumChildren(); ++i)
{
ElementDocument* document = root->GetChild(i)->GetOwnerDocument();
if (document != NULL)
{
document->DirtyLayout();
document->UpdatePosition();
}
}
clip_dimensions = dimensions;
}
}
// Returns the dimensions of the screen.
const Vector2i& Context::GetDimensions() const
{
return dimensions;
}
// Updates all elements in the element tree.
bool Context::Update()
{
root->Update();
// Release any documents that were unloaded during the update.
ReleaseUnloadedDocuments();
return true;
}
// Renders all visible elements in the element tree.
bool Context::Render()
{
RenderInterface* render_interface = GetRenderInterface();
if (render_interface == NULL)
return false;
// Update the layout for all documents in the root. This is done now as events during the
// update may have caused elements to require an update.
for (int i = 0; i < root->GetNumChildren(); ++i)
root->GetChild(i)->UpdateLayout();
render_interface->context = this;
ElementUtilities::ApplyActiveClipRegion(this, render_interface);
root->Render();
ElementUtilities::SetClippingRegion(NULL, this);
// Render the cursor proxy so any elements attached the cursor will be rendered below the cursor.
if (cursor_proxy != NULL)
{
cursor_proxy->Update();
cursor_proxy->SetOffset(Vector2f((float) Math::Clamp(mouse_position.x, 0, dimensions.x),
(float) Math::Clamp(mouse_position.y, 0, dimensions.y)),
NULL);
cursor_proxy->Render();
}
// Render the cursor document if we have one and we're showing the cursor.
if (active_cursor &&
show_cursor)
{
active_cursor->Update();
active_cursor->SetOffset(Vector2f((float) Math::Clamp(mouse_position.x, 0, dimensions.x),
(float) Math::Clamp(mouse_position.y, 0, dimensions.y)),
NULL);
active_cursor->Render();
}
render_interface->context = NULL;
return true;
}
// Creates a new, empty document and places it into this context.
ElementDocument* Context::CreateDocument(const String& tag)
{
Element* element = Factory::InstanceElement(NULL, tag, "body", XMLAttributes());
if (element == NULL)
{
Log::Message(Log::LT_ERROR, "Failed to instance document on tag '%s', instancer returned NULL.", tag.CString());
return NULL;
}
ElementDocument* document = dynamic_cast< ElementDocument* >(element);
if (document == NULL)
{
Log::Message(Log::LT_ERROR, "Failed to instance document on tag '%s', Found type '%s', was expecting derivative of ElementDocument.", tag.CString(), typeid(element).name());
element->RemoveReference();
return NULL;
}
document->context = this;
root->AppendChild(document);
PluginRegistry::NotifyDocumentLoad(document);
return document;
}
// Load a document into the context.
ElementDocument* Context::LoadDocument(const String& document_path)
{
// Open the stream based on the file path
StreamFile* stream = new StreamFile();
if (!stream->Open(document_path))
{
stream->RemoveReference();
return NULL;
}
// Load the document from the stream
ElementDocument* document = LoadDocument(stream);
stream->RemoveReference();
return document;
}
// Load a document into the context.
ElementDocument* Context::LoadDocument(Stream* stream)
{
PluginRegistry::NotifyDocumentOpen(this, stream->GetSourceURL().GetURL());
// Load the document from the stream.
ElementDocument* document = Factory::InstanceDocumentStream(this, stream);
if (!document)
return NULL;
root->AppendChild(document);
// Bind the events, run the layout and fire the 'onload' event.
ElementUtilities::BindEventAttributes(document);
document->UpdateLayout();
// Dispatch the load notifications.
PluginRegistry::NotifyDocumentLoad(document);
document->DispatchEvent(LOAD, Dictionary(), false);
return document;
}
// Load a document into the context.
ElementDocument* Context::LoadDocumentFromMemory(const String& string)
{
// Open the stream based on the string contents.
StreamMemory* stream = new StreamMemory((byte*)string.CString(), string.Length());
stream->SetSourceURL("[document from memory]");
// Load the document from the stream.
ElementDocument* document = LoadDocument(stream);
stream->RemoveReference();
return document;
}
// Unload the given document
void Context::UnloadDocument(ElementDocument* _document)
{
// Has this document already been unloaded?
for (size_t i = 0; i < unloaded_documents.size(); ++i)
{
if (unloaded_documents[i] == _document)
return;
}
// Add a reference, to ensure the document isn't released
// while we're closing it.
unloaded_documents.push_back(_document);
ElementDocument* document = _document;
if (document->GetParentNode() == root)
{
// Dispatch the unload notifications.
document->DispatchEvent(UNLOAD, Dictionary(), false);
PluginRegistry::NotifyDocumentUnload(document);
// Remove the document from the context.
root->RemoveChild(document);
}
// Remove the item from the focus history.
ElementList::iterator itr = std::find(document_focus_history.begin(), document_focus_history.end(), document);
if (itr != document_focus_history.end())
document_focus_history.erase(itr);
// Focus to the previous document if the old document is the current focus.
if (focus && focus->GetOwnerDocument() == document)
{
focus = NULL;
document_focus_history.back()->GetFocusLeafNode()->Focus();
}
// Clear the active element if the old document is the active element.
if (active && active->GetOwnerDocument() == document)
{
active = NULL;
}
// Rebuild the hover state.
UpdateHoverChain(Dictionary(), Dictionary(), mouse_position);
}
// Unload all the currently loaded documents
void Context::UnloadAllDocuments()
{
// Unload all children.
while (root->GetNumChildren(true) > 0)
UnloadDocument(root->GetChild(0)->GetOwnerDocument());
// Force cleanup of child elements now, reference counts must hit zero so that python (if it's in use) cleans up
// before we exit this method.
root->active_children.clear();
root->ReleaseElements(root->deleted_children);
}
// Adds a previously-loaded cursor document as a mouse cursor within this context.
void Context::AddMouseCursor(ElementDocument* cursor_document)
{
cursor_document->AddReference();
CursorMap::iterator i = cursors.find(cursor_document->GetTitle());
if (i != cursors.end())
{
if (active_cursor == (*i).second)
active_cursor = cursor_document;
if (default_cursor == (*i).second)
default_cursor = cursor_document;
(*i).second->RemoveReference();
}
cursors[cursor_document->GetTitle()] = cursor_document;
if (!default_cursor)
{
default_cursor = cursor_document;
active_cursor = cursor_document;
}
}
// Loads a document as a mouse cursor.
ElementDocument* Context::LoadMouseCursor(const String& document_path)
{
StreamFile* stream = new StreamFile();
if (!stream->Open(document_path))
return NULL;
// Load the document from the stream.
ElementDocument* document = Factory::InstanceDocumentStream(this, stream);
if (document == NULL)
{
stream->RemoveReference();
return NULL;
}
AddMouseCursor(document);
// Bind the events, run the layout and fire the 'onload' event.
ElementUtilities::BindEventAttributes(document);
document->UpdateLayout();
document->DispatchEvent(LOAD, Dictionary(), false);
stream->RemoveReference();
return document;
}
// Unload the given cursor.
void Context::UnloadMouseCursor(const String& cursor_name)
{
CursorMap::iterator i = cursors.find(cursor_name);
if (i != cursors.end())
{
if (default_cursor == (*i).second)
default_cursor = NULL;
if (active_cursor == (*i).second)
active_cursor = default_cursor;
(*i).second->RemoveReference();
cursors.erase(i);
}
}
// Unloads all currently loaded cursors.
void Context::UnloadAllMouseCursors()
{
while (!cursors.empty())
UnloadMouseCursor((*cursors.begin()).first.CString());
}
// Sets a cursor as the active cursor.
bool Context::SetMouseCursor(const String& cursor_name)
{
CursorMap::iterator i = cursors.find(cursor_name);
if (i == cursors.end())
{
active_cursor = default_cursor;
Log::Message(Log::LT_WARNING, "Failed to find cursor '%s' in context '%s', reverting to default cursor.", cursor_name.CString(), name.CString());
return false;
}
active_cursor = (*i).second;
return true;
}
// Shows or hides the cursor.
void Context::ShowMouseCursor(bool show)
{
show_cursor = show;
}
// Returns the first document found in the root with the given id.
ElementDocument* Context::GetDocument(const String& id)
{
for (int i = 0; i < root->GetNumChildren(); i++)
{
ElementDocument* document = root->GetChild(i)->GetOwnerDocument();
if (document == NULL)
continue;
if (document->GetId() == id)
return document;
}
return NULL;
}
// Returns a document in the context by index.
ElementDocument* Context::GetDocument(int index)
{
Element* element = root->GetChild(index);
if (element == NULL)
return NULL;
return element->GetOwnerDocument();
}
// Returns the number of documents in the context.
int Context::GetNumDocuments() const
{
return root->GetNumChildren();
}
// Returns the hover element.
Element* Context::GetHoverElement()
{
return *hover;
}
// Returns the focus element.
Element* Context::GetFocusElement()
{
return *focus;
}
// Returns the root element.
Element* Context::GetRootElement()
{
return root;
}
// Brings the document to the front of the document stack.
void Context::PullDocumentToFront(ElementDocument* document)
{
if (document != root->GetLastChild())
{
// Calling RemoveChild() / AppendChild() would be cleaner, but that dirties the document's layout
// unnecessarily, so we'll go under the hood here.
for (int i = 0; i < root->GetNumChildren(); ++i)
{
if (root->GetChild(i) == document)
{
root->children.erase(root->children.begin() + i);
root->children.insert(root->children.begin() + root->GetNumChildren(), document);
root->DirtyStackingContext();
}
}
}
}
// Sends the document to the back of the document stack.
void Context::PushDocumentToBack(ElementDocument* document)
{
if (document != root->GetFirstChild())
{
// See PullDocumentToFront().
for (int i = 0; i < root->GetNumChildren(); ++i)
{
if (root->GetChild(i) == document)
{
root->children.erase(root->children.begin() + i);
root->children.insert(root->children.begin(), document);
root->DirtyStackingContext();
}
}
}
}
// Adds an event listener to the root element.
void Context::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
{
root->AddEventListener(event, listener, in_capture_phase);
}
// Removes an event listener from the root element.
void Context::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
{
root->RemoveEventListener(event, listener, in_capture_phase);
}
// Sends a key down event into Rocket.
bool Context::ProcessKeyDown(Input::KeyIdentifier key_identifier, int key_modifier_state)
{
// Generate the parameters for the key event.
Dictionary parameters;
GenerateKeyEventParameters(parameters, key_identifier);
GenerateKeyModifierEventParameters(parameters, key_modifier_state);
if (focus)
return focus->DispatchEvent(KEYDOWN, parameters, true);
else
return root->DispatchEvent(KEYDOWN, parameters, true);
}
// Sends a key up event into Rocket.
bool Context::ProcessKeyUp(Input::KeyIdentifier key_identifier, int key_modifier_state)
{
// Generate the parameters for the key event.
Dictionary parameters;
GenerateKeyEventParameters(parameters, key_identifier);
GenerateKeyModifierEventParameters(parameters, key_modifier_state);
if (focus)
return focus->DispatchEvent(KEYUP, parameters, true);
else
return root->DispatchEvent(KEYUP, parameters, true);
}
// Sends a single character of text as text input into Rocket.
bool Context::ProcessTextInput(word character)
{
// Generate the parameters for the key event.
Dictionary parameters;
parameters.Set("data", character);
if (focus)
return focus->DispatchEvent(TEXTINPUT, parameters, true);
else
return root->DispatchEvent(TEXTINPUT, parameters, true);
}
// Sends a string of text as text input into Rocket.
bool Context::ProcessTextInput(const String& string)
{
bool consumed = true;
for (size_t i = 0; i < string.Length(); ++i)
{
// Generate the parameters for the key event.
Dictionary parameters;
parameters.Set("data", string[i]);
if (focus)
consumed = focus->DispatchEvent(TEXTINPUT, parameters, true) && consumed;
else
consumed = root->DispatchEvent(TEXTINPUT, parameters, true) && consumed;
}
return consumed;
}
// Sends a mouse movement event into Rocket.
void Context::ProcessMouseMove(int x, int y, int key_modifier_state)
{
// Check whether the mouse moved since the last event came through.
Vector2i old_mouse_position = mouse_position;
bool mouse_moved = (x != mouse_position.x) || (y != mouse_position.y);
if (mouse_moved)
{
mouse_position.x = x;
mouse_position.y = y;
}
// Generate the parameters for the mouse events (there could be a few!).
Dictionary parameters;
GenerateMouseEventParameters(parameters, -1);
GenerateKeyModifierEventParameters(parameters, key_modifier_state);
Dictionary drag_parameters;
GenerateMouseEventParameters(drag_parameters);
GenerateDragEventParameters(drag_parameters);
GenerateKeyModifierEventParameters(drag_parameters, key_modifier_state);
// Update the current hover chain. This will send all necessary 'onmouseout', 'onmouseover', 'ondragout' and
// 'ondragover' messages.
UpdateHoverChain(parameters, drag_parameters, old_mouse_position);
// Dispatch any 'onmousemove' events.
if (mouse_moved)
{
if (hover)
{
hover->DispatchEvent(MOUSEMOVE, parameters, true);
if (drag_hover &&
drag_verbose)
drag_hover->DispatchEvent(DRAGMOVE, drag_parameters, true);
}
}
}
static Element* FindFocusElement(Element* element)
{
ElementDocument* owner_document = element->GetOwnerDocument();
if (!owner_document || owner_document->GetProperty< int >(FOCUS) == FOCUS_NONE)
return NULL;
while (element && element->GetProperty< int >(FOCUS) == FOCUS_NONE)
{
element = element->GetParentNode();
}
return element;
}
// Sends a mouse-button down event into Rocket.
void Context::ProcessMouseButtonDown(int button_index, int key_modifier_state)
{
Dictionary parameters;
GenerateMouseEventParameters(parameters, button_index);
GenerateKeyModifierEventParameters(parameters, key_modifier_state);
if (button_index == 0)
{
Element* new_focus = *hover;
// Set the currently hovered element to focus if it isn't already the focus.
if (hover)
{
new_focus = FindFocusElement(*hover);
if (new_focus && new_focus != *focus)
{
if (!new_focus->Focus())
return;
}
}
// Save the just-pressed-on element as the pressed element.
active = new_focus;
bool propogate = true;
// Call 'onmousedown' on every item in the hover chain, and copy the hover chain to the active chain.
if (hover)
propogate = hover->DispatchEvent(MOUSEDOWN, parameters, true);
if (propogate)
{
// Check for a double-click on an element; if one has occured, we send the 'dblclick' event to the hover
// element. If not, we'll start a timer to catch the next one.
float click_time = GetSystemInterface()->GetElapsedTime();
if (active == last_click_element &&
click_time - last_click_time < DOUBLE_CLICK_TIME)
{
if (hover)
propogate = hover->DispatchEvent(DBLCLICK, parameters, true);
last_click_element = NULL;
last_click_time = 0;
}
else
{
last_click_element = *active;
last_click_time = click_time;
}
}
for (ElementSet::iterator itr = hover_chain.begin(); itr != hover_chain.end(); ++itr)
active_chain.push_back((*itr));
if (propogate)
{
// Traverse down the hierarchy of the newly focussed element (if any), and see if we can begin dragging it.
drag_started = false;
drag = hover;
while (drag)
{
int drag_style = drag->GetProperty(DRAG)->value.Get< int >();
switch (drag_style)
{
case DRAG_NONE: drag = drag->GetParentNode(); continue;
case DRAG_BLOCK: drag = NULL; continue;
default: drag_verbose = (drag_style == DRAG_DRAG_DROP || drag_style == DRAG_CLONE);
}
break;
}
}
}
else
{
// Not the primary mouse button, so we're not doing any special processing.
if (hover)
hover->DispatchEvent(MOUSEDOWN, parameters, true);
}
}
// Sends a mouse-button up event into Rocket.
void Context::ProcessMouseButtonUp(int button_index, int key_modifier_state)
{
Dictionary parameters;
GenerateMouseEventParameters(parameters, button_index);
GenerateKeyModifierEventParameters(parameters, key_modifier_state);
// Process primary click.
if (button_index == 0)
{
// The elements in the new hover chain have the 'onmouseup' event called on them.
if (hover)
hover->DispatchEvent(MOUSEUP, parameters, true);
// If the active element (the one that was being hovered over when the mouse button was pressed) is still being
// hovered over, we click it.
if (hover && active && active == FindFocusElement(*hover))
{
active->DispatchEvent(CLICK, parameters, true);
}
// Unset the 'active' pseudo-class on all the elements in the active chain; because they may not necessarily
// have had 'onmouseup' called on them, we can't guarantee this has happened already.
std::for_each(active_chain.begin(), active_chain.end(), PseudoClassFunctor("active", false));
active_chain.clear();
if (drag)
{
if (drag_started)
{
Dictionary drag_parameters;
GenerateMouseEventParameters(drag_parameters);
GenerateDragEventParameters(drag_parameters);
GenerateKeyModifierEventParameters(drag_parameters, key_modifier_state);
if (drag_hover)
{
if (drag_verbose)
{
drag_hover->DispatchEvent(DRAGDROP, drag_parameters, true);
drag_hover->DispatchEvent(DRAGOUT, drag_parameters, true);
}
}
drag->DispatchEvent(DRAGEND, drag_parameters, true);
ReleaseDragClone();
}
drag = NULL;
drag_hover = NULL;
drag_hover_chain.clear();
}
}
else
{
// Not the left mouse button, so we're not doing any special processing.
if (hover)
hover->DispatchEvent(MOUSEUP, parameters, true);
}
}
// Sends a mouse-wheel movement event into Rocket.
bool Context::ProcessMouseWheel(int wheel_delta, int key_modifier_state)
{
if (hover)
{
Dictionary scroll_parameters;
GenerateKeyModifierEventParameters(scroll_parameters, key_modifier_state);
scroll_parameters.Set("wheel_delta", wheel_delta);
return hover->DispatchEvent(MOUSESCROLL, scroll_parameters, true);
}
return true;
}
// Gets the context's render interface.
RenderInterface* Context::GetRenderInterface() const
{
return render_interface;
}
// Gets the current clipping region for the render traversal
bool Context::GetActiveClipRegion(Vector2i& origin, Vector2i& dimensions) const
{
if (clip_dimensions.x < 0 || clip_dimensions.y < 0)
return false;
origin = clip_origin;
dimensions = clip_dimensions;
return true;
}
// Sets the current clipping region for the render traversal
void Context::SetActiveClipRegion(const Vector2i& origin, const Vector2i& dimensions)
{
clip_origin = origin;
clip_dimensions = dimensions;
}
// Sets the instancer to use for releasing this object.
void Context::SetInstancer(ContextInstancer* _instancer)
{
ROCKET_ASSERT(instancer == NULL);
instancer = _instancer;
instancer->AddReference();
}
// Internal callback for when an element is removed from the hierarchy.
void Context::OnElementRemove(Element* element)
{
ElementSet::iterator i = hover_chain.find(element);
if (i == hover_chain.end())
return;
ElementSet old_hover_chain = hover_chain;
hover_chain.erase(i);
Element* hover_element = element;
while (hover_element != NULL)
{
Element* next_hover_element = NULL;
// Look for a child on this element's children that is also hovered.
for (int j = 0; j < hover_element->GetNumChildren(true); ++j)
{
// Is this child also in the hover chain?
Element* hover_child_element = hover_element->GetChild(j);
ElementSet::iterator k = hover_chain.find(hover_child_element);
if (k != hover_chain.end())
{
next_hover_element = hover_child_element;
hover_chain.erase(k);
break;
}
}
hover_element = next_hover_element;
}
Dictionary parameters;
GenerateMouseEventParameters(parameters, -1);
SendEvents(old_hover_chain, hover_chain, MOUSEOUT, parameters, true);
}
// Internal callback for when a new element gains focus
bool Context::OnFocusChange(Element* new_focus)
{
ElementSet old_chain;
ElementSet new_chain;
Element* old_focus = *(focus);
ElementDocument* old_document = old_focus ? old_focus->GetOwnerDocument() : NULL;
ElementDocument* new_document = new_focus->GetOwnerDocument();
// If the current focus is modal and the new focus is not modal, deny the request
if (old_document && old_document->IsModal() && (!new_document || !new_document->GetOwnerDocument()->IsModal()))
return false;
// Build the old chains
Element* element = old_focus;
while (element)
{
old_chain.insert(element);
element = element->GetParentNode();
}
// Build the new chain
element = new_focus;
while (element)
{
new_chain.insert(element);
element = element->GetParentNode();
}
Dictionary parameters;
// Send out blur/focus events.
SendEvents(old_chain, new_chain, BLUR, parameters, false);
SendEvents(new_chain, old_chain, FOCUS, parameters, false);
focus = new_focus;
// Raise the element's document to the front, if desired.
ElementDocument* document = focus->GetOwnerDocument();
if (document != NULL)
{
const Property* z_index_property = document->GetProperty(Z_INDEX);
if (z_index_property->unit == Property::KEYWORD &&
z_index_property->value.Get< int >() == Z_INDEX_AUTO)
document->PullToFront();
}
// Update the focus history
if (old_document != new_document)
{
// If documents have changed, add the new document to the end of the history
ElementList::iterator itr = std::find(document_focus_history.begin(), document_focus_history.end(), new_document);
if (itr != document_focus_history.end())
document_focus_history.erase(itr);
if (new_document != NULL)
document_focus_history.push_back(new_document);
}
return true;
}
// Generates an event for faking clicks on an element.
void Context::GenerateClickEvent(Element* element)
{
Dictionary parameters;
GenerateMouseEventParameters(parameters, 0);
element->DispatchEvent(CLICK, parameters, true);
}
// Updates the current hover elements, sending required events.
void Context::UpdateHoverChain(const Dictionary& parameters, const Dictionary& drag_parameters, const Vector2i& old_mouse_position)
{
Vector2f position((float) mouse_position.x, (float) mouse_position.y);
// Send out drag events.
if (drag)
{
if (mouse_position != old_mouse_position)
{
if (!drag_started)
{
Dictionary drag_start_parameters = drag_parameters;
drag_start_parameters.Set("mouse_x", old_mouse_position.x);
drag_start_parameters.Set("mouse_y", old_mouse_position.y);
drag->DispatchEvent(DRAGSTART, drag_start_parameters);
drag_started = true;
if (drag->GetProperty< int >(DRAG) == DRAG_CLONE)
{
// Clone the element and attach it to the mouse cursor.
CreateDragClone(*drag);
}
}
drag->DispatchEvent(DRAG, drag_parameters);
}
}
hover = GetElementAtPoint(position);
if (!hover ||
hover->GetProperty(CURSOR)->unit == Property::KEYWORD)
active_cursor = default_cursor;
else
SetMouseCursor(hover->GetProperty< String >(CURSOR));
// Build the new hover chain.
ElementSet new_hover_chain;
Element* element = *hover;
while (element != NULL)
{
new_hover_chain.insert(element);
element = element->GetParentNode();
}
// Send mouseout / mouseover events.
SendEvents(hover_chain, new_hover_chain, MOUSEOUT, parameters, true);
SendEvents(new_hover_chain, hover_chain, MOUSEOVER, parameters, true);
// Send out drag events.
if (drag)
{
drag_hover = GetElementAtPoint(position, *drag);
ElementSet new_drag_hover_chain;
element = *drag_hover;
while (element != NULL)
{
new_drag_hover_chain.insert(element);
element = element->GetParentNode();
}
/* if (mouse_moved && !drag_started)
{
drag->DispatchEvent(DRAGSTART, drag_parameters);
drag_started = true;
if (drag->GetProperty< int >(DRAG) == DRAG_CLONE)
{
// Clone the element and attach it to the mouse cursor.
CreateDragClone(*drag);
}
}*/
if (drag_started &&
drag_verbose)
{
// Send out ondragover and ondragout events as appropriate.
SendEvents(drag_hover_chain, new_drag_hover_chain, DRAGOUT, drag_parameters, true);
SendEvents(new_drag_hover_chain, drag_hover_chain, DRAGOVER, drag_parameters, true);
}
drag_hover_chain.swap(new_drag_hover_chain);
}
// Swap the new chain in.
hover_chain.swap(new_hover_chain);
}
// Returns the youngest descendent of the given element which is under the given point in screen coodinates.
Element* Context::GetElementAtPoint(const Vector2f& point, const Element* ignore_element, Element* element)
{
// Update the layout on all documents prior to this call.
for (int i = 0; i < GetNumDocuments(); ++i)
GetDocument(i)->UpdateLayout();
if (element == NULL)
{
if (ignore_element == root)
return NULL;
element = root;
}
// Check if any documents have modal focus; if so, only check down than document.
if (element == root)
{
if (focus)
{
ElementDocument* focus_document = focus->GetOwnerDocument();
if (focus_document != NULL &&
focus_document->IsModal())
{
element = focus_document;
}
}
}
// Check any elements within our stacking context. We want to return the lowest-down element
// that is under the cursor.
if (element->local_stacking_context)
{
if (element->stacking_context_dirty)
element->BuildLocalStackingContext();
for (int i = (int) element->stacking_context.size() - 1; i >= 0; --i)
{
if (ignore_element != NULL)
{
Element* element_hierarchy = element->stacking_context[i];
while (element_hierarchy != NULL)
{
if (element_hierarchy == ignore_element)
break;
element_hierarchy = element_hierarchy->GetParentNode();
}
if (element_hierarchy != NULL)
continue;
}
Element* child_element = GetElementAtPoint(point, ignore_element, element->stacking_context[i]);
if (child_element != NULL)
return child_element;
}
}
// Check if the point is actually within this element.
bool within_element = element->IsPointWithinElement(point);
if (within_element)
{
Vector2i clip_origin, clip_dimensions;
if (ElementUtilities::GetClippingRegion(clip_origin, clip_dimensions, element))
{
within_element = point.x >= clip_origin.x &&
point.y >= clip_origin.y &&
point.x <= (clip_origin.x + clip_dimensions.x) &&
point.y <= (clip_origin.y + clip_dimensions.y);
}
}
if (within_element)
return element;
return NULL;
}
// Creates the drag clone from the given element.
void Context::CreateDragClone(Element* element)
{
if (cursor_proxy == NULL)
{
Log::Message(Log::LT_ERROR, "Unable to create drag clone, no cursor proxy document.");
return;
}
ReleaseDragClone();
// Instance the drag clone.
drag_clone = element->Clone();
if (drag_clone == NULL)
{
Log::Message(Log::LT_ERROR, "Unable to duplicate drag clone.");
return;
}
// Append the clone to the cursor proxy element.
cursor_proxy->AppendChild(drag_clone);
drag_clone->RemoveReference();
// Set the style sheet on the cursor proxy.
cursor_proxy->SetStyleSheet(element->GetStyleSheet());
// Set all the required properties and pseudo-classes on the clone.
drag_clone->SetPseudoClass("drag", true);
drag_clone->SetProperty("position", "absolute");
drag_clone->SetProperty("left", Property(element->GetAbsoluteLeft() - element->GetBox().GetEdge(Box::MARGIN, Box::LEFT) - mouse_position.x, Property::PX));
drag_clone->SetProperty("top", Property(element->GetAbsoluteTop() - element->GetBox().GetEdge(Box::MARGIN, Box::TOP) - mouse_position.y, Property::PX));
}
// Releases the drag clone, if one exists.
void Context::ReleaseDragClone()
{
if (drag_clone != NULL)
{
cursor_proxy->RemoveChild(drag_clone);
drag_clone = NULL;
}
}
// Builds the parameters for a generic key event.
void Context::GenerateKeyEventParameters(Dictionary& parameters, Input::KeyIdentifier key_identifier)
{
parameters.Set("key_identifier", (int) key_identifier);
}
// Builds the parameters for a generic mouse event.
void Context::GenerateMouseEventParameters(Dictionary& parameters, int button_index)
{
parameters.Set("mouse_x", mouse_position.x);
parameters.Set("mouse_y", mouse_position.y);
if (button_index >= 0)
parameters.Set("button", button_index);
}
// Builds the parameters for the key modifier state.
void Context::GenerateKeyModifierEventParameters(Dictionary& parameters, int key_modifier_state)
{
static String property_names[] = {
"ctrl_key",
"shift_key",
"alt_key",
"meta_key",
"caps_lock_key",
"num_lock_key",
"scroll_lock_key"
};
for (int i = 0; i < 7; i++)
parameters.Set(property_names[i], (int) ((key_modifier_state & (1 << i)) > 0));
}
// Builds the parameters for a drag event.
void Context::GenerateDragEventParameters(Dictionary& parameters)
{
parameters.Set("drag_element", (void*) *drag);
}
// Releases all unloaded documents pending destruction.
void Context::ReleaseUnloadedDocuments()
{
if (!unloaded_documents.empty())
{
ElementList documents = unloaded_documents;
unloaded_documents.clear();
// Clear the deleted list.
for (size_t i = 0; i < documents.size(); ++i)
documents[i]->GetEventDispatcher()->DetachAllEvents();
documents.clear();
}
}
// Sends the specified event to all elements in new_items that don't appear in old_items.
void Context::SendEvents(const ElementSet& old_items, const ElementSet& new_items, const String& event, const Dictionary& parameters, bool interruptible)
{
ElementList elements;
std::set_difference(old_items.begin(), old_items.end(), new_items.begin(), new_items.end(), std::back_inserter(elements));
std::for_each(elements.begin(), elements.end(), RKTEventFunctor(event, parameters, interruptible));
}
void Context::OnReferenceDeactivate()
{
if (instancer != NULL)
{
instancer->ReleaseContext(this);
}
}
}
}
|