ui/motif/text.c

Mon, 31 Mar 2014 20:22:16 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 31 Mar 2014 20:22:16 +0200
changeset 14
e2fd132ab781
parent 12
fe94e0fb9ef3
child 22
bcf880b29bc3
permissions
-rw-r--r--

added menu item lists (Cocoa implementation)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>

#include "text.h"
#include "container.h"

UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
    UiContainer *ct = uic_get_current_container(obj);
    int n = 0;
    Arg args[16];
    
    //XtSetArg(args[n], XmNeditable, TRUE);
    //n++;
    XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT);
    n++;
    
    Widget parent = ct->add(ct, args, &n);
    Widget text_area = XmCreateScrolledText(parent, "text_area", args, n);
    XtManageChild(text_area);
    
    // bind value
    if(value) {
        value->set = ui_textarea_set;
        value->get = ui_textarea_get;
        value->getsubstr = ui_textarea_getsubstr;
        value->insert = ui_textarea_insert;
        value->value = NULL;
        value->obj = text_area;
        
        if(!value->undomgr) {
            value->undomgr = ui_create_undomgr();
        }
        
        XtAddCallback(
                text_area,
                XmNmodifyVerifyCallback,
                (XtCallbackProc)ui_text_modify_callback,
                value);
    }
    
    return text_area;
}

char* ui_textarea_get(UiText *text) {
    if(text->value) {
        XtFree(text->value);
    }
    char *str = XmTextGetString(text->obj);
    text->value = str;
    return str;
}

void ui_textarea_set(UiText *text, char *str) {
    if(text->value) {
        XtFree(text->value);
    }
    text->value = NULL;
    XmTextSetString(text->obj, str);
}

char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
    if(text->value) {
        XtFree(text->value);
    }
    int length = end - begin;
    char *str = XtMalloc(length + 1);
    XmTextGetSubstring(text->obj, begin, length, length + 1, str);
    text->value = str;
    return str;
}

void ui_textarea_insert(UiText *text, int pos, char *str) {
    if(text->value) {
        XtFree(text->value);
    }
    text->value = NULL;
    XmTextInsert(text->obj, pos, str);
}

UiUndoMgr* ui_create_undomgr() {
    UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
    mgr->begin = NULL;
    mgr->cur = NULL;
    mgr->length = 0;
    mgr->event = 1;
    return mgr;
}

void ui_text_modify_callback(Widget widget, UiText *value, XtPointer data) {
    XmTextVerifyCallbackStruct *txv = (XmTextVerifyCallbackStruct*)data;
    int type = txv->text->length > 0 ? UI_TEXTBUF_INSERT : UI_TEXTBUF_DELETE;
    UiUndoMgr *mgr = value->undomgr;
    if(!mgr->event) {
        return;
    }
    
    char *text = txv->text->ptr;
    int length = txv->text->length;
    
    if(mgr->cur) {
        UcxList *elm = mgr->cur->next;
        if(elm) {
            mgr->cur->next = NULL;
            while(elm) {
                elm->prev = NULL;   
                UcxList *next = elm->next;
                ui_free_textbuf_op(elm->data);
                free(elm);
                elm = next;
            }
        }
        
        if(type == UI_TEXTBUF_INSERT) {
            UiTextBufOp *last_op = mgr->cur->data;
            if(
                last_op->type == UI_TEXTBUF_INSERT &&
                ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
            {
                // append text to last op
                int ln = last_op->len;
                char *newtext = malloc(ln + length + 1);
                memcpy(newtext, last_op->text, ln);
                memcpy(newtext+ln, text, length);
                newtext[ln+length] = '\0';
                
                last_op->text = newtext;
                last_op->len = ln + length;
                last_op->end += length;

                return;
            }
        }
    }
    
    char *str;
    if(type == UI_TEXTBUF_INSERT) {
        str = malloc(length + 1);
        memcpy(str, text, length);
        str[length] = 0;
    } else {
        length = txv->endPos - txv->startPos;
        str = malloc(length + 1);
        XmTextGetSubstring(value->obj, txv->startPos, length, length+1, str);
    }
    
    UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
    op->type = type;
    op->start = txv->startPos;
    op->end = txv->endPos + 1;
    op->len = length;
    op->text = str;
    
    UcxList *elm = ucx_list_append(NULL, op);
    mgr->cur = elm;
    mgr->begin = ucx_list_concat(mgr->begin, elm);
}

int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) {
    // return 1 if oldstr + newstr are one word
    
    int has_space = 0;
    for(int i=0;i<oldlen;i++) {
        if(oldstr[i] < 33) {
            has_space = 1;
            break;
        }
    }
    
    for(int i=0;i<newlen;i++) {
        if(has_space && newstr[i] > 32) {
            return 1;
        }
    }
    
    return 0;
}

void ui_free_textbuf_op(UiTextBufOp *op) {
    if(op->text) {
        free(op->text);
    }
    free(op);
}


void ui_text_undo(UiText *value) {
    UiUndoMgr *mgr = value->undomgr;
    
    if(mgr->cur) {
        UiTextBufOp *op = mgr->cur->data;
        mgr->event = 0;
        switch(op->type) {
            case UI_TEXTBUF_INSERT: {
                XmTextReplace(value->obj, op->start, op->end, "");
                break;
            }
            case UI_TEXTBUF_DELETE: {
                XmTextInsert(value->obj, op->start, op->text);
                break;
            }
        }
        mgr->event = 1;
        mgr->cur = mgr->cur->prev;
    }
}

void ui_text_redo(UiText *value) {
    UiUndoMgr *mgr = value->undomgr;
    
    UcxList *elm = NULL;
    if(mgr->cur) {
        if(mgr->cur->next) {
            elm = mgr->cur->next;
        }
    } else if(mgr->begin) {
        elm = mgr->begin;
    }
    
    if(elm) {
        UiTextBufOp *op = elm->data;
        mgr->event = 0;
        switch(op->type) {
            case UI_TEXTBUF_INSERT: {
                XmTextInsert(value->obj, op->start, op->text);
                break;
            }
            case UI_TEXTBUF_DELETE: {
                XmTextReplace(value->obj, op->start, op->end, "");
                break;
            }
        }
        mgr->event = 1;
        mgr->cur = elm;
    }
}

mercurial