twmruined/src/icons.cc

376 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.
*
* 07-Nov-20 Mike Small Switch to C++, simplify placement.
**********************************************************************/
#include <algorithm>
#include <vector>
#include "twm.h"
#include "screen.h"
#include "icons.h"
#include "gram.h"
#include "parse.h"
#include "util.h"
// DEBUG ONLY
#include <iostream>
using namespace std;
using Coord = pair<int, int>;
const int icon_area_w = 70;
const int icon_pad = 2;
#define iconWidth(w) (BW2 + w->icon_w_width)
#define iconHeight(w) (BW2 + w->icon_w_height)
static Coord
PlaceIcon(const TwmWindow& win, int def_x, int def_y)
{
// icons are dropped to the right of this x offset.
const int icon_area_x {Scr->MyDisplayWidth - icon_area_w};
const int height {win.icon_w_height ? win.icon_w_height : 64};
// always place xconsole's icon on the lower right.
if (strcmp(win.name, "xconsole") == 0)
return {icon_area_x + icon_pad, Scr->MyDisplayHeight - height};
using Span = pair<int,int>;
vector<Span> occupied;
for (TwmWindow* pw = Scr->TwmRoot.next; pw; pw = pw->next) {
// Iconified means it was iconified at some point.
if (pw->iconified) {
Window root;
int icon_x, icon_y;
unsigned icon_w, icon_h, border_width, depth;
if (pw->icon_w
&& XGetGeometry(dpy, pw->icon_w, &root,
&icon_x, &icon_y, &icon_w, &icon_h,
&border_width, &depth)) {
if (icon_x >= icon_area_x)
occupied.push_back({icon_y, icon_y + static_cast<int>(icon_h)});
}
}
else {
// A small non-iconified window sitting where an icon would? E.g. xclock.
// non-iconified windows that look like icons are assumed never to have
// been iconified.
if (pw->frame_x >= icon_area_x
&& pw->frame_width <= 150 && pw->frame_height <= 150) {
occupied.push_back({pw->frame_y, pw->frame_y + pw->frame_height});
}
}
}
sort(occupied.begin(), occupied.end());
// The loop below looks for a gap in the icon area. Add a sentinel to represent
// the end of the open area, if any, after all icons.
occupied.push_back({Scr->MyDisplayHeight + icon_pad,
Scr->MyDisplayHeight + 2 * icon_pad});
int y {0}, prev {0};
for (const auto span : occupied) {
const int gap = span.first - prev;
if (gap >= height + icon_pad) {
y = prev + icon_pad;
break;
}
else if (gap >= height) {
y = min(prev, 1);
break;
}
prev = span.second;
}
Coord icon_pos { def_x, def_y };
if (y != 0) {
const int w {win.icon_w_width ? win.icon_w_width : 64};
const int offset { (icon_area_w - w) / 2 };
icon_pos.first = icon_area_x + (offset > 0 ? offset : icon_pad);
icon_pos.second = y;
}
else {
cerr << win.name << ": y == 0, using default x and y of { "
<< def_x << ", " << def_y << " }\n";
}
return icon_pos;
}
void
CreateIconWindow(TwmWindow* 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 */
FB(win->iconc.fore, win->iconc.back);
win->forced = FALSE;
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, win->full_name);
if (icon_name == NULL)
icon_name = LookInList(Scr->IconNames, win->full_name,
&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 *) &win->icon_width, (unsigned int *)&win->icon_height,
&JunkBW, &JunkDepth);
pm = XCreatePixmap(dpy, Scr->Root, win->icon_width,
win->icon_height, Scr->d_depth);
/* the copy plane works on color ! */
XCopyPlane(dpy, bm, pm, Scr->NormalGC,
0,0, win->icon_width, win->icon_height, 0, 0, 1 );
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 && win->wmhints &&
win->wmhints->flags & IconPixmapHint)
{
XGetGeometry(dpy, win->wmhints->icon_pixmap,
&JunkRoot, &JunkX, &JunkY,
(unsigned int *)&win->icon_width, (unsigned int *)&win->icon_height, &JunkBW, &JunkDepth);
pm = XCreatePixmap(dpy, Scr->Root,
win->icon_width, win->icon_height,
Scr->d_depth);
XCopyPlane(dpy, win->wmhints->icon_pixmap, pm, Scr->NormalGC,
0,0, win->icon_width, 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, win->full_name);
if (icon_name == NULL)
icon_name = LookInList(Scr->IconNames, win->full_name,
&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 *)&win->icon_width, (unsigned int *)&win->icon_height,
&JunkBW, &JunkDepth);
pm = XCreatePixmap(dpy, Scr->Root, win->icon_width,
win->icon_height, Scr->d_depth);
/* the copy plane works on color ! */
XCopyPlane(dpy, bm, pm, Scr->NormalGC,
0,0, win->icon_width, win->icon_height, 0, 0, 1 );
}
}
/* if we still don't have an icon, assign the UnknownIcon */
if (pm == None && Scr->UnknownPm != None)
{
win->icon_width = Scr->UnknownWidth;
win->icon_height = Scr->UnknownHeight;
pm = XCreatePixmap(dpy, Scr->Root, win->icon_width,
win->icon_height, Scr->d_depth);
/* the copy plane works on color ! */
XCopyPlane(dpy, Scr->UnknownPm, pm, Scr->NormalGC,
0,0, win->icon_width, win->icon_height, 0, 0, 1 );
}
if (pm == None)
{
win->icon_height = 0;
win->icon_width = 0;
valuemask = 0;
}
else
{
valuemask = CWBackPixmap;
attributes.background_pixmap = pm;
}
win->icon_w_width = MyFont_TextWidth(&Scr->IconFont,
win->icon_name, strlen(win->icon_name));
win->icon_w_width += 6;
if (win->icon_w_width < win->icon_width)
{
win->icon_x = (win->icon_width - win->icon_w_width)/2;
win->icon_x += 3;
win->icon_w_width = win->icon_width;
}
else
{
win->icon_x = 3;
}
win->icon_y = win->icon_height + Scr->IconFont.height;
win->icon_w_height = win->icon_height + Scr->IconFont.height + 4;
event_mask = 0;
if (win->wmhints && win->wmhints->flags & IconWindowHint)
{
win->icon_w = win->wmhints->icon_window;
if (win->forced ||
XGetGeometry(dpy, win->icon_w, &JunkRoot, &JunkX, &JunkY,
(unsigned int *)&win->icon_w_width, (unsigned int *)&win->icon_w_height,
&JunkBW, &JunkDepth) == 0)
{
win->icon_w = None;
win->wmhints->flags &= ~IconWindowHint;
}
else
{
win->icon_not_ours = TRUE;
event_mask = EnterWindowMask | LeaveWindowMask;
}
}
else
{
win->icon_w = None;
}
if (win->icon_w == None)
{
win->icon_w = XCreateSimpleWindow(dpy, Scr->Root,
0,0,
win->icon_w_width, win->icon_w_height,
BW, win->icon_border, win->iconc.back);
event_mask = ExposureMask;
}
XSelectInput (dpy, win->icon_w,
KeyPressMask | ButtonPressMask | ButtonReleaseMask |
event_mask);
win->icon_bm_w = None;
if (pm != None &&
(! (win->wmhints && win->wmhints->flags & IconWindowHint)))
{
int x, y;
y = 0;
if (win->icon_w_width == win->icon_width)
x = 0;
else
x = (win->icon_w_width - win->icon_width)/2;
win->icon_bm_w = XCreateWindow (dpy, win->icon_w, x, y,
(unsigned int)win->icon_width,
(unsigned int)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
*/
Coord icon_pos;
if (win->wmhints &&
win->wmhints->flags & IconPositionHint)
{
icon_pos = {win->wmhints->icon_x, win->wmhints->icon_y};
}
else
{
icon_pos = PlaceIcon(*win, def_x, def_y);
}
if (icon_pos.first > Scr->MyDisplayWidth)
icon_pos.first = Scr->MyDisplayWidth - win->icon_w_width - BW2;
if (icon_pos.second > Scr->MyDisplayHeight)
icon_pos.second = Scr->MyDisplayHeight - win->icon_height
- Scr->IconFont.height - 4 - BW2;
XMoveWindow(dpy, win->icon_w, icon_pos.first, icon_pos.second);
win->iconified = TRUE;
XMapSubwindows(dpy, win->icon_w);
XSaveContext(dpy, win->icon_w, TwmContext, (caddr_t)win);
XSaveContext(dpy, win->icon_w, ScreenContext, (caddr_t)Scr);
XDefineCursor(dpy, win->icon_w, Scr->IconCursor);
if (pm)
XFreePixmap(dpy, pm);
}