2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1999 The LyX Team.
10 * ====================================================== */
15 #pragma implementation
23 #include "lyxlookup.h"
25 #include "minibuffer.h"
29 #include "layout_forms.h"
32 #include "lyxscreen.h"
33 #include "support/filetools.h" // OnlyFilename()
37 extern FD_form_document * fd_form_document;
39 extern void AutoSave();
40 extern char updatetimer;
41 extern void QuitLyX();
42 int current_layout = 0;
44 // This is very temporary
45 BufferView * current_view;
47 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
49 LyXView::LyXView(int width, int height)
51 create_form_form_main(width, height);
52 fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
53 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
54 lyxfunc = new LyXFunc(this);
70 /// Redraw the main form.
71 void LyXView::redraw() {
72 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
73 fl_redraw_form(_form);
74 minibuffer->Activate();
78 // Callback for update timer
79 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
81 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
82 if (!view->view()->available())
84 if (!view->view()->getScreen() || !updatetimer)
87 view->view()->getScreen()->HideCursor();
88 view->view()->update(-2);
90 /* This update can happen, even when the work area has lost
91 * the focus. So suppress the cursor in that case */
96 // Wrapper for the above
97 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
99 LyXView::UpdateTimerCB(ob, data);
103 // Callback for autosave timer
104 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
106 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
111 // Wrapper for the above
112 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
114 LyXView::AutosaveTimerCB(ob, data);
118 /// Reset autosave timer
119 void LyXView::resetAutosaveTimer()
122 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
126 // Callback for close main form from window manager
127 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
134 // Wrapper for the above
135 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
137 return LyXView::atCloseMainFormCB(form, p);
141 void LyXView::setPosition(int x, int y)
143 fl_set_form_position(_form, x, y);
147 void LyXView::show(int place, int border, char const * title)
149 fl_show_form(_form, place, border, title);
151 InitLyXLookup(fl_display, _form->window);
155 void LyXView::create_form_form_main(int width, int height)
156 /* to make this work as it should, .lyxrc should have been
157 * read first; OR maybe this one should be made dynamic.
159 * We will probably not have lyxrc before the main form is
160 * initialized, because error messages from lyxrc parsing
161 * are presented (and rightly so) in GUI popups. Asger.
164 FD_form_main * fdui = static_cast<FD_form_main *>
165 (fl_calloc(1, sizeof(FD_form_main)));
170 _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
171 fdui->form_main->u_vdata = this;
172 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
173 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
175 // Parameters for the appearance of the main form
177 int const bw = abs(fl_get_border_width());
183 menus = new Menus(this, air);
189 toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
198 int const ywork = 60 + 2 * air + bw;
199 int const workheight = height - ywork - (25 + 2 * air);
201 ::current_view = bufferview = new BufferView(this, air, ywork,
209 minibuffer = new MiniBuffer(this, air, height - (25 + air),
210 width - (2 * air), 25);
217 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
218 0, 0, 0, 0, "Timer");
219 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
222 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
223 0, 0, 0, 0, "Timer");
224 fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
231 // assign an icon to main form
233 Pixmap lyx_p, lyx_mask;
234 lyx_p = fl_create_from_pixmapdata(fl_root,
235 const_cast<char**>(lyx_xpm),
242 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
245 fl_set_form_minsize(fdui->form_main, 50, 50);
251 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
255 // Set the textclass choice
256 invalidateLayoutChoice();
257 updateLayoutChoice();
258 UpdateDocumentClassChoice();
260 // Start autosave timer
262 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
265 // Install the raw callback for keyboard events
266 fl_register_raw_callback(_form,
268 C_LyXView_KeyPressMask_raw_callback);
269 intl->InitKeyMapper(lyxrc->use_kbmap);
273 void LyXView::invalidateLayoutChoice()
279 void LyXView::updateLayoutChoice()
281 // Update the layout display
282 if (!toolbar->combox) return;
284 // This has a side-effect that the layouts are not showed when no
285 // document is loaded.
286 if (bufferview == 0 || bufferview->buffer() == 0) {
287 toolbar->combox->clear();
288 toolbar->combox->Redraw();
292 // If textclass is different, we need to update the list
293 if (toolbar->combox->empty() ||
294 (last_textclass != int(buffer()->params.textclass))) {
295 toolbar->combox->clear();
296 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
297 for (LyXTextClass::const_iterator cit = tc.begin();
298 cit != tc.end(); ++cit) {
299 if ((*cit).obsoleted_by().empty())
300 toolbar->combox->addline((*cit).name().c_str());
302 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
304 last_textclass = int(buffer()->params.textclass);
307 // we need to do this.
308 toolbar->combox->Redraw();
310 char layout = bufferview->text->cursor.par->GetLayout();
312 if (layout != current_layout){
313 toolbar->combox->select(layout + 1);
314 current_layout = layout;
319 void LyXView::UpdateDocumentClassChoice()
321 // Update the document class display in the document form
322 if (fd_form_document) {
323 fl_clear_choice(fd_form_document->choice_class);
324 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
325 cit != textclasslist.end(); ++cit) {
326 fl_addto_choice(fd_form_document->choice_class,
327 (*cit).description().c_str());
333 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
334 // as FL_KEYBOARD events :-( Matthias 280596
335 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
337 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
338 int retval = 0; // 0 means XForms should have a look at this event
342 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
343 static Time last_time_pressed = 0;
344 static Time last_time_released = 0;
345 static unsigned int last_key_pressed = 0;
346 static unsigned int last_key_released = 0;
347 static unsigned int last_state_pressed = 0;
348 static unsigned int last_state_released = 0;
350 // funny. Even though the raw_callback is registered with KeyPressMask,
351 // also KeyRelease-events are passed through:-(
352 // [It seems that XForms puts them in pairs... (JMarc)]
353 if (static_cast<XEvent*>(xev)->type == KeyPress
354 && view->bufferview->getWorkArea()->focus
355 && view->bufferview->getWorkArea()->active) {
357 last_time_pressed = xke->time;
358 last_key_pressed = xke->keycode;
359 last_state_pressed = xke->state;
361 retval = view->getLyXFunc()
362 ->processKeyEvent(static_cast<XEvent*>(xev));
365 else if (static_cast<XEvent*>(xev)->type == KeyRelease
366 && view->bufferview->getWorkArea()->focus
367 && view->bufferview->getWorkArea()->active) {
368 last_time_released = xke->time;
369 last_key_released = xke->keycode;
370 last_state_released = xke->state;
373 if (last_key_released == last_key_pressed
374 && last_state_released == last_state_pressed
375 && last_time_released == last_time_pressed) {
376 // When the diff between last_time_released and
377 // last_time_pressed is 0, that sinifies an autoreapeat
378 // at least on my system. It like some feedback from
379 // others, especially from user running LyX remote.
380 //lyxerr << "Syncing - purging X events." << endl;
381 XSync(fl_get_display(), 1);
382 // This purge make f.ex. scrolling stop imidiatly when
383 // releaseing the PageDown button. The question is if this
384 // purging of XEvents can cause any harm...after some testing
385 // I can see no problems, but I'd like other reports too.
392 // wrapper for the above
393 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
395 return LyXView::KeyPressMask_raw_callback(fl, xev);
399 // Updates the title of the window with the filename of the current document
400 void LyXView::updateWindowTitle()
402 static string last_title = "LyX";
403 string title = "LyX";
405 if (view()->available()) {
406 string cur_title = buffer()->fileName();
407 if (!cur_title.empty()){
408 title += ": " + OnlyFilename(cur_title);
409 if (!buffer()->isLyxClean())
410 title += _(" (Changed)");
411 if (buffer()->isReadonly())
412 title += _(" (read only)");
415 // Don't update title if it's the same as last time
416 if (title != last_title) {
417 fl_set_form_title(_form, title.c_str());