]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
d9cdc70bbf09ec2e91b00b036975d0325628b2b9
[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 #include "buffer.h"
36 #include "menus.h"
37 #include "frontends/Dialogs.h"
38 #include "lyx_gui_misc.h"       // [update,Close]AllBufferRelatedDialogs
39 #include "bufferview_funcs.h" // CurrentState()
40
41 using std::endl;
42
43 extern FD_form_document * fd_form_document;
44
45 extern void AutoSave(BufferView *);
46 extern void QuitLyX();
47 LyXTextClass::size_type current_layout = 0;
48
49 // This is very temporary
50 BufferView * current_view;
51
52 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
53
54 #ifdef SIGC_CXX_NAMESPACES
55 using SigC::Connection;
56 using SigC::slot;
57 #endif
58
59 LyXView::LyXView(int width, int height)
60 {
61         create_form_form_main(width, height);
62         fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
63         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
64         lyxfunc = new LyXFunc(this);
65         intl = new Intl;
66
67         // Make sure the buttons are disabled if needed.
68         toolbar->update();
69
70         dialogs_ = new Dialogs(this);
71         // temporary until all dialogs moved into Dialogs.
72         dialogs_->updateBufferDependent
73                 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
74         dialogs_->hideBufferDependent
75                 .connect(slot(&CloseAllBufferRelatedDialogs));
76 }
77
78
79 LyXView::~LyXView()
80 {
81         delete menus;
82         delete toolbar;
83         delete bufferview;
84         delete minibuffer;
85         delete lyxfunc;
86         delete intl;
87         delete dialogs_;
88 }
89
90
91 /// Redraw the main form.
92 void LyXView::redraw() {
93         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
94         fl_redraw_form(form_);
95         minibuffer->Activate();
96 }
97
98
99 /// returns the buffer currently shown in the main form.
100 Buffer * LyXView::buffer() const
101 {
102         return bufferview->buffer();
103 }
104
105
106 BufferView * LyXView::view() const
107 {
108         return bufferview;
109 }
110
111
112 FL_FORM * LyXView::getForm() const
113 {
114         return form_;
115 }
116
117
118 Toolbar * LyXView::getToolbar() const
119 {
120         return toolbar;
121 }
122
123
124 LyXFunc * LyXView::getLyXFunc() const
125 {
126         return lyxfunc;
127 }
128
129
130 MiniBuffer * LyXView::getMiniBuffer() const
131 {
132         return minibuffer;
133 }
134
135
136 Menus * LyXView::getMenus() const
137 {
138         return menus;
139 }
140
141
142 Intl * LyXView::getIntl() const
143 {
144         return intl;
145 }
146
147
148 // Callback for autosave timer
149 void LyXView::AutoSave()
150 {
151         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
152         if (view()->available())
153                 ::AutoSave(view());
154 }
155
156
157 /// Reset autosave timer
158 void LyXView::resetAutosaveTimer()
159 {
160         if (lyxrc.autosave)
161                 autosave_timeout.restart();
162 }
163
164
165 // Callback for close main form from window manager
166 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
167 {
168         QuitLyX();
169         return FL_IGNORE;
170 }
171
172
173 // Wrapper for the above
174 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
175 {
176         return LyXView::atCloseMainFormCB(form, p);
177 }
178
179
180 void LyXView::setPosition(int x, int y)
181 {
182         fl_set_form_position(form_, x, y);
183 }
184
185
186 void LyXView::show(int place, int border, char const * title)
187 {
188         fl_show_form(form_, place, border, title);
189         minibuffer->Init();
190         InitLyXLookup(fl_display, form_->window);
191 }
192
193
194 void LyXView::create_form_form_main(int width, int height)
195         /* to make this work as it should, .lyxrc should have been
196          * read first; OR maybe this one should be made dynamic.
197          * Hmmmm. Lgb. 
198          * We will probably not have lyxrc before the main form is
199          * initialized, because error messages from lyxrc parsing 
200          * are presented (and rightly so) in GUI popups. Asger. 
201          */
202 {
203         // the main form
204         form_ = fl_bgn_form(FL_NO_BOX, width, height);
205         form_->u_vdata = this;
206         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
207         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
208
209         // Parameters for the appearance of the main form
210         int const air = 2;
211         int const bw = abs(fl_get_border_width());
212         
213         //
214         // THE MENUBAR
215         //
216
217         menus = new Menus(this, air);
218
219         //
220         // TOOLBAR
221         //
222
223         toolbar = new Toolbar(this, air, 30 + air + bw);
224
225         // Setup the toolbar
226         toolbar->set(true);
227
228         //
229         // WORKAREA
230         //
231
232         int const ywork = 60 + 2 * air + bw;
233         int const workheight = height - ywork - (25 + 2 * air);
234
235         ::current_view = bufferview = new BufferView(this, air, ywork,
236                                                      width - 3 * air,
237                                                      workheight);
238
239         //
240         // MINIBUFFER
241         //
242
243         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
244                                     width - (2 * air), 25);
245
246         //
247         // TIMERS
248         //
249
250         autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
251         
252         //
253         // Misc
254         //
255
256         //  assign an icon to main form
257         unsigned int w, h;
258         Pixmap lyx_p, lyx_mask;
259         lyx_p = fl_create_from_pixmapdata(fl_root,
260                                           const_cast<char**>(lyx_xpm),
261                                           &w,
262                                           &h,
263                                           &lyx_mask,
264                                           0,
265                                           0,
266                                           0); // this leaks
267         fl_set_form_icon(form_, lyx_p, lyx_mask);
268
269         // set min size
270         fl_set_form_minsize(form_, 50, 50);
271         
272         fl_end_form();
273 }
274
275
276 extern "C"
277 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
278
279 void LyXView::init()
280 {
281         // Set the textclass choice
282         invalidateLayoutChoice();
283         updateLayoutChoice();
284         UpdateDocumentClassChoice();
285         
286         // Start autosave timer
287         if (lyxrc.autosave) {
288                 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
289                 autosave_timeout.start();
290         }
291         
292         // Install the raw callback for keyboard events 
293         fl_register_raw_callback(form_,
294                                  KeyPressMask,
295                                  C_LyXView_KeyPressMask_raw_callback);
296         intl->InitKeyMapper(lyxrc.use_kbmap);
297 }
298
299
300 void LyXView::invalidateLayoutChoice()
301 {
302         last_textclass = -1;
303 }
304
305
306 void LyXView::updateLayoutChoice()
307 {
308         // Update the layout display
309         if (!toolbar->combox) return;
310
311         // This has a side-effect that the layouts are not showed when no
312         // document is loaded.
313         if (bufferview == 0 || bufferview->buffer() == 0) {
314                 toolbar->combox->clear();
315                 toolbar->combox->Redraw();
316                 return; 
317         }
318
319         // If textclass is different, we need to update the list
320         if (toolbar->combox->empty() ||
321             (last_textclass != int(buffer()->params.textclass))) {
322                 toolbar->combox->clear();
323                 LyXTextClass const & tc =
324                         textclasslist.TextClass(buffer()->params.textclass);
325                 for (LyXTextClass::const_iterator cit = tc.begin();
326                      cit != tc.end(); ++cit) {
327                         if ((*cit).obsoleted_by().empty())
328                                 toolbar->combox->addline((*cit).name().c_str());
329                         else
330                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
331                 }
332                 last_textclass = int(buffer()->params.textclass);
333                 current_layout = 0;
334         }
335         // we need to do this.
336         toolbar->combox->Redraw();
337
338         LyXTextClass::size_type layout =
339                 bufferview->text->cursor.par()->GetLayout();
340
341         if (layout != current_layout){
342                 toolbar->combox->select(layout + 1);
343                 current_layout = layout;
344         }
345 }
346
347
348 void LyXView::UpdateDocumentClassChoice()
349 {
350         // Update the document class display in the document form
351         if (fd_form_document) {
352                 fl_clear_choice(fd_form_document->choice_class);
353                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
354                      cit != textclasslist.end(); ++cit) {
355                         fl_addto_choice(fd_form_document->choice_class,
356                                         (*cit).description().c_str());
357                 }
358         }
359 }
360
361
362 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
363 // as FL_KEYBOARD events :-(   Matthias 280596
364 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
365 {
366         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
367         int retval = 0;  // 0 means XForms should have a look at this event
368
369         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
370         static Time last_time_pressed = 0;
371         static Time last_time_released = 0;
372         static unsigned int last_key_pressed = 0;
373         static unsigned int last_key_released = 0;
374         static unsigned int last_state_pressed = 0;
375         static unsigned int last_state_released = 0;
376
377         // funny. Even though the raw_callback is registered with KeyPressMask,
378         // also KeyRelease-events are passed through:-(
379         // [It seems that XForms puts them in pairs... (JMarc)]
380         if (static_cast<XEvent*>(xev)->type == KeyPress
381             && view->bufferview->focus()
382             && view->bufferview->active())
383                 {
384                 last_time_pressed = xke->time;
385                 last_key_pressed = xke->keycode;
386                 last_state_pressed = xke->state;
387                 retval = view->getLyXFunc()
388                         ->processKeyEvent(static_cast<XEvent*>(xev));
389         }
390         else if (static_cast<XEvent*>(xev)->type == KeyRelease
391                  && view->bufferview->focus()
392                  && view->bufferview->active())
393 {
394                 last_time_released = xke->time;
395                 last_key_released = xke->keycode;
396                 last_state_released = xke->state;
397         }
398
399         if (last_key_released == last_key_pressed
400             && last_state_released == last_state_pressed
401             && last_time_released == last_time_pressed) {
402                 // When the diff between last_time_released and
403                 // last_time_pressed is 0, that sinifies an autoreapeat
404                 // at least on my system. It like some feedback from
405                 // others, especially from user running LyX remote.
406                 //lyxerr << "Syncing - purging X events." << endl;
407                 XSync(fl_get_display(), 1);
408                 // This purge make f.ex. scrolling stop imidiatly when
409                 // releaseing the PageDown button. The question is if this
410                 // purging of XEvents can cause any harm...after some testing
411                 // I can see no problems, but I'd like other reports too.
412         }
413         return retval;
414 }
415
416
417 // wrapper for the above
418 extern "C"
419 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
420 {
421         return LyXView::KeyPressMask_raw_callback(fl, xev);
422 }
423
424
425 // Updates the title of the window with the filename of the current document
426 void LyXView::updateWindowTitle()
427 {
428         static string last_title = "LyX";
429         string title = "LyX";
430
431         if (view()->available()) {
432                 string cur_title = buffer()->fileName();
433                 if (!cur_title.empty()){
434                         title += ": " + OnlyFilename(cur_title);
435                         if (!buffer()->isLyxClean())
436                                 title += _(" (Changed)");
437                         if (buffer()->isReadonly())
438                                 title += _(" (read only)");
439                 }
440         }
441         // Don't update title if it's the same as last time
442         if (title != last_title) {
443                 fl_set_form_title(form_, title.c_str());
444                 last_title = title;
445         }
446 }
447
448
449 void LyXView::showState()
450 {
451         getMiniBuffer()->Set(CurrentState(view()));
452         getToolbar()->update();
453 }