Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_EditCopyPasteManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUiCtrl_EditCopyPasteManager.cpp
20  * @brief               This is the implementation file for the _EditCopyPasteManager class.
21  */
22
23 #include <appsvc/appsvc.h>
24 #include <FApp_AppControlManager.h>
25 #include <FApp_AppMessageImpl.h>
26 #include <FGrp_BitmapImpl.h>
27 #include "FUi_Clipboard.h"
28 #include "FUi_ClipboardItem.h"
29 #include "FUi_EcoreEvasMgr.h"
30 #include "FUiCtrl_EditCopyPasteManager.h"
31 #include "FUiCtrl_EditCopyPasteEvent.h"
32 #include "FUiCtrl_EditPresenter.h"
33 #include "FUiCtrl_Form.h"
34 #include "FUiCtrl_Frame.h"
35
36
37 #if defined(MULTI_WINDOW)
38 #include "FUi_EcoreEvas.h"
39 #include "FUi_Window.h"
40 using namespace Tizen::Ui;
41 #endif
42
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::Graphics;
47 using namespace Tizen::Graphics::_Text;
48 using namespace Tizen::Ui::Animations;
49
50 namespace Tizen { namespace Ui { namespace Controls
51 {
52
53 const int COPY_PASTE_SELECT_ID = 231;
54 const int COPY_PASTE_SELECT_ALL_ID = 232;
55 const int COPY_PASTE_COPY_ID = 233;
56 const int COPY_PASTE_CUT_ID = 234;
57 const int COPY_PASTE_PASTE_ID = 235;
58 const int COPY_PASTE_CLIPBOARD_ID = 236;
59
60 /**
61 * @class        _EditCopyPasteHandler
62 * @brief        This class defines the common behavior for the %_EditCopyPasteHandler.
63 *
64 */
65 class _EditCopyPasteHandler
66         : public _Window
67         , virtual public IEventListener
68         , virtual public _IUiEventListener
69         , virtual public _IUiEventPreviewer
70 {
71
72 // Lifecycle
73 public:
74         /**
75         * This is the default class constructor.
76         *
77         */
78         _EditCopyPasteHandler(const Point& point, int handlerCursorPos, _EditCopyPasteManager* pCopyPasteManager, bool singleHandler, bool leftHandler);
79
80         /**
81         * This is the default class destructor.
82         *
83         */
84         virtual ~_EditCopyPasteHandler(void);
85
86 // Operations
87 public:
88         static _EditCopyPasteHandler* CreateInstanceN(const Point& point, int handlerCursorPos, _EditCopyPasteManager* pCopyPasteManager, bool singleHandler, bool leftHandler = true);
89         int GetHandlerCursorPosition(void) const;
90         void SetHandlerCursorPosition(int handlerCursorPos);
91         void SetLeftHandler(bool set);
92         void AdjustBounds(void);
93 #if defined(MULTI_WINDOW)
94         Bitmap* GetHandlerBitmap(void) const;
95 #endif
96
97 // virtual function
98         virtual void OnDraw(void);
99         virtual bool OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo);
100         virtual bool OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo);
101         virtual bool OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo);
102
103         virtual bool IsActivatedOnOpen(void) const;
104
105 private:
106         _EditCopyPasteHandler(const _EditCopyPasteHandler& value);
107         _EditCopyPasteHandler& operator =(const _EditCopyPasteHandler& value);
108
109 // Attribute
110 private:
111         _VisualElement* __pRoot;
112         Bitmap* __pHandlerBitmap;
113         Bitmap* __pHandlerPressedBitmap;
114         Point __touchPressedPoint;
115         _EditCopyPasteManager* __pCopyPasteManager;
116         bool __leftHandler;
117         int __handlerCursorPos;
118         bool __isTouchPressed;
119         bool __isReverseBitmap;
120 }; // _EditCopyPasteHandler
121
122 #if defined(MULTI_WINDOW)
123 Bitmap*
124 _EditCopyPasteHandler::GetHandlerBitmap(void) const
125 {
126         return __pHandlerBitmap;
127 }
128 #endif
129
130 _EditCopyPasteHandler::_EditCopyPasteHandler(const Point& point, int handlerCursorPos, _EditCopyPasteManager* pCopyPasteManager, bool singleHandler, bool leftHandler)
131         : __pRoot(null)
132         , __pHandlerBitmap(null)
133         , __pHandlerPressedBitmap(null)
134         , __touchPressedPoint(0, 0)
135         , __pCopyPasteManager(pCopyPasteManager)
136         , __leftHandler(leftHandler)
137         , __handlerCursorPos(handlerCursorPos)
138         , __isTouchPressed(false)
139         , __isReverseBitmap(false)
140 {
141         AcquireHandle();
142
143         __pRoot = GetVisualElement();
144         __pRoot->SetSurfaceOpaque(false);
145
146         Point handlerPoint(point);
147
148         if (singleHandler)
149         {
150                 GET_BITMAP_CONFIG_N(EDIT::COPY_PASTE_ICON_LEFT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBitmap);
151                 handlerPoint.x = handlerPoint.x - (__pHandlerBitmap->GetWidth() / 2);
152         }
153         else
154         {
155                 if (leftHandler)
156                 {
157                         GET_BITMAP_CONFIG_N(EDIT::COPY_PASTE_ICON_LEFT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBitmap);
158                         GET_BITMAP_CONFIG_N(EDIT::COPY_PASTE_ICON_LEFT_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerPressedBitmap);
159                         handlerPoint.x = handlerPoint.x - __pHandlerBitmap->GetWidth();
160                 }
161                 else
162                 {
163                         GET_BITMAP_CONFIG_N(EDIT::COPY_PASTE_ICON_RIGHT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBitmap);
164                         GET_BITMAP_CONFIG_N(EDIT::COPY_PASTE_ICON_RIGHT_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerPressedBitmap);
165                 }
166         }
167
168         Rectangle bounds(handlerPoint.x, handlerPoint.y, __pHandlerBitmap->GetWidth(), __pHandlerBitmap->GetHeight());
169 #if !defined(MULTI_WINDOW)
170         SetBounds(bounds);
171         _Window::Open(false);
172 #endif
173         _Edit* pEdit = __pCopyPasteManager->GetEdit();
174         SysTryReturnVoidResult(NID_UI_CTRL, pEdit, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
175         
176         _EditPresenter* pEditPresenter = pEdit->GetPresenter();
177         SysTryReturnVoidResult(NID_UI_CTRL, pEdit, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
178
179         _Form* pParentForm = pEditPresenter->GetParentForm();
180         if (pParentForm)
181         {
182                 SetOwner(pParentForm);
183         }
184         else
185         {
186                 _Form* pForm = null;
187                 _Frame* pCurrentFrame = dynamic_cast <_Frame*>(_ControlManager::GetInstance()->GetCurrentFrame());
188                 if (pCurrentFrame)
189                 {
190                         pForm = pCurrentFrame->GetCurrentForm();
191                         if (pForm)
192                         {
193                                 SetOwner(pForm);
194                         }
195                         else
196                         {
197                                 SetOwner(pCurrentFrame);
198                         }
199                 }
200         }
201 }
202
203 _EditCopyPasteHandler::~_EditCopyPasteHandler(void)
204 {
205         if (__pHandlerBitmap)
206         {
207                 delete __pHandlerBitmap;
208                 __pHandlerBitmap = null;
209         }
210         if (__pHandlerPressedBitmap)
211         {
212                 delete __pHandlerPressedBitmap;
213                 __pHandlerPressedBitmap = null;
214         }
215 }
216
217 _EditCopyPasteHandler*
218 _EditCopyPasteHandler::CreateInstanceN(const Point& point, int handlerCursorPos, _EditCopyPasteManager* pCopyPasteManager, bool singleHandler, bool leftHandler)
219 {
220         _EditCopyPasteHandler* pCopyPasteHandler = new (std::nothrow) _EditCopyPasteHandler(point, handlerCursorPos, pCopyPasteManager, singleHandler, leftHandler);
221         SysTryReturn(NID_UI_CTRL, pCopyPasteHandler != null, null, E_OUT_OF_MEMORY, "pContextMenu is null");
222
223 #if defined(MULTI_WINDOW)
224         Point handlerPoint(point);
225         Rectangle bounds(0, 0, 0, 0);
226         Bitmap* pHandlerBitmap = pCopyPasteHandler->GetHandlerBitmap();
227
228         result r = pCopyPasteHandler->CreateRootVisualElement();
229         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
230
231         pCopyPasteHandler->SetActivationEnabled(false);
232
233         if (singleHandler)
234         {
235                 handlerPoint.x = handlerPoint.x - (pHandlerBitmap->GetWidth() / 2);
236         }
237         else
238         {
239                 if (leftHandler)
240                 {
241                         handlerPoint.x = handlerPoint.x - pHandlerBitmap->GetWidth();
242                 }
243         }
244
245         bounds.x = handlerPoint.x;
246         bounds.y = handlerPoint.y;
247         bounds.width = pHandlerBitmap->GetWidth();
248         bounds.height = pHandlerBitmap->GetHeight();
249
250         pCopyPasteHandler->SetBounds(bounds);
251
252         pCopyPasteHandler->Open(false);
253 #endif
254
255         return pCopyPasteHandler;
256
257 #if defined(MULTI_WINDOW)
258 CATCH:
259         pCopyPasteHandler->Close();
260         delete pCopyPasteHandler;
261
262         return null;
263 #endif
264 }
265
266 int
267 _EditCopyPasteHandler::GetHandlerCursorPosition(void) const
268 {
269         return __handlerCursorPos;
270 }
271
272 void
273 _EditCopyPasteHandler::SetHandlerCursorPosition(int handlerCursorPos)
274 {
275         __handlerCursorPos = handlerCursorPos;
276 }
277
278 void
279 _EditCopyPasteHandler::SetLeftHandler(bool set)
280 {
281         __leftHandler = set;
282 }
283
284 void
285 _EditCopyPasteHandler::AdjustBounds(void)
286 {
287         int previousCursorPos;
288         Rectangle cursorRect;
289         Rectangle rect = GetBounds();
290
291         previousCursorPos = __pCopyPasteManager->GetCursorPosition();
292         __pCopyPasteManager->SetCursorPosition(__handlerCursorPos);
293         cursorRect = __pCopyPasteManager->GetCursorBounds(true);
294         __pCopyPasteManager->SetCursorPosition(previousCursorPos);
295
296         rect.x = cursorRect.x;
297         rect.y = cursorRect.y + cursorRect.height;
298
299         if (__leftHandler)
300         {
301                 rect.x = rect.x - __pHandlerBitmap->GetWidth();
302         }
303
304         SetBounds(rect);
305 }
306
307 void
308 _EditCopyPasteHandler::OnDraw(void)
309 {
310         Canvas* pCanvas = GetCanvasN();
311         if (pCanvas == null)
312         {
313                 return;
314         }
315         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
316         pCanvas->Clear();
317         if (__isTouchPressed)
318         {
319                 if (__pHandlerPressedBitmap->IsNinePatchedBitmap())
320                 {
321                         pCanvas->DrawNinePatchedBitmap(pCanvas->GetBounds(), *__pHandlerPressedBitmap);
322                 }
323                 else
324                 {
325                         pCanvas->DrawBitmap(pCanvas->GetBounds(), *__pHandlerPressedBitmap);
326                 }
327         }
328         else
329         {
330                 if (__pHandlerBitmap->IsNinePatchedBitmap())
331                 {
332                         pCanvas->DrawNinePatchedBitmap(pCanvas->GetBounds(), *__pHandlerBitmap);
333                 }
334                 else
335                 {
336                         pCanvas->DrawBitmap(pCanvas->GetBounds(), *__pHandlerBitmap);
337                 }
338         }
339
340         delete pCanvas;
341 }
342
343 bool
344 _EditCopyPasteHandler::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
345 {
346         __pCopyPasteManager->ReleaseCopyPastePopup();
347         __touchPressedPoint = touchinfo.GetCurrentPosition();
348         __isTouchPressed = true;
349         Invalidate();
350         return true;
351 }
352
353 bool
354 _EditCopyPasteHandler::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
355 {
356         __isTouchPressed = false;
357         __pCopyPasteManager->CreateCopyPastePopup();
358         __pCopyPasteManager->Show();
359         __pCopyPasteManager->SendTextBlockEvent();
360         Invalidate();
361         return true;
362 }
363
364 bool
365 _EditCopyPasteHandler::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
366 {
367         int cursorPos;
368         int previousCursorPos;
369         Rectangle cursorRect;
370         Rectangle absCursorRect;
371         Rectangle rect = GetBounds();
372         Point point = touchinfo.GetCurrentPosition();
373         Point touchPoint;
374
375         _Edit* pEdit = __pCopyPasteManager->GetEdit();
376         SysTryReturn(NID_UI_CTRL, pEdit, false, E_INVALID_STATE, "[E_INVALID_STATE] pEdit is null.\n");
377
378         _EditPresenter* pEditPresenter = pEdit->GetPresenter();
379         SysTryReturn(NID_UI_CTRL, pEditPresenter, false, E_INVALID_STATE, "[E_INVALID_STATE] pEditPresenter is null.\n");
380
381         if (__touchPressedPoint.x == point.x && __touchPressedPoint.y == point.y)
382         {
383                 return true;
384         }
385
386         previousCursorPos = __pCopyPasteManager->GetCursorPosition();
387         __pCopyPasteManager->SetCursorPosition(__handlerCursorPos);
388         cursorRect = __pCopyPasteManager->GetCursorBounds(false);
389         absCursorRect = __pCopyPasteManager->GetCursorBounds(true);
390         touchPoint.x = cursorRect.x + (point.x - __touchPressedPoint.x);
391         touchPoint.y = cursorRect.y + cursorRect.height/2 + (point.y - __touchPressedPoint.y);
392         cursorPos = __pCopyPasteManager->GetCursorPositionAt(touchPoint);
393
394         if (cursorPos == -1 || (!__pCopyPasteManager->CheckHandleBounds(Point(absCursorRect.x, absCursorRect.y + absCursorRect.height))))
395         {
396                 TextObject* pTextObject = pEditPresenter->GetTextObject();
397                 int curCursorLine = pTextObject->GetLineIndexAtTextIndex(__handlerCursorPos);
398                 int totalLine = pTextObject->GetTotalLineCount();
399                 Rectangle editVisibleArea = __pCopyPasteManager->GetEditVisibleArea();
400
401                 if (cursorPos != -1)
402                 {
403                         if (editVisibleArea.y <= absCursorRect.y) // DownSide
404                         {
405                                 if (curCursorLine < totalLine - 1)
406                                 {
407                                         int offset = __handlerCursorPos - pTextObject->GetFirstTextIndexAt(curCursorLine);
408                                         int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine+1);
409                                         cursorPos = offset + firstTextIndex;
410                                         int textLength = pTextObject->GetTextLengthAt(curCursorLine+1);
411                                         if (offset > textLength)
412                                         {
413                                                 cursorPos = firstTextIndex+textLength;
414                                         }
415
416                                         if (cursorPos != -1 && __leftHandler)
417                                         {
418                                                 int line = pTextObject->GetFirstDisplayLineIndex();
419                                                 pTextObject->SetFirstDisplayLineIndex(line+1);
420                                                 __pCopyPasteManager->SetCursorPosition(cursorPos);
421                                                 absCursorRect = __pCopyPasteManager->GetCursorBounds(true);
422                                                 int firstDisplayY = pTextObject->GetFirstDisplayPositionY();
423                                                 int gapY = absCursorRect.y + absCursorRect.height - (editVisibleArea.y + editVisibleArea.height);
424                                                 int tempMargin = 2;
425                                                 if (gapY > 0)
426                                                 {
427                                                         pTextObject->SetFirstDisplayPositionY(firstDisplayY + gapY + tempMargin);
428                                                 }
429                                         }
430                                 }
431                         }
432                         else
433                         {
434                                 if (curCursorLine !=0 )
435                                 {
436                                         int offset = __handlerCursorPos - pTextObject->GetFirstTextIndexAt(curCursorLine);
437                                         int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine-1);
438                                         cursorPos = offset + firstTextIndex;
439                                         int textLength = pTextObject->GetTextLengthAt(curCursorLine-1);
440                                         if (offset > textLength)
441                                         {
442                                                 cursorPos = firstTextIndex+textLength;
443                                         }
444
445                                         if (cursorPos != -1 && __leftHandler)
446                                         {
447                                                 int line = pTextObject->GetFirstDisplayLineIndex();
448                                                 if (__leftHandler)
449                                                 {
450                                                         pTextObject->SetFirstDisplayLineIndexFromTextIndex(cursorPos);
451                                                 }
452                                                 else
453                                                 {
454                                                         pTextObject->SetFirstDisplayLineIndex(line-1);
455                                                 }
456                                         }
457                                 }
458                         }
459                 }
460                 else
461                 {
462                         if (pEdit->GetEditStyle() & EDIT_STYLE_SINGLE_LINE)
463                         {
464                                 if (point.x - __touchPressedPoint.x >= 0) // RightSide
465                                 {
466                                         if (__handlerCursorPos == pEditPresenter->GetTextLength())
467                                         {
468                                                 cursorPos = __handlerCursorPos;
469                                         }
470                                         else
471                                         {
472                                                 cursorPos = __handlerCursorPos + 1;
473
474                                                 if (__leftHandler && (cursorPos == pEditPresenter->GetTextLength()))
475                                                 {
476                                                         cursorPos = __handlerCursorPos;
477                                                 }
478                                         }
479                                 }
480                                 else //LeftSide
481                                 {
482                                         if (__handlerCursorPos != 0)
483                                         {
484                                                 cursorPos = __handlerCursorPos - 1;
485
486                                                 if (__leftHandler)
487                                                 {
488                                                         pTextObject->SetFirstDisplayLineIndexFromTextIndex(cursorPos);
489                                                 }
490                                         }
491                                 }
492                                 
493                                 if (point.y < 0 || (point.y > GetBounds().height))
494                                 {
495                                         return true;
496                                 }
497                         }
498                         else
499                         {
500                                 if (point.y - __touchPressedPoint.y >= 0) // DownSide
501                                 {
502                                         if (curCursorLine < totalLine - 1)
503                                         {
504                                                 int offset = __handlerCursorPos - pTextObject->GetFirstTextIndexAt(curCursorLine);
505                                                 int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine+1);
506                                                 cursorPos = offset + firstTextIndex;
507                                                 int textLength = pTextObject->GetTextLengthAt(curCursorLine+1);
508                                                 if (offset > textLength)
509                                                 {
510                                                         cursorPos = firstTextIndex+textLength;
511                                                 }
512
513                                                 if (cursorPos != -1 && __leftHandler)
514                                                 {
515                                                         int line = pTextObject->GetFirstDisplayLineIndex();
516                                                         if (__leftHandler)
517                                                         {
518                                                                 pTextObject->SetFirstDisplayLineIndexFromTextIndex(cursorPos);
519                                                         }
520                                                         else
521                                                         {
522                                                                 pTextObject->SetFirstDisplayLineIndex(line+1);
523                                                         }
524                                                 }
525                                         }
526                                 }
527                                 else    //UpSide
528                                 {
529                                         if (curCursorLine !=0 )
530                                         {
531                                                 int offset = __handlerCursorPos - pTextObject->GetFirstTextIndexAt(curCursorLine);
532                                                 int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine-1);
533                                                 cursorPos = offset + firstTextIndex;
534                                                 int textLength = pTextObject->GetTextLengthAt(curCursorLine-1);
535                                                 if (offset > textLength)
536                                                 {
537                                                         cursorPos = firstTextIndex+textLength;
538                                                 }
539
540                                                 if (!__leftHandler && cursorPos <= __pCopyPasteManager->GetHandlerCursorPosition(false))
541                                                 {
542                                                         cursorPos = __pCopyPasteManager->GetHandlerCursorPosition(false) + 1;
543                                                 }
544
545                                                 if (cursorPos != -1 && __leftHandler)
546                                                 {
547                                                         int line = pTextObject->GetFirstDisplayLineIndex();
548                                                         if (__leftHandler)
549                                                         {
550                                                                 pTextObject->SetFirstDisplayLineIndexFromTextIndex(cursorPos);
551                                                         }
552                                                         else
553                                                         {
554                                                                 pTextObject->SetFirstDisplayLineIndex(line-1);
555                                                         }
556                                                 }
557                                         }
558                                 }
559                         }
560                 }
561         }
562
563         if (cursorPos == -1 || __handlerCursorPos == cursorPos ||!__pCopyPasteManager->CheckHandlePosition(__leftHandler, cursorPos))
564         {
565                 __pCopyPasteManager->SetCursorPosition(previousCursorPos);
566                 pEditPresenter->SetCursorChangedFlag(!__leftHandler);
567                 return true;
568         }
569
570         __pCopyPasteManager->SetCursorPosition(cursorPos);
571
572         cursorRect = __pCopyPasteManager->GetCursorBounds(true);
573
574         rect.x = cursorRect.x;
575         rect.y = cursorRect.y + cursorRect.height;
576
577         if (__leftHandler)
578         {
579                 rect.x = rect.x - __pHandlerBitmap->GetWidth();
580         }
581
582         __handlerCursorPos = cursorPos;
583
584         SetBounds(rect);
585
586         __pCopyPasteManager->RefreshBlock(true, __leftHandler);
587
588         return true;
589 }
590
591 bool
592 _EditCopyPasteHandler::IsActivatedOnOpen(void) const
593 {
594         return false;
595 }
596
597 _EditCopyPasteManager::_EditCopyPasteManager(_Edit& parenEdit)
598         : __pCopyPastePopup(null)
599         , __contextMenuAlign(CONTEXT_MENU_CORE_ALIGN_UP)
600         , __pCoreCopyPasteEvent(null)
601         , __pEdit(&parenEdit)
602         , __contextMenuHeight(0)
603         , __needToReleaseBlock(true)
604         , __isHandlerMoving(false)
605         , __editVisibleArea(0, 0, 0, 0)
606 {
607         __pHandle[HANDLER_TYPE_LEFT] = null;
608         __pHandle[HANDLER_TYPE_RIGHT] = null;
609
610         if (__pEdit->IsBlocked())
611         {
612                 CreateHandle();
613         }
614 }
615
616 _EditCopyPasteManager::~_EditCopyPasteManager(void)
617 {
618         Release();
619
620         if (__pCoreCopyPasteEvent)
621         {
622                 delete __pCoreCopyPasteEvent;
623                 __pCoreCopyPasteEvent = null;
624         }
625 }
626
627 void
628 _EditCopyPasteManager::Release(void)
629 {
630         if (__pCopyPastePopup)
631         {
632                 __pCopyPastePopup->Close();
633                 delete __pCopyPastePopup;
634                 __pCopyPastePopup = null;
635         }
636
637         if (__pHandle[HANDLER_TYPE_LEFT])
638         {
639                 __pHandle[HANDLER_TYPE_LEFT]->Close();
640                 delete __pHandle[HANDLER_TYPE_LEFT];
641                 __pHandle[HANDLER_TYPE_LEFT] = null;
642         }
643
644         if (__pHandle[HANDLER_TYPE_RIGHT])
645         {
646                 __pHandle[HANDLER_TYPE_RIGHT]->Close();
647                 delete __pHandle[HANDLER_TYPE_RIGHT];
648                 __pHandle[HANDLER_TYPE_RIGHT] = null;
649         }
650 }
651
652 void
653 _EditCopyPasteManager::ReleaseCopyPastePopup(void)
654 {
655         if (__pCopyPastePopup)
656         {
657                 __pCopyPastePopup->Close();
658                 delete __pCopyPastePopup;
659                 __pCopyPastePopup = null;
660         }
661 }
662
663 bool
664 _EditCopyPasteManager::IsCopyPastePopup(const _Control& control) const
665 {
666         if (__pHandle[HANDLER_TYPE_LEFT] == &control)
667         {
668                 return true;
669         }
670
671         if (__pHandle[HANDLER_TYPE_RIGHT] == &control)
672         {
673                 return true;
674         }
675
676         if (__pCopyPastePopup == &control)
677         {
678                 return true;
679         }
680
681         return false;
682 }
683
684 result
685 _EditCopyPasteManager::AddCopyPasteEventListener(const _IEditCopyPasteEventListener& listener)
686 {
687         ClearLastResult();
688
689         if (__pCoreCopyPasteEvent == null)
690         {
691                 __pCoreCopyPasteEvent = _EditCopyPasteEvent::CreateInstanceN(*this);
692                 SysTryReturn(NID_UI_CTRL, __pCoreCopyPasteEvent, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.\n");
693         }
694
695         return __pCoreCopyPasteEvent->AddListener(listener);
696 }
697
698 result
699 _EditCopyPasteManager::SendCopyPasteEvent(CoreCopyPasteStatus status, CoreCopyPasteAction action)
700 {
701         if (__pCoreCopyPasteEvent)
702         {
703                 IEventArg* pCopyPasteEventArg = _EditCopyPasteEvent::CreateCopyPasteEventArgN(status, action);
704                 SysTryReturn(NID_UI_CTRL, pCopyPasteEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create _LinkEventArg.");
705
706                 __pCoreCopyPasteEvent->Fire(*pCopyPasteEventArg);
707         }
708
709         return E_SUCCESS;
710 }
711
712 void
713 _EditCopyPasteManager::CreateCopyPastePopup(void)
714 {
715         if (__pCopyPastePopup)
716         {
717                 __pCopyPastePopup->Close();
718                 delete __pCopyPastePopup;
719                 __pCopyPastePopup = null;
720         }
721
722         Rectangle startRect;
723         Rectangle endRect;
724         Rectangle editAbsRect;
725         Rectangle editShowAreaAbsRect;
726         Rectangle commandButtonBounds;
727         Rectangle keypadBounds(0, 0, 0, 0);
728         bool keypadExist = false;
729         bool commandButtonExist = false;
730         Rectangle cursorRect = GetCursorBounds(true);
731
732         int contextMenuHeight = 0;
733         int contextMenuTopMargin = 0;
734         int contextMenuBottomMargin = 0;
735         int contextMenuArrowHeight = 0;
736         int contextMenuLeftMargin = 0;
737         int contextMenuArrowWidth = 0;
738         int handlerHeight = 0;
739         bool isPasswordStyle = false;
740         if (__pEdit->GetEditStyle() & EDIT_STYLE_PASSWORD)
741         {
742                 isPasswordStyle = true;
743         }
744
745         _ControlOrientation orientation = __pEdit->GetOrientation();
746
747         GET_SHAPE_CONFIG(EDIT::COPYPASTE_HANDLER_HEIGHT, orientation, handlerHeight);
748
749         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_HEIGHT, orientation, contextMenuHeight);
750         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_TOP_MARGIN, orientation, contextMenuTopMargin);
751         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_BOTTOM_MARGIN, orientation, contextMenuBottomMargin);
752         GET_SHAPE_CONFIG(CONTEXTMENU::ARROW_HEIGHT, orientation, contextMenuArrowHeight);
753         GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_LEFT_MARGIN, orientation, contextMenuLeftMargin);
754         GET_SHAPE_CONFIG(CONTEXTMENU::ARROW_WIDTH, orientation, contextMenuArrowWidth);
755
756         __contextMenuHeight = contextMenuHeight + contextMenuTopMargin + contextMenuBottomMargin + contextMenuArrowHeight;
757         if (__pEdit->IsClipped() == true && __pEdit->IsViewModeEnabled() == false && __pEdit->GetTextLength() > 0 && orientation == _CONTROL_ORIENTATION_PORTRAIT)
758         {
759                 __contextMenuHeight = __contextMenuHeight + contextMenuHeight;
760         }
761
762         editAbsRect = __pEdit->GetAbsoluteBounds();
763         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
764         _Toolbar* pCommandButton = pEditPresenter->GetKeypadCommandButton();
765         if (pCommandButton)
766         {
767                 commandButtonBounds = pCommandButton->GetAbsoluteBounds();
768                 commandButtonExist = true;
769         }
770         __pEdit->GetKeypadBounds(keypadBounds);
771         Dimension screenSize(0, 0);
772         screenSize = _ControlManager::GetInstance()->GetScreenSize();
773         if (keypadBounds.y != screenSize.height)
774         {
775                 keypadExist = true;
776         }
777
778         // Edit Show Area
779         {
780                 editShowAreaAbsRect = editAbsRect;
781
782                 if (commandButtonExist)
783                 {
784                         if (editShowAreaAbsRect.y + editShowAreaAbsRect.height > commandButtonBounds.y)
785                         {
786                                 editShowAreaAbsRect.height -= (editShowAreaAbsRect.y + editShowAreaAbsRect.height - commandButtonBounds.y);
787                         }
788                 }
789                 else
790                 {
791                         if (editShowAreaAbsRect.y + editShowAreaAbsRect.height > keypadBounds.y)
792                         {
793                                 editShowAreaAbsRect.height -= (editShowAreaAbsRect.y + editShowAreaAbsRect.height - keypadBounds.y);
794                         }
795                 }
796                 __editVisibleArea = editShowAreaAbsRect;
797         }
798         Point copyPastePoint(0, 0);
799         __contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_UP;
800
801         if (__pEdit->IsBlocked())
802         {
803                 int start = -1;
804                 int end = -1;
805                 __pEdit->GetBlockRange(start, end);
806                 if (start == -1 || end == -1)
807                 {
808                         SysLog(NID_UI_CTRL, "[EditCopyPasteManager] There is no blocked Range");
809                         return;
810                 }
811
812                 __pEdit->CalculateAbsoluteCursorBounds(start, startRect);
813                 __pEdit->CalculateAbsoluteCursorBounds(end, endRect);
814                 if (startRect.x > endRect.x)
815                 {
816                         copyPastePoint.x = endRect.x + (startRect.x - endRect.x)/2;
817                 }
818                 else if (startRect.x < endRect.x)
819                 {
820                         copyPastePoint.x = startRect.x + (endRect.x - startRect.x)/2;
821                 }
822                 else
823                 {
824                         copyPastePoint.x = startRect.x;
825                 }
826                 //Both of handlers are located on the top of the Editor.
827                 if ( ((startRect.y + startRect.height) < editShowAreaAbsRect.y) && ((endRect.y + endRect.height) < editShowAreaAbsRect.y))
828                 {
829                         SysLog(NID_UI_CTRL, "Both of handlers are located on the top of the Editor.\n");
830                         PrintLog(copyPastePoint);
831                         return;
832                 }
833                 //Both of handlers are located on the bottom of the Editor.
834                 else if( ((startRect.y + startRect.height) > editShowAreaAbsRect.y + editShowAreaAbsRect.height ) && ((endRect.y + endRect.height) > editShowAreaAbsRect.y + editShowAreaAbsRect.height))
835                 {
836                         SysLog(NID_UI_CTRL, "Both of handlers are located on the bottom of the Editor.\n");
837                         PrintLog(copyPastePoint);
838                         return;
839                 }
840                 // Left handler is located on the top of the Editor and Right handler is located on the bottom of the Editor.
841                 else if ( ((startRect.y + startRect.height) < editShowAreaAbsRect.y) && ((endRect.y + endRect.height) > (editShowAreaAbsRect.y + editShowAreaAbsRect.height)))
842                 {
843                         copyPastePoint.y = editShowAreaAbsRect.y + editShowAreaAbsRect.height/2;
844
845                         if (copyPastePoint.y < __contextMenuHeight)
846                         {
847                                 __contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_DOWN;
848                         }
849
850                 }
851                 // Left handler is located on the top of the Editor and Right handler is located on the Editor
852                 else if ( ((startRect.y + startRect.height) < editShowAreaAbsRect.y) && ((endRect.y + endRect.height) < (editShowAreaAbsRect.y + editShowAreaAbsRect.height)) )
853                 {
854                         if ( (endRect.y + endRect.height + handlerHeight + __contextMenuHeight) < keypadBounds.y)
855                         {
856                                 __contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_DOWN;
857                                 copyPastePoint.y = endRect.y + endRect.height + handlerHeight;
858                         }
859                         else if (endRect.y - editShowAreaAbsRect.y > __contextMenuHeight)
860                         {
861                                 copyPastePoint.y = editShowAreaAbsRect.y + __contextMenuHeight;
862                         }
863                         else if (endRect.y > __contextMenuHeight)
864                         {
865                                 copyPastePoint.y = endRect.y;
866                         }
867                         else
868                         {
869                                 SysLog(NID_UI_CTRL, "There is no space to draw the copy&paste popup\n");
870                                 PrintLog(copyPastePoint);
871                                 return;
872                         }
873                 }
874                 // Left handler is located on the Editor and Right handler is located on the bottom of the Editor
875                 else if ( ((startRect.y + startRect.height) > editShowAreaAbsRect.y) && ((endRect.y + endRect.height) > (editShowAreaAbsRect.y + editShowAreaAbsRect.height)) )
876                 {
877                         if (__contextMenuHeight < startRect.y)
878                         {
879                                 copyPastePoint.y = startRect.y;
880                         }
881                         else
882                         {
883                                 copyPastePoint.y = startRect.y + (editShowAreaAbsRect.y + editShowAreaAbsRect.height - startRect.y)/2;
884                         }
885                 }
886                 // There is a  space on the top of the Editor.
887                 else if (__contextMenuHeight < startRect.y)
888                 {
889                         copyPastePoint.y = startRect.y;
890                 }
891                 // There is a  space on the bottom of the Editor.
892                 else if ( keypadBounds.y  > (endRect.y + endRect.height + handlerHeight + __contextMenuHeight))
893                 {
894                         __contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_DOWN;
895
896                         copyPastePoint.y = endRect.y + endRect.height + handlerHeight;
897                 }
898                 // There is no space on the top and bottom of the Editor. The Popup should be drawn on the center of the Editor.
899                 else
900                 {
901                         if (endRect.y - startRect.y < __contextMenuHeight)
902                         {
903                                 SysLog(NID_UI_CTRL, "There is no space to draw the copy&paste popup\n");
904                                 PrintLog(copyPastePoint);
905                                 return;
906                         }
907                         copyPastePoint.y = startRect.y + __contextMenuHeight;
908                 }
909         }
910
911         else
912         {
913                 Rectangle cursorRect;
914                 int cursorPosition = GetCursorPosition();
915                 __pEdit->CalculateAbsoluteCursorBounds(cursorPosition, cursorRect);
916                 copyPastePoint.x = cursorRect.x;
917                 copyPastePoint.y = cursorRect.y;
918                 if (copyPastePoint.y < __contextMenuHeight)
919                 {
920                         Rectangle cursorRect = GetCursorBounds(true);
921
922                         copyPastePoint.y += cursorRect.height;
923                         __contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_DOWN;
924                 }
925         }
926
927         __pCopyPastePopup = _ContextMenu::CreateContextMenuN(copyPastePoint, CONTEXT_MENU_CORE_STYLE_GRID, __contextMenuAlign);
928         SysTryReturnVoidResult(NID_UI_CTRL, __pCopyPastePopup, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
929
930         if ( __pEdit->GetTextLength() != 0  && !isPasswordStyle)
931         {
932                 if (__pEdit->IsBlocked())
933                 {
934                         String copyText;
935                         String cutText;
936
937                         GET_STRING_CONFIG(IDS_COM_BODY_COPY, copyText);
938                         GET_STRING_CONFIG(IDS_COM_BODY_CUT, cutText);
939
940                         __pCopyPastePopup->AddItem(copyText, COPY_PASTE_COPY_ID, null, null, null);
941                         if (!__pEdit->IsViewModeEnabled())
942                         {
943                                 __pCopyPastePopup->AddItem(cutText, COPY_PASTE_CUT_ID, null, null, null);
944                         }
945                 }
946                 else
947                 {
948                         String selectText;
949                         String selectAllText;
950
951                         GET_STRING_CONFIG(IDS_COM_SK_SELECT, selectText);
952                         GET_STRING_CONFIG(IDS_COM_BODY_SELECT_ALL, selectAllText);
953
954                         __pCopyPastePopup->AddItem(selectText, COPY_PASTE_SELECT_ID, null, null, null);
955                         __pCopyPastePopup->AddItem(selectAllText, COPY_PASTE_SELECT_ALL_ID, null, null, null);
956                 }
957         }
958
959         if (__pEdit->IsClipped() == true)
960         {
961                 if (!__pEdit->IsViewModeEnabled())
962                 {
963                         String pasteText;
964                         String clipboardText;
965
966                         GET_STRING_CONFIG(IDS_COM_BODY_PASTE, pasteText);
967                         GET_STRING_CONFIG(IDS_COM_BODY_CLIPBOARD, clipboardText);
968
969                         __pCopyPastePopup->AddItem(pasteText, COPY_PASTE_PASTE_ID, null, null, null);
970                         if (!isPasswordStyle)
971                         {
972                         __pCopyPastePopup->AddItem(clipboardText, COPY_PASTE_CLIPBOARD_ID, null, null, null);
973                         }
974                 }
975         }
976
977         __pCopyPastePopup->AddActionEventListener(*this);
978
979         return;
980 }
981
982 void
983 _EditCopyPasteManager::PrintLog(const Point& point)
984 {
985         Rectangle startRect;
986         Rectangle endRect;
987         Rectangle editAbsRect;
988         Rectangle keypadBounds;
989         Rectangle editShowAreaAbsRect;
990         Rectangle commandButtonBounds;
991         Rectangle formClientBounds(0, 0, 0, 0);
992         bool hasParentForm = false;
993         bool hasCommandButton = false;
994         Rectangle cursorRect = GetCursorBounds(true);
995         int indicatorHeight = 0;
996
997         editAbsRect = __pEdit->GetAbsoluteBounds();
998         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
999         _Toolbar* pCommandButton = pEditPresenter->GetKeypadCommandButton();
1000         if (pCommandButton)
1001         {
1002                 commandButtonBounds = pCommandButton->GetAbsoluteBounds();
1003                 hasCommandButton = true;
1004         }
1005         _Form* pForm = pEditPresenter->GetParentForm();
1006         if (pForm)
1007         {
1008                 formClientBounds = pForm->GetClientBounds();
1009                 hasParentForm = true;
1010         }
1011         int start = -1;
1012         int end = -1;
1013         __pEdit->GetBlockRange(start, end);
1014         if (start == -1 || end == -1)
1015         {
1016                 SysLog(NID_UI_CTRL, "[EditCopyPasteManager] There is no blocked Range");
1017                 return;
1018         }
1019         __pEdit->GetKeypadBounds(keypadBounds);
1020
1021     if (pForm && pForm->IsIndicatorVisible())
1022     {
1023             indicatorHeight = pForm->GetIndicatorBounds().height;
1024     }
1025
1026         {
1027                 editShowAreaAbsRect = editAbsRect;
1028                 if (indicatorHeight)
1029                 {
1030                         if (editShowAreaAbsRect.y < indicatorHeight)
1031                         {
1032                                 editShowAreaAbsRect.y = indicatorHeight;
1033                         }
1034                 }
1035
1036                 if (hasCommandButton)
1037                 {
1038                         if (editShowAreaAbsRect.y + editShowAreaAbsRect.height > commandButtonBounds.y)
1039                         {
1040                                 editShowAreaAbsRect.height -= (editShowAreaAbsRect.y + editShowAreaAbsRect.height - commandButtonBounds.y);
1041                         }
1042                 }
1043                 else
1044                 {
1045                         if (editShowAreaAbsRect.y + editShowAreaAbsRect.height > keypadBounds.y)
1046                         {
1047                                 editShowAreaAbsRect.height -= (editShowAreaAbsRect.y + editShowAreaAbsRect.height - keypadBounds.y);
1048                         }
1049                 }
1050         }
1051
1052         __pEdit->CalculateAbsoluteCursorBounds(start, startRect);
1053         __pEdit->CalculateAbsoluteCursorBounds(end, endRect);
1054
1055         SysLog(NID_UI_CTRL, "copyPastePoint (x=%d), (y=%d)\n", point.x, point.y );
1056         SysLog(NID_UI_CTRL, "startRect (x=%d), (y=%d), (width=%d), (height=%d) \n", startRect.x, startRect.y ,startRect.width, startRect.height);
1057         SysLog(NID_UI_CTRL, "endRect (x=%d), (y=%d), (width=%d), (height=%d) \n", endRect.x, endRect.y ,endRect.width, endRect.height);
1058         SysLog(NID_UI_CTRL, "editAbsRect (x=%d), (y=%d), (width=%d), (height=%d) \n", editAbsRect.x, editAbsRect.y ,editAbsRect.width, editAbsRect.height);
1059         SysLog(NID_UI_CTRL, "editShowAreaAbsRect (x=%d), (y=%d), (width=%d), (height=%d) \n", editShowAreaAbsRect.x, editShowAreaAbsRect.y ,editShowAreaAbsRect.width, editShowAreaAbsRect.height);
1060         SysLog(NID_UI_CTRL, "keypadBounds (x=%d), (y=%d), (width=%d), (height=%d) \n", keypadBounds.x, keypadBounds.y ,keypadBounds.width, keypadBounds.height);
1061         SysLog(NID_UI_CTRL, "commandButtonBounds (x=%d), (y=%d), (width=%d), (height=%d) \n", commandButtonBounds.x, commandButtonBounds.y ,commandButtonBounds.width, commandButtonBounds.height);
1062         SysLog(NID_UI_CTRL, "formClientBounds (x=%d), (y=%d), (width=%d), (height=%d) \n", formClientBounds.x, formClientBounds.y ,formClientBounds.width, formClientBounds.height);
1063         SysLog(NID_UI_CTRL, "contextMenuHeight = %d\n", __contextMenuHeight);
1064 }
1065
1066 void
1067 _EditCopyPasteManager::CreateHandle(void)
1068 {
1069         int start = -1;
1070         int end = -1;
1071         Rectangle startRect;
1072         Rectangle endRect;
1073
1074         __pEdit->GetBlockRange(start, end);
1075
1076         __pEdit->CalculateAbsoluteCursorBounds(start, startRect);
1077         __pEdit->CalculateAbsoluteCursorBounds(end, endRect);
1078
1079         Point leftHandler(startRect.x, startRect.y + startRect.height);
1080         Point rightHandler(endRect.x, endRect.y + endRect.height);
1081
1082         __pHandle[HANDLER_TYPE_LEFT] = _EditCopyPasteHandler::CreateInstanceN(leftHandler, start, this, false, true);
1083         __pHandle[HANDLER_TYPE_RIGHT] = _EditCopyPasteHandler::CreateInstanceN(rightHandler, end, this, false, false);
1084 }
1085
1086 void
1087 _EditCopyPasteManager::Show(void)
1088 {
1089         bool hasParentForm = false;
1090         Rectangle formClientBounds(0, 0, 0, 0);
1091         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
1092
1093         _Form* pForm = pEditPresenter->GetParentForm();
1094         if (pForm)
1095         {
1096                 formClientBounds = pForm->GetClientBounds();
1097                 hasParentForm = true;
1098         }
1099
1100         if (__pHandle[HANDLER_TYPE_LEFT] && __pHandle[HANDLER_TYPE_RIGHT])
1101         {
1102                 CheckVisibleState(true);
1103                 CheckVisibleState(false);
1104         }
1105
1106         if (__pCopyPastePopup)
1107         {
1108                 __pCopyPastePopup->Open();
1109                 __pCopyPastePopup->ReleaseTouchCapture();
1110
1111                 _Control* pControl = __pCopyPastePopup->GetOwner();
1112                 if (pControl)
1113                 {
1114                         pControl->SetInputEnableState(true);
1115                 }
1116         }
1117 }
1118
1119 bool
1120 _EditCopyPasteManager::CheckHandleBounds(const Point& point)
1121 {
1122         bool hasCommandButton = false;
1123         bool hasParentForm = false;
1124         bool showCheck = true;
1125         Rectangle commandButtonBounds(0, 0, 0, 0);
1126         Rectangle formClientBounds(0, 0, 0, 0);
1127         Rectangle editAbsBounds = __pEdit->GetAbsoluteBounds();
1128         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
1129         _Toolbar* pCommandButton = pEditPresenter->GetKeypadCommandButton();
1130         if (pCommandButton)
1131         {
1132                 commandButtonBounds = pCommandButton->GetAbsoluteBounds();
1133                 hasCommandButton = true;
1134         }
1135         _Form* pForm = pEditPresenter->GetParentForm();
1136         if (pForm)
1137         {
1138                 formClientBounds = pForm->GetClientBounds();
1139                 hasParentForm = true;
1140         }
1141
1142         if (editAbsBounds.x > point.x || point.x > (editAbsBounds.x + editAbsBounds.width))
1143         {
1144                 showCheck = false;
1145         }
1146
1147         if (editAbsBounds.y > point.y || point.y > (editAbsBounds.y + editAbsBounds.height))
1148         {
1149                 showCheck = false;
1150         }
1151         if (hasParentForm && (formClientBounds.y > point.y || point.y > (formClientBounds.y + formClientBounds.height)))
1152         {
1153                 showCheck = false;
1154         }
1155         if (hasCommandButton && commandButtonBounds.Contains(point))
1156         {
1157                 showCheck = false;
1158         }
1159         return showCheck;
1160 }
1161
1162 bool
1163 _EditCopyPasteManager::CheckHandlePosition(bool leftHandle, int cursorPosition)
1164 {
1165         int leftHandlerPosition = __pHandle[HANDLER_TYPE_LEFT]->GetHandlerCursorPosition();
1166         int rightHandlerPosition = __pHandle[HANDLER_TYPE_RIGHT]->GetHandlerCursorPosition();
1167
1168         if (leftHandle)
1169         {
1170                 if (cursorPosition + 1 <= rightHandlerPosition)
1171                 {
1172                         return true;
1173                 }
1174                 else
1175                 {
1176                         return false;
1177                 }
1178         }
1179         else
1180         {
1181                 if (cursorPosition >= leftHandlerPosition + 1)
1182                 {
1183                         return true;
1184                 }
1185                 else
1186                 {
1187                         return false;
1188                 }
1189         }
1190 }
1191
1192 void
1193 _EditCopyPasteManager::CheckVisibleState(bool leftHandle)
1194 {
1195         if (leftHandle)
1196         {
1197                 Point leftHandleXY(__pHandle[HANDLER_TYPE_LEFT]->GetAbsoluteBounds().x + __pHandle[HANDLER_TYPE_LEFT]->GetAbsoluteBounds().width, __pHandle[HANDLER_TYPE_LEFT]->GetAbsoluteBounds().y);
1198                 if (!CheckHandleBounds(leftHandleXY))
1199                 {
1200                         __pHandle[HANDLER_TYPE_LEFT]->SetVisibleState(false);
1201                 }
1202                 else
1203                 {
1204                         __pHandle[HANDLER_TYPE_LEFT]->SetVisibleState(true);
1205                         __pHandle[HANDLER_TYPE_LEFT]->Open();
1206                 }
1207         }
1208         else
1209         {
1210                 Point rightHandleXY(__pHandle[HANDLER_TYPE_RIGHT]->GetAbsoluteBounds().x, __pHandle[HANDLER_TYPE_RIGHT]->GetAbsoluteBounds().y);
1211                 if (!CheckHandleBounds(rightHandleXY))
1212                 {
1213                         __pHandle[HANDLER_TYPE_RIGHT]->SetVisibleState(false);
1214                 }
1215                 else
1216                 {
1217                         __pHandle[HANDLER_TYPE_RIGHT]->SetVisibleState(true);
1218                         __pHandle[HANDLER_TYPE_RIGHT]->Open();
1219                 }
1220         }
1221 }
1222
1223 void
1224 _EditCopyPasteManager::HideHandle(void)
1225 {
1226         __pEdit->ReleaseTextBlock();
1227         if (__pHandle[HANDLER_TYPE_LEFT])
1228         {
1229                 __pHandle[HANDLER_TYPE_LEFT]->SetVisibleState(false);
1230         }
1231         if (__pHandle[HANDLER_TYPE_RIGHT])
1232         {
1233                 __pHandle[HANDLER_TYPE_RIGHT]->SetVisibleState(false);
1234         }
1235 }
1236
1237 void
1238 _EditCopyPasteManager::RefreshBlock(bool changeVisibleState, bool isLeftHandle)
1239 {
1240         int leftHandlerPos = __pHandle[HANDLER_TYPE_LEFT]->GetHandlerCursorPosition();
1241         int rightHandlerPos = __pHandle[HANDLER_TYPE_RIGHT]->GetHandlerCursorPosition();
1242
1243         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
1244         SysTryReturnVoidResult(NID_UI_CTRL, pEditPresenter, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1245
1246         if (!isLeftHandle)
1247         {
1248                 pEditPresenter->ScrollPanelToCursorPosition();
1249         }
1250         else
1251         {
1252                 pEditPresenter->SetCursorPosition(leftHandlerPos);
1253                 pEditPresenter->ScrollPanelToCursorPosition();
1254                 pEditPresenter->SetCursorPosition(rightHandlerPos);
1255         }
1256         __pEdit->SetBlockRange(leftHandlerPos, rightHandlerPos);
1257         pEditPresenter->SetCursorChangedFlag(!isLeftHandle);
1258         pEditPresenter->DrawText();
1259         
1260         __pHandle[HANDLER_TYPE_LEFT]->AdjustBounds();
1261         __pHandle[HANDLER_TYPE_RIGHT]->AdjustBounds();
1262
1263         if (changeVisibleState)
1264         {
1265                 CheckVisibleState(!isLeftHandle);
1266         }
1267 }
1268
1269 Rectangle
1270 _EditCopyPasteManager::GetCursorBounds(bool isAbsRect) const
1271 {
1272         Rectangle cursorBounds;
1273
1274         __pEdit->GetCursorBounds(isAbsRect, cursorBounds);
1275
1276         return cursorBounds;
1277 }
1278
1279 int
1280 _EditCopyPasteManager::GetCursorPositionAt(const Point& touchPoint) const
1281 {
1282         return __pEdit->GetCursorPositionAt(touchPoint);
1283 }
1284
1285 result
1286 _EditCopyPasteManager::SetCursorPosition(int position)
1287 {
1288         return __pEdit->SetCursorPosition(position);
1289 }
1290
1291 int
1292 _EditCopyPasteManager::GetCursorPosition(void) const
1293 {
1294         return __pEdit->GetCursorPosition();
1295 }
1296
1297 int
1298 _EditCopyPasteManager::GetHandlerCursorPosition(bool rightHandler) const
1299 {
1300         return __pHandle[rightHandler]->GetHandlerCursorPosition();
1301 }
1302
1303 void
1304 _EditCopyPasteManager::SendTextBlockEvent(void)
1305 {
1306         int start = -1;
1307         int end = -1;
1308         result r = E_SUCCESS;
1309
1310         __pEdit->GetBlockRange(start, end);
1311         if (start != -1 && end != -1)
1312         {
1313                 r = __pEdit->SendTextBlockEvent(start, end);
1314                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1315         }
1316 }
1317
1318 void
1319 _EditCopyPasteManager::OnActionPerformed(const _Control& source, int actionId)
1320 {
1321         _Clipboard* pClipBoard = _Clipboard::GetInstance();
1322         SysTryReturnVoidResult(NID_UI_CTRL, pClipBoard, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1323
1324         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
1325
1326         ReleaseCopyPastePopup();
1327
1328         switch (actionId)
1329         {
1330         case COPY_PASTE_SELECT_ID:
1331                 {
1332                         int cursorPos = GetCursorPosition();
1333                         int start, end;
1334
1335                         __pEdit->GetWordPosition(cursorPos, start, end);
1336                         __pEdit->SetBlockRange(start, end);
1337                         SendTextBlockEvent();
1338
1339                         Release();
1340                         CreateHandle();
1341                         CreateCopyPastePopup();
1342                         Show();
1343
1344                         __pEdit->Draw();
1345                 }
1346                 break;
1347
1348         case COPY_PASTE_SELECT_ALL_ID:
1349                 {
1350                         int textLength = __pEdit->GetTextLength();
1351                         __pEdit->SetBlockRange(0, textLength);
1352                         SendTextBlockEvent();
1353                         pEditPresenter->UpdateComponentInformation();
1354
1355                         Release();
1356                         CreateHandle();
1357                         CreateCopyPastePopup();
1358                         Show();
1359
1360                         __pEdit->Draw();
1361                 }
1362                 break;
1363
1364         case COPY_PASTE_COPY_ID:
1365                 SendCopyPasteEvent(CORE_COPY_PASTE_STATUS_HIDE, CORE_COPY_PASTE_ACTION_COPY);
1366                 break;
1367
1368         case COPY_PASTE_CUT_ID:
1369                 SendCopyPasteEvent(CORE_COPY_PASTE_STATUS_HIDE, CORE_COPY_PASTE_ACTION_CUT);
1370                 break;
1371
1372         case COPY_PASTE_PASTE_ID:
1373                 SendCopyPasteEvent(CORE_COPY_PASTE_STATUS_HIDE, CORE_COPY_PASTE_ACTION_PASTE);
1374                 break;
1375
1376         case COPY_PASTE_CLIPBOARD_ID:
1377                 __needToReleaseBlock = false;
1378                 pEditPresenter->SetKeypadClosedEventSkipped(true);
1379                 pClipBoard->ShowPopup(CLIPBOARD_DATA_TYPE_TEXT, *__pEdit);
1380                 break;
1381         default:
1382                 break;
1383         }
1384
1385         return;
1386 }
1387
1388 void
1389 _EditCopyPasteManager::ScrollText(int distance)
1390 {
1391         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
1392         TextObject* pTextObject = pEditPresenter->GetTextObject();
1393
1394         int newFirstDisplayY = pTextObject->GetFirstDisplayPositionY();
1395         newFirstDisplayY += distance;
1396         int moveLine = pTextObject->GetLineIndexAtPositionY(newFirstDisplayY);
1397         pTextObject->SetFirstDisplayLineIndex(moveLine);
1398         pTextObject->SetFirstDisplayPositionY(newFirstDisplayY);
1399
1400         return;
1401 }
1402
1403 void
1404 _EditCopyPasteManager::AdjustBounds(void)
1405 {
1406         if (__pHandle[HANDLER_TYPE_LEFT])
1407         {
1408                 __pHandle[HANDLER_TYPE_LEFT]->AdjustBounds();
1409         }
1410         if (__pHandle[HANDLER_TYPE_RIGHT])
1411         {
1412                 __pHandle[HANDLER_TYPE_RIGHT]->AdjustBounds();
1413         }
1414         if (__pCopyPastePopup)
1415         {
1416                 //not need
1417         }
1418 }
1419
1420 bool
1421 _EditCopyPasteManager::GetTextBlockReleaseFlag(void) const
1422 {
1423         return __needToReleaseBlock;
1424 }
1425
1426 void
1427 _EditCopyPasteManager::SetTextBlockReleaseFlag(bool enabled)
1428 {
1429         __needToReleaseBlock = enabled;
1430 }
1431
1432 _Edit*
1433 _EditCopyPasteManager::GetEdit(void) const
1434 {
1435         return __pEdit;
1436 }
1437
1438 void
1439 _EditCopyPasteManager::MoveHandler(HandlerMoveType moveType)
1440 {
1441         if (!__pHandle[HANDLER_TYPE_RIGHT] || ! __pHandle[HANDLER_TYPE_LEFT])
1442         {
1443                 return;
1444         }
1445
1446         int leftHandlerPosition = __pHandle[HANDLER_TYPE_LEFT]->GetHandlerCursorPosition();
1447         int rightHandlerPosition = __pHandle[HANDLER_TYPE_RIGHT]->GetHandlerCursorPosition();
1448         int textLength = __pEdit->GetTextLength();
1449         Rectangle cursorBounds;
1450         int newCursorPosition;
1451         __pEdit->GetCursorBounds(false, cursorBounds);
1452         Point cursorPoint(cursorBounds.x, cursorBounds.y);
1453
1454         _EditPresenter* pEditPresenter = __pEdit->GetPresenter();
1455         TextObject* pTextObject = pEditPresenter->GetTextObject();
1456
1457         switch(moveType)
1458         {
1459                 case HANDLER_MOVE_TYPE_LEFT:
1460                         if (leftHandlerPosition < rightHandlerPosition-1)
1461                         {
1462                                 __pHandle[HANDLER_TYPE_RIGHT]->SetHandlerCursorPosition(--rightHandlerPosition);
1463                                 RefreshBlock(false);
1464                         }
1465                         else
1466                         {
1467                                 return;
1468                         }
1469                         break;
1470                 case HANDLER_MOVE_TYPE_UP:
1471                         cursorPoint.y -= cursorBounds.height/2;
1472                         newCursorPosition = GetCursorPositionAt(cursorPoint);
1473
1474                         if (newCursorPosition <= 0)
1475                         {
1476                                 int curCursorLine = pTextObject->GetLineIndexAtTextIndex(pTextObject->GetCursorIndex());
1477
1478                                 if (curCursorLine !=0 )
1479                                 {
1480                                         int offset = rightHandlerPosition - pTextObject->GetFirstTextIndexAt(curCursorLine);
1481                                         int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine-1);
1482                                         newCursorPosition = offset + firstTextIndex;
1483                                         int textLength = pTextObject->GetTextLengthAt(curCursorLine-1);
1484                                         if (offset > textLength)
1485                                         {
1486                                                 newCursorPosition = firstTextIndex+textLength;
1487                                         }
1488
1489                                         if (leftHandlerPosition >= newCursorPosition)
1490                                         {
1491                                                 newCursorPosition = leftHandlerPosition + 1;
1492                                         }
1493                                 }
1494                         }
1495
1496                         if (newCursorPosition >= 0 && leftHandlerPosition < newCursorPosition)
1497                         {
1498                 __pHandle[HANDLER_TYPE_RIGHT]->SetHandlerCursorPosition(newCursorPosition);
1499                                 RefreshBlock(false);
1500                         }
1501                         else
1502                         {
1503                                 return;
1504                         }
1505                         break;
1506                 case HANDLER_MOVE_TYPE_DOWN:
1507                         cursorPoint.y += cursorBounds.height + cursorBounds.height/2;
1508                         newCursorPosition = GetCursorPositionAt(cursorPoint);
1509
1510                         if (newCursorPosition < 0)
1511                         {
1512                                 int curCursorLine = pTextObject->GetLineIndexAtTextIndex(pTextObject->GetCursorIndex());
1513                                 int totalLine = pTextObject->GetTotalLineCount();
1514                                 if (curCursorLine < totalLine - 1)
1515                                 {
1516                                         int offset = rightHandlerPosition - pTextObject->GetFirstTextIndexAt(curCursorLine);
1517                                         int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine+1);
1518                                         newCursorPosition = offset + firstTextIndex;
1519                                         int textLength = pTextObject->GetTextLengthAt(curCursorLine+1);
1520                                         if (offset > textLength)
1521                                         {
1522                                                 newCursorPosition = firstTextIndex+textLength;
1523
1524                                         }
1525                                 }
1526                                 else if (curCursorLine == totalLine - 1)
1527                                 {
1528                                         int firstTextIndex = pTextObject->GetFirstTextIndexAt(curCursorLine);
1529                                         int textLength = pTextObject->GetTextLengthAt(curCursorLine);
1530                                         newCursorPosition = firstTextIndex + textLength;
1531                                 }
1532                         }
1533
1534                         if (newCursorPosition >= 0)
1535                         {
1536                 __pHandle[HANDLER_TYPE_RIGHT]->SetHandlerCursorPosition(newCursorPosition);
1537                                 RefreshBlock(false);
1538                         }
1539                         else
1540                         {
1541                                 return;
1542                         }
1543                         break;
1544                 case HANDLER_MOVE_TYPE_RIGHT:
1545                         if (textLength >= rightHandlerPosition+1)
1546                         {
1547                                 __pHandle[HANDLER_TYPE_RIGHT]->SetHandlerCursorPosition(++rightHandlerPosition);
1548                                 RefreshBlock(false);
1549                         }
1550                         else
1551                         {
1552                                 return;
1553                         }
1554                         break;
1555                 default:
1556                         break;
1557         }
1558
1559         CheckVisibleState(true);
1560
1561         ReleaseCopyPastePopup();
1562         __isHandlerMoving = true;
1563         return;
1564 }
1565
1566 bool
1567 _EditCopyPasteManager::IsHandlerMovingEnabled(void) const
1568 {
1569         return __isHandlerMoving;
1570 }
1571
1572 void
1573 _EditCopyPasteManager::SetHandlerMovingEnabled(bool enabled)
1574 {
1575         __isHandlerMoving = enabled;
1576 }
1577
1578 Rectangle
1579 _EditCopyPasteManager::GetEditVisibleArea(void) const
1580 {
1581         return __editVisibleArea;
1582 }
1583
1584
1585 }}} // Tizen::Ui::Controls