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