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
23 #include "lyxlookup.h"
25 #include "minibuffer.h"
28 #include "layout_forms.h"
31 #include "support/filetools.h" // OnlyFilename()
35 #include "frontends/Dialogs.h"
36 #include "frontends/Toolbar.h"
37 #include "frontends/Menubar.h"
38 #include "MenuBackend.h"
39 #include "ToolbarDefaults.h"
40 #include "lyx_gui_misc.h" // [update,Close]AllBufferRelatedDialogs
41 #include "bufferview_funcs.h" // CurrentState()
45 extern void AutoSave(BufferView *);
46 extern void QuitLyX();
47 LyXTextClass::size_type current_layout = 0;
49 // This is very temporary
50 BufferView * current_view;
52 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
54 #ifdef SIGC_CXX_NAMESPACES
55 using SigC::Connection;
59 LyXView::LyXView(int width, int height)
61 create_form_form_main(width, height);
62 fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
63 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
64 lyxfunc = new LyXFunc(this);
67 // Make sure the buttons are disabled if needed.
71 dialogs_ = new Dialogs(this);
72 // temporary until all dialogs moved into Dialogs.
73 dialogs_->updateBufferDependent
74 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
75 dialogs_->hideBufferDependent
76 .connect(slot(&CloseAllBufferRelatedDialogs));
92 /// Redraw the main form.
93 void LyXView::redraw() {
94 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
95 fl_redraw_form(form_);
96 minibuffer->Activate();
100 /// returns the buffer currently shown in the main form.
101 Buffer * LyXView::buffer() const
103 return bufferview->buffer();
107 BufferView * LyXView::view() const
113 FL_FORM * LyXView::getForm() const
119 Toolbar * LyXView::getToolbar() const
125 void LyXView::setLayout(LyXTextClass::size_type layout)
127 toolbar->setLayout(layout);
131 void LyXView::updateToolbar()
138 LyXFunc * LyXView::getLyXFunc() const
144 MiniBuffer * LyXView::getMiniBuffer() const
150 Menubar * LyXView::getMenubar() const
156 void LyXView::updateMenubar()
158 if ((!view() || !view()->buffer())
159 && menubackend.hasMenu("main_nobuffer"))
160 menubar->set("main_nobuffer");
162 menubar->set("main");
166 Intl * LyXView::getIntl() const
172 // Callback for autosave timer
173 void LyXView::AutoSave()
175 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
176 if (view()->available())
181 /// Reset autosave timer
182 void LyXView::resetAutosaveTimer()
185 autosave_timeout.restart();
189 // Callback for close main form from window manager
190 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
197 // Wrapper for the above
199 int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
201 return LyXView::atCloseMainFormCB(form, p);
205 void LyXView::setPosition(int x, int y)
207 fl_set_form_position(form_, x, y);
211 void LyXView::show(int place, int border, string const & title)
213 fl_show_form(form_, place, border, title.c_str());
216 InitLyXLookup(fl_get_display(), form_->window);
221 void LyXView::create_form_form_main(int width, int height)
222 /* to make this work as it should, .lyxrc should have been
223 * read first; OR maybe this one should be made dynamic.
225 * We will probably not have lyxrc before the main form is
226 * initialized, because error messages from lyxrc parsing
227 * are presented (and rightly so) in GUI popups. Asger.
231 form_ = fl_bgn_form(FL_NO_BOX, width, height);
232 form_->u_vdata = this;
233 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
234 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
236 // Parameters for the appearance of the main form
238 int const bw = abs(fl_get_border_width());
243 menubar = new Menubar(this, menubackend);
249 toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
258 int const ywork = 60 + 2 * air + bw;
259 int const workheight = height - ywork - (25 + 2 * air);
261 ::current_view = bufferview = new BufferView(this, air, ywork,
269 minibuffer = new MiniBuffer(this, air, height - (25 + air),
270 width - (2 * air), 25);
276 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
282 // assign an icon to main form
283 string iconname = LibFileSearch("images", "lyx", "xpm");
284 if (!iconname.empty()) {
286 Pixmap lyx_p, lyx_mask;
287 lyx_p = fl_read_pixmapfile(fl_root,
295 fl_set_form_icon(form_, lyx_p, lyx_mask);
299 fl_set_form_minsize(form_, 50, 50);
307 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
313 // Set the textclass choice
314 invalidateLayoutChoice();
315 updateLayoutChoice();
318 // Start autosave timer
319 if (lyxrc.autosave) {
320 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
321 autosave_timeout.start();
325 // Install the raw callback for keyboard events
326 fl_register_raw_callback(form_,
328 C_LyXView_KeyPressMask_raw_callback);
330 intl->InitKeyMapper(lyxrc.use_kbmap);
334 void LyXView::invalidateLayoutChoice()
340 void LyXView::updateLayoutChoice()
342 // This has a side-effect that the layouts are not showed when no
343 // document is loaded.
344 if (!view() || !view()->buffer()) {
345 toolbar->clearLayoutList();
349 // Update the layout display
350 if (last_textclass != int(buffer()->params.textclass)) {
351 toolbar->updateLayoutList(true);
352 last_textclass = int(buffer()->params.textclass);
355 toolbar->updateLayoutList(false);
359 LyXTextClass::size_type layout =
360 bufferview->text->cursor.par()->GetLayout();
362 if (layout != current_layout){
363 toolbar->setLayout(layout);
364 current_layout = layout;
370 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
371 // as FL_KEYBOARD events :-( Matthias 280596
372 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
374 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
375 int retval = 0; // 0 means XForms should have a look at this event
377 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
378 static Time last_time_pressed = 0;
379 static Time last_time_released = 0;
380 static unsigned int last_key_pressed = 0;
381 static unsigned int last_key_released = 0;
382 static unsigned int last_state_pressed = 0;
383 static unsigned int last_state_released = 0;
385 // funny. Even though the raw_callback is registered with KeyPressMask,
386 // also KeyRelease-events are passed through:-(
387 // [It seems that XForms puts them in pairs... (JMarc)]
388 if (static_cast<XEvent*>(xev)->type == KeyPress
389 && view->bufferview->focus()
390 && view->bufferview->active())
392 last_time_pressed = xke->time;
393 last_key_pressed = xke->keycode;
394 last_state_pressed = xke->state;
395 retval = view->getLyXFunc()
396 ->processKeyEvent(static_cast<XEvent*>(xev));
398 else if (static_cast<XEvent*>(xev)->type == KeyRelease
399 && view->bufferview->focus()
400 && view->bufferview->active())
402 last_time_released = xke->time;
403 last_key_released = xke->keycode;
404 last_state_released = xke->state;
407 if (last_key_released == last_key_pressed
408 && last_state_released == last_state_pressed
409 && last_time_released == last_time_pressed) {
410 // When the diff between last_time_released and
411 // last_time_pressed is 0, that sinifies an autoreapeat
412 // at least on my system. It like some feedback from
413 // others, especially from user running LyX remote.
414 lyxerr[Debug::KEY] << "Syncing - purging X events." << endl;
415 XSync(fl_get_display(), 1);
416 // This purge make f.ex. scrolling stop imidiatly when
417 // releaseing the PageDown button. The question is if this
418 // purging of XEvents can cause any harm...after some testing
419 // I can see no problems, but I'd like other reports too.
425 // wrapper for the above
427 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
429 return LyXView::KeyPressMask_raw_callback(fl, xev);
434 // Updates the title of the window with the filename of the current document
435 void LyXView::updateWindowTitle()
437 static string last_title = "LyX";
438 string title = "LyX";
440 if (view()->available()) {
441 string cur_title = buffer()->fileName();
442 if (!cur_title.empty()){
443 title += ": " + MakeDisplayPath(cur_title, 30);
444 if (!buffer()->isLyxClean())
445 title += _(" (Changed)");
446 if (buffer()->isReadonly())
447 title += _(" (read only)");
450 // Don't update title if it's the same as last time
451 if (title != last_title) {
452 fl_set_form_title(form_, title.c_str());
458 void LyXView::showState()
460 getMiniBuffer()->Set(CurrentState(view()));
461 getToolbar()->update();