ECMAScript: added writeonly property window.status
[elinks.git] / src / dialogs / status.c
blob3c37acefe349e7e8390ec98f7b85643299b95c5d
1 /* Sessions status management */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "bfu/dialog.h"
12 #include "cache/cache.h"
13 #include "config/options.h"
14 #include "dialogs/progress.h"
15 #include "dialogs/status.h"
16 #include "document/document.h"
17 #include "document/renderer.h"
18 #include "document/view.h"
19 #include "intl/gettext/libintl.h"
20 #include "network/connection.h"
21 #include "network/progress.h"
22 #include "network/state.h"
23 #include "protocol/bittorrent/dialogs.h"
24 #include "protocol/protocol.h"
25 #include "protocol/uri.h"
26 #include "session/download.h"
27 #include "session/session.h"
28 #include "terminal/draw.h"
29 #include "terminal/screen.h"
30 #include "terminal/tab.h"
31 #include "terminal/terminal.h"
32 #include "terminal/window.h"
33 #include "util/color.h"
34 #include "util/conv.h"
35 #include "util/error.h"
36 #include "util/memory.h"
37 #include "util/snprintf.h"
38 #include "util/string.h"
39 #include "viewer/text/form.h"
40 #include "viewer/text/link.h"
41 #include "viewer/text/view.h"
44 unsigned char *
45 get_download_msg(struct download *download, struct terminal *term,
46 int wide, int full, unsigned char *separator)
48 if (!download_is_progressing(download)) {
49 /* DBG("%d -> %s", download->state, _(get_err_msg(download->state), term)); */
50 return stracpy(get_state_message(download->state, term));
53 #ifdef CONFIG_BITTORRENT
54 if (download->conn
55 && download->conn->uri->protocol == PROTOCOL_BITTORRENT)
56 return get_bittorrent_message(download, term, wide, full, separator);
57 #endif
59 return get_progress_msg(download->progress, term, wide, full, separator);
62 #define show_tabs(option, tabs) (((option) > 0) && !((option) == 1 && (tabs) < 2))
64 void
65 update_status(void)
67 int show_title_bar = get_opt_bool("ui.show_title_bar");
68 int show_status_bar = get_opt_bool("ui.show_status_bar");
69 int show_tabs_bar = get_opt_int("ui.tabs.show_bar");
70 int show_tabs_bar_at_top = get_opt_bool("ui.tabs.top");
71 #ifdef CONFIG_LEDS
72 int show_leds = get_opt_bool("ui.leds.enable");
73 #endif
74 int set_window_title = get_opt_bool("ui.window_title");
75 int insert_mode = get_opt_bool("document.browse.forms.insert_mode");
76 struct session *ses;
77 int tabs_count = 1;
78 struct terminal *term = NULL;
80 foreach (ses, sessions) {
81 struct session_status *status = &ses->status;
82 int dirty = 0;
84 /* Try to descrease the number of tab calculation using that
85 * tab sessions share the same term. */
86 if (ses->tab->term != term) {
87 term = ses->tab->term;
88 tabs_count = number_of_tabs(term);
91 if (status->force_show_title_bar >= 0)
92 show_title_bar = status->force_show_title_bar;
93 if (status->show_title_bar != show_title_bar) {
94 status->show_title_bar = show_title_bar;
95 dirty = 1;
98 if (status->force_show_status_bar >= 0)
99 show_status_bar = status->force_show_status_bar;
100 if (status->show_status_bar != show_status_bar) {
101 status->show_status_bar = show_status_bar;
102 dirty = 1;
105 if (show_tabs(show_tabs_bar, tabs_count) != status->show_tabs_bar) {
106 status->show_tabs_bar = show_tabs(show_tabs_bar, tabs_count);
107 dirty = 1;
110 if (status->show_tabs_bar) {
111 if (status->show_tabs_bar_at_top != show_tabs_bar_at_top) {
112 status->show_tabs_bar_at_top = show_tabs_bar_at_top;
113 dirty = 1;
116 #ifdef CONFIG_LEDS
117 if (status->show_leds != show_leds) {
118 status->show_leds = show_leds;
119 dirty = 1;
121 #endif
123 status->set_window_title = set_window_title;
125 /* This more belongs to the current browsing state but ... */
126 if (!insert_mode)
127 ses->insert_mode = INSERT_MODE_LESS;
128 else if (ses->insert_mode == INSERT_MODE_LESS)
129 ses->insert_mode = INSERT_MODE_OFF;
131 if (!dirty) continue;
133 /* Force the current document to be rerendered so the
134 * document view and document height is updated to fit
135 * into the new dimensions. Related to bug 87. */
136 render_document_frames(ses, 1);
138 set_screen_dirty(term->screen, 0, term->height);
142 static unsigned char *
143 get_current_link_info_and_title(struct session *ses,
144 struct document_view *doc_view)
146 unsigned char *link_info, *link_title, *ret = NULL;
148 link_info = get_current_link_info(ses, doc_view);
149 if (!link_info) return NULL;
151 link_title = get_current_link_title(doc_view);
152 if (link_title) {
153 assert(*link_title);
155 ret = straconcat(link_info, " - ", link_title, NULL);
156 mem_free(link_info);
157 mem_free(link_title);
160 if (!ret) ret = link_info;
162 return ret;
165 static inline void
166 display_status_bar(struct session *ses, struct terminal *term, int tabs_count)
168 unsigned char *msg = NULL;
169 unsigned int tab_info_len = 0;
170 struct download *download = get_current_download(ses);
171 struct session_status *status = &ses->status;
172 struct color_pair *text_color = NULL;
173 int msglen;
174 struct box box;
176 #ifdef CONFIG_MARKS
177 if (ses->kbdprefix.mark != KP_MARK_NOTHING) {
178 switch (ses->kbdprefix.mark) {
179 case KP_MARK_NOTHING:
180 assert(0);
181 break;
183 case KP_MARK_SET:
184 msg = msg_text(term, N_("Enter a mark to set"));
185 break;
187 case KP_MARK_GOTO:
188 msg = msg_text(term, N_("Enter a mark"
189 " to which to jump"));
190 break;
192 } else
193 #endif
194 if (ses->kbdprefix.repeat_count) {
195 msg = msg_text(term, N_("Keyboard prefix: %d"),
196 ses->kbdprefix.repeat_count);
198 #ifdef CONFIG_ECMASCRIPT
199 else if (ses->status.window_status) {
200 msg = stracpy(ses->status.window_status);
202 #endif
203 else if (download) {
204 struct document_view *doc_view = current_frame(ses);
206 /* Show S_INTERRUPTED message *once* but then show links
207 * again as usual. */
208 /* doc_view->vs may be NULL here in the short interval between
209 * ses_forward() with @loading_in_frame set, disconnecting the
210 * doc_view from vs, and render_document_frames(), detaching
211 * the doc_view. */
212 if (doc_view && doc_view->vs) {
213 static int last_current_link;
214 int ncl = doc_view->vs->current_link;
216 if (download->state == S_INTERRUPTED
217 && ncl != last_current_link)
218 download->state = S_OK;
219 last_current_link = ncl;
221 if (download->state == S_OK) {
222 if (get_current_link(doc_view)) {
223 msg = get_current_link_info_and_title(ses, doc_view);
224 } else if (ses->navigate_mode == NAVIGATE_CURSOR_ROUTING) {
225 msg = msg_text(term, N_("Cursor position: %dx%d"),
226 ses->tab->x + 1, ses->tab->y + 1);
231 if (!msg) {
232 int full = term->width > 130;
233 int wide = term->width > 80;
235 msg = get_download_msg(download, term, wide, full, ", ");
239 set_box(&box, 0, term->height - 1, term->width, 1);
240 draw_box(term, &box, ' ', 0, get_bfu_color(term, "status.status-bar"));
242 if (!status->show_tabs_bar && tabs_count > 1) {
243 unsigned char tab_info[8];
245 tab_info[tab_info_len++] = '[';
246 ulongcat(tab_info, &tab_info_len, term->current_tab + 1, 4, 0);
247 tab_info[tab_info_len++] = ']';
248 tab_info[tab_info_len++] = ' ';
249 tab_info[tab_info_len] = '\0';
251 text_color = get_bfu_color(term, "status.status-text");
252 draw_text(term, 0, term->height - 1, tab_info, tab_info_len,
253 0, text_color);
256 if (!msg) return;
258 if (!text_color)
259 text_color = get_bfu_color(term, "status.status-text");
261 msglen = strlen(msg);
262 draw_text(term, 0 + tab_info_len, term->height - 1,
263 msg, msglen, 0, text_color);
264 mem_free(msg);
266 if (download_is_progressing(download) && download->progress->size > 0) {
267 int xend = term->width - 1;
268 int width;
270 #ifdef CONFIG_LEDS
271 if (ses->status.show_leds)
272 xend -= term->leds_length;
273 #endif
275 width = int_max(0, xend - msglen - tab_info_len - 1);
276 if (width < 6) return;
277 int_upper_bound(&width, 20);
278 draw_progress_bar(download->progress, term,
279 xend - width, term->height - 1, width,
280 NULL, NULL);
284 static inline void
285 display_tab_bar(struct session *ses, struct terminal *term, int tabs_count)
287 struct color_pair *normal_color = get_bfu_color(term, "tabs.normal");
288 struct color_pair *selected_color = get_bfu_color(term, "tabs.selected");
289 struct color_pair *loading_color = get_bfu_color(term, "tabs.loading");
290 struct color_pair *fresh_color = get_bfu_color(term, "tabs.unvisited");
291 struct color_pair *tabsep_color = get_bfu_color(term, "tabs.separator");
292 struct session_status *status = &ses->status;
293 int tab_width = int_max(1, term->width / tabs_count);
294 int tab_total_width = tab_width * tabs_count;
295 int tab_remain_width = int_max(0, term->width - tab_total_width);
296 int tab_add = int_max(1, (tab_remain_width / tabs_count));
297 int tab_num;
298 struct box box;
300 if (status->show_tabs_bar_at_top) set_box(&box, 0, status->show_title_bar, term->width, 1);
301 else set_box(&box, 0, term->height - (status->show_status_bar ? 2 : 1), 0, 1);
303 for (tab_num = 0; tab_num < tabs_count; tab_num++) {
304 struct download *download = NULL;
305 struct color_pair *color = normal_color;
306 struct window *tab = get_tab_by_number(term, tab_num);
307 struct document_view *doc_view;
308 struct session *tab_ses = tab->data;
309 int actual_tab_width = tab_width - 1;
310 unsigned char *msg;
312 /* Adjust tab size to use full term width. */
313 if (tab_remain_width) {
314 actual_tab_width += tab_add;
315 tab_remain_width -= tab_add;
318 doc_view = tab_ses ? current_frame(tab_ses) : NULL;
320 if (doc_view) {
321 if (doc_view->document->title
322 && *(doc_view->document->title))
323 msg = doc_view->document->title;
324 else
325 msg = _("Untitled", term);
326 } else {
327 msg = _("No document", term);
330 if (tab_num) {
331 draw_char(term, box.x, box.y, BORDER_SVLINE,
332 SCREEN_ATTR_FRAME, tabsep_color);
333 box.x++;
336 if (tab_num == term->current_tab) {
337 color = selected_color;
339 } else {
340 download = get_current_download(tab_ses);
342 if (download && download->state != S_OK) {
343 color = loading_color;
344 } else if (!tab_ses || !tab_ses->status.visited) {
345 color = fresh_color;
348 if (!download_is_progressing(download)
349 || download->progress->size <= 0)
350 download = NULL;
353 box.width = actual_tab_width + 1;
354 draw_box(term, &box, ' ', 0, color);
356 if (download) {
357 draw_progress_bar(download->progress, term,
358 box.x, box.y, actual_tab_width,
359 msg, NULL);
360 } else {
361 int msglen = int_min(strlen(msg), actual_tab_width);
363 draw_text(term, box.x, box.y, msg, msglen, 0, color);
366 tab->xpos = box.x;
367 tab->width = actual_tab_width;
368 if (tab_num == tabs_count - 1) {
369 /* This is the last tab, and is therefore followed
370 * by a space, not a separator; increment tab->width
371 * to count that space as part of the tab.
372 * -- Miciah */
373 tab->width++;
376 box.x += actual_tab_width;
380 /* Print page's title and numbering at window top. */
381 static inline void
382 display_title_bar(struct session *ses, struct terminal *term)
384 struct document_view *doc_view;
385 struct document *document;
386 struct string title;
387 unsigned char buf[40];
388 int buflen = 0;
389 int height;
391 /* Clear the old title */
392 if (!get_opt_bool("ui.show_menu_bar_always")) {
393 struct box box;
395 set_box(&box, 0, 0, term->width, 1);
396 draw_box(term, &box, ' ', 0, get_bfu_color(term, "title.title-bar"));
399 doc_view = current_frame(ses);
400 if (!doc_view || !doc_view->document) return;
402 if (!init_string(&title)) return;
404 document = doc_view->document;
406 /* Set up the document page info string: '(' %page '/' %pages ')' */
407 height = doc_view->box.height;
408 if (height < document->height && doc_view->vs) {
409 int pos = doc_view->vs->y + height;
410 int page = 1;
411 int pages = height ? (document->height + height - 1) / height : 1;
413 /* Check if at the end else calculate the page. */
414 if (pos >= document->height) {
415 page = pages;
416 } else if (height) {
417 page = int_min((pos - height / 2) / height + 1, pages);
420 buflen = snprintf(buf, sizeof(buf), " (%d/%d)", page, pages);
421 if (buflen < 0) buflen = 0;
424 if (document->title) {
425 int maxlen = int_max(term->width - 4 - buflen, 0);
426 int titlelen, titlewidth;
428 #ifdef CONFIG_UTF8
429 if (term->utf8) {
430 titlewidth = utf8_ptr2cells(document->title, NULL);
431 titlewidth = int_min(titlewidth, maxlen);
433 titlelen = utf8_cells2bytes(document->title,
434 titlewidth, NULL);
435 } else
436 #endif /* CONFIG_UTF8 */
438 titlewidth = int_min(strlen(document->title), maxlen);
439 titlelen = titlewidth;
442 add_bytes_to_string(&title, document->title, titlelen);
444 if (titlewidth == maxlen)
445 add_bytes_to_string(&title, "...", 3);
448 if (buflen > 0)
449 add_bytes_to_string(&title, buf, buflen);
451 if (title.length) {
452 int x;
453 #ifdef CONFIG_UTF8
454 if (term->utf8) {
455 x = int_max(term->width - 1
456 - utf8_ptr2cells(title.source,
457 title.source
458 + title.length), 0);
459 } else
460 #endif /* CONFIG_UTF8 */
461 x = int_max(term->width - 1 - title.length, 0);
463 draw_text(term, x, 0, title.source, title.length, 0,
464 get_bfu_color(term, "title.title-text"));
467 done_string(&title);
470 static inline void
471 display_window_title(struct session *ses, struct terminal *term)
473 static struct session *last_ses;
474 struct session_status *status = &ses->status;
475 unsigned char *doc_title = NULL;
476 unsigned char *title;
477 int titlelen;
479 if (ses->doc_view
480 && ses->doc_view->document
481 && ses->doc_view->document->title
482 && ses->doc_view->document->title[0])
483 doc_title = ses->doc_view->document->title;
485 title = doc_title ? straconcat(doc_title, " - ELinks", NULL)
486 : stracpy("ELinks");
487 if (!title) return;
489 titlelen = strlen(title);
490 if (last_ses != ses
491 || !status->last_title
492 || strlen(status->last_title) != titlelen
493 || memcmp(status->last_title, title, titlelen)) {
494 mem_free_set(&status->last_title, title);
495 set_terminal_title(term, title);
496 last_ses = ses;
497 } else {
498 mem_free(title);
502 #ifdef CONFIG_LEDS
503 static inline void
504 display_leds(struct session *ses, struct session_status *status)
506 if (ses->doc_view && ses->doc_view->document
507 && ses->doc_view->document->uri) {
508 struct cache_entry *cached =
509 find_in_cache(ses->doc_view->document->uri);
511 if (cached) {
512 if (cached->ssl_info)
513 set_led_value(status->ssl_led, 'S');
514 else
515 unset_led_value(status->ssl_led);
516 } else {
517 /* FIXME: We should do this thing better. */
518 set_led_value(status->ssl_led, '?');
522 if (ses->insert_mode == INSERT_MODE_LESS) {
523 set_led_value(status->insert_mode_led, 'i');
524 } else {
525 if (ses->insert_mode == INSERT_MODE_ON)
526 set_led_value(status->insert_mode_led, 'I');
527 else
528 unset_led_value(status->insert_mode_led);
531 draw_leds(ses);
533 #endif /* CONFIG_LEDS */
535 /* Print statusbar and titlebar, set terminal title. */
536 void
537 print_screen_status(struct session *ses)
539 struct terminal *term = ses->tab->term;
540 struct session_status *status = &ses->status;
541 int tabs_count = number_of_tabs(term);
542 int ses_tab_is_current = (ses->tab == get_current_tab(term));
544 if (ses_tab_is_current) {
545 if (status->set_window_title)
546 display_window_title(ses, term);
548 if (status->show_title_bar)
549 display_title_bar(ses, term);
551 if (status->show_status_bar)
552 display_status_bar(ses, term, tabs_count);
553 #ifdef CONFIG_LEDS
554 if (status->show_leds)
555 display_leds(ses, status);
556 #endif
558 if (!ses->status.visited)
559 ses->status.visited = 1;
562 if (status->show_tabs_bar) {
563 display_tab_bar(ses, term, tabs_count);
566 redraw_from_window(ses->tab);