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"
35 #include "frontends/Menubar.h"
36 #include "MenuBackend.h"
37 #include "ToolbarDefaults.h"
38 #include "lyx_gui_misc.h" // [update,Close]AllBufferRelatedDialogs
39 #include "bufferview_funcs.h" // CurrentState()
43 extern FD_form_document * fd_form_document;
45 extern void AutoSave(BufferView *);
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 #ifdef SIGC_CXX_NAMESPACES
55 using SigC::Connection;
59 LyXView::LyXView(int width, int height)
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);
67 // Make sure the buttons are disabled if needed.
71 dialogs_ = new Dialogs(this);
72 // temporary until all dialogs moved into Dialogs.
73 dialogs_->updateBufferDependent
74 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
75 dialogs_->hideBufferDependent
76 .connect(slot(&CloseAllBufferRelatedDialogs));
92 /// Redraw the main form.
93 void LyXView::redraw() {
94 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
95 fl_redraw_form(form_);
96 minibuffer->Activate();
100 /// returns the buffer currently shown in the main form.
101 Buffer * LyXView::buffer() const
103 return bufferview->buffer();
107 BufferView * LyXView::view() const
113 FL_FORM * LyXView::getForm() const
119 Toolbar * LyXView::getToolbar() const
125 void LyXView::setLayout(int layout)
127 toolbar->setLayout(layout);
131 void LyXView::updateToolbar()
138 LyXFunc * LyXView::getLyXFunc() const
144 MiniBuffer * LyXView::getMiniBuffer() const
150 Menubar * LyXView::getMenubar() const
156 void LyXView::updateMenubar()
158 if ((!view() || !view()->buffer())
159 && menubackend.hasMenu("main_nobuffer"))
160 menubar->set("main_nobuffer");
162 menubar->set("main");
166 Intl * LyXView::getIntl() const
172 // Callback for autosave timer
173 void LyXView::AutoSave()
175 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
176 if (view()->available())
181 /// Reset autosave timer
182 void LyXView::resetAutosaveTimer()
185 autosave_timeout.restart();
189 // Callback for close main form from window manager
190 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
197 // Wrapper for the above
199 int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
201 return LyXView::atCloseMainFormCB(form, p);
205 void LyXView::setPosition(int x, int y)
207 fl_set_form_position(form_, x, y);
211 void LyXView::show(int place, int border, string const & title)
213 fl_show_form(form_, place, border, title.c_str());
215 InitLyXLookup(fl_display, form_->window);
219 void LyXView::create_form_form_main(int width, int height)
220 /* to make this work as it should, .lyxrc should have been
221 * read first; OR maybe this one should be made dynamic.
223 * We will probably not have lyxrc before the main form is
224 * initialized, because error messages from lyxrc parsing
225 * are presented (and rightly so) in GUI popups. Asger.
229 form_ = fl_bgn_form(FL_NO_BOX, width, height);
230 form_->u_vdata = this;
231 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
232 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
234 // Parameters for the appearance of the main form
236 int const bw = abs(fl_get_border_width());
241 menubar = new Menubar(this, menubackend);
247 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
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);
274 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
280 // assign an icon to main form
281 string iconname = LibFileSearch("images", "lyx", "xpm");
282 if (!iconname.empty()) {
284 Pixmap lyx_p, lyx_mask;
285 lyx_p = fl_read_pixmapfile(fl_root,
293 fl_set_form_icon(form_, lyx_p, lyx_mask);
297 fl_set_form_minsize(form_, 50, 50);
305 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
311 // Set the textclass choice
312 invalidateLayoutChoice();
313 updateLayoutChoice();
314 UpdateDocumentClassChoice();
317 // Start autosave timer
318 if (lyxrc.autosave) {
319 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
320 autosave_timeout.start();
324 // Install the raw callback for keyboard events
325 fl_register_raw_callback(form_,
327 C_LyXView_KeyPressMask_raw_callback);
329 intl->InitKeyMapper(lyxrc.use_kbmap);
333 void LyXView::invalidateLayoutChoice()
339 void LyXView::updateLayoutChoice()
341 // This has a side-effect that the layouts are not showed when no
342 // document is loaded.
343 if (!view() || !view()->buffer()) {
344 toolbar->clearLayoutList();
348 // Update the layout display
349 if (last_textclass != int(buffer()->params.textclass)) {
350 toolbar->updateLayoutList(true);
351 last_textclass = int(buffer()->params.textclass);
354 toolbar->updateLayoutList(false);
358 LyXTextClass::size_type layout =
359 bufferview->text->cursor.par()->GetLayout();
361 if (layout != current_layout){
362 toolbar->setLayout(layout);
363 current_layout = layout;
368 void LyXView::UpdateDocumentClassChoice()
370 // Update the document class display in the document form
371 if (fd_form_document) {
372 fl_clear_choice(fd_form_document->choice_class);
373 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
374 cit != textclasslist.end(); ++cit) {
375 fl_addto_choice(fd_form_document->choice_class,
376 (*cit).description().c_str());
383 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
384 // as FL_KEYBOARD events :-( Matthias 280596
385 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
387 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
388 int retval = 0; // 0 means XForms should have a look at this event
390 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
391 static Time last_time_pressed = 0;
392 static Time last_time_released = 0;
393 static unsigned int last_key_pressed = 0;
394 static unsigned int last_key_released = 0;
395 static unsigned int last_state_pressed = 0;
396 static unsigned int last_state_released = 0;
398 // funny. Even though the raw_callback is registered with KeyPressMask,
399 // also KeyRelease-events are passed through:-(
400 // [It seems that XForms puts them in pairs... (JMarc)]
401 if (static_cast<XEvent*>(xev)->type == KeyPress
402 && view->bufferview->focus()
403 && view->bufferview->active())
405 last_time_pressed = xke->time;
406 last_key_pressed = xke->keycode;
407 last_state_pressed = xke->state;
408 retval = view->getLyXFunc()
409 ->processKeyEvent(static_cast<XEvent*>(xev));
411 else if (static_cast<XEvent*>(xev)->type == KeyRelease
412 && view->bufferview->focus()
413 && view->bufferview->active())
415 last_time_released = xke->time;
416 last_key_released = xke->keycode;
417 last_state_released = xke->state;
420 if (last_key_released == last_key_pressed
421 && last_state_released == last_state_pressed
422 && last_time_released == last_time_pressed) {
423 // When the diff between last_time_released and
424 // last_time_pressed is 0, that sinifies an autoreapeat
425 // at least on my system. It like some feedback from
426 // others, especially from user running LyX remote.
427 lyxerr[Debug::KEY] << "Syncing - purging X events." << endl;
428 XSync(fl_get_display(), 1);
429 // This purge make f.ex. scrolling stop imidiatly when
430 // releaseing the PageDown button. The question is if this
431 // purging of XEvents can cause any harm...after some testing
432 // I can see no problems, but I'd like other reports too.
438 // wrapper for the above
440 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
442 return LyXView::KeyPressMask_raw_callback(fl, xev);
447 // Updates the title of the window with the filename of the current document
448 void LyXView::updateWindowTitle()
450 static string last_title = "LyX";
451 string title = "LyX";
453 if (view()->available()) {
454 string cur_title = buffer()->fileName();
455 if (!cur_title.empty()){
456 title += ": " + MakeDisplayPath(cur_title, 30);
457 if (!buffer()->isLyxClean())
458 title += _(" (Changed)");
459 if (buffer()->isReadonly())
460 title += _(" (read only)");
463 // Don't update title if it's the same as last time
464 if (title != last_title) {
465 fl_set_form_title(form_, title.c_str());
471 void LyXView::showState()
473 getMiniBuffer()->Set(CurrentState(view()));
474 getToolbar()->update();