ui/motif/text.c

Fri, 28 Mar 2014 20:03:49 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 28 Mar 2014 20:03:49 +0100
changeset 11
979c0e0ee853
parent 9
e70e855cea89
child 12
fe94e0fb9ef3
permissions
-rw-r--r--

merge

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
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 Widget parent = ct->add(ct, args, &n);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 Widget text_area = XmCreateScrolledText(parent, "text_area", args, n);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 XtManageChild(text_area);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 // bind value
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 if(value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 value->get = ui_textarea_get;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 value->set = ui_textarea_set;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 value->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 value->obj = text_area;
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
55
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
56 if(!value->undomgr) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
57 value->undomgr = ui_create_undomgr();
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
58 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
59
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
60 XtAddCallback(
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
61 text_area,
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
62 XmNmodifyVerifyCallback,
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
63 (XtCallbackProc)ui_text_modify_callback,
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
64 value);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 return text_area;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 char* ui_textarea_get(UiText *text) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 XtFree(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 char *str = XmTextGetString(text->obj);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 text->value = str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 return str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 void ui_textarea_set(UiText *text, char *str) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 XtFree(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 text->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 text->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 XmTextSetString(text->obj, str);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
87
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
88 UiUndoMgr* ui_create_undomgr() {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
89 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
90 mgr->begin = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
91 mgr->cur = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
92 mgr->length = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
93 mgr->event = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
94 return mgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
95 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
96
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
97 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
98 XmTextVerifyCallbackStruct *txv = (XmTextVerifyCallbackStruct*)data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
99 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
100 UiUndoMgr *mgr = value->undomgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
101 if(!mgr->event) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
102 return;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
103 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
104
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
105 char *text = txv->text->ptr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
106 int length = txv->text->length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
107
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
108 if(mgr->cur) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
109 UcxList *elm = mgr->cur->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
110 if(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
111 mgr->cur->next = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
112 while(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
113 elm->prev = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
114 UcxList *next = elm->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
115 ui_free_textbuf_op(elm->data);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
116 free(elm);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
117 elm = next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
118 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
119 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
120
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
121 if(type == UI_TEXTBUF_INSERT) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
122 UiTextBufOp *last_op = mgr->cur->data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
123 if(
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
124 last_op->type == UI_TEXTBUF_INSERT &&
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
125 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
126 {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
127 // append text to last op
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
128 int ln = last_op->len;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
129 char *newtext = malloc(ln + length + 1);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
130 memcpy(newtext, last_op->text, ln);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
131 memcpy(newtext+ln, text, length);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
132 newtext[ln+length] = '\0';
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
133
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
134 last_op->text = newtext;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
135 last_op->len = ln + length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
136 last_op->end += length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
137
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
138 return;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
139 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
140 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
141 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
142
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
143 char *str;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
144 if(type == UI_TEXTBUF_INSERT) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
145 str = malloc(length + 1);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
146 memcpy(str, text, length);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
147 str[length] = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
148 } else {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
149 length = txv->endPos - txv->startPos;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
150 str = malloc(length + 1);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
151 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
152 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
153
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
154 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
155 op->type = type;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
156 op->start = txv->startPos;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
157 op->end = txv->endPos + 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
158 op->len = length;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
159 op->text = str;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
160
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
161 UcxList *elm = ucx_list_append(NULL, op);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
162 mgr->cur = elm;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
163 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
164 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
165
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
166 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
167 // 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
168
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
169 int has_space = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
170 for(int i=0;i<oldlen;i++) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
171 if(oldstr[i] < 33) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
172 has_space = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
173 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
174 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
175 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
176
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
177 for(int i=0;i<newlen;i++) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
178 if(has_space && newstr[i] > 32) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
179 return 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
180 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
181 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
182
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
183 return 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
184 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
185
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
186 void ui_free_textbuf_op(UiTextBufOp *op) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
187 if(op->text) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
188 free(op->text);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
189 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
190 free(op);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
191 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
192
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
193
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
194 void ui_text_undo(UiText *value) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
195 UiUndoMgr *mgr = value->undomgr;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
196
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
197 if(mgr->cur) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
198 UiTextBufOp *op = mgr->cur->data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
199 mgr->event = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
200 switch(op->type) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
201 case UI_TEXTBUF_INSERT: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
202 XmTextReplace(value->obj, op->start, op->end, "");
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
203 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
204 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
205 case UI_TEXTBUF_DELETE: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
206 XmTextInsert(value->obj, op->start, op->text);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
207 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
208 }
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 mgr->event = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
211 mgr->cur = mgr->cur->prev;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
212 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
213 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
214
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
215 void ui_text_redo(UiText *value) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
216 UiUndoMgr *mgr = value->undomgr;
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 UcxList *elm = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
219 if(mgr->cur) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
220 if(mgr->cur->next) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
221 elm = mgr->cur->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
222 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
223 } else if(mgr->begin) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
224 elm = mgr->begin;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
225 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
226
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
227 if(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
228 UiTextBufOp *op = elm->data;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
229 mgr->event = 0;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
230 switch(op->type) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
231 case UI_TEXTBUF_INSERT: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
232 XmTextInsert(value->obj, op->start, op->text);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
233 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
234 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
235 case UI_TEXTBUF_DELETE: {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
236 XmTextReplace(value->obj, op->start, op->end, "");
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
237 break;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
238 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
239 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
240 mgr->event = 1;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
241 mgr->cur = elm;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
242 }
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
243 }

mercurial