From 399e6e788cee5fb7d4a916365a4ef60f30cfe654 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Thu, 23 Mar 2006 20:04:05 +0000 Subject: [PATCH] Store colors as rgb values in branches. * src/frontends/xforms/Color.[Ch]: move to src * src/Color.[Ch] (getRGBColor): move to src/frontends/*/lyx_gui.C * src/BranchList.h (Branch::color_): change type to lyx::RGBColor * src/BranchList.[Ch] (Branch): new constrcutor, set color_ to LColor::background (getColor, setColor): adapt to type change of color_ * src/bufferparams.C (BufferParams::writeFile): adapt to type change of branch color * src/frontends/lyx_gui.h * src/frontends/gtk/lyx_gui.C * src/frontends/qt2/lyx_gui.C * src/frontends/qt4/lyx_gui.C * src/frontends/xforms/lyx_gui.C (getRGBColor): move from src/Color.[Ch] here * src/frontends/gtk/lyx_gui.C * src/frontends/xforms/lyx_gui.C (hexname): use getRGBColor * src/frontends/gtk/GDocument.C (update): adapt to type change of branch color (apply): add comment about color chooser * src/frontends/qt2/QDocumentDialog.C (updateBranchView): adapt to type change of branch color (toggleBranchColor): ditto * src/frontends/qt2/lcolorcache.[Ch] * src/frontends/qt4/lcolorcache.[Ch] (rgb2qcolor): new utility function * src/frontends/qt4/QBranches.C (QBranches::update): adapt to type change of branch color (QBranches::on_colorPB_clicked): ditto * src/frontends/xforms/FormDocument.C (get_current_color): adapt to type change of branch color (FormDocument::branch_update): * src/frontends/xforms/FormPreferences.C (FormPreferences::Colors::LoadBrowse): adapt to RGBColor changes * src/frontends/xforms/FormPreferences.h: remove unneeded RGBColor forward declaration * src/frontends/xforms/XWorkArea.C (XWorkArea::XWorkArea): adapt to RGBColor changes * src/frontends/xforms/Makefile.am: remove Color.[Ch] * src/frontends/xforms/FormColorpicker.[Ch]: adapt to RGBColor changes * src/frontends/xforms/xformsImage.C: adapt to RGBColor changes * src/frontends/controllers/ControlDocument.C (ControlDocument::dispatchParams): adapt to type change of branch color * src/Makefile.am: add Color.[Ch] git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13466 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BranchList.C | 23 +++++++++++-- src/BranchList.h | 15 +++++++-- src/{frontends/xforms => }/Color.C | 26 --------------- src/{frontends/xforms => }/Color.h | 10 ------ src/Makefile.am | 2 ++ src/bufferparams.C | 2 +- src/frontends/controllers/ControlDocument.C | 7 ++-- src/frontends/gtk/GDocument.C | 6 +++- src/frontends/gtk/lyx_gui.C | 36 +++++++++++++++++---- src/frontends/lyx_gui.h | 10 ++++++ src/frontends/qt2/QDocumentDialog.C | 11 ++----- src/frontends/qt2/lcolorcache.C | 7 ++++ src/frontends/qt2/lcolorcache.h | 7 ++++ src/frontends/qt2/lyx_gui.C | 17 ++++++++++ src/frontends/qt4/QBranches.C | 11 ++----- src/frontends/qt4/lcolorcache.C | 7 ++++ src/frontends/qt4/lcolorcache.h | 7 ++++ src/frontends/qt4/lyx_gui.C | 17 ++++++++++ src/frontends/xforms/FormColorpicker.C | 1 + src/frontends/xforms/FormColorpicker.h | 6 ++-- src/frontends/xforms/FormDocument.C | 24 ++++---------- src/frontends/xforms/FormPreferences.C | 5 ++- src/frontends/xforms/FormPreferences.h | 1 - src/frontends/xforms/Makefile.am | 2 -- src/frontends/xforms/XWorkArea.C | 7 ++-- src/frontends/xforms/lyx_gui.C | 36 +++++++++++++++++---- src/frontends/xforms/xformsImage.C | 9 +++--- 27 files changed, 201 insertions(+), 111 deletions(-) rename src/{frontends/xforms => }/Color.C (84%) rename src/{frontends/xforms => }/Color.h (83%) diff --git a/src/BranchList.C b/src/BranchList.C index 6d89551b74..75f183a0aa 100644 --- a/src/BranchList.C +++ b/src/BranchList.C @@ -11,11 +11,19 @@ #include #include "BranchList.h" +#include "LColor.h" +#include "frontends/lyx_gui.h" #include using std::string; +Branch::Branch() +{ + lyx_gui::getRGBColor(LColor::background, color_); +} + + string const & Branch::getBranch() const { return branch_; @@ -43,18 +51,28 @@ bool Branch::setSelected(bool b) } -string const & Branch::getColor() const +lyx::RGBColor const & Branch::getColor() const { return color_; } -void Branch::setColor(string const & c) +void Branch::setColor(lyx::RGBColor const & c) { color_ = c; } +void Branch::setColor(string const & c) +{ + if (c.size() == 7 && c[0] == '#') + color_ = lyx::RGBColor(c); + else + // no color set or invalid color - use normal background + lyx_gui::getRGBColor(LColor::background, color_); +} + + Branch * BranchList::find(std::string const & name) { List::iterator it = @@ -91,7 +109,6 @@ bool BranchList::add(string const & s) Branch br; br.setBranch(name); br.setSelected(false); - br.setColor("none"); list.push_back(br); } if (j == string::npos) diff --git a/src/BranchList.h b/src/BranchList.h index 610e97fd48..c0b18b57a0 100644 --- a/src/BranchList.h +++ b/src/BranchList.h @@ -30,12 +30,16 @@ #ifndef BRANCHES_H #define BRANCHES_H +#include "Color.h" + #include #include class Branch { public: + /// + Branch(); /// std::string const & getBranch() const; /// @@ -47,8 +51,15 @@ public: */ bool setSelected(bool); /// - std::string const & getColor() const; + lyx::RGBColor const & getColor() const; /// + void setColor(lyx::RGBColor const &); + /** + * Set color from a string "#rrggbb". + * Use LColor:background if the string is no valid color. + * This ensures compatibility with LyX 1.4.0 that had the symbolic + * color "none" that was displayed as LColor:background. + */ void setColor(std::string const &); @@ -58,7 +69,7 @@ private: /// bool selected_; /// - std::string color_; + lyx::RGBColor color_; }; diff --git a/src/frontends/xforms/Color.C b/src/Color.C similarity index 84% rename from src/frontends/xforms/Color.C rename to src/Color.C index c39283214a..2e797540bf 100644 --- a/src/frontends/xforms/Color.C +++ b/src/Color.C @@ -12,8 +12,6 @@ #include "Color.h" -#include "lyx_forms.h" - #include "LColor.h" #include @@ -33,7 +31,6 @@ using std::ostringstream; using std::string; namespace lyx { -namespace frontend { namespace { @@ -51,28 +48,6 @@ int hexstrToInt(string const & str) -bool getRGBColor(LColor_color col, - unsigned int & r, unsigned int & g, unsigned int & b) -{ - string const name = lcolor.getX11Name(col); - Display * const display = fl_get_display(); - Colormap const cmap = fl_state[fl_get_vclass()].colormap; - XColor xcol, ccol; - - if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) { - r = 0; - g = 0; - b = 0; - return false; - } - - r = xcol.red / 256; - g = xcol.green / 256; - b = xcol.blue / 256; - return true; -} - - string const X11hexname(RGBColor const & col) { ostringstream ostr; @@ -199,5 +174,4 @@ HSVColor::HSVColor(RGBColor const & rgb) } } -} // namespace frontend } // namespace lyx diff --git a/src/frontends/xforms/Color.h b/src/Color.h similarity index 83% rename from src/frontends/xforms/Color.h rename to src/Color.h index 498f3e9e9b..ee3d88bf42 100644 --- a/src/frontends/xforms/Color.h +++ b/src/Color.h @@ -18,16 +18,7 @@ #include -class LColor_color; - namespace lyx { -namespace frontend { - -/** Given col, fills r, g, b in the range 0-255. - The function returns true if successful. - It returns false on failure and sets r, g, b to 0. */ -bool getRGBColor(LColor_color col, - unsigned int & r, unsigned int & g, unsigned int & b); struct RGBColor; /// returns a string of form #rrggbb, given an RGBColor struct @@ -78,7 +69,6 @@ bool operator!=(RGBColor const & c1, RGBColor const & c2) return !(c1 == c2); } -} // namespace frontend } // namespace lyx #endif diff --git a/src/Makefile.am b/src/Makefile.am index 3414a16d95..da49ab3f20 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -85,6 +85,8 @@ lyx_SOURCES = \ BranchList.h \ Chktex.C \ Chktex.h \ + Color.C \ + Color.h \ CutAndPaste.C \ CutAndPaste.h \ DepTable.C \ diff --git a/src/bufferparams.C b/src/bufferparams.C index 8c4bed9e43..b03e7b7489 100644 --- a/src/bufferparams.C +++ b/src/bufferparams.C @@ -601,7 +601,7 @@ void BufferParams::writeFile(ostream & os) const for (; it != end; ++it) { os << "\\branch " << it->getBranch() << "\n\\selected " << it->getSelected() - << "\n\\color " << it->getColor() + << "\n\\color " << lyx::X11hexname(it->getColor()) << "\n\\end_branch" << "\n"; } diff --git a/src/frontends/controllers/ControlDocument.C b/src/frontends/controllers/ControlDocument.C index 593b664643..2ea1ade7bd 100644 --- a/src/frontends/controllers/ControlDocument.C +++ b/src/frontends/controllers/ControlDocument.C @@ -114,11 +114,8 @@ void ControlDocument::dispatchParams() for (; it != end; ++it) { string const & current_branch = it->getBranch(); Branch const * branch = branchlist.find(current_branch); - string x11hexname = branch->getColor(); - // check that we have a valid color! - if (x11hexname.empty() || x11hexname[0] != '#') - x11hexname = - lcolor.getX11Name(LColor::background); + string const x11hexname = + lyx::X11hexname(branch->getColor()); // display the new color string const str = current_branch + ' ' + x11hexname; kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str)); diff --git a/src/frontends/gtk/GDocument.C b/src/frontends/gtk/GDocument.C index 08a8836e9b..216994f893 100644 --- a/src/frontends/gtk/GDocument.C +++ b/src/frontends/gtk/GDocument.C @@ -529,7 +529,7 @@ void GDocument::update() (*row)[branchColName_] = (*it).getBranch(); std::cerr << "update: loading '" << (*it).getBranch() << "'\n"; (*row)[branchColActivated_] = (*it).getSelected(); - (*row)[branchColColor_] = (*it).getColor(); + (*row)[branchColColor_] = X11hexname((*it).getColor()); } // *** End "Branches" Page *** @@ -720,6 +720,10 @@ void GDocument::apply() Branch * newbranch = branchlist.find(name); newbranch->setSelected((*row)[branchColActivated_]); Glib::ustring const color = (*row)[branchColColor_]; + // FIXME: The color should be editable via a color + // chooser, not a text field (see qt/xforms frontends) + // setColor will silently ignore an invalid color and + // use the normal background color for now. newbranch->setColor(color); } } diff --git a/src/frontends/gtk/lyx_gui.C b/src/frontends/gtk/lyx_gui.C index 4a856671a6..fef13b1af1 100644 --- a/src/frontends/gtk/lyx_gui.C +++ b/src/frontends/gtk/lyx_gui.C @@ -26,6 +26,7 @@ #include "funcrequest.h" #include "gettext.h" +#include "Color.h" #include "LColor.h" #include "LyXAction.h" #include "lyx_main.h" @@ -175,23 +176,44 @@ FuncStatus lyx_gui::getStatus(FuncRequest const & ev) } -string const lyx_gui::hexname(LColor_color col) +bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol) { Gdk::Color gdkColor; Gdk::Color * gclr = colorCache.getColor(col); if (!gclr) { gclr = &gdkColor; - gclr->parse(lcolor.getX11Name(col)); + if(!gclr->parse(lcolor.getX11Name(col))) { + rgbcol.r = 0; + rgbcol.g = 0; + rgbcol.b = 0; + return false; + } + } + + // Note that X stores the RGB values in the range 0 - 65535 + // whilst we require them in the range 0 - 255. + rgbcol.r = gclr->get_red() / 256; + rgbcol.g = gclr->get_green() / 256; + rgbcol.b = gclr->get_blue() / 256; + return true; +} + + +string const lyx_gui::hexname(LColor_color col) +{ + lyx::RGBColor rgbcol; + if (!getRGBColor(col, rgbcol)) { + lyxerr << "X can't find color for \"" << lcolor.getLyXName(col) + << '"' << std::endl; + return string(); } std::ostringstream os; - // Note that X stores the RGB values in the range 0 - 65535 - // whilst we require them in the range 0 - 255. os << std::setbase(16) << std::setfill('0') - << std::setw(2) << (gclr->get_red() / 256) - << std::setw(2) << (gclr->get_green() / 256) - << std::setw(2) << (gclr->get_blue() / 256); + << std::setw(2) << rgbcol.r + << std::setw(2) << rgbcol.g + << std::setw(2) << rgbcol.b; return os.str(); } diff --git a/src/frontends/lyx_gui.h b/src/frontends/lyx_gui.h index 153c46fa54..f3829b2af3 100644 --- a/src/frontends/lyx_gui.h +++ b/src/frontends/lyx_gui.h @@ -27,6 +27,9 @@ class LyXComm; class LyXDataSocket; class LyXServerSocket; class FuncRequest; +namespace lyx { +struct RGBColor; +} /// GUI interaction namespace lyx_gui { @@ -74,6 +77,13 @@ void exit(); */ FuncStatus getStatus(FuncRequest const & ev); +/** + * Given col, fills r, g, b in the range 0-255. + * The function returns true if successful. + * It returns false on failure and sets r, g, b to 0. + */ +bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol); + /** Eg, passing LColor::black returns "000000", * passing LColor::white returns "ffffff". */ diff --git a/src/frontends/qt2/QDocumentDialog.C b/src/frontends/qt2/QDocumentDialog.C index fb0bba6a3e..146ccdd538 100644 --- a/src/frontends/qt2/QDocumentDialog.C +++ b/src/frontends/qt2/QDocumentDialog.C @@ -14,6 +14,7 @@ #include "QDocumentDialog.h" #include "floatplacement.h" +#include "lcolorcache.h" #include "lengthcombo.h" #include "validators.h" #include "panelstack.h" @@ -421,10 +422,7 @@ void QDocumentDialog::updateBranchView() QString const sel = it->getSelected() ? qt_("Yes") : qt_("No"); QListViewItem * newItem = new QListViewItem(branchesModule->branchesLV, bname, sel); - string const x11hexname = it->getColor(); - QColor itemcolor; - if (x11hexname[0] == '#') - itemcolor.setNamedColor(toqstr(x11hexname)); + QColor const itemcolor = rgb2qcolor(it->getColor()); if (itemcolor.isValid()) { QPixmap coloritem(30, 10); coloritem.fill(itemcolor); @@ -503,16 +501,13 @@ void QDocumentDialog::toggleBranchColor() if (selItem != 0) sel_branch = selItem->text(0); if (sel_branch) { - QColor initial; string current_branch = fromqstr(sel_branch); Branch * branch = form_->branchlist_.find(current_branch); if (!branch) return; - string x11hexname = branch->getColor(); - if (x11hexname[0] == '#') - initial.setNamedColor(toqstr(x11hexname)); + QColor const initial = rgb2qcolor(branch->getColor()); QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget())); if (ncol.isValid()){ // add the color to the branchlist diff --git a/src/frontends/qt2/lcolorcache.C b/src/frontends/qt2/lcolorcache.C index f7cd8a2729..038b6449de 100644 --- a/src/frontends/qt2/lcolorcache.C +++ b/src/frontends/qt2/lcolorcache.C @@ -12,6 +12,7 @@ #include "lcolorcache.h" +#include "Color.h" #include "LColor.h" LColorCache lcolorcache; @@ -38,3 +39,9 @@ void LColorCache::clear() { colormap.clear(); } + + +QColor const rgb2qcolor(lyx::RGBColor const & rgb) +{ + return QColor(rgb.r, rgb.g, rgb.b); +} diff --git a/src/frontends/qt2/lcolorcache.h b/src/frontends/qt2/lcolorcache.h index e03f5b2f78..92af4533c6 100644 --- a/src/frontends/qt2/lcolorcache.h +++ b/src/frontends/qt2/lcolorcache.h @@ -19,6 +19,11 @@ #include +namespace lyx { +struct RGBColor; +} + + // FIXME: use a fixed-size array not a map ? /** @@ -43,4 +48,6 @@ private: /// singleton instance extern LColorCache lcolorcache; +/// +QColor const rgb2qcolor(lyx::RGBColor const &); #endif // LCOLORCACHE_H diff --git a/src/frontends/qt2/lyx_gui.C b/src/frontends/qt2/lyx_gui.C index d1e1f851c9..360c8be666 100644 --- a/src/frontends/qt2/lyx_gui.C +++ b/src/frontends/qt2/lyx_gui.C @@ -16,6 +16,7 @@ // FIXME: move this stuff out again #include "bufferlist.h" #include "BufferView.h" +#include "Color.h" #include "funcrequest.h" #include "LColor.h" #include "lyx_main.h" @@ -300,6 +301,22 @@ FuncStatus getStatus(FuncRequest const & ev) } +bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol) +{ + QColor const & qcol = lcolorcache.get(col); + if (!qcol.isValid()) { + rgbcol.r = 0; + rgbcol.g = 0; + rgbcol.b = 0; + return false; + } + rgbcol.r = qcol.red(); + rgbcol.g = qcol.green(); + rgbcol.b = qcol.blue(); + return true; +} + + string const hexname(LColor_color col) { return ltrim(fromqstr(lcolorcache.get(col).name()), "#"); diff --git a/src/frontends/qt4/QBranches.C b/src/frontends/qt4/QBranches.C index cfeac11bbf..77c74f6900 100644 --- a/src/frontends/qt4/QBranches.C +++ b/src/frontends/qt4/QBranches.C @@ -13,6 +13,7 @@ #include "QBranches.h" +#include "lcolorcache.h" #include "validators.h" #include "qt_helpers.h" @@ -68,10 +69,7 @@ void QBranches::update() QString const sel = it->getSelected() ? qt_("Yes") : qt_("No"); Q3ListViewItem * newItem = new Q3ListViewItem(branchesLV, bname, sel); - string const x11hexname = it->getColor(); - QColor itemcolor; - if (x11hexname[0] == '#') - itemcolor.setNamedColor(toqstr(x11hexname)); + QColor const itemcolor = rgb2qcolor(it->getColor()); if (itemcolor.isValid()) { QPixmap coloritem(30, 10); coloritem.fill(itemcolor); @@ -154,16 +152,13 @@ void QBranches::on_colorPB_clicked() if (selItem != 0) sel_branch = selItem->text(0); if (!sel_branch.isEmpty()) { - QColor initial("lightskyblue"); string current_branch = fromqstr(sel_branch); Branch * branch = branchlist_.find(current_branch); if (!branch) return; - string x11hexname = branch->getColor(); - if (x11hexname[0] == '#') - initial.setNamedColor(toqstr(x11hexname)); + QColor const initial = rgb2qcolor(branch->getColor()); QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget())); if (ncol.isValid()){ // add the color to the branchlist diff --git a/src/frontends/qt4/lcolorcache.C b/src/frontends/qt4/lcolorcache.C index 9f7dfa1767..bc4392c46b 100644 --- a/src/frontends/qt4/lcolorcache.C +++ b/src/frontends/qt4/lcolorcache.C @@ -10,6 +10,7 @@ #include +#include "Color.h" #include "lcolorcache.h" #include "LColor.h" @@ -51,3 +52,9 @@ void LColorCache::clear() { colormap.clear(); } + + +QColor const rgb2qcolor(lyx::RGBColor const & rgb) +{ + return QColor(rgb.r, rgb.g, rgb.b); +} diff --git a/src/frontends/qt4/lcolorcache.h b/src/frontends/qt4/lcolorcache.h index 1c68d11610..376588ecdc 100644 --- a/src/frontends/qt4/lcolorcache.h +++ b/src/frontends/qt4/lcolorcache.h @@ -19,6 +19,11 @@ #include +namespace lyx { +struct RGBColor; +} + + // FIXME: use a fixed-size array not a map ? /** @@ -43,4 +48,6 @@ private: /// singleton instance extern LColorCache lcolorcache; +/// +QColor const rgb2qcolor(lyx::RGBColor const &); #endif // LCOLORCACHE_H diff --git a/src/frontends/qt4/lyx_gui.C b/src/frontends/qt4/lyx_gui.C index da1f7c0e34..60084c5ffc 100644 --- a/src/frontends/qt4/lyx_gui.C +++ b/src/frontends/qt4/lyx_gui.C @@ -16,6 +16,7 @@ // FIXME: move this stuff out again #include "bufferlist.h" #include "BufferView.h" +#include "Color.h" #include "funcrequest.h" #include "LColor.h" #include "lyx_main.h" @@ -294,6 +295,22 @@ FuncStatus getStatus(FuncRequest const & ev) } +bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol) +{ + QColor const & qcol = lcolorcache.get(col); + if (!qcol.isValid()) { + rgbcol.r = 0; + rgbcol.g = 0; + rgbcol.b = 0; + return false; + } + rgbcol.r = qcol.red(); + rgbcol.g = qcol.green(); + rgbcol.b = qcol.blue(); + return true; +} + + string const hexname(LColor_color col) { return ltrim(fromqstr(lcolorcache.get(col).name()), "#"); diff --git a/src/frontends/xforms/FormColorpicker.C b/src/frontends/xforms/FormColorpicker.C index c9e8d25120..d235741d50 100644 --- a/src/frontends/xforms/FormColorpicker.C +++ b/src/frontends/xforms/FormColorpicker.C @@ -24,6 +24,7 @@ #include "lyx_forms.h" +using lyx::RGBColor; using std::string; namespace lyx { diff --git a/src/frontends/xforms/FormColorpicker.h b/src/frontends/xforms/FormColorpicker.h index ab59809887..8c335f39c8 100644 --- a/src/frontends/xforms/FormColorpicker.h +++ b/src/frontends/xforms/FormColorpicker.h @@ -35,7 +35,7 @@ public: a color is chosen (or the dialog is closed). \param color the color used to initialise the dialog. */ - RGBColor const & requestColor(RGBColor const & color); + lyx::RGBColor const & requestColor(lyx::RGBColor const & color); /** Input callback function. * Invoked only by the xforms callback interface @@ -71,8 +71,8 @@ private: /// The title displayed by the Window Manager. std::string title_; - RGBColor input_color_; - RGBColor color_; + lyx::RGBColor input_color_; + lyx::RGBColor color_; bool finished_; /// Passed to the window manager to give a pretty little symbol ;-) diff --git a/src/frontends/xforms/FormDocument.C b/src/frontends/xforms/FormDocument.C index 6dee5c771d..739abd997c 100644 --- a/src/frontends/xforms/FormDocument.C +++ b/src/frontends/xforms/FormDocument.C @@ -678,21 +678,13 @@ RGBColor get_current_color(FL_OBJECT * browser, BranchList const & branchlist) { BOOST_ASSERT(browser && browser->objclass == FL_BROWSER); - RGBColor color; - int const i = fl_get_browser(browser); string const branch_name = fl_get_browser_line(browser, i); Branch const * branch = branchlist.find(branch_name); if (!branch) - return color; + return RGBColor(); - string const x11hexname = branch->getColor(); - if (x11hexname[0] == '#') { - color = RGBColor(x11hexname); - } else{ - fl_getmcolor(FL_COL1, &color.r, &color.g, &color.b); - } - return color; + return branch->getColor(); } } // namespace anon @@ -1298,20 +1290,16 @@ void FormDocument::branch_update(BufferParams const & params) // display proper colour... RGBColor rgb; - string x11hexname; if (current_branch == "none") - x11hexname = "none"; + fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b); else { Branch * branch = branchlist_.find(current_branch); if (branch) - x11hexname = branch->getColor(); + rgb = branch->getColor(); + else + fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b); } - if (x11hexname[0] == '#') { - rgb = RGBColor(x11hexname); - } else { - fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b); - } fl_mapcolor(GUI_COLOR_CHOICE, rgb.r, rgb.g, rgb.b); fl_redraw_object(branch_->button_color); diff --git a/src/frontends/xforms/FormPreferences.C b/src/frontends/xforms/FormPreferences.C index eb4eb8b66d..04f78aef1a 100644 --- a/src/frontends/xforms/FormPreferences.C +++ b/src/frontends/xforms/FormPreferences.C @@ -27,6 +27,7 @@ #include "lastfiles.h" #include "LColor.h" #include "lyxfont.h" +#include "frontends/lyx_gui.h" #include "support/convert.h" #include "support/lstrings.h" @@ -38,6 +39,8 @@ #include +using lyx::RGBColor; + using std::endl; using std::make_pair; using std::max; @@ -621,7 +624,7 @@ void FormPreferences::Colors::LoadBrowserLyX() || lc == LColor::ignore) continue; RGBColor col; - bool const success = getRGBColor(lc, col.r, col.g, col.b); + bool const success = lyx_gui::getRGBColor(lc, col); if (!success) { lyxerr << "FormPreferences::Colors::LoadBrowserLyX:\n" << "LColor " << lcolor.getLyXName(lc) diff --git a/src/frontends/xforms/FormPreferences.h b/src/frontends/xforms/FormPreferences.h index d82d9bf54b..424bc5c5e8 100644 --- a/src/frontends/xforms/FormPreferences.h +++ b/src/frontends/xforms/FormPreferences.h @@ -50,7 +50,6 @@ struct FD_preferences_screen_fonts; struct FD_preferences_spelloptions; class FormColorpicker; -class RGBColor; /** This class provides an XForms implementation of the FormPreferences Dialog. * The preferences dialog allows users to set/save their preferences. diff --git a/src/frontends/xforms/Makefile.am b/src/frontends/xforms/Makefile.am index 4a04a4e6f5..adc87126fa 100644 --- a/src/frontends/xforms/Makefile.am +++ b/src/frontends/xforms/Makefile.am @@ -60,8 +60,6 @@ libxforms_la_SOURCES = \ xscreen.C \ xscreen.h \ Alert_pimpl.C \ - Color.C \ - Color.h \ ColorHandler.C \ ColorHandler.h \ Dialogs.C \ diff --git a/src/frontends/xforms/XWorkArea.C b/src/frontends/xforms/XWorkArea.C index 52e1644168..73c6d1ac40 100644 --- a/src/frontends/xforms/XWorkArea.C +++ b/src/frontends/xforms/XWorkArea.C @@ -16,6 +16,7 @@ #include "Color.h" #include "XFormsView.h" #include "XLyXKeySym.h" +#include "lyx_gui.h" #include "debug.h" #include "funcrequest.h" @@ -119,9 +120,9 @@ XWorkArea::XWorkArea(LyXView & owner, int w, int h) fl_set_object_resize(obj, FL_RESIZE_ALL); fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity); - unsigned int r, g, b; - if (getRGBColor(LColor::background, r, g, b)) { - fl_mapcolor(FL_FREE_COL12, r, g, b); + RGBColor col; + if (lyx_gui::getRGBColor(LColor::background, col)) { + fl_mapcolor(FL_FREE_COL12, col.r, col.g, col.b); fl_set_object_color(obj, FL_FREE_COL12, FL_MCOL); } diff --git a/src/frontends/xforms/lyx_gui.C b/src/frontends/xforms/lyx_gui.C index 9b19a07309..bf562beded 100644 --- a/src/frontends/xforms/lyx_gui.C +++ b/src/frontends/xforms/lyx_gui.C @@ -51,7 +51,6 @@ using lyx::support::AddName; using lyx::support::package; using lyx::frontend::fontloader; -using lyx::frontend::getRGBColor; using lyx::frontend::lyxColorHandler; using lyx::frontend::LyXColorHandler; using lyx::frontend::XformsColor; @@ -348,11 +347,34 @@ FuncStatus getStatus(FuncRequest const & /*ev*/) return FuncStatus(); } + +bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol) +{ + string const name = lcolor.getX11Name(col); + Display * const display = fl_get_display(); + Colormap const cmap = fl_state[fl_get_vclass()].colormap; + XColor xcol, ccol; + + if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) { + rgbcol.r = 0; + rgbcol.g = 0; + rgbcol.b = 0; + return false; + } + + // Note that X stores the RGB values in the range 0 - 65535 + // whilst we require them in the range 0 - 255. + rgbcol.r = xcol.red / 256; + rgbcol.g = xcol.green / 256; + rgbcol.b = xcol.blue / 256; + return true; +} + + string const hexname(LColor_color col) { - unsigned int r, g, b; - bool const success = getRGBColor(col, r, g, b); - if (!success) { + lyx::RGBColor rgbcol; + if (!getRGBColor(col, rgbcol)) { lyxerr << "X can't find color for \"" << lcolor.getLyXName(col) << '"' << endl; return string(); @@ -361,9 +383,9 @@ string const hexname(LColor_color col) ostringstream os; os << setbase(16) << setfill('0') - << setw(2) << r - << setw(2) << g - << setw(2) << b; + << setw(2) << rgbcol.r + << setw(2) << rgbcol.g + << setw(2) << rgbcol.b; return os.str(); } diff --git a/src/frontends/xforms/xformsImage.C b/src/frontends/xforms/xformsImage.C index fe1e8158ee..b5f6ecc5b1 100644 --- a/src/frontends/xforms/xformsImage.C +++ b/src/frontends/xforms/xformsImage.C @@ -12,6 +12,7 @@ #include "xformsImage.h" #include "Color.h" +#include "lyx_gui.h" #include "debug.h" #include "format.h" @@ -33,8 +34,6 @@ #include #include -using lyx::frontend::getRGBColor; - using lyx::support::float_equal; using lyx::support::prefixIs; using lyx::support::rtrim; @@ -470,13 +469,13 @@ void init_graphics() unsigned int packedcolor(LColor::color col) { - unsigned int r, g, b; - bool const success = getRGBColor(col, r, g, b); + lyx::RGBColor rgb; + bool const success = lyx_gui::getRGBColor(col, rgb); if (!success) // Set to black on failure return FL_PACK(255, 255, 255); - return FL_PACK(r, g, b); + return FL_PACK(rgb.r, rgb.g, rgb.b); } } // namespace anon