2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
15 #pragma implementation
23 #include "lyxlookup.h"
25 #include "minibuffer.h"
29 #include "layout_forms.h"
32 #include "support/filetools.h" // OnlyFilename()
40 extern FD_form_document * fd_form_document;
42 extern void AutoSave(BufferView *);
44 extern char updatetimer;
46 extern void QuitLyX();
47 LyXTextClass::size_type current_layout = 0;
49 // This is very temporary
50 BufferView * current_view;
52 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
54 LyXView::LyXView(int width, int height)
55 // : update_timeout(300)
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);
76 /// Redraw the main form.
77 void LyXView::redraw() {
78 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
79 fl_redraw_form(form_);
80 minibuffer->Activate();
84 /// returns the buffer currently shown in the main form.
85 Buffer * LyXView::buffer() const
87 return bufferview->buffer();
91 BufferView * LyXView::view() const
97 FD_form_main * LyXView::getMainForm() const
103 FL_FORM * LyXView::getForm() const
109 Toolbar * LyXView::getToolbar() const
115 LyXFunc * LyXView::getLyXFunc() const
121 MiniBuffer * LyXView::getMiniBuffer() const
127 Menus * LyXView::getMenus() const
133 Intl * LyXView::getIntl() const
139 // Callback for update timer
140 void LyXView::UpdateTimerCB(void * ob)
142 LyXView * view = static_cast<LyXView*>(ob);
143 if (!view->view()->available())
150 view->view()->hideCursor();
151 view->view()->update(-2);
153 /* This update can happen, even when the work area has lost
154 * the focus. So suppress the cursor in that case */
161 // Wrapper for the above
162 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
164 LyXView::UpdateTimerCB(ob, data);
169 // Callback for autosave timer
170 void LyXView::AutoSave()
172 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
173 if (view()->available())
178 // Wrapper for the above
181 void C_LyXView_AutosaveTimerCB(void * ob)
183 LyXView * view = static_cast<LyXView*>(ob);
188 /// Reset autosave timer
189 void LyXView::resetAutosaveTimer()
193 autosave_timeout.restart();
195 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
200 // Callback for close main form from window manager
201 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
208 // Wrapper for the above
209 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
211 return LyXView::atCloseMainFormCB(form, p);
215 void LyXView::setPosition(int x, int y)
217 fl_set_form_position(form_, x, y);
221 void LyXView::show(int place, int border, char const * title)
223 fl_show_form(form_, place, border, title);
225 InitLyXLookup(fl_display, form_->window);
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.
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.
238 FD_form_main * fdui = static_cast<FD_form_main *>
239 (fl_calloc(1, sizeof(FD_form_main)));
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);
249 // Parameters for the appearance of the main form
251 int const bw = abs(fl_get_border_width());
257 menus = new Menus(this, air);
263 toolbar = new Toolbar(this, air, 30 + air + bw);
272 int const ywork = 60 + 2 * air + bw;
273 int const workheight = height - ywork - (25 + 2 * air);
275 ::current_view = bufferview = new BufferView(this, air, ywork,
283 minibuffer = new MiniBuffer(this, air, height - (25 + air),
284 width - (2 * air), 25);
292 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
293 0, 0, 0, 0, "Timer");
295 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
297 autosave_timeout.callback(C_LyXView_AutosaveTimerCB, this);
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);
307 //update_timeout.callback(LyXView::UpdateTimerCB, this);
313 // assign an icon to main form
315 Pixmap lyx_p, lyx_mask;
316 lyx_p = fl_create_from_pixmapdata(fl_root,
317 const_cast<char**>(lyx_xpm),
324 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
327 fl_set_form_minsize(fdui->form_main, 50, 50);
333 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
337 // Set the textclass choice
338 invalidateLayoutChoice();
339 updateLayoutChoice();
340 UpdateDocumentClassChoice();
342 // Start autosave timer
345 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
347 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
348 autosave_timeout.start();
351 // Install the raw callback for keyboard events
352 fl_register_raw_callback(form_,
354 C_LyXView_KeyPressMask_raw_callback);
355 intl->InitKeyMapper(lyxrc.use_kbmap);
359 void LyXView::invalidateLayoutChoice()
365 void LyXView::updateLayoutChoice()
367 // Update the layout display
368 if (!toolbar->combox) return;
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();
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());
388 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
390 last_textclass = int(buffer()->params.textclass);
393 // we need to do this.
394 toolbar->combox->Redraw();
396 LyXTextClass::size_type layout = bufferview->text->cursor.par()->GetLayout();
398 if (layout != current_layout){
399 toolbar->combox->select(layout + 1);
400 current_layout = layout;
405 void LyXView::UpdateDocumentClassChoice()
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());
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)
423 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
424 int retval = 0; // 0 means XForms should have a look at this event
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;
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())
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));
447 else if (static_cast<XEvent*>(xev)->type == KeyRelease
448 && view->bufferview->focus()
449 && view->bufferview->active())
451 last_time_released = xke->time;
452 last_key_released = xke->keycode;
453 last_state_released = xke->state;
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.
474 // wrapper for the above
475 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
477 return LyXView::KeyPressMask_raw_callback(fl, xev);
481 // Updates the title of the window with the filename of the current document
482 void LyXView::updateWindowTitle()
484 static string last_title = "LyX";
485 string title = "LyX";
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)");
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());