ui/motif/text.c

Sun, 24 Jan 2016 19:57:16 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 24 Jan 2016 19:57:16 +0100
changeset 113
500c085d2133
parent 90
2019fdbaadfd
child 122
e82b01d17a78
permissions
-rw-r--r--

added box spacing (Motif)

5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "text.h"
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "container.h"
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 UiContainer *ct = uic_get_current_container(obj);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 int n = 0;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 Arg args[16];
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 //XtSetArg(args[n], XmNeditable, TRUE);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 //n++;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 n++;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
60
7cd1b8890302 added vbox container (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
45 Widget parent = ct->prepare(ct, args, &n, TRUE);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 Widget text_area = XmCreateScrolledText(parent, "text_area", args, n);
63
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
47 ct->add(ct, XtParent(text_area));
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 XtManageChild(text_area);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
22
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
50 UiTextArea *uitext = ucx_mempool_malloc(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
51 obj->ctx->mempool,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
52 sizeof(UiTextArea));
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
53 uitext->ctx = obj->ctx;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
54 uitext->last_selection_state = 0;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
55 XtAddCallback(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
56 text_area,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
57 XmNmotionVerifyCallback,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
58 (XtCallbackProc)ui_text_selection_callback,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
59 uitext);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
60
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 // bind value
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 if(value) {
36
e4198fc2ead4 fixed list, text and added ui_close (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 34
diff changeset
63 if(value->value) {
e4198fc2ead4 fixed list, text and added ui_close (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 34
diff changeset
64 XmTextSetString(text_area, value->value);
63
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
65 XtFree(value->value);
36
e4198fc2ead4 fixed list, text and added ui_close (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 34
diff changeset
66 }
e4198fc2ead4 fixed list, text and added ui_close (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 34
diff changeset
67
12
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
68 value->set = ui_textarea_set;
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 value->get = ui_textarea_get;
12
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
70 value->getsubstr = ui_textarea_getsubstr;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
71 value->insert = ui_textarea_insert;
90
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
72 value->setposition = ui_textarea_setposition;
27
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
73 value->position = ui_textarea_position;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
74 value->selection = ui_textarea_selection;
29
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
75 value->length = ui_textarea_length;
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 value->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 value->obj = text_area;
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
78
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
79 if(!value->undomgr) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
80 value->undomgr = ui_create_undomgr();
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
81 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
82
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
83 XtAddCallback(
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
84 text_area,
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
85 XmNmodifyVerifyCallback,
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
86 (XtCallbackProc)ui_text_modify_callback,
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
87 value);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 return text_area;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
34
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
93 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) {
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
94 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_TEXT);
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
95 if(var) {
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
96 UiText *value = var->value;
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
97 return ui_textarea(obj, value);
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
98 } else {
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
99 // TODO: error
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
100 }
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
101 return NULL;
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
102 }
0ec8a5f17782 added listview and sidebar (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
103
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 char* ui_textarea_get(UiText *text) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 XtFree(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 char *str = XmTextGetString(text->obj);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 text->value = str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 return str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 void ui_textarea_set(UiText *text, char *str) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 XtFree(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 text->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 XmTextSetString(text->obj, str);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
120
12
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
121 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
122 if(text->value) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
123 XtFree(text->value);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
124 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
125 int length = end - begin;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
126 char *str = XtMalloc(length + 1);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
127 XmTextGetSubstring(text->obj, begin, length, length + 1, str);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
128 text->value = str;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
129 return str;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
130 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
131
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
132 void ui_textarea_insert(UiText *text, int pos, char *str) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
133 if(text->value) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
134 XtFree(text->value);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
135 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
136 text->value = NULL;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
137 XmTextInsert(text->obj, pos, str);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
138 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
139
90
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
140 void ui_textarea_setposition(UiText *text, int pos) {
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
141 XmTextSetInsertionPosition(text->obj, pos);
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
142 }
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
143
27
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
144 int ui_textarea_position(UiText *text) {
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
145 long begin;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
146 long end;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
147 XmTextGetSelectionPosition(text->obj, &begin, &end);
90
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
148 text->pos = begin;
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 63
diff changeset
149 return text->pos;
27
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
150 }
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
151
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
152 void ui_textarea_selection(UiText *text, int *begin, int *end) {
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
153 XmTextGetSelectionPosition(text->obj, (long*)begin, (long*)end);
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
154 }
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
155
29
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
156 int ui_textarea_length(UiText *text) {
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
157 return (int)XmTextGetLastPosition(text->obj);
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
158 }
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
159
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
160 UiUndoMgr* ui_create_undomgr() {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
161 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
162 mgr->begin = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
163 mgr->cur = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
164 mgr->length = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
165 mgr->event = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
166 return mgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
167 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
168
22
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
169 void ui_text_selection_callback(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
170 Widget widget,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
171 UiTextArea *textarea,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
172 XtPointer data)
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
173 {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
174 long left = 0;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
175 long right = 0;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
176 XmTextGetSelectionPosition(widget, &left, &right);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
177 int sel = left < right ? 1 : 0;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
178 if(sel != textarea->last_selection_state) {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
179 if(sel) {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
180 ui_set_group(textarea->ctx, UI_GROUP_SELECTION);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
181 } else {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
182 ui_unset_group(textarea->ctx, UI_GROUP_SELECTION);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
183 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
184 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
185 textarea->last_selection_state = sel;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
186 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
187
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
188 void ui_text_modify_callback(Widget widget, UiText *value, XtPointer data) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
189 XmTextVerifyCallbackStruct *txv = (XmTextVerifyCallbackStruct*)data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
190 int type = txv->text->length > 0 ? UI_TEXTBUF_INSERT : UI_TEXTBUF_DELETE;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
191 UiUndoMgr *mgr = value->undomgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
192 if(!mgr->event) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
193 return;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
194 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
195
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
196 char *text = txv->text->ptr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
197 int length = txv->text->length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
198
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
199 if(mgr->cur) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
200 UcxList *elm = mgr->cur->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
201 if(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
202 mgr->cur->next = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
203 while(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
204 elm->prev = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
205 UcxList *next = elm->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
206 ui_free_textbuf_op(elm->data);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
207 free(elm);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
208 elm = next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
209 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
210 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
211
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
212 if(type == UI_TEXTBUF_INSERT) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
213 UiTextBufOp *last_op = mgr->cur->data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
214 if(
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
215 last_op->type == UI_TEXTBUF_INSERT &&
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
216 ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
217 {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
218 // append text to last op
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
219 int ln = last_op->len;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
220 char *newtext = malloc(ln + length + 1);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
221 memcpy(newtext, last_op->text, ln);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
222 memcpy(newtext+ln, text, length);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
223 newtext[ln+length] = '\0';
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
224
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
225 last_op->text = newtext;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
226 last_op->len = ln + length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
227 last_op->end += length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
228
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
229 return;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
230 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
231 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
232 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
233
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
234 char *str;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
235 if(type == UI_TEXTBUF_INSERT) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
236 str = malloc(length + 1);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
237 memcpy(str, text, length);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
238 str[length] = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
239 } else {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
240 length = txv->endPos - txv->startPos;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
241 str = malloc(length + 1);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
242 XmTextGetSubstring(value->obj, txv->startPos, length, length+1, str);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
243 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
244
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
245 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
246 op->type = type;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
247 op->start = txv->startPos;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
248 op->end = txv->endPos + 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
249 op->len = length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
250 op->text = str;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
251
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
252 UcxList *elm = ucx_list_append(NULL, op);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
253 mgr->cur = elm;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
254 mgr->begin = ucx_list_concat(mgr->begin, elm);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
255 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
256
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
257 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
258 // return 1 if oldstr + newstr are one word
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
259
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
260 int has_space = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
261 for(int i=0;i<oldlen;i++) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
262 if(oldstr[i] < 33) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
263 has_space = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
264 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
265 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
266 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
267
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
268 for(int i=0;i<newlen;i++) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
269 if(has_space && newstr[i] > 32) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
270 return 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
271 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
272 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
273
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
274 return 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
275 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
276
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
277 void ui_free_textbuf_op(UiTextBufOp *op) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
278 if(op->text) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
279 free(op->text);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
280 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
281 free(op);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
282 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
283
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
284
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
285 void ui_text_undo(UiText *value) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
286 UiUndoMgr *mgr = value->undomgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
287
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
288 if(mgr->cur) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
289 UiTextBufOp *op = mgr->cur->data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
290 mgr->event = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
291 switch(op->type) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
292 case UI_TEXTBUF_INSERT: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
293 XmTextReplace(value->obj, op->start, op->end, "");
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
294 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
295 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
296 case UI_TEXTBUF_DELETE: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
297 XmTextInsert(value->obj, op->start, op->text);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
298 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
299 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
300 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
301 mgr->event = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
302 mgr->cur = mgr->cur->prev;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
303 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
304 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
305
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
306 void ui_text_redo(UiText *value) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
307 UiUndoMgr *mgr = value->undomgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
308
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
309 UcxList *elm = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
310 if(mgr->cur) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
311 if(mgr->cur->next) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
312 elm = mgr->cur->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
313 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
314 } else if(mgr->begin) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
315 elm = mgr->begin;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
316 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
317
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
318 if(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
319 UiTextBufOp *op = elm->data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
320 mgr->event = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
321 switch(op->type) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
322 case UI_TEXTBUF_INSERT: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
323 XmTextInsert(value->obj, op->start, op->text);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
324 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
325 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
326 case UI_TEXTBUF_DELETE: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
327 XmTextReplace(value->obj, op->start, op->end, "");
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
328 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
329 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
330 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
331 mgr->event = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
332 mgr->cur = elm;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
333 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
334 }
63
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
335
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
336
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
337 /* ------------------------- textfield ------------------------- */
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
338
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
339 UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
340 UiContainer *ct = uic_get_current_container(obj);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
341 int n = 0;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
342 Arg args[16];
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
343 XtSetArg(args[n], XmNeditMode, XmSINGLE_LINE_EDIT);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
344 n++;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
345
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
346 Widget parent = ct->prepare(ct, args, &n, FALSE);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
347 Widget textfield = XmCreateText(parent, "text_field", args, n);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
348 ct->add(ct, textfield);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
349 XtManageChild(textfield);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
350
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
351 // bind value
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
352 if(value) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
353 if(value->value) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
354 XmTextSetString(textfield, value->value);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
355 XtFree(value->value);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
356 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
357
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
358 value->set = ui_textfield_set;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
359 value->get = ui_textfield_get;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
360 value->value = NULL;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
361 value->obj = textfield;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
362 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
363
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
364 return textfield;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
365 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
366
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
367 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
368 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_STRING);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
369 if(var) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
370 UiString *value = var->value;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
371 return ui_textfield(obj, value);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
372 } else {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
373 // TODO: error
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
374 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
375 return NULL;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
376 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
377
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
378 char* ui_textfield_get(UiString *str) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
379 if(str->value) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
380 XtFree(str->value);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
381 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
382 char *value = XmTextGetString(str->obj);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
383 str->value = value;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
384 return value;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
385 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
386
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
387 void ui_textfield_set(UiString *str, char *value) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
388 if(str->value) {
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
389 XtFree(str->value);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
390 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
391 str->value = NULL;
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
392 XmTextSetString(str->obj, value);
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
393 }
46a42f0c4f93 added textfield and some container fixes (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
394

mercurial