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();
43 extern char updatetimer;
44 extern void QuitLyX();
45 LyXTextClass::size_type current_layout = 0;
47 // This is very temporary
48 BufferView * current_view;
50 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
52 LyXView::LyXView(int width, int height)
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);
73 /// Redraw the main form.
74 void LyXView::redraw() {
75 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
76 fl_redraw_form(form_);
77 minibuffer->Activate();
81 /// returns the buffer currently shown in the main form.
82 Buffer * LyXView::buffer() const
84 return bufferview->buffer();
88 BufferView * LyXView::view() const
94 FD_form_main * LyXView::getMainForm() const
100 FL_FORM * LyXView::getForm() const
106 Toolbar * LyXView::getToolbar() const
112 LyXFunc * LyXView::getLyXFunc() const
118 MiniBuffer * LyXView::getMiniBuffer() const
124 Menus * LyXView::getMenus() const
130 Intl * LyXView::getIntl() const
136 // Callback for update timer
137 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
139 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
140 if (!view->view()->available())
145 view->view()->hideCursor();
146 view->view()->update(-2);
148 /* This update can happen, even when the work area has lost
149 * the focus. So suppress the cursor in that case */
154 // Wrapper for the above
155 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
157 LyXView::UpdateTimerCB(ob, data);
161 // Callback for autosave timer
162 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
164 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
169 // Wrapper for the above
170 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
172 LyXView::AutosaveTimerCB(ob, data);
176 /// Reset autosave timer
177 void LyXView::resetAutosaveTimer()
180 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
184 // Callback for close main form from window manager
185 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
192 // Wrapper for the above
193 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
195 return LyXView::atCloseMainFormCB(form, p);
199 void LyXView::setPosition(int x, int y)
201 fl_set_form_position(form_, x, y);
205 void LyXView::show(int place, int border, char const * title)
207 fl_show_form(form_, place, border, title);
209 InitLyXLookup(fl_display, form_->window);
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.
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.
222 FD_form_main * fdui = static_cast<FD_form_main *>
223 (fl_calloc(1, sizeof(FD_form_main)));
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);
233 // Parameters for the appearance of the main form
235 int const bw = abs(fl_get_border_width());
241 menus = new Menus(this, air);
247 toolbar = new Toolbar(this, air, 30 + air + bw);
256 int const ywork = 60 + 2 * air + bw;
257 int const workheight = height - ywork - (25 + 2 * air);
259 ::current_view = bufferview = new BufferView(this, air, ywork,
267 minibuffer = new MiniBuffer(this, air, height - (25 + air),
268 width - (2 * air), 25);
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);
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);
289 // assign an icon to main form
291 Pixmap lyx_p, lyx_mask;
292 lyx_p = fl_create_from_pixmapdata(fl_root,
293 const_cast<char**>(lyx_xpm),
300 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
303 fl_set_form_minsize(fdui->form_main, 50, 50);
309 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
313 // Set the textclass choice
314 invalidateLayoutChoice();
315 updateLayoutChoice();
316 UpdateDocumentClassChoice();
318 // Start autosave timer
320 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
323 // Install the raw callback for keyboard events
324 fl_register_raw_callback(form_,
326 C_LyXView_KeyPressMask_raw_callback);
327 intl->InitKeyMapper(lyxrc.use_kbmap);
331 void LyXView::invalidateLayoutChoice()
337 void LyXView::updateLayoutChoice()
339 // Update the layout display
340 if (!toolbar->combox) return;
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();
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());
360 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
362 last_textclass = int(buffer()->params.textclass);
365 // we need to do this.
366 toolbar->combox->Redraw();
368 LyXTextClass::size_type layout = bufferview->text->cursor.par->GetLayout();
370 if (layout != current_layout){
371 toolbar->combox->select(layout + 1);
372 current_layout = layout;
377 void LyXView::UpdateDocumentClassChoice()
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());
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)
395 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
396 int retval = 0; // 0 means XForms should have a look at this event
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;
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())
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));
419 else if (static_cast<XEvent*>(xev)->type == KeyRelease
420 && view->bufferview->focus()
421 && view->bufferview->active())
423 last_time_released = xke->time;
424 last_key_released = xke->keycode;
425 last_state_released = xke->state;
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.
446 // wrapper for the above
447 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
449 return LyXView::KeyPressMask_raw_callback(fl, xev);
453 // Updates the title of the window with the filename of the current document
454 void LyXView::updateWindowTitle()
456 static string last_title = "LyX";
457 string title = "LyX";
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)");
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());