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