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.
74 dialogs_ = new Dialogs(this);
75 // temporary until all dialogs moved into Dialogs.
76 dialogs_->updateBufferDependent
77 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
78 dialogs_->hideBufferDependent
79 .connect(slot(&CloseAllBufferRelatedDialogs));
99 /// Redraw the main form.
100 void LyXView::redraw() {
101 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
102 fl_redraw_form(form_);
103 minibuffer->Activate();
107 /// returns the buffer currently shown in the main form.
108 Buffer * LyXView::buffer() const
110 return bufferview->buffer();
114 BufferView * LyXView::view() const
120 FL_FORM * LyXView::getForm() const
126 Toolbar * LyXView::getToolbar() const
132 void LyXView::setLayout(int layout)
134 toolbar->setLayout(layout);
138 void LyXView::updateToolbar()
144 LyXFunc * LyXView::getLyXFunc() const
150 MiniBuffer * LyXView::getMiniBuffer() const
157 Menubar * LyXView::getMenubar() const
163 void LyXView::updateMenubar()
165 if ((!view() || !view()->buffer())
166 && menubackend.hasMenu("main_nobuffer"))
167 menubar->set("main_nobuffer");
169 menubar->set("main");
173 Menus * LyXView::getMenus() const
180 Intl * LyXView::getIntl() const
186 // Callback for autosave timer
187 void LyXView::AutoSave()
189 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
190 if (view()->available())
195 /// Reset autosave timer
196 void LyXView::resetAutosaveTimer()
199 autosave_timeout.restart();
203 // Callback for close main form from window manager
204 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
211 // Wrapper for the above
212 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
214 return LyXView::atCloseMainFormCB(form, p);
218 void LyXView::setPosition(int x, int y)
220 fl_set_form_position(form_, x, y);
224 void LyXView::show(int place, int border, char const * title)
226 fl_show_form(form_, place, border, title);
228 InitLyXLookup(fl_display, form_->window);
232 void LyXView::create_form_form_main(int width, int height)
233 /* to make this work as it should, .lyxrc should have been
234 * read first; OR maybe this one should be made dynamic.
236 * We will probably not have lyxrc before the main form is
237 * initialized, because error messages from lyxrc parsing
238 * are presented (and rightly so) in GUI popups. Asger.
242 form_ = fl_bgn_form(FL_NO_BOX, width, height);
243 form_->u_vdata = this;
244 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
245 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
247 // Parameters for the appearance of the main form
249 int const bw = abs(fl_get_border_width());
255 menubar = new Menubar(this, menubackend);
257 menus = new Menus(this, air);
264 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
273 int const ywork = 60 + 2 * air + bw;
274 int const workheight = height - ywork - (25 + 2 * air);
276 ::current_view = bufferview = new BufferView(this, air, ywork,
284 minibuffer = new MiniBuffer(this, air, height - (25 + air),
285 width - (2 * air), 25);
291 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
297 // assign an icon to main form
298 string iconname = LibFileSearch("images", "lyx", "xpm");
299 if (!iconname.empty()) {
301 Pixmap lyx_p, lyx_mask;
302 lyx_p = fl_read_pixmapfile(fl_root,
310 fl_set_form_icon(form_, lyx_p, lyx_mask);
314 fl_set_form_minsize(form_, 50, 50);
321 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
325 // Set the textclass choice
326 invalidateLayoutChoice();
327 updateLayoutChoice();
328 UpdateDocumentClassChoice();
333 // Start autosave timer
334 if (lyxrc.autosave) {
335 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
336 autosave_timeout.start();
339 // Install the raw callback for keyboard events
340 fl_register_raw_callback(form_,
342 C_LyXView_KeyPressMask_raw_callback);
343 intl->InitKeyMapper(lyxrc.use_kbmap);
347 void LyXView::invalidateLayoutChoice()
353 void LyXView::updateLayoutChoice()
355 // This has a side-effect that the layouts are not showed when no
356 // document is loaded.
357 if (!view() || !view()->buffer()) {
358 toolbar->clearLayoutList();
362 // Update the layout display
363 if (last_textclass != int(buffer()->params.textclass)) {
364 toolbar->updateLayoutList(true);
365 last_textclass = int(buffer()->params.textclass);
368 toolbar->updateLayoutList(false);
372 LyXTextClass::size_type layout =
373 bufferview->text->cursor.par()->GetLayout();
375 if (layout != current_layout){
376 toolbar->setLayout(layout);
377 current_layout = layout;
382 void LyXView::UpdateDocumentClassChoice()
384 // Update the document class display in the document form
385 if (fd_form_document) {
386 fl_clear_choice(fd_form_document->choice_class);
387 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
388 cit != textclasslist.end(); ++cit) {
389 fl_addto_choice(fd_form_document->choice_class,
390 (*cit).description().c_str());
396 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
397 // as FL_KEYBOARD events :-( Matthias 280596
398 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
400 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
401 int retval = 0; // 0 means XForms should have a look at this event
403 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
404 static Time last_time_pressed = 0;
405 static Time last_time_released = 0;
406 static unsigned int last_key_pressed = 0;
407 static unsigned int last_key_released = 0;
408 static unsigned int last_state_pressed = 0;
409 static unsigned int last_state_released = 0;
411 // funny. Even though the raw_callback is registered with KeyPressMask,
412 // also KeyRelease-events are passed through:-(
413 // [It seems that XForms puts them in pairs... (JMarc)]
414 if (static_cast<XEvent*>(xev)->type == KeyPress
415 && view->bufferview->focus()
416 && view->bufferview->active())
418 last_time_pressed = xke->time;
419 last_key_pressed = xke->keycode;
420 last_state_pressed = xke->state;
421 retval = view->getLyXFunc()
422 ->processKeyEvent(static_cast<XEvent*>(xev));
424 else if (static_cast<XEvent*>(xev)->type == KeyRelease
425 && view->bufferview->focus()
426 && view->bufferview->active())
428 last_time_released = xke->time;
429 last_key_released = xke->keycode;
430 last_state_released = xke->state;
433 if (last_key_released == last_key_pressed
434 && last_state_released == last_state_pressed
435 && last_time_released == last_time_pressed) {
436 // When the diff between last_time_released and
437 // last_time_pressed is 0, that sinifies an autoreapeat
438 // at least on my system. It like some feedback from
439 // others, especially from user running LyX remote.
440 //lyxerr << "Syncing - purging X events." << endl;
441 XSync(fl_get_display(), 1);
442 // This purge make f.ex. scrolling stop imidiatly when
443 // releaseing the PageDown button. The question is if this
444 // purging of XEvents can cause any harm...after some testing
445 // I can see no problems, but I'd like other reports too.
451 // wrapper for the above
453 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
455 return LyXView::KeyPressMask_raw_callback(fl, xev);
459 // Updates the title of the window with the filename of the current document
460 void LyXView::updateWindowTitle()
462 static string last_title = "LyX";
463 string title = "LyX";
465 if (view()->available()) {
466 string cur_title = buffer()->fileName();
467 if (!cur_title.empty()){
468 title += ": " + OnlyFilename(cur_title);
469 if (!buffer()->isLyxClean())
470 title += _(" (Changed)");
471 if (buffer()->isReadonly())
472 title += _(" (read only)");
475 // Don't update title if it's the same as last time
476 if (title != last_title) {
477 fl_set_form_title(form_, title.c_str());
483 void LyXView::showState()
485 getMiniBuffer()->Set(CurrentState(view()));
486 getToolbar()->update();