diff options
Diffstat (limited to 'Dalos')
-rw-r--r-- | Dalos/Dalos.cc | 716 | ||||
-rw-r--r-- | Dalos/Hexview.cc | 400 |
2 files changed, 558 insertions, 558 deletions
diff --git a/Dalos/Dalos.cc b/Dalos/Dalos.cc index b6e6a75..4a045ef 100644 --- a/Dalos/Dalos.cc +++ b/Dalos/Dalos.cc @@ -1,358 +1,358 @@ -/*
- * Dalos
- * Copyright (C) 2003-2005 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: Dalos.cc,v 1.14 2004-12-29 01:58:33 pixel Exp $ */
-
-#include <SDL.h>
-#include <SDL_thread.h>
-
-#include <vector>
-
-#include <Main.h>
-#include <Handle.h>
-#include <Input.h>
-#include <Buffer.h>
-#include <BLua.h>
-#include <LuaHandle.h>
-
-#include <engine.h>
-#include <glbase.h>
-#include <glshape.h>
-#include <font.h>
-
-#include <luacd.h>
-#include <luapsx.h>
-
-#include <Console.h>
-#include <Hexview.h>
-
-#include "cd-tool-hc.h"
-
-#ifdef __MINGW32__
-#define main SDL_main
-#endif
-
-
-// all these global variables are ugly...
-bool auto_exec = true;
-bool lua_started = false;
-bool do_lua_break = false;
-
-CODE_BEGINS
-
-mogltk::widgets::Root * Root;
-mogltk::widget * Frame, * MainPanel;
-mogltk::widgets::Menu * MainMenu;
-
-class threaded_Lua : public Lua {
- public:
- threaded_Lua() : mutex(SDL_CreateMutex()) { }
- virtual ~threaded_Lua() { SDL_DestroyMutex(mutex); }
- virtual void lock() {
- SDL_mutexP(mutex);
- }
- virtual void unlock() {
- SDL_mutexV(mutex);
- }
- private:
- SDL_mutex * mutex;
-};
-
-class threaded_locker : public locker_t {
- public:
- threaded_locker() : mutex(SDL_CreateMutex()) { }
- virtual ~threaded_locker() { SDL_DestroyMutex(mutex); }
- virtual void lock() {
- SDL_mutexP(mutex);
- }
- virtual void unlock() {
- SDL_mutexV(mutex);
- }
- private:
- SDL_mutex * mutex;
-};
-
-static int lua_print(lua_State * _L) {
- Lua * L = Lua::find(_L);
- String t = L->tostring() + "\n";
- char * tc = t.strdup();
-
- printm(M_STATUS, "%s", tc);
-
- free(tc);
-
- return 0;
-}
-
-static void lua_hook(lua_State * _L, lua_Debug * ar) {
- if (!lua_started)
- return;
- Lua * L = Lua::find(_L);
-
- if (do_lua_break) {
- L->do_break();
- do_lua_break = false;
- }
-}
-
-void start_lua() {
- Lua * L = new threaded_Lua();
- bool use_builtin_cdtool = false;
- int i;
-
- L->open_base();
- L->open_math();
- L->open_string();
- L->open_table();
-
- LuaInput::pushconstruct(L);
- LuaOutput::pushconstruct(L);
- LuaBuffer::pushconstruct(L);
-
- CD_PUSHSTATICS(L);
- Luapsx::pushstatics(L);
-
- L->push("print");
- L->push(lua_print);
- L->setvar();
-
- L->sethook(lua_hook, LUA_MASKLINE, 0);
-
- console_lock = SDL_CreateMutex();
- console_sem = SDL_CreateSemaphore(0);
-
- try {
- L->load(&Input("cd-tool.lua"));
- } catch (GeneralException e) {
- printm(M_WARNING, "There was an error loading cd-tool.lua: %s, using built-in.\n", e.GetMsg());
- use_builtin_cdtool = true;
- }
-
- if (use_builtin_cdtool) {
- Buffer built;
- built.write(cd_tool_lua, cd_tool_lua_size);
- try {
- L->load(&built);
- }
- catch (GeneralException e) {
- printm(M_WARNING, "There was an error loading built-in cd-tool.lua: %s\n", e.GetMsg());
- }
- }
-
- SDL_CreateThread(LuaThread, L);
-}
-
-static int LuaThread(void * d) {
- Lua * L = (Lua *) d;
- while (true) {
- SDL_SemWait(console_sem);
- lua_started = true;
- SDL_mutexP(console_lock);
- try {
- L->load(&console_buffer);
- }
- catch (LuaException e) {
- /* If there was an error, ignore it, and free the stack */
- while(L->gettop())
- L->pop();
- }
- catch (GeneralException e) {
- /* A more severe exception... */
- while(L->gettop())
- L->pop();
- printm(M_ERROR, "Aborted. LUA caused the following exception: %s\n", e.GetMsg());
- }
- SDL_mutexV(console_lock);
- lua_started = false;
- }
-}
-
-class myprinter : public printer_t {
- public:
- myprinter() : lock(SDL_CreateMutex()) { }
- virtual ~myprinter() { SDL_DestroyMutex(lock); }
- virtual bool printm(int level, const char * m, va_list ap) {
- static String heads[] = {"EE", "--", "WW", "II"};
- static char buffer[20480];
-
- if (level >= M_INFO)
- return true;
-
- SDL_mutexP(lock);
-
- vsnprintf(buffer, 20479, m, ap);
- if (level >= 0)
- CurrentConsole->add_line("(" + heads[level] + ") " + buffer);
- else
- CurrentConsole->add_line(buffer);
-
- SDL_mutexV(lock);
-
- return true;
- }
- private:
- SDL_mutex * lock;
-};
-
-class frame : public mogltk::widget {
- public:
- frame(mogltk::shape * sh, mogltk::widget * father) :
- widget(father, 2, 2, father->GetW() - 4, father->GetH() - 4, 0, "MyFrame", sh) { }
- protected:
- virtual void draw() {
- }
- virtual mogltk::rect GetDrawRect() {
- mogltk::rect r;
-
- r.x = GetX() + 2;
- r.y = GetY() + 2;
- r.w = GetW() - 4;
- r.h = GetH() - 4;
-
- return r;
- }
- virtual bool process_event(int mx, int my, mogltk::event_t event) {
- mx -= GetAX();
- my -= GetAY();
- return false;
- }
- virtual void resize_notify() {
- resize(Father()->GetW() - 4, Father()->GetH() - 4);
- }
-};
-
-class timer : public mogltk::widget {
- public:
- timer() :
- widget(Application->Root, 0, 0, 0, 0, 0, "Timer", 0), tick(0)
- { set_timed_event(100); }
- protected:
- virtual bool process_event(int, int, mogltk::event_t event) {
- if (event == mogltk::E_TIMER) {
- set_timed_event(100);
- tick = (tick + 1) % 4;
- switch (tick) {
- case 0:
- Application->MainMenu->SetCaption(0, "/");
- break;
- case 1:
- Application->MainMenu->SetCaption(0, "-");
- break;
- case 2:
- Application->MainMenu->SetCaption(0, "\\");
- break;
- case 3:
- Application->MainMenu->SetCaption(0, "I");
- break;
- }
- Application->MainMenu->SetCaption(3, String("FPS: ") + mogltk::engine::FPS());
- return true;
- }
- return false;
- }
- private:
- int tick;
-};
-
-class quit : public mogltk::widgets::action {
- public:
- virtual void do_action(mogltk::widget * w) {
- mogltk::engine::quit();
- }
-} action_quit;
-
-class about : public mogltk::widgets::action {
- public:
- virtual void do_action(mogltk::widget * w) {
- new mogltk::widgets::MsgBox(w->Shaper(), w->Father(), "About...",
- "Dalos version 0.1 - OpenGL version\n"
- "Copyright (C) 2004 Nicolas \"Pixel\" Noble\n"
- "\n"
- "Thanks and greetings fly to (no particular order)\n"
- "GreatSkaori, Orphis, Ti Dragon, Yaz0r, S-O-R,\n"
- "Meradrin, SkeuD, Moogle, InVerse, LavosSpawn\n"
- "\n"
- "And to all I forgot!\n"
- );
- }
-} about_dialog;
-
-virtual int startup() throw (GeneralException) {
- verbosity = M_INFO;
- try {
- new Archive(argv[0], ARCHIVE_EXECUTABLE);
- }
- catch (...) {
- new Archive("Dalos.paq");
- }
-
- mogltk::widgets::ContextMenu * c;
- mogltk::base * gl = new mogltk::glbase();
- mogltk::shape * sh = new mogltk::glshape();
-
- SDL_EnableKeyRepeat(250, 40);
-
- mogltk::engine::setcursorvisible(true);
- mogltk::engine::setappactive(true);
-
- Root = new mogltk::widgets::Root(sh);
- MainPanel = Root->InnerPanel();
- MainMenu = new mogltk::widgets::Menu(sh, MainPanel);
- Frame = new frame(sh, new mogltk::widgets::Frame(sh, MainPanel, 0, MainMenu->GetH(), Root->GetW() - 1, Root->GetH() - MainMenu->GetH() - 1));
-
- mogltk::widget * box = new mogltk::widgets::SmartBox(sh, MainPanel, 50, 50, 500, 400, "Hexview");
- (new hexview(sh, box->InnerPanel()))->bind_handle(new Input(argv[0]));
-
- CurrentConsole = new console(sh, Root, 0, 8);
- console::create_console_thread();
- CurrentConsole->move(0, Root->GetH() - CurrentConsole->GetH());
- CurrentConsole->add_line("Dalos v0.1 - LUA console - Press ESC to show/hide it.");
- CurrentConsole->SetVisible(true);
-
- printer = new myprinter();
- locker = new threaded_locker();
-
- start_lua();
-
- // Filling menu.
- MainMenu->addnode("/", 0);
-
- c = MainMenu->createsub("File");
- c->addnode("Quit", &action_quit);
-
- c = MainMenu->createsub("Help");
- c->addnode("About", &about_dialog);
-
- MainMenu->addnode("FPS:", 0);
-
- new timer();
-
- // Setting up the key event handlers
- new hexview::hexview_keyevent;
- new console::console_keyevent; // Should be one of the last
-
- // And launching the main loop
- Root->mainloop();
-
- // Should cleanup here... nevermind ;)
-
- return 0;
-}
-
-CODE_ENDS
+/* + * Dalos + * Copyright (C) 2003-2005 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: Dalos.cc,v 1.15 2005-02-24 11:30:04 pixel Exp $ */ + +#include <SDL.h> +#include <SDL_thread.h> + +#include <vector> + +#include <Main.h> +#include <Handle.h> +#include <Input.h> +#include <Buffer.h> +#include <BLua.h> +#include <LuaHandle.h> + +#include <engine.h> +#include <glbase.h> +#include <glshape.h> +#include <font.h> + +#include <luacd.h> +#include <luapsx.h> + +#include <Console.h> +#include <Hexview.h> + +#include "cd-tool-hc.h" + +#ifdef __MINGW32__ +#define main SDL_main +#endif + + +// all these global variables are ugly... +bool auto_exec = true; +bool lua_started = false; +bool do_lua_break = false; + +CODE_BEGINS + +mogltk::widgets::Root * Root; +mogltk::widget * Frame, * MainPanel; +mogltk::widgets::Menu * MainMenu; + +class threaded_Lua : public Lua { + public: + threaded_Lua() : mutex(SDL_CreateMutex()) { } + virtual ~threaded_Lua() { SDL_DestroyMutex(mutex); } + virtual void lock() { + SDL_mutexP(mutex); + } + virtual void unlock() { + SDL_mutexV(mutex); + } + private: + SDL_mutex * mutex; +}; + +class threaded_locker : public locker_t { + public: + threaded_locker() : mutex(SDL_CreateMutex()) { } + virtual ~threaded_locker() { SDL_DestroyMutex(mutex); } + virtual void lock() { + SDL_mutexP(mutex); + } + virtual void unlock() { + SDL_mutexV(mutex); + } + private: + SDL_mutex * mutex; +}; + +static int lua_print(lua_State * _L) { + Lua * L = Lua::find(_L); + String t = L->tostring() + "\n"; + char * tc = t.strdup(); + + printm(M_STATUS, "%s", tc); + + free(tc); + + return 0; +} + +static void lua_hook(lua_State * _L, lua_Debug * ar) { + if (!lua_started) + return; + Lua * L = Lua::find(_L); + + if (do_lua_break) { + L->do_break(); + do_lua_break = false; + } +} + +void start_lua() { + Lua * L = new threaded_Lua(); + bool use_builtin_cdtool = false; + int i; + + L->open_base(); + L->open_math(); + L->open_string(); + L->open_table(); + + LuaInput::pushconstruct(L); + LuaOutput::pushconstruct(L); + LuaBuffer::pushconstruct(L); + + CD_PUSHSTATICS(L); + Luapsx::pushstatics(L); + + L->push("print"); + L->push(lua_print); + L->setvar(); + + L->sethook(lua_hook, LUA_MASKLINE, 0); + + console_lock = SDL_CreateMutex(); + console_sem = SDL_CreateSemaphore(0); + + try { + L->load(&Input("cd-tool.lua")); + } catch (GeneralException e) { + printm(M_WARNING, "There was an error loading cd-tool.lua: %s, using built-in.\n", e.GetMsg()); + use_builtin_cdtool = true; + } + + if (use_builtin_cdtool) { + Buffer built; + built.write(cd_tool_lua, cd_tool_lua_size); + try { + L->load(&built); + } + catch (GeneralException e) { + printm(M_WARNING, "There was an error loading built-in cd-tool.lua: %s\n", e.GetMsg()); + } + } + + SDL_CreateThread(LuaThread, L); +} + +static int LuaThread(void * d) { + Lua * L = (Lua *) d; + while (true) { + SDL_SemWait(console_sem); + lua_started = true; + SDL_mutexP(console_lock); + try { + L->load(&console_buffer); + } + catch (LuaException e) { + /* If there was an error, ignore it, and free the stack */ + while(L->gettop()) + L->pop(); + } + catch (GeneralException e) { + /* A more severe exception... */ + while(L->gettop()) + L->pop(); + printm(M_ERROR, "Aborted. LUA caused the following exception: %s\n", e.GetMsg()); + } + SDL_mutexV(console_lock); + lua_started = false; + } +} + +class myprinter : public printer_t { + public: + myprinter() : lock(SDL_CreateMutex()) { } + virtual ~myprinter() { SDL_DestroyMutex(lock); } + virtual bool printm(int level, const char * m, va_list ap) { + static String heads[] = {"EE", "--", "WW", "II"}; + static char buffer[20480]; + + if (level >= M_INFO) + return true; + + SDL_mutexP(lock); + + vsnprintf(buffer, 20479, m, ap); + if (level >= 0) + CurrentConsole->add_line("(" + heads[level] + ") " + buffer); + else + CurrentConsole->add_line(buffer); + + SDL_mutexV(lock); + + return true; + } + private: + SDL_mutex * lock; +}; + +class frame : public mogltk::widget { + public: + frame(mogltk::shape * sh, mogltk::widget * father) : + widget(father, 2, 2, father->GetW() - 4, father->GetH() - 4, 0, "MyFrame", sh) { } + protected: + virtual void draw() { + } + virtual mogltk::rect GetDrawRect() { + mogltk::rect r; + + r.x = GetX() + 2; + r.y = GetY() + 2; + r.w = GetW() - 4; + r.h = GetH() - 4; + + return r; + } + virtual bool process_event(int mx, int my, mogltk::event_t event) { + mx -= GetAX(); + my -= GetAY(); + return false; + } + virtual void resize_notify() { + resize(Father()->GetW() - 4, Father()->GetH() - 4); + } +}; + +class timer : public mogltk::widget { + public: + timer() : + widget(Application->Root, 0, 0, 0, 0, 0, "Timer", 0), tick(0) + { set_timed_event(100); } + protected: + virtual bool process_event(int, int, mogltk::event_t event) { + if (event == mogltk::E_TIMER) { + set_timed_event(100); + tick = (tick + 1) % 4; + switch (tick) { + case 0: + Application->MainMenu->SetCaption(0, "/"); + break; + case 1: + Application->MainMenu->SetCaption(0, "-"); + break; + case 2: + Application->MainMenu->SetCaption(0, "\\"); + break; + case 3: + Application->MainMenu->SetCaption(0, "I"); + break; + } + Application->MainMenu->SetCaption(3, String("FPS: ") + mogltk::engine::FPS()); + return true; + } + return false; + } + private: + int tick; +}; + +class quit : public mogltk::widgets::action { + public: + virtual void do_action(mogltk::widget * w) { + mogltk::engine::quit(); + } +} action_quit; + +class about : public mogltk::widgets::action { + public: + virtual void do_action(mogltk::widget * w) { + new mogltk::widgets::MsgBox(w->Shaper(), w->Father(), "About...", + "Dalos version 0.1 - OpenGL version\n" + "Copyright (C) 2004 Nicolas \"Pixel\" Noble\n" + "\n" + "Thanks and greetings fly to (no particular order)\n" + "GreatSkaori, Orphis, Ti Dragon, Yaz0r, S-O-R,\n" + "Meradrin, SkeuD, Moogle, InVerse, LavosSpawn\n" + "\n" + "And to all I forgot!\n" + ); + } +} about_dialog; + +virtual int startup() throw (GeneralException) { + verbosity = M_INFO; + try { + new Archive(argv[0], ARCHIVE_EXECUTABLE); + } + catch (...) { + new Archive("Dalos.paq"); + } + + mogltk::widgets::ContextMenu * c; + mogltk::base * gl = new mogltk::glbase(); + mogltk::shape * sh = new mogltk::glshape(); + + SDL_EnableKeyRepeat(250, 40); + + mogltk::engine::setcursorvisible(true); + mogltk::engine::setappactive(true); + + Root = new mogltk::widgets::Root(sh); + MainPanel = Root->InnerPanel(); + MainMenu = new mogltk::widgets::Menu(sh, MainPanel); + Frame = new frame(sh, new mogltk::widgets::Frame(sh, MainPanel, 0, MainMenu->GetH(), Root->GetW() - 1, Root->GetH() - MainMenu->GetH() - 1)); + + mogltk::widget * box = new mogltk::widgets::SmartBox(sh, MainPanel, 50, 50, 500, 400, "Hexview"); + (new hexview(sh, box->InnerPanel()))->bind_handle(new Input(argv[0])); + + CurrentConsole = new console(sh, Root, 0, 8); + console::create_console_thread(); + CurrentConsole->move(0, Root->GetH() - CurrentConsole->GetH()); + CurrentConsole->add_line("Dalos v0.1 - LUA console - Press ESC to show/hide it."); + CurrentConsole->SetVisible(true); + + printer = new myprinter(); + locker = new threaded_locker(); + + start_lua(); + + // Filling menu. + MainMenu->addnode("/", 0); + + c = MainMenu->createsub("File"); + c->addnode("Quit", &action_quit); + + c = MainMenu->createsub("Help"); + c->addnode("About", &about_dialog); + + MainMenu->addnode("FPS:", 0); + + new timer(); + + // Setting up the key event handlers + new hexview::hexview_keyevent; + new console::console_keyevent; // Should be one of the last + + // And launching the main loop + Root->mainloop(); + + // Should cleanup here... nevermind ;) + + return 0; +} + +CODE_ENDS diff --git a/Dalos/Hexview.cc b/Dalos/Hexview.cc index 0b3fc28..0519526 100644 --- a/Dalos/Hexview.cc +++ b/Dalos/Hexview.cc @@ -1,200 +1,200 @@ -/*
- * Dalos
- * Copyright (C) 2003-2005 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: Hexview.cc,v 1.1 2004-12-26 03:29:10 pixel Exp $ */
-
-#include <SDL.h>
-
-#include <font.h>
-
-#include "Hexview.h"
-
-void hexview::hexview_keyevent::down(SDL_keysym k) {
- if (!CurrentHexview || !CurrentHexview->GetVisible()) {
- if (old_handler)
- old_handler->down(k);
- return;
- }
- switch (k.sym) {
- case SDLK_DOWN:
- CurrentHexview->change_offset(CurrentHexview->get_offset() + CurrentHexview->get_width());
- break;
- case SDLK_UP:
- CurrentHexview->change_offset(CurrentHexview->get_offset() - CurrentHexview->get_width());
- break;
- case SDLK_RIGHT:
- if (KMOD_ALT & k.mod) {
- CurrentHexview->change_width(CurrentHexview->get_width() + 1);
- } else if (KMOD_CTRL & k.mod) {
- CurrentHexview->change_shift(CurrentHexview->get_shift() - 1);
- } else {
- CurrentHexview->change_offset(CurrentHexview->get_offset() + 1);
- }
- break;
- case SDLK_LEFT:
- if (KMOD_ALT & k.mod) {
- CurrentHexview->change_width(CurrentHexview->get_width() - 1);
- } else if (KMOD_CTRL & k.mod) {
- CurrentHexview->change_shift(CurrentHexview->get_shift() + 1);
- } else {
- CurrentHexview->change_offset(CurrentHexview->get_offset() - 1);
- }
- break;
- case SDLK_PAGEDOWN:
- CurrentHexview->change_offset(CurrentHexview->get_offset() + CurrentHexview->get_width() * CurrentHexview->get_nlines());
- break;
- case SDLK_PAGEUP:
- CurrentHexview->change_offset(CurrentHexview->get_offset() - CurrentHexview->get_width() * CurrentHexview->get_nlines());
- break;
- case SDLK_HOME:
- CurrentHexview->change_offset(0);
- break;
- case SDLK_END:
- CurrentHexview->change_offset(CurrentHexview->get_size() - 1);
- break;
- default:
- if (old_handler)
- old_handler->down(k);
- return;
- }
-}
-
-void hexview::hexview_keyevent::up(SDL_keysym k) {
- if (old_handler)
- old_handler->up(k);
-}
-
-hexview::hexview(mogltk::shape * sh, mogltk::widget * father) :
- widget(father, 0, 0, father->GetW(), father->GetH(), 0, "hexview", sh), h(0), width(16), offset(0), offset_loaded(-1), size_loaded(0), data(0), virtual_base(0), shift(0)
-{
- nlines = GetH() / 13; CurrentHexview = this;
-}
-
-hexview::~hexview() {
- free(data);
- CurrentHexview = 0;
-}
-
-void hexview::set_virtual_base(int _virtual_base) {
- virtual_base = _virtual_base;
-}
-
-int hexview::get_nlines() {
- return nlines;
-}
-
-int hexview::get_size() {
- if (!h)
- return 0;
- return h->GetSize();
-}
-
-void hexview::change_width(int _width) {
- if (_width <= 0)
- _width = 1;
- if (width != _width) {
- free(data);
- width = _width;
- }
-}
-
-int hexview::get_width() {
- return width;
-}
-
-void hexview::change_offset(int _offset) {
- if ((_offset < 0) || !h)
- _offset = 0;
- if (h && (_offset >= h->GetSize()))
- _offset = h->GetSize() - 1;
- offset = _offset;
-}
-
-int hexview::get_offset() {
- return offset;
-}
-
-void hexview::change_shift(int _shift) {
- if (_shift < 0)
- _shift = 0;
- shift = _shift;
-}
-
-int hexview::get_shift() {
- return shift;
-}
-
-void hexview::bind_handle(Handle * _h) {
- h = _h;
-}
-
-Uint8 * hexview::get_data() {
- if (!data) {
- data = (Uint8 *) malloc(nlines * width);
- }
-
- if ((offset_loaded == offset) && (size_loaded == (nlines * width)))
- return data;
-
- h->seek(offset);
- h->read(data, nlines * width);
-
- offset_loaded = offset;
- size_loaded = nlines * width;
-
- return data;
-}
-
-void hexview::draw() {
- int i, max_o, j, k, start_o, l, c;
-
- if (!h)
- return;
-
- get_data();
-
- mogltk::FixedFont->setcolor(WHITE);
- mogltk::FixedFont->putcursor(GetAX() - shift * 6, GetAY());
-
- max_o = min(h->GetSize(), offset + nlines * width);
-
- for (i = start_o = offset, j = 0, l = 0; i < max_o; i++) {
- if ((j % width) == 0) {
- mogltk::FixedFont->printf("%08X ", i + virtual_base);
- }
- mogltk::FixedFont->printf("%02X ", data[j]);
- j++;
- if ((j % width) == 0) {
- for (k = start_o, c = 0; k < start_o + width; k++, c++) {
- mogltk::FixedFont->putcursor(GetAX() + (c + width * 3 + 14 - shift) * 6, GetAY() + l * 13);
- if (data[j - width + c] >= 0x20)
- // Have better font support here...
- mogltk::FixedFont->putentry(data[j - width + c] - 0x20);
- }
- l++;
- mogltk::FixedFont->putcursor(GetAX() - shift * 6, GetAY() + l * 13);
- start_o = i;
- }
- }
-}
-
-void hexview::resize_notify() {
- nlines = Father()->GetH() / 13;
- resize(Father()->GetW(), Father()->GetH());
-}
+/* + * Dalos + * Copyright (C) 2003-2005 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: Hexview.cc,v 1.2 2005-02-24 11:30:04 pixel Exp $ */ + +#include <SDL.h> + +#include <font.h> + +#include "Hexview.h" + +void hexview::hexview_keyevent::down(SDL_keysym k) { + if (!CurrentHexview || !CurrentHexview->GetVisible()) { + if (old_handler) + old_handler->down(k); + return; + } + switch (k.sym) { + case SDLK_DOWN: + CurrentHexview->change_offset(CurrentHexview->get_offset() + CurrentHexview->get_width()); + break; + case SDLK_UP: + CurrentHexview->change_offset(CurrentHexview->get_offset() - CurrentHexview->get_width()); + break; + case SDLK_RIGHT: + if (KMOD_ALT & k.mod) { + CurrentHexview->change_width(CurrentHexview->get_width() + 1); + } else if (KMOD_CTRL & k.mod) { + CurrentHexview->change_shift(CurrentHexview->get_shift() - 1); + } else { + CurrentHexview->change_offset(CurrentHexview->get_offset() + 1); + } + break; + case SDLK_LEFT: + if (KMOD_ALT & k.mod) { + CurrentHexview->change_width(CurrentHexview->get_width() - 1); + } else if (KMOD_CTRL & k.mod) { + CurrentHexview->change_shift(CurrentHexview->get_shift() + 1); + } else { + CurrentHexview->change_offset(CurrentHexview->get_offset() - 1); + } + break; + case SDLK_PAGEDOWN: + CurrentHexview->change_offset(CurrentHexview->get_offset() + CurrentHexview->get_width() * CurrentHexview->get_nlines()); + break; + case SDLK_PAGEUP: + CurrentHexview->change_offset(CurrentHexview->get_offset() - CurrentHexview->get_width() * CurrentHexview->get_nlines()); + break; + case SDLK_HOME: + CurrentHexview->change_offset(0); + break; + case SDLK_END: + CurrentHexview->change_offset(CurrentHexview->get_size() - 1); + break; + default: + if (old_handler) + old_handler->down(k); + return; + } +} + +void hexview::hexview_keyevent::up(SDL_keysym k) { + if (old_handler) + old_handler->up(k); +} + +hexview::hexview(mogltk::shape * sh, mogltk::widget * father) : + widget(father, 0, 0, father->GetW(), father->GetH(), 0, "hexview", sh), h(0), width(16), offset(0), offset_loaded(-1), size_loaded(0), data(0), virtual_base(0), shift(0) +{ + nlines = GetH() / 13; CurrentHexview = this; +} + +hexview::~hexview() { + free(data); + CurrentHexview = 0; +} + +void hexview::set_virtual_base(int _virtual_base) { + virtual_base = _virtual_base; +} + +int hexview::get_nlines() { + return nlines; +} + +int hexview::get_size() { + if (!h) + return 0; + return h->GetSize(); +} + +void hexview::change_width(int _width) { + if (_width <= 0) + _width = 1; + if (width != _width) { + free(data); + width = _width; + } +} + +int hexview::get_width() { + return width; +} + +void hexview::change_offset(int _offset) { + if ((_offset < 0) || !h) + _offset = 0; + if (h && (_offset >= h->GetSize())) + _offset = h->GetSize() - 1; + offset = _offset; +} + +int hexview::get_offset() { + return offset; +} + +void hexview::change_shift(int _shift) { + if (_shift < 0) + _shift = 0; + shift = _shift; +} + +int hexview::get_shift() { + return shift; +} + +void hexview::bind_handle(Handle * _h) { + h = _h; +} + +Uint8 * hexview::get_data() { + if (!data) { + data = (Uint8 *) malloc(nlines * width); + } + + if ((offset_loaded == offset) && (size_loaded == (nlines * width))) + return data; + + h->seek(offset); + h->read(data, nlines * width); + + offset_loaded = offset; + size_loaded = nlines * width; + + return data; +} + +void hexview::draw() { + int i, max_o, j, k, start_o, l, c; + + if (!h) + return; + + get_data(); + + mogltk::FixedFont->setcolor(WHITE); + mogltk::FixedFont->putcursor(GetAX() - shift * 6, GetAY()); + + max_o = min(h->GetSize(), offset + nlines * width); + + for (i = start_o = offset, j = 0, l = 0; i < max_o; i++) { + if ((j % width) == 0) { + mogltk::FixedFont->printf("%08X ", i + virtual_base); + } + mogltk::FixedFont->printf("%02X ", data[j]); + j++; + if ((j % width) == 0) { + for (k = start_o, c = 0; k < start_o + width; k++, c++) { + mogltk::FixedFont->putcursor(GetAX() + (c + width * 3 + 14 - shift) * 6, GetAY() + l * 13); + if (data[j - width + c] >= 0x20) + // Have better font support here... + mogltk::FixedFont->putentry(data[j - width + c] - 0x20); + } + l++; + mogltk::FixedFont->putcursor(GetAX() - shift * 6, GetAY() + l * 13); + start_o = i; + } + } +} + +void hexview::resize_notify() { + nlines = Father()->GetH() / 13; + resize(Father()->GetW(), Father()->GetH()); +} |