| 1 | #include <FL/fl_draw.H> |
| 2 | |
| 3 | #include <fstream> |
| 4 | |
| 5 | #include "clickablebox.hh" |
| 6 | #include "../fe/background.hh" |
| 7 | |
| 8 | fe::ClickableBox *fe::ClickableBox::lastSelected = nullptr; |
| 9 | |
| 10 | namespace fe { |
| 11 | int ClickableBox::handle(int event) { |
| 12 | switch (event) { |
| 13 | case FL_PUSH: |
| 14 | if (!imagePath.empty()) { |
| 15 | if (lastSelected && lastSelected != this) { |
| 16 | lastSelected->isSelected = false; |
| 17 | lastSelected->redraw(); |
| 18 | } |
| 19 | |
| 20 | isSelected = true; |
| 21 | lastSelected = this; |
| 22 | redraw(); |
| 23 | |
| 24 | background = std::make_unique<uw::Background>(imagePath); |
| 25 | background->setWallpaper(fe::Background().getMode()); |
| 26 | alterXinitrc(getImagePath()); |
| 27 | } |
| 28 | return 1; |
| 29 | default: |
| 30 | return Fl_Box::handle(event); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void ClickableBox::draw() { |
| 35 | Fl_Box::draw(); |
| 36 | |
| 37 | if (isSelected) { |
| 38 | fl_color(234, 121, 216); |
| 39 | fl_line_style(FL_SOLID, 9); |
| 40 | fl_rect(x(), y(), w(), h()); |
| 41 | fl_line_style(0); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void ClickableBox::alterXinitrc(const std::string &npath) { |
| 46 | const std::string xinitrc = std::string(getenv("HOME")) + "/.xinitrc"; |
| 47 | std::ifstream in(xinitrc); |
| 48 | std::vector<std::string> lines; |
| 49 | std::string line; |
| 50 | bool founduw = false; |
| 51 | bool addeduw = false; |
| 52 | |
| 53 | std::string modeFlag; |
| 54 | switch(fe::Background().getMode()) { |
| 55 | case uw::Mode::TILE: modeFlag = "-t"; break; |
| 56 | case uw::Mode::CENTER: modeFlag = "-c"; break; |
| 57 | case uw::Mode::FILL: modeFlag = "-f"; break; |
| 58 | default: modeFlag = "-t"; break; |
| 59 | } |
| 60 | |
| 61 | while (std::getline(in, line)) { |
| 62 | if (line.rfind("uw -", 0) == 0) { |
| 63 | line = "uw " + modeFlag + " " + npath + " &"; |
| 64 | founduw = true; |
| 65 | } |
| 66 | lines.push_back(line); |
| 67 | |
| 68 | if (line.rfind("exec ", 0) == 0 && !founduw && !addeduw) { |
| 69 | lines.insert(lines.end() - 1, "uw " + modeFlag + " " + npath + " &"); |
| 70 | addeduw = true; |
| 71 | } |
| 72 | } |
| 73 | in.close(); |
| 74 | |
| 75 | if (!founduw && !addeduw) { |
| 76 | lines.push_back("uw " + modeFlag + " " + npath + " &"); |
| 77 | } |
| 78 | |
| 79 | std::ofstream out(xinitrc); |
| 80 | for (const auto &eline : lines) { |
| 81 | out << eline << std::endl; |
| 82 | } |
| 83 | out.close(); |
| 84 | } |
| 85 | } |