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
131 void LyXView::setLayout(int layout)
133 toolbar->setLayout(layout);
136 LyXFunc * LyXView::getLyXFunc() const
142 MiniBuffer * LyXView::getMiniBuffer() const
149 Menubar * LyXView::getMenubar() const
155 void LyXView::updateMenubar()
157 if ((!view() || !view()->buffer())
158 && menubackend.hasMenu("main_nobuffer"))
159 menubar->set("main_nobuffer");
161 menubar->set("main");
165 Menus * LyXView::getMenus() const
172 Intl * LyXView::getIntl() const
178 // Callback for autosave timer
179 void LyXView::AutoSave()
181 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
182 if (view()->available())
187 /// Reset autosave timer
188 void LyXView::resetAutosaveTimer()
191 autosave_timeout.restart();
195 // Callback for close main form from window manager
196 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
203 // Wrapper for the above
204 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
206 return LyXView::atCloseMainFormCB(form, p);
210 void LyXView::setPosition(int x, int y)
212 fl_set_form_position(form_, x, y);
216 void LyXView::show(int place, int border, char const * title)
218 fl_show_form(form_, place, border, title);
220 InitLyXLookup(fl_display, form_->window);
224 void LyXView::create_form_form_main(int width, int height)
225 /* to make this work as it should, .lyxrc should have been
226 * read first; OR maybe this one should be made dynamic.
228 * We will probably not have lyxrc before the main form is
229 * initialized, because error messages from lyxrc parsing
230 * are presented (and rightly so) in GUI popups. Asger.
234 form_ = fl_bgn_form(FL_NO_BOX, width, height);
235 form_->u_vdata = this;
236 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
237 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
239 // Parameters for the appearance of the main form
241 int const bw = abs(fl_get_border_width());
247 menubar = new Menubar(this, menubackend);
249 menus = new Menus(this, air);
256 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
265 int const ywork = 60 + 2 * air + bw;
266 int const workheight = height - ywork - (25 + 2 * air);
268 ::current_view = bufferview = new BufferView(this, air, ywork,
276 minibuffer = new MiniBuffer(this, air, height - (25 + air),
277 width - (2 * air), 25);
283 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
289 // assign an icon to main form
290 string iconname = LibFileSearch("images", "lyx", "xpm");
291 if (!iconname.empty()) {
293 Pixmap lyx_p, lyx_mask;
294 lyx_p = fl_read_pixmapfile(fl_root,
302 fl_set_form_icon(form_, lyx_p, lyx_mask);
306 fl_set_form_minsize(form_, 50, 50);
313 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
317 // Set the textclass choice
318 invalidateLayoutChoice();
319 updateLayoutChoice();
320 UpdateDocumentClassChoice();
325 // Start autosave timer
326 if (lyxrc.autosave) {
327 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
328 autosave_timeout.start();
331 // Install the raw callback for keyboard events
332 fl_register_raw_callback(form_,
334 C_LyXView_KeyPressMask_raw_callback);
335 intl->InitKeyMapper(lyxrc.use_kbmap);
339 void LyXView::invalidateLayoutChoice()
345 void LyXView::updateLayoutChoice()
347 // This has a side-effect that the layouts are not showed when no
348 // document is loaded.
349 if (!view() || !view()->buffer()) {
350 toolbar->clearLayoutList();
354 // Update the layout display
355 if (last_textclass != int(buffer()->params.textclass)) {
356 toolbar->updateLayoutList(true);
357 last_textclass = int(buffer()->params.textclass);
360 toolbar->updateLayoutList(false);
364 LyXTextClass::size_type layout =
365 bufferview->text->cursor.par()->GetLayout();
367 if (layout != current_layout){
368 toolbar->setLayout(layout);
369 current_layout = layout;
374 void LyXView::UpdateDocumentClassChoice()
376 // Update the document class display in the document form
377 if (fd_form_document) {
378 fl_clear_choice(fd_form_document->choice_class);
379 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
380 cit != textclasslist.end(); ++cit) {
381 fl_addto_choice(fd_form_document->choice_class,
382 (*cit).description().c_str());
388 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
389 // as FL_KEYBOARD events :-( Matthias 280596
390 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
392 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
393 int retval = 0; // 0 means XForms should have a look at this event
395 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
396 static Time last_time_pressed = 0;
397 static Time last_time_released = 0;
398 static unsigned int last_key_pressed = 0;
399 static unsigned int last_key_released = 0;
400 static unsigned int last_state_pressed = 0;
401 static unsigned int last_state_released = 0;
403 // funny. Even though the raw_callback is registered with KeyPressMask,
404 // also KeyRelease-events are passed through:-(
405 // [It seems that XForms puts them in pairs... (JMarc)]
406 if (static_cast<XEvent*>(xev)->type == KeyPress
407 && view->bufferview->focus()
408 && view->bufferview->active())
410 last_time_pressed = xke->time;
411 last_key_pressed = xke->keycode;
412 last_state_pressed = xke->state;
413 retval = view->getLyXFunc()
414 ->processKeyEvent(static_cast<XEvent*>(xev));
416 else if (static_cast<XEvent*>(xev)->type == KeyRelease
417 && view->bufferview->focus()
418 && view->bufferview->active())
420 last_time_released = xke->time;
421 last_key_released = xke->keycode;
422 last_state_released = xke->state;
425 if (last_key_released == last_key_pressed
426 && last_state_released == last_state_pressed
427 && last_time_released == last_time_pressed) {
428 // When the diff between last_time_released and
429 // last_time_pressed is 0, that sinifies an autoreapeat
430 // at least on my system. It like some feedback from
431 // others, especially from user running LyX remote.
432 //lyxerr << "Syncing - purging X events." << endl;
433 XSync(fl_get_display(), 1);
434 // This purge make f.ex. scrolling stop imidiatly when
435 // releaseing the PageDown button. The question is if this
436 // purging of XEvents can cause any harm...after some testing
437 // I can see no problems, but I'd like other reports too.
443 // wrapper for the above
445 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
447 return LyXView::KeyPressMask_raw_callback(fl, xev);
451 // Updates the title of the window with the filename of the current document
452 void LyXView::updateWindowTitle()
454 static string last_title = "LyX";
455 string title = "LyX";
457 if (view()->available()) {
458 string cur_title = buffer()->fileName();
459 if (!cur_title.empty()){
460 title += ": " + OnlyFilename(cur_title);
461 if (!buffer()->isLyxClean())
462 title += _(" (Changed)");
463 if (buffer()->isReadonly())
464 title += _(" (read only)");
467 // Don't update title if it's the same as last time
468 if (title != last_title) {
469 fl_set_form_title(form_, title.c_str());
475 void LyXView::showState()
477 getMiniBuffer()->Set(CurrentState(view()));
478 getToolbar()->update();