]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XFormsView.C
Fix leaking pixmap icon.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "XFormsView.h"
18 #include FORMS_H_LOCATION
19 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
20 #include "frontends/xforms/lyxlookup.h"
21 #endif
22 #include "frontends/xforms/XMiniBuffer.h"
23 #include "debug.h"
24 #include "intl.h"
25 #include "lyxrc.h"
26 #include "support/filetools.h"        // OnlyFilename()
27 #include "frontends/Toolbar.h"
28 #include "frontends/Menubar.h"
29 #include "frontends/Timeout.h"
30 #include "frontends/Dialogs.h"
31 #include "MenuBackend.h"
32 #include "ToolbarDefaults.h"
33 #include "lyxfunc.h"
34 #include "bufferview_funcs.h"
35 #include "BufferView.h"
36
37 #include <boost/bind.hpp>
38 #include <boost/signals/connection.hpp>
39 using std::abs;
40 using std::endl;
41
42 //extern void AutoSave(BufferView *);
43 extern void QuitLyX();
44
45 // This is very temporary
46 BufferView * current_view;
47
48 extern "C" {
49
50 static
51 int C_XFormsView_atCloseMainFormCB(FL_FORM * form, void * p)
52 {
53         return XFormsView::atCloseMainFormCB(form, p);
54 }
55
56 }
57
58
59 XFormsView::XFormsView(int width, int height)
60         : LyXView(),
61           icon_pixmap_(0), icon_mask_(0)
62 {
63         create_form_form_main(width, height);
64         fl_set_form_atclose(getForm(), C_XFormsView_atCloseMainFormCB, 0);
65
66         view_state_con = view_state_changed.connect(boost::bind(&XFormsView::show_view_state, this));
67         focus_con = focus_command_buffer.connect(boost::bind(&XMiniBuffer::focus, minibuffer_.get()));
68
69         // Make sure the buttons are disabled if needed.
70         updateToolbar();
71         redraw_con = getDialogs().redrawGUI().connect(boost::bind(&XFormsView::redraw, this));
72 }
73
74
75 XFormsView::~XFormsView()
76 {
77         if (icon_pixmap_)
78                 XFreePixmap(fl_get_display(), icon_pixmap_);
79
80         minibuffer_->freeze();
81         fl_hide_form(form_);
82         fl_free_form(form_);
83 }
84
85
86 /// Redraw the main form.
87 void XFormsView::redraw()
88 {
89         lyxerr[Debug::INFO] << "XFormsView::redraw()" << endl;
90         fl_redraw_form(getForm());
91         minibuffer_->redraw();
92 }
93
94
95 FL_FORM * XFormsView::getForm() const
96 {
97         return form_;
98 }
99
100
101 // Callback for close main form from window manager
102 int XFormsView::atCloseMainFormCB(FL_FORM *, void *)
103 {
104         QuitLyX();
105         return FL_IGNORE;
106 }
107
108
109 void XFormsView::show(int x, int y, string const & title)
110 {
111         FL_FORM * form = getForm();
112
113         fl_set_form_minsize(form, form->w, form->h);
114
115         int placement = FL_PLACE_CENTER | FL_FREE_SIZE;
116
117         // Did we get a valid geometry position ?
118         if (x >= 0 && y >= 0) {
119                 fl_set_form_position(form, x, y);
120                 placement = FL_PLACE_POSITION;
121         }
122
123         fl_show_form(form, placement, FL_FULLBORDER, title.c_str());
124
125         show_view_state();
126 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
127         InitLyXLookup(fl_get_display(), form_->window);
128 #endif
129 }
130
131
132 void XFormsView::create_form_form_main(int width, int height)
133         /* to make this work as it should, .lyxrc should have been
134          * read first; OR maybe this one should be made dynamic.
135          * Hmmmm. Lgb.
136          * We will probably not have lyxrc before the main form is
137          * initialized, because error messages from lyxrc parsing
138          * are presented (and rightly so) in GUI popups. Asger.
139          */
140 {
141         // the main form
142         form_ = fl_bgn_form(FL_NO_BOX, width, height);
143         getForm()->u_vdata = this;
144         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
145         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
146
147         // Parameters for the appearance of the main form
148         int const air = 2;
149         int const bw = abs(fl_get_border_width());
150
151         menubar_.reset(new Menubar(this, menubackend));
152
153         toolbar_.reset(new Toolbar(this, air, 30 + air + bw, toolbardefaults));
154
155         int const ywork = 60 + 2 * air + bw;
156         int const workheight = height - ywork - (25 + 2 * air);
157
158         bufferview_.reset(new BufferView(this, air, ywork,
159                 width - 3 * air, workheight));
160         ::current_view = bufferview_.get();
161
162         minibuffer_.reset(new XMiniBuffer(this, *controlcommand_,
163                 air, height - (25 + air), width - (2 * air), 25));
164
165         //  assign an icon to main form
166         string const iconname = LibFileSearch("images", "lyx", "xpm");
167         if (!iconname.empty()) {
168                 unsigned int w, h;
169                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
170                                            iconname.c_str(),
171                                            &w,
172                                            &h,
173                                            &icon_mask_,
174                                            0,
175                                            0,
176                                            0);
177                 fl_set_form_icon(getForm(), icon_pixmap_, icon_mask_);
178         }
179
180         // set min size
181         fl_set_form_minsize(getForm(), 50, 50);
182
183         fl_end_form();
184
185         minibuffer_->dd_init();
186 }
187
188
189 void XFormsView::setWindowTitle(string const & title, string const & icon_title)
190 {
191         fl_set_form_title(getForm(), title.c_str());
192         fl_winicontitle(form_->window, icon_title.c_str());
193 }
194
195
196 void XFormsView::message(string const & str)
197 {
198         minibuffer_->message(str);
199 }
200
201
202 void XFormsView::show_view_state()
203 {
204         message(getLyXFunc().view_status_message());
205 }
206
207
208 // How should this actually work? Should it prohibit input in all BufferViews,
209 // or just in the current one? If "just the current one", then it should be
210 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
211 // run "prohibitInput" on all LyXViews which will run prohibitInput on all
212 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
213 // current LyXView that should be prohibited (Lgb) (This applies to
214 // "allowInput" as well.)
215 void XFormsView::prohibitInput() const
216 {
217         view()->hideCursor();
218
219         static Cursor cursor;
220         static bool cursor_undefined = true;
221
222         if (cursor_undefined) {
223                 cursor = XCreateFontCursor(fl_get_display(), XC_watch);
224                 XFlush(fl_get_display());
225                 cursor_undefined = false;
226         }
227
228         /* set the cursor to the watch for all forms and the canvas */
229         XDefineCursor(fl_get_display(), getForm()->window, cursor);
230
231         XFlush(fl_get_display());
232         fl_deactivate_all_forms();
233 }
234
235
236 void XFormsView::allowInput() const
237 {
238         /* reset the cursor from the watch for all forms and the canvas */
239
240         XUndefineCursor(fl_get_display(), getForm()->window);
241
242         XFlush(fl_get_display());
243         fl_activate_all_forms();
244 }