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