]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
white-space changes, removed definitions.h several enum changes because of this,...
[lyx.git] / src / LyXView.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-1999 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "LyXView.h"
19 #include "lyx_main.h"
20 #include "lyxlookup.h"
21 #include "toolbar.h"
22 #include "minibuffer.h"
23 #include "lyxfunc.h"
24 #include "lyx.xpm"
25 #include "debug.h"
26 #include "layout_forms.h"
27 #include "intl.h"
28 #include "lyxrc.h"
29 #include "lyxscreen.h"
30 #include "support/filetools.h"        // OnlyFilename()
31 #include "layout.h"
32 #include "lyxtext.h"
33
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 */
37
38 MiniBuffer * minibuffer;/* a pointer to the one in LyXView
39                           should be removed as soon as possible */
40
41 extern void AutoSave();
42 extern char updatetimer;
43 extern void QuitLyX();
44 int current_layout = 0;
45
46 // This is very temporary
47 BufferView * current_view;
48
49 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
50
51 LyXView::LyXView(int width, int height)
52 {
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);
57         intl = new Intl;
58 }
59
60
61 LyXView::~LyXView()
62 {
63         delete menus;
64         delete toolbar;
65         delete bufferview;
66         delete minibuffer;
67         delete lyxfunc;
68         delete intl;
69 }
70
71 /// Redraw the main form.
72 void LyXView::redraw() {
73         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
74         fl_redraw_form(_form);
75         minibuffer->Activate();
76 }
77
78
79 // Callback for update timer
80 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
81 {
82         LyXView * view = static_cast<LyXView*>(ob->u_vdata);
83         if (!view->currentView()->available()) 
84                 return;
85         if (!view->currentView()->getScreen() || !updatetimer)
86                 return;
87
88         view->currentView()->getScreen()->HideCursor();
89         view->buffer()->update(-2);
90         /* This update can happen, even when the work area has lost
91          * the focus. So suppress the cursor in that case */
92         updatetimer = 0;
93 }
94
95 // Wrapper for the above
96 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data) {
97         LyXView::UpdateTimerCB(ob, data);
98 }
99
100
101
102 // Callback for autosave timer
103 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
104 {
105         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
106         AutoSave();
107 }
108
109 // Wrapper for the above
110 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data) {
111         LyXView::AutosaveTimerCB(ob, data);
112 }
113
114
115 /// Reset autosave timer
116 void LyXView::resetAutosaveTimer()
117 {
118         if (lyxrc->autosave)
119                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
120 }
121
122
123 // Callback for close main form from window manager
124 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
125 {
126         QuitLyX();
127         return FL_IGNORE;
128 }
129
130
131 // Wrapper for the above
132 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p) {
133         return LyXView::atCloseMainFormCB(form, p);
134 }
135
136
137 void LyXView::setPosition(int x, int y)
138 {
139         fl_set_form_position(_form, x, y);
140 }
141
142
143 void LyXView::show(int place, int border, char const * title)
144 {
145         fl_show_form(_form, place, border, title);
146         minibuffer->Init();
147         InitLyXLookup(fl_display, _form->window);
148 }
149
150
151 FD_form_main * LyXView::create_form_form_main(int width, int height)
152         /* to make this work as it should, .lyxrc should have been
153          * read first; OR maybe this one should be made dynamic.
154          * Hmmmm. Lgb. 
155          * We will probably not have lyxrc before the main form is
156          * initialized, because error messages from lyxrc parsing 
157          * are presented (and rightly so) in GUI popups. Asger. 
158          */
159 {
160         FL_OBJECT * obj;
161         
162         FD_form_main *fdui = (FD_form_main *)
163                 fl_calloc(1, sizeof(FD_form_main));
164
165         _form_main = fdui;
166
167         // the main form
168         _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
169         fdui->form_main->u_vdata = this;
170         obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
171         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
172
173         // Parameters for the appearance of the main form
174         const int air = 2;
175         const int bw = abs(fl_get_border_width());
176         
177         //
178         // THE MENUBAR
179         //
180
181         menus = new Menus(this, air);
182
183         //
184         // TOOLBAR
185         //
186
187         toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
188
189         // Setup the toolbar
190         toolbar->set(true);
191
192         //
193         // WORKAREA
194         //
195
196         const int ywork = 60 + 2*air + bw;
197         const int workheight = height - ywork - (25 + 2*air);
198
199         ::current_view = bufferview = new BufferView(this, air, ywork,
200                                                      width - 3*air,
201                                                      workheight);
202
203         //
204         // MINIBUFFER
205         //
206
207         minibuffer = new MiniBuffer(this, air, height-(25+air), 
208                                           width-(2*air), 25);
209         ::minibuffer = minibuffer; // to be removed later
210
211
212         //
213         // TIMERS
214         //
215         
216         // timer_autosave
217         fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
218                                                   0, 0, 0, 0, "Timer");
219         fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
220         
221         // timer_update
222         fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
223                                                 0, 0, 0, 0, "Timer");
224         fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
225         obj->u_vdata = this;
226
227         //
228         // Misc
229         //
230
231         //  assign an icon to main form
232         unsigned int w, h;
233         Pixmap lyx_p, lyx_mask;
234         lyx_p = fl_create_from_pixmapdata(fl_root,
235                                           lyx_xpm,
236                                           &w,
237                                           &h,
238                                           &lyx_mask,
239                                           0,
240                                           0,
241                                           0); // this leaks
242         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
243
244         // set min size
245         fl_set_form_minsize(fdui->form_main, 50, 50);
246         
247         fl_end_form();
248
249         return fdui;
250 }
251
252 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
253
254 void LyXView::init()
255 {
256         // Set the textclass choice
257         invalidateLayoutChoice();
258         updateLayoutChoice();
259         UpdateDocumentClassChoice();
260         
261         // Start autosave timer
262         if (lyxrc->autosave)
263                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
264         
265         
266         // Install the raw callback for keyboard events 
267         fl_register_raw_callback(_form,
268                                  KeyPressMask,
269                                  C_LyXView_KeyPressMask_raw_callback);
270         intl->InitKeyMapper(lyxrc->use_kbmap);
271 }
272
273
274 void LyXView::invalidateLayoutChoice()
275 {
276         last_textclass = -1;
277 }
278
279
280 void LyXView::updateLayoutChoice()
281 {
282         /* update the layout display */
283         if (!toolbar->combox) return;
284
285         // this has a bi-effect that the layouts are not showed when no
286         // document is loaded.
287         if (bufferview == 0 || bufferview->buffer() == 0) {
288                 toolbar->combox->clear();
289                 toolbar->combox->Redraw();
290                 return; 
291         }
292
293         // If textclass is different, we need to update the list
294         if (toolbar->combox->empty() ||
295             (last_textclass != int(buffer()->params.textclass))) {
296                 toolbar->combox->clear();
297                 for (int i = 0;
298                      textclasslist.NameOfLayout(buffer()->
299                                            params.textclass, i) != "@@end@@";
300                      i++) {
301                         LyXLayout const & layout = textclasslist.
302                                 Style(buffer()->params.textclass, i);
303                         if (layout.obsoleted_by().empty())
304                                 toolbar->combox->addline(layout.name().c_str());
305                         else
306                                 toolbar->combox->addline(("@N"+layout.name()).c_str());
307                 }
308                 last_textclass = int(buffer()->params.textclass);
309                 current_layout = 0;
310         }
311         // we need to do this.
312         toolbar->combox->Redraw();
313         
314         char layout = buffer()->text->cursor.par->GetLayout();
315
316         if (layout != current_layout){
317                 toolbar->combox->select(layout + 1);
318                 current_layout = layout;
319         }
320 }
321
322
323 void LyXView::UpdateDocumentClassChoice()
324 {
325         // update the document class display in the document form
326         int i;
327         if (fd_form_document) {
328                 fl_clear_choice(fd_form_document->choice_class);
329                 for (i = 0; textclasslist.DescOfClass (i)!= "@@end@@"; i++) {
330                         fl_addto_choice(fd_form_document->choice_class,
331                                         textclasslist.DescOfClass(i).c_str());
332                 }
333         }
334 }
335
336
337 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
338 // as FL_KEYBOARD events :-(   Matthias 280596
339 int LyXView::KeyPressMask_raw_callback(FL_FORM *fl, void *xev)
340 {
341         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
342         int retval = 0;  // 0 means XForms should have a look at this event
343
344         // funny. Even though the raw_callback is registered with KeyPressMask,
345         // also KeyRelease-events are passed through:-(
346         // [It seems that xforms puts them in pairs... (JMarc)]
347         if (static_cast<XEvent*>(xev)->type == KeyPress
348             && view->bufferview->getWorkArea()->focus
349             && view->bufferview->getWorkArea()->active)
350                 retval = view->getLyXFunc()
351                         ->processKeyEvent(static_cast<XEvent*>(xev));
352         return retval;
353 }
354
355 // wrapper for the above
356 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
357 {
358         return LyXView::KeyPressMask_raw_callback(fl, xev);
359 }
360
361 // Updates the title of the window with the filename of the current document
362 void LyXView::updateWindowTitle() {
363         static string last_title = "LyX";
364         string title = "LyX";
365
366         if (currentView()->available()) {
367                 string cur_title = buffer()->getFileName();
368                 if (!cur_title.empty()){
369                         title += ": " + OnlyFilename(cur_title);
370                         if (!buffer()->isLyxClean())
371                                 title += _(" (Changed)");
372                         if (buffer()->isReadonly())
373                                 title += _(" (read only)");
374                 }
375         }
376         // Don't update title if it's the same as last time
377         if (title != last_title) {
378                 fl_set_form_title(_form, title.c_str());
379                 last_title = title;
380         }
381 }