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