factorize mouse button press event handling for status bar
[awesome.git] / layouts / tile.c
blobd592d0efe3af7bf3e89164ff509ccbdb1e708acf
1 /*
2 * tile.c - tile layout
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
24 #include "util.h"
25 #include "screen.h"
26 #include "awesome.h"
27 #include "tag.h"
28 #include "layout.h"
29 #include "layouts/tile.h"
31 void
32 uicb_setnmaster(awesome_config *awesomeconf,
33 const char * arg)
35 Tag *curtag = get_current_tag(awesomeconf->tags, awesomeconf->ntags);
36 Layout *curlay = curtag->layout;
38 if(!arg || (curlay->arrange != layout_tile && curlay->arrange != layout_tileleft))
39 return;
41 if((curtag->nmaster = (int) compute_new_value_from_arg(arg, (double) curtag->nmaster)) < 0)
42 curtag->nmaster = 0;
44 arrange(awesomeconf);
47 void
48 uicb_setncol(awesome_config *awesomeconf,
49 const char * arg)
51 Tag *curtag = get_current_tag(awesomeconf->tags, awesomeconf->ntags);
52 Layout *curlay = curtag->layout;
54 if(!arg || (curlay->arrange != layout_tile && curlay->arrange != layout_tileleft))
55 return;
57 if((curtag->ncol = (int) compute_new_value_from_arg(arg, (double) curtag->ncol)) < 1)
58 curtag->ncol = 1;
60 arrange(awesomeconf);
63 void
64 uicb_setmwfact(awesome_config * awesomeconf,
65 const char *arg)
67 char *newarg;
68 Tag *curtag = get_current_tag(awesomeconf->tags, awesomeconf->ntags);
69 Layout *curlay = curtag->layout;
71 if(!arg || (curlay->arrange != layout_tile && curlay->arrange != layout_tileleft))
72 return;
74 newarg = a_strdup(arg);
75 if(curlay->arrange == layout_tileleft)
77 if(newarg[0] == '+')
78 newarg[0] = '-';
79 else if(arg[0] == '-')
80 newarg[0] = '+';
83 if((curtag->mwfact = compute_new_value_from_arg(newarg, curtag->mwfact)) < 0.1)
84 curtag->mwfact = 0.1;
85 else if(curtag->mwfact > 0.9)
86 curtag->mwfact = 0.9;
88 arrange(awesomeconf);
89 p_delete(&newarg);
92 static void
93 _tile(awesome_config *awesomeconf, const Bool right)
95 /* windows area geometry */
96 int wah = 0, waw = 0, wax = 0, way = 0;
97 /* new coordinates */
98 unsigned int nx, ny, nw, nh;
99 /* master size */
100 unsigned int mw = 0, mh = 0;
101 int n, i, masterwin = 0, otherwin = 0;
102 int real_ncol = 1, win_by_col = 1, current_col = 0;
103 ScreenInfo *screens_info = NULL;
104 Client *c;
105 Tag *curtag = get_current_tag(awesomeconf->tags, awesomeconf->ntags);
107 screens_info = get_screen_info(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar);
109 for(n = 0, c = *awesomeconf->clients; c; c = c->next)
110 if(IS_TILED(c, awesomeconf->screen, awesomeconf->tags, awesomeconf->ntags))
111 n++;
113 wah = screens_info[awesomeconf->screen].height;
114 waw = screens_info[awesomeconf->screen].width;
115 wax = screens_info[awesomeconf->screen].x_org;
116 way = screens_info[awesomeconf->screen].y_org;
118 masterwin = MIN(n, curtag->nmaster);
120 otherwin = n - masterwin;
122 if(otherwin < 0)
123 otherwin = 0;
125 if(curtag->nmaster)
127 mh = masterwin ? wah / masterwin : waw;
128 mw = otherwin ? waw * curtag->mwfact : waw;
130 else
131 mh = mw = 0;
133 real_ncol = MIN(otherwin, curtag->ncol);
135 for(i = 0, c = *awesomeconf->clients; c; c = c->next)
137 if(!IS_TILED(c, awesomeconf->screen, awesomeconf->tags, awesomeconf->ntags))
138 continue;
140 c->ismax = False;
141 if(i < curtag->nmaster)
142 { /* master */
143 ny = way + i * mh;
144 nx = wax + (right ? 0 : waw - mw);
145 client_resize(c, nx, ny, mw - 2 * c->border, mh - 2 * c->border, awesomeconf, awesomeconf->resize_hints, False);
147 else
148 { /* tile window */
149 if(real_ncol)
150 win_by_col = otherwin / real_ncol;
152 if((i - curtag->nmaster) && (i - curtag->nmaster) % win_by_col == 0 && current_col < real_ncol - 1)
153 current_col++;
155 if(current_col == real_ncol - 1)
156 win_by_col += otherwin % real_ncol;
158 if(otherwin <= real_ncol)
159 nh = wah - 2 * c->border;
160 else
161 nh = (wah / win_by_col) - 2 * c->border;
163 nw = (waw - mw) / real_ncol - 2 * c->border;
165 if(i == curtag->nmaster || otherwin <= real_ncol || (i - curtag->nmaster) % win_by_col == 0)
166 ny = way;
167 else
168 ny = way + ((i - curtag->nmaster) % win_by_col) * (nh + 2 * c->border);
170 nx = wax + current_col * (nw + 2 * c->border) + (right ? mw : 0);
171 client_resize(c, nx, ny, nw, nh, awesomeconf, awesomeconf->resize_hints, False);
173 i++;
175 p_delete(&screens_info);
178 void
179 layout_tile(awesome_config *awesomeconf)
181 _tile(awesomeconf, True);
184 void
185 layout_tileleft(awesome_config *awesomeconf)
187 _tile(awesomeconf, False);
189 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99