Files
mcde/programs/dtterm/Tabs.c
T
lotherkandClaude Opus 5.5 3969c15907 mcde: dtterm: closing a split pane leaves the window its size
The paned window gave the closed pane's height back to the shell and the
window shrank.  The neighbour above (else below) is held at both heights
until the paned window has laid it out, as a split holds the new pane
(SplitDone).  Measured on Xvfb: build/mcde-shots/split-1.png, split-2.png.

www: split and drop-down are done, the search field of dtfile too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 22:18:09 +00:00

353 lines
9.6 KiB
C

/*
* Tabs.c -- MCDE: tabs in a dtterm window
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* A dtterm window was a shell with one DtTermView. Now the shell holds a
* Motif XmTabStack; every tab is an XmPanedWindow (a "page", mcdeSplit) with
* one or more whole DtTermViews, each with its own shell process, menu and
* scroll bar: dtterm's terminal code does not change. A page is made with
* the tab and never moves -- Xt cannot give a widget another parent -- so a
* split is one more view in the same page.
*
* Ctrl+Shift+T a new tab, with the profile of this one
* Ctrl+Shift+D split: one more terminal in this tab, below
* Ctrl+Shift+W close the terminal (its process gets SIGHUP as on
* close); the tab with its last one
* Ctrl+Shift+Right/Left the next / previous tab
*
* Ctrl+Shift, so that no key a program in the terminal uses is taken. A
* tab whose shell exits closes by itself; the window closes with its last
* tab. The tab shows the title a program sets (TermParse.c sets it on the
* tab, and on the window only for the tab in front); choosing a tab gives
* the window that tab's title.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#include <stdio.h>
#include <string.h>
#include <X11/IntrinsicP.h> /* core.being_destroyed: a tab closing now is no tab */
#include <X11/keysym.h>
#include <Xm/Xm.h>
#include <Xm/PanedW.h>
#include <Xm/TabStack.h>
#include "TermView.h"
#include "TermViewMenu.h"
#include "Tabs.h"
static void (*newProc)(Widget view, const char *profile);
static Widget pendingSplit; /* the page the next view goes to (Split) */
void
_McdeTabsSetNewProc(void (*proc)(Widget view, const char *profile))
{
newProc = proc;
}
/* a tab is a page (an XmPanedWindow named mcdeSplit); the stack also holds
* its own bar of tabs */
static Boolean
IsTab(Widget w)
{
return XtIsManaged(w) && !w->core.being_destroyed &&
XmIsPanedWindow(w) && strcmp(XtName(w), "mcdeSplit") == 0;
}
static Boolean
IsView(Widget w)
{
return XtIsManaged(w) && !w->core.being_destroyed &&
XtIsSubclass(w, dtTermViewWidgetClass);
}
/* the views of PAGE (a paned window has sashes and separators too) */
static int
Views(Widget page, Widget *views, int max)
{
WidgetList kids;
Cardinal n, i;
int k = 0;
XtVaGetValues(page, XmNchildren, &kids, XmNnumChildren, &n, NULL);
for (i = 0; i < n && k < max; i++)
if (IsView(kids[i]))
views[k++] = kids[i];
return k;
}
/* the keyboard goes to the terminal of the tab in front, and the menus act
* on it (also from the top bar, where they are not posted from the tab) */
static void
Focus(Widget view)
{
Widget term = XtNameToWidget(view, "*dtTerm");
_DtTermViewMenuSetCurrent(view);
if (term != NULL)
(void) XmProcessTraversal(term, XmTRAVERSE_CURRENT);
}
static void
TermFocus(Widget w, XtPointer client, XEvent *ev, Boolean *cont)
{
(void) w;
(void) cont;
if (ev->type == FocusIn)
_DtTermViewMenuSetCurrent((Widget) client);
}
static Widget
ShellOf(Widget w)
{
while (w != NULL && !XtIsShell(w))
w = XtParent(w);
return w;
}
int
_McdeTabsCount(Widget container)
{
WidgetList kids;
Cardinal n, i;
int count = 0;
XtVaGetValues(container, XmNchildren, &kids, XmNnumChildren, &n, NULL);
for (i = 0; i < n; i++)
if (IsTab(kids[i]))
count++;
return count;
}
/* the managed tabs in order, and the position of W among them (or -1) */
static int
TabIndex(Widget container, Widget w, Widget *tabs, int max, int *count)
{
WidgetList kids;
Cardinal n, i;
int k = 0, at = -1;
XtVaGetValues(container, XmNchildren, &kids, XmNnumChildren, &n, NULL);
for (i = 0; i < n && k < max; i++) {
if (!IsTab(kids[i]))
continue;
if (kids[i] == w)
at = k;
tabs[k++] = kids[i];
}
*count = k;
return at;
}
/* the window takes the title of the tab in front */
static void
TabSelected(Widget w, XtPointer client, XtPointer call)
{
XmTabStackCallbackStruct *cbs = (XmTabStackCallbackStruct *) call;
XmString label = NULL;
char *text;
(void) client;
if (cbs == NULL || cbs->selected_child == NULL)
return;
XtVaGetValues(cbs->selected_child, XmNtabLabelString, &label, NULL);
if (label == NULL)
return;
text = (char *) XmStringUnparse(label, NULL, XmCHARSET_TEXT,
XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL);
if (text != NULL) {
XtVaSetValues(ShellOf(w), XmNtitle, text, NULL);
XtFree(text);
}
XmStringFree(label);
{
Widget views[16];
if (Views(cbs->selected_child, views, 16) > 0)
Focus(views[0]);
}
}
Widget
_McdeTabsContainer(Widget shell)
{
Widget c = XtNameToWidget(shell, "mcdeTabs");
Arg args[4];
int n = 0;
if (c != NULL)
return c;
XtSetArg(args[n], XmNtabSide, XmTABS_ON_TOP); n++;
XtSetArg(args[n], XmNtabStyle, XmTABS_BEVELED); n++;
XtSetArg(args[n], XmNshadowThickness, 1); n++;
c = XmCreateTabStack(shell, "mcdeTabs", args, n);
XtAddCallback(c, XmNtabSelectedCallback, TabSelected, NULL);
XtManageChild(c);
return c;
}
/* a new page in the window's tabs -- or the page being split -- for the
* view dtterm makes next (DtTermMain.c, CreateInstance) */
Widget
_McdeTabsPageFor(Widget shell)
{
Widget page = pendingSplit;
pendingSplit = NULL;
if (page != NULL && !page->core.being_destroyed)
return page;
page = XtVaCreateManagedWidget("mcdeSplit", xmPanedWindowWidgetClass,
_McdeTabsContainer(shell), XmNmarginWidth, 0, XmNmarginHeight, 0,
XmNspacing, 4, NULL);
return page;
}
static void
Select(Widget container, Widget page, int delta)
{
Widget tabs[64];
int count, at = TabIndex(container, page, tabs, 64, &count);
if (at < 0 || count < 2)
return;
XmTabStackSelectTab(tabs[(at + delta + count) % count], True);
}
static void
Keys(Widget w, XtPointer client, XEvent *ev, Boolean *cont)
{
Widget view = (Widget) client;
Widget page = XtParent(view);
Widget container = XtParent(page);
unsigned int mods = ev->xkey.state & (ControlMask | ShiftMask | Mod1Mask);
KeySym ks;
(void) w;
if (mods != (ControlMask | ShiftMask))
return;
ks = XLookupKeysym(&ev->xkey, 0);
if (ks == XK_t || ks == XK_T) {
/* the same profile as this tab (Profiles.c) */
if (newProc != NULL)
(*newProc)(view, NULL);
} else if (ks == XK_d || ks == XK_D) {
/* one more terminal in this page, the same profile */
if (newProc != NULL) {
pendingSplit = page;
(*newProc)(view, NULL);
pendingSplit = NULL;
}
} else if (ks == XK_w || ks == XK_W) {
/* as if its process had ended: the terminal, the tab with its
* last one, the window with its last tab */
XtCallCallbacks(view, DtNsubprocessTerminationCallback, NULL);
} else if (ks == XK_Right || ks == XK_Page_Down) {
Select(container, page, 1);
} else if (ks == XK_Left || ks == XK_Page_Up) {
Select(container, page, -1);
} else
return;
*cont = False;
}
/* after the layout of a split: the pane free again, and the keyboard in it
* (it could not take it before it was shown) */
static void
SplitDone(XtPointer data, XtIntervalId *id)
{
Widget view = (Widget) data;
(void) id;
if (view->core.being_destroyed)
return;
XtVaSetValues(view, XmNpaneMinimum, 60, XmNpaneMaximum, 10000, NULL);
Focus(view);
}
void
_McdeTabsAdopt(Widget view)
{
Widget page = XtParent(view);
Widget term = XtNameToWidget(view, "*dtTerm");
Widget views[16];
int nviews = Views(page, views, 16);
/* a split: the new view takes half the page, not more window -- its
* pane is held at that height until the paned window has laid it out
* (SplitDone); a pane is never squeezed below 60 pixels */
if (nviews > 1) {
Dimension h = 0;
XtVaGetValues(page, XmNheight, &h, NULL);
h = h / (Dimension) nviews > 60 ? h / (Dimension) nviews : 60;
XtVaSetValues(view, XmNpaneMinimum, h, XmNpaneMaximum, h, NULL);
XtAppAddTimeOut(XtWidgetToApplicationContext(view), 0, SplitDone,
(XtPointer) view);
} else
XtVaSetValues(view, XmNpaneMinimum, 60, NULL);
/* the first view of a page names its tab */
if (nviews <= 1) {
XmString s = XmStringCreateLocalized("Terminal");
XtVaSetValues(page, XmNtabLabelString, s, NULL);
XmStringFree(s);
}
XtInsertEventHandler(term != NULL ? term : view, KeyPressMask, False,
Keys, (XtPointer) view, XtListHead);
XtAddEventHandler(term != NULL ? term : view, FocusChangeMask, False,
TermFocus, (XtPointer) view);
XmTabStackSelectTab(page, True);
Focus(view);
}
Boolean
_McdeTabsClose(Widget view)
{
Widget page = XtParent(view);
Widget container = page != NULL ? XtParent(page) : NULL;
Widget tabs[64], views[16];
int count, at, nviews, i;
if (container == NULL || !XmIsTabStack(container))
return False;
/* one of several terminals of a page: only it goes */
nviews = Views(page, views, 16);
if (nviews > 1) {
Widget next = NULL;
Dimension hv = 0, hn = 0, sp = 0;
/* the neighbour above, else below, takes the space: it is held
* at both heights until the paned window has laid it out
* (SplitDone), or the paned window gives the space back and
* the window shrinks (measured) */
for (i = 0; i < nviews; i++)
if (views[i] == view) {
next = views[i > 0 ? i - 1 : i + 1];
break;
}
if (next == NULL)
next = views[0] != view ? views[0] : views[1];
XtVaGetValues(view, XmNheight, &hv, NULL);
XtVaGetValues(next, XmNheight, &hn, NULL);
XtVaGetValues(page, XmNspacing, &sp, NULL);
hn += hv + sp;
XtVaSetValues(next, XmNpaneMinimum, hn, XmNpaneMaximum, hn,
NULL);
XtDestroyWidget(view);
XtAppAddTimeOut(XtWidgetToApplicationContext(next), 0,
SplitDone, (XtPointer) next);
return True;
}
at = TabIndex(container, page, tabs, 64, &count);
if (at < 0 || count < 2)
return False;
/* the neighbour comes to the front, then the tab goes */
XmTabStackSelectTab(tabs[at + 1 < count ? at + 1 : at - 1], True);
XtDestroyWidget(page);
return True;
}