]> git.lyx.org Git - features.git/blob - src/LyXView.C
fixes because of SUN CC warnings, bmtable now compiled with C compilator, countChar...
[features.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
36 extern void AutoSave();
37 extern char updatetimer;
38 extern void QuitLyX();
39 int current_layout = 0;
40
41 // This is very temporary
42 BufferView * current_view;
43
44 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
45
46 LyXView::LyXView(int width, int height)
47 {
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);
52         intl = new Intl;
53 }
54
55
56 LyXView::~LyXView()
57 {
58         delete menus;
59         delete toolbar;
60         delete bufferview;
61         delete minibuffer;
62         delete lyxfunc;
63         delete intl;
64 }
65
66
67 /// Redraw the main form.
68 void LyXView::redraw() {
69         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
70         fl_redraw_form(_form);
71         minibuffer->Activate();
72 }
73
74
75 // Callback for update timer
76 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
77 {
78         LyXView * view = static_cast<LyXView*>(ob->u_vdata);
79         if (!view->view()->available()) 
80                 return;
81         if (!view->view()->getScreen() || !updatetimer)
82                 return;
83
84         view->view()->getScreen()->HideCursor();
85         view->view()->update(-2);
86
87         /* This update can happen, even when the work area has lost
88          * the focus. So suppress the cursor in that case */
89         updatetimer = 0;
90 }
91
92
93 // Wrapper for the above
94 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
95 {
96         LyXView::UpdateTimerCB(ob, data);
97 }
98
99
100 // Callback for autosave timer
101 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
102 {
103         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
104         AutoSave();
105 }
106
107
108 // Wrapper for the above
109 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
110 {
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 {
134         return LyXView::atCloseMainFormCB(form, p);
135 }
136
137
138 void LyXView::setPosition(int x, int y)
139 {
140         fl_set_form_position(_form, x, y);
141 }
142
143
144 void LyXView::show(int place, int border, char const * title)
145 {
146         fl_show_form(_form, place, border, title);
147         minibuffer->Init();
148         InitLyXLookup(fl_display, _form->window);
149 }
150
151
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.
155          * Hmmmm. Lgb. 
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. 
159          */
160 {
161         FD_form_main * fdui = static_cast<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 = this;
169         FL_OBJECT * 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         int const air = 2;
174         int const 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         int const ywork = 60 + 2 * air + bw;
196         int const 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
209         //
210         // TIMERS
211         //
212         
213         // timer_autosave
214         fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
215                                                   0, 0, 0, 0, "Timer");
216         fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
217         
218         // timer_update
219         fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
220                                                 0, 0, 0, 0, "Timer");
221         fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
222         obj->u_vdata = this;
223
224         //
225         // Misc
226         //
227
228         //  assign an icon to main form
229         unsigned int w, h;
230         Pixmap lyx_p, lyx_mask;
231         lyx_p = fl_create_from_pixmapdata(fl_root,
232                                           const_cast<char**>(lyx_xpm),
233                                           &w,
234                                           &h,
235                                           &lyx_mask,
236                                           0,
237                                           0,
238                                           0); // this leaks
239         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
240
241         // set min size
242         fl_set_form_minsize(fdui->form_main, 50, 50);
243         
244         fl_end_form();
245 }
246
247
248 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
249
250 void LyXView::init()
251 {
252         // Set the textclass choice
253         invalidateLayoutChoice();
254         updateLayoutChoice();
255         UpdateDocumentClassChoice();
256         
257         // Start autosave timer
258         if (lyxrc->autosave)
259                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
260         
261         
262         // Install the raw callback for keyboard events 
263         fl_register_raw_callback(_form,
264                                  KeyPressMask,
265                                  C_LyXView_KeyPressMask_raw_callback);
266         intl->InitKeyMapper(lyxrc->use_kbmap);
267 }
268
269
270 void LyXView::invalidateLayoutChoice()
271 {
272         last_textclass = -1;
273 }
274
275
276 void LyXView::updateLayoutChoice()
277 {
278         // Update the layout display
279         if (!toolbar->combox) return;
280
281         // This has a side-effect that the layouts are not showed when no
282         // document is loaded.
283         if (bufferview == 0 || bufferview->buffer() == 0) {
284                 toolbar->combox->clear();
285                 toolbar->combox->Redraw();
286                 return; 
287         }
288
289         // If textclass is different, we need to update the list
290         if (toolbar->combox->empty() ||
291             (last_textclass != int(buffer()->params.textclass))) {
292                 toolbar->combox->clear();
293                 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
294                 for (LyXTextClass::const_iterator cit = tc.begin();
295                      cit != tc.end(); ++cit) {
296                         if ((*cit).obsoleted_by().empty())
297                                 toolbar->combox->addline((*cit).name().c_str());
298                         else
299                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
300                 }
301                 last_textclass = int(buffer()->params.textclass);
302                 current_layout = 0;
303         }
304         // we need to do this.
305         toolbar->combox->Redraw();
306
307         char layout = bufferview->text->cursor.par->GetLayout();
308
309         if (layout != current_layout){
310                 toolbar->combox->select(layout + 1);
311                 current_layout = layout;
312         }
313 }
314
315
316 void LyXView::UpdateDocumentClassChoice()
317 {
318         // Update the document class display in the document form
319         if (fd_form_document) {
320                 fl_clear_choice(fd_form_document->choice_class);
321                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
322                      cit != textclasslist.end(); ++cit) {
323                         fl_addto_choice(fd_form_document->choice_class,
324                                         (*cit).description().c_str());
325                 }
326         }
327 }
328
329
330 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
331 // as FL_KEYBOARD events :-(   Matthias 280596
332 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
333 {
334         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
335         int retval = 0;  // 0 means XForms should have a look at this event
336
337         // funny. Even though the raw_callback is registered with KeyPressMask,
338         // also KeyRelease-events are passed through:-(
339         // [It seems that XForms puts them in pairs... (JMarc)]
340         if (static_cast<XEvent*>(xev)->type == KeyPress
341             && view->bufferview->getWorkArea()->focus
342             && view->bufferview->getWorkArea()->active)
343                 retval = view->getLyXFunc()
344                         ->processKeyEvent(static_cast<XEvent*>(xev));
345         return retval;
346 }
347
348
349 // wrapper for the above
350 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
351 {
352         return LyXView::KeyPressMask_raw_callback(fl, xev);
353 }
354
355
356 // Updates the title of the window with the filename of the current document
357 void LyXView::updateWindowTitle()
358 {
359         static string last_title = "LyX";
360         string title = "LyX";
361
362         if (view()->available()) {
363                 string cur_title = buffer()->fileName();
364                 if (!cur_title.empty()){
365                         title += ": " + OnlyFilename(cur_title);
366                         if (!buffer()->isLyxClean())
367                                 title += _(" (Changed)");
368                         if (buffer()->isReadonly())
369                                 title += _(" (read only)");
370                 }
371         }
372         // Don't update title if it's the same as last time
373         if (title != last_title) {
374                 fl_set_form_title(_form, title.c_str());
375                 last_title = title;
376         }
377 }