2 * Copyright (C) ????-2004 Justin Hibbits <jrh29@po.cwru.edu>
3 * Copyright (C) 2004 Thomer M. Gil <mutt@thomer.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 #include "mutt_menu.h"
24 #include "mutt_curses.h"
31 /*BUFFY *CurBuffy = 0;*/
32 static BUFFY *TopBuffy = 0;
33 static BUFFY *BottomBuffy = 0;
34 static int known_lines = 0;
36 static int quick_log10(int n)
39 sprintf(string, "%d", n);
40 return strlen(string);
43 void calc_boundaries (int menu)
45 BUFFY *tmp = Incoming;
47 if ( known_lines != LINES ) {
48 TopBuffy = BottomBuffy = 0;
51 for ( ; tmp->next != 0; tmp = tmp->next )
52 tmp->next->prev = tmp;
54 if ( TopBuffy == 0 && BottomBuffy == 0 )
56 if ( BottomBuffy == 0 ) {
57 int count = LINES - 2 - (menu != MENU_PAGER);
58 BottomBuffy = TopBuffy;
59 while ( --count && BottomBuffy->next )
60 BottomBuffy = BottomBuffy->next;
62 else if ( TopBuffy == CurBuffy->next ) {
63 int count = LINES - 2 - (menu != MENU_PAGER);
64 BottomBuffy = CurBuffy;
66 while ( --count && tmp->prev)
70 else if ( BottomBuffy == CurBuffy->prev ) {
71 int count = LINES - 2 - (menu != MENU_PAGER);
74 while ( --count && tmp->next )
80 static char * shortened_hierarchy(char * box) {
85 for (i=0;i<strlen(box);++i)
86 if (box[i] == '.') ++dots;
87 last_dot = strrchr(box,'.');
90 new_box = safe_malloc(strlen(last_dot)+2*dots+1);
92 for (i=1,j=1;i<strlen(box);++i) {
96 if (&box[i+1] != last_dot) {
97 new_box[j++] = box[i+1];
100 strcat(&new_box[j],last_dot);
107 return safe_strdup(box);
110 char *make_sidebar_entry(char *box, int size, int new)
112 static char *entry = 0;
116 c = realloc(entry, SidebarWidth + 1);
118 entry[SidebarWidth] = 0;
119 for (; i < SidebarWidth; entry[i++] = ' ' );
121 if (ImapHomeNamespace && strlen(ImapHomeNamespace)>0) {
122 if (strncmp(box,ImapHomeNamespace,strlen(ImapHomeNamespace))==0 && strcmp(box,ImapHomeNamespace)!=0) {
123 box+=strlen(ImapHomeNamespace)+1;
127 if (option(OPTSHORTENHIERARCHY)) {
128 box = shortened_hierarchy(box);
131 strncpy( entry, box, i < SidebarWidth ? i :SidebarWidth );
135 entry + SidebarWidth - 5 - quick_log10(size) - quick_log10(new),
136 "% d(%d)", size, new);
138 sprintf( entry + SidebarWidth - 3 - quick_log10(size), "% d", size);
139 if (option(OPTSHORTENHIERARCHY)) {
145 void set_curbuffy(char buf[LONG_STRING])
147 BUFFY* tmp = CurBuffy = Incoming;
153 if(!strcmp(tmp->path, buf)) {
165 int draw_sidebar(int menu) {
167 int lines = option(OPTHELP) ? 1 : 0;
169 #ifndef USE_SLANG_CURSES
174 static bool initialized = false;
175 static int prev_show_value;
176 static short saveSidebarWidth;
178 /* initialize first time */
180 prev_show_value = option(OPTMBOXPANE);
181 saveSidebarWidth = SidebarWidth;
182 if(!option(OPTMBOXPANE)) SidebarWidth = 0;
186 /* save or restore the value SidebarWidth */
187 if(prev_show_value != option(OPTMBOXPANE)) {
188 if(prev_show_value && !option(OPTMBOXPANE)) {
189 saveSidebarWidth = SidebarWidth;
191 } else if(!prev_show_value && option(OPTMBOXPANE)) {
192 SidebarWidth = saveSidebarWidth;
194 prev_show_value = option(OPTMBOXPANE);
198 if ( SidebarWidth == 0 ) return 0;
200 /* get attributes for divider */
201 SETCOLOR(MT_COLOR_STATUS);
202 #ifndef USE_SLANG_CURSES
203 attr_get(&attrs, &color_pair, 0);
205 color_pair = attr_get();
207 SETCOLOR(MT_COLOR_NORMAL);
209 /* draw the divider */
211 for ( ; lines < LINES-1-(menu != MENU_PAGER); lines++ ) {
212 move(lines, SidebarWidth - 1);
214 #ifndef USE_SLANG_CURSES
215 mvchgat(lines, SidebarWidth - 1, 1, 0, color_pair, NULL);
218 if ( Incoming == 0 ) return 0;
219 lines = option(OPTHELP) ? 1 : 0; /* go back to the top */
221 if ( known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0 )
222 calc_boundaries(menu);
223 if ( CurBuffy == 0 ) CurBuffy = Incoming;
227 SETCOLOR(MT_COLOR_NORMAL);
229 for ( ; tmp && lines < LINES-1 - (menu != MENU_PAGER); tmp = tmp->next ) {
230 if ( tmp == CurBuffy )
231 SETCOLOR(MT_COLOR_INDICATOR);
232 else if ( tmp->msg_unread > 0 )
233 SETCOLOR(MT_COLOR_NEW);
235 SETCOLOR(MT_COLOR_NORMAL);
238 if ( Context && !strcmp( tmp->path, Context->path ) ) {
239 printw( "%.*s", SidebarWidth,
240 make_sidebar_entry(basename(tmp->path), Context->msgcount,
242 tmp->msg_unread = Context->unread;
243 tmp->msgcount = Context->msgcount;
246 printw( "%.*s", SidebarWidth,
247 make_sidebar_entry(basename(tmp->path), tmp->msgcount,
251 SETCOLOR(MT_COLOR_NORMAL);
252 for ( ; lines < LINES - 1 - (menu != MENU_PAGER); lines++ ) {
255 for ( ; i < SidebarWidth - 1; i++ )
261 void scroll_sidebar(int op, int menu)
263 if(!SidebarWidth) return;
264 if(!CurBuffy) return;
267 case OP_SIDEBAR_NEXT:
268 if ( CurBuffy->next == NULL ) return;
269 CurBuffy = CurBuffy->next;
271 case OP_SIDEBAR_PREV:
272 if ( CurBuffy == Incoming ) return;
274 BUFFY *tmp = Incoming;
275 while ( tmp->next && strcmp(tmp->next->path, CurBuffy->path) ) tmp = tmp->next;
279 case OP_SIDEBAR_SCROLL_UP:
281 if ( CurBuffy != Incoming ) {
282 calc_boundaries(menu);
283 CurBuffy = CurBuffy->prev;
286 case OP_SIDEBAR_SCROLL_DOWN:
287 CurBuffy = BottomBuffy;
288 if ( CurBuffy->next ) {
289 calc_boundaries(menu);
290 CurBuffy = CurBuffy->next;
296 calc_boundaries(menu);