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;
35 FD_form_main * fd_form_main; /* a pointer to the one in LyXView
36 should be removed as soon as possible */
38 MiniBuffer * minibuffer;/* a pointer to the one in LyXView
39 should be removed as soon as possible */
41 extern void AutoSave();
42 extern char updatetimer;
43 extern void QuitLyX();
44 int current_layout = 0;
46 // This is very temporary
47 BufferView * current_view;
49 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
51 LyXView::LyXView(int width, int height)
53 fd_form_main = create_form_form_main(width, height);
54 fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
55 lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
56 lyxfunc = new LyXFunc(this);
72 /// Redraw the main form.
73 void LyXView::redraw() {
74 lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
75 fl_redraw_form(_form);
76 minibuffer->Activate();
80 // Callback for update timer
81 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
83 LyXView * view = static_cast<LyXView*>(ob->u_vdata);
84 if (!view->view()->available())
86 if (!view->view()->getScreen() || !updatetimer)
89 view->view()->getScreen()->HideCursor();
90 view->view()->update(-2);
92 /* This update can happen, even when the work area has lost
93 * the focus. So suppress the cursor in that case */
98 // Wrapper for the above
99 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
101 LyXView::UpdateTimerCB(ob, data);
105 // Callback for autosave timer
106 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
108 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
113 // Wrapper for the above
114 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
116 LyXView::AutosaveTimerCB(ob, data);
120 /// Reset autosave timer
121 void LyXView::resetAutosaveTimer()
124 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
128 // Callback for close main form from window manager
129 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
136 // Wrapper for the above
137 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
139 return LyXView::atCloseMainFormCB(form, p);
143 void LyXView::setPosition(int x, int y)
145 fl_set_form_position(_form, x, y);
149 void LyXView::show(int place, int border, char const * title)
151 fl_show_form(_form, place, border, title);
153 InitLyXLookup(fl_display, _form->window);
157 FD_form_main * LyXView::create_form_form_main(int width, int height)
158 /* to make this work as it should, .lyxrc should have been
159 * read first; OR maybe this one should be made dynamic.
161 * We will probably not have lyxrc before the main form is
162 * initialized, because error messages from lyxrc parsing
163 * are presented (and rightly so) in GUI popups. Asger.
168 FD_form_main *fdui = (FD_form_main *)
169 fl_calloc(1, sizeof(FD_form_main));
174 _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
175 fdui->form_main->u_vdata = this;
176 obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
177 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
179 // Parameters for the appearance of the main form
181 const int bw = abs(fl_get_border_width());
187 menus = new Menus(this, air);
193 toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
202 const int ywork = 60 + 2*air + bw;
203 const int workheight = height - ywork - (25 + 2*air);
205 ::current_view = bufferview = new BufferView(this, air, ywork,
213 minibuffer = new MiniBuffer(this, air, height-(25+air),
215 ::minibuffer = minibuffer; // to be removed later
222 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
223 0, 0, 0, 0, "Timer");
224 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
227 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
228 0, 0, 0, 0, "Timer");
229 fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
236 // assign an icon to main form
238 Pixmap lyx_p, lyx_mask;
239 lyx_p = fl_create_from_pixmapdata(fl_root,
240 const_cast<char**>(lyx_xpm),
247 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
250 fl_set_form_minsize(fdui->form_main, 50, 50);
258 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
262 // Set the textclass choice
263 invalidateLayoutChoice();
264 updateLayoutChoice();
265 UpdateDocumentClassChoice();
267 // Start autosave timer
269 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
272 // Install the raw callback for keyboard events
273 fl_register_raw_callback(_form,
275 C_LyXView_KeyPressMask_raw_callback);
276 intl->InitKeyMapper(lyxrc->use_kbmap);
280 void LyXView::invalidateLayoutChoice()
286 void LyXView::updateLayoutChoice()
288 /* update the layout display */
289 if (!toolbar->combox) return;
291 // this has a bi-effect that the layouts are not showed when no
292 // document is loaded.
293 if (bufferview == 0 || bufferview->buffer() == 0) {
294 toolbar->combox->clear();
295 toolbar->combox->Redraw();
299 // If textclass is different, we need to update the list
300 if (toolbar->combox->empty() ||
301 (last_textclass != int(buffer()->params.textclass))) {
302 toolbar->combox->clear();
304 textclasslist.NameOfLayout(buffer()->
305 params.textclass, i) != "@@end@@";
307 LyXLayout const & layout = textclasslist.
308 Style(buffer()->params.textclass, i);
309 if (layout.obsoleted_by().empty())
310 toolbar->combox->addline(layout.name().c_str());
312 toolbar->combox->addline(("@N"+layout.name()).c_str());
314 last_textclass = int(buffer()->params.textclass);
317 // we need to do this.
318 toolbar->combox->Redraw();
320 char layout = bufferview->text->cursor.par->GetLayout();
322 if (layout != current_layout){
323 toolbar->combox->select(layout + 1);
324 current_layout = layout;
329 void LyXView::UpdateDocumentClassChoice()
331 // update the document class display in the document form
332 if (fd_form_document) {
333 fl_clear_choice(fd_form_document->choice_class);
335 textclasslist.DescOfClass(i) != "@@end@@"; ++i) {
336 fl_addto_choice(fd_form_document->choice_class,
337 textclasslist.DescOfClass(i).c_str());
343 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
344 // as FL_KEYBOARD events :-( Matthias 280596
345 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
347 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
348 int retval = 0; // 0 means XForms should have a look at this event
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)
356 retval = view->getLyXFunc()
357 ->processKeyEvent(static_cast<XEvent*>(xev));
362 // wrapper for the above
363 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
365 return LyXView::KeyPressMask_raw_callback(fl, xev);
369 // Updates the title of the window with the filename of the current document
370 void LyXView::updateWindowTitle()
372 static string last_title = "LyX";
373 string title = "LyX";
375 if (view()->available()) {
376 string cur_title = buffer()->fileName();
377 if (!cur_title.empty()){
378 title += ": " + OnlyFilename(cur_title);
379 if (!buffer()->isLyxClean())
380 title += _(" (Changed)");
381 if (buffer()->isReadonly())
382 title += _(" (read only)");
385 // Don't update title if it's the same as last time
386 if (title != last_title) {
387 fl_set_form_title(_form, title.c_str());