edit.cxx
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:17k
- /*
- * edit.cxx
- *
- * Resource Editor
- *
- * Portable Windows Library
- *
- * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
- *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is Portable Windows Library.
- *
- * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
- *
- * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
- * All Rights Reserved.
- *
- * Contributor(s): ______________________________________.
- *
- * $Log: edit.cxx,v $
- * Revision 1.3 1998/09/26 01:24:07 robertj
- * Added open source license
- *
- * Revision 1.2 1996/01/23 13:32:42 robertj
- * More changes for editor
- *
- * Revision 1.1 1995/12/23 03:52:17 robertj
- * Initial revision
- *
- */
- #include <pwlib.h>
- #include "pwrc.h"
- #include "splitter.h"
- #include "ansiterm.h"
- #include "resource.h"
- #if defined (YYDEBUG)
- extern int yydebug;
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for listing resources
- PDECLARE_CLASS(ResourceWindow, PMDIDocWindow)
- public:
- ResourceWindow(PMDIFrameWindow * parent,
- const PString & title, ResourceFile * res);
- ~ResourceWindow();
- private:
- virtual void OnResize(const PDim & newSize, ResizeType type);
- ResourceFile * resfile;
- PSplitter * splitter;
- PDECLARE_CLASS(ResTypeListBox, PListBox)
- ResTypeListBox(PInteractor *, const PNotifier &);
- virtual void OnDrawEntry(PINDEX index, PObject & obj, PCanvas & canvas,
- const PRect & rect, BOOL hasFocus, BOOL isSelected);
- virtual PDim OnMeasureEntry(PINDEX index,PObject & obj,PCanvas & canvas);
- ResourceList * GetResourceList() const;
- };
- PDECLARE_NOTIFIER(ResTypeListBox, ResourceWindow, OnSelectResType);
- ResTypeListBox * resType;
- PDECLARE_CLASS(ResourceListBox, PListBox)
- ResourceListBox(PInteractor *, const PNotifier &);
- virtual void OnDrawEntry(PINDEX index, PObject & obj, PCanvas & canvas,
- const PRect & rect, BOOL hasFocus, BOOL isSelected);
- virtual PDim OnMeasureEntry(PINDEX index,PObject & obj,PCanvas & canvas);
- ResourceObject * GetResourceObject() const;
- };
- PDECLARE_NOTIFIER(ResourceListBox, ResourceWindow, OnSelectResource);
- ResourceListBox * resIDs;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing a particular resource
- PDECLARE_CLASS(ResourceEditor, PMDIDocWindow)
- public:
- ResourceEditor(PMDIFrameWindow * parent,
- PRESOURCE_ID title, ResourceFile & file, ResourceObject & resource);
- protected:
- PComboBox * identifier;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing strings
- PDECLARE_CLASS(StringEditor, ResourceEditor)
- public:
- StringEditor(PMDIFrameWindow * parent,
- ResourceFile & file, StringResource & res);
- private:
- StringResource & resource;
- PMultiLineEditBox * string;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing menus
- PDECLARE_CLASS(MenuEditor, ResourceEditor)
- public:
- MenuEditor(PMDIFrameWindow * parent, ResourceFile & file, MenuBar & res);
- private:
- MenuBar & resource;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing layouts
- PDECLARE_CLASS(LayoutEditor, ResourceEditor)
- public:
- LayoutEditor(PMDIFrameWindow * parent, ResourceFile & file, Layout & res);
- private:
- Layout & resource;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing pixels
- PDECLARE_CLASS(PixelEditor, ResourceEditor)
- public:
- PixelEditor(PMDIFrameWindow * parent,
- ResourceFile & file, PixelResource & res);
- private:
- PixelResource & resource;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing arbitrary data resources
- PDECLARE_CLASS(DataEditor, ResourceEditor)
- public:
- DataEditor(PMDIFrameWindow * parent,
- ResourceFile & file, DataResource & res);
- private:
- DataResource & resource;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for listing errors
- PDECLARE_CLASS(ErrorWindow, PMDIDocWindow)
- public:
- ErrorWindow(PMDIFrameWindow * parent);
- ~ErrorWindow();
- private:
- virtual void OnResize(const PDim & newSize, ResizeType type);
- PScroller * scroller;
- PTerminal * terminal;
- ostream * oldError;
- PTerminalChannel channel;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // Main MDI frame window class
- PDECLARE_CLASS(MainWindow, PMDIFrameWindow)
- public:
- MainWindow(const PArgList & args);
- PDECLARE_COMMAND("Open", MainWindow, OpenCmd);
- PDECLARE_COMMAND("Exit", MainWindow, ExitCmd);
- private:
- virtual void OnResize(const PDim & newSize, ResizeType type);
- void OpenResFile(const PString & filename);
- ErrorWindow * error;
- friend ErrorWindow::~ErrorWindow();
- };
- #define new PNEW
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for listing resources
- ResourceWindow::ResourceWindow(PMDIFrameWindow * parent,
- const PString & title, ResourceFile * res)
- : PMDIDocWindow(parent, title),
- resfile(res)
- {
- // Set up the frame windows title, icon and menu
- SetIcon(PIcon(IDI_APPICON));
- splitter = new PVerticalSplitter(this);
- splitter->SetPercent(25);
- resType = new ResTypeListBox(splitter, PCREATE_NOTIFIER(OnSelectResType));
- resIDs = new ResourceListBox(splitter, PCREATE_NOTIFIER(OnSelectResource));
- for (PINDEX i = 0; i < ResourceList::NumResTypes; i++) {
- if (resfile->resources[i].GetSize() != 0)
- resType->AddEntry(&resfile->resources[i]);
- }
- ShowAll();
- }
- ResourceWindow::~ResourceWindow()
- {
- resIDs->DeleteAllEntries();
- resType->DeleteAllEntries();
- delete resfile;
- }
- void ResourceWindow::OnResize(const PDim & newSize, ResizeType type)
- {
- if (type != Iconic) {
- PRect bounds(newSize);
- splitter->AutoAdjustBounds(bounds);
- }
- }
- ResourceWindow::ResTypeListBox::ResTypeListBox(PInteractor * parent,
- const PNotifier & notifier)
- : PListBox(parent, PListBox::Sorted)
- {
- SetNotifier(notifier);
- DisallowDeleteObjects();
- }
- void ResourceWindow::ResTypeListBox::OnDrawEntry(PINDEX , PObject & obj,
- PCanvas & canvas, const PRect & rect, BOOL hasFocus, BOOL)
- {
- int resName[ResourceList::NumResTypes] = {
- IDS_STRING_RES, IDS_MENUBAR_RES, IDS_LAYOUT_RES, IDS_ICON_RES,
- IDS_CURSOR_RES, IDS_PATTERN_RES, IDS_IMAGE_RES, IDS_DATA_RES
- };
- PResourceString str = resName[((ResourceList &)obj).GetType()];
- canvas.DrawRect(rect);
- canvas.DrawTextLine(rect.X()+1, rect.Y(), str, NULL, str.GetLength());
- if (hasFocus)
- canvas.DrawFocusRect(rect);
- }
- PDim ResourceWindow::ResTypeListBox::OnMeasureEntry(PINDEX index,
- PObject & obj, PCanvas & canvas)
- {
- return PListBox::OnMeasureEntry(index, obj, canvas);
- }
- ResourceList * ResourceWindow::ResTypeListBox::GetResourceList() const
- {
- PINDEX sel = GetSelection();
- if (sel == P_MAX_INDEX)
- return NULL;
- return (ResourceList *)GetEntry(sel);
- }
- void ResourceWindow::OnSelectResType(ResTypeListBox & box, INT)
- {
- resIDs->DeleteAllEntries();
- ResourceList * resList = box.GetResourceList();
- if (resList != NULL) {
- for (PINDEX i = 0; i < resList->GetSize(); i++)
- resIDs->AddEntry(resList->GetAt(i));
- }
- }
- ResourceWindow::ResourceListBox::ResourceListBox(PInteractor * parent,
- const PNotifier & notifier)
- : PListBox(parent, PListBox::Sorted)
- {
- SetNotifier(notifier);
- DisallowDeleteObjects();
- }
- void ResourceWindow::ResourceListBox::OnDrawEntry(PINDEX, PObject & obj,
- PCanvas & canvas, const PRect & rect, BOOL hasFocus, BOOL)
- {
- PString str = ((ResourceObject&)obj).GetIDString();
- canvas.DrawRect(rect);
- canvas.DrawTextLine(rect.X()+1, rect.Y(), str, NULL, str.GetLength());
- if (hasFocus)
- canvas.DrawFocusRect(rect);
- }
- PDim ResourceWindow::ResourceListBox::OnMeasureEntry(PINDEX index,
- PObject & obj, PCanvas & canvas)
- {
- return PListBox::OnMeasureEntry(index, obj, canvas);
- }
- ResourceObject * ResourceWindow::ResourceListBox::GetResourceObject() const
- {
- PINDEX sel = GetSelection();
- if (sel == P_MAX_INDEX)
- return NULL;
- return (ResourceObject *)GetEntry(sel);
- }
- void ResourceWindow::OnSelectResource(ResourceListBox & box, INT code)
- {
- if (code == PListBox::DoubleClick) {
- ResourceObject * res = box.GetResourceObject();
- if (res != NULL)
- res->EditResource((MainWindow*)parent, resfile);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing a particular resource
- ResourceEditor::ResourceEditor(PMDIFrameWindow * parent,
- PRESOURCE_ID title, ResourceFile & file, ResourceObject & resource)
- : PMDIDocWindow(parent, PResourceString(title))
- {
- identifier = new PComboBox(this);
- identifier->SetPosition(10, 10);
- identifier->SetDimensions(150, 50);
- identifier->SetText(resource.GetIDString());
- for (PINDEX i = 0; i < file.GetNumDefines(); i++)
- identifier->AddString(file.GetDefineName(i));
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing strings
- StringEditor::StringEditor(PMDIFrameWindow * parent,
- ResourceFile & file, StringResource & res)
- : ResourceEditor(parent, IDS_STRING_EDITOR, file, res),
- resource(res)
- {
- string = new PMultiLineEditBox(this);
- string->SetPosition(10, 30);
- string->SetDimensions(150, 50);
- PString str = resource.GetConvertedText();
- for (PINDEX i = 0; i < str.GetLength(); i++)
- if (!isprint(str[i])) {
- str = resource.GetLiteralText();
- break;
- }
- string->SetText(str);
- SetDimensions(170, 90);
- ShowAll();
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing menus
- MenuEditor::MenuEditor(PMDIFrameWindow * parent,
- ResourceFile & file, MenuBar & res)
- : ResourceEditor(parent, IDS_MENU_EDITOR, file, res),
- resource(res)
- {
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing layouts
- LayoutEditor::LayoutEditor(PMDIFrameWindow * parent,
- ResourceFile & file, Layout & res)
- : ResourceEditor(parent, IDS_LAYOUT_EDITOR, file, res),
- resource(res)
- {
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing images
- PixelEditor::PixelEditor(PMDIFrameWindow * parent,
- ResourceFile & file, PixelResource & res)
- : ResourceEditor(parent, IDS_IMAGE_EDITOR, file, res),
- resource(res)
- {
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for editing arbitrary data resources
- DataEditor::DataEditor(PMDIFrameWindow * parent,
- ResourceFile & file, DataResource & res)
- : ResourceEditor(parent, IDS_DATA_EDITOR, file, res),
- resource(res)
- {
- }
- ///////////////////////////////////////////////////////////////////////////////
- // MDI child window class for listing errors
- ErrorWindow::ErrorWindow(PMDIFrameWindow * parent)
- : PMDIDocWindow(parent, PResourceString(IDS_ERROR_OUTPUT))
- {
- // Set up the frame windows title, icon and menu
- SetIcon(PIcon(IDI_APPICON));
- scroller = new PScroller(this);
- terminal = new PAnsiTerminal(scroller);
- terminal->SetVariableRowColumns(FALSE);
- terminal->SetRows(25);
- terminal->SetColumns(P_MAX_INDEX);
- terminal->SetReturnOnLineFeed(TRUE);
- terminal->SetWrapEOL(TRUE);
- channel.Open(*terminal);
- oldError = PErrorStream;
- PErrorStream = &channel;
- PRect parentBounds = parent->GetDimensions(TRUE);
- SetDimensions(10, parentBounds.Height()/4, TRUE);
- AutoAdjustBounds(parentBounds, AdjustBottom);
- ShowAll();
- }
- ErrorWindow::~ErrorWindow()
- {
- PErrorStream = oldError;
- ((MainWindow*)parent)->error = NULL;
- }
- void ErrorWindow::OnResize(const PDim & newSize, ResizeType type)
- {
- if (type != Iconic) {
- PRect bounds(newSize);
- scroller->AutoAdjustBounds(bounds);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Main MDI frame window class
- MainWindow::MainWindow(const PArgList & args)
- {
- error = NULL;
- // Set up the frame windows title, icon and menu
- SetTitle(PResourceString(IDS_TITLE));
- SetIcon(PIcon(IDI_APPICON));
- SetMenu(new MainMenu());
- ShowAll();
- Update();
- IncludePath[0] = "d:\Work\PWLib\common";
- IncludePath[1] = "d:\Work\PWLib\mswin\include";
- if (args.GetCount() == 0)
- OpenCmd();
- else {
- for (PINDEX i = 0; i < args.GetCount(); i++)
- OpenResFile(args[i]);
- }
- }
- void MainWindow::OnResize(const PDim & newSize, ResizeType type)
- {
- if (type != Iconic) {
- PRect bounds(newSize);
- // statusBar->AutoAdjustBounds(bounds, AdjustBottom);
- SetDocumentArea(bounds);
- }
- }
- void MainWindow::ExitCmd()
- {
- Close();
- }
- void MainWindow::OpenCmd()
- {
- POpenFileDialog dlg(this);
- dlg.AddFileType(".prc");
- if (dlg.RunModal())
- OpenResFile(dlg.GetFile());
- }
- void MainWindow::OpenResFile(const PString & filename)
- {
- PFilePath path = filename;
- if (path.GetType().IsEmpty())
- path += ".prc";
- // attempt to open the input file
- PTextFile prcfile = path;
- if (!prcfile.Open(PFile::ReadOnly)) {
- PSimpleDialog::Error(this, "Cannot open "" +
- prcfile.GetFilePath() + "" :" + prcfile.GetErrorText());
- return;
- }
- if (error == NULL)
- error = new ErrorWindow(this);
- ResourceFile * resfile = new ResourceFile(prcfile);
- if (resfile->Warnings() == 0 && resfile->Fatals() == 0)
- PError << "Portable resource compilation successful.n" << endl;
- else {
- PError << resfile->Warnings() << " warnings(s)" << endl;
- PError << resfile->Fatals() << " error(s)n" << endl;
- }
- if (resfile->Fatals() != 0)
- delete resfile;
- else {
- ResourceWindow * wnd = new ResourceWindow(this, path.GetTitle(), resfile);
- wnd->Activate();
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Application class
- #undef new
- PDECLARE_CLASS(PwreApp, PApplication)
- virtual void Main();
- virtual void OnAbout();
- };
- PwreApp App;
- void PwreApp::Main()
- {
- new MainWindow(GetArguments());
- PApplication::Main();
- }
- void PwreApp::OnAbout()
- {
- AboutDialog dlg(mainWindow);
- dlg.RunModal();
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Application class
- void StringResource::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW StringEditor(parent, *resfile, *this);
- }
- void MenuBar::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW MenuEditor(parent, *resfile, *this);
- }
- void Layout::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW LayoutEditor(parent, *resfile, *this);
- }
- void Icon::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW PixelEditor(parent, *resfile, *this);
- }
- void Cursor::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW PixelEditor(parent, *resfile, *this);
- }
- void Pattern::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW PixelEditor(parent, *resfile, *this);
- }
- void Image::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW PixelEditor(parent, *resfile, *this);
- }
- void DataResource::EditResource(MainWindow * parent, ResourceFile * resfile)
- {
- PNEW DataEditor(parent, *resfile, *this);
- }
- // EDIT.CXX