]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XFormsView.C
* filetools.[Ch]: Make functions that start with a capital
[lyx.git] / src / frontends / xforms / XFormsView.C
1 /**
2  * \file XFormsView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "XFormsView.h"
14
15 #include "XFormsMenubar.h"
16 #include "XMiniBuffer.h"
17
18 #include "BufferView.h"
19 #include "debug.h"
20 #include "lyx_main.h"
21 #include "session.h"
22 #include "lyxfunc.h"
23 #include "MenuBackend.h"
24 #include "funcrequest.h"
25
26 #include "frontends/Dialogs.h"
27 #include "frontends/Toolbars.h"
28
29 #include "support/filetools.h"        // OnlyFilename()
30 #include "support/convert.h"
31
32 #include <boost/bind.hpp>
33
34 using boost::shared_ptr;
35
36 using std::abs;
37 using std::endl;
38 using std::string;
39
40 //extern void AutoSave(BufferView *);
41 extern void QuitLyX(bool);
42
43 namespace lyx {
44
45 using support::libFileSearch;
46
47 namespace frontend {
48
49 extern "C" {
50
51 static
52 int C_XFormsView_atCloseMainFormCB(FL_FORM * /*form*/, void * arg)
53 {
54         // For some reason u_vdata does not contain the pointer
55         // to the XFormsView that we need. We get it through arg instead.
56         //XFormsView * view = static_cast<XFormsView*>(form->u_vdata);
57         XFormsView * view = static_cast<XFormsView*>(arg);
58         return view->atCloseMainFormCB();
59 }
60
61 }
62
63
64 void print_metrics(std::ostream & os, std::string const & name, Box const & box)
65 {
66         os << name << " metrics:"
67            << "\tx = " << box.xorigin()
68            << "\ty = " << box.yorigin()
69            << "\tw = " << box.width()
70            << "\th = " << box.height() << '\n';
71 }
72
73
74 XFormsView::XFormsView(int width, int height)
75         : LyXView(),
76           window_(Box(width, height)),
77           icon_pixmap_(0), icon_mask_(0)
78 {
79         int const air = 2;
80
81         // Logical layout of the boxes making up the LyX window.
82         shared_ptr<Box> top = window_.children().push_back(Box(0,0));
83         shared_ptr<Box> middle = window_.children().push_back(Box(0,0));
84         middle->set(Box::Horizontal);
85         shared_ptr<Box> bottom = window_.children().push_back(Box(0,0));
86
87         shared_ptr<Box> left = middle->children().push_back(Box(air,0));
88         shared_ptr<Box> center = middle->children().push_back(Box(0,0));
89         center->set(Box::Expand);
90         shared_ptr<Box> right = middle->children().push_back(Box(air,0));
91
92         // Define accessors to the various boxes.
93         box_map_[Top]    = top;
94         box_map_[Bottom] = bottom;
95         box_map_[Left]   = left;
96         box_map_[Center] = center;
97         box_map_[Right]  = right;
98
99         // Define the XForms components making up the window.
100         // Each uses the layout engine defined above to control its
101         // dimensions.
102         form_ = fl_bgn_form(FL_NO_BOX, width, height);
103         form_->u_vdata = this;
104         fl_set_form_atclose(form_, C_XFormsView_atCloseMainFormCB, this);
105
106         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
107         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
108
109         menubar_.reset(new XFormsMenubar(this, menubackend));
110         getToolbars().init();
111         bufferview_.reset(new BufferView(this, width, height));
112         minibuffer_.reset(new XMiniBuffer(*this, *controlcommand_));
113
114         //  Assign an icon to the main form.
115         string const iconname = libFileSearch("images", "lyx", "xpm");
116         if (!iconname.empty()) {
117                 unsigned int w, h;
118                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
119                                            iconname.c_str(),
120                                            &w,
121                                            &h,
122                                            &icon_mask_,
123                                            0,
124                                            0,
125                                            0);
126                 fl_set_form_icon(form_, icon_pixmap_, icon_mask_);
127         }
128
129         fl_end_form();
130
131         // Update the layout so that all widgets fit.
132         window_.show();
133         updateMetrics(true);
134
135         view_state_con =
136                 view_state_changed.connect(boost::bind(&XFormsView::show_view_state, this));
137         focus_con =
138                 focus_command_buffer.connect(boost::bind(&XMiniBuffer::focus, minibuffer_.get()));
139
140         // Make sure the buttons are disabled if needed.
141         updateToolbars();
142         redraw_con =
143                 getDialogs().redrawGUI().connect(boost::bind(&XFormsView::redraw, this));
144 }
145
146
147 XFormsView::~XFormsView()
148 {
149         if (icon_pixmap_)
150                 XFreePixmap(fl_get_display(), icon_pixmap_);
151
152         minibuffer_->freeze();
153         fl_hide_form(form_);
154         fl_free_form(form_);
155 }
156
157
158 shared_ptr<Box> XFormsView::getBox(Position pos) const
159 {
160         BoxMap::const_iterator it = box_map_.find(pos);
161         BOOST_ASSERT(it != box_map_.end());
162         return it->second;
163 }
164
165
166 /// Redraw the main form.
167 void XFormsView::redraw()
168 {
169         lyxerr[Debug::INFO] << "XFormsView::redraw()" << endl;
170         fl_redraw_form(getForm());
171         minibuffer_->redraw();
172 }
173
174
175 FL_FORM * XFormsView::getForm() const
176 {
177         return form_;
178 }
179
180
181 // Callback for close main form from window manager
182 int XFormsView::atCloseMainFormCB()
183 {
184         // save windows size
185         LyX::ref().session().saveSessionInfo("WindowWidth", convert<string>(form_->w));
186         LyX::ref().session().saveSessionInfo("WindowHeight", convert<string>(form_->h));
187         // trigger LFUN_QUIT instead of quit directly
188         // since LFUN_QUIT may have more cleanup stuff
189         //
190         getLyXFunc().dispatch(FuncRequest(LFUN_QUIT));
191         return FL_IGNORE;
192 }
193
194
195 void XFormsView::show(int x, int y, string const & title)
196 {
197         FL_FORM * form = getForm();
198
199         fl_set_form_minsize(form, form->w, form->h);
200
201         int placement = FL_PLACE_CENTER | FL_FREE_SIZE;
202
203         // Did we get a valid geometry position ?
204         if (x >= 0 && y >= 0) {
205                 fl_set_form_position(form, x, y);
206                 placement = FL_PLACE_POSITION;
207         }
208
209         fl_show_form(form, placement, FL_FULLBORDER, title.c_str());
210
211         show_view_state();
212 }
213
214
215 void XFormsView::updateMetrics(bool resize_form)
216 {
217         FL_FORM * form = getForm();
218
219         // We don't want the window to be downsized.
220         if (!resize_form)
221                 window_.setMinimumDimensions(form->w, form->h);
222
223         window_.updateMetrics();
224
225         fl_freeze_form(form);
226
227         if (resize_form)
228                 fl_set_form_size(form, window_.width(), window_.height());
229
230         // Emit a signal so that all daughter widgets are layed-out
231         // correctly.
232         metricsUpdated();
233         fl_unfreeze_form(form);
234 }
235
236
237 bool XFormsView::hasFocus() const
238 {
239         // No real implementation needed for now
240         return true;
241 }
242
243
244 void XFormsView::setWindowTitle(string const & title, string const & icon_title)
245 {
246         fl_set_form_title(getForm(), title.c_str());
247         fl_winicontitle(form_->window, icon_title.c_str());
248 }
249
250
251 void XFormsView::message(string const & str)
252 {
253         minibuffer_->message(str);
254 }
255
256
257 void XFormsView::clearMessage()
258 {
259         message(getLyXFunc().viewStatusMessage());
260 }
261
262
263 void XFormsView::show_view_state()
264 {
265         message(getLyXFunc().viewStatusMessage());
266 }
267
268
269 void XFormsView::busy(bool yes) const
270 {
271         if (yes) {
272                 view()->hideCursor();
273
274                 static Cursor cursor;
275                 static bool cursor_undefined = true;
276
277                 if (cursor_undefined) {
278                         cursor = XCreateFontCursor(fl_get_display(), XC_watch);
279                         XFlush(fl_get_display());
280                         cursor_undefined = false;
281                 }
282
283                 /// set the cursor to the watch for all forms and the canvas
284                 XDefineCursor(fl_get_display(), getForm()->window, cursor);
285
286                 XFlush(fl_get_display());
287
288                 /// we only need to deactivate to prevent resetting the cursor
289                 /// to I-beam over the workarea
290                 fl_deactivate_all_forms();
291         } else {
292                 /// reset the cursor from the watch for all forms and the canvas
293
294                 XUndefineCursor(fl_get_display(), getForm()->window);
295
296                 XFlush(fl_get_display());
297                 fl_activate_all_forms();
298         }
299 }
300
301 } // namespace frontend
302 } // namespace lyx