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()
38 extern FD_form_document * fd_form_document;
40 extern void AutoSave();
41 extern char updatetimer;
42 extern void QuitLyX();
43 LyXTextClass::size_type current_layout = 0;
45 // This is very temporary
46 BufferView * current_view;
48 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
50 LyXView::LyXView(int width, int height)
52 create_form_form_main(width, height);
53 fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
54 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
55 lyxfunc = new LyXFunc(this);
71 /// Redraw the main form.
72 void LyXView::redraw() {
73 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
74 fl_redraw_form(_form);
75 minibuffer->Activate();
79 // Callback for update timer
80 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
82 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
83 if (!view->view()->available())
88 view->view()->hideCursor();
89 view->view()->update(-2);
91 /* This update can happen, even when the work area has lost
92 * the focus. So suppress the cursor in that case */
97 // Wrapper for the above
98 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
100 LyXView::UpdateTimerCB(ob, data);
104 // Callback for autosave timer
105 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
107 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
112 // Wrapper for the above
113 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
115 LyXView::AutosaveTimerCB(ob, data);
119 /// Reset autosave timer
120 void LyXView::resetAutosaveTimer()
123 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
127 // Callback for close main form from window manager
128 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
135 // Wrapper for the above
136 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
138 return LyXView::atCloseMainFormCB(form, p);
142 void LyXView::setPosition(int x, int y)
144 fl_set_form_position(_form, x, y);
148 void LyXView::show(int place, int border, char const * title)
150 fl_show_form(_form, place, border, title);
152 InitLyXLookup(fl_display, _form->window);
156 void LyXView::create_form_form_main(int width, int height)
157 /* to make this work as it should, .lyxrc should have been
158 * read first; OR maybe this one should be made dynamic.
160 * We will probably not have lyxrc before the main form is
161 * initialized, because error messages from lyxrc parsing
162 * are presented (and rightly so) in GUI popups. Asger.
165 FD_form_main * fdui = static_cast<FD_form_main *>
166 (fl_calloc(1, sizeof(FD_form_main)));
171 _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
172 fdui->form_main->u_vdata = this;
173 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
174 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
176 // Parameters for the appearance of the main form
178 int const bw = abs(fl_get_border_width());
184 menus = new Menus(this, air);
190 toolbar = new Toolbar(this, air, 30 + air + bw);
199 int const ywork = 60 + 2 * air + bw;
200 int const workheight = height - ywork - (25 + 2 * air);
202 ::current_view = bufferview = new BufferView(this, air, ywork,
210 minibuffer = new MiniBuffer(this, air, height - (25 + air),
211 width - (2 * air), 25);
218 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
219 0, 0, 0, 0, "Timer");
220 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
223 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
224 0, 0, 0, 0, "Timer");
225 fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
232 // assign an icon to main form
234 Pixmap lyx_p, lyx_mask;
235 lyx_p = fl_create_from_pixmapdata(fl_root,
236 const_cast<char**>(lyx_xpm),
243 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
246 fl_set_form_minsize(fdui->form_main, 50, 50);
252 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
256 // Set the textclass choice
257 invalidateLayoutChoice();
258 updateLayoutChoice();
259 UpdateDocumentClassChoice();
261 // Start autosave timer
263 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
266 // Install the raw callback for keyboard events
267 fl_register_raw_callback(_form,
269 C_LyXView_KeyPressMask_raw_callback);
270 intl->InitKeyMapper(lyxrc.use_kbmap);
274 void LyXView::invalidateLayoutChoice()
280 void LyXView::updateLayoutChoice()
282 // Update the layout display
283 if (!toolbar->combox) return;
285 // This has a side-effect that the layouts are not showed when no
286 // document is loaded.
287 if (bufferview == 0 || bufferview->buffer() == 0) {
288 toolbar->combox->clear();
289 toolbar->combox->Redraw();
293 // If textclass is different, we need to update the list
294 if (toolbar->combox->empty() ||
295 (last_textclass != int(buffer()->params.textclass))) {
296 toolbar->combox->clear();
297 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
298 for (LyXTextClass::const_iterator cit = tc.begin();
299 cit != tc.end(); ++cit) {
300 if ((*cit).obsoleted_by().empty())
301 toolbar->combox->addline((*cit).name().c_str());
303 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
305 last_textclass = int(buffer()->params.textclass);
308 // we need to do this.
309 toolbar->combox->Redraw();
311 LyXTextClass::size_type layout = bufferview->text->cursor.par->GetLayout();
313 if (layout != current_layout){
314 toolbar->combox->select(layout + 1);
315 current_layout = layout;
320 void LyXView::UpdateDocumentClassChoice()
322 // Update the document class display in the document form
323 if (fd_form_document) {
324 fl_clear_choice(fd_form_document->choice_class);
325 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
326 cit != textclasslist.end(); ++cit) {
327 fl_addto_choice(fd_form_document->choice_class,
328 (*cit).description().c_str());
334 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
335 // as FL_KEYBOARD events :-( Matthias 280596
336 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
338 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
339 int retval = 0; // 0 means XForms should have a look at this event
341 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
342 static Time last_time_pressed = 0;
343 static Time last_time_released = 0;
344 static unsigned int last_key_pressed = 0;
345 static unsigned int last_key_released = 0;
346 static unsigned int last_state_pressed = 0;
347 static unsigned int last_state_released = 0;
349 // funny. Even though the raw_callback is registered with KeyPressMask,
350 // also KeyRelease-events are passed through:-(
351 // [It seems that XForms puts them in pairs... (JMarc)]
352 if (static_cast<XEvent*>(xev)->type == KeyPress
353 && view->bufferview->focus()
354 && view->bufferview->active())
356 last_time_pressed = xke->time;
357 last_key_pressed = xke->keycode;
358 last_state_pressed = xke->state;
359 retval = view->getLyXFunc()
360 ->processKeyEvent(static_cast<XEvent*>(xev));
362 else if (static_cast<XEvent*>(xev)->type == KeyRelease
363 && view->bufferview->focus()
364 && view->bufferview->active())
366 last_time_released = xke->time;
367 last_key_released = xke->keycode;
368 last_state_released = xke->state;
371 if (last_key_released == last_key_pressed
372 && last_state_released == last_state_pressed
373 && last_time_released == last_time_pressed) {
374 // When the diff between last_time_released and
375 // last_time_pressed is 0, that sinifies an autoreapeat
376 // at least on my system. It like some feedback from
377 // others, especially from user running LyX remote.
378 //lyxerr << "Syncing - purging X events." << endl;
379 XSync(fl_get_display(), 1);
380 // This purge make f.ex. scrolling stop imidiatly when
381 // releaseing the PageDown button. The question is if this
382 // purging of XEvents can cause any harm...after some testing
383 // I can see no problems, but I'd like other reports too.
389 // wrapper for the above
390 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
392 return LyXView::KeyPressMask_raw_callback(fl, xev);
396 // Updates the title of the window with the filename of the current document
397 void LyXView::updateWindowTitle()
399 static string last_title = "LyX";
400 string title = "LyX";
402 if (view()->available()) {
403 string cur_title = buffer()->fileName();
404 if (!cur_title.empty()){
405 title += ": " + OnlyFilename(cur_title);
406 if (!buffer()->isLyxClean())
407 title += _(" (Changed)");
408 if (buffer()->isReadonly())
409 title += _(" (read only)");
412 // Don't update title if it's the same as last time
413 if (title != last_title) {
414 fl_set_form_title(_form, title.c_str());