]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
more changes, read the Changelog
[lyx.git] / src / LyXView.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 #include "LyXView.h"
22 #include "lyx_main.h"
23 #include "lyxlookup.h"
24 #include "toolbar.h"
25 #include "minibuffer.h"
26 #include "lyxfunc.h"
27 #include "lyx.xpm"
28 #include "debug.h"
29 #include "layout_forms.h"
30 #include "intl.h"
31 #include "lyxrc.h"
32 #include "support/filetools.h"        // OnlyFilename()
33 #include "layout.h"
34 #include "lyxtext.h"
35 #include "buffer.h"
36 #include "menus.h"
37 #include "frontends/Dialogs.h"
38 #include "lyx_gui_misc.h"       // [update,Close]AllBufferRelatedDialogs
39
40 using std::endl;
41
42 extern FD_form_document * fd_form_document;
43
44 extern void AutoSave(BufferView *);
45 extern void QuitLyX();
46 LyXTextClass::size_type current_layout = 0;
47
48 // This is very temporary
49 BufferView * current_view;
50
51 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
52
53 #ifdef SIGC_CXX_NAMESPACES
54 using SigC::Connection;
55 using SigC::slot;
56 #endif
57
58 LyXView::LyXView(int width, int height)
59 {
60         create_form_form_main(width, height);
61         fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
62         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
63         lyxfunc = new LyXFunc(this);
64         intl = new Intl;
65
66         dialogs_ = new Dialogs(this);
67         // temporary until all dialogs moved into Dialogs.
68         dialogs_->updateBufferDependent
69                 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
70         dialogs_->hideBufferDependent
71                 .connect(slot(&CloseAllBufferRelatedDialogs));
72 }
73
74
75 LyXView::~LyXView()
76 {
77         delete menus;
78         delete toolbar;
79         delete bufferview;
80         delete minibuffer;
81         delete lyxfunc;
82         delete intl;
83         delete dialogs_;
84 }
85
86
87 /// Redraw the main form.
88 void LyXView::redraw() {
89         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
90         fl_redraw_form(form_);
91         minibuffer->Activate();
92 }
93
94
95 /// returns the buffer currently shown in the main form.
96 Buffer * LyXView::buffer() const
97 {
98         return bufferview->buffer();
99 }
100
101
102 BufferView * LyXView::view() const
103 {
104         return bufferview;
105 }
106
107
108 FL_FORM * LyXView::getForm() const
109 {
110         return form_;
111 }
112
113
114 Toolbar * LyXView::getToolbar() const
115 {
116         return toolbar;
117 }
118
119
120 LyXFunc * LyXView::getLyXFunc() const
121 {
122         return lyxfunc;
123 }
124
125
126 MiniBuffer * LyXView::getMiniBuffer() const
127 {
128         return minibuffer;
129 }
130
131
132 Menus * LyXView::getMenus() const
133 {
134         return menus;
135 }
136
137
138 Intl * LyXView::getIntl() const
139 {
140         return intl;
141 }
142
143
144 // Callback for autosave timer
145 void LyXView::AutoSave()
146 {
147         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
148         if (view()->available())
149                 ::AutoSave(view());
150 }
151
152
153 /// Reset autosave timer
154 void LyXView::resetAutosaveTimer()
155 {
156         if (lyxrc.autosave)
157                 autosave_timeout.restart();
158 }
159
160
161 // Callback for close main form from window manager
162 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
163 {
164         QuitLyX();
165         return FL_IGNORE;
166 }
167
168
169 // Wrapper for the above
170 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
171 {
172         return LyXView::atCloseMainFormCB(form, p);
173 }
174
175
176 void LyXView::setPosition(int x, int y)
177 {
178         fl_set_form_position(form_, x, y);
179 }
180
181
182 void LyXView::show(int place, int border, char const * title)
183 {
184         fl_show_form(form_, place, border, title);
185         minibuffer->Init();
186         InitLyXLookup(fl_display, form_->window);
187 }
188
189
190 void LyXView::create_form_form_main(int width, int height)
191         /* to make this work as it should, .lyxrc should have been
192          * read first; OR maybe this one should be made dynamic.
193          * Hmmmm. Lgb. 
194          * We will probably not have lyxrc before the main form is
195          * initialized, because error messages from lyxrc parsing 
196          * are presented (and rightly so) in GUI popups. Asger. 
197          */
198 {
199         // the main form
200         form_ = fl_bgn_form(FL_NO_BOX, width, height);
201         form_->u_vdata = this;
202         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
203         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
204
205         // Parameters for the appearance of the main form
206         int const air = 2;
207         int const bw = abs(fl_get_border_width());
208         
209         //
210         // THE MENUBAR
211         //
212
213         menus = new Menus(this, air);
214
215         //
216         // TOOLBAR
217         //
218
219         toolbar = new Toolbar(this, air, 30 + air + bw);
220
221         // Setup the toolbar
222         toolbar->set(true);
223
224         //
225         // WORKAREA
226         //
227
228         int const ywork = 60 + 2 * air + bw;
229         int const workheight = height - ywork - (25 + 2 * air);
230
231         ::current_view = bufferview = new BufferView(this, air, ywork,
232                                                      width - 3 * air,
233                                                      workheight);
234
235         //
236         // MINIBUFFER
237         //
238
239         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
240                                     width - (2 * air), 25);
241
242         //
243         // TIMERS
244         //
245
246         autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
247         
248         //
249         // Misc
250         //
251
252         //  assign an icon to main form
253         unsigned int w, h;
254         Pixmap lyx_p, lyx_mask;
255         lyx_p = fl_create_from_pixmapdata(fl_root,
256                                           const_cast<char**>(lyx_xpm),
257                                           &w,
258                                           &h,
259                                           &lyx_mask,
260                                           0,
261                                           0,
262                                           0); // this leaks
263         fl_set_form_icon(form_, lyx_p, lyx_mask);
264
265         // set min size
266         fl_set_form_minsize(form_, 50, 50);
267         
268         fl_end_form();
269 }
270
271
272 extern "C"
273 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
274
275 void LyXView::init()
276 {
277         // Set the textclass choice
278         invalidateLayoutChoice();
279         updateLayoutChoice();
280         UpdateDocumentClassChoice();
281         
282         // Start autosave timer
283         if (lyxrc.autosave) {
284                 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
285                 autosave_timeout.start();
286         }
287         
288         // Install the raw callback for keyboard events 
289         fl_register_raw_callback(form_,
290                                  KeyPressMask,
291                                  C_LyXView_KeyPressMask_raw_callback);
292         intl->InitKeyMapper(lyxrc.use_kbmap);
293 }
294
295
296 void LyXView::invalidateLayoutChoice()
297 {
298         last_textclass = -1;
299 }
300
301
302 void LyXView::updateLayoutChoice()
303 {
304         // Update the layout display
305         if (!toolbar->combox) return;
306
307         // This has a side-effect that the layouts are not showed when no
308         // document is loaded.
309         if (bufferview == 0 || bufferview->buffer() == 0) {
310                 toolbar->combox->clear();
311                 toolbar->combox->Redraw();
312                 return; 
313         }
314
315         // If textclass is different, we need to update the list
316         if (toolbar->combox->empty() ||
317             (last_textclass != int(buffer()->params.textclass))) {
318                 toolbar->combox->clear();
319                 LyXTextClass const & tc =
320                         textclasslist.TextClass(buffer()->params.textclass);
321                 for (LyXTextClass::const_iterator cit = tc.begin();
322                      cit != tc.end(); ++cit) {
323                         if ((*cit).obsoleted_by().empty())
324                                 toolbar->combox->addline((*cit).name().c_str());
325                         else
326                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
327                 }
328                 last_textclass = int(buffer()->params.textclass);
329                 current_layout = 0;
330         }
331         // we need to do this.
332         toolbar->combox->Redraw();
333
334         LyXTextClass::size_type layout =
335                 bufferview->text->cursor.par()->GetLayout();
336
337         if (layout != current_layout){
338                 toolbar->combox->select(layout + 1);
339                 current_layout = layout;
340         }
341 }
342
343
344 void LyXView::UpdateDocumentClassChoice()
345 {
346         // Update the document class display in the document form
347         if (fd_form_document) {
348                 fl_clear_choice(fd_form_document->choice_class);
349                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
350                      cit != textclasslist.end(); ++cit) {
351                         fl_addto_choice(fd_form_document->choice_class,
352                                         (*cit).description().c_str());
353                 }
354         }
355 }
356
357
358 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
359 // as FL_KEYBOARD events :-(   Matthias 280596
360 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
361 {
362         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
363         int retval = 0;  // 0 means XForms should have a look at this event
364
365         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
366         static Time last_time_pressed = 0;
367         static Time last_time_released = 0;
368         static unsigned int last_key_pressed = 0;
369         static unsigned int last_key_released = 0;
370         static unsigned int last_state_pressed = 0;
371         static unsigned int last_state_released = 0;
372
373         // funny. Even though the raw_callback is registered with KeyPressMask,
374         // also KeyRelease-events are passed through:-(
375         // [It seems that XForms puts them in pairs... (JMarc)]
376         if (static_cast<XEvent*>(xev)->type == KeyPress
377             && view->bufferview->focus()
378             && view->bufferview->active())
379                 {
380                 last_time_pressed = xke->time;
381                 last_key_pressed = xke->keycode;
382                 last_state_pressed = xke->state;
383                 retval = view->getLyXFunc()
384                         ->processKeyEvent(static_cast<XEvent*>(xev));
385         }
386         else if (static_cast<XEvent*>(xev)->type == KeyRelease
387                  && view->bufferview->focus()
388                  && view->bufferview->active())
389 {
390                 last_time_released = xke->time;
391                 last_key_released = xke->keycode;
392                 last_state_released = xke->state;
393         }
394
395         if (last_key_released == last_key_pressed
396             && last_state_released == last_state_pressed
397             && last_time_released == last_time_pressed) {
398                 // When the diff between last_time_released and
399                 // last_time_pressed is 0, that sinifies an autoreapeat
400                 // at least on my system. It like some feedback from
401                 // others, especially from user running LyX remote.
402                 //lyxerr << "Syncing - purging X events." << endl;
403                 XSync(fl_get_display(), 1);
404                 // This purge make f.ex. scrolling stop imidiatly when
405                 // releaseing the PageDown button. The question is if this
406                 // purging of XEvents can cause any harm...after some testing
407                 // I can see no problems, but I'd like other reports too.
408         }
409         return retval;
410 }
411
412
413 // wrapper for the above
414 extern "C"
415 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
416 {
417         return LyXView::KeyPressMask_raw_callback(fl, xev);
418 }
419
420
421 // Updates the title of the window with the filename of the current document
422 void LyXView::updateWindowTitle()
423 {
424         static string last_title = "LyX";
425         string title = "LyX";
426
427         if (view()->available()) {
428                 string cur_title = buffer()->fileName();
429                 if (!cur_title.empty()){
430                         title += ": " + OnlyFilename(cur_title);
431                         if (!buffer()->isLyxClean())
432                                 title += _(" (Changed)");
433                         if (buffer()->isReadonly())
434                                 title += _(" (read only)");
435                 }
436         }
437         // Don't update title if it's the same as last time
438         if (title != last_title) {
439                 fl_set_form_title(form_, title.c_str());
440                 last_title = title;
441         }
442 }