Files
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

495 lines
13 KiB
C

/*
* Strip.c -- MCDE: the strip view (Strip.h)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The reference's Cover Flow, without what MCDE does not do: no
* compositing, no perspective, no reflection, no animation. The chosen
* picture is large in the middle, up to three neighbours stand flat beside
* it (the type's picture where a file is no picture), the name and the kind
* are under it, and the folder's list is under them in a paned window. A
* picture is read through McdeThumb (the cache in ~/.dt/thumbs), so a
* second visit costs no decoder; the strip is drawn on Expose and when the
* choice changes, nothing runs in between.
*
* C99, POSIX, Xt and Motif (ext/src/mcde/AGENTS.md).
*/
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/PanedW.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrollBar.h>
#include <Xm/ScrolledW.h>
#include <Xm/Xm.h>
#include <Dt/Dts.h>
#include <Mcde/Thumb.h>
#include "Encaps.h"
#include "SharedProcs.h"
#include "FileMgr.h"
#include "Navigation.h"
#include "Peek.h"
#include "Strip.h"
#define BIG_W 260
#define BIG_H 200
#define SMALL_W 76
#define SMALL_H 60
#define SIDE 3 /* neighbours each side */
#define SLOTS (2 * SIDE + 1)
#define MAX_ENTRIES 4096
typedef struct {
FileMgrRec *rec;
Widget form, paned, area, bar, list, name, kind;
char dir[2048];
char **names;
int n, chosen;
Pixmap pm[SLOTS];
int pmw[SLOTS], pmh[SLOTS], pmidx[SLOTS];
} Strip;
#define MAX_WINDOWS 32
static Strip strips[MAX_WINDOWS];
static Strip *
Of(FileMgrRec *rec, Boolean make)
{
int i;
for (i = 0; i < MAX_WINDOWS; i++)
if (strips[i].rec == rec)
return &strips[i];
if (!make)
return NULL;
for (i = 0; i < MAX_WINDOWS; i++)
if (strips[i].rec == NULL) {
memset(&strips[i], 0, sizeof strips[i]);
strips[i].rec = rec;
for (int k = 0; k < SLOTS; k++)
strips[i].pmidx[k] = -1;
return &strips[i];
}
return NULL;
}
static void
SetText(Widget w, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(w, XmNlabelString, s, NULL);
XmStringFree(s);
}
static char *
Join(const char *dir, const char *name)
{
size_t n = strlen(dir) + strlen(name) + 2;
char *p = XtMalloc((Cardinal) n);
(void)snprintf(p, n, "%s%s%s", dir, strcmp(dir, "/") == 0 ? "" : "/",
name);
return p;
}
static int
NameCompare(const void *a, const void *b)
{
return strcasecmp(*(char *const *) a, *(char *const *) b);
}
static void
FreePixmaps(Strip *s)
{
int i;
for (i = 0; i < SLOTS; i++) {
if (s->pm[i] != None)
McdeThumbFreePixmap(XtScreen(s->area), s->pm[i]);
s->pm[i] = None;
s->pmidx[i] = -1;
}
}
/* the kind of a file: "Folder", or the type's label, or its extension */
static void
KindOf(const char *path, char *buf, size_t len)
{
struct stat st;
char *type = NULL, *label = NULL;
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
(void)snprintf(buf, len, "Folder");
return;
}
type = DtDtsFileToDataType((char *) path);
if (type != NULL) {
label = DtDtsDataTypeToAttributeValue(type, "TYPE_LABEL", NULL);
if (label != NULL) {
(void)snprintf(buf, len, "%s", label);
DtDtsFreeAttributeValue(label);
DtDtsFreeDataType(type);
return;
}
DtDtsFreeDataType(type);
}
{
const char *dot = strrchr(path, '.');
(void)snprintf(buf, len, "%s", dot != NULL && dot[1] != '\0' ?
dot + 1 : "File");
}
}
/* the picture of ENTRY into SLOT: big for the chosen one, small beside */
static void
LoadSlot(Strip *s, int slot, int entry)
{
char *path;
int w = 0, h = 0;
s->pm[slot] = None;
s->pmidx[slot] = entry;
if (entry < 0 || entry >= s->n)
return;
path = Join(s->dir, s->names[entry]);
if (McdeThumbLoad(XtScreen(s->area), path, slot == SIDE ? BIG_W :
SMALL_W, slot == SIDE ? BIG_H : SMALL_H, &s->pm[slot], &w,
&h) == 0) {
s->pmw[slot] = w;
s->pmh[slot] = h;
} else
s->pm[slot] = None;
XtFree(path);
}
static void Draw(Strip *s);
static void
Select(Strip *s, int entry)
{
char path[2200], kind[128];
if (s->n <= 0)
return;
if (entry < 0)
entry = 0;
if (entry >= s->n)
entry = s->n - 1;
s->chosen = entry;
FreePixmaps(s);
for (int i = 0; i < SLOTS; i++)
LoadSlot(s, i, entry - SIDE + i);
SetText(s->name, s->names[entry]);
(void)snprintf(path, sizeof path, "%s/%s", s->dir, s->names[entry]);
KindOf(path, kind, sizeof kind);
SetText(s->kind, kind);
if (s->list != NULL)
XmListSelectPos(s->list, entry + 1, False);
if (s->bar != NULL) {
XmScrollBarSetValues(s->bar, entry, 1, 0, s->n, False);
XtVaSetValues(s->bar, XmNmaximum, s->n > 0 ? s->n : 1, NULL);
}
Draw(s);
}
/* the chosen picture large in the middle, the neighbours flat beside it */
static void
Draw(Strip *s)
{
Display *dpy = XtDisplay(s->area);
Window win = XtWindow(s->area);
Dimension width = 0, height = 0;
Pixel bg, fg, top, bottom;
GC gc;
int x = 6, i;
if (win == None)
return;
XtVaGetValues(s->area, XmNwidth, &width, XmNheight, &height,
XmNbackground, &bg, XmNforeground, &fg,
XmNtopShadowColor, &top, XmNbottomShadowColor, &bottom, NULL);
gc = XCreateGC(dpy, win, 0, NULL);
XSetForeground(dpy, gc, bg);
XFillRectangle(dpy, win, gc, 0, 0, width, height);
for (i = 0; i < SLOTS; i++) {
int w, h, y;
if (s->pm[i] == None)
continue;
w = s->pmw[i];
h = s->pmh[i];
y = ((int) height - h) / 2;
if (i == SIDE) {
x = ((int) width - w) / 2;
/* the shadows of a Motif frame, two lines */
XSetForeground(dpy, gc, top);
XDrawRectangle(dpy, win, gc, x - 3, y - 3, w + 5,
h + 5);
XSetForeground(dpy, gc, bottom);
XDrawRectangle(dpy, win, gc, x - 2, y - 2, w + 5,
h + 5);
} else {
XSetForeground(dpy, gc, top);
XDrawRectangle(dpy, win, gc, x - 2, y - 2, w + 3,
h + 3);
XSetForeground(dpy, gc, bottom);
XDrawRectangle(dpy, win, gc, x - 1, y - 1, w + 3,
h + 3);
}
XCopyArea(dpy, s->pm[i], win, gc, 0, 0, (unsigned) w,
(unsigned) h, x, y);
x += (i == SIDE ? w + 24 : w + 10);
}
/* a file that is no picture: its name where the picture would be */
if (s->n > 0 && s->pm[SIDE] == None) {
const char *nm = s->names[s->chosen];
int len = (int) strlen(nm);
XSetForeground(dpy, gc, fg);
XDrawString(dpy, win, gc, ((int) width - 6 * len) / 2,
(int) height / 2, nm, len);
}
XFreeGC(dpy, gc);
}
static void
AreaExpose(Widget w, XtPointer data, XtPointer call)
{
Strip *s = data;
(void) w;
(void) call;
Draw(s);
}
static void
ListChosen(Widget w, XtPointer data, XtPointer call)
{
Strip *s = data;
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
(void) w;
if (c->item_position - 1 != s->chosen)
Select(s, c->item_position - 1);
}
static void
BarMoved(Widget w, XtPointer data, XtPointer call)
{
Strip *s = data;
XmScrollBarCallbackStruct *c = (XmScrollBarCallbackStruct *) call;
(void) w;
if (c->value != s->chosen)
Select(s, c->value);
}
static void
StripKey(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
{
Strip *s = data;
KeySym key = XLookupKeysym(&ev->xkey, 0);
(void) w;
if (ev->type != KeyPress)
return;
if (key == XK_Left) {
Select(s, s->chosen - 1);
*cont = False;
} else if (key == XK_Right) {
Select(s, s->chosen + 1);
*cont = False;
} else if (key == XK_space) {
char *path;
if (s->n <= 0)
return;
path = Join(s->dir, s->names[s->chosen]);
_McdePeekPath(s->rec, path);
XtFree(path);
*cont = False;
}
}
static void
Fill(Strip *s, const char *dir)
{
DIR *d;
struct dirent *de;
int cap = 0, i;
for (i = 0; i < s->n; i++)
XtFree(s->names[i]);
XtFree((char *) s->names);
s->names = NULL;
s->n = 0;
(void)snprintf(s->dir, sizeof s->dir, "%s", dir);
if ((d = opendir(dir)) != NULL) {
while ((de = readdir(d)) != NULL && s->n < MAX_ENTRIES) {
if (de->d_name[0] == '.')
continue;
if (s->n == cap) {
cap = cap ? 2 * cap : 64;
s->names = (char **)XtRealloc((char *) s->names,
(Cardinal) (cap * sizeof(char *)));
}
s->names[s->n++] = XtNewString(de->d_name);
}
closedir(d);
}
if (s->n > 1)
qsort(s->names, (size_t) s->n, sizeof(char *), NameCompare);
XmListDeleteAllItems(s->list);
for (i = 0; i < s->n; i++) {
char *path = Join(dir, s->names[i]), kind[128], size[32];
struct stat st;
char line[400];
XmString x;
if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode))
(void)snprintf(size, sizeof size, "%.0f KB",
(double) st.st_size / 1024.0);
else
(void)snprintf(size, sizeof size, "-");
KindOf(path, kind, sizeof kind);
XtFree(path);
(void)snprintf(line, sizeof line, "%-34s %9s %s",
s->names[i], size, kind);
x = XmStringCreateLocalized(line);
XmListAddItemUnselected(s->list, x, 0);
XmStringFree(x);
}
XtVaSetValues(s->bar, XmNmaximum, s->n > 0 ? s->n : 1, NULL);
s->chosen = -1;
Select(s, 0);
}
void
_McdeStripCreate(FileMgrRec *rec, Widget parent, Widget work)
{
Strip *s = Of(rec, True);
Widget paned, top;
Arg args[8];
int n = 0;
if (s == NULL)
return;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(args[n], XmNtopWidget, work); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
XtSetArg(args[n], XmNleftWidget, work); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
s->form = XmCreateForm(parent, "mcdeStrip", args, n);
rec->mcde_strip = s->form;
/* the picture above, its name and kind, its scroll bar, and the
* folder's list under them (a fixed split; no sash to drag) */
s->area = XtVaCreateManagedWidget("stripArea", xmDrawingAreaWidgetClass,
s->form, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, XmNheight, 250,
XmNresizePolicy, XmRESIZE_ANY, XmNmarginWidth, 0,
XmNmarginHeight, 0, NULL);
s->name = XtVaCreateManagedWidget("stripName", xmLabelWidgetClass,
s->form, XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, s->area, XmNtopOffset, 4,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
s->kind = XtVaCreateManagedWidget("stripKind", xmLabelWidgetClass,
s->form, XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, s->name, XmNtopOffset, 2,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
{
XmRenderTable rt;
XmRendition r;
Arg ra[3];
int rn = 0;
XtSetArg(ra[rn], XmNfontType, XmFONT_IS_XFT); rn++;
XtSetArg(ra[rn], XmNfontName, "Hack"); rn++;
XtSetArg(ra[rn], XmNfontSize, 12); rn++;
r = XmRenditionCreate(s->name, XmFONTLIST_DEFAULT_TAG, ra, rn);
rt = XmRenderTableAddRenditions(NULL, &r, 1, XmMERGE_REPLACE);
XmRenditionFree(r);
XtVaSetValues(s->name, XmNrenderTable, rt, NULL);
XmRenderTableFree(rt);
}
s->bar = XtVaCreateManagedWidget("stripBar", xmScrollBarWidgetClass,
s->form, XmNorientation, XmHORIZONTAL,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, s->kind,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
s->list = XmCreateScrolledList(s->form, "stripList", NULL, 0);
XtVaSetValues(s->list, XmNvisibleItemCount, 12,
XmNselectionPolicy, XmBROWSE_SELECT, NULL);
XtVaSetValues(XtParent(s->list),
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, s->bar,
XmNtopOffset, 6,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, NULL);
XtManageChild(s->list);
XtAddCallback(s->area, XmNexposeCallback, AreaExpose, s);
XtAddEventHandler(s->area, KeyPressMask, False, StripKey, s);
XtAddEventHandler(s->list, KeyPressMask, False, StripKey, s);
XtAddCallback(s->list, XmNbrowseSelectionCallback, ListChosen, s);
XtAddCallback(s->bar, XmNvalueChangedCallback, BarMoved, s);
}
void
_McdeStripShow(FileMgrRec *rec, Widget work, const char *dir, Boolean on)
{
Strip *s = Of(rec, False);
if (s == NULL || s->form == NULL)
return;
XtVaSetValues(XtParent(work), XmNresizePolicy, XmRESIZE_NONE, NULL);
if (!on) {
XtManageChild(work);
XtUnmanageChild(s->form);
rec->mcde_strip_on = False;
return;
}
{
Dimension w = 0, h = 0;
XtVaGetValues(work, XmNwidth, &w, XmNheight, &h, NULL);
XtVaSetValues(s->form, XmNwidth, w, XmNheight, h,
XmNresizePolicy, XmRESIZE_NONE, NULL);
}
rec->mcde_strip_on = True;
XtManageChild(s->form);
XtUnmanageChild(work);
Fill(s, dir != NULL ? dir : "");
}
void
_McdeStripFollow(FileMgrRec *rec, const char *dir)
{
Strip *s = Of(rec, False);
if (s == NULL || s->form == NULL || !XtIsManaged(s->form) || dir == NULL)
return;
if (strcmp(s->dir, dir) == 0)
return;
Fill(s, dir);
}