url_bar.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "url_bar.h"
  26. #include "url_utils.h"
  27. #include <Edje.h>
  28. #include <Ecore_Evas.h>
  29. #define PADDING_SIZE 5
  30. static char *
  31. _url_bar_url_get_with_protocol(Url_Bar *urlBar)
  32. {
  33. const char *url = edje_object_part_text_get(urlBar->entry, "url.text");
  34. return url_from_user_input(url);
  35. }
  36. static void
  37. on_urlbar_key_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
  38. {
  39. Evas_Event_Key_Down *ev = event_info;
  40. Url_Bar *urlBar = (Url_Bar *)data;
  41. if (!ev->key || strcmp(ev->key, "Return"))
  42. return;
  43. char *url = _url_bar_url_get_with_protocol(urlBar);
  44. if (url) {
  45. ewk_view_uri_set(urlBar->webView, url);
  46. free(url);
  47. }
  48. evas_object_focus_set(urlBar->webView, EINA_TRUE);
  49. }
  50. static void
  51. on_urlbar_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
  52. {
  53. Evas_Event_Mouse_Down *ev = event_info;
  54. Url_Bar *urlBar = (Url_Bar *)data;
  55. if (ev->button == 1) {
  56. evas_object_focus_set(urlBar->entry, EINA_TRUE);
  57. edje_object_signal_emit(urlBar->entry, "entry,action,focus", "entry");
  58. }
  59. }
  60. static void
  61. on_urlbar_focus_out(void *data, Evas *e, Evas_Object *obj, void *event_info)
  62. {
  63. Url_Bar *urlBar = (Url_Bar *)data;
  64. edje_object_signal_emit(urlBar->entry, "entry,action,unfocus", "entry");
  65. }
  66. void
  67. url_bar_width_set(Url_Bar *urlBar, int width)
  68. {
  69. evas_object_move(urlBar->area, 0, 0);
  70. evas_object_resize(urlBar->area, width, URL_BAR_HEIGHT);
  71. evas_object_move(urlBar->entry, PADDING_SIZE, PADDING_SIZE);
  72. evas_object_resize(urlBar->entry, width - PADDING_SIZE * 2, URL_BAR_HEIGHT - PADDING_SIZE * 2);
  73. }
  74. Url_Bar *
  75. url_bar_add(Evas_Object *webView, int width)
  76. {
  77. Evas *evas;
  78. Url_Bar *url_bar;
  79. if (!webView)
  80. return NULL;
  81. evas = evas_object_evas_get(webView);
  82. url_bar = (Url_Bar *)malloc(sizeof(Url_Bar));
  83. url_bar->webView = webView;
  84. url_bar->area = evas_object_rectangle_add(evas);
  85. evas_object_name_set(url_bar->area, "url_barArea");
  86. evas_object_color_set(url_bar->area, 255, 255, 255, 255);
  87. url_bar->entry = edje_object_add(evas);
  88. Eina_Bool ret = edje_object_file_set(url_bar->entry, TEST_THEME_DIR "/entry.edj", "control/entry/base/default");
  89. if (!ret) {
  90. evas_object_del(url_bar->area);
  91. free(url_bar);
  92. return NULL;
  93. }
  94. edje_object_part_text_set(url_bar->entry, "url.text", "");
  95. /* Set URL bar dimensions and show it */
  96. url_bar_width_set(url_bar, width);
  97. evas_object_show(url_bar->area);
  98. evas_object_show(url_bar->entry);
  99. evas_object_event_callback_add(url_bar->entry, EVAS_CALLBACK_MOUSE_DOWN, on_urlbar_mouse_down, url_bar);
  100. evas_object_event_callback_add(url_bar->entry, EVAS_CALLBACK_KEY_DOWN, on_urlbar_key_down, url_bar);
  101. evas_object_event_callback_add(url_bar->entry, EVAS_CALLBACK_FOCUS_OUT, on_urlbar_focus_out, url_bar);
  102. return url_bar;
  103. }
  104. void
  105. url_bar_del(Url_Bar *urlBar)
  106. {
  107. if (!urlBar)
  108. return;
  109. evas_object_event_callback_del(urlBar->entry, EVAS_CALLBACK_KEY_DOWN, on_urlbar_key_down);
  110. evas_object_event_callback_del(urlBar->entry, EVAS_CALLBACK_MOUSE_DOWN, on_urlbar_mouse_down);
  111. evas_object_event_callback_del(urlBar->entry, EVAS_CALLBACK_FOCUS_OUT, on_urlbar_focus_out);
  112. evas_object_del(urlBar->area);
  113. evas_object_del(urlBar->entry);
  114. free(urlBar);
  115. }
  116. void
  117. url_bar_url_set(Url_Bar *urlBar, const char *url)
  118. {
  119. if (!urlBar || !url)
  120. return;
  121. edje_object_part_text_set(urlBar->entry, "url.text", url);
  122. }