| 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include <simgear/props/props.hxx> |
| 6 | |
| 7 | #include "FlightGear_pu.h" |
| 8 | #include "layout.hxx" |
| 9 | |
| 10 | // This file contains the code implementing the LayoutWidget class in |
| 11 | // terms of a PropertyNode (plus a tiny bit of PUI glue). See |
| 12 | // layout.cxx for the actual layout engine. |
| 13 | |
| 14 | puFont LayoutWidget::FONT; |
| 15 | |
| 16 | void LayoutWidget::setDefaultFont(puFont* font, int pixels) |
| 17 | { |
| 18 | UNIT = (int)(pixels * (1/3.) + 0.999); |
| 19 | FONT = *font; |
| 20 | } |
| 21 | |
| 22 | int LayoutWidget::stringLength(const char* s) |
| 23 | { |
| 24 | return (int)(FONT.getFloatStringWidth(s) + 0.999); |
| 25 | } |
| 26 | |
| 27 | std::string LayoutWidget::type() |
| 28 | { |
| 29 | std::string t = _prop->getNameString(); |
| 30 | return t.empty() ? "dialog" : t; |
| 31 | } |
| 32 | |
| 33 | bool LayoutWidget::hasParent() |
| 34 | { |
| 35 | return _prop->getParent() ? true : false; |
| 36 | } |
| 37 | |
| 38 | LayoutWidget LayoutWidget::parent() |
| 39 | { |
| 40 | return LayoutWidget(_prop->getParent()); |
| 41 | } |
| 42 | |
| 43 | int LayoutWidget::nChildren() |
| 44 | { |
| 45 | // Hack: assume that any non-leaf nodes but "hrule" and "vrule" |
| 46 | // are widgets... |
| 47 | int n = 0; |
| 48 | for(int i=0; i<_prop->nChildren(); i++) { |
| 49 | SGPropertyNode* p = _prop->getChild(i); |
| 50 | std::string name = p->getNameString(); |
| 51 | if (p->nChildren() != 0 || name == "hrule" || name == "vrule") |
| 52 | n++; |
| 53 | } |
| 54 | return n; |
| 55 | } |
| 56 | |
| 57 | LayoutWidget LayoutWidget::getChild(int idx) |
| 58 | { |
| 59 | // Same hack. Note that access is linear time in the number of |
| 60 | // children... |
| 61 | int n = 0; |
| 62 | for(int i=0; i<_prop->nChildren(); i++) { |
| 63 | SGPropertyNode* p = _prop->getChild(i); |
| 64 | std::string name = p->getNameString(); |
| 65 | if (p->nChildren() != 0 || name == "hrule" || name == "vrule") { |
| 66 | if(idx == n) return LayoutWidget(p); |
| 67 | n++; |
| 68 | } |
| 69 | } |
| 70 | return LayoutWidget(0); |
| 71 | } |
| 72 | |
| 73 | bool LayoutWidget::hasField(const char* f) |
| 74 | { |
| 75 | return _prop->hasChild(f); |
| 76 | } |
| 77 | |
| 78 | int LayoutWidget::getNum(const char* f) |
| 79 | { |
| 80 | return _prop->getIntValue(f); |
| 81 | } |
| 82 | |
| 83 | bool LayoutWidget::getBool(const char* f, bool dflt) |
| 84 | { |
| 85 | return _prop->getBoolValue(f, dflt); |
| 86 | } |
| 87 | |
| 88 | std::string LayoutWidget::getStr(const char* f) |
| 89 | { |
| 90 | return _prop->getStringValue(f); |
| 91 | } |
| 92 | |
| 93 | void LayoutWidget::setNum(const char* f, int num) |
| 94 | { |
| 95 | _prop->setIntValue(f, num); |
| 96 | } |
| 97 |