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)
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 dialogs_ = new Dialogs(this);
67 // temporary until all dialogs moved into Dialogs.
68 dialogs_->updateBufferDependent
69 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
70 dialogs_->hideBufferDependent
71 .connect(slot(&CloseAllBufferRelatedDialogs));
87 /// Redraw the main form.
88 void LyXView::redraw() {
89 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
90 fl_redraw_form(form_);
91 minibuffer->Activate();
95 /// returns the buffer currently shown in the main form.
96 Buffer * LyXView::buffer() const
98 return bufferview->buffer();
102 BufferView * LyXView::view() const
108 FL_FORM * LyXView::getForm() const
114 Toolbar * LyXView::getToolbar() const
120 LyXFunc * LyXView::getLyXFunc() const
126 MiniBuffer * LyXView::getMiniBuffer() const
132 Menus * LyXView::getMenus() const
138 Intl * LyXView::getIntl() const
144 // Callback for autosave timer
145 void LyXView::AutoSave()
147 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
148 if (view()->available())
153 /// Reset autosave timer
154 void LyXView::resetAutosaveTimer()
157 autosave_timeout.restart();
161 // Callback for close main form from window manager
162 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
169 // Wrapper for the above
170 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
172 return LyXView::atCloseMainFormCB(form, p);
176 void LyXView::setPosition(int x, int y)
178 fl_set_form_position(form_, x, y);
182 void LyXView::show(int place, int border, char const * title)
184 fl_show_form(form_, place, border, title);
186 InitLyXLookup(fl_display, form_->window);
190 void LyXView::create_form_form_main(int width, int height)
191 /* to make this work as it should, .lyxrc should have been
192 * read first; OR maybe this one should be made dynamic.
194 * We will probably not have lyxrc before the main form is
195 * initialized, because error messages from lyxrc parsing
196 * are presented (and rightly so) in GUI popups. Asger.
200 form_ = fl_bgn_form(FL_NO_BOX, width, height);
201 form_->u_vdata = this;
202 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
203 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
205 // Parameters for the appearance of the main form
207 int const bw = abs(fl_get_border_width());
213 menus = new Menus(this, air);
219 toolbar = new Toolbar(this, air, 30 + air + bw);
228 int const ywork = 60 + 2 * air + bw;
229 int const workheight = height - ywork - (25 + 2 * air);
231 ::current_view = bufferview = new BufferView(this, air, ywork,
239 minibuffer = new MiniBuffer(this, air, height - (25 + air),
240 width - (2 * air), 25);
246 autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
252 // assign an icon to main form
254 Pixmap lyx_p, lyx_mask;
255 lyx_p = fl_create_from_pixmapdata(fl_root,
256 const_cast<char**>(lyx_xpm),
263 fl_set_form_icon(form_, lyx_p, lyx_mask);
266 fl_set_form_minsize(form_, 50, 50);
273 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
277 // Set the textclass choice
278 invalidateLayoutChoice();
279 updateLayoutChoice();
280 UpdateDocumentClassChoice();
282 // Start autosave timer
283 if (lyxrc.autosave) {
284 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
285 autosave_timeout.start();
288 // Install the raw callback for keyboard events
289 fl_register_raw_callback(form_,
291 C_LyXView_KeyPressMask_raw_callback);
292 intl->InitKeyMapper(lyxrc.use_kbmap);
296 void LyXView::invalidateLayoutChoice()
302 void LyXView::updateLayoutChoice()
304 // Update the layout display
305 if (!toolbar->combox) return;
307 // This has a side-effect that the layouts are not showed when no
308 // document is loaded.
309 if (bufferview == 0 || bufferview->buffer() == 0) {
310 toolbar->combox->clear();
311 toolbar->combox->Redraw();
315 // If textclass is different, we need to update the list
316 if (toolbar->combox->empty() ||
317 (last_textclass != int(buffer()->params.textclass))) {
318 toolbar->combox->clear();
319 LyXTextClass const & tc =
320 textclasslist.TextClass(buffer()->params.textclass);
321 for (LyXTextClass::const_iterator cit = tc.begin();
322 cit != tc.end(); ++cit) {
323 if ((*cit).obsoleted_by().empty())
324 toolbar->combox->addline((*cit).name().c_str());
326 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
328 last_textclass = int(buffer()->params.textclass);
331 // we need to do this.
332 toolbar->combox->Redraw();
334 LyXTextClass::size_type layout =
335 bufferview->text->cursor.par()->GetLayout();
337 if (layout != current_layout){
338 toolbar->combox->select(layout + 1);
339 current_layout = layout;
344 void LyXView::UpdateDocumentClassChoice()
346 // Update the document class display in the document form
347 if (fd_form_document) {
348 fl_clear_choice(fd_form_document->choice_class);
349 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
350 cit != textclasslist.end(); ++cit) {
351 fl_addto_choice(fd_form_document->choice_class,
352 (*cit).description().c_str());
358 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
359 // as FL_KEYBOARD events :-( Matthias 280596
360 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
362 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
363 int retval = 0; // 0 means XForms should have a look at this event
365 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
366 static Time last_time_pressed = 0;
367 static Time last_time_released = 0;
368 static unsigned int last_key_pressed = 0;
369 static unsigned int last_key_released = 0;
370 static unsigned int last_state_pressed = 0;
371 static unsigned int last_state_released = 0;
373 // funny. Even though the raw_callback is registered with KeyPressMask,
374 // also KeyRelease-events are passed through:-(
375 // [It seems that XForms puts them in pairs... (JMarc)]
376 if (static_cast<XEvent*>(xev)->type == KeyPress
377 && view->bufferview->focus()
378 && view->bufferview->active())
380 last_time_pressed = xke->time;
381 last_key_pressed = xke->keycode;
382 last_state_pressed = xke->state;
383 retval = view->getLyXFunc()
384 ->processKeyEvent(static_cast<XEvent*>(xev));
386 else if (static_cast<XEvent*>(xev)->type == KeyRelease
387 && view->bufferview->focus()
388 && view->bufferview->active())
390 last_time_released = xke->time;
391 last_key_released = xke->keycode;
392 last_state_released = xke->state;
395 if (last_key_released == last_key_pressed
396 && last_state_released == last_state_pressed
397 && last_time_released == last_time_pressed) {
398 // When the diff between last_time_released and
399 // last_time_pressed is 0, that sinifies an autoreapeat
400 // at least on my system. It like some feedback from
401 // others, especially from user running LyX remote.
402 //lyxerr << "Syncing - purging X events." << endl;
403 XSync(fl_get_display(), 1);
404 // This purge make f.ex. scrolling stop imidiatly when
405 // releaseing the PageDown button. The question is if this
406 // purging of XEvents can cause any harm...after some testing
407 // I can see no problems, but I'd like other reports too.
413 // wrapper for the above
415 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
417 return LyXView::KeyPressMask_raw_callback(fl, xev);
421 // Updates the title of the window with the filename of the current document
422 void LyXView::updateWindowTitle()
424 static string last_title = "LyX";
425 string title = "LyX";
427 if (view()->available()) {
428 string cur_title = buffer()->fileName();
429 if (!cur_title.empty()){
430 title += ": " + OnlyFilename(cur_title);
431 if (!buffer()->isLyxClean())
432 title += _(" (Changed)");
433 if (buffer()->isReadonly())
434 title += _(" (read only)");
437 // Don't update title if it's the same as last time
438 if (title != last_title) {
439 fl_set_form_title(form_, title.c_str());