This repository was archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathnotebook.cc
More file actions
721 lines (656 loc) · 26 KB
/
notebook.cc
File metadata and controls
721 lines (656 loc) · 26 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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#include "notebook.h"
#include "config.h"
#include "directories.h"
#include <fstream>
#include <regex>
#include "project.h"
#include "filesystem.h"
#include "selection_dialog.h"
#include "source_clang.h"
#include "source_language_protocol.h"
#include "gtksourceview-3.0/gtksourceview/gtksourcemap.h"
Notebook::TabLabel::TabLabel(const boost::filesystem::path &path, std::function<void()> on_close) {
set_can_focus(false);
set_tooltip_text(path.string());
auto button=Gtk::manage(new Gtk::Button());
auto hbox=Gtk::manage(new Gtk::Box());
hbox->set_can_focus(false);
label.set_text(path.filename().string()+' ');
label.set_can_focus(false);
button->set_image_from_icon_name("window-close-symbolic", Gtk::ICON_SIZE_MENU);
button->set_can_focus(false);
button->set_relief(Gtk::ReliefStyle::RELIEF_NONE);
hbox->pack_start(label, Gtk::PACK_SHRINK);
hbox->pack_end(*button, Gtk::PACK_SHRINK);
add(*hbox);
button->signal_clicked().connect(on_close);
signal_button_press_event().connect([on_close](GdkEventButton *event) {
if(event->button==GDK_BUTTON_MIDDLE) {
on_close();
return true;
}
return false;
});
show_all();
}
Notebook::Notebook() : Gtk::Paned(), notebooks(2) {
Gsv::init();
for(auto ¬ebook: notebooks) {
notebook.set_scrollable();
notebook.set_group_name("source_notebooks");
notebook.signal_switch_page().connect([this](Gtk::Widget *widget, guint) {
auto hbox=dynamic_cast<Gtk::Box*>(widget);
for(size_t c=0;c<hboxes.size();++c) {
if(hboxes[c].get()==hbox) {
focus_view(source_views[c]);
set_current_view(source_views[c]);
break;
}
}
last_index=-1;
});
notebook.signal_page_added().connect([this](Gtk::Widget* widget, guint) {
auto hbox=dynamic_cast<Gtk::Box*>(widget);
for(size_t c=0;c<hboxes.size();++c) {
if(hboxes[c].get()==hbox) {
focus_view(source_views[c]);
set_current_view(source_views[c]);
break;
}
}
});
auto provider = Gtk::CssProvider::create();
//GtkNotebook-tab-overlap got removed in gtk 3.20, but margin works in 3.20
#if GTK_VERSION_GE(3, 20)
provider->load_from_data("tab {border-radius: 5px 5px 0 0; padding: 0 4px; margin: 0;}");
#else
provider->load_from_data(".notebook {-GtkNotebook-tab-overlap: 0px;} tab {border-radius: 5px 5px 0 0; padding: 4px 4px;}");
#endif
notebook.get_style_context()->add_provider(provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
pack1(notebooks[0], true, true);
}
size_t Notebook::size() {
return source_views.size();
}
Source::View* Notebook::get_view(size_t index) {
if(index>=size())
return nullptr;
return source_views[index];
}
Source::View* Notebook::get_current_view() {
if(intermediate_view) {
for(auto view: source_views) {
if(view==intermediate_view)
return view;
}
}
for(auto view: source_views) {
if(view==current_view)
return view;
}
//In case there exist a tab that has not yet received focus again in a different notebook
for(int notebook_index=0;notebook_index<2;++notebook_index) {
auto page=notebooks[notebook_index].get_current_page();
if(page>=0)
return get_view(notebook_index, page);
}
return nullptr;
}
std::vector<Source::View*> &Notebook::get_views() {
return source_views;
}
void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_index) {
auto file_path=filesystem::get_normal_path(file_path_);
if(notebook_index==1 && !split)
toggle_split();
boost::system::error_code ec;
auto canonical_file_path=boost::filesystem::canonical(file_path, ec);
if(ec)
canonical_file_path=file_path;
for(size_t c=0;c<size();c++) {
if(canonical_file_path==source_views[c]->canonical_file_path) {
auto notebook_page=get_notebook_page(c);
notebooks[notebook_page.first].set_current_page(notebook_page.second);
focus_view(source_views[c]);
return;
}
}
if(boost::filesystem::exists(file_path)) {
std::ifstream can_read(file_path.string());
if(!can_read) {
Terminal::get().print("Error: could not open "+file_path.string()+"\n", true);
return;
}
can_read.close();
}
auto last_view=get_current_view();
auto language=Source::guess_language(file_path);
std::string language_protocol_language_id;
if(language) {
language_protocol_language_id=language->get_id();
if(language_protocol_language_id=="js") {
if(file_path.extension()==".ts")
language_protocol_language_id="typescript";
else
language_protocol_language_id="javascript";
}
}
if(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc"))
source_views.emplace_back(new Source::ClangView(file_path, language));
else if(language && !language_protocol_language_id.empty() && !filesystem::find_executable(language_protocol_language_id+"-language-server").empty())
source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id));
else
source_views.emplace_back(new Source::GenericView(file_path, language));
source_views.back()->scroll_to_cursor_delayed=[this](Source::View* view, bool center, bool show_tooltips) {
while(Gtk::Main::events_pending())
Gtk::Main::iteration(false);
if(get_current_view()==view) {
if(center)
view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5);
else
view->scroll_to(view->get_buffer()->get_insert());
if(!show_tooltips)
view->hide_tooltips();
}
};
source_views.back()->update_status_location=[this](Source::View* view) {
if(get_current_view()==view) {
auto iter=view->get_buffer()->get_insert()->get_iter();
status_location.set_text(" "+std::to_string(iter.get_line()+1)+":"+std::to_string(iter.get_line_offset()+1));
}
};
source_views.back()->update_status_file_path=[this](Source::View* view) {
if(get_current_view()==view)
status_file_path.set_text(' '+filesystem::get_short_path(view->file_path).string());
};
source_views.back()->update_status_branch=[this](Source::DiffView* view) {
if(get_current_view()==view) {
if(!view->status_branch.empty())
status_branch.set_text(" ("+view->status_branch+")");
else
status_branch.set_text("");
}
};
source_views.back()->update_modified = [this](Source::View *view) {
const auto view_index = get_index(view);
auto &label = tab_labels.at(view_index)->label;
const auto tab_title = label.get_text();
const auto title = view->get_buffer()->get_modified() ? tab_title + '*' : tab_title.substr(0, tab_title.size() - 1);
label.set_text(title);
};
source_views.back()->update_tab_label = [this](Source::View *view) {
const auto update_label = [&](size_t index, const std::string &prepend) {
const auto current_view = source_views[index];
auto title = prepend + current_view->file_path.filename().string();
auto &tab_label = tab_labels.at(index);
tab_label->label.set_text(title);
if (current_view->update_modified && current_view->get_buffer()->get_modified())
current_view->update_modified(current_view);
tab_label->set_tooltip_text(filesystem::get_short_path(current_view->file_path).string());
};
const auto file_name = view->file_path.filename();
std::string prepend_current_view;
size_t current_view_index = 0;
for (size_t c = 0; c < size(); ++c) {
if (source_views[c] == view) {
current_view_index = c;
continue;
}
if (source_views[c]->file_path.filename() == file_name) {
int diff = 0;
const auto parent_path1 = view->file_path.parent_path();
const auto parent_path2 = source_views[c]->file_path.parent_path();
auto it1 = parent_path1.end();
const auto it1_end = parent_path1.begin();
auto it2 = parent_path2.end();
const auto it2_end = parent_path2.begin();
while (it1 != it1_end && it2 != it2_end && *it1 == *it2) {
--it1;
--it2;
++diff;
}
if (prepend_current_view.empty())
prepend_current_view = it1->string() + (diff > 0 ? "/…/" : "/");
update_label(c, it2->string() + (diff > 0 ? "/…/" : "/"));
}
}
update_label(current_view_index, prepend_current_view);
update_status(view);
};
source_views.back()->update_status_diagnostics=[this](Source::View* view) {
if(get_current_view()==view) {
std::string diagnostic_info;
auto num_warnings=std::get<0>(view->status_diagnostics);
auto num_errors=std::get<1>(view->status_diagnostics);
auto num_fix_its=std::get<2>(view->status_diagnostics);
if(num_warnings>0 || num_errors>0 || num_fix_its>0) {
auto normal_color=get_style_context()->get_color(Gtk::StateFlags::STATE_FLAG_NORMAL);
Gdk::RGBA yellow;
yellow.set_rgba(1.0, 1.0, 0.2);
double factor=0.5;
yellow.set_red(normal_color.get_red()+factor*(yellow.get_red()-normal_color.get_red()));
yellow.set_green(normal_color.get_green()+factor*(yellow.get_green()-normal_color.get_green()));
yellow.set_blue(normal_color.get_blue()+factor*(yellow.get_blue()-normal_color.get_blue()));
Gdk::RGBA red;
red.set_rgba(1.0, 0.0, 0.0);
factor=0.5;
red.set_red(normal_color.get_red()+factor*(red.get_red()-normal_color.get_red()));
red.set_green(normal_color.get_green()+factor*(red.get_green()-normal_color.get_green()));
red.set_blue(normal_color.get_blue()+factor*(red.get_blue()-normal_color.get_blue()));
Gdk::RGBA green;
green.set_rgba(0.0, 1.0, 0.0);
factor=0.4;
green.set_red(normal_color.get_red()+factor*(green.get_red()-normal_color.get_red()));
green.set_green(normal_color.get_green()+factor*(green.get_green()-normal_color.get_green()));
green.set_blue(normal_color.get_blue()+factor*(green.get_blue()-normal_color.get_blue()));
std::stringstream yellow_ss, red_ss, green_ss;
yellow_ss << std::hex << std::setfill('0') << std::setw(2) << (int)(yellow.get_red_u()>>8) << std::setw(2) << (int)(yellow.get_green_u()>>8) << std::setw(2) << (int)(yellow.get_blue_u()>>8);
red_ss << std::hex << std::setfill('0') << std::setw(2) << (int)(red.get_red_u()>>8) << std::setw(2) << (int)(red.get_green_u()>>8) << std::setw(2) << (int)(red.get_blue_u()>>8);
green_ss << std::hex << std::setfill('0') << std::setw(2) << (int)(green.get_red_u()>>8) << std::setw(2) << (int)(green.get_green_u()>>8) << std::setw(2) << (int)(green.get_blue_u()>>8);
if(num_warnings>0) {
diagnostic_info+="<span color='#"+yellow_ss.str()+"'>";
diagnostic_info+=std::to_string(num_warnings)+" warning";
if(num_warnings>1)
diagnostic_info+='s';
diagnostic_info+="</span>";
}
if(num_errors>0) {
if(num_warnings>0)
diagnostic_info+=", ";
diagnostic_info+="<span color='#"+red_ss.str()+"'>";
diagnostic_info+=std::to_string(num_errors)+" error";
if(num_errors>1)
diagnostic_info+='s';
diagnostic_info+="</span>";
}
if(num_fix_its>0) {
if(num_warnings>0 || num_errors>0)
diagnostic_info+=", ";
diagnostic_info+="<span color='#"+green_ss.str()+"'>";
diagnostic_info+=std::to_string(num_fix_its)+" fix it";
if(num_fix_its>1)
diagnostic_info+='s';
diagnostic_info+="</span>";
}
}
status_diagnostics.set_markup(diagnostic_info);
}
};
source_views.back()->update_status_state=[this](Source::View* view) {
if(get_current_view()==view)
status_state.set_text(view->status_state+" ");
};
scrolled_windows.emplace_back(new Gtk::ScrolledWindow());
hboxes.emplace_back(new Gtk::Box());
scrolled_windows.back()->add(*source_views.back());
hboxes.back()->pack_start(*scrolled_windows.back());
source_maps.emplace_back(Glib::wrap(gtk_source_map_new()));
gtk_source_map_set_view(GTK_SOURCE_MAP(source_maps.back()->gobj()), source_views.back()->gobj());
configure(source_views.size()-1);
//Set up tab label
auto source_view=source_views.back();
tab_labels.emplace_back(new TabLabel(file_path, [this, source_view]() {
auto index=get_index(source_view);
if(index!=static_cast<size_t>(-1))
close(index);
}));
if(source_view->update_tab_label)
source_view->update_tab_label(source_view);
source_view->get_buffer()->signal_modified_changed().connect([source_view]() {
if (source_view->update_modified)
source_view->update_modified(source_view);
});
//Cursor history
auto update_cursor_locations=[this, source_view](const Gtk::TextBuffer::iterator &iter) {
bool mark_moved=false;
if(current_cursor_location!=static_cast<size_t>(-1)) {
auto &cursor_location=cursor_locations.at(current_cursor_location);
if(cursor_location.view==source_view && abs(cursor_location.mark->get_iter().get_line()-iter.get_line())<=2) {
source_view->get_buffer()->move_mark(cursor_location.mark, iter);
mark_moved=true;
}
}
if(!mark_moved) {
if(current_cursor_location!=static_cast<size_t>(-1)) {
for(auto it=cursor_locations.begin()+current_cursor_location+1;it!=cursor_locations.end();) {
it->view->get_buffer()->delete_mark(it->mark);
it=cursor_locations.erase(it);
}
}
cursor_locations.emplace_back(source_view, source_view->get_buffer()->create_mark(iter));
current_cursor_location=cursor_locations.size()-1;
}
// Combine adjacent cursor histories that are similar
if(!cursor_locations.empty()) {
size_t cursor_locations_index=1;
auto last_it=cursor_locations.begin();
for(auto it=cursor_locations.begin()+1;it!=cursor_locations.end();) {
if(last_it->view==it->view && abs(last_it->mark->get_iter().get_line()-it->mark->get_iter().get_line())<=2) {
last_it->view->get_buffer()->delete_mark(last_it->mark);
last_it->mark=it->mark;
it=cursor_locations.erase(it);
if(current_cursor_location!=static_cast<size_t>(-1) && current_cursor_location>cursor_locations_index)
--current_cursor_location;
}
else {
++it;
++last_it;
++cursor_locations_index;
}
}
}
// Remove start of cache if cache limit is exceeded
while(cursor_locations.size()>10) {
cursor_locations.begin()->view->get_buffer()->delete_mark(cursor_locations.begin()->mark);
cursor_locations.erase(cursor_locations.begin());
if(current_cursor_location!=static_cast<size_t>(-1))
--current_cursor_location;
}
if(current_cursor_location>=cursor_locations.size())
current_cursor_location=cursor_locations.size()-1;
};
source_view->get_buffer()->signal_mark_set().connect([this, source_view, update_cursor_locations](const Gtk::TextBuffer::iterator &iter, const Glib::RefPtr<Gtk::TextBuffer::Mark> &mark) {
if(mark->get_name()=="insert") {
if(disable_next_update_cursor_locations) {
disable_next_update_cursor_locations=false;
return;
}
update_cursor_locations(iter);
}
});
source_view->get_buffer()->signal_changed().connect([source_view, update_cursor_locations] {
update_cursor_locations(source_view->get_buffer()->get_insert()->get_iter());
});
#ifdef JUCI_ENABLE_DEBUG
if(dynamic_cast<Source::ClangView*>(source_view) || (source_view->language && source_view->language->get_id()=="rust")) {
source_view->toggle_breakpoint=[source_view](int line_nr) {
if(source_view->get_source_buffer()->get_source_marks_at_line(line_nr, "debug_breakpoint").size()>0) {
auto start_iter=source_view->get_buffer()->get_iter_at_line(line_nr);
auto end_iter=source_view->get_iter_at_line_end(line_nr);
source_view->get_source_buffer()->remove_source_marks(start_iter, end_iter, "debug_breakpoint");
source_view->get_source_buffer()->remove_source_marks(start_iter, end_iter, "debug_breakpoint_and_stop");
if(Project::current && Project::debugging)
Project::current->debug_remove_breakpoint(source_view->file_path, line_nr+1, source_view->get_buffer()->get_line_count()+1);
}
else {
auto iter=source_view->get_buffer()->get_iter_at_line(line_nr);
source_view->get_source_buffer()->create_source_mark("debug_breakpoint", iter);
if(source_view->get_source_buffer()->get_source_marks_at_line(line_nr, "debug_stop").size()>0)
source_view->get_source_buffer()->create_source_mark("debug_breakpoint_and_stop", iter);
if(Project::current && Project::debugging)
Project::current->debug_add_breakpoint(source_view->file_path, line_nr+1);
}
};
}
#endif
source_view->signal_focus_in_event().connect([this, source_view](GdkEventFocus *) {
set_current_view(source_view);
return false;
});
if(notebook_index==static_cast<size_t>(-1)) {
if(!split)
notebook_index=0;
else if(notebooks[0].get_n_pages()==0)
notebook_index=0;
else if(notebooks[1].get_n_pages()==0)
notebook_index=1;
else if(last_view)
notebook_index=get_notebook_page(get_index(last_view)).first;
}
auto ¬ebook=notebooks[notebook_index];
notebook.append_page(*hboxes.back(), *tab_labels.back());
notebook.set_tab_reorderable(*hboxes.back(), true);
notebook.set_tab_detachable(*hboxes.back(), true);
show_all_children();
notebook.set_current_page(notebook.get_n_pages()-1);
last_index=-1;
if(last_view) {
auto index=get_index(last_view);
auto notebook_page=get_notebook_page(index);
if(notebook_page.first==notebook_index)
last_index=index;
}
set_focus_child(*source_views.back());
source_view->get_buffer()->set_modified(false);
focus_view(source_view);
}
void Notebook::configure(size_t index) {
auto source_font_description=Pango::FontDescription(Config::get().source.font);
auto source_map_font_desc=Pango::FontDescription(static_cast<std::string>(source_font_description.get_family())+" "+Config::get().source.map_font_size);
source_maps.at(index)->override_font(source_map_font_desc);
if(Config::get().source.show_map) {
if(hboxes.at(index)->get_children().size()==1)
hboxes.at(index)->pack_end(*source_maps.at(index), Gtk::PACK_SHRINK);
}
else if(hboxes.at(index)->get_children().size()==2)
hboxes.at(index)->remove(*source_maps.at(index));
}
bool Notebook::save(size_t index) {
if(!source_views[index]->save())
return false;
Project::on_save(index);
return true;
}
bool Notebook::save_current() {
if(auto view=get_current_view())
return save(get_index(view));
return false;
}
bool Notebook::close(size_t index) {
if(auto view=get_view(index)) {
if(view->get_buffer()->get_modified()){
if(!save_modified_dialog(index))
return false;
}
if(view==get_current_view()) {
bool focused=false;
if(last_index!=static_cast<size_t>(-1)) {
auto notebook_page=get_notebook_page(last_index);
if(notebook_page.first==get_notebook_page(get_index(view)).first) {
focus_view(source_views[last_index]);
notebooks[notebook_page.first].set_current_page(notebook_page.second);
last_index=-1;
focused=true;
}
}
if(!focused) {
auto notebook_page=get_notebook_page(get_index(view));
if(notebook_page.second>0)
focus_view(get_view(notebook_page.first, notebook_page.second-1));
else {
size_t notebook_index=notebook_page.first==0?1:0;
if(notebooks[notebook_index].get_n_pages()>0)
focus_view(get_view(notebook_index, notebooks[notebook_index].get_current_page()));
else
set_current_view(nullptr);
}
}
}
else if(index==last_index)
last_index=-1;
else if(index<last_index && last_index!=static_cast<size_t>(-1))
last_index--;
auto notebook_page=get_notebook_page(index);
notebooks[notebook_page.first].remove_page(notebook_page.second);
source_maps.erase(source_maps.begin()+index);
if(on_close_page)
on_close_page(view);
delete_cursor_locations(view);
SelectionDialog::get()=nullptr;
CompletionDialog::get()=nullptr;
if(auto clang_view=dynamic_cast<Source::ClangView*>(view))
clang_view->async_delete();
else
delete view;
source_views.erase(source_views.begin()+index);
scrolled_windows.erase(scrolled_windows.begin()+index);
hboxes.erase(hboxes.begin()+index);
tab_labels.erase(tab_labels.begin()+index);
}
for (auto view : get_views()) {
if (view->update_tab_label)
view->update_tab_label(view);
}
return true;
}
void Notebook::delete_cursor_locations(Source::View *view) {
size_t cursor_locations_index=0;
for(auto it=cursor_locations.begin();it!=cursor_locations.end();) {
if(it->view==view) {
view->get_buffer()->delete_mark(it->mark);
it=cursor_locations.erase(it);
if(current_cursor_location!=static_cast<size_t>(-1) && current_cursor_location>cursor_locations_index)
--current_cursor_location;
}
else {
++it;
++cursor_locations_index;
}
}
if(current_cursor_location>=cursor_locations.size())
current_cursor_location=cursor_locations.size()-1;
}
bool Notebook::close_current() {
return close(get_index(get_current_view()));
}
void Notebook::next() {
if(auto view=get_current_view()) {
auto notebook_page=get_notebook_page(get_index(view));
int page=notebook_page.second+1;
if(page>=notebooks[notebook_page.first].get_n_pages())
notebooks[notebook_page.first].set_current_page(0);
else
notebooks[notebook_page.first].set_current_page(page);
}
}
void Notebook::previous() {
if(auto view=get_current_view()) {
auto notebook_page=get_notebook_page(get_index(view));
int page=notebook_page.second-1;
if(page<0)
notebooks[notebook_page.first].set_current_page(notebooks[notebook_page.first].get_n_pages()-1);
else
notebooks[notebook_page.first].set_current_page(page);
}
}
void Notebook::toggle_split() {
if(!split) {
pack2(notebooks[1], true, true);
set_position(get_width()/2);
show_all();
//Make sure the position is correct
//TODO: report bug to gtk if it is not fixed in gtk3.22
Glib::signal_timeout().connect([this] {
set_position(get_width()/2);
return false;
}, 200);
}
else {
for(size_t c=size()-1;c!=static_cast<size_t>(-1);--c) {
auto notebook_index=get_notebook_page(c).first;
if(notebook_index==1 && !close(c))
return;
}
remove(notebooks[1]);
}
split=!split;
}
void Notebook::toggle_tabs() {
//Show / Hide tabs for each notebook.
for(auto ¬ebook : Notebook::notebooks)
notebook.set_show_tabs(!notebook.get_show_tabs());
}
boost::filesystem::path Notebook::get_current_folder() {
if(!Directories::get().path.empty())
return Directories::get().path;
else if(auto view=get_current_view())
return view->file_path.parent_path();
else
return boost::filesystem::path();
}
std::vector<std::pair<size_t, Source::View *>> Notebook::get_notebook_views() {
std::vector<std::pair<size_t, Source::View *>> notebook_views;
for(size_t notebook_index=0;notebook_index<notebooks.size();++notebook_index) {
for(int page=0;page<notebooks[notebook_index].get_n_pages();++page) {
if(auto view=get_view(notebook_index, page))
notebook_views.emplace_back(notebook_index, view);
}
}
return notebook_views;
}
void Notebook::update_status(Source::View *view) {
if(view->update_status_location)
view->update_status_location(view);
if(view->update_status_file_path)
view->update_status_file_path(view);
if(view->update_status_branch)
view->update_status_branch(view);
if(view->update_status_diagnostics)
view->update_status_diagnostics(view);
if(view->update_status_state)
view->update_status_state(view);
}
void Notebook::clear_status() {
status_location.set_text("");
status_file_path.set_text("");
status_branch.set_text("");
status_diagnostics.set_text("");
status_state.set_text("");
}
size_t Notebook::get_index(Source::View *view) {
for(size_t c=0;c<size();++c) {
if(source_views[c]==view)
return c;
}
return -1;
}
Source::View *Notebook::get_view(size_t notebook_index, int page) {
if(notebook_index==static_cast<size_t>(-1) || notebook_index>=notebooks.size() ||
page<0 || page>=notebooks[notebook_index].get_n_pages())
return nullptr;
auto hbox=dynamic_cast<Gtk::Box*>(notebooks[notebook_index].get_nth_page(page));
auto scrolled_window=dynamic_cast<Gtk::ScrolledWindow*>(hbox->get_children()[0]);
return dynamic_cast<Source::View*>(scrolled_window->get_children()[0]);
}
void Notebook::focus_view(Source::View *view) {
intermediate_view=view;
view->grab_focus();
}
std::pair<size_t, int> Notebook::get_notebook_page(size_t index) {
if(index>=hboxes.size())
return {-1, -1};
for(size_t c=0;c<notebooks.size();++c) {
auto page_num=notebooks[c].page_num(*hboxes[index]);
if(page_num>=0)
return {c, page_num};
}
return {-1, -1};
}
void Notebook::set_current_view(Source::View *view) {
intermediate_view=nullptr;
if(current_view!=view) {
if(auto view=get_current_view()) {
view->hide_tooltips();
view->hide_dialogs();
}
current_view=view;
if(view && on_change_page)
on_change_page(view);
}
}
bool Notebook::save_modified_dialog(size_t index) {
Gtk::MessageDialog dialog(*static_cast<Gtk::Window*>(get_toplevel()), "Save file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog.set_default_response(Gtk::RESPONSE_YES);
dialog.set_secondary_text("Do you want to save: " + get_view(index)->file_path.string()+" ?");
int result = dialog.run();
if(result==Gtk::RESPONSE_YES) {
return save(index);
}
else if(result==Gtk::RESPONSE_NO) {
return true;
}
else {
return false;
}
}