1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * This file is Copyright 1996-1998
12 * ====================================================== */
14 // Added pseudo-action handling, asierra 180296
19 #pragma implementation "toolbar.h"
23 #include "lyx_gui_misc.h"
31 #include "LyXAction.h"
32 #include "support/lstrings.h"
33 #include "support/filetools.h"
38 // this one is not "C" because combox callbacks are really C++ %-|
39 extern void LayoutsCB(int, void *);
40 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
41 extern LyXAction lyxaction;
44 Toolbar::toolbarItem::toolbarItem()
46 action = LFUN_NOACTION;
51 void Toolbar::toolbarItem::clean()
54 fl_delete_object(icon);
61 Toolbar::toolbarItem::~toolbarItem()
67 Toolbar::toolbarItem &
68 Toolbar::toolbarItem::operator=(Toolbar::toolbarItem const & ti)
70 // do we have to check icon and IsBitmap too?
72 icon = 0; // locally we need to get the icon anew
78 Toolbar::Toolbar(LyXView * o, int x, int y)
79 : owner(o), sxpos(x), sypos(y)
87 // extracts the default toolbar actions from LyXRC
88 for (ToolbarDefaults::const_iterator cit =
89 lyxrc.toolbardefaults.begin();
90 cit != lyxrc.toolbardefaults.end(); ++cit) {
92 lyxerr[Debug::TOOLBAR] << "tool action: "
99 // timer-cb for bubble-help (Matthias)
100 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
102 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
103 // The trick we use to get the help text is to read the
104 // argument of the callback that has been registered for
105 // ToolBarCB. (JMarc)
106 string help = lyxaction.helpText(ob->argument);
107 fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
108 ob->form->y + ob->y + ob->h);
112 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
114 Toolbar::BubbleTimerCB(ob, data);
118 // post_handler for bubble-help (Matthias)
119 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
120 FL_Coord /*mx*/, FL_Coord /*my*/,
121 int /*key*/, void */*xev*/)
123 Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_vdata);
125 // We do not test for empty help here, since this can never happen
126 if(event == FL_ENTER){
127 fl_set_object_callback(t->bubble_timer,
128 C_Toolbar_BubbleTimerCB,
129 reinterpret_cast<long>(ob));
130 fl_set_timer(t->bubble_timer, 1);
132 else if(event != FL_MOTION){
133 fl_set_timer(t->bubble_timer, 0);
140 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
141 FL_Coord /*mx*/, FL_Coord /*my*/,
144 return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
149 void Toolbar::activate()
151 ToolbarList::const_iterator p = toollist.begin();
152 for (; p != toollist.end(); ++p) {
154 fl_activate_object(p->icon);
160 void Toolbar::deactivate()
162 ToolbarList::const_iterator p = toollist.begin();
163 for (; p != toollist.end(); ++p) {
165 fl_deactivate_object(p->icon);
170 void Toolbar::update()
172 ToolbarList::const_iterator p = toollist.begin();
173 for (; p != toollist.end(); ++p) {
175 int status = owner->getLyXFunc()->getStatus(p->action);
176 if (status & LyXFunc::ToggleOn) {
177 // I'd like to use a different color
178 // here, but then the problem is to
179 // know how to use transparency with
180 // Xpm library. It seems pretty
181 // complicated to me (JMarc)
182 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
183 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
185 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
186 fl_set_object_boxtype(p->icon, FL_UP_BOX);
189 if (status & LyXFunc::Disabled) {
190 // Is there a way here to specify a
191 // mask in order to show that the
192 // button is disabled? (JMarc)
193 fl_deactivate_object(p->icon);
196 fl_activate_object(p->icon);
202 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
204 Toolbar * t = static_cast<Toolbar*>(ob->u_vdata);
206 string res = t->owner->getLyXFunc()->Dispatch(int(ac));
208 lyxerr[Debug::TOOLBAR] << res << endl;
212 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
214 Toolbar::ToolbarCB(ob, data);
218 int Toolbar::get_toolbar_func(string const & func)
220 int action = lyxaction.LookupFunc(func.c_str());
222 if (func == "separator"){
223 action = TOOL_SEPARATOR;
224 } else if (func == "layouts"){
225 action = TOOL_LAYOUTS;
232 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
233 string name, arg, xpm_name;
236 if (lyxaction.isPseudoAction(action)) {
237 lyxerr[Debug::TOOLBAR] << "Pseudo action " << action << endl;
239 act = lyxaction.retrieveActionArg(action, arg);
240 name = lyxaction.getActionName(act);
241 xpm_name = subst(name + ' ' + arg, ' ','_');
243 act = (kb_action)action;
244 name = lyxaction.getActionName(action);
248 lyxerr[Debug::TOOLBAR] << "Icon name for action " << action
249 << " is `" << xpm_name << "'" << endl;
251 string fullname = LibFileSearch("images", xpm_name, "xpm");
253 if (!fullname.empty()) {
254 lyxerr[Debug::TOOLBAR] << "Full icon name is `"
255 << fullname << "'" << endl;
256 fl_set_pixmapbutton_file(obj, fullname.c_str());
260 if (act == LFUN_INSERT_MATH && !arg.empty()) {
261 lyxerr[Debug::TOOLBAR] << "Using mathed-provided icon" << endl;
262 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
265 fl_set_pixmapbutton_data(obj, const_cast<char **>(pixmap));
269 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
270 fullname = LibFileSearch("images", "unknown", "xpm");
271 if (!fullname.empty()) {
272 lyxerr[Debug::TOOLBAR] << "Using default `unknown' icon"
274 fl_set_pixmapbutton_file(obj, fullname.c_str());
278 void Toolbar::set(bool doingmain)
280 // we shouldn't set if we have not cleaned
281 if (!cleaned) return;
286 fl_freeze_form(owner->getForm());
287 fl_addto_form(owner->getForm());
291 // add the time if it don't exist
292 if (bubble_timer == 0)
293 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
294 xpos, ypos, 0, 0, "Timer");
297 ToolbarList::iterator item = toollist.begin();
298 for (; item != toollist.end(); ++item) {
299 switch(item->action){
304 xpos += standardspacing;
306 combox = new Combox(FL_COMBOX_DROPLIST);
307 combox->add(xpos, ypos, 135, height, 400);
308 combox->setcallback(LayoutsCB);
309 combox->resize(FL_RESIZE_ALL);
310 combox->gravity(NorthWestGravity, NorthWestGravity);
314 xpos += standardspacing;
316 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
320 fl_set_object_resize(obj, FL_RESIZE_ALL);
321 fl_set_object_gravity(obj,
324 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
325 static_cast<long>(item->action));
326 // Remove the blue feedback rectangle
327 fl_set_pixmapbutton_focus_outline(obj, 0);
330 #if FL_REVISION >= 89
331 string help = lyxaction.helpText(item->action);
332 fl_set_object_helper(obj, help.c_str());
334 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
337 // The toolbar that this object belongs too.
340 setPixmap(obj, item->action, buttonwidth, height);
341 // we must remember to update the positions
344 /* Here will come a check to see if the new
345 * pos is within the bounds of the main frame,
346 * and perhaps wrap the toolbar if not.
354 fl_unfreeze_form(owner->getForm());
355 // Should be safe to do this here.
356 owner->updateLayoutChoice();
359 // set the state of the icons
366 void Toolbar::add(int action, bool doclean)
368 if (doclean && !cleaned) clean();
370 // this is what we do if we want to add to an existing
372 if (!doclean && owner) {
373 // first "hide" the toolbar buttons. This is not a real hide
374 // actually it deletes and frees the button altogether.
375 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
380 fl_freeze_form(owner->getForm());
382 ToolbarList::iterator p = toollist.begin();
383 for (; p != toollist.end(); ++p) {
391 fl_unfreeze_form(owner->getForm());
392 cleaned = true; // this is not completely true, but OK anyway
395 // there exist some special actions not part of
396 // kb_action: SEPARATOR, LAYOUTS
399 newItem.action = action;
400 toollist.push_back(newItem);
404 void Toolbar::clean()
406 //reset(); // I do not understand what this reset() is, anyway
408 //now delete all the objects..
410 fl_freeze_form(owner->getForm());
412 // G++ vector does not have clear defined
414 toollist.erase(toollist.begin(), toollist.end());
416 lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
423 fl_unfreeze_form(owner->getForm());
424 lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
429 void Toolbar::push(int nth)
431 lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
432 << nth << '\'' << endl;
434 if (nth <= 0 || nth >= int(toollist.size())) {
435 // item nth not found...
440 fl_trigger_object(toollist[nth - 1].icon);
444 void Toolbar::add(string const & func, bool doclean)
446 int tf = lyxaction.LookupFunc(func);
449 lyxerr << "Toolbar::add: no LyX command called`"
450 << func << "'exists!" << endl;
457 void Toolbar::reset()
459 //toollist = 0; // what is this supposed to do?
464 // void Toolbar::lightReset()
466 // standardspacing = 2; // the usual space between items
467 // sepspace = 6; // extra space
468 // xpos = sxpos - standardspacing;
470 // buttonwidth = 30; // the standard button width
471 // height = 30; // the height of all items in the toolbar