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