04360f728c3bf97d0f5872cc7274847c93bfc4e6
[profile/ivi/org.tizen.browser.git] / src / browser-extension / browser-find-word.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.tizenopensource.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
19 #include "browser-find-word.h"
20 #include "browser-window.h"
21
22 #define FIND_WORD_MAX_COUNT     999
23
24 Browser_Find_Word::Browser_Find_Word(Browser_View *browser_view)
25 //:     m_page(NULL)
26 :       m_find_word_index(0)
27         ,m_find_word_max_count(0)
28         ,m_browser_view(browser_view)
29 {
30         BROWSER_LOGD("[%s]", __func__);
31 }
32
33 Browser_Find_Word::~Browser_Find_Word(void)
34 {
35         BROWSER_LOGD("[%s]", __func__);
36 }
37
38 void Browser_Find_Word::__did_find_string_cb(Evas_Object* o, const char* string, int match_count, void* user_data)
39 {
40         BROWSER_LOGD("match_count = %d", match_count);
41
42         Browser_Find_Word *find_word = (Browser_Find_Word *)user_data;
43         find_word->m_find_word_max_count = (unsigned int)match_count;
44
45         if (match_count == 0) {
46                 find_word->m_find_word_index = 0;
47                 find_word->m_find_word_max_count = 0;
48
49                 find_word->m_browser_view->_update_find_word_index_text("0/0");
50                 return;
51         }
52
53         if (find_word->m_find_word_max_count < 0 || find_word->m_find_word_max_count > FIND_WORD_MAX_COUNT) {
54                 BROWSER_LOGD("Matched word counts are over Max. which browser supported. Set as Maximum");
55                 find_word->m_find_word_max_count = FIND_WORD_MAX_COUNT;
56         }
57
58         GString *index_string = g_string_new(NULL);
59         if (!index_string) {
60                 BROWSER_LOGE("g_string_new failed");
61                 return;
62         }
63         g_string_printf(index_string, "%d/%d", find_word->m_find_word_index, find_word->m_find_word_max_count);
64         find_word->m_browser_view->_update_find_word_index_text(index_string->str);
65         g_string_free(index_string, true);
66 }
67
68 int Browser_Find_Word::find_word(const char *word, Find_Word_Direction direction)
69 {
70         BROWSER_LOGD("word to find=[%s]", word);
71
72         if (direction == BROWSER_FIND_WORD_FORWARD) {
73                 m_find_word_index++;
74
75                 ewk_view_string_find(m_browser_view->m_focused_window->m_ewk_view, word,
76                         EWK_FIND_OPTIONS_CASE_INSENSITIVE | EWK_FIND_OPTIONS_WRAP_AROUND
77                         | EWK_FIND_OPTIONS_SHOW_FIND_INDICATOR | EWK_FIND_OPTIONS_SHOW_HIGHLIGHT, FIND_WORD_MAX_COUNT,
78                         __did_find_string_cb, this);
79         } else if (direction == BROWSER_FIND_WORD_BACKWARD) {
80                 m_find_word_index--;
81                 ewk_view_string_find(m_browser_view->m_focused_window->m_ewk_view, word,
82                         EWK_FIND_OPTIONS_CASE_INSENSITIVE | EWK_FIND_OPTIONS_BACKWARDS | EWK_FIND_OPTIONS_WRAP_AROUND
83                         | EWK_FIND_OPTIONS_SHOW_FIND_INDICATOR | EWK_FIND_OPTIONS_SHOW_HIGHLIGHT, FIND_WORD_MAX_COUNT,
84                         __did_find_string_cb, this);
85         }
86         if (m_find_word_index > FIND_WORD_MAX_COUNT)
87                 m_find_word_index = FIND_WORD_MAX_COUNT;
88
89         if (m_find_word_index < 0)
90                 m_find_word_index = 0;
91
92         return m_find_word_index;
93 }
94
95 void Browser_Find_Word::init_index()
96 {
97         m_find_word_index = 0;
98 }
99
100 int Browser_Find_Word::get_match_max_value()
101 {
102         return m_find_word_max_count;
103 }