#include <X11/Intrinsic.h>
#include <X11/IntrinsicP.h>
#include <X11/Core.h>
#include <X11/CoreP.h>
#include <Xm/Xm.h>
#include <Xm/DialogS.h>

#ifndef   _NO_PROTO
static void PopdownApplicationShell(Widget shell)
#else  /* _NO_PROTO */
static void PopdownApplicationShell(shell)
	 Widget  shell ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 if (XtIsRealized(shell)) {
			 Display *display = XtDisplay(shell) ;
			 Window   window  = XtWindow(shell) ;
			 Screen  *screen  = XtScreen(shell) ;

			 (void) XWithdrawWindow(display, window, XScreenNumberOfScreen(screen)) ;
		 }
	 }
}

#ifndef   _NO_PROTO
static void PopdownDialogShell(Widget shell)
#else  /* _NO_PROTO */
static void PopdownDialogShell(shell)
	 Widget  shell ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 WidgetList children     = (WidgetList) 0 ;
		 Cardinal   num_children = 0 ;
		 Widget     child        = (Widget) 0 ;

		 XtVaGetValues(shell, XmNchildren, &children, XmNnumChildren, &num_children, NULL) ;

		 if (num_children > 0) {
			 if ((child = children[0]) != (Widget) 0) {
				 if (!child->core.being_destroyed) {
					 XtUnmanageChild(child) ;
				 }
			 }
		 }
	 }
}

#ifndef   _NO_PROTO
static void PopdownGeneralShell(Widget shell)
#else  /* _NO_PROTO */
static void PopdownGeneralShell(shell)
	 Widget  shell ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 if (XtIsRealized(shell)) {
			 XtPopdown(shell) ;
		 }
	 }
}

#ifndef   _NO_PROTO
void PopdownShell(Widget shell)
#else  /* _NO_PROTO */
void PopdownShell(shell)
	 Widget  shell ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 if (XmIsDialogShell(shell)) {
			 PopdownDialogShell(shell) ;
		 }
		 else if (XtIsApplicationShell(shell)) {
			 PopdownApplicationShell(shell) ;
		 }
		 else if (XtIsShell(shell)) {
			 PopdownGeneralShell(shell) ;
		 }
	 }
}

#ifndef   _NO_PROTO
void PopdownAllShells(Widget shell, Boolean include_me)
#else  /* _NO_PROTO */
void PopdownAllShells(shell, include_me)
	 Widget  shell ;
	 Boolean include_me ;
#endif /* _NO_PROTO */
{
	 WidgetList popups     = (WidgetList) 0 ;
	 Cardinal   num_popups = (Cardinal) 0 ;
	 Widget     popup ;
	 int        i ;

	 if (shell != (Widget) 0) {
		 popups     = shell->core.popup_list ;
		 num_popups = shell->core.num_popups ;

		 for (i = 0 ; i < num_popups ; i++) {
			 popup = popups[i] ;

			 if (popup != (Widget) 0) {
				 PopdownAllShells(popup, True) ;
			 }
		 }

		 if (include_me == True) {
			 PopdownShell(shell) ;
		 }
	 }
}

#ifndef   _NO_PROTO
static void PopupApplicationShell(Widget shell, Boolean de_iconify)
#else  /* _NO_PROTO */
static void PopupApplicationShell(shell, de_iconify)
	 Widget  shell ;
	 Boolean de_iconify ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 Boolean iconic = False ;

		 if (XtIsRealized(shell)) {
			 XtMapWidget(shell) ;
		 }

		 XtRealizeWidget(shell) ;

		 if (de_iconify == True) {
			 XtVaGetValues(shell, XmNiconic, &iconic, NULL) ;

			 if (iconic == True) {
				 XtVaSetValues(shell, XmNiconic, False, NULL) ;

				 XRaiseWindow(XtDisplay(shell), XtWindow(shell)) ;
			 }
		 }
	 }
}

#ifndef   _NO_PROTO
static void PopupDialogShell(Widget shell)
#else  /* _NO_PROTO */
static void PopupDialogShell(shell)
	 Widget  shell ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 WidgetList children     = (WidgetList) 0 ;
		 Cardinal   num_children = 0 ;
		 Widget     child        = (Widget) 0 ;

		 XtVaGetValues(shell, XmNchildren, &children, XmNnumChildren, &num_children, NULL) ;

		 if (num_children > 0) {
			 if ((child = children[0]) != (Widget) 0) {
				 if (!child->core.being_destroyed) {
					 XtManageChild(child) ;
				 }
			 }
		 }
	 }
}

#ifndef   _NO_PROTO
static void PopupGeneralShell(Widget shell, Boolean de_iconify)
#else  /* _NO_PROTO */
static void PopupGeneralShell(shell, de_iconify)
	 Widget  shell ;
	 Boolean de_iconify ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 Boolean iconic = False ;

		 if (XtIsRealized(shell)) {
			 XtPopup(shell, XtGrabNone) ;
		 }
		 else {
			 XtRealizeWidget(shell) ;
			 XtPopup(shell, XtGrabNone) ;
		 }

		 if (de_iconify == True) {
			 XtVaGetValues(shell, XmNiconic, &iconic, NULL) ;

			 if (iconic == True) {
				 XtVaSetValues(shell, XmNiconic, False, NULL) ;

				 XtMapWidget(shell) ;
				 XRaiseWindow(XtDisplay(shell), XtWindow(shell)) ;
			 }
		 }
	 }
}

#ifndef   _NO_PROTO
void PopupShell(Widget shell, Boolean de_iconify)
#else  /* _NO_PROTO */
void PopupShell(shell, de_iconify)
	 Widget  shell ;
	 Boolean de_iconify ;
#endif /* _NO_PROTO */
{
	 if ((shell != (Widget) 0) && !shell->core.being_destroyed) {
		 if (XmIsDialogShell(shell)) {
			 PopupDialogShell(shell) ;
		 }
		 else if (XtIsApplicationShell(shell)) {
			 PopupApplicationShell(shell, de_iconify) ;
		 }
		 else if (XtIsShell(shell)) {
			 PopupGeneralShell(shell, de_iconify) ;
		 }
	 }
}
