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.
163 FD_form_main *fdui = (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 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 const int bw = abs(fl_get_border_width());
182 menus = new Menus(this, air);
188 toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
197 const int ywork = 60 + 2*air + bw;
198 const int workheight = height - ywork - (25 + 2*air);
200 ::current_view = bufferview = new BufferView(this, air, ywork,
208 minibuffer = new MiniBuffer(this, air, height-(25+air),
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();
296 textclasslist.NameOfLayout(buffer()->
297 params.textclass, i) != "@@end@@";
299 LyXLayout const & layout = textclasslist.
300 Style(buffer()->params.textclass, i);
301 if (layout.obsoleted_by().empty())
302 toolbar->combox->addline(layout.name().c_str());
304 toolbar->combox->addline(("@N"+layout.name()).c_str());
306 last_textclass = int(buffer()->params.textclass);
309 // we need to do this.
310 toolbar->combox->Redraw();
312 char layout = bufferview->text->cursor.par->GetLayout();
314 if (layout != current_layout){
315 toolbar->combox->select(layout + 1);
316 current_layout = layout;
321 void LyXView::UpdateDocumentClassChoice()
323 // Update the document class display in the document form
324 if (fd_form_document) {
325 fl_clear_choice(fd_form_document->choice_class);
327 textclasslist.DescOfClass(i) != "@@end@@"; ++i) {
328 fl_addto_choice(fd_form_document->choice_class,
329 textclasslist.DescOfClass(i).c_str());
335 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
336 // as FL_KEYBOARD events :-( Matthias 280596
337 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
339 LyXView * view = static_cast<LyXView*>(fl->u_vdata);
340 int retval = 0; // 0 means XForms should have a look at this event
342 // funny. Even though the raw_callback is registered with KeyPressMask,
343 // also KeyRelease-events are passed through:-(
344 // [It seems that XForms puts them in pairs... (JMarc)]
345 if (static_cast<XEvent*>(xev)->type == KeyPress
346 && view->bufferview->getWorkArea()->focus
347 && view->bufferview->getWorkArea()->active)
348 retval = view->getLyXFunc()
349 ->processKeyEvent(static_cast<XEvent*>(xev));
354 // wrapper for the above
355 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
357 return LyXView::KeyPressMask_raw_callback(fl, xev);
361 // Updates the title of the window with the filename of the current document
362 void LyXView::updateWindowTitle()
364 static string last_title = "LyX";
365 string title = "LyX";
367 if (view()->available()) {
368 string cur_title = buffer()->fileName();
369 if (!cur_title.empty()){
370 title += ": " + OnlyFilename(cur_title);
371 if (!buffer()->isLyxClean())
372 title += _(" (Changed)");
373 if (buffer()->isReadonly())
374 title += _(" (read only)");
377 // Don't update title if it's the same as last time
378 if (title != last_title) {
379 fl_set_form_title(_form, title.c_str());