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
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)
59 // : update_timeout(300)
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 dialogs_ = new Dialogs(this);
68 // temporary until all dialogs moved into Dialogs.
69 dialogs_->updateBufferDependent
70 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
71 dialogs_->hideBufferDependent
72 .connect(slot(&CloseAllBufferRelatedDialogs));
88 /// Redraw the main form.
89 void LyXView::redraw() {
90 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
91 fl_redraw_form(form_);
92 minibuffer->Activate();
96 /// returns the buffer currently shown in the main form.
97 Buffer * LyXView::buffer() const
99 return bufferview->buffer();
103 BufferView * LyXView::view() const
109 FD_form_main * LyXView::getMainForm() const
115 FL_FORM * LyXView::getForm() const
121 Toolbar * LyXView::getToolbar() const
127 LyXFunc * LyXView::getLyXFunc() const
133 MiniBuffer * LyXView::getMiniBuffer() const
139 Menus * LyXView::getMenus() const
145 Intl * LyXView::getIntl() const
151 // Callback for autosave timer
152 void LyXView::AutoSave()
154 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
155 if (view()->available())
160 // Wrapper for the above
163 void C_LyXView_AutosaveTimerCB(void * ob)
165 LyXView * view = static_cast<LyXView*>(ob);
170 /// Reset autosave timer
171 void LyXView::resetAutosaveTimer()
174 autosave_timeout.restart();
178 // Callback for close main form from window manager
179 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
186 // Wrapper for the above
187 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
189 return LyXView::atCloseMainFormCB(form, p);
193 void LyXView::setPosition(int x, int y)
195 fl_set_form_position(form_, x, y);
199 void LyXView::show(int place, int border, char const * title)
201 fl_show_form(form_, place, border, title);
203 InitLyXLookup(fl_display, form_->window);
207 void LyXView::create_form_form_main(int width, int height)
208 /* to make this work as it should, .lyxrc should have been
209 * read first; OR maybe this one should be made dynamic.
211 * We will probably not have lyxrc before the main form is
212 * initialized, because error messages from lyxrc parsing
213 * are presented (and rightly so) in GUI popups. Asger.
216 FD_form_main * fdui = static_cast<FD_form_main *>
217 (fl_calloc(1, sizeof(FD_form_main)));
222 form_ = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
223 fdui->form_main->u_vdata = this;
224 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
225 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
227 // Parameters for the appearance of the main form
229 int const bw = abs(fl_get_border_width());
235 menus = new Menus(this, air);
241 toolbar = new Toolbar(this, air, 30 + air + bw);
250 int const ywork = 60 + 2 * air + bw;
251 int const workheight = height - ywork - (25 + 2 * air);
253 ::current_view = bufferview = new BufferView(this, air, ywork,
261 minibuffer = new MiniBuffer(this, air, height - (25 + air),
262 width - (2 * air), 25);
268 autosave_timeout.callback(C_LyXView_AutosaveTimerCB, this);
274 // assign an icon to main form
276 Pixmap lyx_p, lyx_mask;
277 lyx_p = fl_create_from_pixmapdata(fl_root,
278 const_cast<char**>(lyx_xpm),
285 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
288 fl_set_form_minsize(fdui->form_main, 50, 50);
294 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
298 // Set the textclass choice
299 invalidateLayoutChoice();
300 updateLayoutChoice();
301 UpdateDocumentClassChoice();
303 // Start autosave timer
306 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
308 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
309 autosave_timeout.start();
312 // Install the raw callback for keyboard events
313 fl_register_raw_callback(form_,
315 C_LyXView_KeyPressMask_raw_callback);
316 intl->InitKeyMapper(lyxrc.use_kbmap);
320 void LyXView::invalidateLayoutChoice()
326 void LyXView::updateLayoutChoice()
328 // Update the layout display
329 if (!toolbar->combox) return;
331 // This has a side-effect that the layouts are not showed when no
332 // document is loaded.
333 if (bufferview == 0 || bufferview->buffer() == 0) {
334 toolbar->combox->clear();
335 toolbar->combox->Redraw();
339 // If textclass is different, we need to update the list
340 if (toolbar->combox->empty() ||
341 (last_textclass != int(buffer()->params.textclass))) {
342 toolbar->combox->clear();
343 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
344 for (LyXTextClass::const_iterator cit = tc.begin();
345 cit != tc.end(); ++cit) {
346 if ((*cit).obsoleted_by().empty())
347 toolbar->combox->addline((*cit).name().c_str());
349 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
351 last_textclass = int(buffer()->params.textclass);
354 // we need to do this.
355 toolbar->combox->Redraw();
357 LyXTextClass::size_type layout = bufferview->text->cursor.par()->GetLayout();
359 if (layout != current_layout){
360 toolbar->combox->select(layout + 1);
361 current_layout = layout;
366 void LyXView::UpdateDocumentClassChoice()
368 // Update the document class display in the document form
369 if (fd_form_document) {
370 fl_clear_choice(fd_form_document->choice_class);
371 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
372 cit != textclasslist.end(); ++cit) {
373 fl_addto_choice(fd_form_document->choice_class,
374 (*cit).description().c_str());
380 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
381 // as FL_KEYBOARD events :-( Matthias 280596
382 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
384 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
385 int retval = 0; // 0 means XForms should have a look at this event
387 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
388 static Time last_time_pressed = 0;
389 static Time last_time_released = 0;
390 static unsigned int last_key_pressed = 0;
391 static unsigned int last_key_released = 0;
392 static unsigned int last_state_pressed = 0;
393 static unsigned int last_state_released = 0;
395 // funny. Even though the raw_callback is registered with KeyPressMask,
396 // also KeyRelease-events are passed through:-(
397 // [It seems that XForms puts them in pairs... (JMarc)]
398 if (static_cast<XEvent*>(xev)->type == KeyPress
399 && view->bufferview->focus()
400 && view->bufferview->active())
402 last_time_pressed = xke->time;
403 last_key_pressed = xke->keycode;
404 last_state_pressed = xke->state;
405 retval = view->getLyXFunc()
406 ->processKeyEvent(static_cast<XEvent*>(xev));
408 else if (static_cast<XEvent*>(xev)->type == KeyRelease
409 && view->bufferview->focus()
410 && view->bufferview->active())
412 last_time_released = xke->time;
413 last_key_released = xke->keycode;
414 last_state_released = xke->state;
417 if (last_key_released == last_key_pressed
418 && last_state_released == last_state_pressed
419 && last_time_released == last_time_pressed) {
420 // When the diff between last_time_released and
421 // last_time_pressed is 0, that sinifies an autoreapeat
422 // at least on my system. It like some feedback from
423 // others, especially from user running LyX remote.
424 //lyxerr << "Syncing - purging X events." << endl;
425 XSync(fl_get_display(), 1);
426 // This purge make f.ex. scrolling stop imidiatly when
427 // releaseing the PageDown button. The question is if this
428 // purging of XEvents can cause any harm...after some testing
429 // I can see no problems, but I'd like other reports too.
435 // wrapper for the above
436 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
438 return LyXView::KeyPressMask_raw_callback(fl, xev);
442 // Updates the title of the window with the filename of the current document
443 void LyXView::updateWindowTitle()
445 static string last_title = "LyX";
446 string title = "LyX";
448 if (view()->available()) {
449 string cur_title = buffer()->fileName();
450 if (!cur_title.empty()){
451 title += ": " + OnlyFilename(cur_title);
452 if (!buffer()->isLyxClean())
453 title += _(" (Changed)");
454 if (buffer()->isReadonly())
455 title += _(" (read only)");
458 // Don't update title if it's the same as last time
459 if (title != last_title) {
460 fl_set_form_title(form_, title.c_str());