377 lines
10 KiB
C++
377 lines
10 KiB
C++
|
/*
|
||
|
*
|
||
|
Copyright 1989, 1998 The Open Group
|
||
|
|
||
|
Permission to use, copy, modify, distribute, and sell this software and its
|
||
|
documentation for any purpose is hereby granted without fee, provided that
|
||
|
the above copyright notice appear in all copies and that both that
|
||
|
copyright notice and this permission notice appear in supporting
|
||
|
documentation.
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in
|
||
|
all copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||
|
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
|
||
|
Except as contained in this notice, the name of The Open Group shall not be
|
||
|
used in advertising or otherwise to promote the sale, use or other dealings
|
||
|
in this Software without prior written authorization from The Open Group.
|
||
|
* */
|
||
|
|
||
|
/**********************************************************************
|
||
|
*
|
||
|
* Icon releated routines
|
||
|
*
|
||
|
* 10-Apr-89 Tom LaStrange Initial Version.
|
||
|
*
|
||
|
**********************************************************************/
|
||
|
|
||
|
#include <algorithm>
|
||
|
#include <stdio.h>
|
||
|
#include <vector>
|
||
|
#include "twm.h"
|
||
|
#include "screen.h"
|
||
|
#include "icons.h"
|
||
|
#include "gram.h"
|
||
|
#include "parse.h"
|
||
|
#include "util.h"
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
// TODO: initialize at runtime. Per screen unless you simplify to only one screen allowed.
|
||
|
const int icon_area_x = 1600; // big enough area for my xclock to look like an icon, not be stacked on.
|
||
|
const int icon_pad = 10;
|
||
|
|
||
|
#define iconWidth(w) (BW2 + w->icon_w_width)
|
||
|
#define iconHeight(w) (BW2 + w->icon_w_height)
|
||
|
|
||
|
static inline int
|
||
|
roundUp (int v, int multiple)
|
||
|
{
|
||
|
return ((v + multiple - 1) / multiple) * multiple;
|
||
|
}
|
||
|
|
||
|
struct Span {
|
||
|
int p1, p2;
|
||
|
};
|
||
|
inline bool operator<(Span a, Span b)
|
||
|
{
|
||
|
return a.p1 == b.p1 && a.p2 < b.p2 || a.p1 < b.p1;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
PlaceIcon(TwmWindow *tmp_win, int def_x, int def_y, int *final_x, int *final_y)
|
||
|
{
|
||
|
// Try to place in a gap on the right side.
|
||
|
|
||
|
Window root, parent;
|
||
|
Window* children;
|
||
|
unsigned int nchildren;
|
||
|
XWindowAttributes attr;
|
||
|
int y = 0;
|
||
|
if (XQueryTree(dpy, Scr->Root, &root, &parent, &children, &nchildren)) {
|
||
|
XArray<Window> childarray(children, nchildren);
|
||
|
vector<Span> occupied;
|
||
|
for (Window w : childarray) {
|
||
|
if (XGetWindowAttributes(dpy, w, &attr)) {
|
||
|
// w looks like icon?
|
||
|
if (attr.map_state != IsUnmapped
|
||
|
&& attr.height <= 200 && attr.width <= 200
|
||
|
&& attr.x >= icon_area_x) {
|
||
|
|
||
|
occupied.push_back({attr.y, attr.y + attr.height});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
int h = 64;
|
||
|
if (tmp_win->icon_height) {
|
||
|
h = tmp_win->icon_height;
|
||
|
}
|
||
|
|
||
|
sort(occupied.begin(), occupied.end());
|
||
|
int prev = 0;
|
||
|
for (auto span : occupied) {
|
||
|
if (span.p1 - prev >= h) {
|
||
|
if (span.p1 - prev >= h + 2*icon_pad)
|
||
|
y = prev + icon_pad;
|
||
|
else
|
||
|
y = prev;
|
||
|
break;
|
||
|
}
|
||
|
prev = span.p2;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (y) {
|
||
|
*final_x = icon_area_x + 20; // TODO: stick the icon roughly in the center of the space.
|
||
|
*final_y = y;
|
||
|
} else {
|
||
|
*final_x = def_x;
|
||
|
*final_y = def_y;
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
IconUp (TwmWindow *tmp_win)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void
|
||
|
IconDown (TwmWindow *tmp_win)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void
|
||
|
CreateIconWindow(TwmWindow *tmp_win, int def_x, int def_y)
|
||
|
{
|
||
|
unsigned long event_mask;
|
||
|
unsigned long valuemask; /* mask for create windows */
|
||
|
XSetWindowAttributes attributes; /* attributes for create windows */
|
||
|
Pixmap pm = None; /* tmp pixmap variable */
|
||
|
int final_x, final_y;
|
||
|
int x;
|
||
|
|
||
|
|
||
|
FB(tmp_win->iconc.fore, tmp_win->iconc.back);
|
||
|
|
||
|
tmp_win->forced = FALSE;
|
||
|
tmp_win->icon_not_ours = FALSE;
|
||
|
|
||
|
/* now go through the steps to get an icon window, if ForceIcon is
|
||
|
* set, then no matter what else is defined, the bitmap from the
|
||
|
* .twmrc file is used
|
||
|
*/
|
||
|
if (Scr->ForceIcon)
|
||
|
{
|
||
|
char *icon_name;
|
||
|
Pixmap bm;
|
||
|
|
||
|
icon_name = LookInNameList(Scr->IconNames, tmp_win->full_name);
|
||
|
if (icon_name == NULL)
|
||
|
icon_name = LookInList(Scr->IconNames, tmp_win->full_name,
|
||
|
&tmp_win->classh);
|
||
|
|
||
|
bm = None;
|
||
|
if (icon_name != NULL)
|
||
|
{
|
||
|
if ((bm = (Pixmap)LookInNameList(Scr->Icons, icon_name)) == None)
|
||
|
{
|
||
|
if ((bm = GetBitmap (icon_name)) != None)
|
||
|
AddToList(&Scr->Icons, icon_name, (char *)bm);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (bm != None)
|
||
|
{
|
||
|
XGetGeometry(dpy, bm, &JunkRoot, &JunkX, &JunkY,
|
||
|
(unsigned int *) &tmp_win->icon_width, (unsigned int *)&tmp_win->icon_height,
|
||
|
&JunkBW, &JunkDepth);
|
||
|
|
||
|
pm = XCreatePixmap(dpy, Scr->Root, tmp_win->icon_width,
|
||
|
tmp_win->icon_height, Scr->d_depth);
|
||
|
|
||
|
/* the copy plane works on color ! */
|
||
|
XCopyPlane(dpy, bm, pm, Scr->NormalGC,
|
||
|
0,0, tmp_win->icon_width, tmp_win->icon_height, 0, 0, 1 );
|
||
|
|
||
|
tmp_win->forced = TRUE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* if the pixmap is still NULL, we didn't get one from the above code,
|
||
|
* that could mean that ForceIcon was not set, or that the window
|
||
|
* was not in the Icons list, now check the WM hints for an icon
|
||
|
*/
|
||
|
if (pm == None && tmp_win->wmhints &&
|
||
|
tmp_win->wmhints->flags & IconPixmapHint)
|
||
|
{
|
||
|
|
||
|
XGetGeometry(dpy, tmp_win->wmhints->icon_pixmap,
|
||
|
&JunkRoot, &JunkX, &JunkY,
|
||
|
(unsigned int *)&tmp_win->icon_width, (unsigned int *)&tmp_win->icon_height, &JunkBW, &JunkDepth);
|
||
|
|
||
|
pm = XCreatePixmap(dpy, Scr->Root,
|
||
|
tmp_win->icon_width, tmp_win->icon_height,
|
||
|
Scr->d_depth);
|
||
|
|
||
|
XCopyPlane(dpy, tmp_win->wmhints->icon_pixmap, pm, Scr->NormalGC,
|
||
|
0,0, tmp_win->icon_width, tmp_win->icon_height, 0, 0, 1 );
|
||
|
}
|
||
|
|
||
|
/* if we still haven't got an icon, let's look in the Icon list
|
||
|
* if ForceIcon is not set
|
||
|
*/
|
||
|
if (pm == None && !Scr->ForceIcon)
|
||
|
{
|
||
|
char *icon_name;
|
||
|
Pixmap bm;
|
||
|
|
||
|
icon_name = LookInNameList(Scr->IconNames, tmp_win->full_name);
|
||
|
if (icon_name == NULL)
|
||
|
icon_name = LookInList(Scr->IconNames, tmp_win->full_name,
|
||
|
&tmp_win->classh);
|
||
|
|
||
|
bm = None;
|
||
|
if (icon_name != NULL)
|
||
|
{
|
||
|
if ((bm = (Pixmap)LookInNameList(Scr->Icons, icon_name)) == None)
|
||
|
{
|
||
|
if ((bm = GetBitmap (icon_name)) != None)
|
||
|
AddToList(&Scr->Icons, icon_name, (char *)bm);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (bm != None)
|
||
|
{
|
||
|
XGetGeometry(dpy, bm, &JunkRoot, &JunkX, &JunkY,
|
||
|
(unsigned int *)&tmp_win->icon_width, (unsigned int *)&tmp_win->icon_height,
|
||
|
&JunkBW, &JunkDepth);
|
||
|
|
||
|
pm = XCreatePixmap(dpy, Scr->Root, tmp_win->icon_width,
|
||
|
tmp_win->icon_height, Scr->d_depth);
|
||
|
|
||
|
/* the copy plane works on color ! */
|
||
|
XCopyPlane(dpy, bm, pm, Scr->NormalGC,
|
||
|
0,0, tmp_win->icon_width, tmp_win->icon_height, 0, 0, 1 );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* if we still don't have an icon, assign the UnknownIcon */
|
||
|
|
||
|
if (pm == None && Scr->UnknownPm != None)
|
||
|
{
|
||
|
tmp_win->icon_width = Scr->UnknownWidth;
|
||
|
tmp_win->icon_height = Scr->UnknownHeight;
|
||
|
|
||
|
pm = XCreatePixmap(dpy, Scr->Root, tmp_win->icon_width,
|
||
|
tmp_win->icon_height, Scr->d_depth);
|
||
|
|
||
|
/* the copy plane works on color ! */
|
||
|
XCopyPlane(dpy, Scr->UnknownPm, pm, Scr->NormalGC,
|
||
|
0,0, tmp_win->icon_width, tmp_win->icon_height, 0, 0, 1 );
|
||
|
}
|
||
|
|
||
|
if (pm == None)
|
||
|
{
|
||
|
tmp_win->icon_height = 0;
|
||
|
tmp_win->icon_width = 0;
|
||
|
valuemask = 0;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
valuemask = CWBackPixmap;
|
||
|
attributes.background_pixmap = pm;
|
||
|
}
|
||
|
|
||
|
tmp_win->icon_w_width = MyFont_TextWidth(&Scr->IconFont,
|
||
|
tmp_win->icon_name, strlen(tmp_win->icon_name));
|
||
|
|
||
|
tmp_win->icon_w_width += 6;
|
||
|
if (tmp_win->icon_w_width < tmp_win->icon_width)
|
||
|
{
|
||
|
tmp_win->icon_x = (tmp_win->icon_width - tmp_win->icon_w_width)/2;
|
||
|
tmp_win->icon_x += 3;
|
||
|
tmp_win->icon_w_width = tmp_win->icon_width;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
tmp_win->icon_x = 3;
|
||
|
}
|
||
|
tmp_win->icon_y = tmp_win->icon_height + Scr->IconFont.height;
|
||
|
tmp_win->icon_w_height = tmp_win->icon_height + Scr->IconFont.height + 4;
|
||
|
|
||
|
event_mask = 0;
|
||
|
if (tmp_win->wmhints && tmp_win->wmhints->flags & IconWindowHint)
|
||
|
{
|
||
|
tmp_win->icon_w = tmp_win->wmhints->icon_window;
|
||
|
if (tmp_win->forced ||
|
||
|
XGetGeometry(dpy, tmp_win->icon_w, &JunkRoot, &JunkX, &JunkY,
|
||
|
(unsigned int *)&tmp_win->icon_w_width, (unsigned int *)&tmp_win->icon_w_height,
|
||
|
&JunkBW, &JunkDepth) == 0)
|
||
|
{
|
||
|
tmp_win->icon_w = None;
|
||
|
tmp_win->wmhints->flags &= ~IconWindowHint;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
tmp_win->icon_not_ours = TRUE;
|
||
|
event_mask = EnterWindowMask | LeaveWindowMask;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
tmp_win->icon_w = None;
|
||
|
}
|
||
|
|
||
|
if (tmp_win->icon_w == None)
|
||
|
{
|
||
|
tmp_win->icon_w = XCreateSimpleWindow(dpy, Scr->Root,
|
||
|
0,0,
|
||
|
tmp_win->icon_w_width, tmp_win->icon_w_height,
|
||
|
BW, tmp_win->icon_border, tmp_win->iconc.back);
|
||
|
event_mask = ExposureMask;
|
||
|
}
|
||
|
|
||
|
XSelectInput (dpy, tmp_win->icon_w,
|
||
|
KeyPressMask | ButtonPressMask | ButtonReleaseMask |
|
||
|
event_mask);
|
||
|
|
||
|
tmp_win->icon_bm_w = None;
|
||
|
if (pm != None &&
|
||
|
(! (tmp_win->wmhints && tmp_win->wmhints->flags & IconWindowHint)))
|
||
|
{
|
||
|
int y;
|
||
|
|
||
|
y = 0;
|
||
|
if (tmp_win->icon_w_width == tmp_win->icon_width)
|
||
|
x = 0;
|
||
|
else
|
||
|
x = (tmp_win->icon_w_width - tmp_win->icon_width)/2;
|
||
|
|
||
|
tmp_win->icon_bm_w = XCreateWindow (dpy, tmp_win->icon_w, x, y,
|
||
|
(unsigned int)tmp_win->icon_width,
|
||
|
(unsigned int)tmp_win->icon_height,
|
||
|
(unsigned int) 0, Scr->d_depth,
|
||
|
(unsigned int) CopyFromParent,
|
||
|
Scr->d_visual, valuemask,
|
||
|
&attributes);
|
||
|
}
|
||
|
|
||
|
/* I need to figure out where to put the icon window now, because
|
||
|
* getting here means that I am going to make the icon visible
|
||
|
*/
|
||
|
if (tmp_win->wmhints &&
|
||
|
tmp_win->wmhints->flags & IconPositionHint)
|
||
|
{
|
||
|
final_x = tmp_win->wmhints->icon_x;
|
||
|
final_y = tmp_win->wmhints->icon_y;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
PlaceIcon(tmp_win, def_x, def_y, &final_x, &final_y);
|
||
|
}
|
||
|
|
||
|
if (final_x > Scr->MyDisplayWidth)
|
||
|
final_x = Scr->MyDisplayWidth - tmp_win->icon_w_width - BW2;
|
||
|
|
||
|
if (final_y > Scr->MyDisplayHeight)
|
||
|
final_y = Scr->MyDisplayHeight - tmp_win->icon_height -
|
||
|
Scr->IconFont.height - 4 - BW2;
|
||
|
|
||
|
XMoveWindow(dpy, tmp_win->icon_w, final_x, final_y);
|
||
|
tmp_win->iconified = TRUE;
|
||
|
|
||
|
XMapSubwindows(dpy, tmp_win->icon_w);
|
||
|
XSaveContext(dpy, tmp_win->icon_w, TwmContext, (caddr_t)tmp_win);
|
||
|
XSaveContext(dpy, tmp_win->icon_w, ScreenContext, (caddr_t)Scr);
|
||
|
XDefineCursor(dpy, tmp_win->icon_w, Scr->IconCursor);
|
||
|
if (pm) XFreePixmap (dpy, pm);
|
||
|
return;
|
||
|
}
|