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()
36 extern FD_form_document * fd_form_document;
38 extern void AutoSave();
39 extern char updatetimer;
40 extern void QuitLyX();
41 LyXTextClass::size_type current_layout = 0;
43 // This is very temporary
44 BufferView * current_view;
46 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
48 LyXView::LyXView(int width, int height)
50 create_form_form_main(width, height);
51 fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
52 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
53 lyxfunc = new LyXFunc(this);
69 /// Redraw the main form.
70 void LyXView::redraw() {
71 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
72 fl_redraw_form(_form);
73 minibuffer->Activate();
77 // Callback for update timer
78 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
80 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
81 if (!view->view()->available())
86 view->view()->hideCursor();
87 view->view()->update(-2);
89 /* This update can happen, even when the work area has lost
90 * the focus. So suppress the cursor in that case */
95 // Wrapper for the above
96 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
98 LyXView::UpdateTimerCB(ob, data);
102 // Callback for autosave timer
103 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
105 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
110 // Wrapper for the above
111 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
113 LyXView::AutosaveTimerCB(ob, data);
117 /// Reset autosave timer
118 void LyXView::resetAutosaveTimer()
121 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
125 // Callback for close main form from window manager
126 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
133 // Wrapper for the above
134 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
136 return LyXView::atCloseMainFormCB(form, p);
140 void LyXView::setPosition(int x, int y)
142 fl_set_form_position(_form, x, y);
146 void LyXView::show(int place, int border, char const * title)
148 fl_show_form(_form, place, border, title);
150 InitLyXLookup(fl_display, _form->window);
154 void LyXView::create_form_form_main(int width, int height)
155 /* to make this work as it should, .lyxrc should have been
156 * read first; OR maybe this one should be made dynamic.
158 * We will probably not have lyxrc before the main form is
159 * initialized, because error messages from lyxrc parsing
160 * are presented (and rightly so) in GUI popups. Asger.
163 FD_form_main * fdui = static_cast<FD_form_main *>
164 (fl_calloc(1, sizeof(FD_form_main)));
169 _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
170 fdui->form_main->u_vdata = this;
171 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
172 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
174 // Parameters for the appearance of the main form
176 int const bw = abs(fl_get_border_width());
182 menus = new Menus(this, air);
188 toolbar = new Toolbar(this, air, 30 + air + bw);
197 int const ywork = 60 + 2 * air + bw;
198 int const workheight = height - ywork - (25 + 2 * air);
200 ::current_view = bufferview = new BufferView(this, air, ywork,
208 minibuffer = new MiniBuffer(this, air, height - (25 + air),
209 width - (2 * air), 25);
216 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
217 0, 0, 0, 0, "Timer");
218 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
221 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
222 0, 0, 0, 0, "Timer");
223 fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
230 // assign an icon to main form
232 Pixmap lyx_p, lyx_mask;
233 lyx_p = fl_create_from_pixmapdata(fl_root,
234 const_cast<char**>(lyx_xpm),
241 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
244 fl_set_form_minsize(fdui->form_main, 50, 50);
250 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
254 // Set the textclass choice
255 invalidateLayoutChoice();
256 updateLayoutChoice();
257 UpdateDocumentClassChoice();
259 // Start autosave timer
261 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
264 // Install the raw callback for keyboard events
265 fl_register_raw_callback(_form,
267 C_LyXView_KeyPressMask_raw_callback);
268 intl->InitKeyMapper(lyxrc.use_kbmap);
272 void LyXView::invalidateLayoutChoice()
278 void LyXView::updateLayoutChoice()
280 // Update the layout display
281 if (!toolbar->combox) return;
283 // This has a side-effect that the layouts are not showed when no
284 // document is loaded.
285 if (bufferview == 0 || bufferview->buffer() == 0) {
286 toolbar->combox->clear();
287 toolbar->combox->Redraw();
291 // If textclass is different, we need to update the list
292 if (toolbar->combox->empty() ||
293 (last_textclass != int(buffer()->params.textclass))) {
294 toolbar->combox->clear();
295 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
296 for (LyXTextClass::const_iterator cit = tc.begin();
297 cit != tc.end(); ++cit) {
298 if ((*cit).obsoleted_by().empty())
299 toolbar->combox->addline((*cit).name().c_str());
301 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
303 last_textclass = int(buffer()->params.textclass);
306 // we need to do this.
307 toolbar->combox->Redraw();
309 LyXTextClass::size_type layout = bufferview->text->cursor.par->GetLayout();
311 if (layout != current_layout){
312 toolbar->combox->select(layout + 1);
313 current_layout = layout;
318 void LyXView::UpdateDocumentClassChoice()
320 // Update the document class display in the document form
321 if (fd_form_document) {
322 fl_clear_choice(fd_form_document->choice_class);
323 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
324 cit != textclasslist.end(); ++cit) {
325 fl_addto_choice(fd_form_document->choice_class,
326 (*cit).description().c_str());
332 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
333 // as FL_KEYBOARD events :-( Matthias 280596
334 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
336 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
337 int retval = 0; // 0 means XForms should have a look at this event
339 XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
340 static Time last_time_pressed = 0;
341 static Time last_time_released = 0;
342 static unsigned int last_key_pressed = 0;
343 static unsigned int last_key_released = 0;
344 static unsigned int last_state_pressed = 0;
345 static unsigned int last_state_released = 0;
347 // funny. Even though the raw_callback is registered with KeyPressMask,
348 // also KeyRelease-events are passed through:-(
349 // [It seems that XForms puts them in pairs... (JMarc)]
350 if (static_cast<XEvent*>(xev)->type == KeyPress
351 && view->bufferview->focus()
352 && view->bufferview->active())
354 last_time_pressed = xke->time;
355 last_key_pressed = xke->keycode;
356 last_state_pressed = xke->state;
357 retval = view->getLyXFunc()
358 ->processKeyEvent(static_cast<XEvent*>(xev));
360 else if (static_cast<XEvent*>(xev)->type == KeyRelease
361 && view->bufferview->focus()
362 && view->bufferview->active())
364 last_time_released = xke->time;
365 last_key_released = xke->keycode;
366 last_state_released = xke->state;
369 if (last_key_released == last_key_pressed
370 && last_state_released == last_state_pressed
371 && last_time_released == last_time_pressed) {
372 // When the diff between last_time_released and
373 // last_time_pressed is 0, that sinifies an autoreapeat
374 // at least on my system. It like some feedback from
375 // others, especially from user running LyX remote.
376 //lyxerr << "Syncing - purging X events." << endl;
377 XSync(fl_get_display(), 1);
378 // This purge make f.ex. scrolling stop imidiatly when
379 // releaseing the PageDown button. The question is if this
380 // purging of XEvents can cause any harm...after some testing
381 // I can see no problems, but I'd like other reports too.
387 // wrapper for the above
388 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
390 return LyXView::KeyPressMask_raw_callback(fl, xev);
394 // Updates the title of the window with the filename of the current document
395 void LyXView::updateWindowTitle()
397 static string last_title = "LyX";
398 string title = "LyX";
400 if (view()->available()) {
401 string cur_title = buffer()->fileName();
402 if (!cur_title.empty()){
403 title += ": " + OnlyFilename(cur_title);
404 if (!buffer()->isLyxClean())
405 title += _(" (Changed)");
406 if (buffer()->isReadonly())
407 title += _(" (read only)");
410 // Don't update title if it's the same as last time
411 if (title != last_title) {
412 fl_set_form_title(_form, title.c_str());