chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / coords.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "E.h"
24 #include "desktops.h"
25 #include "eobj.h"
26 #include "ewins.h"
27 #include "hints.h"
28 #include "iclass.h"
29 #include "tclass.h"
30 #include "timers.h"
31 #include "xwin.h"
32
33 static EObj        *coord_eo = NULL;
34
35 static void
36 _CoordsShow(EWin * ewin, int mode)
37 {
38    TextClass          *tc;
39    ImageClass         *ic;
40    char                s[256];
41    int                 md;
42    int                 x, y;
43    unsigned int        w, h;
44    int                 cx, cy, cw, ch;
45    EObj               *eo = coord_eo;
46    EImageBorder       *pad;
47    int                 bl, br, bt, bb;
48
49    if (!Conf.movres.mode_info)
50       return;
51    if (ewin == NULL || !ewin->state.show_coords)
52       return;
53
54    tc = TextclassFind("COORDS", 1);
55    ic = ImageclassFind("COORDS", 1);
56    if ((!ic) || (!tc))
57       return;
58
59    cx = cy = cw = ch = 0;
60
61    x = ewin->shape_x;
62    y = ewin->shape_y;
63    w = (ewin->state.shaded) ? ewin->client.w : ewin->shape_w;
64    h = (ewin->state.shaded) ? ewin->client.h : ewin->shape_h;
65    ICCCM_GetIncrementalSize(ewin, w, h, &w, &h);
66
67    switch (mode)
68      {
69      default:
70      case 0:
71         Esnprintf(s, sizeof(s), "%i x %i (%i, %i)", w, h, x, y);
72         break;
73      case 1:
74         Esnprintf(s, sizeof(s), _("Focused/unfocused opacity: %d/%d %%"),
75                   OpacityToPercent(ewin->props.focused_opacity),
76                   OpacityToPercent(ewin->ewmh.opacity));
77         break;
78      }
79    TextSize(tc, 0, 0, 0, s, &cw, &ch, 17);
80    pad = ImageclassGetPadding(ic);
81    cw += pad->left + pad->right;
82    ch += pad->top + pad->bottom;
83
84    /* Width hysteresis (hack - assuming horizontal text) */
85    cw += 8;
86    if (eo && abs(EobjGetW(eo) - cw) < 8)
87       cw = EobjGetW(eo);
88
89    if (Mode.mode == MODE_MOVE)
90       md = Conf.movres.mode_move;
91    else
92       md = Conf.movres.mode_resize;
93
94    if ((md == 0) || ((cw < ewin->shape_w - 2) && (ch < ewin->shape_h - 2)))
95      {
96         if (Conf.movres.mode_info == 1)
97           {
98              switch (md)
99                {
100                case 0:
101                case 1:
102                case 2:
103                   EwinBorderGetSize(ewin, &bl, &br, &bt, &bb);
104                   cx = x + (ewin->shape_w + bl + br - cw) / 2 +
105                      EoGetX(EoGetDesk(ewin));
106                   cy = y + (ewin->shape_h + bt + bb - ch) / 2 +
107                      EoGetY(EoGetDesk(ewin));
108                   break;
109                }
110           }
111      }
112
113    if (!eo)
114      {
115         eo = EobjWindowCreate(EOBJ_TYPE_MISC, 0, 0, 1, 1, 2, "Coord");
116         if (!eo)
117            return;
118         coord_eo = eo;
119         eo->fade = eo->shadow = 1;
120
121         /* Center text (override theme) */
122         TextclassSetJustification(tc, 512);
123      }
124
125 #define TEST_COORD_REPARENT_TO_FRAME 0
126 #if TEST_COORD_REPARENT_TO_FRAME
127    cx -= x;
128    cy -= y;
129 #endif
130    md = cw != EobjGetW(eo) || ch != EobjGetH(eo);       /* md is change size flag */
131    EobjMoveResize(eo, cx, cy, cw, ch);
132
133    if (!eo->shown)
134      {
135 #if TEST_COORD_REPARENT_TO_FRAME
136         EobjReparent(eo, EoObj(ewin), cx, cy);
137 #endif
138         EobjMap(eo, 0);
139      }
140
141    ITApply(EobjGetWin(eo), ic, NULL, STATE_NORMAL, 1, 0, ST_SOLID, tc, NULL, s,
142            1);
143
144    if (md)                      /* Assuming that shape change only happens when size changes too */
145       EobjShapeUpdate(eo, 0);
146
147    EFlush();
148 }
149
150 void
151 CoordsHide(void)
152 {
153    EObj               *eo = coord_eo;
154
155    if (eo && eo->shown)
156      {
157         EobjUnmap(eo);
158 #if TEST_COORD_REPARENT_TO_FRAME
159         EobjReparent(eo, EoObj(DeskGet(0)), 0, 0);
160 #endif
161      }
162 }
163
164 void
165 CoordsShow(EWin * ewin)
166 {
167    _CoordsShow(ewin, 0);
168 }
169
170 static Timer       *timer_show_op = NULL;
171
172 static int
173 _CoordsHideTimeout(void *data __UNUSED__)
174 {
175    CoordsHide();
176
177    timer_show_op = NULL;
178    return 0;
179 }
180
181 void
182 CoordsShowOpacity(EWin * ewin)
183 {
184    EwinShapeSet(ewin);
185    ewin->state.show_coords = 1;
186    _CoordsShow(ewin, 1);
187    TIMER_DEL(timer_show_op);
188    TIMER_ADD(timer_show_op, 1., _CoordsHideTimeout, NULL);
189 }