Skip to content

Commit e2117b5

Browse files
committed
filemanager/chown: refactoring.
* (chown_but): don't store button width. Instead, call button_get_width() where needed. Store pointer to button. * (chown_init): sync with modified chown_but. * (chown_buttons_create): new function to create buttons in one line. * (chown_dlg_create): use chown_buttons_create(). Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
1 parent 29722a0 commit e2117b5

File tree

1 file changed

+40
-52
lines changed

1 file changed

+40
-52
lines changed

src/filemanager/chown.c

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,16 @@ static struct
7777
int ret_cmd;
7878
button_flags_t flags;
7979
int y;
80-
int len;
8180
const char *text;
81+
WButton *button;
8282
} chown_but[BUTTONS] = {
83-
{ B_SETALL, NORMAL_BUTTON, 5, 0, N_ ("Set &all") },
84-
{ B_SETGRP, NORMAL_BUTTON, 5, 0, N_ ("Set &groups") },
85-
{ B_SETUSR, NORMAL_BUTTON, 5, 0, N_ ("Set &users") },
86-
{ B_ENTER, DEFPUSH_BUTTON, 3, 0, N_ ("&Set") },
87-
{ B_CANCEL, NORMAL_BUTTON, 3, 0, N_ ("&Cancel") },
83+
{ B_SETALL, NORMAL_BUTTON, 5, N_ ("Set &all"), NULL },
84+
{ B_SETGRP, NORMAL_BUTTON, 5, N_ ("Set &groups"), NULL },
85+
{ B_SETUSR, NORMAL_BUTTON, 5, N_ ("Set &users"), NULL },
86+
{ B_ENTER, DEFPUSH_BUTTON, 3, N_ ("&Set"), NULL },
87+
{ B_CANCEL, NORMAL_BUTTON, 3, N_ ("&Cancel"), NULL },
8888
};
8989

90-
/* summary length of three buttons */
91-
static int blen = 0;
92-
9390
static struct
9491
{
9592
int y;
@@ -111,29 +108,16 @@ static void
111108
chown_init (void)
112109
{
113110
static gboolean i18n = FALSE;
114-
int i;
115111

116112
if (i18n)
117113
return;
118114

119115
i18n = TRUE;
120116

121117
#ifdef ENABLE_NLS
122-
for (i = 0; i < BUTTONS; i++)
118+
for (int i = 0; i < BUTTONS; i++)
123119
chown_but[i].text = _ (chown_but[i].text);
124120
#endif
125-
126-
for (i = 0; i < BUTTONS; i++)
127-
{
128-
chown_but[i].len = str_term_width1 (chown_but[i].text) + 3; // [], spaces and w/o &
129-
if (chown_but[i].flags == DEFPUSH_BUTTON)
130-
chown_but[i].len += 2; // <>
131-
132-
if (i < BUTTONS - 2)
133-
blen += chown_but[i].len;
134-
}
135-
136-
blen += 2;
137121
}
138122

139123
/* --------------------------------------------------------------------------------------------- */
@@ -177,14 +161,43 @@ chown_bg_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *
177161

178162
/* --------------------------------------------------------------------------------------------- */
179163

164+
static void
165+
chown_buttons_create (WGroup *g, const int first, const int last)
166+
{
167+
int i;
168+
int blen = 0;
169+
int x;
170+
171+
const int y = WIDGET (g)->rect.lines - chown_but[first].y;
172+
173+
group_add_widget (g, hline_new (y - 1, -1, -1));
174+
175+
for (i = first; i <= last; i++)
176+
{
177+
chown_but[i].button =
178+
button_new (y, 1, chown_but[i].ret_cmd, chown_but[i].flags, chown_but[i].text, NULL);
179+
blen += button_get_width (chown_but[i].button) + 1;
180+
group_add_widget (g, chown_but[i].button);
181+
}
182+
183+
x = WIDGET (g)->rect.x + (WIDGET (g)->rect.cols - blen) / 2;
184+
185+
for (i = first; i <= last; i++)
186+
{
187+
WIDGET (chown_but[i].button)->rect.x = x;
188+
x += button_get_width (chown_but[i].button) + 1;
189+
}
190+
}
191+
192+
/* --------------------------------------------------------------------------------------------- */
193+
180194
static WDialog *
181195
chown_dlg_create (WPanel *panel)
182196
{
183197
int single_set;
184198
WDialog *ch_dlg;
185199
WGroup *g;
186200
int lines, cols;
187-
int i, y;
188201
struct passwd *l_pass;
189202
struct group *l_grp;
190203

@@ -223,41 +236,16 @@ chown_dlg_create (WPanel *panel)
223236

224237
group_add_widget (g, groupbox_new (2, 5 + GW * 2, GH, GW, _ ("File")));
225238
// add widgets for the file information
226-
for (i = 0; i < LABELS; i++)
239+
for (int i = 0; i < LABELS; i++)
227240
{
228241
chown_label[i].l = label_new (chown_label[i].y, 7 + GW * 2, NULL);
229242
group_add_widget (g, chown_label[i].l);
230243
}
231244

232245
if (single_set == 0)
233-
{
234-
int x;
235-
236-
group_add_widget (g, hline_new (lines - chown_but[0].y - 1, -1, -1));
237-
238-
y = lines - chown_but[0].y;
239-
x = (cols - blen) / 2;
240-
241-
for (i = 0; i < BUTTONS - 2; i++)
242-
{
243-
group_add_widget (g,
244-
button_new (y, x, chown_but[i].ret_cmd, chown_but[i].flags,
245-
chown_but[i].text, NULL));
246-
x += chown_but[i].len + 1;
247-
}
248-
}
246+
chown_buttons_create (g, 0, BUTTONS - 3);
249247

250-
i = BUTTONS - 2;
251-
y = lines - chown_but[i].y;
252-
group_add_widget (g, hline_new (y - 1, -1, -1));
253-
group_add_widget (g,
254-
button_new (y, WIDGET (ch_dlg)->rect.cols / 2 - chown_but[i].len,
255-
chown_but[i].ret_cmd, chown_but[i].flags, chown_but[i].text,
256-
NULL));
257-
i++;
258-
group_add_widget (g,
259-
button_new (y, WIDGET (ch_dlg)->rect.cols / 2 + 1, chown_but[i].ret_cmd,
260-
chown_but[i].flags, chown_but[i].text, NULL));
248+
chown_buttons_create (g, BUTTONS - 2, BUTTONS - 1);
261249

262250
// select first listbox
263251
widget_select (WIDGET (l_user));

0 commit comments

Comments
 (0)