]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
make text private in LyXParagraph some changes to import ascii file
[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-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 #include "LyXView.h"
22 #include "lyx_main.h"
23 #include "lyxlookup.h"
24 #include "toolbar.h"
25 #include "minibuffer.h"
26 #include "lyxfunc.h"
27 #include "lyx.xpm"
28 #include "debug.h"
29 #include "layout_forms.h"
30 #include "intl.h"
31 #include "lyxrc.h"
32 #include "support/filetools.h"        // OnlyFilename()
33 #include "layout.h"
34 #include "lyxtext.h"
35
36 extern FD_form_document * fd_form_document;
37
38 extern void AutoSave();
39 extern char updatetimer;
40 extern void QuitLyX();
41 LyXTextClass::size_type current_layout = 0;
42
43 // This is very temporary
44 BufferView * current_view;
45
46 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
47
48 LyXView::LyXView(int width, int height)
49 {
50         create_form_form_main(width, height);
51         fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
52         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
53         lyxfunc = new LyXFunc(this);
54         intl = new Intl;
55 }
56
57
58 LyXView::~LyXView()
59 {
60         delete menus;
61         delete toolbar;
62         delete bufferview;
63         delete minibuffer;
64         delete lyxfunc;
65         delete intl;
66 }
67
68
69 /// Redraw the main form.
70 void LyXView::redraw() {
71         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
72         fl_redraw_form(_form);
73         minibuffer->Activate();
74 }
75
76
77 // Callback for update timer
78 void LyXView::UpdateTimerCB(FL_OBJECT * ob, long)
79 {
80         LyXView * view = static_cast<LyXView*>(ob->u_vdata);
81         if (!view->view()->available()) 
82                 return;
83         if (!updatetimer)
84                 return;
85
86         view->view()->hideCursor();
87         view->view()->update(-2);
88
89         /* This update can happen, even when the work area has lost
90          * the focus. So suppress the cursor in that case */
91         updatetimer = 0;
92 }
93
94
95 // Wrapper for the above
96 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
97 {
98         LyXView::UpdateTimerCB(ob, data);
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
110 // Wrapper for the above
111 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
112 {
113         LyXView::AutosaveTimerCB(ob, data);
114 }
115
116
117 /// Reset autosave timer
118 void LyXView::resetAutosaveTimer()
119 {
120         if (lyxrc.autosave)
121                 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
122 }
123
124
125 // Callback for close main form from window manager
126 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
127 {
128         QuitLyX();
129         return FL_IGNORE;
130 }
131
132
133 // Wrapper for the above
134 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
135 {
136         return LyXView::atCloseMainFormCB(form, p);
137 }
138
139
140 void LyXView::setPosition(int x, int y)
141 {
142         fl_set_form_position(_form, x, y);
143 }
144
145
146 void LyXView::show(int place, int border, char const * title)
147 {
148         fl_show_form(_form, place, border, title);
149         minibuffer->Init();
150         InitLyXLookup(fl_display, _form->window);
151 }
152
153
154 void LyXView::create_form_form_main(int width, int height)
155         /* to make this work as it should, .lyxrc should have been
156          * read first; OR maybe this one should be made dynamic.
157          * Hmmmm. Lgb. 
158          * We will probably not have lyxrc before the main form is
159          * initialized, because error messages from lyxrc parsing 
160          * are presented (and rightly so) in GUI popups. Asger. 
161          */
162 {
163         FD_form_main * fdui = static_cast<FD_form_main *>
164                 (fl_calloc(1, sizeof(FD_form_main)));
165
166         _form_main = fdui;
167
168         // the main form
169         _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
170         fdui->form_main->u_vdata = this;
171         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
172         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
173
174         // Parameters for the appearance of the main form
175         int const air = 2;
176         int const bw = abs(fl_get_border_width());
177         
178         //
179         // THE MENUBAR
180         //
181
182         menus = new Menus(this, air);
183
184         //
185         // TOOLBAR
186         //
187
188         toolbar = new Toolbar(this, air, 30 + air + bw);
189
190         // Setup the toolbar
191         toolbar->set(true);
192
193         //
194         // WORKAREA
195         //
196
197         int const ywork = 60 + 2 * air + bw;
198         int const workheight = height - ywork - (25 + 2 * air);
199
200         ::current_view = bufferview = new BufferView(this, air, ywork,
201                                                      width - 3 * air,
202                                                      workheight);
203
204         //
205         // MINIBUFFER
206         //
207
208         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
209                                     width - (2 * air), 25);
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 = 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                                           const_cast<char**>(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
249
250 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
251
252 void LyXView::init()
253 {
254         // Set the textclass choice
255         invalidateLayoutChoice();
256         updateLayoutChoice();
257         UpdateDocumentClassChoice();
258         
259         // Start autosave timer
260         if (lyxrc.autosave)
261                 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
262         
263         
264         // Install the raw callback for keyboard events 
265         fl_register_raw_callback(_form,
266                                  KeyPressMask,
267                                  C_LyXView_KeyPressMask_raw_callback);
268         intl->InitKeyMapper(lyxrc.use_kbmap);
269 }
270
271
272 void LyXView::invalidateLayoutChoice()
273 {
274         last_textclass = -1;
275 }
276
277
278 void LyXView::updateLayoutChoice()
279 {
280         // Update the layout display
281         if (!toolbar->combox) return;
282
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();
288                 return; 
289         }
290
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();
295                 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
296                 for (LyXTextClass::const_iterator cit = tc.begin();
297                      cit != tc.end(); ++cit) {
298                         if ((*cit).obsoleted_by().empty())
299                                 toolbar->combox->addline((*cit).name().c_str());
300                         else
301                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
302                 }
303                 last_textclass = int(buffer()->params.textclass);
304                 current_layout = 0;
305         }
306         // we need to do this.
307         toolbar->combox->Redraw();
308
309         LyXTextClass::size_type layout = bufferview->text->cursor.par->GetLayout();
310
311         if (layout != current_layout){
312                 toolbar->combox->select(layout + 1);
313                 current_layout = layout;
314         }
315 }
316
317
318 void LyXView::UpdateDocumentClassChoice()
319 {
320         // Update the document class display in the document form
321         if (fd_form_document) {
322                 fl_clear_choice(fd_form_document->choice_class);
323                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
324                      cit != textclasslist.end(); ++cit) {
325                         fl_addto_choice(fd_form_document->choice_class,
326                                         (*cit).description().c_str());
327                 }
328         }
329 }
330
331
332 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
333 // as FL_KEYBOARD events :-(   Matthias 280596
334 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
335 {
336         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
337         int retval = 0;  // 0 means XForms should have a look at this event
338
339         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
340         static Time last_time_pressed = 0;
341         static Time last_time_released = 0;
342         static unsigned int last_key_pressed = 0;
343         static unsigned int last_key_released = 0;
344         static unsigned int last_state_pressed = 0;
345         static unsigned int last_state_released = 0;
346
347         // funny. Even though the raw_callback is registered with KeyPressMask,
348         // also KeyRelease-events are passed through:-(
349         // [It seems that XForms puts them in pairs... (JMarc)]
350         if (static_cast<XEvent*>(xev)->type == KeyPress
351             && view->bufferview->focus()
352             && view->bufferview->active())
353                 {
354                 last_time_pressed = xke->time;
355                 last_key_pressed = xke->keycode;
356                 last_state_pressed = xke->state;
357                 retval = view->getLyXFunc()
358                         ->processKeyEvent(static_cast<XEvent*>(xev));
359         }
360         else if (static_cast<XEvent*>(xev)->type == KeyRelease
361                  && view->bufferview->focus()
362                  && view->bufferview->active())
363 {
364                 last_time_released = xke->time;
365                 last_key_released = xke->keycode;
366                 last_state_released = xke->state;
367         }
368
369         if (last_key_released == last_key_pressed
370             && last_state_released == last_state_pressed
371             && last_time_released == last_time_pressed) {
372                 // When the diff between last_time_released and
373                 // last_time_pressed is 0, that sinifies an autoreapeat
374                 // at least on my system. It like some feedback from
375                 // others, especially from user running LyX remote.
376                 //lyxerr << "Syncing - purging X events." << endl;
377                 XSync(fl_get_display(), 1);
378                 // This purge make f.ex. scrolling stop imidiatly when
379                 // releaseing the PageDown button. The question is if this
380                 // purging of XEvents can cause any harm...after some testing
381                 // I can see no problems, but I'd like other reports too.
382         }
383         return retval;
384 }
385
386
387 // wrapper for the above
388 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
389 {
390         return LyXView::KeyPressMask_raw_callback(fl, xev);
391 }
392
393
394 // Updates the title of the window with the filename of the current document
395 void LyXView::updateWindowTitle()
396 {
397         static string last_title = "LyX";
398         string title = "LyX";
399
400         if (view()->available()) {
401                 string cur_title = buffer()->fileName();
402                 if (!cur_title.empty()){
403                         title += ": " + OnlyFilename(cur_title);
404                         if (!buffer()->isLyxClean())
405                                 title += _(" (Changed)");
406                         if (buffer()->isReadonly())
407                                 title += _(" (read only)");
408                 }
409         }
410         // Don't update title if it's the same as last time
411         if (title != last_title) {
412                 fl_set_form_title(_form, title.c_str());
413                 last_title = title;
414         }
415 }