]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
46b4a0d01b5195ba2e674fcbbf38630b41296ad4
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Lars Gullik Bjornes
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "LyXView.h"
17 #include "debug.h"
18 #include "intl.h"
19 #include "lyxrc.h"
20 #include "lyxtext.h"
21 #include "buffer.h"
22 #include "MenuBackend.h"
23 #include "gettext.h"
24 #include "lyxfunc.h"
25 #include "lyx_cb.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
28
29 #include "Dialogs.h"
30 #include "Toolbar.h"
31 #include "Timeout.h"
32 #include "Menubar.h"
33 #include "controllers/ControlCommandBuffer.h"
34
35 #include "support/filetools.h" // OnlyFilename()
36
37 #include <boost/bind.hpp>
38
39 #include <sys/time.h>
40 #include <unistd.h>
41
42 using std::endl;
43
44 string current_layout;
45
46
47 LyXView::LyXView()
48         : controlcommand_(new ControlCommandBuffer(getLyXFunc())),
49           intl_(new Intl),
50           autosave_timeout_(new Timeout(5000)),
51           lyxfunc_(new LyXFunc(this)),
52           dialogs_(new Dialogs(this))
53 {
54         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
55 }
56
57
58 LyXView::~LyXView()
59 {
60 }
61
62
63 void LyXView::init()
64 {
65         updateLayoutChoice();
66         updateMenubar();
67
68         // Start autosave timer
69         if (lyxrc.autosave) {
70                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
71                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
72                 autosave_timeout_->start();
73         }
74
75         intl_->InitKeyMapper(lyxrc.use_kbmap);
76 }
77
78
79 Buffer * LyXView::buffer() const
80 {
81         return bufferview_->buffer();
82 }
83
84
85 boost::shared_ptr<BufferView> const & LyXView::view() const
86 {
87         return bufferview_;
88 }
89
90
91 void LyXView::setLayout(string const & layout)
92 {
93         toolbar_->setLayout(layout);
94 }
95
96
97 void LyXView::updateToolbar()
98 {
99         toolbar_->update();
100 }
101
102
103 void LyXView::updateMenubar()
104 {
105         menubar_->update();
106 }
107
108
109 void LyXView::autoSave()
110 {
111         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
112
113         if (view()->available()) {
114                 ::AutoSave(view().get());
115         }
116 }
117
118
119 void LyXView::resetAutosaveTimer()
120 {
121         if (lyxrc.autosave)
122                 autosave_timeout_->restart();
123 }
124
125
126 void LyXView::updateLayoutChoice()
127 {
128         // don't show any layouts without a buffer
129         if (!view()->buffer()) {
130                 toolbar_->clearLayoutList();
131                 return;
132         }
133
134         // update the layout display
135         if (toolbar_->updateLayoutList(buffer()->params.textclass)) {
136                 current_layout = buffer()->params.getLyXTextClass().defaultLayoutName();
137         }
138
139         string const & layout =
140                 bufferview_->getLyXText()->cursor.par()->layout()->name();
141
142         if (layout != current_layout) {
143                 toolbar_->setLayout(layout);
144                 current_layout = layout;
145         }
146 }
147
148
149 void LyXView::updateWindowTitle()
150 {
151         static string last_title = "LyX";
152         string title = "LyX";
153         string icon_title = "LyX";
154
155         if (view()->available()) {
156                 string const cur_title = buffer()->fileName();
157                 if (!cur_title.empty()) {
158                         title += ": " + MakeDisplayPath(cur_title, 30);
159                         if (!buffer()->isClean())
160                                 title += _(" (changed)");
161                         if (buffer()->isReadonly())
162                                 title += _(" (read only)");
163                         // Show only the filename if it's available
164                         icon_title = OnlyFilename(cur_title);
165                 }
166         }
167
168         if (title != last_title) {
169                 setWindowTitle(title, icon_title);
170                 last_title = title;
171         }
172 }