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"
28 #include "layout_forms.h"
31 #include "support/filetools.h" // OnlyFilename()
36 #include "frontends/Dialogs.h"
37 #include "lyx_gui_misc.h" // [update,Close]AllBufferRelatedDialogs
38 #include "bufferview_funcs.h" // CurrentState()
42 extern FD_form_document * fd_form_document;
44 extern void AutoSave(BufferView *);
45 extern void QuitLyX();
46 LyXTextClass::size_type current_layout = 0;
48 // This is very temporary
49 BufferView * current_view;
51 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
53 #ifdef SIGC_CXX_NAMESPACES
54 using SigC::Connection;
58 LyXView::LyXView(int width, int height)
60 create_form_form_main(width, height);
61 fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
62 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
63 lyxfunc = new LyXFunc(this);
66 // 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 LyXFunc * LyXView::getLyXFunc() const
129 MiniBuffer * LyXView::getMiniBuffer() const
135 Menus * LyXView::getMenus() const
141 Intl * LyXView::getIntl() const
147 // Callback for autosave timer
148 void LyXView::AutoSave()
150 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
151 if (view()->available())
156 /// Reset autosave timer
157 void LyXView::resetAutosaveTimer()
160 autosave_timeout.restart();
164 // Callback for close main form from window manager
165 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
172 // Wrapper for the above
173 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
175 return LyXView::atCloseMainFormCB(form, p);
179 void LyXView::setPosition(int x, int y)
181 fl_set_form_position(form_, x, y);
185 void LyXView::show(int place, int border, char const * title)
187 fl_show_form(form_, place, border, title);
189 InitLyXLookup(fl_display, form_->window);
193 void LyXView::create_form_form_main(int width, int height)
194 /* to make this work as it should, .lyxrc should have been
195 * read first; OR maybe this one should be made dynamic.
197 * We will probably not have lyxrc before the main form is
198 * initialized, because error messages from lyxrc parsing
199 * are presented (and rightly so) in GUI popups. Asger.
203 form_ = fl_bgn_form(FL_NO_BOX, width, height);
204 form_->u_vdata = this;
205 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
206 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
208 // Parameters for the appearance of the main form
210 int const bw = abs(fl_get_border_width());
216 menus = new Menus(this, air);
222 toolbar = new Toolbar(this, air, 30 + air + bw);
231 int const ywork = 60 + 2 * air + bw;
232 int const workheight = height - ywork - (25 + 2 * air);
234 ::current_view = bufferview = new BufferView(this, air, ywork,
242 minibuffer = new MiniBuffer(this, air, height - (25 + air),
243 width - (2 * air), 25);
249 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
255 // assign an icon to main form
256 string iconname = LibFileSearch("images", "lyx", "xpm");
257 if (!iconname.empty()) {
259 Pixmap lyx_p, lyx_mask;
260 lyx_p = fl_read_pixmapfile(fl_root,
268 fl_set_form_icon(form_, lyx_p, lyx_mask);
272 fl_set_form_minsize(form_, 50, 50);
279 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
283 // Set the textclass choice
284 invalidateLayoutChoice();
285 updateLayoutChoice();
286 UpdateDocumentClassChoice();
288 // Start autosave timer
289 if (lyxrc.autosave) {
290 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
291 autosave_timeout.start();
294 // Install the raw callback for keyboard events
295 fl_register_raw_callback(form_,
297 C_LyXView_KeyPressMask_raw_callback);
298 intl->InitKeyMapper(lyxrc.use_kbmap);
302 void LyXView::invalidateLayoutChoice()
308 void LyXView::updateLayoutChoice()
310 // Update the layout display
311 if (!toolbar->combox) return;
313 // This has a side-effect that the layouts are not showed when no
314 // document is loaded.
315 if (bufferview == 0 || bufferview->buffer() == 0) {
316 toolbar->combox->clear();
317 toolbar->combox->Redraw();
321 // If textclass is different, we need to update the list
322 if (toolbar->combox->empty() ||
323 (last_textclass != int(buffer()->params.textclass))) {
324 toolbar->combox->clear();
325 LyXTextClass const & tc =
326 textclasslist.TextClass(buffer()->params.textclass);
327 for (LyXTextClass::const_iterator cit = tc.begin();
328 cit != tc.end(); ++cit) {
329 if ((*cit).obsoleted_by().empty())
330 toolbar->combox->addline((*cit).name().c_str());
332 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
334 last_textclass = int(buffer()->params.textclass);
337 // we need to do this.
338 toolbar->combox->Redraw();
340 LyXTextClass::size_type layout =
341 bufferview->text->cursor.par()->GetLayout();
343 if (layout != current_layout){
344 toolbar->combox->select(layout + 1);
345 current_layout = layout;
350 void LyXView::UpdateDocumentClassChoice()
352 // Update the document class display in the document form
353 if (fd_form_document) {
354 fl_clear_choice(fd_form_document->choice_class);
355 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
356 cit != textclasslist.end(); ++cit) {
357 fl_addto_choice(fd_form_document->choice_class,
358 (*cit).description().c_str());
364 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
365 // as FL_KEYBOARD events :-( Matthias 280596
366 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
368 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
369 int retval = 0; // 0 means XForms should have a look at this event
371 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
372 static Time last_time_pressed = 0;
373 static Time last_time_released = 0;
374 static unsigned int last_key_pressed = 0;
375 static unsigned int last_key_released = 0;
376 static unsigned int last_state_pressed = 0;
377 static unsigned int last_state_released = 0;
379 // funny. Even though the raw_callback is registered with KeyPressMask,
380 // also KeyRelease-events are passed through:-(
381 // [It seems that XForms puts them in pairs... (JMarc)]
382 if (static_cast<XEvent*>(xev)->type == KeyPress
383 && view->bufferview->focus()
384 && view->bufferview->active())
386 last_time_pressed = xke->time;
387 last_key_pressed = xke->keycode;
388 last_state_pressed = xke->state;
389 retval = view->getLyXFunc()
390 ->processKeyEvent(static_cast<XEvent*>(xev));
392 else if (static_cast<XEvent*>(xev)->type == KeyRelease
393 && view->bufferview->focus()
394 && view->bufferview->active())
396 last_time_released = xke->time;
397 last_key_released = xke->keycode;
398 last_state_released = xke->state;
401 if (last_key_released == last_key_pressed
402 && last_state_released == last_state_pressed
403 && last_time_released == last_time_pressed) {
404 // When the diff between last_time_released and
405 // last_time_pressed is 0, that sinifies an autoreapeat
406 // at least on my system. It like some feedback from
407 // others, especially from user running LyX remote.
408 //lyxerr << "Syncing - purging X events." << endl;
409 XSync(fl_get_display(), 1);
410 // This purge make f.ex. scrolling stop imidiatly when
411 // releaseing the PageDown button. The question is if this
412 // purging of XEvents can cause any harm...after some testing
413 // I can see no problems, but I'd like other reports too.
419 // wrapper for the above
421 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
423 return LyXView::KeyPressMask_raw_callback(fl, xev);
427 // Updates the title of the window with the filename of the current document
428 void LyXView::updateWindowTitle()
430 static string last_title = "LyX";
431 string title = "LyX";
433 if (view()->available()) {
434 string cur_title = buffer()->fileName();
435 if (!cur_title.empty()){
436 title += ": " + OnlyFilename(cur_title);
437 if (!buffer()->isLyxClean())
438 title += _(" (Changed)");
439 if (buffer()->isReadonly())
440 title += _(" (read only)");
443 // Don't update title if it's the same as last time
444 if (title != last_title) {
445 fl_set_form_title(form_, title.c_str());
451 void LyXView::showState()
453 getMiniBuffer()->Set(CurrentState(view()));
454 getToolbar()->update();