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