summaryrefslogtreecommitdiff
path: root/menu.c
blob: 7c7f16f970064f1a62bbc1f6eb36f91228eae16a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// deals with all of ted's menus ("open" menu, "save as" menu, etc.)

#include "ted-internal.h"

bool menu_is_open(Ted *ted, const char *menu_name) {
	if (!menu_is_any_open(ted))
		return false;
	return streq(ted->all_menus[ted->menu_open_idx].name, menu_name);
}

bool menu_is_any_open(Ted *ted) {
	return ted->menu_open_idx > 0;
}

void *menu_get_context(Ted *ted) {
	return ted->menu_context;
}

void menu_close(Ted *ted) {
	if (!menu_is_any_open(ted))
		return;
	const MenuInfo *menu = &ted->all_menus[ted->menu_open_idx];
	if (menu->close) {
		if (!menu->close(ted))
			return;
	}
	
	ted_switch_to_buffer(ted, ted->prev_active_buffer);
	ted->prev_active_buffer = NULL;
	ted->menu_open_idx = 0;
	ted->menu_context = NULL;
	ted->selector_open = NULL;
}

void menu_open_with_context(Ted *ted, const char *menu_name, void *context) {
	if (menu_is_open(ted, menu_name))
		return;
	u32 menu_idx = U32_MAX;
	if (*menu_name) {
		for (u32 i = 0; i < arr_len(ted->all_menus); ++i) {
			if (streq(ted->all_menus[i].name, menu_name)) {
				menu_idx = i;
				break;
			}
		}
	}
	if (menu_idx == U32_MAX) {
		ted_error(ted, "No such menu: %s", menu_name);
		return;
	}
	
	if (menu_is_any_open(ted))
		menu_close(ted);
	if (ted->find) find_close(ted);
	autocomplete_close(ted);
		
	const MenuInfo *info = &ted->all_menus[menu_idx];
	ted->menu_open_idx = menu_idx;
	ted->menu_context = context;
	ted->prev_active_buffer = ted->active_buffer;
	
	ted_switch_to_buffer(ted, NULL);
	*ted->warn_overwrite = 0; // clear warn_overwrite
	buffer_clear(ted->line_buffer);
	if (info->open) info->open(ted);
}

void menu_open(Ted *ted, const char *menu_name) {
	menu_open_with_context(ted, menu_name, NULL);
}

void menu_escape(Ted *ted) {
	if (!menu_is_any_open(ted)) return;
	
	if (*ted->warn_overwrite) {
		// just close "are you sure you want to overwrite?"
		*ted->warn_overwrite = 0;
		ted_switch_to_buffer(ted, ted->line_buffer);
	} else {
		menu_close(ted);
	}
}

void menu_update(Ted *ted) {
	if (!menu_is_any_open(ted)) return;
	
	const MenuInfo *info = &ted->all_menus[ted->menu_open_idx];
	
	if (info->update) info->update(ted);
}

Rect selection_menu_render_bg(Ted *ted) {
	const Settings *settings = ted_active_settings(ted);
	const float menu_width = ted_get_menu_width(ted);
	const float padding = settings->padding;
	const float window_width = ted->window_width, window_height = ted->window_height;
	Rect bounds = rect_xywh(
		window_width * 0.5f - 0.5f * menu_width, padding,
		menu_width, window_height - 2 * padding
	);
	
	float x1, y1, x2, y2;
	rect_coords(bounds, &x1, &y1, &x2, &y2);

	// menu rectangle & border
	gl_geometry_rect(bounds, settings_color(settings, COLOR_MENU_BG));
	gl_geometry_rect_border(bounds, settings->border_thickness, settings_color(settings, COLOR_BORDER));
	gl_geometry_draw();
	
	x1 += padding;
	y1 += padding;
	x2 -= padding;
	y2 -= padding;
	return rect4(x1, y1, x2, y2);
}

void menu_render(Ted *ted) {
	const Settings *settings = ted_active_settings(ted);
	const float window_width = ted->window_width, window_height = ted->window_height;
	const MenuInfo *info = &ted->all_menus[ted->menu_open_idx];
	// render backdrop
	gl_geometry_rect(rect_xywh(0, 0, window_width, window_height), settings_color(settings, COLOR_MENU_BACKDROP));
	gl_geometry_draw();

	if (info->render)
		info->render(ted);
}

static void menu_edit_notify(void *context, TextBuffer *buffer, const EditInfo *info) {
	(void)info;
	
	Ted *ted = context;
	if (buffer == ted->line_buffer && menu_is_open(ted, MENU_SHELL)) {
		ted->shell_command_modified = true;
	}
	
}

void menu_shell_move(Ted *ted, int direction) {
	TextBuffer *line_buffer = ted->line_buffer;
	i64 pos = ted->shell_history_pos;
	pos += direction;
	if (pos >= 0 && pos <= arr_len(ted->shell_history)) {
		ted->shell_history_pos = (u32)pos;
		buffer_clear(line_buffer);
		if (pos == arr_len(ted->shell_history)) {
			// bottom of history; just clear line buffer
		} else {
			buffer_set_undo_enabled(line_buffer, false);
			buffer_insert_utf8_at_cursor(line_buffer, ted->shell_history[pos]);
			buffer_set_undo_enabled(line_buffer, true);
			ted->shell_command_modified = true;
		}
		// line_buffer->x/y1/2 are wrong (all 0), because of buffer_clear
		buffer_center_cursor_next_frame(line_buffer);
	}
}

void menu_shell_up(Ted *ted) {
	menu_shell_move(ted, -1);
}
void menu_shell_down(Ted *ted) {
	menu_shell_move(ted, +1);
}

static void open_menu_open(Ted *ted) {
	ted_switch_to_buffer(ted, ted->line_buffer);
	file_selector_set_create(ted->file_selector, false);
}

static void open_menu_update(Ted *ted) {
	char *selected_file = file_selector_update(ted, ted->file_selector);
	if (selected_file) {
		// open that file!
		menu_close(ted);
		ted_open_file(ted, selected_file);
		free(selected_file);
	}
}

static void open_menu_render(Ted *ted) {
	FileSelector *fs = ted->file_selector;
	file_selector_set_title(fs, "Open...");
	file_selector_set_bounds(fs, selection_menu_render_bg(ted));
	file_selector_render(ted, fs);
}

static bool open_menu_close(Ted *ted) {
	file_selector_clear(ted->file_selector);
	buffer_clear(ted->line_buffer);
	return true;
}

static void save_as_menu_open(Ted *ted) {
	ted_switch_to_buffer(ted, ted->line_buffer);
	file_selector_set_create(ted->file_selector, true);
}

static void save_as_menu_update(Ted *ted) {
	if (*ted->warn_overwrite) {
		switch (popup_update(ted, POPUP_YES_NO_CANCEL)) {
		case POPUP_NONE:
			// no option selected
			break;
		case POPUP_YES: {
			// overwrite it!
			TextBuffer *buffer = ted->prev_active_buffer;
			if (buffer) {
				buffer_save_as(buffer, ted->warn_overwrite);
			}
			menu_close(ted);
		} break;
		case POPUP_NO:
			// back to the file selector
			*ted->warn_overwrite = '\0';
			ted_switch_to_buffer(ted, ted->line_buffer);
			break;
		case POPUP_CANCEL:
			// close "save as" menu
			menu_close(ted);
			break;
		}
	} else {
		char *selected_file = file_selector_update(ted, ted->file_selector);
		if (selected_file) {
			TextBuffer *buffer = ted->prev_active_buffer;
			if (buffer) {
				if (fs_path_type(selected_file) != FS_NON_EXISTENT) {
					// file already exists! warn about overwriting it.
					strbuf_cpy(ted->warn_overwrite, selected_file);
					ted_switch_to_buffer(ted, NULL);
				} else {
					// create the new file.
					buffer_save_as(buffer, selected_file);
					menu_close(ted);
				}
			}
			free(selected_file);
		}
	}
}

static void save_as_menu_render(Ted *ted) {
	if (*ted->warn_overwrite) {
		const char *path = ted->warn_overwrite;
		const char *filename = path_filename(path);
		char title[64] = {0}, body[1024] = {0};
		strbuf_printf(title, "Overwrite %s?", filename);
		strbuf_printf(body, "Are you sure you want to overwrite %s?", path);
		popup_render(ted, POPUP_YES_NO_CANCEL, title, body);
		return;
	}

	FileSelector *fs = ted->file_selector;
	file_selector_set_title(fs, "Save as...");
	file_selector_set_bounds(fs, selection_menu_render_bg(ted));
	file_selector_render(ted, fs);
}

static bool save_as_menu_close(Ted *ted) {
	file_selector_clear(ted->file_selector);
	buffer_clear(ted->line_buffer);
	return true;
}

static void warn_unsaved_menu_update(Ted *ted) {
	assert(ted->warn_unsaved);
	assert(*ted->warn_unsaved_names);
	switch (popup_update(ted, POPUP_YES_NO_CANCEL)) {
	case POPUP_NONE: break;
	case POPUP_YES:
		// save changes
		switch (ted->warn_unsaved) {
		case CMD_TAB_CLOSE: {
			menu_close(ted);
			TextBuffer *buffer = ted->active_buffer;
			command_execute(ted, CMD_SAVE, 1); 
			if (!buffer_unsaved_changes(buffer)) {
				command_execute(ted, CMD_TAB_CLOSE, 1);
			}
		} break;
		case CMD_QUIT:
			menu_close(ted);
			if (ted_save_all(ted)) {
				command_execute(ted, CMD_QUIT, 1);
			}
			break;
		default:
			assert(0);
			break;
		}
		break;
	case POPUP_NO: {
		// pass in an argument of 2 to override dialog
		Command cmd = ted->warn_unsaved;
		menu_close(ted);
		command_execute(ted, cmd, 2);
	} break;
	case POPUP_CANCEL:
		menu_close(ted);
		break;
	}
}

static void warn_unsaved_menu_render(Ted *ted) {
	char title[64] = {0}, body[1024] = {0};
	strbuf_printf(title, "Save changes?");
	strbuf_printf(body, "Do you want to save your changes to %s?", ted->warn_unsaved_names);
	popup_render(ted, POPUP_YES_NO_CANCEL, title, body);
}

static bool warn_unsaved_menu_close(Ted *ted) {
	ted->warn_unsaved = 0;
	*ted->warn_unsaved_names = 0;
	return true;
}

static void ask_reload_menu_update(Ted *ted) {
	assert(*ted->ask_reload);
	TextBuffer *buffer = ted->prev_active_buffer;
	switch (popup_update(ted, POPUP_YES_NO)) {
	case POPUP_NONE: break;
	case POPUP_YES:
		menu_close(ted);
		if (buffer)
			buffer_reload(buffer);
		break;
	case POPUP_NO:
		menu_close(ted);
		if (buffer)
			buffer_ignore_changes_on_disk(buffer);
		break;
	case POPUP_CANCEL: assert(0); break;
	}
}

static void ask_reload_menu_render(Ted *ted) {
	char title[64] = {0}, body[1024] = {0};
	strbuf_printf(title, "Reload %s?", ted->ask_reload);
	strbuf_printf(body, "%s has been changed by another program. Do you want to reload it?", ted->ask_reload);
	popup_render(ted, POPUP_YES_NO, title, body);
}

static bool ask_reload_menu_close(Ted *ted) {
	*ted->ask_reload = 0;
	return true;
}

static void command_selector_open(Ted *ted) {
	ted_switch_to_buffer(ted, ted->line_buffer);
	buffer_insert_char_at_cursor(ted->argument_buffer, '1');
	Selector *selector = ted->command_selector;
	selector_set_cursor(selector, 0);
}

static void command_selector_update(Ted *ted) {
	TextBuffer *line_buffer = ted->line_buffer;
	Selector *selector = ted->command_selector;
	char *search_term = str32_to_utf8_cstr(buffer_get_line(line_buffer, 0));
	char *chosen_command = selector_update(ted, selector);
	if (chosen_command) {
		Command c = command_from_str(chosen_command);
		if (c != CMD_UNKNOWN) {
			char *argument = str32_to_utf8_cstr(buffer_get_line(ted->argument_buffer, 0)), *endp = NULL;
			long long arg = 1;
			bool execute = true;
			if (*argument) {
				strtoll(argument, &endp, 0);
				if (*endp != '\0')
					execute = false;
			}
			
			if (execute) {
				menu_close(ted);
				command_execute(ted, c, arg);
			}

			free(argument);
		}

		free(chosen_command);
	}
	free(search_term);
}

static void command_selector_render(Ted *ted) {
	const Settings *settings = ted_active_settings(ted);
	const float padding = settings->padding;
	const float line_buffer_height = ted_line_buffer_height(ted);
	Font *font_bold = ted->font_bold;
	
	Rect r = selection_menu_render_bg(ted);
	
	float x1=0, y1=0, x2=0, y2=0;
	rect_coords(r, &x1, &y1, &x2, &y2);
	
	// argument field
	const char *text = "Argument";
	text_utf8(font_bold, text, x1, y1, settings_color(settings, COLOR_TEXT));
	float x = x1 + text_get_size_vec2(font_bold, text).x + padding;
	buffer_render(ted->argument_buffer, rect4(x, y1, x2, y1 + line_buffer_height));

	y1 += line_buffer_height + padding;

	Selector *selector = ted->command_selector;
	selector_set_bounds(selector, rect4(x1, y1, x2, y2));
	selector_render(ted, selector);

	text_render(font_bold);
}

static bool command_selector_close(Ted *ted) {
	buffer_clear(ted->line_buffer);
	buffer_clear(ted->argument_buffer);
	return true;
}

static void goto_line_menu_open(Ted *ted) {
	ted_switch_to_buffer(ted, ted->line_buffer);
}

static void goto_line_menu_update(Ted *ted) {
	TextBuffer *line_buffer = ted->line_buffer;
	
	char *contents = str32_to_utf8_cstr(buffer_get_line(line_buffer, 0));
	char *end;
	long line_number = strtol(contents, &end, 0);
	TextBuffer *buffer = ted->prev_active_buffer;
	if (*contents != '\0' && *end == '\0') {
		if (line_number < 1) line_number = 1;
		if (line_number > (long)buffer_line_count(buffer)) line_number = (long)buffer_line_count(buffer); 
		BufferPos pos = {(u32)line_number - 1, 0};
		
		if (line_buffer_is_submitted(line_buffer)) {
			// let's go there!
			menu_close(ted);
			buffer_cursor_move_to_pos(buffer, pos);
			buffer_center_cursor(buffer);
		} else {
			// scroll to the line
			buffer_scroll_center_pos(buffer, pos);
		}
	}
	line_buffer_clear_submitted(line_buffer);
	free(contents);
}

static void goto_line_menu_render(Ted *ted) {
	const Settings *settings = ted_active_settings(ted);
	const float padding = settings->padding;
	const float window_width = ted->window_width, window_height = ted->window_height;
	Font *font_bold = ted->font_bold;
	
	float menu_height = ted_line_buffer_height(ted) + 2 * padding;
	Rect r = rect_xywh(padding, window_height - menu_height - padding, window_width - 2 * padding, menu_height);
	gl_geometry_rect(r, settings_color(settings, COLOR_MENU_BG));
	gl_geometry_rect_border(r, settings->border_thickness, settings_color(settings, COLOR_BORDER));
	const char *text = "Go to line...";
	vec2 text_size = text_get_size_vec2(font_bold, text);
	float x1=0, y1=0, x2=0, y2=0;
	rect_coords(r, &x1, &y1, &x2, &y2);
	x1 += padding;
	y1 += padding;
	x2 -= padding;
	y2 -= padding;
	// render "Go to line" text
	text_utf8(font_bold, text, x1, 0.5f * (y1 + y2 - text_size.y), settings_color(settings, COLOR_TEXT));
	x1 += text_size.x + padding;
	gl_geometry_draw();
	text_render(font_bold);
	
	// line buffer
	buffer_render(ted->line_buffer, rect4(x1, y1, x2, y2));
}

static bool goto_line_menu_close(Ted *ted) {
	buffer_clear(ted->line_buffer);
	return true;
}

static void shell_menu_open(Ted *ted) {
	ted_switch_to_buffer(ted, ted->line_buffer);
	ted->shell_history_pos = arr_len(ted->shell_history);
	ted->shell_command_modified = false;
}

static void shell_menu_update(Ted *ted) {
	TextBuffer *line_buffer = ted->line_buffer;
	if (line_buffer_is_submitted(line_buffer)) {
		char *command = str32_to_utf8_cstr(buffer_get_line(line_buffer, 0));
		if (ted->shell_history_pos == arr_len(ted->shell_history) || ted->shell_command_modified) {
			arr_add(ted->shell_history, command);
		}
		menu_close(ted);
		build_set_working_directory(ted, ted->cwd);
		build_start_with_command(ted, command);
	}
}

static void shell_menu_render(Ted *ted) {
	const float line_buffer_height = ted_line_buffer_height(ted);
	const Settings *settings = ted_active_settings(ted);
	const float padding = settings->padding;
	const float width = ted_get_menu_width(ted);
	const float height = line_buffer_height + 2 * padding;
	Rect bounds = {
		.pos = {(ted->window_width - width) / 2, padding},
		.size = {width, height},
	};
	gl_geometry_rect(bounds, settings_color(settings, COLOR_MENU_BG));
	gl_geometry_rect_border(bounds, settings->border_thickness, settings_color(settings, COLOR_BORDER));
	gl_geometry_draw();
	rect_shrink(&bounds, padding);
	const char *text = "Run";
	text_utf8(ted->font_bold, text, bounds.pos.x, bounds.pos.y, settings_color(settings, COLOR_TEXT));
	rect_shrink_left(&bounds, text_get_size_vec2(ted->font_bold, text).x + padding);
	text_render(ted->font_bold);
	buffer_render(ted->line_buffer, bounds);
}

static bool shell_menu_close(Ted *ted) {
	buffer_clear(ted->line_buffer);
	return true;
}

void menu_register(Ted *ted, const MenuInfo *infop) {
	MenuInfo info = *infop;
	if (!*info.name) {
		ted_error(ted, "menu has no name");
		return;
	}
	info.name[sizeof info.name - 1] = '\0';
	arr_add(ted->all_menus, info);
}

void menu_init(Ted *ted) {
	// dummy 0 entry so that nothing has index 0.
	arr_add(ted->all_menus, (MenuInfo){0});
	
	ted_add_edit_notify(ted, menu_edit_notify, ted);
	
	ted->command_selector = selector_new();
        for (Command c = 0; c < CMD_COUNT; ++c) {
		const char *name = command_to_str(c);
		if (c != CMD_UNKNOWN && *name) {
			SelectorEntry entry = {
				.name = name
			};
			selector_add_entry(ted->command_selector, &entry);
		}
        }
	
	MenuInfo save_as_menu = {
		.open = save_as_menu_open,
		.update = save_as_menu_update,
		.render = save_as_menu_render,
		.close = save_as_menu_close,
	};
	strbuf_cpy(save_as_menu.name, MENU_SAVE_AS);
	menu_register(ted, &save_as_menu);
	
	MenuInfo open_menu = {
		.open = open_menu_open,
		.update = open_menu_update,
		.render = open_menu_render,
		.close = open_menu_close,
	};
	strbuf_cpy(open_menu.name, MENU_OPEN);
	menu_register(ted, &open_menu);
	
	MenuInfo warn_unsaved_menu = {
		.open = NULL,
		.update = warn_unsaved_menu_update,
		.render = warn_unsaved_menu_render,
		.close = warn_unsaved_menu_close,
	};
	strbuf_cpy(warn_unsaved_menu.name, MENU_WARN_UNSAVED);
	menu_register(ted, &warn_unsaved_menu);
	
	MenuInfo ask_reload_menu = {
		.open = NULL,
		.update = ask_reload_menu_update,
		.render = ask_reload_menu_render,
		.close = ask_reload_menu_close,
	};
	strbuf_cpy(ask_reload_menu.name, MENU_ASK_RELOAD);
	menu_register(ted, &ask_reload_menu);
	
	MenuInfo command_selector_menu = {
		.open = command_selector_open,
		.update = command_selector_update,
		.render = command_selector_render,
		.close = command_selector_close,
	};
	strbuf_cpy(command_selector_menu.name, MENU_COMMAND_SELECTOR);
	menu_register(ted, &command_selector_menu);
	
	MenuInfo goto_line_menu = {
		.open = goto_line_menu_open,
		.update = goto_line_menu_update,
		.render = goto_line_menu_render,
		.close = goto_line_menu_close,
	};
	strbuf_cpy(goto_line_menu.name, MENU_GOTO_LINE);
	menu_register(ted, &goto_line_menu);
	
	MenuInfo shell_menu = {
		.open = shell_menu_open,
		.update = shell_menu_update,
		.render = shell_menu_render,
		.close = shell_menu_close,
	};
	strbuf_cpy(shell_menu.name, MENU_SHELL);
	menu_register(ted, &shell_menu);
}

void menu_quit(Ted *ted) {
	arr_clear(ted->all_menus);
	selector_free(ted->command_selector);
}