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
20 #include "lyxlookup.h"
22 #include "minibuffer.h"
26 #include "layout_forms.h"
29 #include "lyxscreen.h"
30 #include "support/filetools.h" // OnlyFilename()
34 extern FD_form_document * fd_form_document;
36 extern void AutoSave();
37 extern char updatetimer;
38 extern void QuitLyX();
39 int current_layout = 0;
41 // This is very temporary
42 BufferView * current_view;
44 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
46 LyXView::LyXView(int width, int height)
48 create_form_form_main(width, height);
49 fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
50 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
51 lyxfunc = new LyXFunc(this);
67 /// Redraw the main form.
68 void LyXView::redraw() {
69 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
70 fl_redraw_form(_form);
71 minibuffer->Activate();
75 // Callback for update timer
76 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
78 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
79 if (!view->view()->available())
81 if (!view->view()->getScreen() || !updatetimer)
84 view->view()->getScreen()->HideCursor();
85 view->view()->update(-2);
87 /* This update can happen, even when the work area has lost
88 * the focus. So suppress the cursor in that case */
93 // Wrapper for the above
94 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
96 LyXView::UpdateTimerCB(ob, data);
100 // Callback for autosave timer
101 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
103 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
108 // Wrapper for the above
109 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
111 LyXView::AutosaveTimerCB(ob, data);
115 /// Reset autosave timer
116 void LyXView::resetAutosaveTimer()
119 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
123 // Callback for close main form from window manager
124 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
131 // Wrapper for the above
132 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
134 return LyXView::atCloseMainFormCB(form, p);
138 void LyXView::setPosition(int x, int y)
140 fl_set_form_position(_form, x, y);
144 void LyXView::show(int place, int border, char const * title)
146 fl_show_form(_form, place, border, title);
148 InitLyXLookup(fl_display, _form->window);
152 void LyXView::create_form_form_main(int width, int height)
153 /* to make this work as it should, .lyxrc should have been
154 * read first; OR maybe this one should be made dynamic.
156 * We will probably not have lyxrc before the main form is
157 * initialized, because error messages from lyxrc parsing
158 * are presented (and rightly so) in GUI popups. Asger.
161 FD_form_main * fdui = static_cast<FD_form_main *>
162 (fl_calloc(1, sizeof(FD_form_main)));
167 _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
168 fdui->form_main->u_vdata = this;
169 FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
170 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
172 // Parameters for the appearance of the main form
174 int const bw = abs(fl_get_border_width());
180 menus = new Menus(this, air);
186 toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
195 int const ywork = 60 + 2 * air + bw;
196 int const workheight = height - ywork - (25 + 2 * air);
198 ::current_view = bufferview = new BufferView(this, air, ywork,
206 minibuffer = new MiniBuffer(this, air, height - (25 + air),
207 width - (2 * air), 25);
214 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
215 0, 0, 0, 0, "Timer");
216 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
219 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
220 0, 0, 0, 0, "Timer");
221 fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
228 // assign an icon to main form
230 Pixmap lyx_p, lyx_mask;
231 lyx_p = fl_create_from_pixmapdata(fl_root,
232 const_cast<char**>(lyx_xpm),
239 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
242 fl_set_form_minsize(fdui->form_main, 50, 50);
248 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
252 // Set the textclass choice
253 invalidateLayoutChoice();
254 updateLayoutChoice();
255 UpdateDocumentClassChoice();
257 // Start autosave timer
259 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
262 // Install the raw callback for keyboard events
263 fl_register_raw_callback(_form,
265 C_LyXView_KeyPressMask_raw_callback);
266 intl->InitKeyMapper(lyxrc->use_kbmap);
270 void LyXView::invalidateLayoutChoice()
276 void LyXView::updateLayoutChoice()
278 // Update the layout display
279 if (!toolbar->combox) return;
281 // This has a side-effect that the layouts are not showed when no
282 // document is loaded.
283 if (bufferview == 0 || bufferview->buffer() == 0) {
284 toolbar->combox->clear();
285 toolbar->combox->Redraw();
289 // If textclass is different, we need to update the list
290 if (toolbar->combox->empty() ||
291 (last_textclass != int(buffer()->params.textclass))) {
292 toolbar->combox->clear();
293 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
294 for (LyXTextClass::const_iterator cit = tc.begin();
295 cit != tc.end(); ++cit) {
296 if ((*cit).obsoleted_by().empty())
297 toolbar->combox->addline((*cit).name().c_str());
299 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
301 last_textclass = int(buffer()->params.textclass);
304 // we need to do this.
305 toolbar->combox->Redraw();
307 char layout = bufferview->text->cursor.par->GetLayout();
309 if (layout != current_layout){
310 toolbar->combox->select(layout + 1);
311 current_layout = layout;
316 void LyXView::UpdateDocumentClassChoice()
318 // Update the document class display in the document form
319 if (fd_form_document) {
320 fl_clear_choice(fd_form_document->choice_class);
321 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
322 cit != textclasslist.end(); ++cit) {
323 fl_addto_choice(fd_form_document->choice_class,
324 (*cit).description().c_str());
330 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
331 // as FL_KEYBOARD events :-( Matthias 280596
332 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
334 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
335 int retval = 0; // 0 means XForms should have a look at this event
337 // funny. Even though the raw_callback is registered with KeyPressMask,
338 // also KeyRelease-events are passed through:-(
339 // [It seems that XForms puts them in pairs... (JMarc)]
340 if (static_cast<XEvent*>(xev)->type == KeyPress
341 && view->bufferview->getWorkArea()->focus
342 && view->bufferview->getWorkArea()->active)
343 retval = view->getLyXFunc()
344 ->processKeyEvent(static_cast<XEvent*>(xev));
349 // wrapper for the above
350 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
352 return LyXView::KeyPressMask_raw_callback(fl, xev);
356 // Updates the title of the window with the filename of the current document
357 void LyXView::updateWindowTitle()
359 static string last_title = "LyX";
360 string title = "LyX";
362 if (view()->available()) {
363 string cur_title = buffer()->fileName();
364 if (!cur_title.empty()){
365 title += ": " + OnlyFilename(cur_title);
366 if (!buffer()->isLyxClean())
367 title += _(" (Changed)");
368 if (buffer()->isReadonly())
369 title += _(" (read only)");
372 // Don't update title if it's the same as last time
373 if (title != last_title) {
374 fl_set_form_title(_form, title.c_str());