Fixed AppControl issue
[apps/osp/Gallery.git] / src / GlImageCropForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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                GlImageCrop.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 <FText.h>
26 #include "GlGalleryApp.h"
27 #include "GlImageCropForm.h"
28 #include "GlResourceManager.h"
29 #include "GlTypes.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::Text;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Controls;
42 using namespace Tizen::Ui::Scenes;
43
44 static const unsigned int COLOR_GALLERY_BG = Color32<31,31,31>::Value;
45 static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
46 static const int CROP_BOX_LINE_WIDTH = 5;
47 static const int H_CROP_RECTANGLE = 20;
48 static const wchar_t* PATH_DATA_DIRECTORY_IN_CROP = L"data/";
49 static const wchar_t* FILE_NAME_SEPARATOR = L"_";
50 static const int MAX_FILE_LENGHT = 255;
51 static const int CROP_BOX_OFFSET = 20;
52
53 ImageCropForm::ImageCropForm(void)
54         : __sourceFilePath(L"")
55         , __cropMode(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN)
56         , __pCanvas(null)
57     , __pCurrentBitmap(null)
58         , __pRectangleBitmap(null)
59         , __imageFormat(IMG_FORMAT_NONE)
60         , __pointLocation(OUTSIDE_CROPBOX)
61         , __isOrientationChanged(false)
62         , __imageHeight(0)
63         , __imageWidth(0)
64         , __statusValue(-1)
65         , __formHeight(0)
66         , __formWidth(0)
67         , __skipMoveEvent(0)
68 {
69 }
70
71 ImageCropForm::~ImageCropForm(void)
72 {
73         if (__pCurrentBitmap != null)
74         {
75                 delete __pCurrentBitmap;
76         }
77         if (__pRectangleBitmap != null)
78         {
79                 delete __pRectangleBitmap;
80         }
81         if (__pCanvas != null)
82         {
83                 delete __pCanvas;
84         }
85 }
86
87 bool
88 ImageCropForm::Initialize(void)
89 {
90         Form::Construct(IDL_FORM_IMAGE_CROP);
91
92         return true;
93 }
94
95 result
96 ImageCropForm::OnInitializing(void)
97 {
98         AppLogDebug("OnInitializing");
99
100         AppResource* pAppResource = null;
101         pAppResource = UiApp::App::GetInstance()->GetAppResource();
102
103         if (pAppResource != null)
104         {
105                 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
106         }
107
108         Footer* pFooter = GetFooter();
109         if (pFooter != null)
110         {
111                 pFooter->AddActionEventListener(*this);
112                 pFooter->RemoveAllItems();
113                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
114
115                 FooterItem footerItem1;
116                 footerItem1.Construct(IDA_BUTTON_SAVE);
117                 footerItem1.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
118
119                 pFooter->AddItem(footerItem1);
120         }
121
122         AddTouchEventListener(*this);
123         AddOrientationEventListener(*this);
124         SetFormBackEventListener(this);
125
126         return E_SUCCESS;
127 }
128
129 result
130 ImageCropForm::OnDraw(void)
131 {
132         AppLogDebug("OnDraw");
133         result r = E_SUCCESS;
134
135         if (__pCanvas != null)
136         {
137                 __pCanvas->Clear();
138                 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
139                 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap); // Image to be cropped
140                 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height)); // Cropbox rectangle
141                 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Rectangle
142                 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Rectangle
143                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); //Left Rectangle
144                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); //Right Rectangle
145                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Left Rectangle
146                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Right Rectangle
147                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Right Rectangle
148                 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Left Rectangle
149                 __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
150         }
151
152         return r;
153 }
154
155 void
156 ImageCropForm::OnFormBackRequested(Form& source)
157 {
158         AppLogDebug("OnFormBackRequested");
159
160         SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
161 }
162
163 void
164 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
165 {
166         AppLogDebug("OnActionPerformed");
167         result r = E_SUCCESS;
168
169         switch(actionId)
170         {
171         case IDA_BUTTON_SAVE:
172         {
173                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
174
175                 if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
176                 {
177                         ImageBuffer* pCropBuffer = null;
178                         int count = 1;
179                         int index = 0;
180
181                         String destFilePath;
182                         destFilePath.Append(App::GetInstance()->GetAppSharedPath());
183                         destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
184                         destFilePath.Append(GetFileName(__sourceFilePath));
185                         destFilePath.Reverse();
186                         r = destFilePath.IndexOf(FILE_EXT_SEPARATOR, 0, index);
187                         destFilePath.Reverse();
188                         if (r != E_SUCCESS)
189                         {
190                                 index = 0;
191                                 destFilePath.Append(FILE_EXT_SEPARATOR);
192                         }
193                         destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
194                         destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
195
196                         if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
197                         {
198                                 destFilePath.Remove(destFilePath.GetLength() - index, index);
199                                 destFilePath.Append(CONTENT_EXT_JPG);
200                         }
201
202                         while (File::IsFileExist(destFilePath) == true)
203                         {
204                                 count++;
205                                 destFilePath.Clear();
206                                 destFilePath.Append(App::GetInstance()->GetAppSharedPath());
207                                 destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
208                                 destFilePath.Append(GetFileName(__sourceFilePath));
209                                 destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
210                                 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
211                                 if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
212                                 {
213                                         destFilePath.Remove(destFilePath.GetLength() - index, index);
214                                         destFilePath.Append(CONTENT_EXT_JPG);
215                                 }
216                         }
217
218                         AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
219                         HashMap* pMap = null;
220                         int inputLength = 0;
221                         Utf8Encoding utf8;
222                         r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
223
224                         while (inputLength > MAX_FILE_LENGHT)
225                         {
226                                 destFilePath.IndexOf(FILE_NAME_SEPARATOR, 0, index);
227                                 destFilePath.Remove(index - 1, 1);
228                                 r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
229                         }
230
231                         pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
232                                                 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
233
234                         if (pCropBuffer != null)
235                         {
236                                 if (__imageFormat == IMG_FORMAT_JPG || __imageFormat == IMG_FORMAT_PNG || __imageFormat == IMG_FORMAT_BMP)
237                                 {
238                                         r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
239                                 }
240                                 else
241                                 {
242                                         r = pCropBuffer->EncodeToFile(destFilePath, IMG_FORMAT_JPG, true, 100);
243                                 }
244                                 delete pCropBuffer;
245
246                                 if (r == E_SUCCESS )
247                                 {
248                                         ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
249                                         pList->Add(new (std::nothrow) String(destFilePath));
250
251                                         pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
252                                         pMap->Construct();
253                                         pMap->Add(new (std::nothrow) String(APPCONTROL_KEY_DATA_SELECTED), pList);
254
255                                         appControlResult = APP_CTRL_RESULT_SUCCEEDED;
256                                 }
257                                 else
258                                 {
259                                         appControlResult = APP_CTRL_RESULT_FAILED;
260                                 }
261                         }
262                         else
263                         {
264                                 appControlResult = APP_CTRL_RESULT_FAILED;
265                         }
266
267                         pApp->SendAppControlResult(appControlResult, pMap);
268                         pApp->Terminate();
269                 }
270         }
271         break;
272         }
273
274         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
275         return;
276 }
277
278 void
279 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
280 {
281         AppLogDebug("OnOrientationChanged");
282
283         __isOrientationChanged = true;
284         SetValue();
285
286         if (__pCanvas != null)
287         {
288                 __pCanvas->Clear();
289         }
290         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
291 }
292
293 void
294 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
295 {
296         AppLogDebug("OnTouchPressed");
297
298         __touchStart = currentPosition;
299         CheckCurrentPosition(currentPosition);
300
301         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
302 }
303
304 void
305 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
306 {
307         if (++__skipMoveEvent % 5 != 0)
308         {
309                 return;
310         }
311         else
312         {
313                 __skipMoveEvent = 0;
314         }
315
316         int deltaX = currentPosition.x - __touchStart.x;
317         int deltaY = currentPosition.y - __touchStart.y;
318         if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_AUTO, false))
319         {
320                 switch(__pointLocation)
321                 {
322                 case INSIDE_TOP_RECTANGLE:
323                 {
324                         if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3)))
325                         {
326                                 __cropBox.y += deltaY;
327                                 __cropBox.height -= deltaY;
328                         }
329                 }
330                 break;
331
332                 case INSIDE_BOTTOM_RECTANGLE:
333                 {
334                         if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
335                                 && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
336                         {
337                                 __cropBox.height += deltaY;
338                         }
339                 }
340                 break;
341
342                 case INSIDE_LEFT_RECTANGLE:
343                 {
344                         if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
345                         {
346                                 __cropBox.x += deltaX;
347                                 __cropBox.width -= deltaX;
348                         }
349                 }
350                 break;
351
352                 case INSIDE_RIGHT_RECTANGLE:
353                 {
354                         if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
355                                 && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
356                         {
357                                 __cropBox.width += deltaX;
358                         }
359                 }
360                 break;
361
362                 case INSIDE_TOP_LEFT_RECTANGLE:
363                 {
364                         if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
365                                         && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
366                         {
367                                 __cropBox.y += deltaY;
368                                 __cropBox.height -= deltaY;
369                                 __cropBox.x += deltaX;
370                                 __cropBox.width -= deltaX;
371                         }
372                 }
373                 break;
374
375                 case INSIDE_TOP_RIGHT_RECTANGLE:
376                 {
377                         if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
378                                         && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
379                         {
380                                 __cropBox.width += deltaX;
381                                 __cropBox.y += deltaY;
382                                 __cropBox.height -= deltaY;
383                         }
384                 }
385                 break;
386
387                 case INSIDE_BOTTOM_LEFT_RECTANGLE:
388                 {
389                         if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3)
390                                 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
391                         {
392                                 __cropBox.x += deltaX;
393                                 __cropBox.width -= deltaX;
394                                 __cropBox.height += deltaY;
395                         }
396                 }
397                 break;
398
399                 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
400                 {
401                         if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3)
402                                 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
403                         {
404                                 __cropBox.width += deltaX;
405                                 __cropBox.height += deltaY;
406                         }
407                 }
408                 break;
409
410                 case INSIDE_CROPBOX:
411                 {
412                         if ((__cropBox.x + deltaX) > __imageBox.x)
413                         {
414                                 __cropBox.x += deltaX;
415                         }
416                         else
417                         {
418                                 __cropBox.x = __imageBox.x;
419                         }
420                         if ((__cropBox.y + deltaY) > __imageBox.y)
421                         {
422                                 __cropBox.y += deltaY;
423                         }
424                         else
425                         {
426                                 __cropBox.y = __imageBox.y;
427                         }
428                         if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
429                         {
430                                 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
431                         }
432                         if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
433                         {
434                                 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
435                         }
436                 }
437                 break;
438
439                 default:
440                         break;
441                 }
442         }
443         else if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN, false))
444         {
445                 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
446                         || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
447                 {
448                         int temp = __cropBox.height;
449                         int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
450                         temp = height - temp;
451                         if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
452                         {
453                                 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
454                                         && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
455                                         && (__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)))
456                                 {
457                                         __cropBox.x += deltaX;
458                                         __cropBox.width = __cropBox.width - (2 * deltaX);
459                                         __cropBox.height = height;
460                                         __cropBox.y = __cropBox.y - (temp / 2);
461                                 }
462                                 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
463                                                 && __cropBox.width < __imageBox.width
464                                                 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
465                                 {
466                                         if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
467                                                 && __cropBox.x + deltaX >= __imageBox.x)
468                                         {
469                                                 __cropBox.x += deltaX;
470                                         }
471                                         else
472                                         {
473                                                 __cropBox.x = __cropBox.x + (2 * deltaX);
474                                         }
475                                         __cropBox.width = __cropBox.width - (2 * deltaX);
476                                         __cropBox.height = height;
477                                         __cropBox.y = __cropBox.y - temp;
478                                 }
479                                 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
480                                                 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
481                                 {
482                                         if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
483                                         {
484                                                 __cropBox.x += deltaX;
485                                         }
486                                         else
487                                         {
488                                                 __cropBox.x = __cropBox.x + (2 * deltaX);
489                                         }
490                                         __cropBox.width = __cropBox.width - (2 * deltaX);
491                                         __cropBox.height = height;
492                                 }
493                         }
494                 }
495                 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
496                                 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
497                 {
498                         deltaX *= -1;
499                         int temp = __cropBox.height;
500                         int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
501                         temp = height - temp;
502                         if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
503                         {
504                                 if (deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
505                                 {
506                                         if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
507                                                 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
508                                         {
509                                                 __cropBox.x += deltaX;
510                                                 __cropBox.width = __cropBox.width - (2 * deltaX);;
511                                                 __cropBox.height = height;
512                                                 __cropBox.y = __cropBox.y - (temp / 2);
513                                         }
514                                         else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
515                                                         && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
516                                                         && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
517                                         {
518                                                 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
519                                                         && __cropBox.x + deltaX >= __imageBox.x)
520                                                 {
521                                                         __cropBox.x += deltaX;
522                                                 }
523                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
524                                                 __cropBox.height = height;
525                                                 __cropBox.y = __cropBox.y - temp;
526                                         }
527                                         else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
528                                                         && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
529                                         {
530                                                 if (__cropBox.x + deltaX >= __imageBox.x)
531                                                 {
532                                                         __cropBox.x += deltaX;
533                                                 }
534                                                 else
535                                                 {
536                                                         __cropBox.x = __imageBox.x;
537                                                 }
538                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
539                                                 __cropBox.height = height;
540                                         }
541                                         else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
542                                                         && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
543                                                         && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
544                                         {
545                                                 __cropBox.x += deltaX;
546                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
547                                                 __cropBox.height = height;
548                                         }
549                                         else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
550                                                         && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
551                                                         && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
552                                         {
553                                                 __cropBox.width = __cropBox.width - (2 * deltaX);
554                                                 __cropBox.y = __cropBox.y - (temp / 2);
555                                                 __cropBox.height = height;
556                                         }
557                                 }
558                         }
559                 }
560                 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
561                 {
562                         int temp = __cropBox.width;
563                         int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
564                         temp = width - temp;
565                         if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
566                         {
567                                 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
568                                         && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
569                                 {
570                                         __cropBox.y += deltaY;
571                                         __cropBox.height = __cropBox.height - (2 * deltaY);
572                                         __cropBox.width = width;
573                                         __cropBox.x = __cropBox.x - (temp / 2);
574                                 }
575                                 else if (__cropBox.x - (temp / 2) < __imageBox.x)
576                                 {
577                                         if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
578                                         {
579                                                 __cropBox.width = width;
580                                         }
581                                         if (__cropBox.y + deltaY >= __imageBox.y)
582                                         {
583                                                 __cropBox.y += deltaY;
584                                         }
585                                         if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
586                                         {
587                                                 __cropBox.height = __cropBox.height - (2 * deltaY);
588                                         }
589                                 }
590                                 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
591                                                 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
592                                 {
593                                         if (__cropBox.x - temp >= __imageBox.x
594                                                 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
595                                         {
596                                                 __cropBox.x = __cropBox.x - temp;
597                                         }
598                                         else if (__cropBox.x - temp >= __imageBox.x)
599                                         {
600                                                 __cropBox.x = __cropBox.x - temp / 2;
601                                         }
602                                         if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
603                                         {
604                                                 __cropBox.width = width;
605                                         }
606                                         if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
607                                         {
608                                                 __cropBox.y = __cropBox.y + (2 * deltaY);
609                                                 __cropBox.height = __cropBox.height - (2 * deltaY);
610                                         }
611                                 }
612                                 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
613                                                 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
614                                                 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
615                                 {
616                                         __cropBox.x = __cropBox.x - temp;
617                                         __cropBox.width = width;
618                                         __cropBox.y += deltaY;
619                                         __cropBox.height = __cropBox.height - (2 * deltaY);
620                                 }
621                         }
622                 }
623                 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
624                 {
625                         deltaY = deltaY * (-1);
626                         int temp = __cropBox.width;
627                         int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
628                         temp = width - temp;
629                         if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
630                         {
631                                 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
632                                         && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
633                                 {
634                                         __cropBox.y += deltaY;
635                                         __cropBox.height = __cropBox.height - (2 * deltaY);;
636                                         __cropBox.width = width;
637                                         __cropBox.x = __cropBox.x - (temp / 2);
638                                 }
639                                 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
640                                                 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
641                                 {
642                                         __cropBox.x = __cropBox.x - (temp / 2);
643                                         __cropBox.width = width;
644                                         __cropBox.height = __cropBox.height - (2 * deltaY);
645                                 }
646                                 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
647                                                 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
648                                                 && __cropBox.x - temp >= __imageBox.x)
649                                 {
650                                         __cropBox.x = __cropBox.x - temp;
651                                         __cropBox.y += deltaY;
652                                         __cropBox.width = width;
653                                         __cropBox.height = __cropBox.height - (2 * deltaY);
654                                 }
655                                 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
656                                                 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
657                                                 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
658                                 {
659                                         __cropBox.x -= temp;
660                                         __cropBox.width = width;
661                                         __cropBox.height = __cropBox.height - (2 * deltaY);
662                                 }
663                                 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
664                                                 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
665                                 {
666                                         if (__cropBox.y + deltaY >= __imageBox.y)
667                                         {
668                                                 __cropBox.y += deltaY;
669                                         }
670                                         __cropBox.width = width;
671                                         __cropBox.height = __cropBox.height - (2 * deltaY);
672                                 }
673                         }
674                 }
675                 else if (__pointLocation == INSIDE_CROPBOX)
676                 {
677                         if ((__cropBox.x + deltaX) > __imageBox.x)
678                         {
679                                 __cropBox.x += deltaX;
680                         }
681                         else
682                         {
683                                 __cropBox.x = __imageBox.x;
684                         }
685                         if ((__cropBox.y + deltaY) > __imageBox.y)
686                         {
687                                 __cropBox.y += deltaY;
688                         }
689                         else
690                         {
691                                 __cropBox.y = __imageBox.y;
692                         }
693                         if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
694                         {
695                                 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
696                         }
697                         if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
698                         {
699                                 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
700                         }
701                 }
702         }
703         RequestRedraw(false);
704         __touchStart = currentPosition;
705 }
706
707 void
708 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
709 {
710         AppLogDebug("OnTouchReleased");
711
712         __pointLocation = OUTSIDE_CROPBOX;
713 }
714
715 void
716 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
717 {
718         AppLogDebug("OnSceneActivatedN");
719
720         if (pArgs != null)
721         {
722                 __sourceFilePath = *(static_cast<String*>(pArgs->GetAt(0)));
723                 pArgs->RemoveAll(true);
724                 delete pArgs;
725
726                 if (__sourceFilePath.IsEmpty() == false)
727                 {
728                         result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
729                         if (r == E_SUCCESS)
730                         {
731                                 __imageWidth = __imageBuffer.GetWidth();
732                                 __imageHeight = __imageBuffer.GetHeight();
733
734                                 int physicalHeight = CoordinateSystem::ConvertToPhysicalY(__imageHeight);
735                                 int physicalWidth = CoordinateSystem::ConvertToPhysicalX(__imageWidth);
736                                 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_ARGB8888, physicalWidth, physicalHeight);
737                                 SetValue();
738
739                                 Image img;
740                                 r = img.Construct();
741                                 if (r == E_SUCCESS)
742                                 {
743                                         __imageFormat = img.GetImageFormat(__sourceFilePath);
744                                 }
745                         }
746                         else
747                         {
748                                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
749                                 if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
750                                 {
751                                         pApp->SendAppControlResult(APP_CTRL_RESULT_FAILED, null);
752                                         pApp->Terminate();
753                                 }
754                         }
755                 }
756         }
757         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
758 }
759
760 void
761 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
762 {
763         AppLogDebug("OnSceneDeactivated");
764
765         RemoveOrientationEventListener(*this);
766 }
767
768 void
769 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
770 {
771         AppLogDebug("CheckCurrentPosition");
772
773         if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
774                 && currentPosition.y >  (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
775         {
776                 __pointLocation = INSIDE_TOP_RECTANGLE;
777         }
778         else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
779                 && currentPosition.y >  ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
780         {
781                 __pointLocation = INSIDE_LEFT_RECTANGLE;
782         }
783         else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
784                 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
785         {
786                 __pointLocation = INSIDE_RIGHT_RECTANGLE;
787         }
788         else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
789                 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
790         {
791                 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
792         }
793         else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
794                          && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
795         {
796                 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
797         }
798         else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
799                         && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
800         {
801                 __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
802         }
803         else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
804                         && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
805         {
806                 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
807         }
808         else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
809                         && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
810         {
811                 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
812         }
813         else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
814         {
815                 __pointLocation = INSIDE_CROPBOX;
816         }
817         else
818         {
819                 __pointLocation = OUTSIDE_CROPBOX;
820         }
821         return;
822 }
823
824 void
825 ImageCropForm::SetValue(void)
826 {
827         int prevHeight = __imageBox.height;
828         int prevWidth = __imageBox.width;
829         int prevImageboxX = __imageBox.x;
830         int prevImageBoxY = __imageBox.y;
831
832         if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
833         {
834                 __formHeight = Form::GetBounds().height;
835                 __formWidth = Form::GetBounds().width;
836         }
837         else if (GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
838         {
839                 __formHeight = Form::GetBounds().width;
840                 __formWidth = Form::GetBounds().height;
841         }
842
843         float clientAreaRatio = (GetClientAreaBoundsF().height) / GetClientAreaBoundsF().width;
844
845         if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > GetClientAreaBounds().height)
846         {
847                 float imageRatio = __imageHeight / (float) __imageWidth;
848                 if (imageRatio < clientAreaRatio)
849                 {
850                         __imageBox.width = GetClientAreaBounds().width;
851                         __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
852                         __imageBox.x = GetClientAreaBounds().x;
853                         __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2;
854                 }
855                 else
856                 {
857                         __imageBox.height = GetClientAreaBounds().height;
858                         __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
859                         __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
860                         __imageBox.y = GetClientAreaBounds().y;
861                 }
862         }
863         else
864         {
865                 __imageBox.height = __imageHeight;
866                 __imageBox.width = __imageWidth;
867                 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
868                 __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2 ;
869         }
870         if (__isOrientationChanged == false)
871         {
872                 if (__imageBox.height > __imageBox.width)
873                 {
874                         __cropBox.width = (2 * __imageBox.width) / 3;
875                         __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
876                         if (__cropBox.height > __imageBox.height)
877                         {
878                                 __cropBox.height = (2 * __imageBox.height) / 3;
879                                 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
880                         }
881                         __cropBox.y =  __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
882                         __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
883                 }
884                 else
885                 {
886                         __cropBox.height = (2 *__imageBox.height) / 3;
887                         __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
888                         if (__cropBox.width > __imageBox.width)
889                         {
890                                 __cropBox.width = (2 * __imageBox.width) / 3;
891                                 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
892                         }
893                         __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
894                         __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
895                 }
896         }
897         else
898         {
899                 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float) prevWidth;
900                 __cropBox.x = (temp + 0.5);
901                 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float) prevHeight;
902                 __cropBox.y = (temp + 0.5);
903                 temp = (__cropBox.height * __imageBox.height) / (float) prevHeight;
904                 __cropBox.height = (temp + 0.5);
905                 temp = (__cropBox.width * __imageBox.width) / (float) prevWidth;
906                 __cropBox.width = (temp + 0.5);
907         }
908         __pCanvas = GetCanvasN(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height));
909         if (__pCanvas != null)
910         {
911                 __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
912                 __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
913         }
914 }
915
916 String
917 ImageCropForm::GetFileName(const String& filePath, bool isCheckExe) const
918 {
919         String strResult;
920         int lastIndex = -1 ;
921
922         filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, lastIndex);
923         filePath.SubString(lastIndex+1, strResult);
924
925         if (isCheckExe == true)
926         {
927                 String strTemp = strResult;
928
929                 lastIndex = -1;
930                 strTemp.LastIndexOf(FILE_EXT_SEPARATOR, strTemp.GetLength() - 1, lastIndex);
931                 strTemp.SubString(0, lastIndex, strResult);
932         }
933
934         return strResult;
935 }