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()
37 #include "frontends/Dialogs.h"
38 #include "lyx_gui_misc.h" // [update,Close]AllBufferRelatedDialogs
42 extern FD_form_document * fd_form_document;
44 extern void AutoSave(BufferView *);
45 extern void QuitLyX();
46 LyXTextClass::size_type current_layout = 0;
48 // This is very temporary
49 BufferView * current_view;
51 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
53 #ifdef SIGC_CXX_NAMESPACES
54 using SigC::Connection;
58 LyXView::LyXView(int width, int height)
59 // : update_timeout(300)
61 create_form_form_main(width, height);
62 fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
63 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
64 lyxfunc = new LyXFunc(this);
66 dialogs_ = new Dialogs(this);
67 // temporary until all dialogs moved into Dialogs.
68 dialogs_->updateBufferDependent
69 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
70 dialogs_->hideBufferDependent
71 .connect(slot(&CloseAllBufferRelatedDialogs));
87 /// Redraw the main form.
88 void LyXView::redraw() {
89 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
90 fl_redraw_form(form_);
91 minibuffer->Activate();
95 /// returns the buffer currently shown in the main form.
96 Buffer * LyXView::buffer() const
98 return bufferview->buffer();
102 BufferView * LyXView::view() const
108 FD_form_main * LyXView::getMainForm() const
114 FL_FORM * LyXView::getForm() const
120 Toolbar * LyXView::getToolbar() const
126 LyXFunc * LyXView::getLyXFunc() const
132 MiniBuffer * LyXView::getMiniBuffer() const
138 Menus * LyXView::getMenus() const
144 Intl * LyXView::getIntl() const
150 // Callback for autosave timer
151 void LyXView::AutoSave()
153 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
154 if (view()->available())
159 // Wrapper for the above
162 void C_LyXView_AutosaveTimerCB(void * ob)
164 LyXView * view = static_cast<LyXView*>(ob);
169 /// Reset autosave timer
170 void LyXView::resetAutosaveTimer()
173 autosave_timeout.restart();
177 // Callback for close main form from window manager
178 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
185 // Wrapper for the above
186 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
188 return LyXView::atCloseMainFormCB(form, p);
192 void LyXView::setPosition(int x, int y)
194 fl_set_form_position(form_, x, y);
198 void LyXView::show(int place, int border, char const * title)
200 fl_show_form(form_, place, border, title);
202 InitLyXLookup(fl_display, form_->window);
206 void LyXView::create_form_form_main(int width, int height)
207 /* to make this work as it should, .lyxrc should have been
208 * read first; OR maybe this one should be made dynamic.
210 * We will probably not have lyxrc before the main form is
211 * initialized, because error messages from lyxrc parsing
212 * are presented (and rightly so) in GUI popups. Asger.
215 FD_form_main * fdui = static_cast<FD_form_main *>
216 (fl_calloc(1, sizeof(FD_form_main)));
221 form_ = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
222 fdui->form_main->u_vdata = this;
223 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
224 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
226 // Parameters for the appearance of the main form
228 int const bw = abs(fl_get_border_width());
234 menus = new Menus(this, air);
240 toolbar = new Toolbar(this, air, 30 + air + bw);
249 int const ywork = 60 + 2 * air + bw;
250 int const workheight = height - ywork - (25 + 2 * air);
252 ::current_view = bufferview = new BufferView(this, air, ywork,
260 minibuffer = new MiniBuffer(this, air, height - (25 + air),
261 width - (2 * air), 25);
267 autosave_timeout.callback(C_LyXView_AutosaveTimerCB, this);
273 // assign an icon to main form
275 Pixmap lyx_p, lyx_mask;
276 lyx_p = fl_create_from_pixmapdata(fl_root,
277 const_cast<char**>(lyx_xpm),
284 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
287 fl_set_form_minsize(fdui->form_main, 50, 50);
293 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
297 // Set the textclass choice
298 invalidateLayoutChoice();
299 updateLayoutChoice();
300 UpdateDocumentClassChoice();
302 // Start autosave timer
305 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
307 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
308 autosave_timeout.start();
311 // Install the raw callback for keyboard events
312 fl_register_raw_callback(form_,
314 C_LyXView_KeyPressMask_raw_callback);
315 intl->InitKeyMapper(lyxrc.use_kbmap);
319 void LyXView::invalidateLayoutChoice()
325 void LyXView::updateLayoutChoice()
327 // Update the layout display
328 if (!toolbar->combox) return;
330 // This has a side-effect that the layouts are not showed when no
331 // document is loaded.
332 if (bufferview == 0 || bufferview->buffer() == 0) {
333 toolbar->combox->clear();
334 toolbar->combox->Redraw();
338 // If textclass is different, we need to update the list
339 if (toolbar->combox->empty() ||
340 (last_textclass != int(buffer()->params.textclass))) {
341 toolbar->combox->clear();
342 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
343 for (LyXTextClass::const_iterator cit = tc.begin();
344 cit != tc.end(); ++cit) {
345 if ((*cit).obsoleted_by().empty())
346 toolbar->combox->addline((*cit).name().c_str());
348 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
350 last_textclass = int(buffer()->params.textclass);
353 // we need to do this.
354 toolbar->combox->Redraw();
356 LyXTextClass::size_type layout = bufferview->text->cursor.par()->GetLayout();
358 if (layout != current_layout){
359 toolbar->combox->select(layout + 1);
360 current_layout = layout;
365 void LyXView::UpdateDocumentClassChoice()
367 // Update the document class display in the document form
368 if (fd_form_document) {
369 fl_clear_choice(fd_form_document->choice_class);
370 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
371 cit != textclasslist.end(); ++cit) {
372 fl_addto_choice(fd_form_document->choice_class,
373 (*cit).description().c_str());
379 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
380 // as FL_KEYBOARD events :-( Matthias 280596
381 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
383 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
384 int retval = 0; // 0 means XForms should have a look at this event
386 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
387 static Time last_time_pressed = 0;
388 static Time last_time_released = 0;
389 static unsigned int last_key_pressed = 0;
390 static unsigned int last_key_released = 0;
391 static unsigned int last_state_pressed = 0;
392 static unsigned int last_state_released = 0;
394 // funny. Even though the raw_callback is registered with KeyPressMask,
395 // also KeyRelease-events are passed through:-(
396 // [It seems that XForms puts them in pairs... (JMarc)]
397 if (static_cast<XEvent*>(xev)->type == KeyPress
398 && view->bufferview->focus()
399 && view->bufferview->active())
401 last_time_pressed = xke->time;
402 last_key_pressed = xke->keycode;
403 last_state_pressed = xke->state;
404 retval = view->getLyXFunc()
405 ->processKeyEvent(static_cast<XEvent*>(xev));
407 else if (static_cast<XEvent*>(xev)->type == KeyRelease
408 && view->bufferview->focus()
409 && view->bufferview->active())
411 last_time_released = xke->time;
412 last_key_released = xke->keycode;
413 last_state_released = xke->state;
416 if (last_key_released == last_key_pressed
417 && last_state_released == last_state_pressed
418 && last_time_released == last_time_pressed) {
419 // When the diff between last_time_released and
420 // last_time_pressed is 0, that sinifies an autoreapeat
421 // at least on my system. It like some feedback from
422 // others, especially from user running LyX remote.
423 //lyxerr << "Syncing - purging X events." << endl;
424 XSync(fl_get_display(), 1);
425 // This purge make f.ex. scrolling stop imidiatly when
426 // releaseing the PageDown button. The question is if this
427 // purging of XEvents can cause any harm...after some testing
428 // I can see no problems, but I'd like other reports too.
434 // wrapper for the above
435 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
437 return LyXView::KeyPressMask_raw_callback(fl, xev);
441 // Updates the title of the window with the filename of the current document
442 void LyXView::updateWindowTitle()
444 static string last_title = "LyX";
445 string title = "LyX";
447 if (view()->available()) {
448 string cur_title = buffer()->fileName();
449 if (!cur_title.empty()){
450 title += ": " + OnlyFilename(cur_title);
451 if (!buffer()->isLyxClean())
452 title += _(" (Changed)");
453 if (buffer()->isReadonly())
454 title += _(" (read only)");
457 // Don't update title if it's the same as last time
458 if (title != last_title) {
459 fl_set_form_title(form_, title.c_str());