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
|
/*
* $Id: ExtEdit.c,v 1.6 1994/05/13 14:11:44 mallet Exp $
*
* Copyright (c) 1991-1994 Lionel MALLET
*
* 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 Lionel MALLET 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 Lionel MALLET shall
* not be used in advertising or otherwise to promote the sale, use or
* other dealings in this Software without prior written authorization
* from Lionel MALLET.
*
* Author: Tim Wise - Scientific & Engineering Software (SES), Inc.
*/
/*--------------------------------------------------------------------------*/
/*
ExtEditor.c
This file contains routines used for the xpm extension editor
of pixmap.
*/
/*--------------------------------------------------------------------------*/
#define NEWLINE '\n'
#define min(x, y) (((x) < (y)) ? (x) : (y))
#define max(x, y) (((x) > (y)) ? (x) : (y))
extern void unsetKillfromWM();
/*--------------------------------------------------------------------------*/
/*
X p m E x t e n s i o n T o S t r i n g
Convert an xpm extension to a string. Lines of the extension
are separated by newlines in the string.
*/
/*--------------------------------------------------------------------------*/
String XpmExtensionToString( ext )
XpmExtension *ext;
{
String s = NULL;
int i, len;
if (!ext)
return NULL;
if (ext->nlines == 0)
return "";
for ( i=0, len = 0; i < ext->nlines; i++)
len += strlen( ext->lines[i] ) + 1; /* plus one for newline or NULL */
s = XtCalloc( len, sizeof(char) );
for ( i=0; i < ext->nlines - 1; i++ ) {
strcat( s, ext->lines[i] );
strcat( s, "\n" );
}
strcat( s, ext->lines[i] );
return s;
}
/*--------------------------------------------------------------------------*/
/*
S t r i n g T o X p m E x t e n s i o n
Convert to a string to an xpm extension. newlines in the
string cause a new line of the extension to be created.
*/
/*--------------------------------------------------------------------------*/
XpmExtension *StringToXpmExtension( s )
String s;
{
XpmExtension *ext;
String beg, end;
int i, len, n;
if (!s)
return NULL;
ext = (XpmExtension *) XtCalloc(1, sizeof(XpmExtension));
/* count nlines (number of newlines + 1) */
n = 0;
beg = s;
for( end=index(beg, NEWLINE); end; end=index(beg, NEWLINE) ) {
n++;
beg = end + 1;
}
n++;
/* assert: n > 0 */
ext->nlines = n;
ext->lines = (char **) XtCalloc(n, sizeof(char *));
/* do newline terminated lines */
beg = s;
for ( i=0; i < n - 1; i++ ) {
end = index(beg, NEWLINE);
len = end - beg;
ext->lines[i] = (char *) XtCalloc(len + 1, sizeof(char));
strncpy( ext->lines[i], beg, len );
beg = end + 1;
}
/* last line is null terminated */
ext->lines[i] = (char *) XtCalloc(strlen(beg) + 1, sizeof(char));
strcpy( ext->lines[i], beg );
return ext;
}
/*--------------------------------------------------------------------------*/
/*
P o s i t i o n P o p u p
Position a popup in the center of its parent. This is the same
code as is used in Dialog.c/PopupDialog().
*/
/*--------------------------------------------------------------------------*/
void PositionPopup( popup )
Widget popup; /* shell widget to postion */
{
Widget parent = XtParent( popup );
Position parentX, parentY;
Dimension parentWidth, parentHeight;
Position popupX, popupY;
Dimension popupWidth, popupHeight, popupBorder;
Dimension dpyWidth, dpyHeight;
Display *dpy;
int n;
Arg args[4];
n = 0;
XtSetArg( args[n], XtNx, &parentX ); n++;
XtSetArg( args[n], XtNy, &parentY ); n++;
XtSetArg( args[n], XtNwidth, &parentWidth ); n++;
XtSetArg( args[n], XtNheight, &parentHeight ); n++;
XtGetValues( parent , args, n );
n = 0;
XtSetArg( args[n], XtNwidth, &popupWidth ); n++;
XtSetArg( args[n], XtNheight, &popupHeight ); n++;
XtSetArg( args[n], XtNborderWidth, &popupBorder ); n++;
XtGetValues( popup, args, n );
dpy = XtDisplay(popup);
dpyWidth = (Position) DisplayWidth ( dpy, DefaultScreen(dpy) );
dpyHeight = (Position) DisplayHeight( dpy, DefaultScreen(dpy) );
popupX =
max( 0,
min( parentX + ((Position)parentWidth - (Position)popupWidth) / 2,
dpyWidth - (Position)popupWidth - 2 * (Position)popupBorder
));
popupY =
max( 0,
min( parentY + ((Position)parentHeight - (Position)popupHeight) / 2,
dpyHeight - (Position)popupHeight - 2 * (Position)popupBorder
));
n = 0;
XtSetArg( args[n], XtNx, popupX); n++;
XtSetArg( args[n], XtNy, popupY); n++;
XtSetValues( popup, args, n );
}
/*--------------------------------------------------------------------------*/
/*
P o p u p
Position a shell relative to parent then pop it up.
*/
/*--------------------------------------------------------------------------*/
static void Popup( popup, grab )
Widget popup;
XtGrabKind grab;
{
/* position shell relative to parent */
PositionPopup( popup );
#ifndef USE_ATHENA
XtManageChild(popup);
#else
XtPopup( popup, grab );
#endif
}
/*--------------------------------------------------------------------------*/
/*
P o p d o w n
A callback to popdown a given shell.
*/
/*--------------------------------------------------------------------------*/
/* ARGSUSED */
static void Popdown( w, client_data, call_data )
Widget w; /* unused */
XtPointer client_data; /* child of shell to popdown */
XtPointer call_data; /* unused */
{
#ifndef USE_ATHENA
XtUnmanageChild((Widget) client_data);
#else
XtPopdown( XtParent((Widget) client_data) );
#endif
}
/*--------------------------------------------------------------------------*/
/*
P o p u p E x t e n s i o n E d i t o r
Popup the xpm extension editor form using the given name
and text.
*/
/*--------------------------------------------------------------------------*/
static void PopupExtensionEditor( formW, name, text )
Widget formW;
String name;
String text;
{
Widget textW = XtNameToWidget( formW, "*text" );
Widget nameW = XtNameToWidget( formW, "name" );
Arg args[1];
#ifndef USE_ATHENA
XmString xmstr;
XmTextSetString(textW, text);
xmstr = XmStringCreateLtoR(name, XmSTRING_DEFAULT_CHARSET);
XtSetArg(args[0], XmNlabelString, xmstr);
XtSetValues( nameW, args, 1 );
XmStringFree(xmstr);
Popup(formW, XtGrabExclusive);
#else
XtSetArg( args[0], XtNstring, text );
XtSetValues( textW, args, 1 );
XtSetArg( args[0], XtNlabel, name );
XtSetValues( nameW, args, 1 );
Popup( XtParent(formW), XtGrabExclusive );
#endif
}
/*--------------------------------------------------------------------------*/
/*
D o E d i t E x t e n s i o n
Given an xpm extension name, get the extension text from the
pixmap widget and popup it up in a form for the user to change.
*/
/*--------------------------------------------------------------------------*/
void DoEditExtension( name )
String name;
{
if ( name != NULL && strcmp(name, "") != 0 ) {
XpmExtension *ext = PWFindExtension( pixmap_widget, name );
String text;
if ( ext == NULL || ext->nlines == 0 )
text = "";
else
text = XpmExtensionToString( ext );
PopupExtensionEditor( extensionEditor, name, text );
}
}
/*--------------------------------------------------------------------------*/
/*
A d d T o E x t e n s i o n M e n u
Add given name to extension menu.
*/
void AddToExtensionMenu (name)
char *name;
{
void ExtensionMenuCallback();
#ifndef USE_ATHENA
Widget item = XmCreatePushButtonGadget(extensionMenu_widget,
name, NULL, 0);
XtManageChild(item);
XtAddCallback(item, XmNactivateCallback,
ExtensionMenuCallback, (XtPointer) NULL );
#else
Widget item = XtCreateManagedWidget(name,
smeBSBObjectClass,
extensionMenu_widget,
NULL, 0 );
XtAddCallback(item, XtNcallback, ExtensionMenuCallback,
(XtPointer) NULL );
#endif
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*
D o A d d E x t e n s i o n
Prompt user for name of new xpm extension then popup the editor
for the user to supply the text for the extension.
*/
/*--------------------------------------------------------------------------*/
void DoAddExtension()
{
char *name = NULL;
if ( PopupDialog( input_dialog,
"Add extension name:",
"", &name,
XtGrabExclusive ) == Okay ) {
if ( name != NULL && strcmp( name, "" ) != 0 ) {
/* add to menu, if necessary */
if ( ! XtNameToWidget(extensionMenu_widget, name) )
AddToExtensionMenu (name);
PWAddExtension( pixmap_widget, name ); /* add, if necessary */
DoEditExtension( name );
}
}
}
/*--------------------------------------------------------------------------*/
/*
F i x E x t e n s i o n M e n u
The list of extensions in a pixmap widget has been changed.
Update the extension menu by destroying the old menu and
recreating it from scratch.
*/
/*--------------------------------------------------------------------------*/
void FixExtensionMenu (w)
Widget w;
{
void ExtensionMenuCallback();
char **PWGetExtensionNames();
char **names;
char **name;
Widget *children;
Cardinal num_children, i;
Arg arglist[2];
#ifndef USE_ATHENA
XtSetArg(arglist[0], XmNchildren, &children);
XtSetArg(arglist[1], XmNnumChildren, &num_children);
#else
XtSetArg(arglist[0], XtNchildren, &children);
XtSetArg(arglist[1], XtNnumChildren, &num_children);
#endif
XtGetValues(extensionMenu_widget, arglist, 2);
/* destroy all children */
for (i = 0; i < num_children; i++)
XtDestroyWidget(*(children + i));
/* build again base extension menu */
MakeMenuPanel(extensionMenu_widget, extension_menu,
XtNumber(extension_menu), ExtensionMenuCallback);
names = PWGetExtensionNames (w);
if (names) {
for (name = names; name && *name; name++) {
AddToExtensionMenu (*name);
XtFree (*name);
}
if (names)
XtFree ((char *)names);
}
}
/*--------------------------------------------------------------------------*/
/*
D o R e m o v e E x t e n s i o n
Callback for extension editor form Remove button. Get the name
of the extension from the form then remove the extension from
the pixmap widget and from the menu of extension names.
*/
/*--------------------------------------------------------------------------*/
void DoRemoveExtension( w, client_data, call_data )
Widget w; /* unused */
XtPointer client_data; /* extension editor form */
XtPointer call_data; /* unused */
{
Widget formW = (Widget) client_data;
Widget nameW = XtNameToWidget( formW, "name" );
String name = NULL;
Popdown( NULL, (XtPointer) formW, NULL );
/* get name of extension */
#ifndef USE_ATHENA
{
XmString name_xms;
XtVaGetValues(nameW, XmNlabelString, &name_xms, NULL);
XmStringGetLtoR(name_xms, XmSTRING_DEFAULT_CHARSET, &name);
}
#else
XtVaGetValues( nameW,
XtNlabel, &name,
NULL );
#endif
PWRemoveExtension( pixmap_widget, name );
/* remove name from menu */
{
Widget item = XtNameToWidget(extensionMenu_widget, name);
XtDestroyWidget( item );
}
}
/*--------------------------------------------------------------------------*/
/*
D o S a v e E x t e n s i o n
Callback for the Okay button of the extension editor form.
Save the text of the extension with the pixmap widget.
*/
/*--------------------------------------------------------------------------*/
void DoSaveExtension( w, client_data, call_data )
Widget w; /* unused */
XtPointer client_data; /* extension editor form */
XtPointer call_data; /* unused */
{
Widget formW = (Widget) client_data;
Widget nameW = XtNameToWidget( formW, "name" );
Widget textW = XtNameToWidget( formW, "*text" );
String name = NULL;
String text = NULL;
Popdown( NULL, (XtPointer) formW, NULL );
#ifndef USE_ATHENA
{
XmString name_xms;
XtVaGetValues(nameW, XmNlabelString, &name_xms, NULL);
XmStringGetLtoR(name_xms, XmSTRING_DEFAULT_CHARSET, &name);
text = (String) XmTextGetString(textW);
}
#else
XtVaGetValues( textW,
XtNstring, &text,
NULL );
XtVaGetValues( nameW,
XtNlabel, &name,
NULL );
#endif
/*
if ( strcmp(text, "") == 0 ) {
PWRemoveExtension( pixmap_widget, name );
}
else
*/
{
XpmExtension *ext = StringToXpmExtension( text );
ext->name = XtNewString( name );
PWAddExtension( pixmap_widget, name ); /* add if necessary */
PWUpdateExtension( pixmap_widget, ext );
}
}
/*--------------------------------------------------------------------------*/
/*
D o S h o w C u r r e n t E x t e n s i o n s
Print names of xpm extensions in pixmap widget.
*/
/*--------------------------------------------------------------------------*/
void DoShowCurrentExtensions()
{
char **extension_names;
char **name;
extension_names = PWGetExtensionNames( pixmap_widget );
if ( extension_names != NULL ) {
fprintf( stderr, "Extension names:\n" );
for ( name = extension_names; *name != NULL; name++ )
fprintf( stderr, "\t%s\n", *name );
}
}
/*--------------------------------------------------------------------------*/
/*
C r e a t e E x t e n s i o n E d i t o r
The xpm extension editor is a form with a label for the extension
name, and a text box for the extension text.
Buttons:
- ok : save text.
- cancel : ignore any changes made to text.
- remove : delete extension from pixmap widget and menu.
*/
/*--------------------------------------------------------------------------*/
static Widget CreateExtensionEditor( topWidget, editorName )
Widget topWidget;
String editorName;
{
Widget popup, form, name, text, ok, cancel, remove;
#ifndef USE_ATHENA
popup = XmCreateFormDialog(top_widget, editorName, NULL, 0);
form = popup;
name = XmCreateLabelGadget(form, "name", NULL, 0);
XtManageChild(name);
text = (Widget) XmCreateScrolledText(form, "text", NULL, 0);
XtManageChild(text);
ok = XmCreatePushButtonGadget(form, "ok", NULL, 0);
XtManageChild(ok);
XtAddCallback(ok, XmNactivateCallback, DoSaveExtension, (XtPointer)form);
cancel = XmCreatePushButtonGadget(form, "cancel", NULL, 0);
XtManageChild(cancel);
XtAddCallback(cancel, XmNactivateCallback, Popdown, (XtPointer)form);
remove = XmCreatePushButtonGadget(form, "remove", NULL, 0);
XtManageChild(remove);
XtAddCallback(remove, XmNactivateCallback, DoRemoveExtension,
(XtPointer)form);
#else
popup = XtCreatePopupShell(
"extEditorShell", transientShellWidgetClass,
top_widget,
NULL, 0 );
form = XtCreateManagedWidget(
editorName, formWidgetClass,
popup,
NULL, 0 );
name = XtCreateManagedWidget(
"name", labelWidgetClass,
form,
NULL, 0 );
text = XtCreateManagedWidget(
"text", asciiTextWidgetClass,
form,
NULL, 0 );
ok = XtCreateManagedWidget(
"ok", commandWidgetClass,
form,
NULL, 0);
XtAddCallback( ok, XtNcallback,
DoSaveExtension, (XtPointer) form );
cancel = XtCreateManagedWidget(
"cancel", commandWidgetClass,
form,
NULL, 0);
XtAddCallback( cancel, XtNcallback, Popdown, (XtPointer) form );
remove = XtCreateManagedWidget(
"remove", commandWidgetClass,
form,
NULL, 0);
XtAddCallback( remove, XtNcallback, DoRemoveExtension, (XtPointer) form );
#endif
return form;
}
/*--------------------------------------------------------------------------*/
/*
E x t e n s i o n M e n u C a l l b a c k
Callback for items on the xpm extension menu. Check for adding
new item to menu, otherwise invoke the extension editor for the
chosen extension name.
*/
/*--------------------------------------------------------------------------*/
void ExtensionMenuCallback(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{
int *id = (int *)client_data;
if (id && *id == AddExtension)
DoAddExtension(); /* add then edit */
else
DoEditExtension( XtName(w) );
}
|