Debian Codegraph @

src/widget/Labels.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "Labels.h"
10
11 #include "Path.h"
12 #include "ResDialogPack.h"
13 #include "Dialog.h"
14 #include "ScriptState.h"
15 #include "def-script.h"
16
17 //-----------------------------------------------------------------
18 inline Labels *
19 getLabels(lua_State *L)
20 {
21 return dynamic_cast<Labels*>(script_getLeader(L));
22 }
23 //-----------------------------------------------------------------
24 /**
25 * void label_text(labelName, lang, text)
26 */
27 int
28 script_label_text(lua_State *L) throw()
29 {
30 BEGIN_NOEXCEPTION;
31 const char *labelName = luaL_checkstring(L, 1);
32 const char *lang = luaL_checkstring(L, 2);
33 const char *text = luaL_checkstring(L, 3);
34
35 getLabels(L)->addLabel(labelName, lang, text);
36 END_NOEXCEPTION;
37 return 0;
38 }
39
40 //-----------------------------------------------------------------
41 Labels::Labels(const Path &source)
42 {
43 m_labels = new ResDialogPack();
44 m_script->registerFunc("label_text", script_label_text);
45 scriptInclude(source);
46 }
47 //-----------------------------------------------------------------
48 Labels::~Labels()
49 {
50 m_labels->removeAll();
51 delete m_labels;
52 }
53 //-----------------------------------------------------------------
54 void
55 Labels::addLabel(const std::string &name, const std::string &lang,
56 const std::string &text)
57 {
58 m_labels->addRes(name, new Dialog(lang, "", text));
59 }
60 //-----------------------------------------------------------------
61 std::string
62 Labels::getLabel(const std::string &name) const
63 {
64 std::string result;
65 const Dialog *dialog = m_labels->findDialogHard(name);
66 if (dialog) {
67 result = dialog->getSubtitle();
68 }
69 else {
70 result = "???";
71 }
72 return result;
73 }
74 //-----------------------------------------------------------------
75 std::string
76 Labels::getFormatedLabel(const std::string &name,
77 const StringTool::t_args &args) const
78 {
79 std::string result;
80 const Dialog *dialog = m_labels->findDialogHard(name);
81 if (dialog) {
82 result = dialog->getFormatedSubtitle(args);
83 }
84 else {
85 result = "???";
86 }
87 return result;
88 }
89