1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
|
/* $XConsortium: TextSrc.c,v 1.15 94/04/17 20:13:14 kaleb Exp $ */
/*
Copyright (c) 1989, 1994 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
*/
/* $XFree86: xc/lib/Xaw/TextSrc.c,v 1.1.1.1.12.2 1998/05/16 09:05:22 dawes Exp $ */
/*
* Author: Chris Peterson, MIT X Consortium.
* Much code taken from X11R3 String and Disk Sources.
*/
/*
* TextSrc.c - TextSrc object. (For use with the text widget).
*
*/
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xutil.h>
#include <X11/neXtaw/XawInit.h>
#include <X11/neXtaw/TextSrcP.h>
#include <X11/Xmu/Atoms.h>
#include <X11/Xmu/CharSet.h>
#include "XawI18n.h"
#include <stdio.h>
#include <ctype.h>
/****************************************************************
*
* Full class record constant
*
****************************************************************/
/* Private Data */
#define offset(field) XtOffsetOf(TextSrcRec, textSrc.field)
static XtResource resources[] = {
{XtNeditType, XtCEditType, XtREditMode, sizeof(XawTextEditType),
offset(edit_mode), XtRString, "read"},
};
static void ClassInitialize(), ClassPartInitialize(), SetSelection();
static void CvtStringToEditMode();
static Boolean ConvertSelection();
static XawTextPosition Search(), Scan(), Read();
static int Replace();
#define SuperClass (&objectClassRec)
TextSrcClassRec textSrcClassRec = {
{
/* core_class fields */
/* superclass */ (WidgetClass) SuperClass,
/* class_name */ "TextSrc",
/* widget_size */ sizeof(TextSrcRec),
/* class_initialize */ ClassInitialize,
/* class_part_initialize */ ClassPartInitialize,
/* class_inited */ FALSE,
/* initialize */ NULL,
/* initialize_hook */ NULL,
/* realize */ NULL,
/* actions */ NULL,
/* num_actions */ 0,
/* resources */ resources,
/* num_resources */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ FALSE,
/* compress_exposure */ FALSE,
/* compress_enterleave */ FALSE,
/* visible_interest */ FALSE,
/* destroy */ NULL,
/* resize */ NULL,
/* expose */ NULL,
/* set_values */ NULL,
/* set_values_hook */ NULL,
/* set_values_almost */ NULL,
/* get_values_hook */ NULL,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ NULL,
/* query_geometry */ NULL,
/* display_accelerator */ NULL,
/* extension */ NULL,
},
/* textSrc_class fields */
{
/* Read */ Read,
/* Replace */ Replace,
/* Scan */ Scan,
/* Search */ Search,
/* SetSelection */ SetSelection,
/* ConvertSelection */ ConvertSelection
}
};
WidgetClass textSrcObjectClass = (WidgetClass)&textSrcClassRec;
static void
ClassInitialize ()
{
XawInitializeWidgetSet ();
XtAddConverter(XtRString, XtREditMode, CvtStringToEditMode, NULL, 0);
}
static void
ClassPartInitialize(wc)
WidgetClass wc;
{
TextSrcObjectClass t_src, superC;
t_src = (TextSrcObjectClass) wc;
superC = (TextSrcObjectClass) t_src->object_class.superclass;
/*
* We don't need to check for null super since we'll get to TextSrc
* eventually.
*/
if (t_src->textSrc_class.Read == XtInheritRead)
t_src->textSrc_class.Read = superC->textSrc_class.Read;
if (t_src->textSrc_class.Replace == XtInheritReplace)
t_src->textSrc_class.Replace = superC->textSrc_class.Replace;
if (t_src->textSrc_class.Scan == XtInheritScan)
t_src->textSrc_class.Scan = superC->textSrc_class.Scan;
if (t_src->textSrc_class.Search == XtInheritSearch)
t_src->textSrc_class.Search = superC->textSrc_class.Search;
if (t_src->textSrc_class.SetSelection == XtInheritSetSelection)
t_src->textSrc_class.SetSelection = superC->textSrc_class.SetSelection;
if (t_src->textSrc_class.ConvertSelection == XtInheritConvertSelection)
t_src->textSrc_class.ConvertSelection =
superC->textSrc_class.ConvertSelection;
}
/************************************************************
*
* Class specific methods.
*
************************************************************/
/* Function Name: Read
* Description: This function reads the source.
* Arguments: w - the TextSrc Object.
* pos - position of the text to retreive.
* RETURNED text - text block that will contain returned text.
* length - maximum number of characters to read.
* Returns: The number of characters read into the buffer.
*/
/* ARGSUSED */
static XawTextPosition
Read(w, pos, text, length)
Widget w;
XawTextPosition pos;
XawTextBlock *text;
int length;
{
XtAppError(XtWidgetToApplicationContext(w),
"TextSrc Object: No read function is defined.");
return( (XawTextPosition) 0 ); /* for gcc -Wall and lint */
}
/* Function Name: Replace.
* Description: Replaces a block of text with new text.
* Arguments: src - the Text Source Object.
* startPos, endPos - ends of text that will be removed.
* text - new text to be inserted into buffer at startPos.
* Returns: XawEditError.
*/
/*ARGSUSED*/
static int
Replace (w, startPos, endPos, text)
Widget w;
XawTextPosition startPos, endPos;
XawTextBlock *text;
{
return(XawEditError);
}
/* Function Name: Scan
* Description: Scans the text source for the number and type
* of item specified.
* Arguments: w - the TextSrc Object.
* position - the position to start scanning.
* type - type of thing to scan for.
* dir - direction to scan.
* count - which occurance if this thing to search for.
* include - whether or not to include the character found in
* the position that is returned.
* Returns: EXITS WITH AN ERROR MESSAGE.
*
*/
/* ARGSUSED */
static
XawTextPosition
Scan (w, position, type, dir, count, include)
Widget w;
XawTextPosition position;
XawTextScanType type;
XawTextScanDirection dir;
int count;
Boolean include;
{
XtAppError(XtWidgetToApplicationContext(w),
"TextSrc Object: No SCAN function is defined.");
return( (XawTextPosition) 0 ); /* for gcc -Wall and lint */
}
/* Function Name: Search
* Description: Searchs the text source for the text block passed
* Arguments: w - the TextSource Object.
* position - the position to start scanning.
* dir - direction to scan.
* text - the text block to search for.
* Returns: XawTextSearchError.
*/
/* ARGSUSED */
static XawTextPosition
Search(w, position, dir, text)
Widget w;
XawTextPosition position;
XawTextScanDirection dir;
XawTextBlock * text;
{
return(XawTextSearchError);
}
/* Function Name: ConvertSelection
* Description: Dummy selection converter.
* Arguments: w - the TextSrc object.
* selection - the current selection atom.
* target - the current target atom.
* type - the type to conver the selection to.
* RETURNED value, length - the return value that has been converted.
* RETURNED format - the format of the returned value.
* Returns: TRUE if the selection has been converted.
*
*/
/* ARGSUSED */
static Boolean
ConvertSelection(w, selection, target, type, value, length, format)
Widget w;
Atom * selection, * target, * type;
XtPointer * value;
unsigned long * length;
int * format;
{
return(FALSE);
}
/* Function Name: SetSelection
* Description: allows special setting of the selection.
* Arguments: w - the TextSrc object.
* left, right - bounds of the selection.
* selection - the selection atom.
* Returns: none
*/
/* ARGSUSED */
static void
SetSelection(w, left, right, selection)
Widget w;
XawTextPosition left, right;
Atom selection;
{
/* This space intentionally left blank. */
}
#define done(address, type) \
{ toVal->size = sizeof(type); toVal->addr = (XPointer) address; }
/* ARGSUSED */
static void
CvtStringToEditMode(args, num_args, fromVal, toVal)
XrmValuePtr args; /* unused */
Cardinal *num_args; /* unused */
XrmValuePtr fromVal;
XrmValuePtr toVal;
{
static XawTextEditType editType;
static XrmQuark QRead, QAppend, QEdit;
XrmQuark q;
char lowerName[BUFSIZ];
static Boolean inited = FALSE;
if ( !inited ) {
QRead = XrmPermStringToQuark(XtEtextRead);
QAppend = XrmPermStringToQuark(XtEtextAppend);
QEdit = XrmPermStringToQuark(XtEtextEdit);
inited = TRUE;
}
if (strlen((char *)fromVal->addr) >= sizeof(lowerName)) {
XtStringConversionWarning((char *) fromVal->addr, XtREditMode);
return;
}
XmuCopyISOLatin1Lowered (lowerName, (char *)fromVal->addr);
q = XrmStringToQuark(lowerName);
if (q == QRead) editType = XawtextRead;
else if (q == QAppend) editType = XawtextAppend;
else if (q == QEdit) editType = XawtextEdit;
else {
XtStringConversionWarning((char *) fromVal->addr, XtREditMode);
return;
}
done(&editType, XawTextEditType);
return;
}
/************************************************************
*
* Public Functions.
*
************************************************************/
/* Function Name: XawTextSourceRead
* Description: This function reads the source.
* Arguments: w - the TextSrc Object.
* pos - position of the text to retreive.
* RETURNED text - text block that will contain returned text.
* length - maximum number of characters to read.
* Returns: The number of characters read into the buffer.
*/
XawTextPosition
#if NeedFunctionPrototypes
XawTextSourceRead(Widget w, XawTextPosition pos, XawTextBlock *text,
int length)
#else
XawTextSourceRead(w, pos, text, length)
Widget w;
XawTextPosition pos;
XawTextBlock *text;
int length;
#endif
{
TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
if ( !XtIsSubclass( w, textSrcObjectClass ) )
XtErrorMsg("bad argument", "textSource", "XawError",
"XawTextSourceRead's 1st parameter must be subclass of asciiSrc.",
NULL, NULL);
return((*class->textSrc_class.Read)(w, pos, text, length));
}
/* Function Name: XawTextSourceReplace.
* Description: Replaces a block of text with new text.
* Arguments: src - the Text Source Object.
* startPos, endPos - ends of text that will be removed.
* text - new text to be inserted into buffer at startPos.
* Returns: XawEditError or XawEditDone.
*/
/*ARGSUSED*/
int
#if NeedFunctionPrototypes
XawTextSourceReplace (Widget w, XawTextPosition startPos,
XawTextPosition endPos, XawTextBlock *text)
#else
XawTextSourceReplace (w, startPos, endPos, text)
Widget w;
XawTextPosition startPos, endPos;
XawTextBlock *text;
#endif
{
TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
if ( !XtIsSubclass( w, textSrcObjectClass ) )
XtErrorMsg("bad argument", "textSource", "XawError",
"XawTextSourceReplace's 1st parameter must be subclass of asciiSrc.",
NULL, NULL);
return((*class->textSrc_class.Replace)(w, startPos, endPos, text));
}
/* Function Name: XawTextSourceScan
* Description: Scans the text source for the number and type
* of item specified.
* Arguments: w - the TextSrc Object.
* position - the position to start scanning.
* type - type of thing to scan for.
* dir - direction to scan.
* count - which occurance if this thing to search for.
* include - whether or not to include the character found in
* the position that is returned.
* Returns: The position of the text.
*
*/
XawTextPosition
#if NeedFunctionPrototypes
XawTextSourceScan(Widget w, XawTextPosition position,
#if NeedWidePrototypes
int type, int dir,
#else
XawTextScanType type, XawTextScanDirection dir,
#endif
int count,
#if NeedWidePrototypes
int include)
#else
Boolean include)
#endif
#else
XawTextSourceScan(w, position, type, dir, count, include)
Widget w;
XawTextPosition position;
XawTextScanType type;
XawTextScanDirection dir;
int count;
Boolean include;
#endif
{
TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
if ( !XtIsSubclass( w, textSrcObjectClass ) )
XtErrorMsg("bad argument", "textSource", "XawError",
"XawTextSourceScan's 1st parameter must be subclass of asciiSrc.",
NULL, NULL);
return((*class->textSrc_class.Scan)(w, position, type, dir, count, include));
}
/* Function Name: XawTextSourceSearch
* Description: Searchs the text source for the text block passed
* Arguments: w - the TextSource Object.
* position - the position to start scanning.
* dir - direction to scan.
* text - the text block to search for.
* Returns: The position of the text we are searching for or
* XawTextSearchError.
*/
XawTextPosition
#if NeedFunctionPrototypes
XawTextSourceSearch(Widget w, XawTextPosition position,
#if NeedWidePrototypes
int dir,
#else
XawTextScanDirection dir,
#endif
XawTextBlock *text)
#else
XawTextSourceSearch(w, position, dir, text)
Widget w;
XawTextPosition position;
XawTextScanDirection dir;
XawTextBlock * text;
#endif
{
TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
if ( !XtIsSubclass( w, textSrcObjectClass ) )
XtErrorMsg("bad argument", "textSource", "XawError",
"XawTextSourceSearch's 1st parameter must be subclass of asciiSrc.",
NULL, NULL);
return((*class->textSrc_class.Search)(w, position, dir, text));
}
/* Function Name: XawTextSourceConvertSelection
* Description: Dummy selection converter.
* Arguments: w - the TextSrc object.
* selection - the current selection atom.
* target - the current target atom.
* type - the type to conver the selection to.
* RETURNED value, length - the return value that has been converted.
* RETURNED format - the format of the returned value.
* Returns: TRUE if the selection has been converted.
*
*/
Boolean
#if NeedFunctionPrototypes
XawTextSourceConvertSelection(Widget w, Atom *selection, Atom *target,
Atom *type, XtPointer *value,
unsigned long *length, int *format)
#else
XawTextSourceConvertSelection(w, selection,
target, type, value, length, format)
Widget w;
Atom * selection, * target, * type;
XtPointer * value;
unsigned long * length;
int * format;
#endif
{
TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
if ( !XtIsSubclass( w, textSrcObjectClass ) )
XtErrorMsg("bad argument", "textSource", "XawError",
"XawTextSourceConvertSelectionXawTextSourceConvertSelection's 1st parameter must be subclass of asciiSrc.",
NULL, NULL);
return((*class->textSrc_class.ConvertSelection)(w, selection, target, type,
value, length, format));
}
/* Function Name: XawTextSourceSetSelection
* Description: allows special setting of the selection.
* Arguments: w - the TextSrc object.
* left, right - bounds of the selection.
* selection - the selection atom.
* Returns: none
*/
void
#if NeedFunctionPrototypes
XawTextSourceSetSelection(Widget w, XawTextPosition left,
XawTextPosition right, Atom selection)
#else
XawTextSourceSetSelection(w, left, right, selection)
Widget w;
XawTextPosition left, right;
Atom selection;
#endif
{
TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
if ( !XtIsSubclass( w, textSrcObjectClass ) )
XtErrorMsg("bad argument", "textSource", "XawError",
"'s 1st parameter must be subclass of asciiSrc.",
NULL, NULL);
(*class->textSrc_class.SetSelection)(w, left, right, selection);
}
/********************************************************************
*
* External Functions for Multi Text.
*
********************************************************************/
/*
* TextFormat():
* returns the format of text: FMT8BIT or FMTWIDE.
*
*/
XrmQuark
#if NeedFunctionPrototypes
_XawTextFormat(TextWidget tw)
#else
_XawTextFormat(tw)
TextWidget tw;
#endif
{
return (((TextSrcObject)(tw->text.source))->textSrc.text_format);
}
/* _XawTextWCToMB():
* convert the wchar string to external encoding.
* The caller is responsible for freeing both the source and ret string.
*
* wstr - source wchar string.
* len_in_out - lengh of string.
* As In, length of source wchar string, measured in wchar.
* As Out, length of returned string.
*/
char *
_XawTextWCToMB( d, wstr, len_in_out )
Display* d;
wchar_t* wstr;
int* len_in_out;
{
XTextProperty textprop;
if (XwcTextListToTextProperty(d, (wchar_t**)&wstr, 1,
XTextStyle, &textprop) < Success) {
XtWarningMsg("convertError", "textSource", "XawError",
"Non-character code(s) in buffer.", NULL, NULL);
*len_in_out = 0;
return( NULL );
}
*len_in_out = textprop.nitems;
return((char *)textprop.value);
}
/* _XawTextMBToWC():
* convert the string to internal processing codeset WC.
* The caller is responsible for freeing both the source and ret string.
*
* str - source string.
* len_in_out - lengh of string.
* As In, it is length of source string.
* As Out, it is length of returned string, measured in wchar.
*/
wchar_t* _XawTextMBToWC( d, str, len_in_out )
Display *d;
char *str;
int *len_in_out;
{
if (*len_in_out == 0) {
return(NULL);
} else {
XTextProperty textprop;
char *buf;
wchar_t **wlist, *wstr;
int count;
buf = XtMalloc(*len_in_out + 1);
if (!buf) {
XtErrorMsg("convertError", "multiSourceCreate", "XawError",
"No Memory", NULL, NULL);
*len_in_out = 0;
return (NULL); /* The above function doesn't really return. */
}
strncpy(buf, str, *len_in_out);
*(buf + *len_in_out) = '\0';
if (XmbTextListToTextProperty(d, &buf, 1, XTextStyle, &textprop)
!= Success) {
XtWarningMsg("convertError", "textSource", "XawError",
"No Memory, or Locale not supported.", NULL, NULL);
XtFree(buf);
*len_in_out = 0;
return (NULL);
}
XtFree(buf);
if (XwcTextPropertyToTextList(d, &textprop,
(wchar_t***)&wlist, &count) != Success) {
XtWarningMsg("convertError", "multiSourceCreate", "XawError",
"Non-character code(s) in source.", NULL, NULL);
*len_in_out = 0;
return (NULL);
}
wstr = wlist[0];
*len_in_out = wcslen(wstr);
XtFree((XtPointer)wlist);
return(wstr);
}
}
|