|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2014 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include "text.h" |
|
33 #include "container.h" |
|
34 |
|
35 UIWIDGET ui_textarea(UiObject *obj, UiText *value) { |
|
36 GtkWidget *text_area = gtk_text_view_new(); |
|
37 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_area), GTK_WRAP_WORD_CHAR); |
|
38 g_signal_connect( |
|
39 text_area, |
|
40 "realize", |
|
41 G_CALLBACK(ui_textarea_realize_event), |
|
42 NULL); |
|
43 |
|
44 GtkWidget *scroll_area = gtk_scrolled_window_new (NULL, NULL); |
|
45 gtk_scrolled_window_set_policy( |
|
46 GTK_SCROLLED_WINDOW(scroll_area), |
|
47 GTK_POLICY_AUTOMATIC, |
|
48 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS |
|
49 gtk_container_add(GTK_CONTAINER(scroll_area), text_area); |
|
50 |
|
51 // font and padding |
|
52 PangoFontDescription *font; |
|
53 font = pango_font_description_from_string("Monospace"); |
|
54 gtk_widget_modify_font(text_area, font); |
|
55 pango_font_description_free(font); |
|
56 |
|
57 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2); |
|
58 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2); |
|
59 |
|
60 // add |
|
61 UiContainer *ct = uic_get_current_container(obj); |
|
62 ct->add(ct, scroll_area); |
|
63 |
|
64 // bind value |
|
65 if(value) { |
|
66 value->get = ui_textarea_get; |
|
67 value->set = ui_textarea_set; |
|
68 value->value = NULL; |
|
69 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area)); |
|
70 value->obj = buf; |
|
71 |
|
72 // register undo manager |
|
73 g_signal_connect( |
|
74 buf, |
|
75 "insert-text", |
|
76 G_CALLBACK(ui_textbuf_insert), |
|
77 NULL); |
|
78 g_signal_connect( |
|
79 buf, |
|
80 "delete-range", |
|
81 G_CALLBACK(ui_textbuf_delete), |
|
82 NULL); |
|
83 } |
|
84 |
|
85 return scroll_area; |
|
86 } |
|
87 |
|
88 char* ui_textarea_get(UiText *text) { |
|
89 if(text->value) { |
|
90 g_free(text->value); |
|
91 } |
|
92 GtkTextBuffer *buf = text->obj; |
|
93 GtkTextIter start; |
|
94 GtkTextIter end; |
|
95 gtk_text_buffer_get_bounds(buf, &start, &end); |
|
96 char *str = gtk_text_buffer_get_text(buf, &start, &end, FALSE); |
|
97 text->value = str; |
|
98 return str; |
|
99 } |
|
100 |
|
101 void ui_textarea_set(UiText *text, char *str) { |
|
102 if(text->value) { |
|
103 g_free(text->value); |
|
104 text->value = NULL; |
|
105 } |
|
106 gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1); |
|
107 } |
|
108 |
|
109 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) { |
|
110 gtk_widget_grab_focus(widget); |
|
111 } |
|
112 |
|
113 void ui_textbuf_insert( |
|
114 GtkTextBuffer *textbuffer, |
|
115 GtkTextIter *location, |
|
116 char *text, |
|
117 int len, |
|
118 void *data) |
|
119 { |
|
120 //TODO |
|
121 } |
|
122 |
|
123 void ui_textbuf_delete( |
|
124 GtkTextBuffer *textbuffer, |
|
125 GtkTextIter *start, |
|
126 GtkTextIter *end, |
|
127 void *data) |
|
128 { |
|
129 //TODO |
|
130 } |