- // we may have been called through the close window button
- // which bypasses the LFUN machinery.
- if (!d.quitting_by_menu_ && guiApp->viewCount() == 1) {
- if (!quitWriteAll()) {
+ // it can happen that this event arrives without selecting the view,
+ // e.g. when clicking the close button on a background window.
+ theLyXFunc().setLyXView(this);
+
+ while (Buffer * b = buffer()) {
+ if (b->parent()) {
+ // This is a child document, just close the tab after saving
+ // but keep the file loaded.
+ if (!saveBuffer(*b)) {
+ close_event->ignore();
+ return;
+ }
+ removeWorkArea(d.current_work_area_);
+ continue;
+ }
+
+ std::vector<int> const & ids = guiApp->viewIds();
+ for (size_type i = 0; i != ids.size(); ++i) {
+ if (id_ == ids[i])
+ continue;
+ if (guiApp->view(ids[i]).workArea(*b)) {
+ // FIXME 1: should we put an alert box here that the buffer
+ // is viewed elsewhere?
+ // FIXME 2: should we try to save this buffer in any case?
+ //saveBuffer(b);
+
+ // This buffer is also opened in another view, so
+ // but close the associated work area nevertheless.
+ removeWorkArea(d.current_work_area_);
+ // but don't close it.
+ b = 0;
+ break;
+ }
+ }
+ if (b && !closeBuffer(*b)) {