2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
15 #pragma implementation
23 #include "lyxlookup.h"
25 #include "minibuffer.h"
29 #include "layout_forms.h"
32 #include "support/filetools.h" // OnlyFilename()
37 #include "frontends/Dialogs.h"
38 #include "lyx_gui_misc.h" // [update,Close]AllBufferRelatedDialogs
39 #include "bufferview_funcs.h" // CurrentState()
43 extern FD_form_document * fd_form_document;
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.
70 dialogs_ = new Dialogs(this);
71 // temporary until all dialogs moved into Dialogs.
72 dialogs_->updateBufferDependent
73 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
74 dialogs_->hideBufferDependent
75 .connect(slot(&CloseAllBufferRelatedDialogs));
91 /// Redraw the main form.
92 void LyXView::redraw() {
93 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
94 fl_redraw_form(form_);
95 minibuffer->Activate();
99 /// returns the buffer currently shown in the main form.
100 Buffer * LyXView::buffer() const
102 return bufferview->buffer();
106 BufferView * LyXView::view() const
112 FL_FORM * LyXView::getForm() const
118 Toolbar * LyXView::getToolbar() const
124 LyXFunc * LyXView::getLyXFunc() const
130 MiniBuffer * LyXView::getMiniBuffer() const
136 Menus * LyXView::getMenus() const
142 Intl * LyXView::getIntl() const
148 // Callback for autosave timer
149 void LyXView::AutoSave()
151 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
152 if (view()->available())
157 /// Reset autosave timer
158 void LyXView::resetAutosaveTimer()
161 autosave_timeout.restart();
165 // Callback for close main form from window manager
166 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
173 // Wrapper for the above
174 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
176 return LyXView::atCloseMainFormCB(form, p);
180 void LyXView::setPosition(int x, int y)
182 fl_set_form_position(form_, x, y);
186 void LyXView::show(int place, int border, char const * title)
188 fl_show_form(form_, place, border, title);
190 InitLyXLookup(fl_display, form_->window);
194 void LyXView::create_form_form_main(int width, int height)
195 /* to make this work as it should, .lyxrc should have been
196 * read first; OR maybe this one should be made dynamic.
198 * We will probably not have lyxrc before the main form is
199 * initialized, because error messages from lyxrc parsing
200 * are presented (and rightly so) in GUI popups. Asger.
204 form_ = fl_bgn_form(FL_NO_BOX, width, height);
205 form_->u_vdata = this;
206 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
207 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
209 // Parameters for the appearance of the main form
211 int const bw = abs(fl_get_border_width());
217 menus = new Menus(this, air);
223 toolbar = new Toolbar(this, air, 30 + air + bw);
232 int const ywork = 60 + 2 * air + bw;
233 int const workheight = height - ywork - (25 + 2 * air);
235 ::current_view = bufferview = new BufferView(this, air, ywork,
243 minibuffer = new MiniBuffer(this, air, height - (25 + air),
244 width - (2 * air), 25);
250 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
256 // assign an icon to main form
258 Pixmap lyx_p, lyx_mask;
259 lyx_p = fl_create_from_pixmapdata(fl_root,
260 const_cast<char**>(lyx_xpm),
267 fl_set_form_icon(form_, lyx_p, lyx_mask);
270 fl_set_form_minsize(form_, 50, 50);
277 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
281 // Set the textclass choice
282 invalidateLayoutChoice();
283 updateLayoutChoice();
284 UpdateDocumentClassChoice();
286 // Start autosave timer
287 if (lyxrc.autosave) {
288 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
289 autosave_timeout.start();
292 // Install the raw callback for keyboard events
293 fl_register_raw_callback(form_,
295 C_LyXView_KeyPressMask_raw_callback);
296 intl->InitKeyMapper(lyxrc.use_kbmap);
300 void LyXView::invalidateLayoutChoice()
306 void LyXView::updateLayoutChoice()
308 // Update the layout display
309 if (!toolbar->combox) return;
311 // This has a side-effect that the layouts are not showed when no
312 // document is loaded.
313 if (bufferview == 0 || bufferview->buffer() == 0) {
314 toolbar->combox->clear();
315 toolbar->combox->Redraw();
319 // If textclass is different, we need to update the list
320 if (toolbar->combox->empty() ||
321 (last_textclass != int(buffer()->params.textclass))) {
322 toolbar->combox->clear();
323 LyXTextClass const & tc =
324 textclasslist.TextClass(buffer()->params.textclass);
325 for (LyXTextClass::const_iterator cit = tc.begin();
326 cit != tc.end(); ++cit) {
327 if ((*cit).obsoleted_by().empty())
328 toolbar->combox->addline((*cit).name().c_str());
330 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
332 last_textclass = int(buffer()->params.textclass);
335 // we need to do this.
336 toolbar->combox->Redraw();
338 LyXTextClass::size_type layout =
339 bufferview->text->cursor.par()->GetLayout();
341 if (layout != current_layout){
342 toolbar->combox->select(layout + 1);
343 current_layout = layout;
348 void LyXView::UpdateDocumentClassChoice()
350 // Update the document class display in the document form
351 if (fd_form_document) {
352 fl_clear_choice(fd_form_document->choice_class);
353 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
354 cit != textclasslist.end(); ++cit) {
355 fl_addto_choice(fd_form_document->choice_class,
356 (*cit).description().c_str());
362 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
363 // as FL_KEYBOARD events :-( Matthias 280596
364 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
366 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
367 int retval = 0; // 0 means XForms should have a look at this event
369 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
370 static Time last_time_pressed = 0;
371 static Time last_time_released = 0;
372 static unsigned int last_key_pressed = 0;
373 static unsigned int last_key_released = 0;
374 static unsigned int last_state_pressed = 0;
375 static unsigned int last_state_released = 0;
377 // funny. Even though the raw_callback is registered with KeyPressMask,
378 // also KeyRelease-events are passed through:-(
379 // [It seems that XForms puts them in pairs... (JMarc)]
380 if (static_cast<XEvent*>(xev)->type == KeyPress
381 && view->bufferview->focus()
382 && view->bufferview->active())
384 last_time_pressed = xke->time;
385 last_key_pressed = xke->keycode;
386 last_state_pressed = xke->state;
387 retval = view->getLyXFunc()
388 ->processKeyEvent(static_cast<XEvent*>(xev));
390 else if (static_cast<XEvent*>(xev)->type == KeyRelease
391 && view->bufferview->focus()
392 && view->bufferview->active())
394 last_time_released = xke->time;
395 last_key_released = xke->keycode;
396 last_state_released = xke->state;
399 if (last_key_released == last_key_pressed
400 && last_state_released == last_state_pressed
401 && last_time_released == last_time_pressed) {
402 // When the diff between last_time_released and
403 // last_time_pressed is 0, that sinifies an autoreapeat
404 // at least on my system. It like some feedback from
405 // others, especially from user running LyX remote.
406 //lyxerr << "Syncing - purging X events." << endl;
407 XSync(fl_get_display(), 1);
408 // This purge make f.ex. scrolling stop imidiatly when
409 // releaseing the PageDown button. The question is if this
410 // purging of XEvents can cause any harm...after some testing
411 // I can see no problems, but I'd like other reports too.
417 // wrapper for the above
419 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
421 return LyXView::KeyPressMask_raw_callback(fl, xev);
425 // Updates the title of the window with the filename of the current document
426 void LyXView::updateWindowTitle()
428 static string last_title = "LyX";
429 string title = "LyX";
431 if (view()->available()) {
432 string cur_title = buffer()->fileName();
433 if (!cur_title.empty()){
434 title += ": " + OnlyFilename(cur_title);
435 if (!buffer()->isLyxClean())
436 title += _(" (Changed)");
437 if (buffer()->isReadonly())
438 title += _(" (read only)");
441 // Don't update title if it's the same as last time
442 if (title != last_title) {
443 fl_set_form_title(form_, title.c_str());
449 void LyXView::showState()
451 getMiniBuffer()->Set(CurrentState(view()));
452 getToolbar()->update();