summaryrefslogtreecommitdiff
path: root/src/bin/e_maximize.c
blob: d6737d42c4e210833ddd4080c48bcbc97ad28781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "e.h"

typedef struct _E_Maximize_Rect E_Maximize_Rect;

struct _E_Maximize_Rect
{
   int x1, yy1, x2, y2;
};

#define OBSTACLE(_x1, _y1, _x2, _y2)                              \
  {                                                               \
     r = E_NEW(E_Maximize_Rect, 1);                               \
     r->x1 = (_x1); r->yy1 = (_y1); r->x2 = (_x2); r->y2 = (_y2); \
     rects = eina_list_append(rects, r);                          \
  }

static void _e_maximize_client_rects_fill(const E_Client *ec, Eina_List *list, int *x1, int *yy1, int *x2, int *y2, E_Maximize dir);
static void _e_maximize_client_rects_fill_both(const E_Client *ec, Eina_List *rects, int *x1, int *yy1, int *x2, int *y2);
static void _e_maximize_client_rects_fill_horiz(const E_Client *ec, Eina_List *rects, int *x1, int *x2, int *bx, int *by, int *bw, int *bh);
static void _e_maximize_client_rects_fill_vert(const E_Client *ec, Eina_List *rects, int *yy1, int *y2, int *bx, int *by, int *bw, int *bh);

E_API void
e_maximize_client_shelf_fit(const E_Client *ec, int *x1, int *yy1, int *x2, int *y2, E_Maximize dir)
{
   e_maximize_client_shelf_fill(ec, x1, yy1, x2, y2, dir);
}

E_API void
e_maximize_client_dock_fit(const E_Client *ec, int *x1, int *yy1, int *x2, int *y2)
{
   E_Client *ec2;
   int cx1, cx2, cy1, cy2;

   cx1 = ec->zone->x;
   if (x1) cx1 = *x1;

   cy1 = ec->zone->y;
   if (yy1) cy1 = *yy1;

   cx2 = ec->zone->x + ec->zone->w;
   if (x2) cx2 = *x2;

   cy2 = ec->zone->y + ec->zone->h;
   if (y2) cy2 = *y2;

   E_CLIENT_FOREACH(ec2)
     {
        enum
        {
           NONE,
           TOP,
           RIGHT,
           BOTTOM,
           LEFT
        } edge = NONE;

        if ((ec2->zone != ec->zone) || (ec2 == ec) ||
            (ec2->netwm.type != E_WINDOW_TYPE_DOCK))
          continue;

        if (((ec2->x == ec2->zone->x) || ((ec2->x + ec2->w) == (ec2->zone->x + ec2->zone->w))) &&
            ((ec2->y == ec2->zone->y) || ((ec2->x + ec2->h) == (ec2->zone->x + ec2->zone->h))))
          {
             /* corner */
             if (ec2->w > ec2->h)
               {
                  if (ec2->y == ec2->zone->y)
                    edge = TOP;
                  else if ((ec2->y + ec2->h) == (ec2->zone->y + ec2->zone->h))
                    edge = BOTTOM;
               }
             else
               {
                  if ((ec2->x + ec2->w) == (ec2->zone->x + ec2->zone->w))
                    edge = RIGHT;
                  else if (ec2->x == ec2->zone->x)
                    edge = LEFT;
               }
          }
        else
          {
             if (ec2->y == ec2->zone->y)
               edge = TOP;
             else if ((ec2->y + ec2->h) == (ec2->zone->y + ec2->zone->h))
               edge = BOTTOM;
             else if (ec2->x == ec2->zone->x)
               edge = LEFT;
             else if ((ec2->x + ec2->w) == (ec2->zone->x + ec2->zone->w))
               edge = RIGHT;
          }

        switch (edge)
          {
           case TOP:
             if ((ec2->y + ec2->h) > cy1)
               cy1 = (ec2->y + ec2->h);
             break;

           case RIGHT:
             if (ec2->x < cx2)
               cx2 = ec2->x;
             break;

           case BOTTOM:
             if (ec2->y < cy2)
               cy2 = ec2->y;
             break;

           case LEFT:
             if ((ec2->x + ec2->w) > cx1)
               cx1 = (ec2->x + ec2->w);
             break;

           case NONE:
             printf("Crazy people. Dock isn't at the edge.\n");
             break;
          }
     }

   if (x1) *x1 = cx1;
   if (yy1) *yy1 = cy1;
   if (x2) *x2 = cx2;
   if (y2) *y2 = cy2;
}

E_API void
e_maximize_client_shelf_fill(const E_Client *ec, int *x1, int *yy1, int *x2, int *y2, E_Maximize dir)
{
   Eina_List *l, *rects = NULL;
   E_Shelf *es;
   E_Maximize_Rect *r;

   l = e_shelf_list_all();
   EINA_LIST_FREE(l, es)
     {
        if (es->cfg->overlap) continue;
        if (!e_shelf_desk_visible(es, ec->desk)) continue;
        OBSTACLE(es->x + es->zone->x, es->y + es->zone->y,
                 es->x + es->zone->x + es->w, es->y + es->zone->y + es->h);
     }
   if (rects)
     {
        _e_maximize_client_rects_fill(ec, rects, x1, yy1, x2, y2, dir);
        E_FREE_LIST(rects, free);
     }
}

E_API void
e_maximize_client_client_fill(const E_Client *ec, int *x1, int *yy1, int *x2, int *y2, E_Maximize dir)
{
   Eina_List *rects = NULL;
   E_Maximize_Rect *r;
   E_Client *ec2;

   E_CLIENT_FOREACH(ec2)
     {
        if ((ec2->zone != ec->zone) || (ec == ec2) || (ec2->desk != ec->desk && !ec2->sticky) || (ec2->iconic))
          continue;
        OBSTACLE(ec2->x, ec2->y, ec2->x + ec2->w, ec2->y + ec2->h);
     }
   if (rects)
     {
        _e_maximize_client_rects_fill(ec, rects, x1, yy1, x2, y2, dir);
        E_FREE_LIST(rects, free);
     }
}

static void
_e_maximize_client_rects_fill(const E_Client *ec, Eina_List *rects, int *x1, int *yy1, int *x2, int *y2, E_Maximize dir)
{
   if ((dir & E_MAXIMIZE_DIRECTION) == E_MAXIMIZE_BOTH)
     {
        _e_maximize_client_rects_fill_both(ec, rects, x1, yy1, x2, y2);
     }
   else
     {
        int bx, by, bw, bh;

        bx = E_CLAMP(ec->x, ec->zone->x, ec->zone->x + ec->zone->w);
        by = E_CLAMP(ec->y, ec->zone->y, ec->zone->y + ec->zone->h);
        bw = E_CLAMP(ec->w, 0, ec->zone->w);
        bh = E_CLAMP(ec->h, 0, ec->zone->h);

        if ((dir & E_MAXIMIZE_DIRECTION) == E_MAXIMIZE_HORIZONTAL)
          _e_maximize_client_rects_fill_horiz(ec, rects, x1, x2, &bx, &by, &bw, &bh);
        else if (((dir & E_MAXIMIZE_DIRECTION) == E_MAXIMIZE_VERTICAL) ||
                 ((dir & E_MAXIMIZE_LEFT) == E_MAXIMIZE_LEFT) ||
                 ((dir & E_MAXIMIZE_RIGHT) == E_MAXIMIZE_RIGHT))
          _e_maximize_client_rects_fill_vert(ec, rects, yy1, y2, &bx, &by, &bw, &bh);
     }
}

static void
_e_maximize_client_rects_fill_both(const E_Client *ec, Eina_List *rects, int *x1, int *yy1, int *x2, int *y2)
{
   int hx1, hy1, hx2, hy2;
   int vx1, vy1, vx2, vy2;
   int bx, by, bw, bh;

   hx1 = vx1 = ec->zone->x;
   if (x1) hx1 = vx1 = *x1;

   hy1 = vy1 = ec->zone->y;
   if (yy1) hy1 = vy1 = *yy1;

   hx2 = vx2 = ec->zone->x + ec->zone->w;
   if (x2) hx2 = vx2 = *x2;

   hy2 = vy2 = ec->zone->y + ec->zone->h;
   if (y2) hy2 = vy2 = *y2;

   /* Init working values, try maximizing horizontally first */
   bx = ec->saved.x ?: ec->x;
   by = ec->saved.y ?: ec->y;
   bw = ec->saved.w ?: ec->w;
   bh = ec->saved.h ?: ec->h;
   _e_maximize_client_rects_fill_horiz(ec, rects, &hx1, &hx2, &bx, &by, &bw, &bh);
   _e_maximize_client_rects_fill_vert(ec, rects, &hy1, &hy2, &bx, &by, &bw, &bh);

   /* Reset working values, try maximizing vertically first */
   bx = ec->saved.x ?: ec->x;
   by = ec->saved.y ?: ec->y;
   bw = ec->saved.w ?: ec->w;
   bh = ec->saved.h ?: ec->h;
   _e_maximize_client_rects_fill_vert(ec, rects, &vy1, &vy2, &bx, &by, &bw, &bh);
   _e_maximize_client_rects_fill_horiz(ec, rects, &vx1, &vx2, &bx, &by, &bw, &bh);

   /* Use the result set that gives the largest volume */
   if (((hx2 - hx1) * (hy2 - hy1)) > ((vx2 - vx1) * (vy2 - vy1)))
     {
        if (x1) *x1 = hx1;
        if (yy1) *yy1 = hy1;
        if (x2) *x2 = hx2;
        if (y2) *y2 = hy2;
     }
   else
     {
        if (x1) *x1 = vx1;
        if (yy1) *yy1 = vy1;
        if (x2) *x2 = vx2;
        if (y2) *y2 = vy2;
     }
}

static void
_e_maximize_client_rects_fill_horiz(const E_Client *ec, Eina_List *rects, int *x1, int *x2, int *bx, int *by, int *bw, int *bh)
{
   Eina_List *l;
   E_Maximize_Rect *rect;
   int cx1, cx2;

   cx1 = ec->zone->x;
   if (x1) cx1 = *x1;

   cx2 = ec->zone->x + ec->zone->w;
   if (x2) cx2 = *x2;

   /* Expand left */
   EINA_LIST_FOREACH(rects, l, rect)
     {
        if ((rect->x2 > cx1) && (rect->x2 <= *bx) &&
            E_INTERSECTS(0, rect->yy1, ec->zone->w, (rect->y2 - rect->yy1), 0, *by, ec->zone->w, *bh))
          {
             cx1 = rect->x2;
          }
     }
   *bw += (*bx - cx1);
   *bx = cx1;

   /* Expand right */
   EINA_LIST_FOREACH(rects, l, rect)
     {
        if ((rect->x1 < cx2) && (rect->x1 >= (*bx + *bw)) &&
            E_INTERSECTS(0, rect->yy1, ec->zone->w, (rect->y2 - rect->yy1), 0, *by, ec->zone->w, *bh))
          {
             cx2 = rect->x1;
          }
     }
   *bw = (cx2 - cx1);

   if (x1) *x1 = cx1;
   if (x2) *x2 = cx2;
}

static void
_e_maximize_client_rects_fill_vert(const E_Client *ec, Eina_List *rects, int *yy1, int *y2, int *bx, int *by, int *bw, int *bh)
{
   Eina_List *l;
   E_Maximize_Rect *rect;
   int cy1, cy2;

   cy1 = ec->zone->y;
   if (yy1) cy1 = *yy1;

   cy2 = ec->zone->y + ec->zone->h;
   if (y2) cy2 = *y2;

   /* Expand up */
   EINA_LIST_FOREACH(rects, l, rect)
     {
        if ((rect->y2 > cy1) && ((rect->yy1 <= *by) || (rect->y2 <= *by)) &&
            E_INTERSECTS(rect->x1, 0, (rect->x2 - rect->x1), ec->zone->h, *bx, 0, *bw, ec->zone->h))
          {
             cy1 = rect->y2;
          }
     }
   *bh += (*by - cy1);
   *by = cy1;

   /* Expand down */
   EINA_LIST_FOREACH(rects, l, rect)
     {
        if ((rect->yy1 < cy2) && (rect->yy1 >= (*by + *bh)) &&
            E_INTERSECTS(rect->x1, 0, (rect->x2 - rect->x1), ec->zone->h, *bx, 0, *bw, ec->zone->h))
          {
             cy2 = rect->yy1;
          }
     }
   *bh = (cy2 - cy1);

   if (yy1) *yy1 = cy1;
   if (y2) *y2 = cy2;
}