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