1.Uncropped image area is dimmed 2.Crash in crop is fixed
[apps/osp/ImageViewer.git] / src / IvImageCropForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                IvImageCrop.cpp
19  * @brief               This is the implementation file for Image Crop.
20  */
21
22 #include <FApp.h>
23 #include <FMedia.h>
24 #include <FSystem.h>
25 #include <FSocial.h>
26 #include "IvImageCropForm.h"
27 #include "IvImageViewerPresentationModel.h"
28 #include "IvResourceManager.h"
29 #include "IvTypes.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Content;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Io;
37 using namespace Tizen::Media;
38 using namespace Tizen::System;
39 using namespace Tizen::Social;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Ui::Scenes;
42
43 static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
44 static const unsigned int UNCROPPED_IMAGE_COLOR = Color32<139, 137, 137, 100>::Value;
45 static const int CROP_BOX_LINE_WIDTH = 5;
46
47 ImageCropForm::ImageCropForm(void)
48         : __imageHeight(0)
49         , __imageWidth(0)
50         , __isOrientationChanged(false)
51         , __pCanvas(null)
52         , __pCurrentBitmap(null)
53         , __pRectangleBitmap(null)
54         , __pointLocation(OUTSIDE_CROPBOX)
55         , __pPresentationModel(null)
56 {
57 }
58
59 ImageCropForm::~ImageCropForm(void)
60 {
61         if (__pCurrentBitmap != null)
62         {
63                 delete __pCurrentBitmap;
64         }
65         if (__pRectangleBitmap != null)
66         {
67                 delete __pRectangleBitmap;
68         }
69         if (__pCanvas != null)
70         {
71                 delete __pCanvas;
72         }
73 }
74
75 bool
76 ImageCropForm::Initialize(void)
77 {
78         Form::Construct(IDL_FORM_IMAGE_CROP);
79         Form::SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
80
81         __pPresentationModel = ImageViewerPresentationModel::GetInstance();
82
83         return true;
84 }
85
86 result
87 ImageCropForm::OnInitializing(void)
88 {
89         result r = E_SUCCESS;
90
91         AppResource* pAppResource = null;
92         Bitmap* pSaveButtonBitmap = null;
93         Bitmap* pCancelButtonBitmap  = null;
94         Panel* pPanel = null;
95         Button* pSaveButton = null;
96         Button* pCancelButton = null;
97         pAppResource = UiApp::App::GetInstance()->GetAppResource();
98         if (pAppResource != null)
99         {
100                 pSaveButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_SAVE_ICON);
101                 pCancelButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_CANCEL_ICON);
102                 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
103         }
104
105         pPanel = static_cast<Panel*>(GetControl(L"IDC_FOOTER_PANEL"));
106         if (pPanel != null)
107         {
108                 pSaveButton = static_cast<Button*>(pPanel->GetControl(L"IDC_SAVE_BUTTON"));
109                 if (pSaveButton != null && pSaveButtonBitmap != null)
110                 {
111                         Point startPoint((pSaveButton->GetWidth() - pSaveButtonBitmap->GetWidth()) / 2,
112                                         (pSaveButton->GetHeight() - pSaveButtonBitmap->GetHeight()) / 2);
113                         pSaveButton->SetNormalBitmap(startPoint, *pSaveButtonBitmap);
114                         pSaveButton->AddActionEventListener(*this);
115                         pSaveButton->SetActionId(IDA_BUTTON_SAVE);
116                         delete pSaveButtonBitmap;
117                 }
118
119                 pCancelButton = static_cast<Button*>(pPanel->GetControl(L"IDC_CANCEL_BUTTON"));
120                 if (pCancelButton != null && pCancelButtonBitmap != null)
121                 {
122                         Point startPoint((pCancelButton->GetWidth() - pCancelButtonBitmap->GetWidth()) / 2,
123                                         (pCancelButton->GetHeight() - pCancelButtonBitmap->GetHeight()) / 2);
124                         pCancelButton->SetNormalBitmap(startPoint, *pCancelButtonBitmap);
125                         pCancelButton->AddActionEventListener(*this);
126                         pCancelButton->SetActionId(IDA_BUTTON_CANCEL);
127                         delete pCancelButtonBitmap;
128                 }
129                 SetControlAlwaysOnTop(*pPanel, true);
130                 Label* pLabel = static_cast<Label*>(pPanel->GetControl(L"IDC_PANEL_LABEL"));
131                 if (pLabel != null)
132                 {
133                         SetControlAlwaysAtBottom(*pLabel, true);
134                 }
135         }
136         AddTouchEventListener(*this);
137         AddOrientationEventListener(*this);
138         __pPresentationModel->AddFileUpdateListener(this);
139
140         return r;
141 }
142
143 void
144 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
145 {
146         AppLogDebug("ENTER pArgs(%x)", pArgs);
147         if (pArgs != null)
148         {
149                 IEnumerator* pEnum = pArgs->GetEnumeratorN();
150                 if (pEnum->MoveNext() != E_SUCCESS)
151                 {
152                         delete pEnum;
153                         delete pArgs;
154                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
155
156                         return;
157                 }
158                 __sourceFilePath = *(static_cast<String*>(pEnum->GetCurrent()));
159                 if (__sourceFilePath.IsEmpty() == false)
160                 {
161                         result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
162                         if (r == E_SUCCESS)
163                         {
164                                 __imageWidth = __imageBuffer.GetWidth();
165                                 __imageHeight = __imageBuffer.GetHeight();
166                                 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_AUTO);
167                         }
168                         Image img;
169                         __imageFormat = img.GetImageFormat(__sourceFilePath);
170                 }
171                 if (pEnum->MoveNext() != E_SUCCESS)
172                 {
173                         delete pEnum;
174                         delete pArgs;
175                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
176
177                         return;
178                 }
179                 __cropMode = *(static_cast<String*>(pEnum->GetCurrent()));
180                 if (__cropMode.IsEmpty() == false)
181                 {
182                         __cropMode.ToLowerCase();
183                         if (__cropMode == "auto")
184                         {
185                                 delete pEnum;
186                                 delete pArgs;
187                                 SetValue();
188                                 return;
189                         }
190                         else if (__cropMode == "fit-to-screen")
191                         {
192                                 if (pEnum->MoveNext() != E_SUCCESS)
193                                 {
194                                         delete pEnum;
195                                         delete pArgs;
196                                         AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
197
198                                         return;
199                                 }
200                                 Integer *pStatus = static_cast<Integer*>(pEnum->GetCurrent());
201                                 if (pStatus != null)
202                                 {
203                                         __statusValue = pStatus->ToInt();
204                                 }
205                                 if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
206                                 {
207                                         if (pEnum->MoveNext() != E_SUCCESS)
208                                         {
209                                                 delete pEnum;
210                                                 delete pArgs;
211                                                 AppLogDebug("EXIT 4(%s)", GetErrorMessage(GetLastResult()));
212
213                                                 return;
214                                         }
215
216                                         LongLong* pContentId = static_cast<LongLong*>(pEnum->GetCurrent());
217                                         if (pContentId != null)
218                                         {
219                                                 __contentId = pContentId->ToLongLong();
220                                         }
221                                 }
222
223                                 delete pEnum;
224                                 delete pArgs;
225                                 SetValue();
226                         }
227                 }
228         }
229         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
230 }
231
232 void
233 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
234 {
235         AppLogDebug("ENTER");
236         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
237 }
238
239 result
240 ImageCropForm::OnDraw(void)
241 {
242         result r = E_SUCCESS;
243         AppLogDebug("ENTER");
244         if (__pCanvas != null)
245         {
246                 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
247                 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap);
248                 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height));
249                 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2), __cropBox.y - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Top Rectangle
250                 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Bottom Rectangle
251                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); //Left Rectangle
252                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); //Right Rectangle
253                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2), __cropBox.y - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Top Left Rectangle
254                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2), __cropBox.y - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Top Right Rectangle
255                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Bottom Right Rectangle
256                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Bottom Left Rectangle
257                 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, (__cropBox.y - __imageBox.y)));
258                 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle(__imageBox.x, (__cropBox.y + __cropBox.height), __imageBox.width, ((__imageBox.y + __imageBox.height) - (__cropBox.y + __cropBox.height))));
259                 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle(__imageBox.x, __cropBox.y, (__cropBox.x - __imageBox.x), __cropBox.height));
260                 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle((__cropBox.x + __cropBox.width), __cropBox.y, ((__imageBox.x + __imageBox.width) - (__cropBox.x + __cropBox.width)), __cropBox.height));
261         }
262         AppLogDebug("EXIT");
263         return r;
264 }
265
266 void
267 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
268 {
269         SceneManager* pSceneManager = SceneManager::GetInstance();
270         switch(actionId)
271         {
272         case IDA_BUTTON_CANCEL:
273                 pSceneManager->GoBackward(BackwardSceneTransition());
274                 break;
275         case IDA_BUTTON_SAVE:
276         {
277                 if (__cropMode == "auto")
278                 {
279                         ImageBuffer* pCropBuffer = null;
280                         int count = 1;
281                         int index = 0;
282                         String destFilePath;
283                         String stringToInsert = L"_";
284                         String delimiter = L".";
285                         if (__sourceFilePath.IsEmpty() == false)
286                         {
287                                 __sourceFilePath.Reverse();
288                                 __sourceFilePath.IndexOf(delimiter, 0, index);
289                                 __sourceFilePath.Reverse();
290                                 __sourceFilePath.Insert(stringToInsert, __sourceFilePath.GetLength() - index - 1);
291                                 destFilePath.Append(__sourceFilePath);
292                                 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
293                                 while (File::IsFileExist(destFilePath) == true)
294                                 {
295                                         count++;
296                                         destFilePath.Clear();
297                                         destFilePath.Append(__sourceFilePath);
298                                         destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
299                                 }
300                                 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
301                                                 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
302                                 pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
303                                 ContentManager::ScanFile(destFilePath);
304                                 delete pCropBuffer;
305                         }
306                         ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
307                         pList->Add(new (std::nothrow) String(destFilePath));
308                         pSceneManager->GoBackward(BackwardSceneTransition(), pList);
309                 }
310                 else if(__cropMode == "fit-to-screen")
311                 {
312                         ImageBuffer* pCropBuffer = null;
313                         result r = E_SUCCESS;
314                         pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
315                                         (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
316                         if (__statusValue == SET_AT_TYPE_HOME_SCREEN_WALLPAPER)
317                         {
318                                 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
319                                 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
320                                 if (r == E_SUCCESS)
321                                 {
322                                         r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPath);
323                                 }
324                                 else if (r == E_OVERFLOW)
325                                 {
326                                         MessageBox messageBox;
327                                         messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
328                                         int modalResult = 0;
329                                         messageBox.ShowAndWait(modalResult);
330                                 }
331                         }
332                         else if (__statusValue == SET_AT_TYPE_LOCK_SCREEN_WALLPAPER)
333                         {
334                                 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
335                                 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
336                                 if (r == E_SUCCESS)
337                                 {
338                                         r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
339                                 }
340                                 else if (r == E_OVERFLOW)
341                                 {
342                                         MessageBox messageBox;
343                                         messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
344                                         int modalResult = 0;
345                                         messageBox.ShowAndWait(modalResult);
346                                 }
347                         }
348                         else if (__statusValue == SET_AT_TYPE_HOME_AND_LOCK_SCREEN_WALLPAPER)
349                         {
350                                 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
351                                 String destPathHome = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
352                                 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
353                                 r = pCropBuffer->EncodeToFile(destPathHome, __imageFormat, true, 100);
354                                 if (r == E_SUCCESS)
355                                 {
356                                         r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
357                                         r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPathHome);
358                                 }
359                                 else if (r == E_OVERFLOW)
360                                 {
361                                         MessageBox messageBox;
362                                         messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
363                                         int modalResult = 0;
364                                         messageBox.ShowAndWait(modalResult);
365                                 }
366                         }
367                         else if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
368                         {
369                                 int count = 1;
370                                 int index = 0;
371                                 String destFilePath;
372                                 String stringToInsert = L"_";
373                                 String delimiter = L".";
374                                 if (__sourceFilePath.IsEmpty() == false)
375                                 {
376                                         __sourceFilePath.Reverse();
377                                         __sourceFilePath.IndexOf(delimiter, 0, index);
378                                         __sourceFilePath.Reverse();
379                                         __sourceFilePath.Insert(stringToInsert, __sourceFilePath.GetLength() - index - 1);
380                                         destFilePath.Append(__sourceFilePath);
381                                         destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
382                                         while (File::IsFileExist(destFilePath) == true)
383                                         {
384                                                 count++;
385                                                 destFilePath.Clear();
386                                                 destFilePath.Append(__sourceFilePath);
387                                                 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
388                                         }
389                                 }
390                                  r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
391                                 ContentManager::ScanFile(destFilePath);
392                                 Contact* pContact = null;
393                                 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
394                                 Addressbook* pAddressbook = null;
395
396                                 pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
397                                 pContact = pAddressbook->GetContactN(__contentId);
398                                 pContact->SetThumbnail(destFilePath);
399                                 pAddressbook->UpdateContact(*pContact);
400
401                                 delete pContact;
402                                 delete pAddressbook;
403                                 delete pCropBuffer;
404                                 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
405                                 pList->Add(new (std::nothrow) String(destFilePath));
406                                 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
407                                 AppLog("Exit");
408                                 return;
409                         }
410                         delete pCropBuffer;
411                         pSceneManager->GoBackward(BackwardSceneTransition());
412                 }
413         }
414         break;
415         }
416         AppLog("Exit");
417 }
418
419 void
420 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
421 {
422         AppLogDebug("ENTER");
423         __isOrientationChanged = true;
424         SetValue();
425         __pCanvas->Clear();
426         AppLogDebug("EXIT");
427 }
428
429 void
430 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
431 {
432         AppLogDebug("ENTER");
433         __touchStart = currentPosition;
434         CheckCurrentPosition(currentPosition);
435         AppLogDebug("EXIT");
436 }
437
438 void
439 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
440 {
441         AppLogDebug("ENTER");
442         int deltaX = currentPosition.x - __touchStart.x;
443         int deltaY = currentPosition.y - __touchStart.y;
444         if (__cropMode.Equals("auto", false))
445         {
446                 switch(__pointLocation)
447                 {
448                 case INSIDE_TOP_RECTANGLE:
449                 {
450                         if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3)))
451                         {
452                                 __cropBox.y += deltaY;
453                                 __cropBox.height -= deltaY;
454                         }
455                 }
456                 break;
457
458                 case INSIDE_BOTTOM_RECTANGLE:
459                 {
460                         if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
461                                 && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3))
462                         {
463                                 __cropBox.height += deltaY;
464                         }
465                 }
466                 break;
467
468                 case INSIDE_LEFT_RECTANGLE:
469                 {
470                         if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
471                         {
472                                 __cropBox.x += deltaX;
473                                 __cropBox.width -= deltaX;
474                         }
475                 }
476                 break;
477
478                 case INSIDE_RIGHT_RECTANGLE:
479                 {
480                         if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
481                                 && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3))
482                         {
483                                 __cropBox.width += deltaX;
484                         }
485                 }
486                 break;
487
488                 case INSIDE_TOP_LEFT_RECTANGLE:
489                 {
490                         if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3))
491                                         && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
492                         {
493                                 __cropBox.y += deltaY;
494                                 __cropBox.height -= deltaY;
495                                 __cropBox.x += deltaX;
496                                 __cropBox.width -= deltaX;
497                         }
498                 }
499                 break;
500
501                 case INSIDE_TOP_RIGHT_RECTANGLE:
502                 {
503                         if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3))
504                                         && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3))
505                         {
506                                 __cropBox.width += deltaX;
507                                 __cropBox.y += deltaY;
508                                 __cropBox.height -= deltaY;
509                         }
510                 }
511                 break;
512
513                 case INSIDE_BOTTOM_LEFT_RECTANGLE:
514                 {
515                         if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3)
516                                 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
517                         {
518                                 __cropBox.x += deltaX;
519                                 __cropBox.width -= deltaX;
520                                 __cropBox.height += deltaY;
521                         }
522                 }
523                 break;
524
525                 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
526                 {
527                         if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3)
528                                 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3))
529                         {
530                                 __cropBox.width += deltaX;
531                                 __cropBox.height += deltaY;
532                         }
533                 }
534                 break;
535
536                 case INSIDE_CROPBOX:
537                 {
538                         if ((__cropBox.x + deltaX) > __imageBox.x)
539                         {
540                                 __cropBox.x += deltaX;
541                         }
542                         else
543                         {
544                                 __cropBox.x = __imageBox.x;
545                         }
546                         if ((__cropBox.y + deltaY) > __imageBox.y)
547                         {
548                                 __cropBox.y += deltaY;
549                         }
550                         else
551                         {
552                                 __cropBox.y = __imageBox.y;
553                         }
554                         if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
555                         {
556                                 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
557                         }
558                         if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
559                         {
560                                 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
561                         }
562                 }
563                 break;
564
565                 default:
566                         break;
567                 }
568         }
569         else if (__cropMode != "auto")
570         {
571                 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
572                         || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
573                 {
574                         int temp = __cropBox.height;
575                         int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
576                         temp = height - temp;
577                         if ((__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
578                         {
579                                 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
580                                         && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
581                                         && (__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)))
582                                 {
583                                         __cropBox.x += deltaX;
584                                         __cropBox.width = __cropBox.width - (2 * deltaX);
585                                         __cropBox.height = height;
586                                         __cropBox.y = __cropBox.y - (temp / 2);
587                                 }
588                                 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
589                                                 && __cropBox.width < __imageBox.width
590                                                 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
591                                 {
592                                         if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
593                                                 && __cropBox.x + deltaX >= __imageBox.x)
594                                         {
595                                                 __cropBox.x += deltaX;
596                                         }
597                                         else
598                                         {
599                                                 __cropBox.x = __cropBox.x + (2 * deltaX);
600                                         }
601                                         __cropBox.width = __cropBox.width - (2 * deltaX);
602                                         __cropBox.height = height;
603                                         __cropBox.y = __cropBox.y - temp;
604                                 }
605                                 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
606                                                 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
607                                 {
608                                         if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
609                                         {
610                                                 __cropBox.x += deltaX;
611                                         }
612                                         else
613                                         {
614                                                 __cropBox.x = __cropBox.x + (2 * deltaX);
615                                         }
616                                         __cropBox.width = __cropBox.width - (2 * deltaX);
617                                         __cropBox.height = height;
618                                 }
619                         }
620                 }
621                 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
622                                 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
623                 {
624                         deltaX *= -1;
625
626                         int temp = __cropBox.height;
627                         int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
628                         temp = height - temp;
629                         if ((__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
630                         {
631                                 if (deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
632                                 {
633                                         if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
634                                                 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
635                                         {
636                                                 __cropBox.x += deltaX;
637                                                 __cropBox.width = __cropBox.width - (2 * deltaX);;
638                                                 __cropBox.height = height;
639                                                 __cropBox.y = __cropBox.y - (temp / 2);
640                                         }
641                                         else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
642                                                         && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
643                                                         && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
644                                         {
645                                                 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
646                                                         && __cropBox.x + deltaX >= __imageBox.x)
647                                                 {
648                                                         __cropBox.x += deltaX;
649                                                 }
650                                                 else
651                                                 {
652                                                         __cropBox.width = __cropBox.width - (2 * deltaX);
653                                                 }
654                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
655                                                 __cropBox.height = height;
656                                                 __cropBox.y = __cropBox.y - temp;
657                                         }
658                                         else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
659                                                         && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
660                                         {
661                                                 if (__cropBox.x + deltaX >= __imageBox.x)
662                                                 {
663                                                         __cropBox.x += deltaX;
664                                                 }
665                                                 else
666                                                 {
667                                                         __cropBox.x = __imageBox.x;
668                                                 }
669                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
670                                                 __cropBox.height = height;
671                                         }
672                                         else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
673                                                         && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
674                                                         && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
675                                         {
676                                                 __cropBox.x += deltaX;
677                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
678                                                 __cropBox.height = height;
679                                         }
680                                         else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
681                                                         && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
682                                                         && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
683                                         {
684                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
685                                                 __cropBox.y = __cropBox.y - (temp / 2);
686                                                 __cropBox.height = height;
687                                         }
688                                 }
689                         }
690                 }
691                 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
692                 {
693                         int temp = __cropBox.width;
694                         int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
695                         temp = width - temp;
696                         if (__cropBox.height - (2 * deltaY) > (__formHeight * (CROP_RECTANGLE_HEIGHT * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
697                         {
698                                 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
699                                         && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
700                                 {
701                                         __cropBox.y += deltaY;
702                                         __cropBox.height = __cropBox.height - (2 * deltaY);
703                                         __cropBox.width = width;
704                                         __cropBox.x = __cropBox.x - (temp / 2);
705                                 }
706                                 else if (__cropBox.x - (temp / 2) < __imageBox.x)
707                                 {
708                                         if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
709                                         {
710                                                 __cropBox.width = width;
711                                         }
712                                         if (__cropBox.y + deltaY >= __imageBox.y)
713                                         {
714                                                 __cropBox.y += deltaY;
715                                         }
716                                         if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
717                                         {
718                                                 __cropBox.height = __cropBox.height - (2 * deltaY);
719                                         }
720                                 }
721                                 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
722                                                 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
723                                 {
724                                         if (__cropBox.x - temp >= __imageBox.x
725                                                 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
726                                         {
727                                                 __cropBox.x = __cropBox.x - temp;
728                                         }
729                                         else if (__cropBox.x - temp >= __imageBox.x)
730                                         {
731                                                 __cropBox.x = __cropBox.x - temp / 2;
732                                         }
733                                         if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
734                                         {
735                                                 __cropBox.width = width;
736                                         }
737                                         if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
738                                         {
739                                                 __cropBox.y = __cropBox.y + (2 * deltaY);
740                                                 __cropBox.height = __cropBox.height - (2 * deltaY);
741                                         }
742                                 }
743                                 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
744                                                 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
745                                                 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
746                                 {
747                                         __cropBox.x = __cropBox.x - temp;
748                                         __cropBox.width = width;
749                                         __cropBox.y += deltaY;
750                                         __cropBox.height = __cropBox.height - (2 * deltaY);
751                                 }
752                         }
753                 }
754                 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
755                 {
756                         deltaY = deltaY * (-1);
757                         int temp = __cropBox.width;
758                         int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
759                         temp = width - temp;
760                         if (__cropBox.height - (2 * deltaY) > (__formHeight * (CROP_RECTANGLE_HEIGHT * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
761                         {
762                                 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
763                                         && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
764                                 {
765                                         __cropBox.y += deltaY;
766                                         __cropBox.height = __cropBox.height - (2 * deltaY);;
767                                         __cropBox.width = width;
768                                         __cropBox.x = __cropBox.x - (temp / 2);
769                                 }
770                                 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
771                                                 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
772                                 {
773                                         __cropBox.x = __cropBox.x - (temp / 2);
774                                         __cropBox.width = width;
775                                         __cropBox.height = __cropBox.height - (2 * deltaY);
776                                 }
777                                 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
778                                                 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
779                                                 && __cropBox.x - temp >= __imageBox.x)
780                                 {
781                                         __cropBox.x = __cropBox.x - temp;
782                                         __cropBox.y += deltaY;
783                                         __cropBox.width = width;
784                                         __cropBox.height = __cropBox.height - (2 * deltaY);
785                                 }
786                                 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
787                                                 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
788                                                 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
789                                 {
790                                         __cropBox.x -= temp;
791                                         __cropBox.width = width;
792                                         __cropBox.height = __cropBox.height - (2 * deltaY);
793                                 }
794                                 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
795                                                 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
796                                 {
797                                         if (__cropBox.y + deltaY >= __imageBox.y)
798                                         {
799                                                 __cropBox.y += deltaY;
800                                         }
801                                         __cropBox.width = width;
802                                         __cropBox.height = __cropBox.height - (2 * deltaY);
803                                 }
804                         }
805                 }
806                 else if (__pointLocation == INSIDE_CROPBOX)
807                 {
808                         if ((__cropBox.x + deltaX) > __imageBox.x)
809                         {
810                                 __cropBox.x += deltaX;
811                         }
812                         else
813                         {
814                                 __cropBox.x = __imageBox.x;
815                         }
816                         if ((__cropBox.y + deltaY) > __imageBox.y)
817                         {
818                                 __cropBox.y += deltaY;
819                         }
820                         else
821                         {
822                                 __cropBox.y = __imageBox.y;
823                         }
824                         if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
825                         {
826                                 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
827                         }
828                         if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
829                         {
830                                 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
831                         }
832                 }
833         }
834         RequestRedraw(false);
835         __touchStart = currentPosition;
836         AppLogDebug("EXIT");
837 }
838
839 void
840 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
841 {
842         AppLogDebug("ENTER");
843         __pointLocation = OUTSIDE_CROPBOX;
844         AppLogDebug("EXIT");
845 }
846
847 void
848 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
849 {
850         AppLogDebug("ENTER");
851         if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (CROP_RECTANGLE_HEIGHT / 2))
852                 && currentPosition.y >  (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + (CROP_RECTANGLE_HEIGHT / 2)))
853         {
854                 __pointLocation = INSIDE_TOP_RECTANGLE;
855         }
856         else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
857                 && currentPosition.y >  ((__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (CROP_RECTANGLE_HEIGHT / 2)))
858         {
859                 __pointLocation = INSIDE_LEFT_RECTANGLE;
860         }
861         else if (currentPosition.x > (__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
862                 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (CROP_RECTANGLE_HEIGHT / 2)))
863         {
864                 __pointLocation = INSIDE_RIGHT_RECTANGLE;
865         }
866         else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (CROP_RECTANGLE_HEIGHT / 2))
867                 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
868         {
869                 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
870         }
871         else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
872                          && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y +(CROP_RECTANGLE_HEIGHT / 2)))
873         {
874                 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
875         }
876         else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
877                         && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y +(CROP_RECTANGLE_HEIGHT / 2)))
878         {
879                 __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
880         }
881         else if (currentPosition.x > (__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
882                         && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
883         {
884                 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
885         }
886         else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
887                         && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
888         {
889                 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
890         }
891         else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
892         {
893                 __pointLocation = INSIDE_CROPBOX;
894         }
895         else
896         {
897                 __pointLocation = OUTSIDE_CROPBOX;
898         }
899         AppLogDebug("EXIT");
900         return;
901 }
902
903 void
904 ImageCropForm::SetValue(void)
905 {
906         int prevHeight = __imageBox.height;
907         int prevWidth = __imageBox.width;
908         int prevImageboxX = __imageBox.x;
909         int prevImageBoxY = __imageBox.y;
910         if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
911         {
912                 __formHeight = Form::GetBounds().height;
913                 __formWidth = Form::GetBounds().width;
914         }
915         else if(GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
916         {
917                 __formHeight = Form::GetBounds().width;
918                 __formWidth = Form::GetBounds().height;
919         }
920
921         float clientAreaRatio = (GetClientAreaBoundsF().height - FOOTER_PANEL_HEIGHT) / GetClientAreaBoundsF().width;
922         if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > (GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT))
923         {
924                 float imageRatio = __imageHeight / (float) __imageWidth;
925                 if (imageRatio < clientAreaRatio)
926                 {
927                         __imageBox.width = GetClientAreaBounds().width;
928                         __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
929                         __imageBox.x = GetClientAreaBounds().x;
930                         __imageBox.y = GetClientAreaBounds().y + ((GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT) - __imageBox.height) / 2;
931                 }
932                 else
933                 {
934                         __imageBox.height = GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT;
935                         __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
936                         __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
937                         __imageBox.y = GetClientAreaBounds().y;
938                 }
939         }
940         else
941         {
942                 __imageBox.height = __imageHeight;
943                 __imageBox.width = __imageWidth;
944                 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
945                 __imageBox.y = (GetClientAreaBounds().height - __imageBox.height) / 2 ;
946         }
947         if (__isOrientationChanged == false)
948         {
949                 if (__imageBox.height > __imageBox.width)
950                 {
951                         __cropBox.width = (2 * __imageBox.width) / 3;
952                         __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
953                         if (__cropBox.height > __imageBox.height)
954                         {
955                                 __cropBox.height = (2 * __imageBox.height) / 3;
956                                 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
957                         }
958                         __cropBox.y =  __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
959                         __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
960                 }
961                 else
962                 {
963                         __cropBox.height = (2 *__imageBox.height) / 3;
964                         __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
965                         if (__cropBox.width > __imageBox.width)
966                         {
967                                 __cropBox.width = (2 * __imageBox.width) / 3;
968                                 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
969                         }
970                         __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
971                         __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
972                 }
973         }
974         else
975         {
976                 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float)prevWidth;
977                 __cropBox.x = (temp + 0.5);
978                 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float)prevHeight;
979                 __cropBox.y = (temp + 0.5);
980                 temp = (__cropBox.height * __imageBox.height) / (float)prevHeight;
981                 __cropBox.height = (temp + 0.5);
982                 temp = (__cropBox.width * __imageBox.width) / (float)prevWidth;
983                 __cropBox.width = (temp + 0.5);
984         }
985         __pCanvas = GetCanvasN();
986         __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
987 }
988
989 void ImageCropForm::OnFormFileEventOccuered(const int index, const unsigned long eventId)
990 {
991         AppLogDebug(" ENTER");
992         UiApp::GetInstance()->Terminate();
993         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
994 }