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);
323 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
327 // Set the textclass choice
328 invalidateLayoutChoice();
329 updateLayoutChoice();
330 UpdateDocumentClassChoice();
335 // Start autosave timer
336 if (lyxrc.autosave) {
337 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
338 autosave_timeout.start();
341 // Install the raw callback for keyboard events
342 fl_register_raw_callback(form_,
344 C_LyXView_KeyPressMask_raw_callback);
345 intl->InitKeyMapper(lyxrc.use_kbmap);
349 void LyXView::invalidateLayoutChoice()
355 void LyXView::updateLayoutChoice()
357 // This has a side-effect that the layouts are not showed when no
358 // document is loaded.
359 if (!view() || !view()->buffer()) {
360 toolbar->clearLayoutList();
364 // Update the layout display
365 if (last_textclass != int(buffer()->params.textclass)) {
366 toolbar->updateLayoutList(true);
367 last_textclass = int(buffer()->params.textclass);
370 toolbar->updateLayoutList(false);
374 LyXTextClass::size_type layout =
375 bufferview->text->cursor.par()->GetLayout();
377 if (layout != current_layout){
378 toolbar->setLayout(layout);
379 current_layout = layout;
384 void LyXView::UpdateDocumentClassChoice()
386 // Update the document class display in the document form
387 if (fd_form_document) {
388 fl_clear_choice(fd_form_document->choice_class);
389 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
390 cit != textclasslist.end(); ++cit) {
391 fl_addto_choice(fd_form_document->choice_class,
392 (*cit).description().c_str());
398 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
399 // as FL_KEYBOARD events :-( Matthias 280596
400 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
402 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
403 int retval = 0; // 0 means XForms should have a look at this event
405 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
406 static Time last_time_pressed = 0;
407 static Time last_time_released = 0;
408 static unsigned int last_key_pressed = 0;
409 static unsigned int last_key_released = 0;
410 static unsigned int last_state_pressed = 0;
411 static unsigned int last_state_released = 0;
413 // funny. Even though the raw_callback is registered with KeyPressMask,
414 // also KeyRelease-events are passed through:-(
415 // [It seems that XForms puts them in pairs... (JMarc)]
416 if (static_cast<XEvent*>(xev)->type == KeyPress
417 && view->bufferview->focus()
418 && view->bufferview->active())
420 last_time_pressed = xke->time;
421 last_key_pressed = xke->keycode;
422 last_state_pressed = xke->state;
423 retval = view->getLyXFunc()
424 ->processKeyEvent(static_cast<XEvent*>(xev));
426 else if (static_cast<XEvent*>(xev)->type == KeyRelease
427 && view->bufferview->focus()
428 && view->bufferview->active())
430 last_time_released = xke->time;
431 last_key_released = xke->keycode;
432 last_state_released = xke->state;
435 if (last_key_released == last_key_pressed
436 && last_state_released == last_state_pressed
437 && last_time_released == last_time_pressed) {
438 // When the diff between last_time_released and
439 // last_time_pressed is 0, that sinifies an autoreapeat
440 // at least on my system. It like some feedback from
441 // others, especially from user running LyX remote.
442 //lyxerr << "Syncing - purging X events." << endl;
443 XSync(fl_get_display(), 1);
444 // This purge make f.ex. scrolling stop imidiatly when
445 // releaseing the PageDown button. The question is if this
446 // purging of XEvents can cause any harm...after some testing
447 // I can see no problems, but I'd like other reports too.
453 // wrapper for the above
455 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
457 return LyXView::KeyPressMask_raw_callback(fl, xev);
461 // Updates the title of the window with the filename of the current document
462 void LyXView::updateWindowTitle()
464 static string last_title = "LyX";
465 string title = "LyX";
467 if (view()->available()) {
468 string cur_title = buffer()->fileName();
469 if (!cur_title.empty()){
470 title += ": " + OnlyFilename(cur_title);
471 if (!buffer()->isLyxClean())
472 title += _(" (Changed)");
473 if (buffer()->isReadonly())
474 title += _(" (read only)");
477 // Don't update title if it's the same as last time
478 if (title != last_title) {
479 fl_set_form_title(form_, title.c_str());
485 void LyXView::showState()
487 getMiniBuffer()->Set(CurrentState(view()));
488 getToolbar()->update();