Added a window within the stdscr, which is used for printing the text, as well as another 'status window'
This commit is contained in:
		
							
								
								
									
										40
									
								
								editor.c
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								editor.c
									
									
									
									
									
								
							| @@ -10,6 +10,8 @@ | |||||||
| #define ctrl(x) ((x) & 0x1f) | #define ctrl(x) ((x) & 0x1f) | ||||||
|  |  | ||||||
| int index_to_start = 0; | int index_to_start = 0; | ||||||
|  | WINDOW* mainwin; | ||||||
|  | WINDOW* statusbar; | ||||||
|  |  | ||||||
| typedef struct Buffer_struct Buffer; | typedef struct Buffer_struct Buffer; | ||||||
| struct Buffer_struct { | struct Buffer_struct { | ||||||
| @@ -104,6 +106,10 @@ void scroll_page_down_handler(Buffer* buffer) { | |||||||
| } | } | ||||||
|  |  | ||||||
| void page_down_handler(Buffer* buffer) { | void page_down_handler(Buffer* buffer) { | ||||||
|  | 	if ((getcury(mainwin) + 1) == getmaxy(mainwin)) { | ||||||
|  | 		scroll_page_down_handler(buffer); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	buffer_right(buffer); /* I must advance the cursor at least  | 	buffer_right(buffer); /* I must advance the cursor at least  | ||||||
| 				once, so this hardcoded statement is fine. */ | 				once, so this hardcoded statement is fine. */ | ||||||
|  |  | ||||||
| @@ -112,10 +118,6 @@ void page_down_handler(Buffer* buffer) { | |||||||
| 		buffer_right(buffer); | 		buffer_right(buffer); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if ((getcury(stdscr) + 1) == getmaxy(stdscr)) { |  | ||||||
| 		scroll_page_down_handler(buffer); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	/* You would think that I need to call 'buffer_right' once  | 	/* You would think that I need to call 'buffer_right' once  | ||||||
| 	more, to advance the cursor onto the next line.	In fact, if  | 	more, to advance the cursor onto the next line.	In fact, if  | ||||||
| 	you think about it, the place where the cursor (the rectangle)  | 	you think about it, the place where the cursor (the rectangle)  | ||||||
| @@ -150,7 +152,7 @@ void page_up_handler(Buffer* buffer) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
| 	if ((getcury(stdscr) == 0) && (index_to_start > 0)) { | 	if ((getcury(mainwin) == 0) && (index_to_start > 0)) { | ||||||
| 		scroll_page_up_handler(buffer); | 		scroll_page_up_handler(buffer); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -183,8 +185,17 @@ void save_text_helper(Buffer* buffer,char* filename) { | |||||||
|  |  | ||||||
| void init_curses() { | void init_curses() { | ||||||
| 	initscr(); | 	initscr(); | ||||||
|  | 	mainwin = newwin(getmaxy(stdscr)-1,getmaxx(stdscr),0,0); | ||||||
|  |  | ||||||
|  | 	statusbar = newwin(1,getmaxx(stdscr),getmaxy(stdscr)-1,0); | ||||||
|  | 	wattrset(statusbar,A_REVERSE); | ||||||
|  | 	mvwhline(statusbar,0,0,' ',getmaxx(statusbar)); | ||||||
|  | 	mvwprintw(statusbar,0,0,"Welcome to Editor!"); | ||||||
|  | 	wrefresh(statusbar); | ||||||
|  |  | ||||||
| 	noecho(); | 	noecho(); | ||||||
| 	keypad(stdscr,TRUE); | 	keypad(stdscr,TRUE); | ||||||
|  | 	keypad(mainwin,TRUE); | ||||||
| 	raw(); | 	raw(); | ||||||
| //	scrollok(stdscr,TRUE); | //	scrollok(stdscr,TRUE); | ||||||
| } | } | ||||||
| @@ -221,7 +232,7 @@ int main(int argc, char** argv) { | |||||||
|  |  | ||||||
| 	int y, x; | 	int y, x; | ||||||
| 	while (true) { | 	while (true) { | ||||||
| 		clear(); | 		wclear(mainwin); | ||||||
|  |  | ||||||
| 		int i=index_to_start; | 		int i=index_to_start; | ||||||
| 		if ((buffer->start != buffer->text) || (buffer->gap_size != buffer->size)) {  /* We don't want to print the string, if the  | 		if ((buffer->start != buffer->text) || (buffer->gap_size != buffer->size)) {  /* We don't want to print the string, if the  | ||||||
| @@ -230,28 +241,30 @@ int main(int argc, char** argv) { | |||||||
| 	                while (i < buffer->size) { | 	                while (i < buffer->size) { | ||||||
| 	                        if ((buffer->start - buffer->text) == i) {  /* If we have encountered  | 	                        if ((buffer->start - buffer->text) == i) {  /* If we have encountered  | ||||||
| 										the start of the gap */ | 										the start of the gap */ | ||||||
| 					getyx(stdscr,y,x); | 					getyx(mainwin,y,x); | ||||||
| 	                                i += buffer->gap_size; | 	                                i += buffer->gap_size; | ||||||
| 				} | 				} | ||||||
| 				if (i >= buffer->size) { | 				if (i >= buffer->size) { | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 	                        addch(*(buffer->text + i)); | 	                        waddch(mainwin,*(buffer->text + i)); | ||||||
| 				i++; | 				i++; | ||||||
|  |  | ||||||
| 				fprintf(logfile,"At line %d of %d\n",getcury(stdscr),getmaxy(stdscr)); | 				fprintf(logfile,"At line %d of %d\n",getcury(mainwin),getmaxy(mainwin)); | ||||||
| 				if (*(buffer->text + i) == '\n' && ((getcury(stdscr) + 1) == getmaxy(stdscr))) { | 				if (*(buffer->text + i) == '\n' && ((getcury(mainwin) + 1) == getmaxy(mainwin))) { | ||||||
| 					fprintf(logfile,"Read newline\n"); | 					fprintf(logfile,"Read newline\n"); | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 	                } | 	                } | ||||||
|  |  | ||||||
| 			move(y,x); | 			wmove(mainwin,y,x); | ||||||
|  |  | ||||||
|  | 			wrefresh(mainwin); | ||||||
|  |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  |  | ||||||
| 		ch = getch(); | 		ch = wgetch(mainwin); | ||||||
|  |  | ||||||
| 		switch(ch) { | 		switch(ch) { | ||||||
| 			case KEY_BACKSPACE: | 			case KEY_BACKSPACE: | ||||||
| @@ -287,6 +300,9 @@ int main(int argc, char** argv) { | |||||||
| 				page_up_handler(buffer); | 				page_up_handler(buffer); | ||||||
| 				break; | 				break; | ||||||
|  |  | ||||||
|  | 			case ctrl(KEY_NPAGE): | ||||||
|  | 				abort(); | ||||||
|  |  | ||||||
| 			case ctrl('s'): | 			case ctrl('s'): | ||||||
| 				if (argc == 2) { | 				if (argc == 2) { | ||||||
| 					save_text_helper(buffer,argv[1]); | 					save_text_helper(buffer,argv[1]); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user