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
214 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
216 return LyXView::atCloseMainFormCB(form, p);
220 void LyXView::setPosition(int x, int y)
222 fl_set_form_position(form_, x, y);
226 void LyXView::show(int place, int border, char const * title)
228 fl_show_form(form_, place, border, title);
230 InitLyXLookup(fl_display, form_->window);
234 void LyXView::create_form_form_main(int width, int height)
235 /* to make this work as it should, .lyxrc should have been
236 * read first; OR maybe this one should be made dynamic.
238 * We will probably not have lyxrc before the main form is
239 * initialized, because error messages from lyxrc parsing
240 * are presented (and rightly so) in GUI popups. Asger.
244 form_ = fl_bgn_form(FL_NO_BOX, width, height);
245 form_->u_vdata = this;
246 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
247 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
249 // Parameters for the appearance of the main form
251 int const bw = abs(fl_get_border_width());
257 menubar = new Menubar(this, menubackend);
259 menus = new Menus(this, air);
266 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
275 int const ywork = 60 + 2 * air + bw;
276 int const workheight = height - ywork - (25 + 2 * air);
278 ::current_view = bufferview = new BufferView(this, air, ywork,
286 minibuffer = new MiniBuffer(this, air, height - (25 + air),
287 width - (2 * air), 25);
293 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
299 // assign an icon to main form
300 string iconname = LibFileSearch("images", "lyx", "xpm");
301 if (!iconname.empty()) {
303 Pixmap lyx_p, lyx_mask;
304 lyx_p = fl_read_pixmapfile(fl_root,
312 fl_set_form_icon(form_, lyx_p, lyx_mask);
316 fl_set_form_minsize(form_, 50, 50);
324 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
330 // Set the textclass choice
331 invalidateLayoutChoice();
332 updateLayoutChoice();
333 UpdateDocumentClassChoice();
338 // Start autosave timer
339 if (lyxrc.autosave) {
340 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
341 autosave_timeout.start();
345 // Install the raw callback for keyboard events
346 fl_register_raw_callback(form_,
348 C_LyXView_KeyPressMask_raw_callback);
350 intl->InitKeyMapper(lyxrc.use_kbmap);
354 void LyXView::invalidateLayoutChoice()
360 void LyXView::updateLayoutChoice()
362 // This has a side-effect that the layouts are not showed when no
363 // document is loaded.
364 if (!view() || !view()->buffer()) {
365 toolbar->clearLayoutList();
369 // Update the layout display
370 if (last_textclass != int(buffer()->params.textclass)) {
371 toolbar->updateLayoutList(true);
372 last_textclass = int(buffer()->params.textclass);
375 toolbar->updateLayoutList(false);
379 LyXTextClass::size_type layout =
380 bufferview->text->cursor.par()->GetLayout();
382 if (layout != current_layout){
383 toolbar->setLayout(layout);
384 current_layout = layout;
389 void LyXView::UpdateDocumentClassChoice()
391 // Update the document class display in the document form
392 if (fd_form_document) {
393 fl_clear_choice(fd_form_document->choice_class);
394 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
395 cit != textclasslist.end(); ++cit) {
396 fl_addto_choice(fd_form_document->choice_class,
397 (*cit).description().c_str());
404 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
405 // as FL_KEYBOARD events :-( Matthias 280596
406 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
408 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
409 int retval = 0; // 0 means XForms should have a look at this event
411 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
412 static Time last_time_pressed = 0;
413 static Time last_time_released = 0;
414 static unsigned int last_key_pressed = 0;
415 static unsigned int last_key_released = 0;
416 static unsigned int last_state_pressed = 0;
417 static unsigned int last_state_released = 0;
419 // funny. Even though the raw_callback is registered with KeyPressMask,
420 // also KeyRelease-events are passed through:-(
421 // [It seems that XForms puts them in pairs... (JMarc)]
422 if (static_cast<XEvent*>(xev)->type == KeyPress
423 && view->bufferview->focus()
424 && view->bufferview->active())
426 last_time_pressed = xke->time;
427 last_key_pressed = xke->keycode;
428 last_state_pressed = xke->state;
429 retval = view->getLyXFunc()
430 ->processKeyEvent(static_cast<XEvent*>(xev));
432 else if (static_cast<XEvent*>(xev)->type == KeyRelease
433 && view->bufferview->focus()
434 && view->bufferview->active())
436 last_time_released = xke->time;
437 last_key_released = xke->keycode;
438 last_state_released = xke->state;
441 if (last_key_released == last_key_pressed
442 && last_state_released == last_state_pressed
443 && last_time_released == last_time_pressed) {
444 // When the diff between last_time_released and
445 // last_time_pressed is 0, that sinifies an autoreapeat
446 // at least on my system. It like some feedback from
447 // others, especially from user running LyX remote.
448 lyxerr[Debug::KEY] << "Syncing - purging X events." << endl;
449 XSync(fl_get_display(), 1);
450 // This purge make f.ex. scrolling stop imidiatly when
451 // releaseing the PageDown button. The question is if this
452 // purging of XEvents can cause any harm...after some testing
453 // I can see no problems, but I'd like other reports too.
459 // wrapper for the above
461 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
463 return LyXView::KeyPressMask_raw_callback(fl, xev);
468 // Updates the title of the window with the filename of the current document
469 void LyXView::updateWindowTitle()
471 static string last_title = "LyX";
472 string title = "LyX";
474 if (view()->available()) {
475 string cur_title = buffer()->fileName();
476 if (!cur_title.empty()){
477 title += ": " + OnlyFilename(cur_title);
478 if (!buffer()->isLyxClean())
479 title += _(" (Changed)");
480 if (buffer()->isReadonly())
481 title += _(" (read only)");
484 // Don't update title if it's the same as last time
485 if (title != last_title) {
486 fl_set_form_title(form_, title.c_str());
492 void LyXView::showState()
494 getMiniBuffer()->Set(CurrentState(view()));
495 getToolbar()->update();