GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/****************************************************************************1**2*W popdial.c XGAP source Frank Celler3**4**5*Y Copyright 1995-1997, Lehrstuhl D fuer Mathematik, RWTH Aachen, Germany6*Y Copyright 1997, Frank Celler, Huerth, Germany7**8** This file contains functions for popping up dialogs. Two functions are9** exported:10**11** CreatePopupDialog( <app>, <top>, <name>, <bt>, <def>, <pix> )12** -------------------------------------------------------------13**14** 'CreatePopupDialog' creates a popup dialog structure. A popup shell of15** type "transientShellWidgetClass" is created together with a dialog widget16** and buttons according to <bt>. None of these widgets are realised, this17** is done by calling 'PopupDialog'. The default button activated by18** pressing "return" is <def>.19**20** Possible buttons are: PD_YES, PD_NO, PD_OK, PD_CANCEL, PD_ABORT, PD_RETRY21** PD_APPEND, PD_OVERWRITE22**23** PopupDialog( <dialog>, <message>, <deflt>, <result> )24** -----------------------------------------------------25**26** 'PopupDialog' will pop up a dialog created with 'CreatePopupDialog'. It27** then grabs the keyboard and returns only if a button or "return" is28** pressed. A pointer to the result is stored in <result> and the number of29** the button pressed is returned. If "return" was hit by the user the30** button number is the number given as <def> in 'CreatePopupDialog'.31**32** The layout of the popup dialog is basically:33**34** +-----------------------------------------+35** | |36** | pixmap some text <message> |37** | <pix> |38** | |39** | [[[ INPUT FIELD (default: <deflt>) ]]] |40** | |41** | Btn1 Btn2 ... BtnN |42** | |43** +-----------------------------------------+44**45** PopupDialogBrokenWM()46** ---------------------47**48** Some window manager don't handle transient shell widgets correctly. In49** this case calling 'PopupDialogBrokenWM' will toggle a flag such that50** a override shell widet is used instead.51*/52#include "utils.h"53#include "popdial.h" /* this package */545556/****************************************************************************57**5859*F ButtonSelected( <w>, <cld>, <cd> ) . . . . . . callback for button click60*/61static struct { String name; Int flag; } buttons[] =62{63{ "yes", PD_YES },64{ "no", PD_NO },65{ "OK", PD_OK },66{ "cancel", PD_CANCEL },67{ "abort", PD_ABORT },68{ "retry", PD_RETRY },69{ "append", PD_APPEND },70{ "overwrite", PD_OVERWRITE }71};7273static void ButtonSelected ( w, cld, cd )74Widget w;75XtPointer cld;76XtPointer cd;77{78Int i;79Int * res = (int*) cld;80String name;8182/* find name in <buttons> and set result */83XtVaGetValues( w, XtNlabel, (XtArgVal)&name, (String)NULL );84for ( i = 0; i < XtNumber(buttons); i++ )85if ( ! strcmp( buttons[i].name, name ) )86(*res) |= buttons[i].flag;87}888990/****************************************************************************91**92*F CreatePopupDialog( <app>, <top>, <name>, <button>, <def>, <pix> ) create93*/94#include "bitmaps/return.bm"9596static Boolean BrokenWM = False;9798TypePopupDialog CreatePopupDialog ( app, top, name, bt, def, pix )99XtAppContext app;100Widget top;101String name;102Int bt;103Int def;104Pixmap pix;105{106Display * dp;107TypePopupDialog dialog;108Int i;109static Pixmap symbol = 0;110111112/* create pixmap for default button */113if ( symbol == 0 )114{115dp = XtDisplay(top);116symbol = XCreateBitmapFromData( dp, DefaultRootWindow(dp),117return_bits, return_width, return_height );118}119120/* create a new popup variable */121dialog = (TypePopupDialog) XtMalloc( sizeof( struct _popup_dialog ) );122dialog->topLevel = top;123124if ( BrokenWM )125dialog->popupShell = XtVaCreatePopupShell(126name, overrideShellWidgetClass, top,127XtNallowShellResize, (XtArgVal)True,128(String)NULL );129else130dialog->popupShell = XtVaCreatePopupShell(131name, transientShellWidgetClass, top,132XtNallowShellResize, (XtArgVal)True,133XtNtransientFor, (XtArgVal)top,134(String)NULL );135if ( pix == 0 )136dialog->dialog = XtVaCreateManagedWidget(137"xgapDialog", dialogWidgetClass,138dialog->popupShell,139(String)NULL );140else141dialog->dialog = XtVaCreateManagedWidget(142"xgapDialog", dialogWidgetClass,143dialog->popupShell,144XtNicon, (XtArgVal)pix,145(String)NULL );146dialog->button = bt;147dialog->context = app;148dialog->defaultButton = def;149150/* add buttons to the dialog */151for ( i = 0; i < XtNumber(buttons); i++ )152if ( bt & buttons[i].flag )153{154if ( buttons[i].flag == def )155dialog->buttons[i] = XtVaCreateManagedWidget(156buttons[i].name, commandWidgetClass,157dialog->dialog,158XtNleftBitmap, (XtArgVal)symbol,159(String)NULL );160else161dialog->buttons[i] = XtVaCreateManagedWidget(162buttons[i].name, commandWidgetClass,163dialog->dialog,164(String)NULL );165XtAddCallback( dialog->buttons[i],166XtNcallback,167ButtonSelected,168&(dialog->result) );169}170else171dialog->buttons[i] = 0;172return dialog;173}174175176/****************************************************************************177**178*V NormalFont . . . . . . . . . . . . . . . . . normal font for text output179**180** see xcmds.c, imported here by Max 11.3.1999, see below181*/182extern XFontStruct * NormalFont;183184/****************************************************************************185**186*F PopupDialogBrokenWM() . . . . . . . . . . . . . . toggle <BrokenWM> flag187*/188void PopupDialogBrokenWM ( )189{190BrokenWM = ! BrokenWM;191}192193194/****************************************************************************195**196*F PopupDialog( <dialog>, <message>, <deflt>, <result> ) . . . . . . . do it197*/198static Cursor BlobCursor = 0;199200Int PopupDialog ( dialog, message, deflt, result )201TypePopupDialog dialog;202String message;203String deflt;204String * result;205{206Display * display;207Position x1, y1, tmp;208Dimension w1, h1, bw;209Window root;210Window child;211Int x2, y2, x3, y3;212UInt bt;213Int i;214Int textlen, deflen;215216/* create font cursor */217display = XtDisplay(dialog->popupShell);218if ( BlobCursor == 0 )219BlobCursor = XCreateFontCursor( display, XC_dot );220221/* set message and default answer in dialog */222/* Changes by Max: 11.3.1999:223XtVaSetValues( dialog->dialog,224XtNlabel, " ",225XtNvalue, " ",2260, 0 );*/227XtVaSetValues( dialog->dialog,228XtNlabel, (XtArgVal)message,229XtNvalue, (XtArgVal)deflt,230(String)NULL );231/* get size of popup dialog */232XtVaGetValues( dialog->popupShell,233XtNwidth, (XtArgVal)&w1,234(String)NULL );235textlen = strlen(message)*NormalFont->max_bounds.width + 80;236deflen = strlen(deflt)*NormalFont->max_bounds.width + 60;237if (textlen > deflen) {238if (textlen < w1)239textlen = w1;240} else {241if (deflen < w1)242textlen = w1;243else244textlen = deflen;245}246XtVaSetValues( dialog->dialog,247XtNwidth,textlen,2480,NULL );249/* End of changes by Max. */250251XtRealizeWidget( dialog->popupShell );252253/* get size of popup dialog */254XtVaGetValues( dialog->popupShell,255XtNwidth, (XtArgVal)&w1,256XtNheight, (XtArgVal)&h1,257XtNborderWidth, (XtArgVal)&bw,258(String)NULL );259260/* get position of the mouse pointer */261XQueryPointer( display, XtWindow(dialog->popupShell),262&root, &child, &x2, &y2, &x3, &y3, &bt );263264/* find a nice position for the dialog */265tmp = DisplayWidth( display, DefaultScreen(display) );266x1 = x2 - (bw+w1)/2;267if ( tmp-w1-2*bw < x1 ) x1 = tmp-w1-2*bw;268if ( x1 < 0 ) x1 = 0;269tmp = DisplayHeight( display, DefaultScreen(display) );270y1 = y2 - (bw+h1)/2;271if ( tmp-h1-2*bw < y1 ) y1 = tmp-h1-2*bw;272if ( y1 < 0 ) y1 = 0;273274/* move the popup dialog to this position */275XtVaSetValues( dialog->popupShell,276XtNx, (XtArgVal)x1,277XtNy, (XtArgVal)y1,278(String)NULL );279280/* pop up shell */281XtPopup( dialog->popupShell, XtGrabExclusive );282for ( i = 0; i < XtNumber(buttons); i++ )283if ( dialog->buttons[i] )284XDefineCursor( display,285XtWindow(dialog->buttons[i]), BlobCursor );286287288/* and grab keyboard */289XGrabKeyboard( display, XtWindow(dialog->dialog), False,290GrabModeAsync, GrabModeAsync, CurrentTime );291292/* wait until at least one button is selected */293dialog->result = 0;294while ( ( dialog->result & dialog->button ) == 0 )295{296XEvent event;297char str[128];298KeySym keysym;299Int len;300301/* get next application event */302XtAppNextEvent(dialog->context, &event);303304/* is the event a key event */305if ( event.type == KeyPress )306{307if ( dialog->defaultButton != 0 )308{309310/* convert key event into text string and check for return */311len = XLookupString((XKeyEvent*)&event,str,128,&keysym,0);312if ( len != 0 && ( str[0] == '\n' || str[0] == '\r' ) )313{314dialog->result |= dialog->defaultButton;315continue;316}317}318}319320/* dipatch event normally */321XtDispatchEvent(&event);322}323324/* return answer string in <result> */325if ( result )326*result = XawDialogGetValueString(dialog->dialog);327328/* remove pop up menu from screen */329XtPopdown(dialog->popupShell);330return dialog->result & dialog->button;331}332333334/****************************************************************************335**336337*E popdial.c . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here338*/339340341342