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 void AutoSave(BufferView *);
44 extern void QuitLyX();
45 LyXTextClass::size_type current_layout = 0;
47 // This is very temporary
48 BufferView * current_view;
50 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
52 #ifdef SIGC_CXX_NAMESPACES
53 using SigC::Connection;
57 LyXView::LyXView(int width, int height)
59 create_form_form_main(width, height);
60 fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
61 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
62 lyxfunc = new LyXFunc(this);
65 // Make sure the buttons are disabled if needed.
69 dialogs_ = new Dialogs(this);
70 // temporary until all dialogs moved into Dialogs.
71 dialogs_->updateBufferDependent
72 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
73 dialogs_->hideBufferDependent
74 .connect(slot(&CloseAllBufferRelatedDialogs));
90 /// Redraw the main form.
91 void LyXView::redraw() {
92 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
93 fl_redraw_form(form_);
94 minibuffer->Activate();
98 /// returns the buffer currently shown in the main form.
99 Buffer * LyXView::buffer() const
101 return bufferview->buffer();
105 BufferView * LyXView::view() const
111 FL_FORM * LyXView::getForm() const
117 Toolbar * LyXView::getToolbar() const
123 void LyXView::setLayout(int layout)
125 toolbar->setLayout(layout);
129 void LyXView::updateToolbar()
136 LyXFunc * LyXView::getLyXFunc() const
142 MiniBuffer * LyXView::getMiniBuffer() const
148 Menubar * LyXView::getMenubar() const
154 void LyXView::updateMenubar()
156 if ((!view() || !view()->buffer())
157 && menubackend.hasMenu("main_nobuffer"))
158 menubar->set("main_nobuffer");
160 menubar->set("main");
164 Intl * LyXView::getIntl() const
170 // Callback for autosave timer
171 void LyXView::AutoSave()
173 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
174 if (view()->available())
179 /// Reset autosave timer
180 void LyXView::resetAutosaveTimer()
183 autosave_timeout.restart();
187 // Callback for close main form from window manager
188 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
195 // Wrapper for the above
197 int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
199 return LyXView::atCloseMainFormCB(form, p);
203 void LyXView::setPosition(int x, int y)
205 fl_set_form_position(form_, x, y);
209 void LyXView::show(int place, int border, string const & title)
211 fl_show_form(form_, place, border, title.c_str());
213 InitLyXLookup(fl_display, form_->window);
217 void LyXView::create_form_form_main(int width, int height)
218 /* to make this work as it should, .lyxrc should have been
219 * read first; OR maybe this one should be made dynamic.
221 * We will probably not have lyxrc before the main form is
222 * initialized, because error messages from lyxrc parsing
223 * are presented (and rightly so) in GUI popups. Asger.
227 form_ = fl_bgn_form(FL_NO_BOX, width, height);
228 form_->u_vdata = this;
229 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
230 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
232 // Parameters for the appearance of the main form
234 int const bw = abs(fl_get_border_width());
239 menubar = new Menubar(this, menubackend);
245 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
254 int const ywork = 60 + 2 * air + bw;
255 int const workheight = height - ywork - (25 + 2 * air);
257 ::current_view = bufferview = new BufferView(this, air, ywork,
265 minibuffer = new MiniBuffer(this, air, height - (25 + air),
266 width - (2 * air), 25);
272 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
278 // assign an icon to main form
279 string iconname = LibFileSearch("images", "lyx", "xpm");
280 if (!iconname.empty()) {
282 Pixmap lyx_p, lyx_mask;
283 lyx_p = fl_read_pixmapfile(fl_root,
291 fl_set_form_icon(form_, lyx_p, lyx_mask);
295 fl_set_form_minsize(form_, 50, 50);
303 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
309 // Set the textclass choice
310 invalidateLayoutChoice();
311 updateLayoutChoice();
314 // Start autosave timer
315 if (lyxrc.autosave) {
316 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
317 autosave_timeout.start();
321 // Install the raw callback for keyboard events
322 fl_register_raw_callback(form_,
324 C_LyXView_KeyPressMask_raw_callback);
326 intl->InitKeyMapper(lyxrc.use_kbmap);
330 void LyXView::invalidateLayoutChoice()
336 void LyXView::updateLayoutChoice()
338 // This has a side-effect that the layouts are not showed when no
339 // document is loaded.
340 if (!view() || !view()->buffer()) {
341 toolbar->clearLayoutList();
345 // Update the layout display
346 if (last_textclass != int(buffer()->params.textclass)) {
347 toolbar->updateLayoutList(true);
348 last_textclass = int(buffer()->params.textclass);
351 toolbar->updateLayoutList(false);
355 LyXTextClass::size_type layout =
356 bufferview->text->cursor.par()->GetLayout();
358 if (layout != current_layout){
359 toolbar->setLayout(layout);
360 current_layout = layout;
366 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
367 // as FL_KEYBOARD events :-( Matthias 280596
368 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
370 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
371 int retval = 0; // 0 means XForms should have a look at this event
373 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
374 static Time last_time_pressed = 0;
375 static Time last_time_released = 0;
376 static unsigned int last_key_pressed = 0;
377 static unsigned int last_key_released = 0;
378 static unsigned int last_state_pressed = 0;
379 static unsigned int last_state_released = 0;
381 // funny. Even though the raw_callback is registered with KeyPressMask,
382 // also KeyRelease-events are passed through:-(
383 // [It seems that XForms puts them in pairs... (JMarc)]
384 if (static_cast<XEvent*>(xev)->type == KeyPress
385 && view->bufferview->focus()
386 && view->bufferview->active())
388 last_time_pressed = xke->time;
389 last_key_pressed = xke->keycode;
390 last_state_pressed = xke->state;
391 retval = view->getLyXFunc()
392 ->processKeyEvent(static_cast<XEvent*>(xev));
394 else if (static_cast<XEvent*>(xev)->type == KeyRelease
395 && view->bufferview->focus()
396 && view->bufferview->active())
398 last_time_released = xke->time;
399 last_key_released = xke->keycode;
400 last_state_released = xke->state;
403 if (last_key_released == last_key_pressed
404 && last_state_released == last_state_pressed
405 && last_time_released == last_time_pressed) {
406 // When the diff between last_time_released and
407 // last_time_pressed is 0, that sinifies an autoreapeat
408 // at least on my system. It like some feedback from
409 // others, especially from user running LyX remote.
410 lyxerr[Debug::KEY] << "Syncing - purging X events." << endl;
411 XSync(fl_get_display(), 1);
412 // This purge make f.ex. scrolling stop imidiatly when
413 // releaseing the PageDown button. The question is if this
414 // purging of XEvents can cause any harm...after some testing
415 // I can see no problems, but I'd like other reports too.
421 // wrapper for the above
423 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
425 return LyXView::KeyPressMask_raw_callback(fl, xev);
430 // Updates the title of the window with the filename of the current document
431 void LyXView::updateWindowTitle()
433 static string last_title = "LyX";
434 string title = "LyX";
436 if (view()->available()) {
437 string cur_title = buffer()->fileName();
438 if (!cur_title.empty()){
439 title += ": " + MakeDisplayPath(cur_title, 30);
440 if (!buffer()->isLyxClean())
441 title += _(" (Changed)");
442 if (buffer()->isReadonly())
443 title += _(" (read only)");
446 // Don't update title if it's the same as last time
447 if (title != last_title) {
448 fl_set_form_title(form_, title.c_str());
454 void LyXView::showState()
456 getMiniBuffer()->Set(CurrentState(view()));
457 getToolbar()->update();