aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_widget_textblock.c
blob: ecd3e1487c94f627c10749fc605bb6dd424b9cea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "e.h"

typedef struct _E_Widget_Data E_Widget_Data;
struct _E_Widget_Data
{
   Evas_Object *o_widget, *o_entry;
};

static void _e_wid_del_hook(Evas_Object *obj);
static void _e_wid_focus_hook(Evas_Object *obj);
static void _e_wid_focus_steal(void *data, Evas *e, Evas_Object *obj, void *event_info);

/* externally accessible functions */
EAPI Evas_Object *
e_widget_textblock_add(Evas *evas)
{
   Evas_Object *obj, *o;
   E_Widget_Data *wd;

   obj = e_widget_add(evas);

   e_widget_del_hook_set(obj, _e_wid_del_hook);
   e_widget_focus_hook_set(obj, _e_wid_focus_hook);
   wd = calloc(1, sizeof(E_Widget_Data));
   e_widget_data_set(obj, wd);

   wd->o_entry = o = elm_entry_add(e_win_evas_win_get(evas));
   elm_entry_scrollable_set(o, 1);
   elm_entry_editable_set(o, 0);
   evas_object_show(o);
   e_widget_sub_object_add(obj, o);
   e_widget_resize_object_set(obj, o);
   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _e_wid_focus_steal, obj);

   evas_object_resize(obj, 32, 32);
   e_widget_size_min_set(obj, 32, 32);
   return obj;
}

EAPI void
e_widget_textblock_markup_set(Evas_Object *obj, const char *text)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   elm_entry_entry_set(wd->o_entry, text);
}

EAPI void
e_widget_textblock_plain_set(Evas_Object *obj, const char *text)
{
   char *markup;
   char *d;
   const char *p;
   int mlen;

   if (!text)
     {
        e_widget_textblock_markup_set(obj, NULL);
        return;
     }
   mlen = strlen(text);
   /* need to look for these and replace with a new string (on the right)
    * '\n' -> "<br>"
    * '\t' -> "        "
    * '<'  -> "&lt;"
    * '>'  -> "&gt;"
    * '&'  -> "&amp;"
    */
   for (p = text; *p != 0; p++)
     {
        if (*p == '\n') mlen += 4 - 1;
        else if (*p == '\t')
          mlen += 8 - 1;
        else if (*p == '<')
          mlen += 4 - 1;
        else if (*p == '>')
          mlen += 4 - 1;
        else if (*p == '&')
          mlen += 5 - 1;
     }
   markup = alloca(mlen + 1);
   if (!markup) return;
   markup[0] = 0;
   for (d = markup, p = text; *p != 0; p++, d++)
     {
        if (*p == '\n')
          {
             strcpy(d, "<br>");
             d += 4 - 1;
          }
        else if (*p == '\t')
          {
             strcpy(d, "        ");
             d += 8 - 1;
          }
        else if (*p == '<')
          {
             strcpy(d, "&lt;");
             d += 4 - 1;
          }
        else if (*p == '>')
          {
             strcpy(d, "&gt;");
             d += 4 - 1;
          }
        else if (*p == '&')
          {
             strcpy(d, "&amp;");
             d += 5 - 1;
          }
        else *d = *p;
     }
   *d = 0;
   elm_entry_entry_set(obj, markup);
}

static void
_e_wid_del_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   free(wd);
}

static void
_e_wid_focus_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   elm_object_focus_set(wd->o_entry, e_widget_focus_get(obj));
}

static void
_e_wid_focus_steal(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
{
   e_widget_focus_steal(data);
}