]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
b9b600c831ceb35686e4e360feb83ee205cdd072
[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() << "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() << "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 = (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->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 */
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() << "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 // Wrapper for the above
131 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *form, void *p) {
132         return LyXView::atCloseMainFormCB(form,p);
133 }
134
135
136 void LyXView::setPosition(int x, int y)
137 {
138         fl_set_form_position(_form, x, y);
139 }
140
141
142 void LyXView::show(int place, int border, char const* title)
143 {
144         fl_show_form(_form, place, border, title);
145         minibuffer->Init();
146         InitLyXLookup(fl_display, _form->window);
147 }
148
149
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.
153          * Hmmmm. Lgb. 
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. 
157          */
158 {
159         FL_OBJECT *obj;
160         
161         FD_form_main *fdui = (FD_form_main *)
162                 fl_calloc(1, sizeof(FD_form_main));
163
164         _form_main = fdui;
165
166         // the main form
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);
171
172         // Parameters for the appearance of the main form
173         const int air = 2;
174         const int bw = abs(fl_get_border_width());
175         
176         //
177         // THE MENUBAR
178         //
179
180         menus = new Menus(this, air);
181
182         //
183         // TOOLBAR
184         //
185
186         toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
187
188         // Setup the toolbar
189         toolbar->set(true);
190
191         //
192         // WORKAREA
193         //
194
195         const int ywork = 60 + 2*air + bw;
196         const int workheight = height - ywork - (25 + 2*air);
197
198         ::current_view = bufferview = new BufferView(this, air, ywork,
199                                                      width - 3*air,
200                                                      workheight);
201
202         //
203         // MINIBUFFER
204         //
205
206         minibuffer = new MiniBuffer(this, air, height-(25+air), 
207                                           width-(2*air), 25);
208         ::minibuffer = minibuffer; // to be removed later
209
210
211         //
212         // TIMERS
213         //
214         
215         // timer_autosave
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);
219         
220         // timer_update
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);
224         obj->u_vdata = (void*) this;
225
226         //
227         // Misc
228         //
229
230         //  assign an icon to main form
231         unsigned int w, h;
232         Pixmap lyx_p, lyx_mask;
233         lyx_p = fl_create_from_pixmapdata(fl_root,
234                                           lyx_xpm,
235                                           &w,
236                                           &h,
237                                           &lyx_mask,
238                                           0,
239                                           0,
240                                           0); // this leaks
241         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
242
243         // set min size
244         fl_set_form_minsize(fdui->form_main, 50, 50);
245         
246         fl_end_form();
247
248         return fdui;
249 }
250
251 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM *fl, void *xev);
252
253 void LyXView::init()
254 {
255         // Set the textclass choice
256         invalidateLayoutChoice();
257         updateLayoutChoice();
258         UpdateDocumentClassChoice();
259         
260         // Start autosave timer
261         if (lyxrc->autosave)
262                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
263         
264         
265         // Install the raw callback for keyboard events 
266         fl_register_raw_callback(_form,
267                                  KeyPressMask,
268                                  C_LyXView_KeyPressMask_raw_callback);
269         intl->InitKeyMapper(lyxrc->use_kbmap);
270 }
271
272
273 void LyXView::invalidateLayoutChoice()
274 {
275         last_textclass = -1;
276 }
277
278
279 void LyXView::updateLayoutChoice()
280 {
281         /* update the layout display */
282         if (!toolbar->combox) return;
283
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();
289                 return; 
290         }
291
292         // If textclass is different, we need to update the list
293         if (toolbar->combox->empty() ||
294             (last_textclass != currentBuffer()->params.textclass)) {
295                 toolbar->combox->clear();
296                 for (int i = 0;
297                      lyxstyle.NameOfLayout(currentBuffer()->
298                                            params.textclass, i) !="@@end@@";
299                      i++) {
300                         LyXLayout *layout = lyxstyle.
301                                 Style(currentBuffer()->params.textclass, i);
302                         if (layout->obsoleted_by.empty())
303                                 toolbar->combox->addline(layout->name.c_str());
304                         else
305                                 toolbar->combox->addline(("@N"+layout->name).c_str());
306                 }
307                 last_textclass = currentBuffer()->params.textclass;
308                 current_layout = 0;
309         }
310         // we need to do this.
311         toolbar->combox->Redraw();
312         
313         char layout = currentBuffer()->text->cursor.par->GetLayout();
314
315         if (layout != current_layout){
316                 toolbar->combox->select(layout + 1);
317                 current_layout = layout;
318         }
319 }
320
321
322 void LyXView::UpdateDocumentClassChoice()
323 {
324         /* update the document class display in the document form */
325         int i;
326         if (fd_form_document) {
327                 fl_clear_choice(fd_form_document->choice_class);
328                 for (i = 0; lyxstyle.DescOfClass (i)!="@@end@@"; i++) {
329                         fl_addto_choice(fd_form_document->choice_class,
330                                         lyxstyle.DescOfClass(i).c_str());
331                 }
332         }
333 }
334
335
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)
339 {
340         LyXView *view = (LyXView*) fl->u_vdata;
341         int retval = 0;  // 0 means XForms should have a look at this event
342
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);
350
351         return retval;
352 }
353
354 // wrapper for the above
355 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM *fl, void *xev)
356 {
357         return LyXView::KeyPressMask_raw_callback(fl, xev);
358 }
359
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";
364
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)");
373                 }
374         }
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());
378                 last_title = title;
379         }
380 }