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>
786 lines
20 KiB
C
786 lines
20 KiB
C
/*
|
|
* Peek.c -- MCDE: the preview of the chosen file (Peek.h)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Space shows what is chosen without opening it: a picture at the size it
|
|
* fits, a text's first 64 KB, a folder's icon and how much is in it, the
|
|
* type's icon for everything else. Space again, Escape or the close button
|
|
* hides it; while it is up, the selection in the window behind it keeps
|
|
* changing it, as the arrow keys of the reference's list do.
|
|
*
|
|
* The window is a plain Motif top level shell in the palette: no
|
|
* transparency, no glass, no fade. A picture is read in the idle time
|
|
* (McdeThumbRequest), so a folder of large photographs does not stop the
|
|
* window, and only the picture that is asked for is read.
|
|
*
|
|
* Option+Space puts the preview over the whole screen
|
|
* (_NET_WM_STATE_FULLSCREEN, dtwm's WmEwmh.c), Option+Space or Escape
|
|
* brings it back.
|
|
*
|
|
* C99, POSIX 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 <X11/Xatom.h>
|
|
#include <X11/keysym.h>
|
|
#include <Xm/Xm.h>
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/ScrolledW.h>
|
|
#include <Xm/Text.h>
|
|
#include <Xm/ToggleB.h>
|
|
|
|
#include <Dt/Action.h>
|
|
#include <Dt/Dts.h>
|
|
#include <Mcde/Thumb.h>
|
|
|
|
#include "Encaps.h"
|
|
#include "SharedProcs.h"
|
|
#include "FileMgr.h"
|
|
#include "Navigation.h"
|
|
#include "Peek.h"
|
|
|
|
#define MAX_WINDOWS 32
|
|
#define MAX_TEXT (64 * 1024)
|
|
#define MAX_W 620 /* the picture's room on a 1024 x 768 screen */
|
|
#define MAX_H 440
|
|
#define SMALL_W 420
|
|
#define SMALL_H 220
|
|
|
|
typedef struct {
|
|
FileMgrRec *rec;
|
|
Widget shell;
|
|
Widget form;
|
|
Widget buttons;
|
|
Widget icon; /* the type's picture, for a folder */
|
|
Widget info; /* one line under it */
|
|
Widget image; /* the picture itself */
|
|
Widget text; /* the scrolled text */
|
|
Widget open;
|
|
Widget full;
|
|
Pixmap pixmap; /* of image */
|
|
Pixmap iconpm; /* of icon */
|
|
char *path; /* what is shown, NULL for none */
|
|
void *request; /* the picture still being read */
|
|
int iw, ih;
|
|
Widget focus; /* what had the keys before the preview */
|
|
Boolean shown; /* XtPopup has been called */
|
|
Boolean fullscreen;
|
|
} Peek;
|
|
|
|
static Peek peeks[MAX_WINDOWS];
|
|
|
|
static void PictureReady(void *client, Pixmap pixmap, int w, int h);
|
|
static void RefocusLater(XtPointer client, XtIntervalId *id);
|
|
static void Room(Peek *p, int *maxw, int *maxh);
|
|
static void KeyHandler(Widget w, XtPointer client, XEvent *event,
|
|
Boolean *cont);
|
|
static void OpenCB(Widget w, XtPointer client, XtPointer call);
|
|
static void FullCB(Widget w, XtPointer client, XtPointer call);
|
|
|
|
static FileMgrData *
|
|
DataOf(FileMgrRec *rec)
|
|
{
|
|
DialogData *dd = _DtGetInstanceData((XtPointer) rec);
|
|
|
|
return dd != NULL ? (FileMgrData *) dd->data : NULL;
|
|
}
|
|
|
|
static Peek *
|
|
PeekOf(FileMgrRec *rec, Boolean make)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_WINDOWS; i++)
|
|
if (peeks[i].rec == rec)
|
|
return &peeks[i];
|
|
if (!make)
|
|
return NULL;
|
|
for (i = 0; i < MAX_WINDOWS; i++)
|
|
if (peeks[i].rec == NULL) {
|
|
memset(&peeks[i], 0, sizeof peeks[i]);
|
|
peeks[i].rec = rec;
|
|
peeks[i].pixmap = XmUNSPECIFIED_PIXMAP;
|
|
peeks[i].iconpm = XmUNSPECIFIED_PIXMAP;
|
|
return &peeks[i];
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/* the path of what is chosen in the window, or NULL */
|
|
static char *
|
|
ChosenPath(FileMgrData *data)
|
|
{
|
|
FileViewData *fvd;
|
|
DirectorySet *ds;
|
|
|
|
if (data == NULL || data->selected_file_count != 1 ||
|
|
data->selection_list == NULL)
|
|
return NULL;
|
|
fvd = data->selection_list[0];
|
|
if (fvd == NULL || fvd->file_data == NULL ||
|
|
strcmp(fvd->file_data->file_name, "..") == 0)
|
|
return NULL;
|
|
ds = (DirectorySet *) fvd->directory_set;
|
|
if (ds == NULL || ds->name == NULL)
|
|
return NULL;
|
|
return Join(ds->name, fvd->file_data->file_name);
|
|
}
|
|
|
|
/* ---- the window --------------------------------------------------------- */
|
|
|
|
static Widget
|
|
Content(Peek *p, const char *name, WidgetClass class)
|
|
{
|
|
return XtVaCreateWidget(name, class, p->form,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_WIDGET,
|
|
XmNbottomWidget, p->buttons, NULL);
|
|
}
|
|
|
|
static void
|
|
Build(Peek *p)
|
|
{
|
|
Widget text;
|
|
Arg args[6];
|
|
XmString s;
|
|
|
|
p->shell = XtVaCreatePopupShell("mcdePeek", topLevelShellWidgetClass,
|
|
p->rec->shell, XmNtitle, "Preview", XmNallowShellResize, True,
|
|
NULL);
|
|
p->form = XtVaCreateManagedWidget("mcdePeekForm", xmFormWidgetClass,
|
|
p->shell, XmNmarginWidth, 8, XmNmarginHeight, 8, NULL);
|
|
|
|
p->buttons = XtVaCreateManagedWidget("mcdePeekButtons",
|
|
xmFormWidgetClass, p->form,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_FORM, NULL);
|
|
p->open = XtVaCreateManagedWidget("mcdePeekOpen",
|
|
xmPushButtonWidgetClass, p->buttons,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM, NULL);
|
|
p->full = XtVaCreateManagedWidget("mcdePeekFull",
|
|
xmToggleButtonWidgetClass, p->buttons,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, NULL);
|
|
s = XmStringCreateLocalized((char *) "Open");
|
|
XtVaSetValues(p->open, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
s = XmStringCreateLocalized((char *) "Full Screen");
|
|
XtVaSetValues(p->full, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
|
|
p->icon = Content(p, "mcdePeekIcon", xmLabelWidgetClass);
|
|
p->info = Content(p, "mcdePeekInfo", xmLabelWidgetClass);
|
|
p->image = Content(p, "mcdePeekImage", xmLabelWidgetClass);
|
|
|
|
XtSetArg(args[0], XmNeditable, False);
|
|
XtSetArg(args[1], XmNeditMode, XmMULTI_LINE_EDIT);
|
|
XtSetArg(args[2], XmNwordWrap, True);
|
|
XtSetArg(args[3], XmNscrollVertical, True);
|
|
XtSetArg(args[4], XmNscrollHorizontal, False);
|
|
text = XmCreateScrolledText(p->form, "mcdePeekText", args, 5);
|
|
p->text = text;
|
|
/* the text is managed inside its scrolled window, the window itself
|
|
* joins the form only while a text is shown (XtParent is the window) */
|
|
XtManageChild(text);
|
|
XtVaSetValues(XtParent(text),
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_WIDGET,
|
|
XmNbottomWidget, p->buttons, NULL);
|
|
|
|
XtAddCallback(p->open, XmNactivateCallback, OpenCB, p);
|
|
XtAddCallback(p->full, XmNvalueChangedCallback, FullCB, p);
|
|
XtAddEventHandler(p->shell, KeyPressMask, False, KeyHandler, p);
|
|
}
|
|
|
|
/* ---- what it shows ------------------------------------------------------ */
|
|
|
|
static void
|
|
HideAll(Peek *p)
|
|
{
|
|
XtUnmanageChild(p->icon);
|
|
XtUnmanageChild(p->info);
|
|
XtUnmanageChild(p->image);
|
|
XtUnmanageChild(XtParent(p->text));
|
|
}
|
|
|
|
/* the icon of a file from CDE's data types, as Columns.c finds it */
|
|
static Pixmap
|
|
TypeIcon(Widget w, const char *path)
|
|
{
|
|
char *type, *icon;
|
|
Pixmap pm = XmUNSPECIFIED_PIXMAP;
|
|
Pixel fg, bg;
|
|
size_t i;
|
|
static const char *const sizes[] = { ".l.pm", ".m.pm", ".l.bm" };
|
|
|
|
if (path == NULL || (type = DtDtsFileToDataType((char *) path)) == NULL)
|
|
return pm;
|
|
icon = DtDtsDataTypeToAttributeValue(type, DtDTS_DA_ICON, NULL);
|
|
if (icon != NULL) {
|
|
XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL);
|
|
for (i = 0; i < sizeof sizes / sizeof sizes[0] &&
|
|
pm == XmUNSPECIFIED_PIXMAP; i++) {
|
|
char buf[256];
|
|
|
|
(void)snprintf(buf, sizeof buf, "%s%s", icon, sizes[i]);
|
|
pm = XmGetPixmap(XtScreen(w), buf, fg, bg);
|
|
if (pm == XmUNSPECIFIED_PIXMAP) {
|
|
(void)snprintf(buf, sizeof buf,
|
|
"%s/appconfig/icons/C/%s%s",
|
|
CDE_INSTALLATION_TOP, icon, sizes[i]);
|
|
pm = XmGetPixmap(XtScreen(w), buf, fg, bg);
|
|
}
|
|
}
|
|
DtDtsFreeAttributeValue(icon);
|
|
}
|
|
DtDtsFreeDataType(type);
|
|
return pm;
|
|
}
|
|
|
|
static void
|
|
ShowInfo(Peek *p, const char *text, Pixmap icon)
|
|
{
|
|
XmString s;
|
|
|
|
HideAll(p);
|
|
if (p->iconpm != XmUNSPECIFIED_PIXMAP)
|
|
McdeThumbFreePixmap(XtScreen(p->shell), p->iconpm);
|
|
p->iconpm = icon;
|
|
if (icon != XmUNSPECIFIED_PIXMAP) {
|
|
XtVaSetValues(p->icon, XmNlabelType, XmPIXMAP,
|
|
XmNlabelPixmap, icon, NULL);
|
|
XtManageChild(p->icon);
|
|
XtVaSetValues(p->info, XmNtopAttachment, XmATTACH_WIDGET,
|
|
XmNtopWidget, p->icon, NULL);
|
|
} else {
|
|
XtVaSetValues(p->info, XmNtopAttachment, XmATTACH_FORM, NULL);
|
|
}
|
|
s = XmStringCreateLocalized((char *) text);
|
|
XtVaSetValues(p->info, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
XtManageChild(p->info);
|
|
{
|
|
int w, h;
|
|
|
|
Room(p, &w, &h);
|
|
p->iw = w < SMALL_W ? w : SMALL_W;
|
|
p->ih = h < SMALL_H ? h : SMALL_H;
|
|
}
|
|
}
|
|
|
|
static void
|
|
ShowImage(Peek *p, Pixmap pixmap, int w, int h)
|
|
{
|
|
if (p->pixmap != XmUNSPECIFIED_PIXMAP)
|
|
McdeThumbFreePixmap(XtScreen(p->shell), p->pixmap);
|
|
p->pixmap = pixmap;
|
|
HideAll(p);
|
|
XtVaSetValues(p->image, XmNlabelType, XmPIXMAP,
|
|
XmNlabelPixmap, pixmap, NULL);
|
|
XtManageChild(p->image);
|
|
p->iw = w;
|
|
p->ih = h;
|
|
}
|
|
|
|
static void
|
|
ShowText(Peek *p, const char *text)
|
|
{
|
|
/* the text first, at its beginning: a widget that is shown with the
|
|
* string already in it does not scroll to the cursor it never had */
|
|
XmTextSetString(p->text, (char *) text);
|
|
XmTextSetInsertionPosition(p->text, 0);
|
|
XmTextSetTopCharacter(p->text, 0);
|
|
HideAll(p);
|
|
XtManageChild(XtParent(p->text));
|
|
}
|
|
|
|
/* a picture from its first bytes, before anything is decoded */
|
|
static int
|
|
IsPicture(const char *path)
|
|
{
|
|
unsigned char b[8];
|
|
const char *dot;
|
|
FILE *fp;
|
|
size_t n;
|
|
|
|
if ((dot = strrchr(path, '.')) != NULL &&
|
|
(strcasecmp(dot, ".png") == 0 || strcasecmp(dot, ".jpg") == 0 ||
|
|
strcasecmp(dot, ".jpeg") == 0 || strcasecmp(dot, ".gif") == 0 ||
|
|
strcasecmp(dot, ".bmp") == 0 || strcasecmp(dot, ".xpm") == 0))
|
|
return 1;
|
|
if ((fp = fopen(path, "rb")) == NULL)
|
|
return 0;
|
|
n = fread(b, 1, sizeof b, fp);
|
|
fclose(fp);
|
|
if (n >= 8 && memcmp(b, "\211PNG\r\n\032\n", 8) == 0)
|
|
return 1;
|
|
if (n >= 3 && b[0] == 0xff && b[1] == 0xd8 && b[2] == 0xff)
|
|
return 1;
|
|
if (n >= 6 && (memcmp(b, "GIF87a", 6) == 0 ||
|
|
memcmp(b, "GIF89a", 6) == 0))
|
|
return 1;
|
|
if (n >= 2 && b[0] == 'B' && b[1] == 'M')
|
|
return 1;
|
|
if (n >= 3 && b[0] == '/' && b[1] == '*' && b[2] == ' ')
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
/* a text file: no NUL and few control characters in the first kilobyte */
|
|
static int
|
|
IsText(const char *path)
|
|
{
|
|
FILE *fp;
|
|
unsigned char buf[1024];
|
|
size_t n, i, bad = 0;
|
|
|
|
if ((fp = fopen(path, "rb")) == NULL)
|
|
return 0;
|
|
n = fread(buf, 1, sizeof buf, fp);
|
|
fclose(fp);
|
|
if (n == 0)
|
|
return 0;
|
|
for (i = 0; i < n; i++) {
|
|
if (buf[i] == '\0')
|
|
return 0;
|
|
if (buf[i] < 32 && buf[i] != '\t' && buf[i] != '\n' &&
|
|
buf[i] != '\r' && buf[i] != '\f' && buf[i] != '\033')
|
|
bad++;
|
|
}
|
|
return bad * 10 < n;
|
|
}
|
|
|
|
static char *
|
|
ReadHead(const char *path)
|
|
{
|
|
FILE *fp;
|
|
char *buf;
|
|
size_t n;
|
|
|
|
if ((fp = fopen(path, "rb")) == NULL)
|
|
return NULL;
|
|
if ((buf = XtMalloc(MAX_TEXT + 1)) == NULL) {
|
|
fclose(fp);
|
|
return NULL;
|
|
}
|
|
n = fread(buf, 1, MAX_TEXT, fp);
|
|
fclose(fp);
|
|
buf[n] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
static void
|
|
Update(Peek *p, const char *path)
|
|
{
|
|
struct stat st;
|
|
const char *name;
|
|
char title[256];
|
|
char text[256];
|
|
int w, h;
|
|
DIR *d;
|
|
struct dirent *de;
|
|
int n;
|
|
char *head;
|
|
|
|
if (p->request != NULL) {
|
|
McdeThumbCancel(p->request);
|
|
p->request = NULL;
|
|
}
|
|
if (p->path != NULL)
|
|
XtFree(p->path);
|
|
p->path = path != NULL ? XtNewString(path) : NULL;
|
|
if (path == NULL) {
|
|
XtVaSetValues(p->shell, XmNtitle, "Preview", NULL);
|
|
ShowInfo(p, "Nothing is chosen.", XmUNSPECIFIED_PIXMAP);
|
|
return;
|
|
}
|
|
name = strrchr(path, '/');
|
|
name = name != NULL && name[1] != '\0' ? name + 1 : path;
|
|
(void)snprintf(title, sizeof title, "%s", name);
|
|
XtVaSetValues(p->shell, XmNtitle, title, NULL);
|
|
|
|
if (stat(path, &st) != 0) {
|
|
ShowInfo(p, "This file cannot be read.",
|
|
XmUNSPECIFIED_PIXMAP);
|
|
return;
|
|
}
|
|
if (S_ISDIR(st.st_mode)) {
|
|
n = 0;
|
|
if ((d = opendir(path)) != NULL) {
|
|
while ((de = readdir(d)) != NULL)
|
|
if (de->d_name[0] != '.')
|
|
n++;
|
|
closedir(d);
|
|
}
|
|
(void)snprintf(text, sizeof text, "%d item%s", n,
|
|
n == 1 ? "" : "s");
|
|
ShowInfo(p, text, TypeIcon(p->info, path));
|
|
return;
|
|
}
|
|
if (IsPicture(path)) {
|
|
HideAll(p);
|
|
Room(p, &w, &h);
|
|
p->request = McdeThumbRequest(p->shell, path, w, h,
|
|
PictureReady, p);
|
|
return;
|
|
}
|
|
if (IsText(path) && (head = ReadHead(path)) != NULL) {
|
|
Room(p, &w, &h);
|
|
ShowText(p, head);
|
|
p->iw = w;
|
|
p->ih = h;
|
|
XtFree(head);
|
|
return;
|
|
}
|
|
(void)snprintf(text, sizeof text, "%s\n%lld bytes", name,
|
|
(long long) st.st_size);
|
|
ShowInfo(p, text, TypeIcon(p->info, path));
|
|
}
|
|
|
|
/* the room beside the window it belongs to: a picture is read no larger
|
|
* than it has to be, and the window never has to cover the list */
|
|
static void
|
|
Room(Peek *p, int *maxw, int *maxh)
|
|
{
|
|
Dimension pw = 0, ph = 0;
|
|
int px = 0, py = 0;
|
|
Display *dpy = XtDisplay(p->shell);
|
|
Screen *screen = DefaultScreenOfDisplay(dpy);
|
|
int sw = WidthOfScreen(screen), sh = HeightOfScreen(screen);
|
|
Window child;
|
|
|
|
XtVaGetValues(p->rec->shell, XmNwidth, &pw, XmNheight, &ph, NULL);
|
|
if (XtWindow(p->rec->shell) != None)
|
|
XTranslateCoordinates(dpy, XtWindow(p->rec->shell),
|
|
DefaultRootWindow(dpy), 0, 0, &px, &py, &child);
|
|
*maxw = sw - (px + (int) pw + 16) - 40;
|
|
if (*maxw < 220)
|
|
*maxw = sw - 88;
|
|
*maxh = sh - (py + 24) - 80;
|
|
if (*maxh < 160)
|
|
*maxh = sh - 120;
|
|
if (*maxw > MAX_W)
|
|
*maxw = MAX_W;
|
|
if (*maxh > MAX_H)
|
|
*maxh = MAX_H;
|
|
}
|
|
|
|
static void
|
|
Fit(Peek *p)
|
|
{
|
|
int w = p->iw > 0 ? p->iw : SMALL_W;
|
|
int h = p->ih > 0 ? p->ih : SMALL_H;
|
|
Dimension pw = 0, ph = 0;
|
|
int px = 0, py = 0, x, y;
|
|
Display *dpy = XtDisplay(p->shell);
|
|
Screen *screen = DefaultScreenOfDisplay(dpy);
|
|
int sw = WidthOfScreen(screen), sh = HeightOfScreen(screen);
|
|
Window child;
|
|
|
|
w += 32;
|
|
h += 88;
|
|
/* beside the window it belongs to, as the reference floats beside
|
|
* the list; left of it or at the edge when there is no room right.
|
|
* dtwm places a new window itself, so the place is given to it as a
|
|
* move of the window that is already up (XmNx alone is its hint). */
|
|
XtVaGetValues(p->rec->shell, XmNwidth, &pw, XmNheight, &ph, NULL);
|
|
if (XtWindow(p->rec->shell) != None)
|
|
XTranslateCoordinates(dpy, XtWindow(p->rec->shell),
|
|
DefaultRootWindow(dpy), 0, 0, &px, &py, &child);
|
|
x = px + (int) pw + 12;
|
|
if (x + w > sw)
|
|
x = px - w - 12;
|
|
if (x < 0)
|
|
x = sw - w - 4;
|
|
if (x < 0)
|
|
x = 0;
|
|
y = py + 24;
|
|
if (y + h > sh)
|
|
y = sh - h;
|
|
if (y < 0)
|
|
y = 0;
|
|
{
|
|
char geometry[32];
|
|
|
|
(void)snprintf(geometry, sizeof geometry, "+%d+%d", x, y);
|
|
XtVaSetValues(p->shell, XmNgeometry, geometry, XmNwidth, w,
|
|
XmNheight, h, XmNx, x, XmNy, y, NULL);
|
|
}
|
|
if (XtWindow(p->shell) != None && p->shown) {
|
|
XMoveWindow(dpy, XtWindow(p->shell), x, y);
|
|
/* a viewer lies over what it shows; dtwm does not raise on
|
|
* focus (ext/src/mcde/AGENTS.md, "Windows") */
|
|
XRaiseWindow(dpy, XtWindow(p->shell));
|
|
}
|
|
}
|
|
|
|
/* ---- buttons, keys, the screen ----------------------------------------- */
|
|
|
|
static void
|
|
OpenCB(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
Peek *p = client;
|
|
char *type, *actions, *first, *end;
|
|
DtActionArg arg;
|
|
|
|
(void)w;
|
|
(void)call;
|
|
if (p->path == NULL)
|
|
return;
|
|
if ((type = DtDtsFileToDataType(p->path)) == NULL)
|
|
return;
|
|
actions = DtDtsDataTypeToAttributeValue(type, DtDTS_DA_ACTION_LIST,
|
|
NULL);
|
|
if (actions == NULL) {
|
|
DtDtsFreeDataType(type);
|
|
return;
|
|
}
|
|
first = XtNewString(actions);
|
|
if ((end = strchr(first, ',')) != NULL)
|
|
*end = '\0';
|
|
memset(&arg, 0, sizeof arg);
|
|
arg.argClass = DtACTION_FILE;
|
|
arg.u.file.name = p->path;
|
|
(void)DtActionInvoke(p->shell, first, &arg, 1, NULL, NULL, NULL, True,
|
|
NULL, NULL);
|
|
XtFree(first);
|
|
DtDtsFreeAttributeValue(actions);
|
|
DtDtsFreeDataType(type);
|
|
}
|
|
|
|
/* _NET_WM_STATE_FULLSCREEN, the way dtwm reads it (WmEwmh.c) */
|
|
static void
|
|
FullScreen(Peek *p)
|
|
{
|
|
Display *dpy = XtDisplay(p->shell);
|
|
Window root = DefaultRootWindow(dpy);
|
|
Atom state = XInternAtom(dpy, "_NET_WM_STATE", False);
|
|
Atom full = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
|
|
XEvent e;
|
|
|
|
memset(&e, 0, sizeof e);
|
|
e.xclient.type = ClientMessage;
|
|
e.xclient.window = XtWindow(p->shell);
|
|
e.xclient.message_type = state;
|
|
e.xclient.format = 32;
|
|
e.xclient.data.l[0] = p->fullscreen ? 0 : 1; /* remove, add */
|
|
e.xclient.data.l[1] = (long) full;
|
|
e.xclient.data.l[2] = 0;
|
|
e.xclient.data.l[3] = 1; /* a normal application */
|
|
XSendEvent(dpy, root, False, SubstructureRedirectMask |
|
|
SubstructureNotifyMask, &e);
|
|
XFlush(dpy);
|
|
p->fullscreen = !p->fullscreen;
|
|
XtVaSetValues(p->full, XmNset, p->fullscreen ? XmSET : XmUNSET, NULL);
|
|
}
|
|
|
|
static void
|
|
FullCB(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
Peek *p = client;
|
|
XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call;
|
|
|
|
(void)w;
|
|
if (c->set != p->fullscreen)
|
|
FullScreen(p);
|
|
}
|
|
|
|
static void
|
|
KeyHandler(Widget w, XtPointer client, XEvent *event, Boolean *cont)
|
|
{
|
|
Peek *p = client;
|
|
KeySym key = XLookupKeysym(&event->xkey, 0);
|
|
|
|
(void)w;
|
|
if (key == XK_Escape || key == XK_space) {
|
|
XtPopdown(p->shell);
|
|
p->shown = False;
|
|
*cont = False;
|
|
}
|
|
}
|
|
|
|
static void
|
|
PictureReady(void *client, Pixmap pixmap, int w, int h)
|
|
{
|
|
Peek *p = client;
|
|
|
|
p->request = NULL;
|
|
if (pixmap == XmUNSPECIFIED_PIXMAP) {
|
|
ShowInfo(p, "This picture cannot be read.",
|
|
TypeIcon(p->info, p->path != NULL ? p->path : "/"));
|
|
} else {
|
|
ShowImage(p, pixmap, w, h);
|
|
}
|
|
Fit(p);
|
|
}
|
|
|
|
/* ---- what the file manager calls ---------------------------------------- */
|
|
|
|
/* the keys go back where they were before the preview: the work area of
|
|
* the icon and list views, the column list in the column view */
|
|
static void
|
|
Refocus(Peek *p)
|
|
{
|
|
FileMgrRec *rec;
|
|
Widget w;
|
|
|
|
if (p == NULL || (rec = p->rec) == NULL || rec->shell == NULL ||
|
|
XtWindow(rec->shell) == None)
|
|
return;
|
|
w = p->focus != NULL ? p->focus : (Widget) rec->file_window;
|
|
if (w != NULL)
|
|
XtSetKeyboardFocus(rec->shell, w);
|
|
XSetInputFocus(XtDisplay(rec->shell), XtWindow(rec->shell),
|
|
RevertToPointerRoot, CurrentTime);
|
|
}
|
|
|
|
static void
|
|
RefocusLater(XtPointer client, XtIntervalId *id)
|
|
{
|
|
(void)id;
|
|
Refocus((Peek *) client);
|
|
}
|
|
|
|
void
|
|
_McdePeekToggle(FileMgrRec *rec)
|
|
{
|
|
Peek *p = PeekOf(rec, True);
|
|
FileMgrData *data = DataOf(rec);
|
|
char *path;
|
|
|
|
if (p == NULL)
|
|
return;
|
|
if (p->shell == NULL)
|
|
Build(p);
|
|
if (p->shown) {
|
|
XtPopdown(p->shell);
|
|
p->shown = False;
|
|
return;
|
|
}
|
|
if (rec->mcde_columns_on &&
|
|
(path = _McdeColumnsChosen(rec)) != NULL) {
|
|
/* the columns keep their own selection (Columns.c) */
|
|
Update(p, path);
|
|
XtFree(path);
|
|
} else if (data == NULL || data->selected_file_count == 0) {
|
|
XBell(XtDisplay(rec->shell), 0);
|
|
return;
|
|
} else if (data->selected_file_count > 1) {
|
|
char text[64];
|
|
|
|
(void)snprintf(text, sizeof text, "%d items selected",
|
|
data->selected_file_count);
|
|
ShowInfo(p, text, XmUNSPECIFIED_PIXMAP);
|
|
} else if ((path = ChosenPath(data)) != NULL) {
|
|
Update(p, path);
|
|
XtFree(path);
|
|
} else {
|
|
XBell(XtDisplay(rec->shell), 0);
|
|
return;
|
|
}
|
|
Fit(p);
|
|
p->focus = XmGetFocusWidget(rec->shell);
|
|
XtPopup(p->shell, XtGrabNone);
|
|
p->shown = True;
|
|
/* a viewer takes no focus: the keys stay with the list behind it.
|
|
* The window manager hands the focus to the new window after it is
|
|
* mapped, so the window's own focus is taken back once more, a
|
|
* moment later (one shot, no polling). */
|
|
Refocus(p);
|
|
XtAppAddTimeOut(XtWidgetToApplicationContext(rec->shell), 150,
|
|
RefocusLater, p);
|
|
}
|
|
|
|
void
|
|
_McdePeekFollow(FileMgrRec *rec)
|
|
{
|
|
Peek *p = PeekOf(rec, False);
|
|
FileMgrData *data = DataOf(rec);
|
|
char *path;
|
|
|
|
if (p == NULL || p->shell == NULL || !p->shown || data == NULL)
|
|
return;
|
|
if (data->selected_file_count == 0) {
|
|
Update(p, NULL);
|
|
Fit(p);
|
|
return;
|
|
}
|
|
if (data->selected_file_count > 1) {
|
|
char text[64];
|
|
|
|
(void)snprintf(text, sizeof text, "%d items selected",
|
|
data->selected_file_count);
|
|
ShowInfo(p, text, XmUNSPECIFIED_PIXMAP);
|
|
Fit(p);
|
|
return;
|
|
}
|
|
if ((path = ChosenPath(data)) != NULL) {
|
|
Update(p, path);
|
|
XtFree(path);
|
|
Fit(p);
|
|
}
|
|
}
|
|
|
|
void
|
|
_McdePeekPath(FileMgrRec *rec, const char *path)
|
|
{
|
|
Peek *p = PeekOf(rec, False);
|
|
|
|
if (p == NULL || p->shell == NULL || !p->shown)
|
|
return;
|
|
Update(p, path);
|
|
Fit(p);
|
|
}
|
|
|
|
Boolean
|
|
_McdePeekClose(FileMgrRec *rec)
|
|
{
|
|
Peek *p = PeekOf(rec, False);
|
|
|
|
if (p == NULL || p->shell == NULL || !p->shown)
|
|
return False;
|
|
XtPopdown(p->shell);
|
|
p->shown = False;
|
|
return True;
|
|
}
|
|
|
|
void
|
|
_McdePeekFullScreen(FileMgrRec *rec)
|
|
{
|
|
Peek *p = PeekOf(rec, False);
|
|
|
|
if (p == NULL || p->shell == NULL || !p->shown)
|
|
return;
|
|
FullScreen(p);
|
|
}
|