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() << "Initializing LyXFunc" << endl;
56 lyxfunc = new LyXFunc(this);
71 /// Redraw the main form.
72 void LyXView::redraw() {
73 lyxerr.debug() << "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 = (LyXView*) ob->u_vdata;
83 if (!view->currentView()->available())
85 if (!view->currentView()->getScreen() || !updatetimer)
88 view->currentView()->getScreen()->HideCursor();
89 view->currentBuffer()->update(-2);
90 /* This update can happen, even when the work area has lost
91 * 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) {
97 LyXView::UpdateTimerCB(ob,data);
102 // Callback for autosave timer
103 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
105 lyxerr.debug() << "Running AutoSave()" << endl;
109 // Wrapper for the above
110 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 *)
130 // Wrapper for the above
131 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *form, void *p) {
132 return LyXView::atCloseMainFormCB(form,p);
136 void LyXView::setPosition(int x, int y)
138 fl_set_form_position(_form, x, y);
142 void LyXView::show(int place, int border, char const* title)
144 fl_show_form(_form, place, border, title);
146 InitLyXLookup(fl_display, _form->window);
150 FD_form_main *LyXView::create_form_form_main(int width, int height)
151 /* to make this work as it should, .lyxrc should have been
152 * read first; OR maybe this one should be made dynamic.
154 * We will probably not have lyxrc before the main form is
155 * initialized, because error messages from lyxrc parsing
156 * are presented (and rightly so) in GUI popups. Asger.
161 FD_form_main *fdui = (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 = (void*) this;
169 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 const int bw = abs(fl_get_border_width());
180 menus = new Menus(this, air);
186 toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
195 const int ywork = 60 + 2*air + bw;
196 const int workheight = height - ywork - (25 + 2*air);
198 ::current_view = bufferview = new BufferView(this, air, ywork,
206 minibuffer = new MiniBuffer(this, air, height-(25+air),
208 ::minibuffer = minibuffer; // to be removed later
216 fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
218 fl_set_object_callback(obj,C_LyXView_AutosaveTimerCB,0);
221 fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
223 fl_set_object_callback(obj,C_LyXView_UpdateTimerCB,0);
224 obj->u_vdata = (void*) this;
230 // assign an icon to main form
232 Pixmap lyx_p, lyx_mask;
233 lyx_p = fl_create_from_pixmapdata(fl_root,
241 fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
244 fl_set_form_minsize(fdui->form_main, 50, 50);
251 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM *fl, void *xev);
255 // Set the textclass choice
256 invalidateLayoutChoice();
257 updateLayoutChoice();
258 UpdateDocumentClassChoice();
260 // Start autosave timer
262 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
265 // Install the raw callback for keyboard events
266 fl_register_raw_callback(_form,
268 C_LyXView_KeyPressMask_raw_callback);
269 intl->InitKeyMapper(lyxrc->use_kbmap);
273 void LyXView::invalidateLayoutChoice()
279 void LyXView::updateLayoutChoice()
281 /* update the layout display */
282 if (!toolbar->combox) return;
284 // this has a bi-effect that the layouts are not showed when no
285 // document is loaded.
286 if (bufferview==0 || bufferview->currentBuffer()==0) {
287 toolbar->combox->clear();
288 toolbar->combox->Redraw();
292 // If textclass is different, we need to update the list
293 if (toolbar->combox->empty() ||
294 (last_textclass != int(currentBuffer()->params.textclass))) {
295 toolbar->combox->clear();
297 textclasslist.NameOfLayout(currentBuffer()->
298 params.textclass, i) !="@@end@@";
300 LyXLayout const & layout = textclasslist.
301 Style(currentBuffer()->params.textclass, i);
302 if (layout.obsoleted_by().empty())
303 toolbar->combox->addline(layout.name().c_str());
305 toolbar->combox->addline(("@N"+layout.name()).c_str());
307 last_textclass = int(currentBuffer()->params.textclass);
310 // we need to do this.
311 toolbar->combox->Redraw();
313 char layout = currentBuffer()->text->cursor.par->GetLayout();
315 if (layout != current_layout){
316 toolbar->combox->select(layout + 1);
317 current_layout = layout;
322 void LyXView::UpdateDocumentClassChoice()
324 /* update the document class display in the document form */
326 if (fd_form_document) {
327 fl_clear_choice(fd_form_document->choice_class);
328 for (i = 0; textclasslist.DescOfClass (i)!="@@end@@"; i++) {
329 fl_addto_choice(fd_form_document->choice_class,
330 textclasslist.DescOfClass(i).c_str());
336 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
337 // as FL_KEYBOARD events :-( Matthias 280596
338 int LyXView::KeyPressMask_raw_callback(FL_FORM *fl, void *xev)
340 LyXView *view = (LyXView*) fl->u_vdata;
341 int retval = 0; // 0 means XForms should have a look at this event
343 // funny. Even though the raw_callback is registered with KeyPressMask,
344 // also KeyRelease-events are passed through:-(
345 // [It seems that xforms puts them in pairs... (JMarc)]
346 if (((XEvent*)xev)->type == KeyPress
347 && view->bufferview->getWorkArea()->focus
348 && view->bufferview->getWorkArea()->active)
349 retval = view->getLyXFunc()->processKeyEvent((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);
360 // Updates the title of the window with the filename of the current document
361 void LyXView::updateWindowTitle() {
362 static string last_title="LyX";
363 string title = "LyX";
365 if (currentView()->available()) {
366 string cur_title = currentBuffer()->getFileName();
367 if (!cur_title.empty()){
368 title += ": " + OnlyFilename(cur_title);
369 if (!currentBuffer()->isLyxClean())
370 title += _(" (Changed)");
371 if (currentBuffer()->isReadonly())
372 title += _(" (read only)");
375 // Don't update title if it's the same as last time
376 if (title != last_title) {
377 fl_set_form_title(_form, title.c_str());