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 *);
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::AutoSave()
164 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
165 if (view()->available())
170 // Wrapper for the above
171 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long)
173 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
178 /// Reset autosave timer
179 void LyXView::resetAutosaveTimer()
182 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
186 // Callback for close main form from window manager
187 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
194 // Wrapper for the above
195 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
197 return LyXView::atCloseMainFormCB(form, p);
201 void LyXView::setPosition(int x, int y)
203 fl_set_form_position(form_, x, y);
207 void LyXView::show(int place, int border, char const * title)
209 fl_show_form(form_, place, border, title);
211 InitLyXLookup(fl_display, form_->window);
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.
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.
224 FD_form_main * fdui = static_cast<FD_form_main *>
225 (fl_calloc(1, sizeof(FD_form_main)));
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);
235 // Parameters for the appearance of the main form
237 int const bw = abs(fl_get_border_width());
243 menus = new Menus(this, air);
249 toolbar = new Toolbar(this, air, 30 + air + bw);
258 int const ywork = 60 + 2 * air + bw;
259 int const workheight = height - ywork - (25 + 2 * air);
261 ::current_view = bufferview = new BufferView(this, air, ywork,
269 minibuffer = new MiniBuffer(this, air, height - (25 + air),
270 width - (2 * air), 25);
277 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
278 0, 0, 0, 0, "Timer");
280 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
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);
292 // assign an icon to main form
294 Pixmap lyx_p, lyx_mask;
295 lyx_p = fl_create_from_pixmapdata(fl_root,
296 const_cast<char**>(lyx_xpm),
303 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
306 fl_set_form_minsize(fdui->form_main, 50, 50);
312 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
316 // Set the textclass choice
317 invalidateLayoutChoice();
318 updateLayoutChoice();
319 UpdateDocumentClassChoice();
321 // Start autosave timer
323 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
326 // Install the raw callback for keyboard events
327 fl_register_raw_callback(form_,
329 C_LyXView_KeyPressMask_raw_callback);
330 intl->InitKeyMapper(lyxrc.use_kbmap);
334 void LyXView::invalidateLayoutChoice()
340 void LyXView::updateLayoutChoice()
342 // Update the layout display
343 if (!toolbar->combox) return;
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();
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());
363 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
365 last_textclass = int(buffer()->params.textclass);
368 // we need to do this.
369 toolbar->combox->Redraw();
371 LyXTextClass::size_type layout = bufferview->text->cursor.par->GetLayout();
373 if (layout != current_layout){
374 toolbar->combox->select(layout + 1);
375 current_layout = layout;
380 void LyXView::UpdateDocumentClassChoice()
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());
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)
398 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
399 int retval = 0; // 0 means XForms should have a look at this event
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;
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())
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));
422 else if (static_cast<XEvent*>(xev)->type == KeyRelease
423 && view->bufferview->focus()
424 && view->bufferview->active())
426 last_time_released = xke->time;
427 last_key_released = xke->keycode;
428 last_state_released = xke->state;
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.
449 // wrapper for the above
450 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
452 return LyXView::KeyPressMask_raw_callback(fl, xev);
456 // Updates the title of the window with the filename of the current document
457 void LyXView::updateWindowTitle()
459 static string last_title = "LyX";
460 string title = "LyX";
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)");
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());