1c9e2be55Smrg/*
2c9e2be55Smrg * $XConsortium: screen.c,v 2.65 95/01/06 16:39:19 swick Exp $
3c9e2be55Smrg *
4c9e2be55Smrg *
5c9e2be55Smrg *		        COPYRIGHT 1987, 1989
6c9e2be55Smrg *		   DIGITAL EQUIPMENT CORPORATION
7c9e2be55Smrg *		       MAYNARD, MASSACHUSETTS
8c9e2be55Smrg *			ALL RIGHTS RESERVED.
9c9e2be55Smrg *
10c9e2be55Smrg * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
11c9e2be55Smrg * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
12c9e2be55Smrg * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
13c9e2be55Smrg * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
14c9e2be55Smrg *
15c9e2be55Smrg * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
16c9e2be55Smrg * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
17c9e2be55Smrg * ADDITION TO THAT SET FORTH ABOVE.
18c9e2be55Smrg *
19c9e2be55Smrg * Permission to use, copy, modify, and distribute this software and its
20c9e2be55Smrg * documentation for any purpose and without fee is hereby granted, provided
21c9e2be55Smrg * that the above copyright notice appear in all copies and that both that
22c9e2be55Smrg * copyright notice and this permission notice appear in supporting
23c9e2be55Smrg * documentation, and that the name of Digital Equipment Corporation not be
24c9e2be55Smrg * used in advertising or publicity pertaining to distribution of the software
25c9e2be55Smrg * without specific, written prior permission.
26c9e2be55Smrg */
27c9e2be55Smrg/* $XFree86: xc/programs/xmh/screen.c,v 1.2 2001/10/28 03:34:39 tsi Exp $ */
28c9e2be55Smrg
29c9e2be55Smrg/* scrn.c -- management of scrns. */
30c9e2be55Smrg
31c9e2be55Smrg#include "xmh.h"
32c9e2be55Smrg
33c9e2be55SmrgXmhMenuEntryRec	folderMenu[] = {
34c9e2be55Smrg    {"open",			DoOpenFolder},
35c9e2be55Smrg    {"openInNew", 		DoOpenFolderInNewWindow},
36c9e2be55Smrg    {"create",			DoCreateFolder},
37c9e2be55Smrg    {"delete",			DoDeleteFolder},
38c9e2be55Smrg    {"line",			(XtCallbackProc) NULL},
39c9e2be55Smrg    {"close",			DoClose},
40c9e2be55Smrg};
41c9e2be55Smrg
42c9e2be55SmrgXmhMenuEntryRec	tocMenu[] = {
43c9e2be55Smrg    {"inc",			DoIncorporateNewMail},
44c9e2be55Smrg    {"commit",			DoCommit},
45c9e2be55Smrg    {"pack",			DoPack},
46c9e2be55Smrg    {"sort",			DoSort},
47c9e2be55Smrg    {"rescan",			DoForceRescan},
48c9e2be55Smrg};
49c9e2be55Smrg
50c9e2be55SmrgXmhMenuEntryRec	messageMenu[] = {
51c9e2be55Smrg    {"compose",			DoComposeMessage},
52c9e2be55Smrg    {"next",			DoNextView},
53c9e2be55Smrg    {"prev",			DoPrevView},
54c9e2be55Smrg    {"delete",			DoDelete},
55c9e2be55Smrg    {"move",			DoMove},
56c9e2be55Smrg    {"copy",			DoCopy},
57c9e2be55Smrg    {"unmark",			DoUnmark},
58c9e2be55Smrg    {"viewNew",			DoViewNew},
59c9e2be55Smrg    {"reply",			DoReply},
60c9e2be55Smrg    {"forward",			DoForward},
61c9e2be55Smrg    {"useAsComp",		DoTocUseAsComp},
62c9e2be55Smrg    {"print",			DoPrint},
63c9e2be55Smrg};
64c9e2be55Smrg
65c9e2be55SmrgXmhMenuEntryRec	sequenceMenu[] = {
66c9e2be55Smrg    {"pick",			DoPickMessages},
67c9e2be55Smrg    {"openSeq",			DoOpenSeq},
68c9e2be55Smrg    {"addToSeq",		DoAddToSeq},
69c9e2be55Smrg    {"removeFromSeq",		DoRemoveFromSeq},
70c9e2be55Smrg    {"deleteSeq",		DoDeleteSeq},
71c9e2be55Smrg    {"line",			(XtCallbackProc) NULL},
72c9e2be55Smrg    {"all",			DoSelectSequence},
73c9e2be55Smrg};
74c9e2be55Smrg
75c9e2be55SmrgXmhMenuEntryRec	viewMenu[] = {
76c9e2be55Smrg    {"reply",			DoViewReply},
77c9e2be55Smrg    {"forward",			DoViewForward},
78c9e2be55Smrg    {"useAsComp",		DoViewUseAsComposition},
79c9e2be55Smrg    {"edit",			DoEditView},
80c9e2be55Smrg    {"save",			DoSaveView},
81c9e2be55Smrg    {"print",			DoPrintView},
82c9e2be55Smrg};
83c9e2be55Smrg
84c9e2be55SmrgXmhMenuEntryRec	optionMenu[] = {
85c9e2be55Smrg    {"reverse",			DoReverseReadOrder},
86c9e2be55Smrg};
87c9e2be55Smrg
88c9e2be55SmrgXmhMenuButtonDescRec	MenuBoxButtons[] = {
89c9e2be55Smrg    {"folderButton",	"folderMenu",	XMH_FOLDER,	folderMenu,
90c9e2be55Smrg	 XtNumber(folderMenu) },
91c9e2be55Smrg    {"tocButton",	"tocMenu",	XMH_TOC,	tocMenu,
92c9e2be55Smrg	 XtNumber(tocMenu) },
93c9e2be55Smrg    {"messageButton",	"messageMenu",	XMH_MESSAGE,	messageMenu,
94c9e2be55Smrg	 XtNumber(messageMenu) },
95c9e2be55Smrg    {"sequenceButton",	"sequenceMenu",	XMH_SEQUENCE,	sequenceMenu,
96c9e2be55Smrg	 XtNumber(sequenceMenu) },
97c9e2be55Smrg    {"viewButton",	"viewMenu",	XMH_VIEW,	viewMenu,
98c9e2be55Smrg	 XtNumber(viewMenu) },
99c9e2be55Smrg    {"optionButton",	"optionMenu",	XMH_OPTION,	optionMenu,
100c9e2be55Smrg	 XtNumber(optionMenu) },
101c9e2be55Smrg};
102c9e2be55Smrg
103c9e2be55Smrg
104c9e2be55Smrg/* Fill in the buttons for the view commands. */
105c9e2be55Smrg
106c9e2be55Smrgstatic void FillViewButtons(
107c9e2be55SmrgScrn scrn)
108c9e2be55Smrg{
109c9e2be55Smrg    ButtonBox buttonbox = scrn->viewbuttons;
110c9e2be55Smrg    BBoxAddButton(buttonbox, "close", commandWidgetClass, True);
111c9e2be55Smrg    BBoxAddButton(buttonbox, "reply", commandWidgetClass, True);
112c9e2be55Smrg    BBoxAddButton(buttonbox, "forward", commandWidgetClass, True);
113c9e2be55Smrg    BBoxAddButton(buttonbox, "useAsComp", commandWidgetClass, True);
114c9e2be55Smrg    BBoxAddButton(buttonbox, "edit", commandWidgetClass, True);
115c9e2be55Smrg    BBoxAddButton(buttonbox, "save", commandWidgetClass, False);
116c9e2be55Smrg    BBoxAddButton(buttonbox, "print", commandWidgetClass, True);
117c9e2be55Smrg    BBoxAddButton(buttonbox, "delete", commandWidgetClass, True);
118c9e2be55Smrg}
119d859ff80Smrg
120c9e2be55Smrg
121c9e2be55Smrg
122c9e2be55Smrgstatic void FillCompButtons(
123c9e2be55SmrgScrn scrn)
124c9e2be55Smrg{
125c9e2be55Smrg    ButtonBox buttonbox = scrn->viewbuttons;
126c9e2be55Smrg    BBoxAddButton(buttonbox, "close", commandWidgetClass, True);
127c9e2be55Smrg    BBoxAddButton(buttonbox, "send", commandWidgetClass, True);
128c9e2be55Smrg    BBoxAddButton(buttonbox, "reset", commandWidgetClass, True);
129c9e2be55Smrg    BBoxAddButton(buttonbox, "compose", commandWidgetClass, True);
130c9e2be55Smrg    BBoxAddButton(buttonbox, "save", commandWidgetClass, True);
131c9e2be55Smrg    BBoxAddButton(buttonbox, "insert", commandWidgetClass, True);
132c9e2be55Smrg}
133c9e2be55Smrg
134c9e2be55Smrg
135c9e2be55Smrgstatic void MakeCommandMenu(
136c9e2be55Smrg    Scrn		scrn,
137c9e2be55Smrg    XmhMenuButtonDesc	mbd)
138c9e2be55Smrg{
139c9e2be55Smrg    register Cardinal i;
140c9e2be55Smrg    Cardinal	 n;
141c9e2be55Smrg    Widget	menu;
142c9e2be55Smrg    ButtonBox	buttonbox = scrn->mainbuttons;
143c9e2be55Smrg    XmhMenuEntry	e;
144c9e2be55Smrg    Boolean	indent;
145c9e2be55Smrg    WidgetClass	widgetclass;
146c9e2be55Smrg    Arg		args[4];
147c9e2be55Smrg    static XtCallbackRec callbacks[] = {
148c9e2be55Smrg	{ (XtCallbackProc) NULL, (XtPointer) NULL},
149c9e2be55Smrg	{ (XtCallbackProc) NULL, (XtPointer) NULL},
150c9e2be55Smrg	{ (XtCallbackProc) NULL, (XtPointer) NULL},
151c9e2be55Smrg    };
152c9e2be55Smrg
153d859ff80Smrg    /* Menus are created as children of the Paned widget of the scrn in order
154c9e2be55Smrg     * that they can be used both as pop-up and as pull-down menus.
155c9e2be55Smrg     */
156c9e2be55Smrg
157c9e2be55Smrg    n = 0;
158c9e2be55Smrg    if (mbd->id == XMH_SEQUENCE) {
159c9e2be55Smrg	XtSetArg(args[n], XtNallowShellResize, True); 	n++;
160c9e2be55Smrg    }
161c9e2be55Smrg    menu = XtCreatePopupShell(mbd->menu_name, simpleMenuWidgetClass,
162c9e2be55Smrg			      scrn->widget, args, n);
163c9e2be55Smrg
164c9e2be55Smrg    indent = (mbd->id == XMH_SEQUENCE || mbd->id == XMH_OPTION) ? True : False;
165c9e2be55Smrg    e = mbd->entry;
166c9e2be55Smrg    for (i=0; i < mbd->num_entries; i++, e++) {
167c9e2be55Smrg	n = 0;
168c9e2be55Smrg	if (e->function) {
169c9e2be55Smrg	    callbacks[0].callback = e->function;
170c9e2be55Smrg	    callbacks[0].closure  = (XtPointer) scrn;
171c9e2be55Smrg	    callbacks[1].callback = (app_resources.sticky_menu
172c9e2be55Smrg				     ? (XtCallbackProc) DoRememberMenuSelection
173c9e2be55Smrg				     : (XtCallbackProc) NULL);
174c9e2be55Smrg	    XtSetArg(args[n], XtNcallback, callbacks);	n++;
175c9e2be55Smrg
176c9e2be55Smrg	    if (indent) { XtSetArg(args[n], XtNleftMargin, 18);	n++; }
177c9e2be55Smrg	    widgetclass = smeBSBObjectClass;
178d859ff80Smrg	} else
179c9e2be55Smrg	    widgetclass = smeLineObjectClass;
180c9e2be55Smrg	XtCreateManagedWidget(e->name, widgetclass, menu, args, n);
181c9e2be55Smrg    }
182c9e2be55Smrg
183c9e2be55Smrg    AttachMenuToButton( BBoxFindButtonNamed( buttonbox, mbd->button_name),
184c9e2be55Smrg		       menu, mbd->menu_name);
185c9e2be55Smrg    if (mbd->id == XMH_OPTION && app_resources.reverse_read_order)
186c9e2be55Smrg	ToggleMenuItem(XtNameToWidget(menu, "reverse"), True);
187c9e2be55Smrg}
188c9e2be55Smrg
189c9e2be55Smrg
190c9e2be55Smrg/* Create subwidgets for a toc&view window. */
191c9e2be55Smrg
192c9e2be55Smrgstatic void MakeTocAndView(Scrn scrn)
193c9e2be55Smrg{
194c9e2be55Smrg    register int	i;
195c9e2be55Smrg    XmhMenuButtonDesc	mbd;
196c9e2be55Smrg    ButtonBox		buttonbox;
19766d665a3Smrg    const char		*name;
198c9e2be55Smrg    static XawTextSelectType sarray[] = {XawselectLine,
199c9e2be55Smrg					XawselectPosition,
200c9e2be55Smrg					XawselectAll,
201c9e2be55Smrg					XawselectNull};
202c9e2be55Smrg    static Arg args[] = {
203c9e2be55Smrg	{ XtNselectTypes,	(XtArgVal) sarray},
204c9e2be55Smrg	{ XtNdisplayCaret,	(XtArgVal) False}
205c9e2be55Smrg    };
206c9e2be55Smrg
207c9e2be55Smrg    scrn->mainbuttons   = BBoxCreate(scrn, "menuBox");
208c9e2be55Smrg    scrn->folderlabel   = CreateTitleBar(scrn, "folderTitlebar");
209c9e2be55Smrg    scrn->folderbuttons = BBoxCreate(scrn, "folders");
210c9e2be55Smrg    scrn->toclabel      = CreateTitleBar(scrn, "tocTitlebar");
211c9e2be55Smrg    scrn->tocwidget	= CreateTextSW(scrn, "toc", args, XtNumber(args));
212d859ff80Smrg    if (app_resources.command_button_count > 0)
213c9e2be55Smrg	scrn->miscbuttons = BBoxCreate(scrn, "commandBox");
214c9e2be55Smrg    scrn->viewlabel     = CreateTitleBar(scrn, "viewTitlebar");
215c9e2be55Smrg    scrn->viewwidget    = CreateTextSW(scrn, "view", args, (Cardinal) 0);
216c9e2be55Smrg
217c9e2be55Smrg    /* the command buttons and menus */
218c9e2be55Smrg
219c9e2be55Smrg    buttonbox = scrn->mainbuttons;
220c9e2be55Smrg    mbd = MenuBoxButtons;
221c9e2be55Smrg    for (i=0; i < XtNumber(MenuBoxButtons); i++, mbd++) {
222c9e2be55Smrg	name = mbd->button_name;
223c9e2be55Smrg	BBoxAddButton(buttonbox, name, menuButtonWidgetClass, True);
224c9e2be55Smrg	MakeCommandMenu(scrn, mbd);
225c9e2be55Smrg    }
226c9e2be55Smrg
227c9e2be55Smrg    /* the folder buttons; folder menus are created on demand. */
228c9e2be55Smrg
229c9e2be55Smrg    buttonbox = scrn->folderbuttons;
230c9e2be55Smrg    for (i=0 ; i<numFolders ; i++) {
231c9e2be55Smrg	name = TocName(folderList[i]);
232c9e2be55Smrg	if (! IsSubfolder(name))
233c9e2be55Smrg	    BBoxAddButton(buttonbox, name, menuButtonWidgetClass, True);
234c9e2be55Smrg	if (app_resources.new_mail_check &&
235c9e2be55Smrg	    numScrns > 1 &&
236c9e2be55Smrg	    TocCanIncorporate(folderList[i]))
237c9e2be55Smrg	    BBoxMailFlag(buttonbox, name, TocHasMail(folderList[i]));
238c9e2be55Smrg    }
239c9e2be55Smrg
240c9e2be55Smrg    /* the optional miscellaneous command buttons */
241c9e2be55Smrg
242c9e2be55Smrg    if (app_resources.command_button_count > 0) {
24366d665a3Smrg	char	name[18];
244c9e2be55Smrg	if (app_resources.command_button_count > 500)
245c9e2be55Smrg	    app_resources.command_button_count = 500;
246c9e2be55Smrg	for (i=1; i <= app_resources.command_button_count; i++) {
24766d665a3Smrg	    snprintf(name, sizeof(name), "button%d", i);
248c9e2be55Smrg	    BBoxAddButton(scrn->miscbuttons, name, commandWidgetClass, True);
249c9e2be55Smrg	}
250c9e2be55Smrg    }
251c9e2be55Smrg}
252c9e2be55Smrg
253c9e2be55Smrgstatic void MakeView(Scrn scrn)
254c9e2be55Smrg{
255c9e2be55Smrg    scrn->viewlabel = CreateTitleBar(scrn, "viewTitlebar");
256c9e2be55Smrg    scrn->viewwidget = CreateTextSW(scrn, "view", (ArgList)NULL, (Cardinal)0);
257c9e2be55Smrg    scrn->viewbuttons = BBoxCreate(scrn, "viewButtons");
258c9e2be55Smrg    FillViewButtons(scrn);
259c9e2be55Smrg}
260c9e2be55Smrg
261c9e2be55Smrg
262c9e2be55Smrgstatic void MakeComp(Scrn scrn)
263c9e2be55Smrg{
264c9e2be55Smrg    scrn->viewlabel = CreateTitleBar(scrn, "composeTitlebar");
265c9e2be55Smrg    scrn->viewwidget = CreateTextSW(scrn, "comp", (ArgList)NULL, (Cardinal)0);
266c9e2be55Smrg    scrn->viewbuttons = BBoxCreate(scrn, "compButtons");
267c9e2be55Smrg    FillCompButtons(scrn);
268c9e2be55Smrg}
269c9e2be55Smrg
270c9e2be55Smrg
271c9e2be55Smrg/* Create a scrn of the given type. */
272c9e2be55Smrg
273c9e2be55SmrgScrn CreateNewScrn(ScrnKind kind)
274c9e2be55Smrg{
275c9e2be55Smrg    int i;
276c9e2be55Smrg    Scrn scrn;
277c9e2be55Smrg    static Arg arglist[] = {
278c9e2be55Smrg	{ XtNgeometry,	(XtArgVal) NULL},
279c9e2be55Smrg	{ XtNinput,	(XtArgVal) True}
280c9e2be55Smrg    };
281c9e2be55Smrg
282c9e2be55Smrg    for (i=0 ; i<numScrns ; i++)
283c9e2be55Smrg	if (scrnList[i]->kind == kind && !scrnList[i]->mapped)
284c9e2be55Smrg	    return scrnList[i];
285c9e2be55Smrg    switch (kind) {
286c9e2be55Smrg       case STtocAndView: arglist[0].value =
287c9e2be55Smrg			   (XtArgVal)app_resources.toc_geometry;	break;
288c9e2be55Smrg       case STview:	  arglist[0].value =
289c9e2be55Smrg			   (XtArgVal)app_resources.view_geometry;	break;
290c9e2be55Smrg       case STcomp:	  arglist[0].value =
291c9e2be55Smrg			   (XtArgVal)app_resources.comp_geometry;	break;
292c9e2be55Smrg       case STpick:	  arglist[0].value =
293c9e2be55Smrg			   (XtArgVal)app_resources.pick_geometry;	break;
294c9e2be55Smrg    }
295c9e2be55Smrg
296c9e2be55Smrg    numScrns++;
29766d665a3Smrg    scrnList = XtReallocArray(scrnList, numScrns, sizeof(Scrn));
298c9e2be55Smrg    scrn = scrnList[numScrns - 1] = XtNew(ScrnRec);
299c9e2be55Smrg    bzero((char *)scrn, sizeof(ScrnRec));
300c9e2be55Smrg    scrn->kind = kind;
301c9e2be55Smrg    if (numScrns == 1) scrn->parent = toplevel;
302c9e2be55Smrg    else scrn->parent = XtCreatePopupShell(
303c9e2be55Smrg				   progName, topLevelShellWidgetClass,
304c9e2be55Smrg				   toplevel, arglist, XtNumber(arglist));
305c9e2be55Smrg    XtAugmentTranslations(scrn->parent,
306c9e2be55Smrg			  app_resources.wm_protocols_translations);
307c9e2be55Smrg    scrn->widget =
308c9e2be55Smrg	XtCreateManagedWidget(progName, panedWidgetClass, scrn->parent,
309c9e2be55Smrg			      (ArgList) NULL, (Cardinal) 0);
310c9e2be55Smrg
311c9e2be55Smrg    switch (kind) {
312c9e2be55Smrg	case STtocAndView: 	MakeTocAndView(scrn); break;
313c9e2be55Smrg	case STview:		MakeView(scrn); break;
314c9e2be55Smrg	case STcomp:		MakeComp(scrn);	break;
315c9e2be55Smrg	default: break;
316c9e2be55Smrg    }
317c9e2be55Smrg
318c9e2be55Smrg    if (kind != STpick) {
319c9e2be55Smrg	int	theight, min, max;
320c9e2be55Smrg	Arg	args[1];
321c9e2be55Smrg
322c9e2be55Smrg	DEBUG("Realizing...")
323c9e2be55Smrg	XtRealizeWidget(scrn->parent);
324c9e2be55Smrg	DEBUG(" done.\n")
325c9e2be55Smrg
326c9e2be55Smrg	switch (kind) {
327c9e2be55Smrg	  case STtocAndView:
328c9e2be55Smrg	    BBoxLockSize(scrn->mainbuttons);
329c9e2be55Smrg	    BBoxLockSize(scrn->folderbuttons);
330c9e2be55Smrg	    theight = GetHeight(scrn->tocwidget) + GetHeight(scrn->viewwidget);
331c9e2be55Smrg	    theight = app_resources.toc_percentage * theight / 100;
332c9e2be55Smrg	    XawPanedGetMinMax((Widget) scrn->tocwidget, &min, &max);
333c9e2be55Smrg	    XawPanedSetMinMax((Widget) scrn->tocwidget, theight, theight);
334c9e2be55Smrg	    XawPanedSetMinMax((Widget) scrn->tocwidget, min, max);
335c9e2be55Smrg	    if (scrn->miscbuttons)
336c9e2be55Smrg		BBoxLockSize(scrn->miscbuttons);
337c9e2be55Smrg
338c9e2be55Smrg	    /* fall through */
339c9e2be55Smrg
340c9e2be55Smrg	  case STview:
341c9e2be55Smrg
342c9e2be55Smrg	    /* Install accelerators; not active while editing in the view */
343c9e2be55Smrg
344c9e2be55Smrg	    XtSetArg(args[0], XtNtranslations, &(scrn->edit_translations));
345c9e2be55Smrg	    XtGetValues(scrn->viewwidget, args, (Cardinal) 1);
346c9e2be55Smrg	    XtInstallAllAccelerators(scrn->widget, scrn->widget);
347c9e2be55Smrg	    if (kind == STtocAndView)
348c9e2be55Smrg		XtInstallAllAccelerators(scrn->tocwidget, scrn->widget);
349c9e2be55Smrg	    XtInstallAllAccelerators(scrn->viewwidget, scrn->widget);
350c9e2be55Smrg	    XtSetArg(args[0], XtNtranslations, &(scrn->read_translations));
351c9e2be55Smrg	    XtGetValues(scrn->viewwidget, args, (Cardinal) 1);
352c9e2be55Smrg
353c9e2be55Smrg	    if (kind == STview)
354c9e2be55Smrg		BBoxLockSize(scrn->viewbuttons);
355c9e2be55Smrg	    break;
356c9e2be55Smrg
357c9e2be55Smrg	  case STcomp:
358c9e2be55Smrg	    BBoxLockSize(scrn->viewbuttons);
359c9e2be55Smrg	    XtInstallAllAccelerators(scrn->viewwidget, scrn->widget);
360c9e2be55Smrg	    XtSetKeyboardFocus(scrn->parent, scrn->viewwidget);
361c9e2be55Smrg	    break;
362c9e2be55Smrg
363c9e2be55Smrg	  default:
364c9e2be55Smrg	    break;
365c9e2be55Smrg	}
366c9e2be55Smrg
367c9e2be55Smrg	InitBusyCursor(scrn);
368c9e2be55Smrg	XDefineCursor(XtDisplay(scrn->parent), XtWindow(scrn->parent),
369c9e2be55Smrg		      app_resources.cursor);
370c9e2be55Smrg	(void) XSetWMProtocols(XtDisplay(scrn->parent), XtWindow(scrn->parent),
371c9e2be55Smrg			       protocolList, XtNumber(protocolList));
372c9e2be55Smrg    }
373c9e2be55Smrg    scrn->mapped = False;
374c9e2be55Smrg    return scrn;
375c9e2be55Smrg}
376c9e2be55Smrg
377c9e2be55Smrg
378c9e2be55SmrgScrn NewViewScrn(void)
379c9e2be55Smrg{
380c9e2be55Smrg    return CreateNewScrn(STview);
381c9e2be55Smrg}
382c9e2be55Smrg
383c9e2be55SmrgScrn NewCompScrn(void)
384c9e2be55Smrg{
385c9e2be55Smrg    Scrn scrn;
386c9e2be55Smrg    scrn = CreateNewScrn(STcomp);
387c9e2be55Smrg    scrn->assocmsg = (Msg)NULL;
388c9e2be55Smrg    return scrn;
389c9e2be55Smrg}
390c9e2be55Smrg
391c9e2be55Smrgvoid ScreenSetAssocMsg(Scrn scrn, Msg msg)
392c9e2be55Smrg{
393c9e2be55Smrg    scrn->assocmsg = msg;
394c9e2be55Smrg}
395c9e2be55Smrg
396c9e2be55Smrg/* Destroy the screen.  If unsaved changes are in a msg, too bad. */
397c9e2be55Smrg
398c9e2be55Smrgvoid DestroyScrn(Scrn scrn)
399c9e2be55Smrg{
400c9e2be55Smrg    if (scrn->mapped) {
401c9e2be55Smrg	scrn->mapped = False;
402c9e2be55Smrg	XtPopdown(scrn->parent);
403c9e2be55Smrg	TocSetScrn((Toc) NULL, scrn);
404c9e2be55Smrg	MsgSetScrnForce((Msg) NULL, scrn);
405c9e2be55Smrg	lastInput.win = -1;
406c9e2be55Smrg    }
407c9e2be55Smrg}
408c9e2be55Smrg
409c9e2be55Smrg
410c9e2be55Smrgvoid MapScrn(Scrn scrn)
411c9e2be55Smrg{
412c9e2be55Smrg    if (!scrn->mapped) {
413c9e2be55Smrg	XtPopup(scrn->parent, XtGrabNone);
414c9e2be55Smrg	scrn->mapped = True;
415c9e2be55Smrg    }
416c9e2be55Smrg}
417c9e2be55Smrg
418c9e2be55Smrg
419c9e2be55SmrgScrn ScrnFromWidget(Widget w) /* heavily used, should be efficient */
420c9e2be55Smrg{
421c9e2be55Smrg    register int i;
422c9e2be55Smrg    while (w && ! XtIsTopLevelShell(w))
423c9e2be55Smrg	w = XtParent(w);
424c9e2be55Smrg    if (w) {
425c9e2be55Smrg	for (i=0 ; i<numScrns ; i++) {
426c9e2be55Smrg	    if (w == (Widget) scrnList[i]->parent)
427c9e2be55Smrg		return scrnList[i];
428c9e2be55Smrg	}
429c9e2be55Smrg    }
430c9e2be55Smrg    Punt("ScrnFromWidget failed!");
431c9e2be55Smrg    return NULL;
432c9e2be55Smrg}
433d859ff80Smrg
434c9e2be55Smrg
435c9e2be55Smrg/* Figure out which buttons should and shouldn't be enabled in the given
436c9e2be55Smrg * screen.  This should be called whenever something major happens to the
437c9e2be55Smrg * screen.
438c9e2be55Smrg */
439c9e2be55Smrg
440c9e2be55Smrg
441c9e2be55Smrg/*ARGSUSED*/
442c9e2be55Smrgstatic void EnableCallback(Widget w, XtPointer data, XtPointer junk)
443c9e2be55Smrg{
444c9e2be55Smrg  EnableProperButtons( (Scrn) data);
445d859ff80Smrg}
446c9e2be55Smrg
447c9e2be55Smrg#define SetButton(buttonbox, name, value) \
448c9e2be55Smrg    if (value) BBoxEnable(BBoxFindButtonNamed(buttonbox, name)); \
449c9e2be55Smrg    else BBoxDisable(BBoxFindButtonNamed(buttonbox, name));
450c9e2be55Smrg
451c9e2be55Smrg
452c9e2be55Smrgvoid EnableProperButtons(Scrn scrn)
453c9e2be55Smrg{
454c9e2be55Smrg    int value, changed, reapable;
455c9e2be55Smrg    Button	button;
456c9e2be55Smrg
457c9e2be55Smrg    if (scrn) {
458c9e2be55Smrg	switch (scrn->kind) {
459c9e2be55Smrg	  case STtocAndView:
460c9e2be55Smrg	    button = BBoxFindButtonNamed
461c9e2be55Smrg		(scrn->mainbuttons, MenuBoxButtons[XMH_TOC].button_name);
462c9e2be55Smrg	    value = TocCanIncorporate(scrn->toc);
463c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "inc", value);
464c9e2be55Smrg
465c9e2be55Smrg	    button = BBoxFindButtonNamed
466c9e2be55Smrg		(scrn->mainbuttons, MenuBoxButtons[XMH_SEQUENCE].button_name);
467c9e2be55Smrg	    value = TocHasSequences(scrn->toc);
468c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "openSeq", value);
469c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "addToSeq", value);
470c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "removeFromSeq", value);
471c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "deleteSeq", value);
472c9e2be55Smrg
473c9e2be55Smrg	    button = BBoxFindButtonNamed
474c9e2be55Smrg		 (scrn->mainbuttons, MenuBoxButtons[XMH_VIEW].button_name);
475c9e2be55Smrg	    value = (scrn->msg != NULL && !MsgGetEditable(scrn->msg));
476c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "edit", value);
477c9e2be55Smrg	    SendMenuEntryEnableMsg(button, "save",
478c9e2be55Smrg				   scrn->msg != NULL && !value);
479c9e2be55Smrg	    break;
480c9e2be55Smrg	  case STview:
481c9e2be55Smrg	    value = (scrn->msg != NULL && !MsgGetEditable(scrn->msg));
482c9e2be55Smrg	    SetButton(scrn->viewbuttons, "edit", value);
483c9e2be55Smrg	    SetButton(scrn->viewbuttons, "save", scrn->msg != NULL && !value);
484c9e2be55Smrg	    break;
485c9e2be55Smrg	  case STcomp:
486c9e2be55Smrg	    if (scrn->msg != NULL) {
487c9e2be55Smrg		changed = MsgChanged(scrn->msg);
488c9e2be55Smrg		reapable = MsgGetReapable(scrn->msg);
489c9e2be55Smrg		SetButton(scrn->viewbuttons, "send", changed || !reapable);
490c9e2be55Smrg		SetButton(scrn->viewbuttons, "save", changed || reapable);
491c9e2be55Smrg		SetButton(scrn->viewbuttons, "insert",
492c9e2be55Smrg			  scrn->assocmsg != NULL ? True : False);
493c9e2be55Smrg
494d859ff80Smrg		if (!changed)
495c9e2be55Smrg		    MsgSetCallOnChange(scrn->msg, EnableCallback,
496c9e2be55Smrg				       (XtPointer) scrn);
497d859ff80Smrg		else
498c9e2be55Smrg		    MsgSetCallOnChange(scrn->msg, (XtCallbackProc) NULL,
499c9e2be55Smrg				       (XtPointer) NULL);
500c9e2be55Smrg
501c9e2be55Smrg	    } else {
502c9e2be55Smrg		BBoxDisable( BBoxFindButtonNamed(scrn->viewbuttons, "send"));
503c9e2be55Smrg		BBoxDisable( BBoxFindButtonNamed(scrn->viewbuttons, "save"));
504c9e2be55Smrg		BBoxDisable( BBoxFindButtonNamed(scrn->viewbuttons, "insert"));
505c9e2be55Smrg	    }
506c9e2be55Smrg	    break;
507c9e2be55Smrg	  default:
508c9e2be55Smrg	    break;
509c9e2be55Smrg	}
510c9e2be55Smrg    }
511c9e2be55Smrg}
512