Sort of working log window.
[dcpomatic.git] / src / tools / servomatic_gui.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/thread.hpp>
21 #include <wx/taskbar.h>
22 #include <wx/icon.h>
23 #include "wx_util.h"
24 #include "lib/util.h"
25 #include "lib/server.h"
26
27 using namespace std;
28 using namespace boost;
29
30 enum {
31         ID_status = 1,
32         ID_quit,
33         ID_timer
34 };
35
36 class MemoryLog : public Log
37 {
38 public:
39
40         string get () const {
41                 boost::mutex::scoped_lock (_mutex);
42                 return _log;
43         }
44
45 private:
46         void do_log (string m)
47         {
48                 _log = m;
49         }
50
51         string _log;    
52 };
53
54 static MemoryLog memory_log;
55
56 class StatusDialog : public wxDialog
57 {
58 public:
59         StatusDialog ()
60                 : wxDialog (0, wxID_ANY, _("DVD-o-matic encode server"), wxDefaultPosition, wxSize (600, 40), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
61                 , _timer (this, ID_timer)
62         {
63                 _sizer = new wxFlexGridSizer (1, 6, 6);
64                 _sizer->AddGrowableCol (0, 1);
65
66                 _text = new wxTextCtrl (this, wxID_ANY);
67                 _sizer->Add (_text, 1, wxEXPAND);
68
69                 SetSizer (_sizer);
70                 _sizer->Layout ();
71
72                 Connect (ID_timer, wxEVT_TIMER, wxTimerEventHandler (StatusDialog::update));
73                 _timer.Start (1000);
74         }
75
76 private:
77         void update (wxTimerEvent &)
78         {
79                 _text->ChangeValue (std_to_wx (memory_log.get ()));
80                 _sizer->Layout ();
81         }
82
83         wxFlexGridSizer* _sizer;
84         wxTextCtrl* _text;
85         wxTimer _timer;
86 };
87
88 class TaskBarIcon : public wxTaskBarIcon
89 {
90 public:
91         TaskBarIcon ()
92         {
93                 wxIcon icon (std_to_wx ("taskbar_icon"));
94                 SetIcon (icon, std_to_wx ("DVD-o-matic encode server"));
95
96                 Connect (ID_status, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::status));
97                 Connect (ID_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::quit));
98         }
99         
100         wxMenu* CreatePopupMenu ()
101         {
102                 wxMenu* menu = new wxMenu;
103                 menu->Append (ID_status, std_to_wx ("Status..."));
104                 menu->Append (ID_quit, std_to_wx ("Quit"));
105                 return menu;
106         }
107
108 private:
109         void status (wxCommandEvent &)
110         {
111                 StatusDialog* d = new StatusDialog;
112                 d->Show ();
113         }
114
115         void quit (wxCommandEvent &)
116         {
117                 wxTheApp->ExitMainLoop ();
118         }
119 };
120
121 class App : public wxApp
122 {
123 public:
124         App ()
125                 : wxApp ()
126                 , _thread (0)
127         {}
128
129 private:        
130         
131         bool OnInit ()
132         {
133                 dvdomatic_setup ();
134
135                 new TaskBarIcon;
136
137                 _thread = new thread (bind (&App::main_thread, this));
138                 return true;
139         }
140
141         void main_thread ()
142         {
143                 Server server (&memory_log);
144                 server.run ();
145         }
146
147         boost::thread* _thread;
148 };
149
150 IMPLEMENT_APP (App)