Files
mcde/programs/dtwm/WmMcdePanel.c
T
lotherk 4663ea653c MCDE front panel: icons in and out of the drawers (t0258, part 2)
The drawers were half done: the drop wrote ~/.dt/panel-drawers and the dock
icon went away, but the icon never showed, because it was added to an already
realized form and sat below the drawer's height.

- WmMcdePanel.c: the drawer's icons (SUBPANEL/TAB/CLASS/TAB/COMMAND in
  ~/.dt/panel-drawers) are widgets in the subpanel's form; the last one takes
  the form's bottom attachment over from the previous last child, as CDE's
  own last control does in SubpanelControlCreate -- that is what makes the
  form, and the drawer, as high as its contents.  A drag from the dock onto a
  drawer's main-row control stores the class and command and leaves the dock;
  a drag from the drawer onto the dock keeps it and removes it from the file.
  A program in a drawer is not shown again in the dock.
- UI.c: SubpanelCreate calls McdePanelFillDrawer at its end, after the CDE
  controls and before the form is first laid out.  (An earlier hook in
  AddSubpanel never ran for the standard drawers: those are made by
  SubpanelCreate from MainControlCreate.)
- WmMcdePanel.h: the declaration.

Measured in Xvfb (1280x800, fresh HOME, ttsession + dtwm + dtbar):
- with ~/.dt/panel-drawers holding "FilesSubpanel XClock 'xclock' '-digital'",
  the opened Files drawer shows the clock icon under Downloads; the log says
  "drawer 'FilesSubpanel' to 311 pixel high for 1 icons", form and shell 311
  (before the change: form 244, icon at y=244 outside);
- dragging that icon from the drawer onto the dock: panel-drawers empty,
  panel-apps "XClock 'xclock' '-digital'" (screenshot: the icon in the dock);
- dragging the running xeyes icon onto the Files control: panel-drawers
  "FilesSubpanel XEyes 'xeyes'" and the dock back to 0 buttons.

make mcde-linux MCDE_DIRS="install programs/dtwm" exit=0.  An icon added
during a running session shows in its drawer after the next session: the
posted drawer keeps the height it was created with.
2026-11-16 09:30:18 +00:00

1325 lines
37 KiB
C

/*
* WmMcdePanel.c -- MCDE: the programs left of the trash in the front panel
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The front panel stays CDE's: its controls, subpanels and workspace
* switch. Left of the trash it gets one more part, made here:
*
* - the programs kept in the panel (~/.dt/panel-apps, one a line:
* CLASS <tab> COMMAND); a click starts one, or brings it forward when
* it runs; the right button offers "Remove from Panel";
* - then the programs running that have no place in the panel -- not
* kept, and no main panel control names them (CLIENT_NAME, or a
* PUSH_ACTION that begins with the program's class); a click brings
* the window forward (on its workspace); the right button offers
* "Keep in Panel", and so does dragging it to the left with the button
* held.
*
* Keeping a program needs the command that starts it: the client's
* WM_COMMAND, else the command line of its process (_NET_WM_PID,
* mcde_sys_process_command), else its class in lower case.
*
* The part is a form in the main panel's control row, between the control
* before the trash and the trash; above it, in the row of the subpanel
* arrows, a blank of the same width keeps every arrow over its control
* (UI.c, ArrowCreate). An unmanaged form passes its attachment on, so the
* trash moves back when the part is empty. The buttons are CDE's own
* panel controls (_DtCreateControl) with the class's icon. The part is
* redrawn after dtwm manages or unmanages a window (WmManage.c).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <sys/types.h>
#include "WmGlobal.h"
#include "WmFunction.h"
#include "WmWrkspace.h"
#include "DataBaseLoad.h"
#include "UI.h"
#include "WmMcdePanel.h"
#include "WmMcdeDisplays.h"
#include <X11/Xatom.h>
#include <Dt/WsmP.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Dt/DtStrDefs.h>
#include <Dt/Icon.h>
#include <Dt/Control.h>
#include <Dt/IconFile.h>
#include <Mcde/McdeSys.h>
#define MAX_APPS 48
typedef struct {
char cls[64]; /* WM_CLASS class */
char cmd[1024]; /* what starts it */
} Kept;
static Kept kept[MAX_APPS];
static int nkept;
static ClientData *WindowOf(const char *cls);
static void Raise(ClientData *pCD);
static void KeepClass(const char *cls, const char *cmd);
static Widget area, spacer, menu, menuItem, separator, prevWidget;
static ControlData *trash, *anchor;
static XtIntervalId pending;
/* the buttons now: class, kept (an index into kept[]) or running only */
static struct {
Widget w;
char cls[64];
int keptIndex; /* -1: running, not kept */
} buttons[MAX_APPS];
static int nbuttons, menuFor = -1, pressX;
/* ---- the kept programs -------------------------------------------------- */
static void
KeptFile(char *buf, size_t len)
{
const char *home = getenv("HOME");
snprintf(buf, len, "%s/.dt/panel-apps", home != NULL ? home : ".");
}
static void
ReadKept(void)
{
char path[1100], line[1200], *tab;
FILE *fp;
nkept = 0;
KeptFile(path, sizeof path);
if ((fp = fopen(path, "r")) == NULL)
return;
while (nkept < MAX_APPS && fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '#' || (tab = strchr(line, '\t')) == NULL)
continue;
*tab = '\0';
snprintf(kept[nkept].cls, sizeof kept[nkept].cls, "%s", line);
snprintf(kept[nkept].cmd, sizeof kept[nkept].cmd, "%s", tab + 1);
nkept++;
}
fclose(fp);
}
static void
WriteKept(void)
{
char path[1100], tmp[1200];
FILE *fp;
int i;
KeptFile(path, sizeof path);
snprintf(tmp, sizeof tmp, "%s.new", path);
if ((fp = fopen(tmp, "w")) == NULL)
return;
fprintf(fp, "# the programs kept in the front panel: CLASS <tab> "
"COMMAND (MCDE dtwm)\n");
for (i = 0; i < nkept; i++)
fprintf(fp, "%s\t%s\n", kept[i].cls, kept[i].cmd);
if (fclose(fp) == 0)
(void) rename(tmp, path);
}
static int
KeptIndex(const char *cls)
{
int i;
for (i = 0; i < nkept; i++)
if (strcasecmp(kept[i].cls, cls) == 0)
return i;
return -1;
}
/* ---- the drawers (subpanels) -------------------------------------------- */
/* A program can be dragged from the dock into a drawer and back. The
* drawer's form is CDE's (UI.c, SubpanelCreate); the icons stored for it in
* ~/.dt/panel-drawers are added before the form is realized (UI.c,
* AddSubpanel calls McdePanelFillDrawer), so the drawer is sized for them.
* An icon dropped on a drawer's main-row control goes in at once; it shows
* after the next session if the drawer was already full (it does not grow
* once posted). */
typedef struct {
char sub[64]; /* SUBPANEL name, e.g. FilesSubpanel */
char cls[64];
char cmd[1024];
Widget w;
} DrawerIcon;
static DrawerIcon drawerIcons[MAX_APPS];
static int ndrawerIcons, drawersRead;
static void
DrawerFile(char *buf, size_t len)
{
const char *home = getenv("HOME");
snprintf(buf, len, "%s/.dt/panel-drawers", home != NULL ? home : ".");
}
static void
ReadDrawers(void)
{
char path[1100], line[1400], *t1, *t2;
FILE *fp;
ndrawerIcons = 0;
drawersRead = 1;
DrawerFile(path, sizeof path);
if ((fp = fopen(path, "r")) == NULL)
return;
while (ndrawerIcons < MAX_APPS && fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '#' || (t1 = strchr(line, '\t')) == NULL)
continue;
*t1++ = '\0';
if ((t2 = strchr(t1, '\t')) == NULL)
continue;
*t2++ = '\0';
snprintf(drawerIcons[ndrawerIcons].sub,
sizeof drawerIcons[0].sub, "%s", line);
snprintf(drawerIcons[ndrawerIcons].cls,
sizeof drawerIcons[0].cls, "%s", t1);
snprintf(drawerIcons[ndrawerIcons].cmd,
sizeof drawerIcons[0].cmd, "%s", t2);
ndrawerIcons++;
}
fclose(fp);
}
static void
WriteDrawers(void)
{
char path[1100], tmp[1200];
FILE *fp;
int i;
DrawerFile(path, sizeof path);
snprintf(tmp, sizeof tmp, "%s.new", path);
if ((fp = fopen(tmp, "w")) == NULL)
return;
fprintf(fp, "# programs in the front panel's drawers: SUBPANEL <tab> "
"CLASS <tab> COMMAND (MCDE dtwm)\n");
for (i = 0; i < ndrawerIcons; i++)
fprintf(fp, "%s\t%s\t%s\n", drawerIcons[i].sub,
drawerIcons[i].cls, drawerIcons[i].cmd);
if (fclose(fp) == 0)
(void) rename(tmp, path);
}
static int
InDrawer(const char *cls)
{
int i;
for (i = 0; i < ndrawerIcons; i++)
if (strcasecmp(drawerIcons[i].cls, cls) == 0)
return i;
return -1;
}
static SubpanelData *
SubpanelByName(const char *name)
{
int i, j;
if (name == NULL)
return NULL;
for (i = 0; i < panel.box_data_count; i++)
for (j = 0; j < panel.box_data[i]->control_data_count;
j++) {
ControlData *c = panel.box_data[i]->control_data[j];
const char *n;
if (c->subpanel_data == NULL)
continue;
n = (const char *) c->subpanel_data->
element_values[SUBPANEL_NAME].parsed_value;
if (n != NULL && strcmp(n, name) == 0)
return c->subpanel_data;
}
return NULL;
}
/* the drawer whose main-row icon is at a root position, or NULL */
static SubpanelData *
SubpanelAtRoot(int xr, int yr)
{
int i, j;
for (i = 0; i < panel.box_data_count; i++)
for (j = 0; j < panel.box_data[i]->control_data_count;
j++) {
ControlData *c = panel.box_data[i]->control_data[j];
Position x = 0, y = 0;
Dimension w = 0, h = 0;
if (c->subpanel_data == NULL || c->icon == NULL ||
!XtIsManaged(c->icon))
continue;
XtTranslateCoords(c->icon, 0, 0, &x, &y);
XtVaGetValues(c->icon, XmNwidth, &w, XmNheight, &h,
NULL);
if (xr >= x && xr < x + (int) w && yr >= y &&
yr < y + (int) h)
return c->subpanel_data;
}
return NULL;
}
static void
DrawerClicked(Widget w, XtPointer data, XtCallbackProc call)
{
int i = (int) (intptr_t) data;
ClientData *pCD;
(void) w;
(void) call;
if (i < 0 || i >= ndrawerIcons)
return;
if ((pCD = WindowOf(drawerIcons[i].cls)) != NULL)
Raise(pCD);
else
(void) F_Exec(drawerIcons[i].cmd, NULL, NULL);
}
/* drag an icon out of a drawer onto the dock: it is kept there. The handler
* sits on the drawer's form: _DtCreateControl makes a gadget, and a gadget
* has no window for an event handler (a segfault, measured 2026-11-16). */
static int drawerPressed = -1;
static void
DrawerFormPressed(Widget form, XtPointer data, XEvent *ev, Boolean *cont)
{
Position ax = 0, ay = 0;
Dimension aw = 0, ah = 0;
Widget g;
int j;
(void) data;
(void) cont;
g = XmObjectAtPoint(form, (Position) ev->xbutton.x,
(Position) ev->xbutton.y);
if (ev->type == ButtonPress && ev->xbutton.button == Button1) {
drawerPressed = -1;
for (j = 0; j < ndrawerIcons; j++)
if (drawerIcons[j].w == g) {
drawerPressed = j;
break;
}
return;
}
if (ev->type != ButtonRelease || ev->xbutton.button != Button1 ||
drawerPressed < 0 || drawerPressed >= ndrawerIcons)
return;
j = drawerPressed;
drawerPressed = -1;
if (area == NULL || !XtIsManaged(area))
return;
XtTranslateCoords(area, 0, 0, &ax, &ay);
XtVaGetValues(area, XmNwidth, &aw, XmNheight, &ah, NULL);
if (ev->xbutton.x_root < ax || ev->xbutton.x_root >= ax + (int) aw ||
ev->xbutton.y_root < ay || ev->xbutton.y_root >= ay + (int) ah)
return;
{
char cls[64], cmd[1024];
snprintf(cls, sizeof cls, "%s", drawerIcons[j].cls);
snprintf(cmd, sizeof cmd, "%s", drawerIcons[j].cmd);
if (drawerIcons[j].w != NULL)
XtDestroyWidget(drawerIcons[j].w);
memmove(drawerIcons + j, drawerIcons + j + 1,
(size_t) (ndrawerIcons - j - 1) * sizeof drawerIcons[0]);
ndrawerIcons--;
WriteDrawers();
KeepClass(cls, cmd);
}
}
static void
AddDrawerWidget(int idx)
{
SubpanelData *sp = SubpanelByName(drawerIcons[idx].sub);
Widget form, *kids = NULL, below = NULL;
Cardinal n = 0, k;
Arg al[20];
int ac = 0;
String icon;
char lower[64];
size_t i;
XmString label;
if (sp == NULL || sp->form == NULL)
return;
form = sp->form;
XtVaGetValues(form, XmNchildren, &kids, XmNnumChildren, &n, NULL);
for (k = n; k > 0; k--)
if (XtIsManaged(kids[k - 1])) {
below = kids[k - 1];
break;
}
icon = _DtGetIconFileName(XtScreen(form), (String) drawerIcons[idx].cls,
NULL, NULL, panel.sub_icon_size);
if (icon == NULL) {
for (i = 0; drawerIcons[idx].cls[i] != '\0' &&
i + 1 < sizeof lower; i++)
lower[i] = (char) tolower((unsigned char)
drawerIcons[idx].cls[i]);
lower[i] = '\0';
icon = _DtGetIconFileName(XtScreen(form), lower, NULL, NULL,
panel.sub_icon_size);
}
if (icon == NULL)
icon = GetIconName("Dtactn", panel.sub_icon_size);
XtSetArg(al[ac], XmNbackground, panel.primary_pixel_set->bg); ac++;
XtSetArg(al[ac], XmNforeground, panel.primary_pixel_set->fg); ac++;
XtSetArg(al[ac], XmNarmColor, panel.primary_pixel_set->sc); ac++;
XtSetArg(al[ac], XmNpixmapBackground, panel.primary_pixel_set->bg);
ac++;
XtSetArg(al[ac], XmNpixmapForeground, panel.primary_pixel_set->fg);
ac++;
XtSetArg(al[ac], XmNpixmapPosition, XmPIXMAP_TOP); ac++;
XtSetArg(al[ac], XmNimageName, icon); ac++;
label = XmStringCreateLocalized(drawerIcons[idx].cls);
XtSetArg(al[ac], XmNstring, label); ac++;
XtSetArg(al[ac], XmNfontList, panel.font_list); ac++;
XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNleftOffset, 5); ac++;
XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNrightOffset, 5); ac++;
XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
XtSetArg(al[ac], XmNtopWidget, below); ac++;
XtSetArg(al[ac], XmNtopOffset, 5); ac++;
/* the last icon holds the form's bottom, as CDE's own last control
* does (UI.c, SubpanelControlCreate): that is what makes the form --
* and with it the drawer -- as high as its contents */
if (below != NULL)
XtVaSetValues(below, XmNbottomAttachment, XmATTACH_NONE,
NULL);
XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNbottomOffset, 5); ac++;
drawerIcons[idx].w = _DtCreateControl(form, "mcdeDrawerApp", al, ac);
XmStringFree(label);
XtFree(icon);
XtManageChild(drawerIcons[idx].w);
XtAddCallback(drawerIcons[idx].w, XmNcallback, DrawerClicked,
(XtPointer) (intptr_t) idx);
{
static Widget forms[8];
static int nforms;
int f, known = 0;
for (f = 0; f < nforms; f++)
if (forms[f] == form)
known = 1;
if (!known && nforms < 8) {
forms[nforms++] = form;
XtAddEventHandler(form, ButtonPressMask |
ButtonReleaseMask, False, DrawerFormPressed,
NULL);
}
}
if (getenv("MCDE_PANEL_DEBUG") != NULL)
fprintf(stderr, "dtwm panel: drawer icon %s in %s (widget %p, "
"below %p, realized %d)\n", drawerIcons[idx].cls,
drawerIcons[idx].sub, (void *) drawerIcons[idx].w,
(void *) below, XtIsRealized(form));
}
/* UI.c, AddSubpanel: the stored icons join the layout of a drawer before its
* form is realized, so the drawer is created with the room for them */
void
McdePanelFillDrawer(void *subpanel)
{
SubpanelData *sp = (SubpanelData *) subpanel;
const char *name;
int i;
if (sp == NULL)
return;
if (!drawersRead)
ReadDrawers();
name = (const char *) sp->element_values[SUBPANEL_NAME].parsed_value;
if (name == NULL)
return;
{
int last = -1;
for (i = 0; i < ndrawerIcons; i++)
if (strcmp(drawerIcons[i].sub, name) == 0 &&
drawerIcons[i].w == NULL) {
AddDrawerWidget(i);
if (drawerIcons[i].w != NULL)
last = i;
}
/* the form is realized with its children; without the height of
* the last added icon it stays at the size CDE gave it and the
* icon sits outside (measured 2026-11-16) */
if (last >= 0) {
Dimension y = 0, h = 0, want;
XtVaGetValues(drawerIcons[last].w, XmNy, &y,
XmNheight, &h, NULL);
want = (Dimension) (y + h + 8);
if (getenv("MCDE_PANEL_DEBUG") != NULL)
fprintf(stderr, "dtwm panel: drawer '%s' to "
"%d pixel high for %d icons\n", name,
(int) want, ndrawerIcons);
XtVaSetValues(sp->form, XmNheight, want, NULL);
if (sp->shell != NULL)
XtVaSetValues(sp->shell, XmNheight, want, NULL);
}
}
}
static void
AddToDrawer(SubpanelData *sp, const char *cls, const char *cmd)
{
const char *name;
int i;
if (sp == NULL || InDrawer(cls) >= 0 || ndrawerIcons >= MAX_APPS)
return;
name = (const char *) sp->element_values[SUBPANEL_NAME].parsed_value;
if (name == NULL)
return;
i = ndrawerIcons++;
snprintf(drawerIcons[i].sub, sizeof drawerIcons[0].sub, "%s", name);
snprintf(drawerIcons[i].cls, sizeof drawerIcons[0].cls, "%s", cls);
snprintf(drawerIcons[i].cmd, sizeof drawerIcons[0].cmd, "%s", cmd);
drawerIcons[i].w = NULL;
AddDrawerWidget(i);
WriteDrawers();
if (KeptIndex(cls) >= 0) {
int k = KeptIndex(cls);
memmove(kept + k, kept + k + 1,
(size_t) (nkept - k - 1) * sizeof kept[0]);
nkept--;
WriteKept();
}
McdePanelChanged();
}
/* ---- the windows -------------------------------------------------------- */
/* a program's window of its own: not a transient, not dtwm's panels */
static Boolean
IsProgram(ClientData *pCD)
{
return pCD != NULL && pCD->clientClass != NULL &&
pCD->transientLeader == NULL &&
!(pCD->dtwmBehaviors & (DtWM_BEHAVIOR_PANEL |
DtWM_BEHAVIOR_SUBPANEL)) &&
pCD->clientState != WITHDRAWN_STATE &&
strcasecmp(pCD->clientClass, "Dtwm") != 0;
}
/* the first window of class CLS, or NULL */
static ClientData *
WindowOf(const char *cls)
{
WmScreenData *pSD = ACTIVE_PSD;
ClientListEntry *e;
for (e = pSD->clientList; e != NULL; e = e->nextSibling)
if (e->pCD != NULL && e == &e->pCD->clientEntry &&
IsProgram(e->pCD) &&
strcasecmp(e->pCD->clientClass, cls) == 0)
return e->pCD;
return NULL;
}
/* does control C stand for class CLS (its CLIENT_NAME, or a PUSH_ACTION
* beginning with the class) */
static Boolean
Names(ControlData *c, const char *cls)
{
size_t n = strlen(cls);
ElementValue *v = c->element_values;
const char *client = (const char *) v[CONTROL_CLIENT_NAME].parsed_value;
const char *action = (const char *) v[CONTROL_PUSH_ACTION].parsed_value;
return (client != NULL && strcasecmp(client, cls) == 0) ||
(action != NULL && strncasecmp(action, cls, n) == 0);
}
/* does a control of the panel, in the main row or in a drawer, stand for
* class CLS: such a program is in the dock and not shown again on the right */
static Boolean
InPanel(const char *cls)
{
int i, j, k;
for (i = 0; i < panel.box_data_count; i++) {
BoxData *b = panel.box_data[i];
for (j = 0; j < b->control_data_count; j++) {
ControlData *c = b->control_data[j];
SubpanelData *sp = c->subpanel_data;
if (Names(c, cls))
return True;
if (sp != NULL)
for (k = 0; k < sp->control_data_count; k++)
if (Names(sp->control_data[k], cls))
return True;
}
}
return False;
}
/* bring PCD forward, on its workspace */
static void
Raise(ClientData *pCD)
{
WmScreenData *pSD = PSD_FOR_CLIENT(pCD);
if (!ClientInWorkspace(pSD->pActiveWS, pCD) && pCD->numInhabited > 0) {
WmWorkspaceData *pWS = GetWorkspaceData(pSD,
pCD->pWsList[0].wsID);
if (pWS != NULL)
ChangeToWorkspace(pWS);
}
(void) F_Normalize_And_Raise(NULL, pCD, NULL);
}
/* the command that starts PCD's program again */
static void
CommandOf(ClientData *pCD, char *buf, size_t len)
{
char **argv = NULL;
int argc = 0, i;
size_t n = 0;
buf[0] = '\0';
if (XGetCommand(DISPLAY, pCD->client, &argv, &argc) && argc > 0) {
for (i = 0; i < argc && n + 4 < len; i++)
n += (size_t) snprintf(buf + n, len - n, "%s'%s'",
i > 0 ? " " : "", argv[i]);
XFreeStringList(argv);
if (strchr(buf, '\n') == NULL)
return;
}
{
Atom a = XInternAtom(DISPLAY, "_NET_WM_PID", False), type;
int format;
unsigned long items, after;
unsigned char *data = NULL;
if (XGetWindowProperty(DISPLAY, pCD->client, a, 0, 1, False,
XA_CARDINAL, &type, &format, &items, &after, &data) ==
Success && data != NULL && items == 1 &&
mcde_sys_process_command((pid_t) *(long *) data, buf,
len) == MCDE_SYS_OK && strchr(buf, '\n') == NULL) {
XFree(data);
return;
}
if (data != NULL)
XFree(data);
}
/* the class, in lower case: dtterm, xclock */
for (i = 0; pCD->clientClass[i] != '\0' && (size_t) i + 1 < len; i++)
buf[i] = (char) tolower((unsigned char) pCD->clientClass[i]);
buf[i] = '\0';
}
/* ---- the part ----------------------------------------------------------- */
static void Update(void);
static void CenterLater(void);
static void
DebugGeometry(XtPointer data, XtIntervalId *id)
{
Widget p = XtParent(area), t = trash->icon;
(void) data;
(void) id;
{
Position ax = 0, ay = 0;
XtTranslateCoords(area, 0, 0, &ax, &ay);
fprintf(stderr, "dtwm panel: area %dx%d+%d managed %d at %d,%d; "
"parent %dx%d; trash +%d; shell %dx%d\n", XtWidth(area),
XtHeight(area), XtX(area), XtIsManaged(area), (int) ax,
(int) ay, XtWidth(p), XtHeight(p), XtX(t),
XtWidth(panel.shell), XtHeight(panel.shell));
}
}
static void
Later(XtPointer data, XtIntervalId *id)
{
(void) data;
(void) id;
pending = 0;
Update();
}
void
McdePanelChanged(void)
{
if (area == NULL || pending != 0)
return;
pending = XtAppAddTimeOut(wmGD.mwmAppContext, 200, Later, NULL);
}
static void
KeepClass(const char *cls, const char *cmd)
{
if (nkept >= MAX_APPS || KeptIndex(cls) >= 0)
return;
snprintf(kept[nkept].cls, sizeof kept[nkept].cls, "%s", cls);
snprintf(kept[nkept].cmd, sizeof kept[nkept].cmd, "%s", cmd);
nkept++;
WriteKept();
McdePanelChanged();
}
static void
KeepClient(ClientData *pCD)
{
char cmd[1024];
if (pCD == NULL)
return;
CommandOf(pCD, cmd, sizeof cmd);
KeepClass(pCD->clientClass, cmd);
}
static void
Keep(int b)
{
KeepClient(WindowOf(buttons[b].cls));
}
static void
Unkeep(int b)
{
int k = buttons[b].keptIndex;
if (k < 0)
return;
memmove(kept + k, kept + k + 1, (size_t) (nkept - k - 1) * sizeof kept[0]);
nkept--;
WriteKept();
McdePanelChanged();
}
static void
MenuChosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
if (menuFor < 0 || menuFor >= nbuttons)
return;
if (buttons[menuFor].keptIndex >= 0)
Unkeep(menuFor);
else
Keep(menuFor);
}
static void
Clicked(Widget w, XtPointer data, XtPointer call)
{
int b = (int) (intptr_t) data;
ClientData *pCD;
(void) w;
(void) call;
if (b < 0 || b >= nbuttons)
return;
if ((pCD = WindowOf(buttons[b].cls)) != NULL)
Raise(pCD);
else if (buttons[b].keptIndex >= 0)
(void) F_Exec(kept[buttons[b].keptIndex].cmd, NULL, NULL);
}
static void
PostMenu(int b, XButtonEvent *ev)
{
XmString s;
menuFor = b;
s = XmStringCreateLocalized(buttons[b].keptIndex >= 0 ?
"Remove from Panel" : "Keep in Panel");
XtVaSetValues(menuItem, XmNlabelString, s, NULL);
XmStringFree(s);
XmMenuPosition(menu, ev);
XtManageChild(menu);
}
Boolean
McdePanelPost(Widget form, XButtonEvent *ev)
{
Position ax, ay;
Widget g;
int b;
if (area == NULL || !XtIsManaged(area) || XtParent(area) != form)
return False;
ax = XtX(area);
ay = XtY(area);
if (ev->x < ax || ev->y < ay || ev->x >= ax + (int) XtWidth(area) ||
ev->y >= ay + (int) XtHeight(area))
return False;
g = XmObjectAtPoint(area, (Position) (ev->x - ax),
(Position) (ev->y - ay));
for (b = 0; b < nbuttons; b++)
if (buttons[b].w == g) {
PostMenu(b, ev);
return True;
}
return True; /* the part, between buttons: no menu of CDE's */
}
/* the right button: the menu; the left one dragged out to the left: keep.
* The buttons are gadgets (_DtCreateControl): their events come to the
* part, which finds the button under the pointer (XmObjectAtPoint). */
static int pressedButton = -1;
/* the half of the part at a root position: 1 kept (left of the divider),
* 2 running (right), 0 outside; a button under the pointer answers for its
* own half */
static int
ZoneAtRoot(int xr, int yr)
{
Position ax = 0, ay = 0;
Widget g;
int b, x;
if (area == NULL || !XtIsManaged(area))
return 0;
XtTranslateCoords(area, 0, 0, &ax, &ay);
x = xr - ax;
if (x < 0 || x >= (int) XtWidth(area) || yr - ay < 0 ||
yr - ay >= (int) XtHeight(area))
return 0;
g = XmObjectAtPoint(area, (Position) x, (Position) (yr - ay));
for (b = 0; b < nbuttons; b++)
if (buttons[b].w == g)
return buttons[b].keptIndex >= 0 ? 1 : 2;
if (separator != NULL && XtIsManaged(separator)) {
Position sx = 0, sy = 0;
XtTranslateCoords(separator, 0, 0, &sx, &sy);
return xr < sx ? 1 : 2;
}
/* no divider yet: the empty room before the first icon is the dock's
* left half, the room after the last one its right half */
if (nbuttons > 0) {
Position bx = 0, by = 0;
XtTranslateCoords(buttons[0].w, 0, 0, &bx, &by);
if (xr < bx)
return 1;
XtTranslateCoords(buttons[nbuttons - 1].w, 0, 0, &bx, &by);
bx += (Position) XtWidth(buttons[nbuttons - 1].w);
if (xr >= bx)
return 2;
}
return 0;
}
static void
Pressed(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
{
int b;
Widget g;
(void) cont;
(void) data;
if (getenv("MCDE_PANEL_DEBUG"))
fprintf(stderr, "dtwm panel: %s button %u at %d,%d\n",
ev->type == ButtonPress ? "press" : "release",
ev->xbutton.button, ev->xbutton.x, ev->xbutton.y);
if (ev->type == ButtonRelease && ev->xbutton.button == Button1)
b = pressedButton;
else {
g = XmObjectAtPoint(w, (Position) ev->xbutton.x,
(Position) ev->xbutton.y);
for (b = 0; b < nbuttons; b++)
if (buttons[b].w == g)
break;
if (b == nbuttons)
return;
}
if (b < 0 || b >= nbuttons)
return;
if (ev->type == ButtonPress && ev->xbutton.button == Button1)
pressedButton = b;
if (ev->type == ButtonPress && ev->xbutton.button == Button1) {
pressX = ev->xbutton.x_root;
return;
}
if (ev->type == ButtonRelease && ev->xbutton.button == Button1) {
Position ax = 0, ay = 0;
int zone;
XtTranslateCoords(area, 0, 0, &ax, &ay);
zone = ZoneAtRoot(ev->xbutton.x_root, ev->xbutton.y_root);
/* dropped on a drawer's icon in the main row: into the drawer */
if (zone == 0) {
SubpanelData *sp = SubpanelAtRoot(ev->xbutton.x_root,
ev->xbutton.y_root);
if (sp != NULL) {
char cmd[1024];
if (buttons[b].keptIndex >= 0)
snprintf(cmd, sizeof cmd, "%s",
kept[buttons[b].keptIndex].cmd);
else if (WindowOf(buttons[b].cls) != NULL)
CommandOf(WindowOf(buttons[b].cls), cmd,
sizeof cmd);
else
cmd[0] = '\0';
if (cmd[0] != '\0')
AddToDrawer(sp, buttons[b].cls, cmd);
return;
}
}
/* dropped on the kept half (left of the divider): keep */
if (zone == 1 && buttons[b].keptIndex < 0)
Keep(b);
/* dropped on the running half (right of the divider): unkeep */
else if (zone == 2 && buttons[b].keptIndex >= 0)
Unkeep(b);
/* let go left of the part, having moved there: keep */
else if (buttons[b].keptIndex < 0 && ev->xbutton.x_root < ax &&
pressX - ev->xbutton.x_root > 8)
Keep(b);
/* and right of it: unkeep (out of the dock) */
else if (buttons[b].keptIndex >= 0 &&
ev->xbutton.x_root > ax + (int) XtWidth(area) &&
ev->xbutton.x_root - pressX > 8)
Unkeep(b);
return;
}
if (ev->type != ButtonPress || ev->xbutton.button != Button3)
return;
PostMenu(b, &ev->xbutton);
}
static void
AddButton(const char *cls, int keptIndex)
{
Widget w;
Arg al[16];
int ac = 0;
String icon;
char lower[64];
size_t i;
if (nbuttons >= MAX_APPS)
return;
/* the class's icon, as CDE names them (Dtterm, Dtcalc), else in
* lower case, else the icon of an action */
icon = _DtGetIconFileName(XtScreen(area), (String) cls, NULL, NULL,
panel.main_icon_size);
if (icon == NULL) {
for (i = 0; cls[i] != '\0' && i + 1 < sizeof lower; i++)
lower[i] = (char) tolower((unsigned char) cls[i]);
lower[i] = '\0';
icon = _DtGetIconFileName(XtScreen(area), lower, NULL, NULL,
panel.main_icon_size);
}
if (icon == NULL)
icon = GetIconName("Dtactn", panel.main_icon_size);
XtSetArg(al[ac], XmNbackground, panel.primary_pixel_set->bg); ac++;
XtSetArg(al[ac], XmNforeground, panel.primary_pixel_set->fg); ac++;
XtSetArg(al[ac], XmNarmColor, panel.primary_pixel_set->sc); ac++;
XtSetArg(al[ac], XmNpixmapBackground, panel.primary_pixel_set->bg); ac++;
XtSetArg(al[ac], XmNpixmapForeground, panel.primary_pixel_set->fg); ac++;
XtSetArg(al[ac], XmNpixmapPosition, XmPIXMAP_TOP); ac++;
XtSetArg(al[ac], XmNimageName, icon); ac++;
XtSetArg(al[ac], XmNstring, NULL); ac++;
XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
if (prevWidget == NULL) {
XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
} else {
XtSetArg(al[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
XtSetArg(al[ac], XmNleftWidget, prevWidget); ac++;
}
w = _DtCreateControl(area, "mcdeApp", al, ac);
XtFree(icon);
XtManageChild(w);
prevWidget = w;
XtAddCallback(w, XmNcallback, Clicked, (XtPointer) (intptr_t) nbuttons);
buttons[nbuttons].w = w;
snprintf(buttons[nbuttons].cls, sizeof buttons[nbuttons].cls, "%s", cls);
buttons[nbuttons].keptIndex = keptIndex;
nbuttons++;
}
/* the panel grows by what the part did not get: with a session's
* geometry the panel's shell did not grow by itself, and the form squeezed
* the part to a pixel (measured in a dtsession); shrinking likewise */
static Dimension shown;
static void
Grow(XtPointer data, XtIntervalId *id)
{
Dimension want = (Dimension) (intptr_t) data, have = XtWidth(area);
int delta = (int) want - (int) have;
(void) id;
if (delta == 0 || !XtIsManaged(area))
return;
XtVaSetValues(panel.shell, XmNwidth,
(Dimension) ((int) XtWidth(panel.shell) + delta), NULL);
/* the form squeezed the part while there was no room; now there is */
XtVaSetValues(area, XmNwidth, want, NULL);
XtVaSetValues(spacer, XmNwidth, want, NULL);
CenterLater();
}
/*
* The workspace switch stays in the middle of the screen: the panel's left
* part (Files ... Desktop) has a fixed width, the right part (the running
* programs and the trash) grows and shrinks, so the shell is moved after
* every change by as much as the switch's centre is off the screen's.
* Konrad: "die 4 workspace knoepfe im dock ... sind immer center auf dem
* screen".
*/
static int centerTries, realizeTries;
/* the window manager puts the shell this far from where it was asked to
* (its frame); learnt from the first move */
static int centerAsked = -1, centerOffset;
static void
Center(XtPointer data, XtIntervalId *id)
{
Widget sw = NULL;
Position sx, sy;
int i, x, want, screenW, shellW, mx, my, mw, mh;
(void) data;
(void) id;
for (i = 0; i < panel.box_data_count && sw == NULL; i++)
if (panel.box_data[i]->switch_form != NULL)
sw = panel.box_data[i]->switch_form;
if (sw == NULL || panel.shell == NULL)
return;
/* at the start the panel is made before it is shown */
if (!XtIsRealized(panel.shell) || !XtIsRealized(sw)) {
if (++realizeTries < 40)
XtAppAddTimeOut(XtWidgetToApplicationContext(panel.shell),
250, Center, NULL);
return;
}
XtTranslateCoords(sw, (Position) (XtWidth(sw) / 2), 0, &sx, &sy);
screenW = WidthOfScreen(XtScreen(panel.shell));
shellW = (int) XtWidth(panel.shell);
/* the middle of the main display (WmMcdeDisplays.c), else of the
* screen */
if (!McdeMainMonitor(&mx, &my, &mw, &mh)) {
mx = 0;
mw = screenW;
}
x = (int) XtX(panel.shell) + mx + mw / 2 - (int) sx;
/* never off the display: then the switch is as near the middle as
* fits */
if (x + shellW > mx + mw)
x = mx + mw - shellW;
if (x < mx)
x = mx;
if (centerAsked >= 0 && (int) XtX(panel.shell) != centerAsked + centerOffset)
centerOffset = (int) XtX(panel.shell) - centerAsked;
want = x;
if (getenv("MCDE_PANEL_DEBUG"))
fprintf(stderr, "dtwm panel: switch centre %d of %d, shell %d -> %d\n",
(int) sx, screenW, (int) XtX(panel.shell), want);
if (want != (int) XtX(panel.shell)) {
centerAsked = want - centerOffset;
XtVaSetValues(panel.shell, XmNx, (Position) centerAsked, NULL);
/* once more, to learn the offset if the shell is not there */
if (++centerTries < 3)
XtAppAddTimeOut(XtWidgetToApplicationContext(panel.shell),
300, Center, NULL);
} else
centerTries = 0;
}
/* after the panel's geometry settled (the shell's resize is a round trip
* to the window manager, which is this program) */
static void
CenterLater(void)
{
if (panel.shell != NULL)
XtAppAddTimeOut(XtWidgetToApplicationContext(panel.shell), 150,
Center, NULL);
}
/* the monitors changed (WmMcdeDisplays.c): the panel to the main display */
void
McdePanelMonitorsChanged(void)
{
centerTries = 0;
CenterLater();
}
static void
Update(void)
{
WmScreenData *pSD = ACTIVE_PSD;
ClientListEntry *e;
Dimension width = 0;
int i, j;
if (area == NULL)
return;
for (i = 0; i < nbuttons; i++)
XtDestroyWidget(buttons[i].w);
nbuttons = 0;
if (separator != NULL) {
XtDestroyWidget(separator);
separator = NULL;
}
/* the programs of the dock come first (left), then the divider, then
* the running ones that are not in the dock -- macOS's two halves in
* CDE's look (Konrad, 2026-11-15) */
{
char run[MAX_APPS][64];
int nrun = 0;
for (e = pSD->clientList; e != NULL; e = e->nextSibling) {
ClientData *pCD = e->pCD;
if (pCD == NULL || e != &pCD->clientEntry ||
!IsProgram(pCD) ||
KeptIndex(pCD->clientClass) >= 0 ||
InDrawer(pCD->clientClass) >= 0 ||
InPanel(pCD->clientClass))
continue;
for (j = 0; j < nrun; j++)
if (strcasecmp(run[j], pCD->clientClass) == 0)
break;
if (j == nrun && nrun < MAX_APPS)
snprintf(run[nrun++], sizeof run[0], "%s",
pCD->clientClass);
}
prevWidget = NULL;
for (i = 0; i < nkept; i++)
AddButton(kept[i].cls, i);
if (nkept > 0 && nrun > 0) {
Arg al[8];
int ac = 0;
XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNtopOffset, 2); ac++;
XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM);
ac++;
XtSetArg(al[ac], XmNbottomOffset, 2); ac++;
XtSetArg(al[ac], XmNleftAttachment, XmATTACH_WIDGET);
ac++;
XtSetArg(al[ac], XmNleftWidget, prevWidget); ac++;
XtSetArg(al[ac], XmNleftOffset, 4); ac++;
XtSetArg(al[ac], XmNorientation, XmVERTICAL); ac++;
separator = XmCreateSeparator(area,
"mcdeDockSeparator", al, ac);
XtManageChild(separator);
prevWidget = separator;
}
for (i = 0; i < nrun; i++)
AddButton(run[i], -1);
}
if (getenv("MCDE_PANEL_DEBUG"))
fprintf(stderr, "dtwm panel: %d buttons (%d kept)\n", nbuttons,
nkept);
if (nbuttons == 0) {
if (XtIsManaged(area) && shown > 1)
XtVaSetValues(panel.shell, XmNwidth, (Dimension)
((int) XtWidth(panel.shell) - (int) shown), NULL);
shown = 0;
XtUnmanageChild(area);
XtUnmanageChild(spacer);
CenterLater();
return;
}
for (i = 0; i < nbuttons; i++) {
XtWidgetGeometry pref;
if (XtQueryGeometry(buttons[i].w, NULL, &pref) != XtGeometryNo &&
(pref.request_mode & CWWidth))
width += pref.width;
else
width += XtWidth(buttons[i].w);
}
if (separator != NULL && XtIsManaged(separator)) {
XtWidgetGeometry pref;
width += 4; /* its left offset */
if (XtQueryGeometry(separator, NULL, &pref) != XtGeometryNo &&
(pref.request_mode & CWWidth))
width += pref.width;
else
width += XtWidth(separator);
}
/* the panel gives back what the part had: Grow asks only for the
* difference */
if (XtIsManaged(area) && shown > 1 && shown != width)
XtVaSetValues(panel.shell, XmNwidth, (Dimension)
((int) XtWidth(panel.shell) + (int) width - (int) shown),
NULL);
shown = width;
XtVaSetValues(area, XmNwidth, width, NULL);
XtVaSetValues(spacer, XmNwidth, width, NULL);
XtManageChild(area);
XtManageChild(spacer);
XtAppAddTimeOut(wmGD.mwmAppContext, 0, Grow, (XtPointer) (intptr_t) width);
CenterLater();
if (getenv("MCDE_PANEL_DEBUG"))
XtAppAddTimeOut(wmGD.mwmAppContext, 500, DebugGeometry, NULL);
}
/* after the main panel's controls are made (UI.c, MainControlCreate) */
void
McdePanelCreate(void)
{
int i, j;
unsigned char att = XmATTACH_FORM;
Widget left = NULL;
Arg al[12];
int ac;
trash = NULL;
anchor = NULL;
for (i = 0; i < panel.box_data_count; i++)
for (j = 0; j < panel.box_data[i]->control_data_count; j++) {
ControlData *c = panel.box_data[i]->control_data[j];
const char *name =
(const char *) c->element_values[CONTROL_NAME].parsed_value;
if (name == NULL || c->icon == NULL || c->arrow == NULL)
continue;
if (strcmp(name, "Trash") == 0)
trash = c;
/* the part goes left of the printer, else of the trash:
* the machine's programs stay together on the right and
* the printer is the drop target at the end */
if (strcmp(name, "Printer") == 0)
anchor = c;
}
if (anchor == NULL)
anchor = trash;
if (getenv("MCDE_PANEL_DEBUG"))
fprintf(stderr, "dtwm panel: create, trash %s, anchor %s\n",
trash != NULL ? "found" : "not found",
anchor != NULL ? "found" : "not found");
if (trash == NULL || anchor == NULL)
return;
ReadKept();
if (!drawersRead)
ReadDrawers();
/* the part, in the control row before the trash */
XtVaGetValues(anchor->icon, XmNleftAttachment, &att, XmNleftWidget,
&left, NULL);
/* the background of the row it sits in: a colour, or a pattern
* (UI.c gives the control forms a pixmap in some colour modes) */
{
Pixel bg;
Pixmap pm = XmUNSPECIFIED_PIXMAP;
XtVaGetValues(XtParent(anchor->icon), XmNbackground, &bg,
XmNbackgroundPixmap, &pm, NULL);
ac = 0;
XtSetArg(al[ac], XmNbackground, bg); ac++;
if (pm != XmUNSPECIFIED_PIXMAP && pm != None) {
XtSetArg(al[ac], XmNbackgroundPixmap, pm); ac++;
}
}
XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNtopOffset, 1); ac++;
XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNbottomOffset, 1); ac++;
XtSetArg(al[ac], XmNleftAttachment, att); ac++;
if (att == XmATTACH_WIDGET) {
XtSetArg(al[ac], XmNleftWidget, left); ac++;
}
XtSetArg(al[ac], XmNresizePolicy, XmRESIZE_NONE); ac++;
XtSetArg(al[ac], XmNwidth, 1); ac++;
area = XmCreateForm(XtParent(anchor->icon), "mcdeRunning", al, ac);
XtVaSetValues(anchor->icon, XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, area, NULL);
/* its blank in the arrow row, before the trash's arrow */
att = XmATTACH_FORM;
left = NULL;
XtVaGetValues(anchor->arrow, XmNleftAttachment, &att, XmNleftWidget,
&left, NULL);
{
Pixel bg;
Pixmap pm = XmUNSPECIFIED_PIXMAP;
XtVaGetValues(anchor->arrow, XmNbackground, &bg, NULL);
XtVaGetValues(XtParent(anchor->arrow), XmNbackgroundPixmap, &pm,
NULL);
ac = 0;
XtSetArg(al[ac], XmNbackground, bg); ac++;
if (pm != XmUNSPECIFIED_PIXMAP && pm != None) {
XtSetArg(al[ac], XmNbackgroundPixmap, pm); ac++;
}
}
XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
XtSetArg(al[ac], XmNleftAttachment, att); ac++;
if (att == XmATTACH_WIDGET) {
XtSetArg(al[ac], XmNleftWidget, left); ac++;
}
XtSetArg(al[ac], XmNrecomputeSize, False); ac++;
XtSetArg(al[ac], XmNwidth, 1); ac++;
{
XmString empty = XmStringCreateLocalized("");
XtSetArg(al[ac], XmNlabelString, empty); ac++;
spacer = XmCreateLabel(XtParent(anchor->arrow),
"mcdeRunningArrows", al, ac);
XmStringFree(empty);
}
XtVaSetValues(anchor->arrow, XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, spacer, NULL);
XtAddEventHandler(area, ButtonPressMask | ButtonReleaseMask, False,
Pressed, NULL);
/* the menu of the right button */
menu = XmCreatePopupMenu(area, "mcdeRunningMenu", NULL, 0);
menuItem = XmCreatePushButton(menu, "keep", NULL, 0);
XtAddCallback(menuItem, XmNactivateCallback, MenuChosen, NULL);
XtManageChild(menuItem);
{
int d;
for (d = 0; d < ndrawerIcons; d++)
if (drawerIcons[d].w == NULL)
AddDrawerWidget(d);
}
Update();
}