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);
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())
85 if (!view->view()->getScreen() || !updatetimer)
88 view->view()->getScreen()->HideCursor();
90 view->view()->update(-2);
92 view->buffer()->update(-2);
94 /* This update can happen, even when the work area has lost
95 * the focus. So suppress the cursor in that case */
99 // Wrapper for the above
100 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data) {
101 LyXView::UpdateTimerCB(ob, data);
106 // Callback for autosave timer
107 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
109 lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
113 // Wrapper for the above
114 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) {
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 FD_form_main * 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.
166 FD_form_main *fdui = (FD_form_main *)
167 fl_calloc(1, sizeof(FD_form_main));
172 _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
173 fdui->form_main->u_vdata = this;
174 obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
175 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
177 // Parameters for the appearance of the main form
179 const int bw = abs(fl_get_border_width());
185 menus = new Menus(this, air);
191 toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
200 const int ywork = 60 + 2*air + bw;
201 const int workheight = height - ywork - (25 + 2*air);
203 ::current_view = bufferview = new BufferView(this, air, ywork,
211 minibuffer = new MiniBuffer(this, air, height-(25+air),
213 ::minibuffer = minibuffer; // to be removed later
221 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
222 0, 0, 0, 0, "Timer");
223 fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
226 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
227 0, 0, 0, 0, "Timer");
228 fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
235 // assign an icon to main form
237 Pixmap lyx_p, lyx_mask;
238 lyx_p = fl_create_from_pixmapdata(fl_root,
239 const_cast<char**>(lyx_xpm),
246 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
249 fl_set_form_minsize(fdui->form_main, 50, 50);
256 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
260 // Set the textclass choice
261 invalidateLayoutChoice();
262 updateLayoutChoice();
263 UpdateDocumentClassChoice();
265 // Start autosave timer
267 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
270 // Install the raw callback for keyboard events
271 fl_register_raw_callback(_form,
273 C_LyXView_KeyPressMask_raw_callback);
274 intl->InitKeyMapper(lyxrc->use_kbmap);
278 void LyXView::invalidateLayoutChoice()
284 void LyXView::updateLayoutChoice()
286 /* update the layout display */
287 if (!toolbar->combox) return;
289 // this has a bi-effect that the layouts are not showed when no
290 // document is loaded.
291 if (bufferview == 0 || bufferview->buffer() == 0) {
292 toolbar->combox->clear();
293 toolbar->combox->Redraw();
297 // If textclass is different, we need to update the list
298 if (toolbar->combox->empty() ||
299 (last_textclass != int(buffer()->params.textclass))) {
300 toolbar->combox->clear();
302 textclasslist.NameOfLayout(buffer()->
303 params.textclass, i) != "@@end@@";
305 LyXLayout const & layout = textclasslist.
306 Style(buffer()->params.textclass, i);
307 if (layout.obsoleted_by().empty())
308 toolbar->combox->addline(layout.name().c_str());
310 toolbar->combox->addline(("@N"+layout.name()).c_str());
312 last_textclass = int(buffer()->params.textclass);
315 // we need to do this.
316 toolbar->combox->Redraw();
319 char layout = bufferview->text->cursor.par->GetLayout();
321 char layout = buffer()->text->cursor.par->GetLayout();
324 if (layout != current_layout){
325 toolbar->combox->select(layout + 1);
326 current_layout = layout;
331 void LyXView::UpdateDocumentClassChoice()
333 // update the document class display in the document form
335 if (fd_form_document) {
336 fl_clear_choice(fd_form_document->choice_class);
337 for (i = 0; textclasslist.DescOfClass (i)!= "@@end@@"; i++) {
338 fl_addto_choice(fd_form_document->choice_class,
339 textclasslist.DescOfClass(i).c_str());
345 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
346 // as FL_KEYBOARD events :-( Matthias 280596
347 int LyXView::KeyPressMask_raw_callback(FL_FORM *fl, void *xev)
349 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
350 int retval = 0; // 0 means XForms should have a look at this event
352 // funny. Even though the raw_callback is registered with KeyPressMask,
353 // also KeyRelease-events are passed through:-(
354 // [It seems that xforms puts them in pairs... (JMarc)]
355 if (static_cast<XEvent*>(xev)->type == KeyPress
356 && view->bufferview->getWorkArea()->focus
357 && view->bufferview->getWorkArea()->active)
358 retval = view->getLyXFunc()
359 ->processKeyEvent(static_cast<XEvent*>(xev));
363 // wrapper for the above
364 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
366 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() {
371 static string last_title = "LyX";
372 string title = "LyX";
374 if (view()->available()) {
375 string cur_title = buffer()->fileName();
376 if (!cur_title.empty()){
377 title += ": " + OnlyFilename(cur_title);
378 if (!buffer()->isLyxClean())
379 title += _(" (Changed)");
380 if (buffer()->isReadonly())
381 title += _(" (read only)");
384 // Don't update title if it's the same as last time
385 if (title != last_title) {
386 fl_set_form_title(_form, title.c_str());