1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
22 #include "lyxlookup.h"
23 #include "minibuffer.h"
26 #include "layout_forms.h"
29 #include "support/filetools.h" // OnlyFilename()
33 #include "frontends/Dialogs.h"
34 #include "frontends/Toolbar.h"
36 # include "frontends/Menubar.h"
37 # include "MenuBackend.h"
41 #include "ToolbarDefaults.h"
42 #include "lyx_gui_misc.h" // [update,Close]AllBufferRelatedDialogs
43 #include "bufferview_funcs.h" // CurrentState()
47 extern FD_form_document * fd_form_document;
49 extern void AutoSave(BufferView *);
50 extern void QuitLyX();
51 LyXTextClass::size_type current_layout = 0;
53 // This is very temporary
54 BufferView * current_view;
56 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
58 #ifdef SIGC_CXX_NAMESPACES
59 using SigC::Connection;
63 LyXView::LyXView(int width, int height)
65 create_form_form_main(width, height);
66 fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
67 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
68 lyxfunc = new LyXFunc(this);
71 // Make sure the buttons are disabled if needed.
75 dialogs_ = new Dialogs(this);
76 // temporary until all dialogs moved into Dialogs.
77 dialogs_->updateBufferDependent
78 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
79 dialogs_->hideBufferDependent
80 .connect(slot(&CloseAllBufferRelatedDialogs));
100 /// Redraw the main form.
101 void LyXView::redraw() {
102 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
103 fl_redraw_form(form_);
104 minibuffer->Activate();
108 /// returns the buffer currently shown in the main form.
109 Buffer * LyXView::buffer() const
111 return bufferview->buffer();
115 BufferView * LyXView::view() const
121 FL_FORM * LyXView::getForm() const
127 Toolbar * LyXView::getToolbar() const
133 void LyXView::setLayout(int layout)
135 toolbar->setLayout(layout);
139 void LyXView::updateToolbar()
146 LyXFunc * LyXView::getLyXFunc() const
152 MiniBuffer * LyXView::getMiniBuffer() const
159 Menubar * LyXView::getMenubar() const
165 void LyXView::updateMenubar()
167 if ((!view() || !view()->buffer())
168 && menubackend.hasMenu("main_nobuffer"))
169 menubar->set("main_nobuffer");
171 menubar->set("main");
175 Menus * LyXView::getMenus() const
182 Intl * LyXView::getIntl() const
188 // Callback for autosave timer
189 void LyXView::AutoSave()
191 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
192 if (view()->available())
197 /// Reset autosave timer
198 void LyXView::resetAutosaveTimer()
201 autosave_timeout.restart();
205 // Callback for close main form from window manager
206 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
213 // Wrapper for the above
215 int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
217 return LyXView::atCloseMainFormCB(form, p);
221 void LyXView::setPosition(int x, int y)
223 fl_set_form_position(form_, x, y);
227 void LyXView::show(int place, int border, char const * title)
229 fl_show_form(form_, place, border, title);
231 InitLyXLookup(fl_display, form_->window);
235 void LyXView::create_form_form_main(int width, int height)
236 /* to make this work as it should, .lyxrc should have been
237 * read first; OR maybe this one should be made dynamic.
239 * We will probably not have lyxrc before the main form is
240 * initialized, because error messages from lyxrc parsing
241 * are presented (and rightly so) in GUI popups. Asger.
245 form_ = fl_bgn_form(FL_NO_BOX, width, height);
246 form_->u_vdata = this;
247 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
248 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
250 // Parameters for the appearance of the main form
252 int const bw = abs(fl_get_border_width());
258 menubar = new Menubar(this, menubackend);
260 menus = new Menus(this, air);
267 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
276 int const ywork = 60 + 2 * air + bw;
277 int const workheight = height - ywork - (25 + 2 * air);
279 ::current_view = bufferview = new BufferView(this, air, ywork,
287 minibuffer = new MiniBuffer(this, air, height - (25 + air),
288 width - (2 * air), 25);
294 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
300 // assign an icon to main form
301 string iconname = LibFileSearch("images", "lyx", "xpm");
302 if (!iconname.empty()) {
304 Pixmap lyx_p, lyx_mask;
305 lyx_p = fl_read_pixmapfile(fl_root,
313 fl_set_form_icon(form_, lyx_p, lyx_mask);
317 fl_set_form_minsize(form_, 50, 50);
325 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
331 // Set the textclass choice
332 invalidateLayoutChoice();
333 updateLayoutChoice();
334 UpdateDocumentClassChoice();
339 // Start autosave timer
340 if (lyxrc.autosave) {
341 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
342 autosave_timeout.start();
346 // Install the raw callback for keyboard events
347 fl_register_raw_callback(form_,
349 C_LyXView_KeyPressMask_raw_callback);
351 intl->InitKeyMapper(lyxrc.use_kbmap);
355 void LyXView::invalidateLayoutChoice()
361 void LyXView::updateLayoutChoice()
363 // This has a side-effect that the layouts are not showed when no
364 // document is loaded.
365 if (!view() || !view()->buffer()) {
366 toolbar->clearLayoutList();
370 // Update the layout display
371 if (last_textclass != int(buffer()->params.textclass)) {
372 toolbar->updateLayoutList(true);
373 last_textclass = int(buffer()->params.textclass);
376 toolbar->updateLayoutList(false);
380 LyXTextClass::size_type layout =
381 bufferview->text->cursor.par()->GetLayout();
383 if (layout != current_layout){
384 toolbar->setLayout(layout);
385 current_layout = layout;
390 void LyXView::UpdateDocumentClassChoice()
392 // Update the document class display in the document form
393 if (fd_form_document) {
394 fl_clear_choice(fd_form_document->choice_class);
395 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
396 cit != textclasslist.end(); ++cit) {
397 fl_addto_choice(fd_form_document->choice_class,
398 (*cit).description().c_str());
405 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
406 // as FL_KEYBOARD events :-( Matthias 280596
407 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
409 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
410 int retval = 0; // 0 means XForms should have a look at this event
412 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
413 static Time last_time_pressed = 0;
414 static Time last_time_released = 0;
415 static unsigned int last_key_pressed = 0;
416 static unsigned int last_key_released = 0;
417 static unsigned int last_state_pressed = 0;
418 static unsigned int last_state_released = 0;
420 // funny. Even though the raw_callback is registered with KeyPressMask,
421 // also KeyRelease-events are passed through:-(
422 // [It seems that XForms puts them in pairs... (JMarc)]
423 if (static_cast<XEvent*>(xev)->type == KeyPress
424 && view->bufferview->focus()
425 && view->bufferview->active())
427 last_time_pressed = xke->time;
428 last_key_pressed = xke->keycode;
429 last_state_pressed = xke->state;
430 retval = view->getLyXFunc()
431 ->processKeyEvent(static_cast<XEvent*>(xev));
433 else if (static_cast<XEvent*>(xev)->type == KeyRelease
434 && view->bufferview->focus()
435 && view->bufferview->active())
437 last_time_released = xke->time;
438 last_key_released = xke->keycode;
439 last_state_released = xke->state;
442 if (last_key_released == last_key_pressed
443 && last_state_released == last_state_pressed
444 && last_time_released == last_time_pressed) {
445 // When the diff between last_time_released and
446 // last_time_pressed is 0, that sinifies an autoreapeat
447 // at least on my system. It like some feedback from
448 // others, especially from user running LyX remote.
449 lyxerr[Debug::KEY] << "Syncing - purging X events." << endl;
450 XSync(fl_get_display(), 1);
451 // This purge make f.ex. scrolling stop imidiatly when
452 // releaseing the PageDown button. The question is if this
453 // purging of XEvents can cause any harm...after some testing
454 // I can see no problems, but I'd like other reports too.
460 // wrapper for the above
462 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
464 return LyXView::KeyPressMask_raw_callback(fl, xev);
469 // Updates the title of the window with the filename of the current document
470 void LyXView::updateWindowTitle()
472 static string last_title = "LyX";
473 string title = "LyX";
475 if (view()->available()) {
476 string cur_title = buffer()->fileName();
477 if (!cur_title.empty()){
478 title += ": " + OnlyFilename(cur_title);
479 if (!buffer()->isLyxClean())
480 title += _(" (Changed)");
481 if (buffer()->isReadonly())
482 title += _(" (read only)");
485 // Don't update title if it's the same as last time
486 if (title != last_title) {
487 fl_set_form_title(form_, title.c_str());
493 void LyXView::showState()
495 getMiniBuffer()->Set(CurrentState(view()));
496 getToolbar()->update();