-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaditor.cpp
More file actions
124 lines (113 loc) · 3.28 KB
/
chaditor.cpp
File metadata and controls
124 lines (113 loc) · 3.28 KB
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
#include <FL/Enumerations.H>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Editor.H>
#include <FL/filename.H>
#include <FL/fl_ask.H>
#include <FL/fl_string_functions.h>
#include <FL/platform_types.h>
Fl_Double_Window *app_window = NULL;
bool text_changed = false;
char app_filename[FL_PATH_MAX] = "";
Fl_Menu_Bar *app_menu_bar = NULL;
Fl_Text_Editor *app_editor = NULL;
Fl_Text_Editor *app_split_editor = NULL; // for later
Fl_Text_Buffer *app_text_buffer = NULL;
void build_app_window()
{
app_window = new Fl_Double_Window(640, 480, "Chaditor");
}
void update_title()
{
const char *fname = NULL;
if (app_filename[0]) {
fname = fl_filename_name(app_filename);
}
if (fname) {
char buf[FL_PATH_MAX + 3];
buf[FL_PATH_MAX + 2] =
'\0'; // ensure that the buffer is always terminated
if (text_changed) {
snprintf(buf, FL_PATH_MAX + 2, "%s *", fname);
} else {
snprintf(buf, FL_PATH_MAX + 2, "%s", fname);
}
app_window->copy_label(buf);
} else {
app_window->label("Chaditor");
}
}
void set_changed(bool v)
{
if (v != text_changed) {
text_changed = v;
update_title();
}
}
void set_filename(const char *new_filename)
{
if (new_filename) {
fl_strlcpy(app_filename, new_filename, FL_PATH_MAX);
} else {
app_filename[0] = 0;
}
update_title();
}
void menu_quit_callback(Fl_Widget *, void *)
{
if (text_changed) {
int c = fl_choice("Changes in your text have not been saved.\n"
"Do you want to quit the Chaditor anyway?",
"Quit", "Cancel", NULL);
if (c == 1) {
return;
}
}
Fl::hide_all_windows();
}
void build_app_menu_bar()
{
app_window->begin();
app_menu_bar = new Fl_Menu_Bar(0, 0, app_window->w(), 25);
app_menu_bar->add("File/Quit Editor", FL_COMMAND + 'q', menu_quit_callback);
app_window->callback(menu_quit_callback);
app_window->end();
}
void menu_new_callback(Fl_Widget *, void *)
{
app_text_buffer->text("");
set_changed(false);
}
void text_changed_callback(int, int n_inserted, int n_deleted, int,
const char *, void *)
{
if (n_inserted || n_deleted) {
set_changed(true);
}
}
void build_main_editor()
{
app_window->begin();
app_text_buffer = new Fl_Text_Buffer();
app_text_buffer->add_modify_callback(text_changed_callback, NULL);
app_editor = new Fl_Text_Editor(0,
app_menu_bar->h(),
app_window->w(),
app_window->h() - app_menu_bar->h());
app_editor->buffer(app_text_buffer);
app_editor->textfont(FL_COURIER);
app_window->resizable(app_editor);
app_window->end();
int ix = app_menu_bar->find_index(menu_quit_callback);
app_menu_bar->insert(ix, "New", FL_COMMAND + 'n', menu_new_callback);
}
int main(int argc, char **argv)
{
build_app_window();
build_app_menu_bar();
build_main_editor();
app_window->show(argc, argv);
return Fl::run();
}