#!/usr/bin/env python ## CURSES BASIC-MAIL GUI # This is the Basic-Mail program, a basic email client written in python # with a curses interface. # # To Run: Go to the Basic-Mail directory and type "python Basic-Mail.py", # without the quotes. ## ## interface.py ## # This file contains the functions that interact with the curses interface. ## ## IMPORTS ## import curses, curses.ascii, curses.panel # Curses imports import traceback, os, sys, select, string # Random imports # Initializes and Globalizes (?) the Menus, Tabs, and Title within the # interface file def initialize_interface_variables(menu_array,tabs_array,program_title_import): global menu, tabs, program_title menu = menu_array tabs = tabs_array program_title = program_title_import ## Function: Fills the title line for windows def fill_line(window,width,header,letter,color,y): x = 0 while (x < width): window.addch(y,x,letter,curses.color_pair(color)) x += 1 window.addstr(y,(width-len(header))/2,header,curses.color_pair(color)) window.noutrefresh() ## Function: Draws borders for windows def draw_borders(window,width,height): window.vline(0,0,'|',height-1) window.vline(0,width-1,'|',height-1) window.hline(height-1,0,'+',width-1) window.hline(height-1,1,'-',width-1) window.hline(height-1,width-1,'+',1) window.noutrefresh() ## Function: Creates a window bar with options. ## Used for tab and menu bars. def create_bar(window,list,width,space): x = 2 count = 0 draw_borders(window,width,1) for number in list[0]: window.addstr(0,x,number) # If negative, crawls back from the end of the word if list[1][count] < 0: window.addch(0,x+len(list[0][count])+list[1][count],\ list[0][count][list[1][count]],\ curses.color_pair(3)) else: window.addch(0,x+list[1][count],list[0][count][list[1][count]],\ curses.color_pair(3)) # Highlights specified letter x += (len(number)+space) count += 1 window.refresh() ## Function: Checks which tab the user has selected def check_tabs(location): if location == 0: return tab1 elif location == 1: return tab2 elif location == 2: return tab3 elif location == 3: return tab4 elif location == 4: return tab5 else: return 0 ## Function: Highlights the user-selected option. ## Used for both menu and tab selection. def select_bar(window,list,location,height,width,options): ## Selects the user-selected option. # Highlights Text on Bar. x = 2 if list == menu: space = 2 if list == tabs: space = 1 # Finds placement of text. for number in list[0][0:location]: x += len(number) + space # Prints text. if list == menu: window.addstr(0,x,list[0][location],curses.color_pair(2)) elif list == tabs: window.addstr(0,x+1,list[0][location][1:-1],curses.color_pair(2)) # x+1 adjusts for the removed '\'. window.refresh() ## Function: Creates a menu window below the user selected option. ## Returns the menu window's panel. def create_menu(list,location): x = 2 y = 2 height = 0 width = 0 for number in list[0][0:location]: x += len(number) + 2 location = (location * 2) + 2 for number in list[location]: if len(number) > width: width = len(number) height += 1 width += 2 # Accounts for borders height += 1 # Again for borders menu_win = curses.newwin(height,width,y,x) menu_panel = curses.panel.new_panel(menu_win) # Begin drawing menu y = 0 x = 0 draw_borders(menu_win,width,height) for number in list[location]: menu_win.addstr(y,x+1,number) menu_win.addch(y,list[location+1][y]+1,number[list[location+1][y]],\ curses.color_pair(3)) y += 1 menu_win.refresh() return menu_panel ## Function: Creates appropriate pop-up window for the option selected. ## Different functions programmed for each option. ## Also returns different things depending on the option selected. def create_pop_up(location,count,menu): # If the user selects Quit from the File menu if menu[location][count][menu[location+1][count]] == 'q' or\ menu[location][count][menu[location+1][count]] == 'Q': pop_up_win = curses.newwin(4,30,13,25) pop_up = curses.panel.new_panel(pop_up_win) fill_line(pop_up_win, 30,"Quit",' ',2,0) draw_borders(pop_up_win,30,4) pop_up_win.addstr(1,4,"Would you like to exit?") pop_up_win.addstr(2,9,"[Yes] or [No]") pop_up_win.addch(2,10,'Y',curses.color_pair(3)) pop_up_win.addch(2,19,'N',curses.color_pair(3)) # Waits for the user's response live = 1 while(live): pop_up_win.move(0,0) # Moves cursor input = pop_up_win.getch(0,0) # Takes input at location if curses.ascii.unctrl(input) == 'y' or\ curses.ascii.unctrl(input) == 'Y': return 0 live = 0 elif curses.ascii.unctrl(input) == 'n' or\ curses.ascii.unctrl(input) == 'N': return 1 live = 0 # If the user has selected to close the last open tab elif menu[location][count][menu[location+1][count]] == 'c' or\ menu[location][count][menu[location+1][count]] == 'C': pop_up_win = curses.newwin(5,30,13,25) pop_up = curses.panel.new_panel(pop_up_win) fill_line(pop_up_win, 30,"Quit",' ',2,0) draw_borders(pop_up_win,30,5) pop_up_win.addstr(1,4,"The last remaining tab") pop_up_win.addstr(2,6,"cannot be closed.") pop_up_win.addstr(3,13,"[Ok]") pop_up_win.addch(3,15,'k',curses.color_pair(3)) # Waits for the user's response live = 1 while(live): pop_up_win.move(0,0) # Moves cursor input = pop_up_win.getch(0,0) # Takes input at location if curses.ascii.unctrl(input) == 'k' or\ curses.ascii.unctrl(input) == 'K': live = 0 else: return 0 """ ## Function: Closes tabs and adjust others appropriately. def close_tab(): ## DOES NOT WORK ## # I cannot figure this out. count = 0 # If user closes the first tab if active_tab == tab2: del tabs[0][1] del tabs[1][1] count = 1 # Sets count to adjust all tabs after it. while count < len(tabs[0]): if count == 0: tabs[0][0] = tabs[0][0][:-2] + '1/' win2.overwrite(win1) win2.erase() if count == 1: tabs[0][1] = tabs[0][1][:-2] + '2/' win3.overwrite(win2) win3.erase() if count == 2: tabs[0][2] = tabs[0][2][:-2] + '3/' win4.overwrite(win3) win4.erase() if count == 3: tabs[0][3] = tabs[0][3][:-2] + '4/' win5.overwrite(win4) win5.erase() count += 1 # Sets first tab as active_tab active_tab = tab1 active_win = win1 tab1.top() return tab1 ## THIS CODE DOES NOT WORK! ## # If user closes last tab elif len(tabs[0]) > 1: select_bar(tab_bar,tabs,0,10,10,0) tabs[0].pop() tabs[1].pop() tab1.top() active_tab = tab1 active_win = win1 menu_win_q = 0 #draw_screen() count = 0 """