/* * Tabs.c -- MCDE: tabs in a dtterm window * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * 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 #include #include /* core.being_destroyed: a tab closing now is no tab */ #include #include #include #include #include /* Command+N: the struct the clone callback takes */ #include #include #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]); } } /* One tab, no tab bar. XmTabStack places its tab box whether it is * managed or not (Motif 2.3.8, TabStack.c: PickSizes, Layout), so the * stack sits in a form, and with a single tab its top is moved up by the * tab box's height: the bar lies above the window and X clips it away. */ static void TabBar(Widget container) { Widget box = XtNameToWidget(container, "tabBox"); Dimension h = 0; int up; if (box == NULL || container->core.being_destroyed) return; XtVaGetValues(box, XmNheight, &h, NULL); up = _McdeTabsCount(container) <= 1 ? -(int) h : 0; XtVaSetValues(container, XmNtopOffset, up, NULL); } static void TabBarLater(XtPointer data, XtIntervalId *id) { (void) id; TabBar((Widget) data); } Widget _McdeTabsContainer(Widget shell) { Widget c = XtNameToWidget(shell, "mcdeTabsFrame.mcdeTabs"), frame; Arg args[8]; int n = 0; if (c != NULL) return c; frame = XtVaCreateManagedWidget("mcdeTabsFrame", xmFormWidgetClass, shell, XmNmarginWidth, 0, XmNmarginHeight, 0, XmNshadowThickness, 0, NULL); XtSetArg(args[n], XmNtabSide, XmTABS_ON_TOP); n++; XtSetArg(args[n], XmNtabStyle, XmTABS_BEVELED); n++; XtSetArg(args[n], XmNshadowThickness, 1); n++; XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++; c = XmCreateTabStack(frame, "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; /* MCDE: the Command key (Mod4, the key left of the space bar on every * keyboard: dtkeys) -- Command+C/V copy and paste, T a new tab, N a * new window (as Window > New), W closes, Left/Right go from tab to * tab; Ctrl+C stays the interrupt. * Any other Command key does not reach the program in the terminal. */ if ((ev->xkey.state & Mod4Mask) && !(ev->xkey.state & ControlMask)) { Widget term = XtNameToWidget(view, "*dtTerm"); ks = XLookupKeysym(&ev->xkey, 0); if (IsModifierKey(ks)) return; if ((ks == XK_c || ks == XK_C) && term != NULL) XtCallActionProc(term, "copy-clipboard", ev, NULL, 0); else if ((ks == XK_v || ks == XK_V) && term != NULL) XtCallActionProc(term, "paste-clipboard", ev, NULL, 0); else if ((ks == XK_t || ks == XK_T) && newProc != NULL) (*newProc)(view, NULL); else if (ks == XK_n || ks == XK_N) { /* a new window: the menu's New (a clone of this * terminal, its callback copies the resources) */ XmPushButtonCallbackStruct cbs; cbs.event = ev; DtTermViewCloneCallback(view, NULL, &cbs); } else if (ks == XK_w || ks == XK_W) XtCallCallbacks(view, DtNsubprocessTerminationCallback, NULL); else if (ks == XK_Right) Select(container, page, 1); else if (ks == XK_Left) Select(container, page, -1); *cont = False; return; } 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); /* the tab bar shows from the second tab on */ XtAppAddTimeOut(XtWidgetToApplicationContext(view), 0, TabBarLater, (XtPointer) XtParent(page)); } 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); /* back to one tab: the bar goes, once the page is really gone */ XtAppAddTimeOut(XtWidgetToApplicationContext(container), 0, TabBarLater, (XtPointer) container); return True; }