OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 |
| 7 #include "xfa/fwl/core/ifwl_combolist.h" |
| 8 |
| 9 #include "xfa/fwl/core/ifwl_combobox.h" |
| 10 #include "xfa/fwl/core/ifwl_comboedit.h" |
| 11 |
| 12 // static |
| 13 IFWL_ComboList* IFWL_ComboList::Create( |
| 14 const CFWL_WidgetImpProperties& properties, |
| 15 IFWL_Widget* pOuter) { |
| 16 return new IFWL_ComboList(properties, pOuter); |
| 17 } |
| 18 |
| 19 IFWL_ComboList::IFWL_ComboList(const CFWL_WidgetImpProperties& properties, |
| 20 IFWL_Widget* pOuter) |
| 21 : IFWL_ListBox(properties, pOuter), m_bNotifyOwner(TRUE) { |
| 22 ASSERT(pOuter); |
| 23 } |
| 24 |
| 25 FWL_Error IFWL_ComboList::Initialize() { |
| 26 if (IFWL_ListBox::Initialize() != FWL_Error::Succeeded) |
| 27 return FWL_Error::Indefinite; |
| 28 delete m_pDelegate; |
| 29 m_pDelegate = new CFWL_ComboListImpDelegate(this); |
| 30 return FWL_Error::Succeeded; |
| 31 } |
| 32 |
| 33 FWL_Error IFWL_ComboList::Finalize() { |
| 34 delete m_pDelegate; |
| 35 m_pDelegate = nullptr; |
| 36 return IFWL_ListBox::Finalize(); |
| 37 } |
| 38 |
| 39 int32_t IFWL_ComboList::MatchItem(const CFX_WideString& wsMatch) { |
| 40 if (wsMatch.IsEmpty()) { |
| 41 return -1; |
| 42 } |
| 43 if (!m_pProperties->m_pDataProvider) |
| 44 return -1; |
| 45 IFWL_ListBoxDP* pData = |
| 46 static_cast<IFWL_ListBoxDP*>(m_pProperties->m_pDataProvider); |
| 47 int32_t iCount = pData->CountItems(this); |
| 48 for (int32_t i = 0; i < iCount; i++) { |
| 49 IFWL_ListItem* hItem = pData->GetItem(this, i); |
| 50 CFX_WideString wsText; |
| 51 pData->GetItemText(this, hItem, wsText); |
| 52 FX_STRSIZE pos = wsText.Find(wsMatch.c_str()); |
| 53 if (!pos) { |
| 54 return i; |
| 55 } |
| 56 } |
| 57 return -1; |
| 58 } |
| 59 |
| 60 void IFWL_ComboList::ChangeSelected(int32_t iSel) { |
| 61 if (!m_pProperties->m_pDataProvider) |
| 62 return; |
| 63 IFWL_ListBoxDP* pData = |
| 64 static_cast<IFWL_ListBoxDP*>(m_pProperties->m_pDataProvider); |
| 65 IFWL_ListItem* hItem = pData->GetItem(this, iSel); |
| 66 CFX_RectF rtInvalidate; |
| 67 rtInvalidate.Reset(); |
| 68 IFWL_ListItem* hOld = GetSelItem(0); |
| 69 int32_t iOld = pData->GetItemIndex(this, hOld); |
| 70 if (iOld == iSel) { |
| 71 return; |
| 72 } else if (iOld > -1) { |
| 73 GetItemRect(iOld, rtInvalidate); |
| 74 SetSelItem(hOld, FALSE); |
| 75 } |
| 76 if (hItem) { |
| 77 CFX_RectF rect; |
| 78 GetItemRect(iSel, rect); |
| 79 rtInvalidate.Union(rect); |
| 80 IFWL_ListItem* hSel = pData->GetItem(this, iSel); |
| 81 SetSelItem(hSel, TRUE); |
| 82 } |
| 83 if (!rtInvalidate.IsEmpty()) { |
| 84 Repaint(&rtInvalidate); |
| 85 } |
| 86 } |
| 87 |
| 88 int32_t IFWL_ComboList::CountItems() { |
| 89 IFWL_ListBoxDP* pData = |
| 90 static_cast<IFWL_ListBoxDP*>(m_pProperties->m_pDataProvider); |
| 91 return pData ? pData->CountItems(this) : 0; |
| 92 } |
| 93 |
| 94 void IFWL_ComboList::GetItemRect(int32_t nIndex, CFX_RectF& rtItem) { |
| 95 IFWL_ListBoxDP* pData = |
| 96 static_cast<IFWL_ListBoxDP*>(m_pProperties->m_pDataProvider); |
| 97 IFWL_ListItem* hItem = pData->GetItem(this, nIndex); |
| 98 pData->GetItemRect(this, hItem, rtItem); |
| 99 } |
| 100 |
| 101 void IFWL_ComboList::ClientToOuter(FX_FLOAT& fx, FX_FLOAT& fy) { |
| 102 fx += m_pProperties->m_rtWidget.left, fy += m_pProperties->m_rtWidget.top; |
| 103 IFWL_Widget* pOwner = GetOwner(); |
| 104 if (!pOwner) |
| 105 return; |
| 106 pOwner->TransformTo(m_pOuter, fx, fy); |
| 107 } |
| 108 |
| 109 void IFWL_ComboList::SetFocus(FX_BOOL bSet) { |
| 110 IFWL_Widget::SetFocus(bSet); |
| 111 } |
| 112 |
| 113 CFWL_ComboListImpDelegate::CFWL_ComboListImpDelegate(IFWL_ComboList* pOwner) |
| 114 : CFWL_ListBoxImpDelegate(pOwner), m_pOwner(pOwner) {} |
| 115 |
| 116 void CFWL_ComboListImpDelegate::OnProcessMessage(CFWL_Message* pMessage) { |
| 117 if (!pMessage) |
| 118 return; |
| 119 |
| 120 CFWL_MessageType dwHashCode = pMessage->GetClassID(); |
| 121 FX_BOOL backDefault = TRUE; |
| 122 if (dwHashCode == CFWL_MessageType::SetFocus || |
| 123 dwHashCode == CFWL_MessageType::KillFocus) { |
| 124 OnDropListFocusChanged(pMessage, dwHashCode == CFWL_MessageType::SetFocus); |
| 125 } else if (dwHashCode == CFWL_MessageType::Mouse) { |
| 126 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); |
| 127 if (m_pOwner->IsShowScrollBar(TRUE) && m_pOwner->m_pVertScrollBar) { |
| 128 CFX_RectF rect; |
| 129 m_pOwner->m_pVertScrollBar->GetWidgetRect(rect); |
| 130 if (rect.Contains(pMsg->m_fx, pMsg->m_fy)) { |
| 131 pMsg->m_fx -= rect.left; |
| 132 pMsg->m_fy -= rect.top; |
| 133 IFWL_WidgetDelegate* pDelegate = |
| 134 m_pOwner->m_pVertScrollBar->SetDelegate(nullptr); |
| 135 pDelegate->OnProcessMessage(pMsg); |
| 136 return; |
| 137 } |
| 138 } |
| 139 switch (pMsg->m_dwCmd) { |
| 140 case FWL_MouseCommand::Move: { |
| 141 backDefault = FALSE; |
| 142 OnDropListMouseMove(pMsg); |
| 143 break; |
| 144 } |
| 145 case FWL_MouseCommand::LeftButtonDown: { |
| 146 backDefault = FALSE; |
| 147 OnDropListLButtonDown(pMsg); |
| 148 break; |
| 149 } |
| 150 case FWL_MouseCommand::LeftButtonUp: { |
| 151 backDefault = FALSE; |
| 152 OnDropListLButtonUp(pMsg); |
| 153 break; |
| 154 } |
| 155 default: |
| 156 break; |
| 157 } |
| 158 } else if (dwHashCode == CFWL_MessageType::Key) { |
| 159 backDefault = !OnDropListKey(static_cast<CFWL_MsgKey*>(pMessage)); |
| 160 } |
| 161 if (backDefault) |
| 162 CFWL_ListBoxImpDelegate::OnProcessMessage(pMessage); |
| 163 } |
| 164 |
| 165 void CFWL_ComboListImpDelegate::OnDropListFocusChanged(CFWL_Message* pMsg, |
| 166 FX_BOOL bSet) { |
| 167 if (!bSet) { |
| 168 CFWL_MsgKillFocus* pKill = static_cast<CFWL_MsgKillFocus*>(pMsg); |
| 169 IFWL_ComboBox* pOuter = static_cast<IFWL_ComboBox*>(m_pOwner->m_pOuter); |
| 170 if (pKill->m_pSetFocus == m_pOwner->m_pOuter || |
| 171 pKill->m_pSetFocus == pOuter->m_pEdit.get()) { |
| 172 pOuter->ShowDropList(FALSE); |
| 173 } |
| 174 } |
| 175 } |
| 176 |
| 177 int32_t CFWL_ComboListImpDelegate::OnDropListMouseMove(CFWL_MsgMouse* pMsg) { |
| 178 if (m_pOwner->m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { |
| 179 if (m_pOwner->m_bNotifyOwner) { |
| 180 m_pOwner->m_bNotifyOwner = FALSE; |
| 181 } |
| 182 if (m_pOwner->IsShowScrollBar(TRUE) && m_pOwner->m_pVertScrollBar) { |
| 183 CFX_RectF rect; |
| 184 m_pOwner->m_pVertScrollBar->GetWidgetRect(rect); |
| 185 if (rect.Contains(pMsg->m_fx, pMsg->m_fy)) { |
| 186 return 1; |
| 187 } |
| 188 } |
| 189 IFWL_ListItem* hItem = m_pOwner->GetItemAtPoint(pMsg->m_fx, pMsg->m_fy); |
| 190 if (hItem) { |
| 191 if (!m_pOwner->m_pProperties->m_pDataProvider) |
| 192 return 0; |
| 193 IFWL_ListBoxDP* pData = static_cast<IFWL_ListBoxDP*>( |
| 194 m_pOwner->m_pProperties->m_pDataProvider); |
| 195 int32_t iSel = pData->GetItemIndex(m_pOwner, hItem); |
| 196 CFWL_EvtCmbHoverChanged event; |
| 197 event.m_pSrcTarget = m_pOwner->m_pOuter; |
| 198 event.m_iCurHover = iSel; |
| 199 m_pOwner->DispatchEvent(&event); |
| 200 m_pOwner->ChangeSelected(iSel); |
| 201 } |
| 202 } else if (m_pOwner->m_bNotifyOwner) { |
| 203 m_pOwner->ClientToOuter(pMsg->m_fx, pMsg->m_fy); |
| 204 IFWL_ComboBox* pOuter = static_cast<IFWL_ComboBox*>(m_pOwner->m_pOuter); |
| 205 pOuter->m_pDelegate->OnProcessMessage(pMsg); |
| 206 } |
| 207 return 1; |
| 208 } |
| 209 |
| 210 int32_t CFWL_ComboListImpDelegate::OnDropListLButtonDown(CFWL_MsgMouse* pMsg) { |
| 211 if (m_pOwner->m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { |
| 212 return 0; |
| 213 } |
| 214 IFWL_ComboBox* pOuter = static_cast<IFWL_ComboBox*>(m_pOwner->m_pOuter); |
| 215 pOuter->ShowDropList(FALSE); |
| 216 return 1; |
| 217 } |
| 218 |
| 219 int32_t CFWL_ComboListImpDelegate::OnDropListLButtonUp(CFWL_MsgMouse* pMsg) { |
| 220 IFWL_ComboBox* pOuter = static_cast<IFWL_ComboBox*>(m_pOwner->m_pOuter); |
| 221 if (m_pOwner->m_bNotifyOwner) { |
| 222 m_pOwner->ClientToOuter(pMsg->m_fx, pMsg->m_fy); |
| 223 pOuter->m_pDelegate->OnProcessMessage(pMsg); |
| 224 } else { |
| 225 if (m_pOwner->IsShowScrollBar(TRUE) && m_pOwner->m_pVertScrollBar) { |
| 226 CFX_RectF rect; |
| 227 m_pOwner->m_pVertScrollBar->GetWidgetRect(rect); |
| 228 if (rect.Contains(pMsg->m_fx, pMsg->m_fy)) { |
| 229 return 1; |
| 230 } |
| 231 } |
| 232 pOuter->ShowDropList(FALSE); |
| 233 IFWL_ListItem* hItem = m_pOwner->GetItemAtPoint(pMsg->m_fx, pMsg->m_fy); |
| 234 if (hItem) { |
| 235 pOuter->ProcessSelChanged(TRUE); |
| 236 } |
| 237 } |
| 238 return 1; |
| 239 } |
| 240 |
| 241 int32_t CFWL_ComboListImpDelegate::OnDropListKey(CFWL_MsgKey* pKey) { |
| 242 IFWL_ComboBox* pOuter = static_cast<IFWL_ComboBox*>(m_pOwner->m_pOuter); |
| 243 FX_BOOL bPropagate = FALSE; |
| 244 if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown) { |
| 245 uint32_t dwKeyCode = pKey->m_dwKeyCode; |
| 246 switch (dwKeyCode) { |
| 247 case FWL_VKEY_Return: |
| 248 case FWL_VKEY_Escape: { |
| 249 pOuter->ShowDropList(FALSE); |
| 250 return 1; |
| 251 } |
| 252 case FWL_VKEY_Up: |
| 253 case FWL_VKEY_Down: { |
| 254 OnDropListKeyDown(pKey); |
| 255 pOuter->SetDelegate(nullptr); |
| 256 pOuter->ProcessSelChanged(FALSE); |
| 257 return 1; |
| 258 } |
| 259 default: { bPropagate = TRUE; } |
| 260 } |
| 261 } else if (pKey->m_dwCmd == FWL_KeyCommand::Char) { |
| 262 bPropagate = TRUE; |
| 263 } |
| 264 if (bPropagate) { |
| 265 pKey->m_pDstTarget = m_pOwner->m_pOuter; |
| 266 pOuter->m_pDelegate->OnProcessMessage(pKey); |
| 267 return 1; |
| 268 } |
| 269 return 0; |
| 270 } |
| 271 |
| 272 void CFWL_ComboListImpDelegate::OnDropListKeyDown(CFWL_MsgKey* pKey) { |
| 273 uint32_t dwKeyCode = pKey->m_dwKeyCode; |
| 274 switch (dwKeyCode) { |
| 275 case FWL_VKEY_Up: |
| 276 case FWL_VKEY_Down: |
| 277 case FWL_VKEY_Home: |
| 278 case FWL_VKEY_End: { |
| 279 IFWL_ComboBox* pOuter = static_cast<IFWL_ComboBox*>(m_pOwner->m_pOuter); |
| 280 IFWL_ListBoxDP* pData = static_cast<IFWL_ListBoxDP*>( |
| 281 m_pOwner->m_pProperties->m_pDataProvider); |
| 282 IFWL_ListItem* hItem = pData->GetItem(m_pOwner, pOuter->m_iCurSel); |
| 283 hItem = m_pOwner->GetItem(hItem, dwKeyCode); |
| 284 if (!hItem) { |
| 285 break; |
| 286 } |
| 287 m_pOwner->SetSelection(hItem, hItem, TRUE); |
| 288 m_pOwner->ScrollToVisible(hItem); |
| 289 CFX_RectF rtInvalidate; |
| 290 rtInvalidate.Set(0, 0, m_pOwner->m_pProperties->m_rtWidget.width, |
| 291 m_pOwner->m_pProperties->m_rtWidget.height); |
| 292 m_pOwner->Repaint(&rtInvalidate); |
| 293 break; |
| 294 } |
| 295 default: |
| 296 break; |
| 297 } |
| 298 } |
OLD | NEW |