#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <xdclass.h>
#include <utils.h>

#include <iostream.h>

void CDialog::xd_set_window_text(LPCSTR lpszString)
{
    // Set the text on a popup shell (the title and iconName).
    if (_xd_rootwidget != NULL)
	XtVaSetValues(_xd_rootwidget,
			XmNtitle, lpszString,
			XmNiconName, lpszString, // XmDialogShell will ignore this
			0);
    return;
}

int CDialog::xd_get_window_text(LPSTR lpszStringBuf, int nMaxCount) const
{
    // Get the text from a popup shell (the title)
    // Return value:
    //  0 if the widget has not been created (no call to create() yet).
    //  The length of the text otherwise.

    if (_xd_rootwidget==NULL)
	return 0;

    // _xd_rootwidget is an XmDialogShell
    String title;
    XtVaGetValues(_xd_rootwidget, XmNtitle, &title, 0);
    strncpy(lpszStringBuf, title, nMaxCount - 1);
    lpszStringBuf[nMaxCount-1]='\0';
    return strlen(lpszStringBuf);
}

int CDialog::xd_get_window_text_length() const
{
    // Get the length of the text from a popup shell (the title)
    // Return value:
    //  0 if the widget has not been created (no call to create() yet).
    //  The length of the text otherwise.

    if (_xd_rootwidget==NULL)
	return 0;

    // _xd_rootwidget is an ApplicationShell
    String title;
    XtVaGetValues(_xd_rootwidget, XmNtitle, &title, 0);
    return strlen(title);
}

static BOOL xd_show_dialog_window(int nCmdShow, Widget rootwidget)
{
    // Implements ShowWindow() for XmDialogShell
    // XmDialogShell is straightforward. It is visible
    // if and only if it has a managed child, and is
    // hidden and restored by unmanaging and managing the child.

    WidgetList children;
    Cardinal numchildren;
    XtVaGetValues(rootwidget,
		    XmNchildren, &children,
		    XmNnumChildren, &numchildren,
		    0);
    if (numchildren==0) // Invisible and staying that way
	return FALSE;
    Boolean wasvisible=XtIsManaged(children[0]);

    switch(nCmdShow)
    {
	case SW_HIDE:       XtUnmanageChild(children[0]);
			    break;
	case SW_RESTORE:    XtManageChild(children[0]);
			    break;
	default:            cerr << "CDialog::ShowWindow: Only SW_HIDE and SW_RESTORE supported for XmDialogShell windows" << endl;
    }
    return wasvisible;
}

static BOOL xd_show_toplevel_window(int nCmdShow, Widget rootwidget)
{
    // Implements ShowWindow() for TopLevelShell
    // First have to determine if window is "visible". In MS-W
    // the normal and iconic states both count as visible.

    unsigned long state=xd_get_visibility_state(rootwidget);
    BOOL wasvisible=(state!=WithdrawnState);

    // Now change the state as required - action depends on current state
    switch(nCmdShow)
    {
	case SW_HIDE:
	    if (XtWindow(rootwidget))     // Iconic or normal
		XtPopdown(rootwidget);
	    else                                // Unrealized
		XtRealizeWidget(rootwidget);
	    break;
	case SW_RESTORE:
	    if (!XtWindow(rootwidget)      // Unrealized
		|| state==WithdrawnState)       // or withdrawn
	    {
		XtVaSetValues(rootwidget,
		    XmNinitialState, NormalState,
		    0);
		XtPopup(rootwidget, XtGrabNone);
	    }
	    else                                // Iconic
		XtMapWidget(rootwidget);
	    break;
	case SW_SHOWMINIMIZED:
	    if (! XtWindow(rootwidget)      // Unrealized
		|| state==WithdrawnState)       // or withdrawn
	    {
		XtVaSetValues(rootwidget,
		    XmNinitialState, IconicState,
		    0);
		XtPopup(rootwidget, XtGrabNone);
	    }
	    else                                // Normal
		XIconifyWindow(XtDisplay(rootwidget),
				XtWindow(rootwidget),
				XScreenNumberOfScreen(XtScreen(rootwidget)));
	    break;
    }
    return wasvisible;
}

BOOL CDialog::xd_show_window(int nCmdShow)
{
    // Implements ShowWindow() for popup shells
    if (XmIsDialogShell(_xd_rootwidget))
	return xd_show_dialog_window(nCmdShow, _xd_rootwidget);
    else
	return xd_show_toplevel_window(nCmdShow, _xd_rootwidget);
}
